blob: 6e6ec229c780465974c1ce15bede42072b3ed3ac [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
Peter Zijlstra90eec102015-11-16 11:08:45 +01006 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040039#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070045#include <linux/bpf.h>
46#include <linux/filter.h>
Alexander Shishkin375637b2016-04-27 18:44:46 +030047#include <linux/namei.h>
48#include <linux/parser.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020049
Frederic Weisbecker76369132011-05-19 19:55:04 +020050#include "internal.h"
51
Ingo Molnarcdd6c482009-09-21 12:02:48 +020052#include <asm/irq_regs.h>
53
Peter Zijlstra272325c2015-04-15 11:41:58 +020054typedef int (*remote_function_f)(void *);
55
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010056struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020057 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020058 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020059 void *info;
60 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010061};
62
63static void remote_function(void *data)
64{
65 struct remote_function_call *tfc = data;
66 struct task_struct *p = tfc->p;
67
68 if (p) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +010069 /* -EAGAIN */
70 if (task_cpu(p) != smp_processor_id())
71 return;
72
73 /*
74 * Now that we're on right CPU with IRQs disabled, we can test
75 * if we hit the right task without races.
76 */
77
78 tfc->ret = -ESRCH; /* No such (running) process */
79 if (p != current)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010080 return;
81 }
82
83 tfc->ret = tfc->func(tfc->info);
84}
85
86/**
87 * task_function_call - call a function on the cpu on which a task runs
88 * @p: the task to evaluate
89 * @func: the function to be called
90 * @info: the function call argument
91 *
92 * Calls the function @func when the task is currently running. This might
93 * be on the current CPU, which just calls the function directly
94 *
95 * returns: @func return value, or
96 * -ESRCH - when the process isn't running
97 * -EAGAIN - when the process moved away
98 */
99static int
Peter Zijlstra272325c2015-04-15 11:41:58 +0200100task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100101{
102 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200103 .p = p,
104 .func = func,
105 .info = info,
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100106 .ret = -EAGAIN,
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100107 };
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100108 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100109
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100110 do {
111 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
112 if (!ret)
113 ret = data.ret;
114 } while (ret == -EAGAIN);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100115
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100116 return ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100117}
118
119/**
120 * cpu_function_call - call a function on the cpu
121 * @func: the function to be called
122 * @info: the function call argument
123 *
124 * Calls the function @func on the remote cpu.
125 *
126 * returns: @func return value or -ENXIO when the cpu is offline
127 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200128static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100129{
130 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200131 .p = NULL,
132 .func = func,
133 .info = info,
134 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100135 };
136
137 smp_call_function_single(cpu, remote_function, &data, 1);
138
139 return data.ret;
140}
141
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100142static inline struct perf_cpu_context *
143__get_cpu_context(struct perf_event_context *ctx)
144{
145 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
146}
147
148static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
149 struct perf_event_context *ctx)
150{
151 raw_spin_lock(&cpuctx->ctx.lock);
152 if (ctx)
153 raw_spin_lock(&ctx->lock);
154}
155
156static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
157 struct perf_event_context *ctx)
158{
159 if (ctx)
160 raw_spin_unlock(&ctx->lock);
161 raw_spin_unlock(&cpuctx->ctx.lock);
162}
163
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100164#define TASK_TOMBSTONE ((void *)-1L)
165
166static bool is_kernel_event(struct perf_event *event)
167{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100168 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100169}
170
Peter Zijlstra39a43642016-01-11 12:46:35 +0100171/*
172 * On task ctx scheduling...
173 *
174 * When !ctx->nr_events a task context will not be scheduled. This means
175 * we can disable the scheduler hooks (for performance) without leaving
176 * pending task ctx state.
177 *
178 * This however results in two special cases:
179 *
180 * - removing the last event from a task ctx; this is relatively straight
181 * forward and is done in __perf_remove_from_context.
182 *
183 * - adding the first event to a task ctx; this is tricky because we cannot
184 * rely on ctx->is_active and therefore cannot use event_function_call().
185 * See perf_install_in_context().
186 *
Peter Zijlstra39a43642016-01-11 12:46:35 +0100187 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
188 */
189
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100190typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
191 struct perf_event_context *, void *);
192
193struct event_function_struct {
194 struct perf_event *event;
195 event_f func;
196 void *data;
197};
198
199static int event_function(void *info)
200{
201 struct event_function_struct *efs = info;
202 struct perf_event *event = efs->event;
203 struct perf_event_context *ctx = event->ctx;
204 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
205 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100206 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100207
208 WARN_ON_ONCE(!irqs_disabled());
209
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100210 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100211 /*
212 * Since we do the IPI call without holding ctx->lock things can have
213 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100214 */
215 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100216 if (ctx->task != current) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100217 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100218 goto unlock;
219 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100220
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100221 /*
222 * We only use event_function_call() on established contexts,
223 * and event_function() is only ever called when active (or
224 * rather, we'll have bailed in task_function_call() or the
225 * above ctx->task != current test), therefore we must have
226 * ctx->is_active here.
227 */
228 WARN_ON_ONCE(!ctx->is_active);
229 /*
230 * And since we have ctx->is_active, cpuctx->task_ctx must
231 * match.
232 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100233 WARN_ON_ONCE(task_ctx != ctx);
234 } else {
235 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100236 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100237
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100238 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100239unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100240 perf_ctx_unlock(cpuctx, task_ctx);
241
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100242 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100243}
244
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100245static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100246{
247 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100248 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100249 struct event_function_struct efs = {
250 .event = event,
251 .func = func,
252 .data = data,
253 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100254
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100255 if (!event->parent) {
256 /*
257 * If this is a !child event, we must hold ctx::mutex to
258 * stabilize the the event->ctx relation. See
259 * perf_event_ctx_lock().
260 */
261 lockdep_assert_held(&ctx->mutex);
262 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100263
264 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100265 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100266 return;
267 }
268
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100269 if (task == TASK_TOMBSTONE)
270 return;
271
Peter Zijlstraa0963092016-02-24 18:45:50 +0100272again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100273 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100274 return;
275
276 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100277 /*
278 * Reload the task pointer, it might have been changed by
279 * a concurrent perf_event_context_sched_out().
280 */
281 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100282 if (task == TASK_TOMBSTONE) {
283 raw_spin_unlock_irq(&ctx->lock);
284 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100285 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100286 if (ctx->is_active) {
287 raw_spin_unlock_irq(&ctx->lock);
288 goto again;
289 }
290 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100291 raw_spin_unlock_irq(&ctx->lock);
292}
293
Peter Zijlstracca20942016-08-16 13:33:26 +0200294/*
295 * Similar to event_function_call() + event_function(), but hard assumes IRQs
296 * are already disabled and we're on the right CPU.
297 */
298static void event_function_local(struct perf_event *event, event_f func, void *data)
299{
300 struct perf_event_context *ctx = event->ctx;
301 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
302 struct task_struct *task = READ_ONCE(ctx->task);
303 struct perf_event_context *task_ctx = NULL;
304
305 WARN_ON_ONCE(!irqs_disabled());
306
307 if (task) {
308 if (task == TASK_TOMBSTONE)
309 return;
310
311 task_ctx = ctx;
312 }
313
314 perf_ctx_lock(cpuctx, task_ctx);
315
316 task = ctx->task;
317 if (task == TASK_TOMBSTONE)
318 goto unlock;
319
320 if (task) {
321 /*
322 * We must be either inactive or active and the right task,
323 * otherwise we're screwed, since we cannot IPI to somewhere
324 * else.
325 */
326 if (ctx->is_active) {
327 if (WARN_ON_ONCE(task != current))
328 goto unlock;
329
330 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
331 goto unlock;
332 }
333 } else {
334 WARN_ON_ONCE(&cpuctx->ctx != ctx);
335 }
336
337 func(event, cpuctx, ctx, data);
338unlock:
339 perf_ctx_unlock(cpuctx, task_ctx);
340}
341
Stephane Eraniane5d13672011-02-14 11:20:01 +0200342#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
343 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100344 PERF_FLAG_PID_CGROUP |\
345 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200346
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100347/*
348 * branch priv levels that need permission checks
349 */
350#define PERF_SAMPLE_BRANCH_PERM_PLM \
351 (PERF_SAMPLE_BRANCH_KERNEL |\
352 PERF_SAMPLE_BRANCH_HV)
353
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200354enum event_type_t {
355 EVENT_FLEXIBLE = 0x1,
356 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100357 EVENT_TIME = 0x4,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200358 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
359};
360
Stephane Eraniane5d13672011-02-14 11:20:01 +0200361/*
362 * perf_sched_events : >0 events exist
363 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
364 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100365
366static void perf_sched_delayed(struct work_struct *work);
367DEFINE_STATIC_KEY_FALSE(perf_sched_events);
368static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
369static DEFINE_MUTEX(perf_sched_mutex);
370static atomic_t perf_sched_count;
371
Stephane Eraniane5d13672011-02-14 11:20:01 +0200372static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500373static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Kan Liangf2fb6be2016-03-23 11:24:37 -0700374static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200375
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200376static atomic_t nr_mmap_events __read_mostly;
377static atomic_t nr_comm_events __read_mostly;
378static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200379static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300380static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200382static LIST_HEAD(pmus);
383static DEFINE_MUTEX(pmus_lock);
384static struct srcu_struct pmus_srcu;
385
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200386/*
387 * perf event paranoia level:
388 * -1 - not paranoid at all
389 * 0 - disallow raw tracepoint access for unpriv
390 * 1 - disallow cpu events for unpriv
391 * 2 - disallow kernel profiling for unpriv
392 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700393int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200394
Frederic Weisbecker20443382011-03-31 03:33:29 +0200395/* Minimum for 512 kiB + 1 user control page */
396int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200397
398/*
399 * max perf event sample rate
400 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700401#define DEFAULT_MAX_SAMPLE_RATE 100000
402#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
403#define DEFAULT_CPU_TIME_MAX_PERCENT 25
404
405int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
406
407static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
408static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
409
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200410static int perf_sample_allowed_ns __read_mostly =
411 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700412
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800413static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700414{
415 u64 tmp = perf_sample_period_ns;
416
417 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100418 tmp = div_u64(tmp, 100);
419 if (!tmp)
420 tmp = 1;
421
422 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700423}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100424
Stephane Eranian9e630202013-04-03 14:21:33 +0200425static int perf_rotate_context(struct perf_cpu_context *cpuctx);
426
Peter Zijlstra163ec432011-02-16 11:22:34 +0100427int perf_proc_update_handler(struct ctl_table *table, int write,
428 void __user *buffer, size_t *lenp,
429 loff_t *ppos)
430{
Knut Petersen723478c2013-09-25 14:29:37 +0200431 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100432
433 if (ret || !write)
434 return ret;
435
Kan Liangab7fdef2016-05-03 00:26:06 -0700436 /*
437 * If throttling is disabled don't allow the write:
438 */
439 if (sysctl_perf_cpu_time_max_percent == 100 ||
440 sysctl_perf_cpu_time_max_percent == 0)
441 return -EINVAL;
442
Peter Zijlstra163ec432011-02-16 11:22:34 +0100443 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700444 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
445 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100446
447 return 0;
448}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200449
Dave Hansen14c63f12013-06-21 08:51:36 -0700450int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
451
452int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
453 void __user *buffer, size_t *lenp,
454 loff_t *ppos)
455{
Tan Xiaojun0f8a75e2017-02-23 14:04:39 +0800456 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700457
458 if (ret || !write)
459 return ret;
460
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200461 if (sysctl_perf_cpu_time_max_percent == 100 ||
462 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100463 printk(KERN_WARNING
464 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
465 WRITE_ONCE(perf_sample_allowed_ns, 0);
466 } else {
467 update_perf_cpu_limits();
468 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700469
470 return 0;
471}
472
473/*
474 * perf samples are done in some very critical code paths (NMIs).
475 * If they take too much CPU time, the system can lock up and not
476 * get any real work done. This will drop the sample rate when
477 * we detect that events are taking too long.
478 */
479#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200480static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700481
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100482static u64 __report_avg;
483static u64 __report_allowed;
484
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100485static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700486{
David Ahern0d87d7e2016-08-01 13:49:29 -0700487 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100488 "perf: interrupt took too long (%lld > %lld), lowering "
489 "kernel.perf_event_max_sample_rate to %d\n",
490 __report_avg, __report_allowed,
491 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100492}
493
494static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
495
496void perf_sample_event_took(u64 sample_len_ns)
497{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100498 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
499 u64 running_len;
500 u64 avg_len;
501 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700502
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100503 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700504 return;
505
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100506 /* Decay the counter by 1 average sample. */
507 running_len = __this_cpu_read(running_sample_length);
508 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
509 running_len += sample_len_ns;
510 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700511
512 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100513 * Note: this will be biased artifically low until we have
514 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700515 * from having to maintain a count.
516 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100517 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
518 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700519 return;
520
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100521 __report_avg = avg_len;
522 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700523
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100524 /*
525 * Compute a throttle threshold 25% below the current duration.
526 */
527 avg_len += avg_len / 4;
528 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
529 if (avg_len < max)
530 max /= (u32)avg_len;
531 else
532 max = 1;
533
534 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
535 WRITE_ONCE(max_samples_per_tick, max);
536
537 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700538 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
539
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100540 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100541 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100542 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100543 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100544 sysctl_perf_event_sample_rate);
545 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700546}
547
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200548static atomic64_t perf_event_id;
549
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200550static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
551 enum event_type_t event_type);
552
553static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200554 enum event_type_t event_type,
555 struct task_struct *task);
556
557static void update_context_time(struct perf_event_context *ctx);
558static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200559
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200560void __weak perf_event_print_debug(void) { }
561
Matt Fleming84c79912010-10-03 21:41:13 +0100562extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200563{
Matt Fleming84c79912010-10-03 21:41:13 +0100564 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565}
566
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200567static inline u64 perf_clock(void)
568{
569 return local_clock();
570}
571
Peter Zijlstra34f43922015-02-20 14:05:38 +0100572static inline u64 perf_event_clock(struct perf_event *event)
573{
574 return event->clock();
575}
576
Stephane Eraniane5d13672011-02-14 11:20:01 +0200577#ifdef CONFIG_CGROUP_PERF
578
Stephane Eraniane5d13672011-02-14 11:20:01 +0200579static inline bool
580perf_cgroup_match(struct perf_event *event)
581{
582 struct perf_event_context *ctx = event->ctx;
583 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
584
Tejun Heoef824fa2013-04-08 19:00:38 -0700585 /* @event doesn't care about cgroup */
586 if (!event->cgrp)
587 return true;
588
589 /* wants specific cgroup scope but @cpuctx isn't associated with any */
590 if (!cpuctx->cgrp)
591 return false;
592
593 /*
594 * Cgroup scoping is recursive. An event enabled for a cgroup is
595 * also enabled for all its descendant cgroups. If @cpuctx's
596 * cgroup is a descendant of @event's (the test covers identity
597 * case), it's a match.
598 */
599 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
600 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200601}
602
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603static inline void perf_detach_cgroup(struct perf_event *event)
604{
Zefan Li4e2ba652014-09-19 16:53:14 +0800605 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200606 event->cgrp = NULL;
607}
608
609static inline int is_cgroup_event(struct perf_event *event)
610{
611 return event->cgrp != NULL;
612}
613
614static inline u64 perf_cgroup_event_time(struct perf_event *event)
615{
616 struct perf_cgroup_info *t;
617
618 t = per_cpu_ptr(event->cgrp->info, event->cpu);
619 return t->time;
620}
621
622static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
623{
624 struct perf_cgroup_info *info;
625 u64 now;
626
627 now = perf_clock();
628
629 info = this_cpu_ptr(cgrp->info);
630
631 info->time += now - info->timestamp;
632 info->timestamp = now;
633}
634
635static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
636{
Song Liud4271d82018-03-12 09:59:43 -0700637 struct perf_cgroup *cgrp = cpuctx->cgrp;
638 struct cgroup_subsys_state *css;
639
640 if (cgrp) {
641 for (css = &cgrp->css; css; css = css->parent) {
642 cgrp = container_of(css, struct perf_cgroup, css);
643 __update_cgrp_time(cgrp);
644 }
645 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200646}
647
648static inline void update_cgrp_time_from_event(struct perf_event *event)
649{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200650 struct perf_cgroup *cgrp;
651
Stephane Eraniane5d13672011-02-14 11:20:01 +0200652 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200653 * ensure we access cgroup data only when needed and
654 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200655 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200656 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200657 return;
658
Stephane Eranian614e4c42015-11-12 11:00:04 +0100659 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200660 /*
661 * Do not update time when cgroup is not active
662 */
663 if (cgrp == event->cgrp)
664 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200665}
666
667static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200668perf_cgroup_set_timestamp(struct task_struct *task,
669 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200670{
671 struct perf_cgroup *cgrp;
672 struct perf_cgroup_info *info;
Song Liud4271d82018-03-12 09:59:43 -0700673 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200674
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200675 /*
676 * ctx->lock held by caller
677 * ensure we do not access cgroup data
678 * unless we have the cgroup pinned (css_get)
679 */
680 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200681 return;
682
Stephane Eranian614e4c42015-11-12 11:00:04 +0100683 cgrp = perf_cgroup_from_task(task, ctx);
Song Liud4271d82018-03-12 09:59:43 -0700684
685 for (css = &cgrp->css; css; css = css->parent) {
686 cgrp = container_of(css, struct perf_cgroup, css);
687 info = this_cpu_ptr(cgrp->info);
688 info->timestamp = ctx->timestamp;
689 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200690}
691
692#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
693#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
694
695/*
696 * reschedule events based on the cgroup constraint of task.
697 *
698 * mode SWOUT : schedule out everything
699 * mode SWIN : schedule in based on cgroup for next
700 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800701static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200702{
703 struct perf_cpu_context *cpuctx;
704 struct pmu *pmu;
705 unsigned long flags;
706
707 /*
708 * disable interrupts to avoid geting nr_cgroup
709 * changes via __perf_event_disable(). Also
710 * avoids preemption.
711 */
712 local_irq_save(flags);
713
714 /*
715 * we reschedule only in the presence of cgroup
716 * constrained events.
717 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718
719 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200720 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200721 if (cpuctx->unique_pmu != pmu)
722 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200723
Stephane Eraniane5d13672011-02-14 11:20:01 +0200724 /*
725 * perf_cgroup_events says at least one
726 * context on this CPU has cgroup events.
727 *
728 * ctx->nr_cgroups reports the number of cgroup
729 * events for a context.
730 */
731 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200732 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
733 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200734
735 if (mode & PERF_CGROUP_SWOUT) {
736 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
737 /*
738 * must not be done before ctxswout due
739 * to event_filter_match() in event_sched_out()
740 */
741 cpuctx->cgrp = NULL;
742 }
743
744 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200745 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200746 /*
747 * set cgrp before ctxsw in to allow
748 * event_filter_match() to not have to pass
749 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100750 * we pass the cpuctx->ctx to perf_cgroup_from_task()
751 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200752 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100753 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200754 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
755 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200756 perf_pmu_enable(cpuctx->ctx.pmu);
757 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200758 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200759 }
760
Stephane Eraniane5d13672011-02-14 11:20:01 +0200761 local_irq_restore(flags);
762}
763
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200764static inline void perf_cgroup_sched_out(struct task_struct *task,
765 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200766{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200767 struct perf_cgroup *cgrp1;
768 struct perf_cgroup *cgrp2 = NULL;
769
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100770 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200771 /*
772 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100773 * we do not need to pass the ctx here because we know
774 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200775 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100776 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100777 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200778
779 /*
780 * only schedule out current cgroup events if we know
781 * that we are switching to a different cgroup. Otherwise,
782 * do no touch the cgroup events.
783 */
784 if (cgrp1 != cgrp2)
785 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100786
787 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200788}
789
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200790static inline void perf_cgroup_sched_in(struct task_struct *prev,
791 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200792{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200793 struct perf_cgroup *cgrp1;
794 struct perf_cgroup *cgrp2 = NULL;
795
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100796 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200797 /*
798 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100799 * we do not need to pass the ctx here because we know
800 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200801 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100802 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100803 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200804
805 /*
806 * only need to schedule in cgroup events if we are changing
807 * cgroup during ctxsw. Cgroup events were not scheduled
808 * out of ctxsw out if that was not the case.
809 */
810 if (cgrp1 != cgrp2)
811 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100812
813 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200814}
815
816static inline int perf_cgroup_connect(int fd, struct perf_event *event,
817 struct perf_event_attr *attr,
818 struct perf_event *group_leader)
819{
820 struct perf_cgroup *cgrp;
821 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400822 struct fd f = fdget(fd);
823 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200824
Al Viro2903ff02012-08-28 12:52:22 -0400825 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200826 return -EBADF;
827
Al Virob5830432014-10-31 01:22:04 -0400828 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400829 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800830 if (IS_ERR(css)) {
831 ret = PTR_ERR(css);
832 goto out;
833 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200834
835 cgrp = container_of(css, struct perf_cgroup, css);
836 event->cgrp = cgrp;
837
838 /*
839 * all events in a group must monitor
840 * the same cgroup because a task belongs
841 * to only one perf cgroup at a time
842 */
843 if (group_leader && group_leader->cgrp != cgrp) {
844 perf_detach_cgroup(event);
845 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200846 }
Li Zefan3db272c2011-03-03 14:25:37 +0800847out:
Al Viro2903ff02012-08-28 12:52:22 -0400848 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200849 return ret;
850}
851
852static inline void
853perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
854{
855 struct perf_cgroup_info *t;
856 t = per_cpu_ptr(event->cgrp->info, event->cpu);
857 event->shadow_ctx_time = now - t->timestamp;
858}
859
860static inline void
861perf_cgroup_defer_enabled(struct perf_event *event)
862{
863 /*
864 * when the current task's perf cgroup does not match
865 * the event's, we need to remember to call the
866 * perf_mark_enable() function the first time a task with
867 * a matching perf cgroup is scheduled in.
868 */
869 if (is_cgroup_event(event) && !perf_cgroup_match(event))
870 event->cgrp_defer_enabled = 1;
871}
872
873static inline void
874perf_cgroup_mark_enabled(struct perf_event *event,
875 struct perf_event_context *ctx)
876{
877 struct perf_event *sub;
878 u64 tstamp = perf_event_time(event);
879
880 if (!event->cgrp_defer_enabled)
881 return;
882
883 event->cgrp_defer_enabled = 0;
884
885 event->tstamp_enabled = tstamp - event->total_time_enabled;
886 list_for_each_entry(sub, &event->sibling_list, group_entry) {
887 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
888 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
889 sub->cgrp_defer_enabled = 0;
890 }
891 }
892}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700893
894/*
895 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
896 * cleared when last cgroup event is removed.
897 */
898static inline void
899list_update_cgroup_event(struct perf_event *event,
900 struct perf_event_context *ctx, bool add)
901{
902 struct perf_cpu_context *cpuctx;
903
904 if (!is_cgroup_event(event))
905 return;
906
907 if (add && ctx->nr_cgroups++)
908 return;
909 else if (!add && --ctx->nr_cgroups)
910 return;
911 /*
912 * Because cgroup events are always per-cpu events,
913 * this will always be called from the right CPU.
914 */
915 cpuctx = __get_cpu_context(ctx);
David Carrillo-Cisneros864c2352016-11-01 11:52:58 -0700916
David Carrillo-Cisneros8fc31ce2016-12-04 00:46:17 -0800917 /*
918 * cpuctx->cgrp is NULL until a cgroup event is sched in or
919 * ctx->nr_cgroup == 0 .
920 */
921 if (add && perf_cgroup_from_task(current, ctx) == event->cgrp)
922 cpuctx->cgrp = event->cgrp;
923 else if (!add)
924 cpuctx->cgrp = NULL;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700925}
926
Stephane Eraniane5d13672011-02-14 11:20:01 +0200927#else /* !CONFIG_CGROUP_PERF */
928
929static inline bool
930perf_cgroup_match(struct perf_event *event)
931{
932 return true;
933}
934
935static inline void perf_detach_cgroup(struct perf_event *event)
936{}
937
938static inline int is_cgroup_event(struct perf_event *event)
939{
940 return 0;
941}
942
943static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
944{
945 return 0;
946}
947
948static inline void update_cgrp_time_from_event(struct perf_event *event)
949{
950}
951
952static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
953{
954}
955
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200956static inline void perf_cgroup_sched_out(struct task_struct *task,
957 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200958{
959}
960
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200961static inline void perf_cgroup_sched_in(struct task_struct *prev,
962 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200963{
964}
965
966static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
967 struct perf_event_attr *attr,
968 struct perf_event *group_leader)
969{
970 return -EINVAL;
971}
972
973static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200974perf_cgroup_set_timestamp(struct task_struct *task,
975 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200976{
977}
978
979void
980perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
981{
982}
983
984static inline void
985perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
986{
987}
988
989static inline u64 perf_cgroup_event_time(struct perf_event *event)
990{
991 return 0;
992}
993
994static inline void
995perf_cgroup_defer_enabled(struct perf_event *event)
996{
997}
998
999static inline void
1000perf_cgroup_mark_enabled(struct perf_event *event,
1001 struct perf_event_context *ctx)
1002{
1003}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001004
1005static inline void
1006list_update_cgroup_event(struct perf_event *event,
1007 struct perf_event_context *ctx, bool add)
1008{
1009}
1010
Stephane Eraniane5d13672011-02-14 11:20:01 +02001011#endif
1012
Stephane Eranian9e630202013-04-03 14:21:33 +02001013/*
1014 * set default to be dependent on timer tick just
1015 * like original code
1016 */
1017#define PERF_CPU_HRTIMER (1000 / HZ)
1018/*
1019 * function must be called with interrupts disbled
1020 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001021static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001022{
1023 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +02001024 int rotations = 0;
1025
1026 WARN_ON(!irqs_disabled());
1027
1028 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001029 rotations = perf_rotate_context(cpuctx);
1030
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001031 raw_spin_lock(&cpuctx->hrtimer_lock);
1032 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001033 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001034 else
1035 cpuctx->hrtimer_active = 0;
1036 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001037
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001038 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001039}
1040
Peter Zijlstra272325c2015-04-15 11:41:58 +02001041static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001042{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001043 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001044 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001045 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001046
1047 /* no multiplexing needed for SW PMU */
1048 if (pmu->task_ctx_nr == perf_sw_context)
1049 return;
1050
Stephane Eranian62b85632013-04-03 14:21:34 +02001051 /*
1052 * check default is sane, if not set then force to
1053 * default interval (1/tick)
1054 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001055 interval = pmu->hrtimer_interval_ms;
1056 if (interval < 1)
1057 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001058
Peter Zijlstra272325c2015-04-15 11:41:58 +02001059 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001060
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001061 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1062 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001063 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001064}
1065
Peter Zijlstra272325c2015-04-15 11:41:58 +02001066static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001067{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001068 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001069 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001070 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001071
1072 /* not for SW PMU */
1073 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001074 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001075
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001076 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1077 if (!cpuctx->hrtimer_active) {
1078 cpuctx->hrtimer_active = 1;
1079 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1080 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1081 }
1082 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001083
Peter Zijlstra272325c2015-04-15 11:41:58 +02001084 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001085}
1086
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001087void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001088{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001089 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1090 if (!(*count)++)
1091 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001092}
1093
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001094void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001096 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1097 if (!--(*count))
1098 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099}
1100
Mark Rutland2fde4f92015-01-07 15:01:54 +00001101static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001102
1103/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001104 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1105 * perf_event_task_tick() are fully serialized because they're strictly cpu
1106 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1107 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001108 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001109static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001110{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001111 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001112
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001113 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001114
Mark Rutland2fde4f92015-01-07 15:01:54 +00001115 WARN_ON(!list_empty(&ctx->active_ctx_list));
1116
1117 list_add(&ctx->active_ctx_list, head);
1118}
1119
1120static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1121{
1122 WARN_ON(!irqs_disabled());
1123
1124 WARN_ON(list_empty(&ctx->active_ctx_list));
1125
1126 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001127}
1128
1129static void get_ctx(struct perf_event_context *ctx)
1130{
1131 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1132}
1133
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001134static void free_ctx(struct rcu_head *head)
1135{
1136 struct perf_event_context *ctx;
1137
1138 ctx = container_of(head, struct perf_event_context, rcu_head);
1139 kfree(ctx->task_ctx_data);
1140 kfree(ctx);
1141}
1142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143static void put_ctx(struct perf_event_context *ctx)
1144{
1145 if (atomic_dec_and_test(&ctx->refcount)) {
1146 if (ctx->parent_ctx)
1147 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001148 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001150 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151 }
1152}
1153
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001154/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001155 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1156 * perf_pmu_migrate_context() we need some magic.
1157 *
1158 * Those places that change perf_event::ctx will hold both
1159 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1160 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001161 * Lock ordering is by mutex address. There are two other sites where
1162 * perf_event_context::mutex nests and those are:
1163 *
1164 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001165 * perf_event_exit_event()
1166 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001167 *
1168 * - perf_event_init_context() [ parent, 0 ]
1169 * inherit_task_group()
1170 * inherit_group()
1171 * inherit_event()
1172 * perf_event_alloc()
1173 * perf_init_event()
1174 * perf_try_init_event() [ child , 1 ]
1175 *
1176 * While it appears there is an obvious deadlock here -- the parent and child
1177 * nesting levels are inverted between the two. This is in fact safe because
1178 * life-time rules separate them. That is an exiting task cannot fork, and a
1179 * spawning task cannot (yet) exit.
1180 *
1181 * But remember that that these are parent<->child context relations, and
1182 * migration does not affect children, therefore these two orderings should not
1183 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001184 *
1185 * The change in perf_event::ctx does not affect children (as claimed above)
1186 * because the sys_perf_event_open() case will install a new event and break
1187 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1188 * concerned with cpuctx and that doesn't have children.
1189 *
1190 * The places that change perf_event::ctx will issue:
1191 *
1192 * perf_remove_from_context();
1193 * synchronize_rcu();
1194 * perf_install_in_context();
1195 *
1196 * to affect the change. The remove_from_context() + synchronize_rcu() should
1197 * quiesce the event, after which we can install it in the new location. This
1198 * means that only external vectors (perf_fops, prctl) can perturb the event
1199 * while in transit. Therefore all such accessors should also acquire
1200 * perf_event_context::mutex to serialize against this.
1201 *
1202 * However; because event->ctx can change while we're waiting to acquire
1203 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1204 * function.
1205 *
1206 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001207 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001208 * task_struct::perf_event_mutex
1209 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001210 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001211 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001212 * perf_event::mmap_mutex
1213 * mmap_sem
1214 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001215static struct perf_event_context *
1216perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001217{
1218 struct perf_event_context *ctx;
1219
1220again:
1221 rcu_read_lock();
1222 ctx = ACCESS_ONCE(event->ctx);
1223 if (!atomic_inc_not_zero(&ctx->refcount)) {
1224 rcu_read_unlock();
1225 goto again;
1226 }
1227 rcu_read_unlock();
1228
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001229 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001230 if (event->ctx != ctx) {
1231 mutex_unlock(&ctx->mutex);
1232 put_ctx(ctx);
1233 goto again;
1234 }
1235
1236 return ctx;
1237}
1238
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001239static inline struct perf_event_context *
1240perf_event_ctx_lock(struct perf_event *event)
1241{
1242 return perf_event_ctx_lock_nested(event, 0);
1243}
1244
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001245static void perf_event_ctx_unlock(struct perf_event *event,
1246 struct perf_event_context *ctx)
1247{
1248 mutex_unlock(&ctx->mutex);
1249 put_ctx(ctx);
1250}
1251
1252/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001253 * This must be done under the ctx->lock, such as to serialize against
1254 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1255 * calling scheduler related locks and ctx->lock nests inside those.
1256 */
1257static __must_check struct perf_event_context *
1258unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001260 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1261
1262 lockdep_assert_held(&ctx->lock);
1263
1264 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001266 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001267
1268 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001269}
1270
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001271static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1272{
1273 /*
1274 * only top level events have the pid namespace they were created in
1275 */
1276 if (event->parent)
1277 event = event->parent;
1278
1279 return task_tgid_nr_ns(p, event->ns);
1280}
1281
1282static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1283{
1284 /*
1285 * only top level events have the pid namespace they were created in
1286 */
1287 if (event->parent)
1288 event = event->parent;
1289
1290 return task_pid_nr_ns(p, event->ns);
1291}
1292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001293/*
1294 * If we inherit events we want to return the parent event id
1295 * to userspace.
1296 */
1297static u64 primary_event_id(struct perf_event *event)
1298{
1299 u64 id = event->id;
1300
1301 if (event->parent)
1302 id = event->parent->id;
1303
1304 return id;
1305}
1306
1307/*
1308 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001309 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001310 * This has to cope with with the fact that until it is locked,
1311 * the context could get moved to another task.
1312 */
1313static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001314perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315{
1316 struct perf_event_context *ctx;
1317
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001318retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001319 /*
1320 * One of the few rules of preemptible RCU is that one cannot do
1321 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001322 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001323 * rcu_read_unlock_special().
1324 *
1325 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001326 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001327 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001328 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001329 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001330 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331 if (ctx) {
1332 /*
1333 * If this context is a clone of another, it might
1334 * get swapped for another underneath us by
1335 * perf_event_task_sched_out, though the
1336 * rcu_read_lock() protects us from any context
1337 * getting freed. Lock the context and check if it
1338 * got swapped before we could get the lock, and retry
1339 * if so. If we locked the right context, then it
1340 * can't get swapped on us any more.
1341 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001342 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001343 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001344 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001345 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001346 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347 goto retry;
1348 }
1349
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001350 if (ctx->task == TASK_TOMBSTONE ||
1351 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001352 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001353 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001354 } else {
1355 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 }
1357 }
1358 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001359 if (!ctx)
1360 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 return ctx;
1362}
1363
1364/*
1365 * Get the context for a task and increment its pin_count so it
1366 * can't get swapped to another task. This also increments its
1367 * reference count so that the context can't get freed.
1368 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001369static struct perf_event_context *
1370perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371{
1372 struct perf_event_context *ctx;
1373 unsigned long flags;
1374
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001375 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376 if (ctx) {
1377 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001378 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379 }
1380 return ctx;
1381}
1382
1383static void perf_unpin_context(struct perf_event_context *ctx)
1384{
1385 unsigned long flags;
1386
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001387 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001389 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001390}
1391
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001392/*
1393 * Update the record of the current time in a context.
1394 */
1395static void update_context_time(struct perf_event_context *ctx)
1396{
1397 u64 now = perf_clock();
1398
1399 ctx->time += now - ctx->timestamp;
1400 ctx->timestamp = now;
1401}
1402
Stephane Eranian41587552011-01-03 18:20:01 +02001403static u64 perf_event_time(struct perf_event *event)
1404{
1405 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001406
1407 if (is_cgroup_event(event))
1408 return perf_cgroup_event_time(event);
1409
Stephane Eranian41587552011-01-03 18:20:01 +02001410 return ctx ? ctx->time : 0;
1411}
1412
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001413/*
1414 * Update the total_time_enabled and total_time_running fields for a event.
1415 */
1416static void update_event_times(struct perf_event *event)
1417{
1418 struct perf_event_context *ctx = event->ctx;
1419 u64 run_end;
1420
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001421 lockdep_assert_held(&ctx->lock);
1422
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001423 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1424 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1425 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001426
Stephane Eraniane5d13672011-02-14 11:20:01 +02001427 /*
1428 * in cgroup mode, time_enabled represents
1429 * the time the event was enabled AND active
1430 * tasks were in the monitored cgroup. This is
1431 * independent of the activity of the context as
1432 * there may be a mix of cgroup and non-cgroup events.
1433 *
1434 * That is why we treat cgroup events differently
1435 * here.
1436 */
1437 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001438 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001439 else if (ctx->is_active)
1440 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001441 else
1442 run_end = event->tstamp_stopped;
1443
1444 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001445
1446 if (event->state == PERF_EVENT_STATE_INACTIVE)
1447 run_end = event->tstamp_stopped;
1448 else
Stephane Eranian41587552011-01-03 18:20:01 +02001449 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001450
1451 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001452
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001453}
1454
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001455/*
1456 * Update total_time_enabled and total_time_running for all events in a group.
1457 */
1458static void update_group_times(struct perf_event *leader)
1459{
1460 struct perf_event *event;
1461
1462 update_event_times(leader);
1463 list_for_each_entry(event, &leader->sibling_list, group_entry)
1464 update_event_times(event);
1465}
1466
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001467static struct list_head *
1468ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1469{
1470 if (event->attr.pinned)
1471 return &ctx->pinned_groups;
1472 else
1473 return &ctx->flexible_groups;
1474}
1475
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001476/*
1477 * Add a event from the lists for its context.
1478 * Must be called with ctx->mutex and ctx->lock held.
1479 */
1480static void
1481list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1482{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001483 lockdep_assert_held(&ctx->lock);
1484
Peter Zijlstra8a495422010-05-27 15:47:49 +02001485 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1486 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487
1488 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001489 * If we're a stand alone event or group leader, we go to the context
1490 * list, group events are kept attached to the group so that
1491 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001493 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001494 struct list_head *list;
1495
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001496 event->group_caps = event->event_caps;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001497
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001498 list = ctx_group_list(event, ctx);
1499 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001500 }
1501
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001502 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504 list_add_rcu(&event->event_entry, &ctx->event_list);
1505 ctx->nr_events++;
1506 if (event->attr.inherit_stat)
1507 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001508
1509 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001510}
1511
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001512/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001513 * Initialize event state based on the perf_event_attr::disabled.
1514 */
1515static inline void perf_event__state_init(struct perf_event *event)
1516{
1517 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1518 PERF_EVENT_STATE_INACTIVE;
1519}
1520
Peter Zijlstraa7239682015-09-09 19:06:33 +02001521static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001522{
1523 int entry = sizeof(u64); /* value */
1524 int size = 0;
1525 int nr = 1;
1526
1527 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1528 size += sizeof(u64);
1529
1530 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1531 size += sizeof(u64);
1532
1533 if (event->attr.read_format & PERF_FORMAT_ID)
1534 entry += sizeof(u64);
1535
1536 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001537 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001538 size += sizeof(u64);
1539 }
1540
1541 size += entry * nr;
1542 event->read_size = size;
1543}
1544
Peter Zijlstraa7239682015-09-09 19:06:33 +02001545static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001546{
1547 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001548 u16 size = 0;
1549
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001550 if (sample_type & PERF_SAMPLE_IP)
1551 size += sizeof(data->ip);
1552
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001553 if (sample_type & PERF_SAMPLE_ADDR)
1554 size += sizeof(data->addr);
1555
1556 if (sample_type & PERF_SAMPLE_PERIOD)
1557 size += sizeof(data->period);
1558
Andi Kleenc3feedf2013-01-24 16:10:28 +01001559 if (sample_type & PERF_SAMPLE_WEIGHT)
1560 size += sizeof(data->weight);
1561
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001562 if (sample_type & PERF_SAMPLE_READ)
1563 size += event->read_size;
1564
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001565 if (sample_type & PERF_SAMPLE_DATA_SRC)
1566 size += sizeof(data->data_src.val);
1567
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001568 if (sample_type & PERF_SAMPLE_TRANSACTION)
1569 size += sizeof(data->txn);
1570
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001571 event->header_size = size;
1572}
1573
Peter Zijlstraa7239682015-09-09 19:06:33 +02001574/*
1575 * Called at perf_event creation and when events are attached/detached from a
1576 * group.
1577 */
1578static void perf_event__header_size(struct perf_event *event)
1579{
1580 __perf_event_read_size(event,
1581 event->group_leader->nr_siblings);
1582 __perf_event_header_size(event, event->attr.sample_type);
1583}
1584
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001585static void perf_event__id_header_size(struct perf_event *event)
1586{
1587 struct perf_sample_data *data;
1588 u64 sample_type = event->attr.sample_type;
1589 u16 size = 0;
1590
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001591 if (sample_type & PERF_SAMPLE_TID)
1592 size += sizeof(data->tid_entry);
1593
1594 if (sample_type & PERF_SAMPLE_TIME)
1595 size += sizeof(data->time);
1596
Adrian Hunterff3d5272013-08-27 11:23:07 +03001597 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1598 size += sizeof(data->id);
1599
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001600 if (sample_type & PERF_SAMPLE_ID)
1601 size += sizeof(data->id);
1602
1603 if (sample_type & PERF_SAMPLE_STREAM_ID)
1604 size += sizeof(data->stream_id);
1605
1606 if (sample_type & PERF_SAMPLE_CPU)
1607 size += sizeof(data->cpu_entry);
1608
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001609 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001610}
1611
Peter Zijlstraa7239682015-09-09 19:06:33 +02001612static bool perf_event_validate_size(struct perf_event *event)
1613{
1614 /*
1615 * The values computed here will be over-written when we actually
1616 * attach the event.
1617 */
1618 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1619 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1620 perf_event__id_header_size(event);
1621
1622 /*
1623 * Sum the lot; should not exceed the 64k limit we have on records.
1624 * Conservative limit to allow for callchains and other variable fields.
1625 */
1626 if (event->read_size + event->header_size +
1627 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1628 return false;
1629
1630 return true;
1631}
1632
Peter Zijlstra8a495422010-05-27 15:47:49 +02001633static void perf_group_attach(struct perf_event *event)
1634{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001635 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001636
Peter Zijlstra3996a912017-01-26 16:39:55 +01001637 lockdep_assert_held(&event->ctx->lock);
1638
Peter Zijlstra74c33372010-10-15 11:40:29 +02001639 /*
1640 * We can have double attach due to group movement in perf_event_open.
1641 */
1642 if (event->attach_state & PERF_ATTACH_GROUP)
1643 return;
1644
Peter Zijlstra8a495422010-05-27 15:47:49 +02001645 event->attach_state |= PERF_ATTACH_GROUP;
1646
1647 if (group_leader == event)
1648 return;
1649
Peter Zijlstra652884f2015-01-23 11:20:10 +01001650 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1651
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001652 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001653
1654 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1655 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001656
1657 perf_event__header_size(group_leader);
1658
1659 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1660 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001661}
1662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663/*
1664 * Remove a event from the lists for its context.
1665 * Must be called with ctx->mutex and ctx->lock held.
1666 */
1667static void
1668list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1669{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001670 WARN_ON_ONCE(event->ctx != ctx);
1671 lockdep_assert_held(&ctx->lock);
1672
Peter Zijlstra8a495422010-05-27 15:47:49 +02001673 /*
1674 * We can have double detach due to exit/hot-unplug + close.
1675 */
1676 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001677 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001678
1679 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1680
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001681 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001682
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683 ctx->nr_events--;
1684 if (event->attr.inherit_stat)
1685 ctx->nr_stat--;
1686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001687 list_del_rcu(&event->event_entry);
1688
Peter Zijlstra8a495422010-05-27 15:47:49 +02001689 if (event->group_leader == event)
1690 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001692 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001693
1694 /*
1695 * If event was in error state, then keep it
1696 * that way, otherwise bogus counts will be
1697 * returned on read(). The only way to get out
1698 * of error state is by explicit re-enabling
1699 * of the event
1700 */
1701 if (event->state > PERF_EVENT_STATE_OFF)
1702 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001703
1704 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001705}
1706
Peter Zijlstra8a495422010-05-27 15:47:49 +02001707static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001708{
1709 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001710 struct list_head *list = NULL;
1711
Peter Zijlstra3996a912017-01-26 16:39:55 +01001712 lockdep_assert_held(&event->ctx->lock);
1713
Peter Zijlstra8a495422010-05-27 15:47:49 +02001714 /*
1715 * We can have double detach due to exit/hot-unplug + close.
1716 */
1717 if (!(event->attach_state & PERF_ATTACH_GROUP))
1718 return;
1719
1720 event->attach_state &= ~PERF_ATTACH_GROUP;
1721
1722 /*
1723 * If this is a sibling, remove it from its group.
1724 */
1725 if (event->group_leader != event) {
1726 list_del_init(&event->group_entry);
1727 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001728 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001729 }
1730
1731 if (!list_empty(&event->group_entry))
1732 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734 /*
1735 * If this was a group event with sibling events then
1736 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001737 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 */
1739 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001740 if (list)
1741 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001743
1744 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001745 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001746
1747 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001749
1750out:
1751 perf_event__header_size(event->group_leader);
1752
1753 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1754 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001755}
1756
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001757static bool is_orphaned_event(struct perf_event *event)
1758{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001759 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001760}
1761
Mark Rutland2c81a642016-06-14 16:10:41 +01001762static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001763{
1764 struct pmu *pmu = event->pmu;
1765 return pmu->filter_match ? pmu->filter_match(event) : 1;
1766}
1767
Mark Rutland2c81a642016-06-14 16:10:41 +01001768/*
1769 * Check whether we should attempt to schedule an event group based on
1770 * PMU-specific filtering. An event group can consist of HW and SW events,
1771 * potentially with a SW leader, so we must check all the filters, to
1772 * determine whether a group is schedulable:
1773 */
1774static inline int pmu_filter_match(struct perf_event *event)
1775{
1776 struct perf_event *child;
1777
1778 if (!__pmu_filter_match(event))
1779 return 0;
1780
1781 list_for_each_entry(child, &event->sibling_list, group_entry) {
1782 if (!__pmu_filter_match(child))
1783 return 0;
1784 }
1785
1786 return 1;
1787}
1788
Stephane Eranianfa66f072010-08-26 16:40:01 +02001789static inline int
1790event_filter_match(struct perf_event *event)
1791{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001792 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1793 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001794}
1795
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001796static void
1797event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001798 struct perf_cpu_context *cpuctx,
1799 struct perf_event_context *ctx)
1800{
Stephane Eranian41587552011-01-03 18:20:01 +02001801 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001802 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001803
1804 WARN_ON_ONCE(event->ctx != ctx);
1805 lockdep_assert_held(&ctx->lock);
1806
Stephane Eranianfa66f072010-08-26 16:40:01 +02001807 /*
1808 * An event which could not be activated because of
1809 * filter mismatch still needs to have its timings
1810 * maintained, otherwise bogus information is return
1811 * via read() for time_enabled, time_running:
1812 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001813 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1814 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001815 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001816 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001817 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001818 }
1819
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001821 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822
Alexander Shishkin44377272013-12-16 14:17:36 +02001823 perf_pmu_disable(event->pmu);
1824
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001825 event->tstamp_stopped = tstamp;
1826 event->pmu->del(event, 0);
1827 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828 event->state = PERF_EVENT_STATE_INACTIVE;
1829 if (event->pending_disable) {
1830 event->pending_disable = 0;
1831 event->state = PERF_EVENT_STATE_OFF;
1832 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833
1834 if (!is_software_event(event))
1835 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001836 if (!--ctx->nr_active)
1837 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001838 if (event->attr.freq && event->attr.sample_freq)
1839 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001840 if (event->attr.exclusive || !cpuctx->active_oncpu)
1841 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001842
1843 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844}
1845
1846static void
1847group_sched_out(struct perf_event *group_event,
1848 struct perf_cpu_context *cpuctx,
1849 struct perf_event_context *ctx)
1850{
1851 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001852 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853
Mark Rutland3f005e72016-07-26 18:12:21 +01001854 perf_pmu_disable(ctx->pmu);
1855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856 event_sched_out(group_event, cpuctx, ctx);
1857
1858 /*
1859 * Schedule out siblings (if any):
1860 */
1861 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1862 event_sched_out(event, cpuctx, ctx);
1863
Mark Rutland3f005e72016-07-26 18:12:21 +01001864 perf_pmu_enable(ctx->pmu);
1865
Stephane Eranianfa66f072010-08-26 16:40:01 +02001866 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867 cpuctx->exclusive = 0;
1868}
1869
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001870#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001871
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872/*
1873 * Cross CPU call to remove a performance event
1874 *
1875 * We disable the event on the hardware level first. After that we
1876 * remove it from the context list.
1877 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001878static void
1879__perf_remove_from_context(struct perf_event *event,
1880 struct perf_cpu_context *cpuctx,
1881 struct perf_event_context *ctx,
1882 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001884 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001885
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001886 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001887 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001888 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001890
Peter Zijlstra39a43642016-01-11 12:46:35 +01001891 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001892 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001893 if (ctx->task) {
1894 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1895 cpuctx->task_ctx = NULL;
1896 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001897 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001898}
1899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900/*
1901 * Remove the event from a task's (or a CPU's) list of events.
1902 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903 * If event->ctx is a cloned context, callers must make sure that
1904 * every task struct that event->ctx->task could possibly point to
1905 * remains valid. This is OK when called from perf_release since
1906 * that only calls us on the top-level context, which can't be a clone.
1907 * When called from perf_event_exit_task, it's OK because the
1908 * context has been detached from its task.
1909 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001910static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911{
Peter Zijlstra3996a912017-01-26 16:39:55 +01001912 struct perf_event_context *ctx = event->ctx;
1913
1914 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001915
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001916 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstra3996a912017-01-26 16:39:55 +01001917
1918 /*
1919 * The above event_function_call() can NO-OP when it hits
1920 * TASK_TOMBSTONE. In that case we must already have been detached
1921 * from the context (by perf_event_exit_event()) but the grouping
1922 * might still be in-tact.
1923 */
1924 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1925 if ((flags & DETACH_GROUP) &&
1926 (event->attach_state & PERF_ATTACH_GROUP)) {
1927 /*
1928 * Since in that case we cannot possibly be scheduled, simply
1929 * detach now.
1930 */
1931 raw_spin_lock_irq(&ctx->lock);
1932 perf_group_detach(event);
1933 raw_spin_unlock_irq(&ctx->lock);
1934 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001935}
1936
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001937/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 * Cross CPU call to disable a performance event
1939 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001940static void __perf_event_disable(struct perf_event *event,
1941 struct perf_cpu_context *cpuctx,
1942 struct perf_event_context *ctx,
1943 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001944{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001945 if (event->state < PERF_EVENT_STATE_INACTIVE)
1946 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001947
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001948 update_context_time(ctx);
1949 update_cgrp_time_from_event(event);
1950 update_group_times(event);
1951 if (event == event->group_leader)
1952 group_sched_out(event, cpuctx, ctx);
1953 else
1954 event_sched_out(event, cpuctx, ctx);
1955 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001956}
1957
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958/*
1959 * Disable a event.
1960 *
1961 * If event->ctx is a cloned context, callers must make sure that
1962 * every task struct that event->ctx->task could possibly point to
1963 * remains valid. This condition is satisifed when called through
1964 * perf_event_for_each_child or perf_event_for_each because they
1965 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001966 * goes to exit will block in perf_event_exit_event().
1967 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 * When called from perf_pending_event it's OK because event->ctx
1969 * is the current context on this CPU and preemption is disabled,
1970 * hence we can't get into perf_event_task_sched_out for this context.
1971 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001972static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973{
1974 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001976 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001977 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001978 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001979 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001981 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001982
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001983 event_function_call(event, __perf_event_disable, NULL);
1984}
1985
1986void perf_event_disable_local(struct perf_event *event)
1987{
1988 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001990
1991/*
1992 * Strictly speaking kernel users cannot create groups and therefore this
1993 * interface does not need the perf_event_ctx_lock() magic.
1994 */
1995void perf_event_disable(struct perf_event *event)
1996{
1997 struct perf_event_context *ctx;
1998
1999 ctx = perf_event_ctx_lock(event);
2000 _perf_event_disable(event);
2001 perf_event_ctx_unlock(event, ctx);
2002}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002003EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002005void perf_event_disable_inatomic(struct perf_event *event)
2006{
2007 event->pending_disable = 1;
2008 irq_work_queue(&event->pending);
2009}
2010
Stephane Eraniane5d13672011-02-14 11:20:01 +02002011static void perf_set_shadow_time(struct perf_event *event,
2012 struct perf_event_context *ctx,
2013 u64 tstamp)
2014{
2015 /*
2016 * use the correct time source for the time snapshot
2017 *
2018 * We could get by without this by leveraging the
2019 * fact that to get to this function, the caller
2020 * has most likely already called update_context_time()
2021 * and update_cgrp_time_xx() and thus both timestamp
2022 * are identical (or very close). Given that tstamp is,
2023 * already adjusted for cgroup, we could say that:
2024 * tstamp - ctx->timestamp
2025 * is equivalent to
2026 * tstamp - cgrp->timestamp.
2027 *
2028 * Then, in perf_output_read(), the calculation would
2029 * work with no changes because:
2030 * - event is guaranteed scheduled in
2031 * - no scheduled out in between
2032 * - thus the timestamp would be the same
2033 *
2034 * But this is a bit hairy.
2035 *
2036 * So instead, we have an explicit cgroup call to remain
2037 * within the time time source all along. We believe it
2038 * is cleaner and simpler to understand.
2039 */
2040 if (is_cgroup_event(event))
2041 perf_cgroup_set_shadow_time(event, tstamp);
2042 else
2043 event->shadow_ctx_time = tstamp - ctx->timestamp;
2044}
2045
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002046#define MAX_INTERRUPTS (~0ULL)
2047
2048static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002049static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002050
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002051static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002052event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002054 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055{
Stephane Eranian41587552011-01-03 18:20:01 +02002056 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02002057 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002058
Peter Zijlstra63342412014-05-05 11:49:16 +02002059 lockdep_assert_held(&ctx->lock);
2060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061 if (event->state <= PERF_EVENT_STATE_OFF)
2062 return 0;
2063
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002064 WRITE_ONCE(event->oncpu, smp_processor_id());
2065 /*
2066 * Order event::oncpu write to happen before the ACTIVE state
2067 * is visible.
2068 */
2069 smp_wmb();
2070 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002071
2072 /*
2073 * Unthrottle events, since we scheduled we might have missed several
2074 * ticks already, also for a heavily scheduling task there is little
2075 * guarantee it'll get a tick in a timely manner.
2076 */
2077 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2078 perf_log_throttle(event, 1);
2079 event->hw.interrupts = 0;
2080 }
2081
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 /*
2083 * The new state must be visible before we turn it on in the hardware:
2084 */
2085 smp_wmb();
2086
Alexander Shishkin44377272013-12-16 14:17:36 +02002087 perf_pmu_disable(event->pmu);
2088
Shaohua Li72f669c2015-02-05 15:55:31 -08002089 perf_set_shadow_time(event, ctx, tstamp);
2090
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002091 perf_log_itrace_start(event);
2092
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002093 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094 event->state = PERF_EVENT_STATE_INACTIVE;
2095 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002096 ret = -EAGAIN;
2097 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 }
2099
Peter Zijlstra00a29162015-07-27 10:35:07 +02002100 event->tstamp_running += tstamp - event->tstamp_stopped;
2101
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002102 if (!is_software_event(event))
2103 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002104 if (!ctx->nr_active++)
2105 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002106 if (event->attr.freq && event->attr.sample_freq)
2107 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002108
2109 if (event->attr.exclusive)
2110 cpuctx->exclusive = 1;
2111
Alexander Shishkin44377272013-12-16 14:17:36 +02002112out:
2113 perf_pmu_enable(event->pmu);
2114
2115 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116}
2117
2118static int
2119group_sched_in(struct perf_event *group_event,
2120 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002121 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002122{
Lin Ming6bde9b62010-04-23 13:56:00 +08002123 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002124 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002125 u64 now = ctx->time;
2126 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127
2128 if (group_event->state == PERF_EVENT_STATE_OFF)
2129 return 0;
2130
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002131 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002132
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002133 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002134 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002135 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002137 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138
2139 /*
2140 * Schedule in siblings as one group (if any):
2141 */
2142 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002143 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002144 partial_group = event;
2145 goto group_error;
2146 }
2147 }
2148
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002149 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002150 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002152group_error:
2153 /*
2154 * Groups can be scheduled in as one unit only, so undo any
2155 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002156 * The events up to the failed event are scheduled out normally,
2157 * tstamp_stopped will be updated.
2158 *
2159 * The failed events and the remaining siblings need to have
2160 * their timings updated as if they had gone thru event_sched_in()
2161 * and event_sched_out(). This is required to get consistent timings
2162 * across the group. This also takes care of the case where the group
2163 * could never be scheduled by ensuring tstamp_stopped is set to mark
2164 * the time the event was actually stopped, such that time delta
2165 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166 */
2167 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2168 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002169 simulate = true;
2170
2171 if (simulate) {
2172 event->tstamp_running += now - event->tstamp_stopped;
2173 event->tstamp_stopped = now;
2174 } else {
2175 event_sched_out(event, cpuctx, ctx);
2176 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002178 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002179
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002180 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002181
Peter Zijlstra272325c2015-04-15 11:41:58 +02002182 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184 return -EAGAIN;
2185}
2186
2187/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188 * Work out whether we can put this event group on the CPU now.
2189 */
2190static int group_can_go_on(struct perf_event *event,
2191 struct perf_cpu_context *cpuctx,
2192 int can_add_hw)
2193{
2194 /*
2195 * Groups consisting entirely of software events can always go on.
2196 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002197 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002198 return 1;
2199 /*
2200 * If an exclusive group is already on, no other hardware
2201 * events can go on.
2202 */
2203 if (cpuctx->exclusive)
2204 return 0;
2205 /*
2206 * If this group is exclusive and there are already
2207 * events on the CPU, it can't go on.
2208 */
2209 if (event->attr.exclusive && cpuctx->active_oncpu)
2210 return 0;
2211 /*
2212 * Otherwise, try to add it if all previous groups were able
2213 * to go on.
2214 */
2215 return can_add_hw;
2216}
2217
2218static void add_event_to_ctx(struct perf_event *event,
2219 struct perf_event_context *ctx)
2220{
Stephane Eranian41587552011-01-03 18:20:01 +02002221 u64 tstamp = perf_event_time(event);
2222
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002224 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002225 event->tstamp_enabled = tstamp;
2226 event->tstamp_running = tstamp;
2227 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228}
2229
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002230static void ctx_sched_out(struct perf_event_context *ctx,
2231 struct perf_cpu_context *cpuctx,
2232 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002233static void
2234ctx_sched_in(struct perf_event_context *ctx,
2235 struct perf_cpu_context *cpuctx,
2236 enum event_type_t event_type,
2237 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002238
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002239static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2240 struct perf_event_context *ctx)
2241{
2242 if (!cpuctx->task_ctx)
2243 return;
2244
2245 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2246 return;
2247
2248 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2249}
2250
Peter Zijlstradce58552011-04-09 21:17:46 +02002251static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2252 struct perf_event_context *ctx,
2253 struct task_struct *task)
2254{
2255 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2256 if (ctx)
2257 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2258 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2259 if (ctx)
2260 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2261}
2262
Peter Zijlstra3e349502016-01-08 10:01:18 +01002263static void ctx_resched(struct perf_cpu_context *cpuctx,
2264 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002265{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002266 perf_pmu_disable(cpuctx->ctx.pmu);
2267 if (task_ctx)
2268 task_ctx_sched_out(cpuctx, task_ctx);
2269 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2270 perf_event_sched_in(cpuctx, task_ctx, current);
2271 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002272}
2273
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002274/*
2275 * Cross CPU call to install and enable a performance event
2276 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002277 * Very similar to remote_function() + event_function() but cannot assume that
2278 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002280static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002282 struct perf_event *event = info;
2283 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002284 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002285 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra1c686332016-12-09 14:59:00 +01002286 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002287 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002288
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002289 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002290 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002291 raw_spin_lock(&ctx->lock);
2292 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002293
Peter Zijlstra1c686332016-12-09 14:59:00 +01002294 reprogram = (ctx->task == current);
2295
2296 /*
2297 * If the task is running, it must be running on this CPU,
2298 * otherwise we cannot reprogram things.
2299 *
2300 * If its not running, we don't care, ctx->lock will
2301 * serialize against it becoming runnable.
2302 */
2303 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002304 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002305 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002306 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307
Peter Zijlstra1c686332016-12-09 14:59:00 +01002308 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002309 } else if (task_ctx) {
2310 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002311 }
2312
Peter Zijlstra1c686332016-12-09 14:59:00 +01002313 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002314 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2315 add_event_to_ctx(event, ctx);
2316 ctx_resched(cpuctx, task_ctx);
2317 } else {
2318 add_event_to_ctx(event, ctx);
2319 }
2320
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002321unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002322 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002323
Peter Zijlstraa0963092016-02-24 18:45:50 +01002324 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325}
2326
2327/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002328 * Attach a performance event to a context.
2329 *
2330 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002331 */
2332static void
2333perf_install_in_context(struct perf_event_context *ctx,
2334 struct perf_event *event,
2335 int cpu)
2336{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002337 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002338
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002339 lockdep_assert_held(&ctx->mutex);
2340
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002341 if (event->cpu != -1)
2342 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002343
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002344 /*
2345 * Ensures that if we can observe event->ctx, both the event and ctx
2346 * will be 'complete'. See perf_iterate_sb_cpu().
2347 */
2348 smp_store_release(&event->ctx, ctx);
2349
Peter Zijlstraa0963092016-02-24 18:45:50 +01002350 if (!task) {
2351 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002352 return;
2353 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002354
Peter Zijlstraa0963092016-02-24 18:45:50 +01002355 /*
2356 * Should not happen, we validate the ctx is still alive before calling.
2357 */
2358 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2359 return;
2360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002361 /*
2362 * Installing events is tricky because we cannot rely on ctx->is_active
2363 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra1c686332016-12-09 14:59:00 +01002364 *
2365 * Instead we use task_curr(), which tells us if the task is running.
2366 * However, since we use task_curr() outside of rq::lock, we can race
2367 * against the actual state. This means the result can be wrong.
2368 *
2369 * If we get a false positive, we retry, this is harmless.
2370 *
2371 * If we get a false negative, things are complicated. If we are after
2372 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2373 * value must be correct. If we're before, it doesn't matter since
2374 * perf_event_context_sched_in() will program the counter.
2375 *
2376 * However, this hinges on the remote context switch having observed
2377 * our task->perf_event_ctxp[] store, such that it will in fact take
2378 * ctx::lock in perf_event_context_sched_in().
2379 *
2380 * We do this by task_function_call(), if the IPI fails to hit the task
2381 * we know any future context switch of task must see the
2382 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002384
Peter Zijlstraa0963092016-02-24 18:45:50 +01002385 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002386 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2387 * task_cpu() load, such that if the IPI then does not find the task
2388 * running, a future context switch of that task must observe the
2389 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002390 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002391 smp_mb();
2392again:
2393 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002394 return;
2395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002396 raw_spin_lock_irq(&ctx->lock);
2397 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002398 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2399 /*
2400 * Cannot happen because we already checked above (which also
2401 * cannot happen), and we hold ctx->mutex, which serializes us
2402 * against perf_event_exit_task_context().
2403 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002404 raw_spin_unlock_irq(&ctx->lock);
2405 return;
2406 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002407 /*
Peter Zijlstra1c686332016-12-09 14:59:00 +01002408 * If the task is not running, ctx->lock will avoid it becoming so,
2409 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002410 */
Peter Zijlstra1c686332016-12-09 14:59:00 +01002411 if (task_curr(task)) {
2412 raw_spin_unlock_irq(&ctx->lock);
2413 goto again;
2414 }
2415 add_event_to_ctx(event, ctx);
2416 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417}
2418
2419/*
2420 * Put a event into inactive state and update time fields.
2421 * Enabling the leader of a group effectively enables all
2422 * the group members that aren't explicitly disabled, so we
2423 * have to update their ->tstamp_enabled also.
2424 * Note: this works for group members as well as group leaders
2425 * since the non-leader members' sibling_lists will be empty.
2426 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002427static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428{
2429 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002430 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431
2432 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002433 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002434 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002435 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2436 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002437 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438}
2439
2440/*
2441 * Cross CPU call to enable a performance event
2442 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002443static void __perf_event_enable(struct perf_event *event,
2444 struct perf_cpu_context *cpuctx,
2445 struct perf_event_context *ctx,
2446 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002447{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002449 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002451 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2452 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002453 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002455 if (ctx->is_active)
2456 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2457
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002458 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002460 if (!ctx->is_active)
2461 return;
2462
Stephane Eraniane5d13672011-02-14 11:20:01 +02002463 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002464 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002465 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002466 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002467 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002469
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470 /*
2471 * If the event is in a group and isn't the group leader,
2472 * then don't put it on unless the group is on.
2473 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002474 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2475 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002476 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002477 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002478
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002479 task_ctx = cpuctx->task_ctx;
2480 if (ctx->task)
2481 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002483 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002484}
2485
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486/*
2487 * Enable a event.
2488 *
2489 * If event->ctx is a cloned context, callers must make sure that
2490 * every task struct that event->ctx->task could possibly point to
2491 * remains valid. This condition is satisfied when called through
2492 * perf_event_for_each_child or perf_event_for_each as described
2493 * for perf_event_disable.
2494 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002495static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002496{
2497 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002498
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002499 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002500 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2501 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002502 raw_spin_unlock_irq(&ctx->lock);
2503 return;
2504 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505
2506 /*
2507 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002508 *
2509 * That way, if we see the event in error state below, we know that it
2510 * has gone back into error state, as distinct from the task having
2511 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002512 */
2513 if (event->state == PERF_EVENT_STATE_ERROR)
2514 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002515 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002516
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002517 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002519
2520/*
2521 * See perf_event_disable();
2522 */
2523void perf_event_enable(struct perf_event *event)
2524{
2525 struct perf_event_context *ctx;
2526
2527 ctx = perf_event_ctx_lock(event);
2528 _perf_event_enable(event);
2529 perf_event_ctx_unlock(event, ctx);
2530}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002531EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002532
Alexander Shishkin375637b2016-04-27 18:44:46 +03002533struct stop_event_data {
2534 struct perf_event *event;
2535 unsigned int restart;
2536};
2537
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002538static int __perf_event_stop(void *info)
2539{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002540 struct stop_event_data *sd = info;
2541 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002542
Alexander Shishkin375637b2016-04-27 18:44:46 +03002543 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002544 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2545 return 0;
2546
2547 /* matches smp_wmb() in event_sched_in() */
2548 smp_rmb();
2549
2550 /*
2551 * There is a window with interrupts enabled before we get here,
2552 * so we need to check again lest we try to stop another CPU's event.
2553 */
2554 if (READ_ONCE(event->oncpu) != smp_processor_id())
2555 return -EAGAIN;
2556
2557 event->pmu->stop(event, PERF_EF_UPDATE);
2558
Alexander Shishkin375637b2016-04-27 18:44:46 +03002559 /*
2560 * May race with the actual stop (through perf_pmu_output_stop()),
2561 * but it is only used for events with AUX ring buffer, and such
2562 * events will refuse to restart because of rb::aux_mmap_count==0,
2563 * see comments in perf_aux_output_begin().
2564 *
2565 * Since this is happening on a event-local CPU, no trace is lost
2566 * while restarting.
2567 */
2568 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01002569 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03002570
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002571 return 0;
2572}
2573
Alexander Shishkin767ae082016-09-06 16:23:49 +03002574static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03002575{
2576 struct stop_event_data sd = {
2577 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03002578 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03002579 };
2580 int ret = 0;
2581
2582 do {
2583 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2584 return 0;
2585
2586 /* matches smp_wmb() in event_sched_in() */
2587 smp_rmb();
2588
2589 /*
2590 * We only want to restart ACTIVE events, so if the event goes
2591 * inactive here (event->oncpu==-1), there's nothing more to do;
2592 * fall through with ret==-ENXIO.
2593 */
2594 ret = cpu_function_call(READ_ONCE(event->oncpu),
2595 __perf_event_stop, &sd);
2596 } while (ret == -EAGAIN);
2597
2598 return ret;
2599}
2600
2601/*
2602 * In order to contain the amount of racy and tricky in the address filter
2603 * configuration management, it is a two part process:
2604 *
2605 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2606 * we update the addresses of corresponding vmas in
2607 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2608 * (p2) when an event is scheduled in (pmu::add), it calls
2609 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2610 * if the generation has changed since the previous call.
2611 *
2612 * If (p1) happens while the event is active, we restart it to force (p2).
2613 *
2614 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2615 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2616 * ioctl;
2617 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2618 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2619 * for reading;
2620 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2621 * of exec.
2622 */
2623void perf_event_addr_filters_sync(struct perf_event *event)
2624{
2625 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2626
2627 if (!has_addr_filter(event))
2628 return;
2629
2630 raw_spin_lock(&ifh->lock);
2631 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2632 event->pmu->addr_filters_sync(event);
2633 event->hw.addr_filters_gen = event->addr_filters_gen;
2634 }
2635 raw_spin_unlock(&ifh->lock);
2636}
2637EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2638
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002639static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002640{
2641 /*
2642 * not supported on inherited events
2643 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002644 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645 return -EINVAL;
2646
2647 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002648 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002649
2650 return 0;
2651}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002652
2653/*
2654 * See perf_event_disable()
2655 */
2656int perf_event_refresh(struct perf_event *event, int refresh)
2657{
2658 struct perf_event_context *ctx;
2659 int ret;
2660
2661 ctx = perf_event_ctx_lock(event);
2662 ret = _perf_event_refresh(event, refresh);
2663 perf_event_ctx_unlock(event, ctx);
2664
2665 return ret;
2666}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002667EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002668
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002669static void ctx_sched_out(struct perf_event_context *ctx,
2670 struct perf_cpu_context *cpuctx,
2671 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002672{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002673 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002674 struct perf_event *event;
2675
2676 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677
Peter Zijlstra39a43642016-01-11 12:46:35 +01002678 if (likely(!ctx->nr_events)) {
2679 /*
2680 * See __perf_remove_from_context().
2681 */
2682 WARN_ON_ONCE(ctx->is_active);
2683 if (ctx->task)
2684 WARN_ON_ONCE(cpuctx->task_ctx);
2685 return;
2686 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002687
Peter Zijlstradb24d332011-04-09 21:17:45 +02002688 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002689 if (!(ctx->is_active & EVENT_ALL))
2690 ctx->is_active = 0;
2691
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002692 if (ctx->task) {
2693 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2694 if (!ctx->is_active)
2695 cpuctx->task_ctx = NULL;
2696 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002697
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002698 /*
2699 * Always update time if it was set; not only when it changes.
2700 * Otherwise we can 'forget' to update time for any but the last
2701 * context we sched out. For example:
2702 *
2703 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2704 * ctx_sched_out(.event_type = EVENT_PINNED)
2705 *
2706 * would only update time for the pinned events.
2707 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002708 if (is_active & EVENT_TIME) {
2709 /* update (and stop) ctx time */
2710 update_context_time(ctx);
2711 update_cgrp_time_from_cpuctx(cpuctx);
2712 }
2713
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002714 is_active ^= ctx->is_active; /* changed bits */
2715
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002716 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002717 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002718
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002719 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002720 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002721 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2722 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002723 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002724
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002725 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002726 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002727 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002728 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002729 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730}
2731
2732/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002733 * Test whether two contexts are equivalent, i.e. whether they have both been
2734 * cloned from the same version of the same context.
2735 *
2736 * Equivalence is measured using a generation number in the context that is
2737 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2738 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739 */
2740static int context_equiv(struct perf_event_context *ctx1,
2741 struct perf_event_context *ctx2)
2742{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002743 lockdep_assert_held(&ctx1->lock);
2744 lockdep_assert_held(&ctx2->lock);
2745
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002746 /* Pinning disables the swap optimization */
2747 if (ctx1->pin_count || ctx2->pin_count)
2748 return 0;
2749
2750 /* If ctx1 is the parent of ctx2 */
2751 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2752 return 1;
2753
2754 /* If ctx2 is the parent of ctx1 */
2755 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2756 return 1;
2757
2758 /*
2759 * If ctx1 and ctx2 have the same parent; we flatten the parent
2760 * hierarchy, see perf_event_init_context().
2761 */
2762 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2763 ctx1->parent_gen == ctx2->parent_gen)
2764 return 1;
2765
2766 /* Unmatched */
2767 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002768}
2769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770static void __perf_event_sync_stat(struct perf_event *event,
2771 struct perf_event *next_event)
2772{
2773 u64 value;
2774
2775 if (!event->attr.inherit_stat)
2776 return;
2777
2778 /*
2779 * Update the event value, we cannot use perf_event_read()
2780 * because we're in the middle of a context switch and have IRQs
2781 * disabled, which upsets smp_call_function_single(), however
2782 * we know the event must be on the current CPU, therefore we
2783 * don't need to use it.
2784 */
2785 switch (event->state) {
2786 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002787 event->pmu->read(event);
2788 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002789
2790 case PERF_EVENT_STATE_INACTIVE:
2791 update_event_times(event);
2792 break;
2793
2794 default:
2795 break;
2796 }
2797
2798 /*
2799 * In order to keep per-task stats reliable we need to flip the event
2800 * values when we flip the contexts.
2801 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002802 value = local64_read(&next_event->count);
2803 value = local64_xchg(&event->count, value);
2804 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805
2806 swap(event->total_time_enabled, next_event->total_time_enabled);
2807 swap(event->total_time_running, next_event->total_time_running);
2808
2809 /*
2810 * Since we swizzled the values, update the user visible data too.
2811 */
2812 perf_event_update_userpage(event);
2813 perf_event_update_userpage(next_event);
2814}
2815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002816static void perf_event_sync_stat(struct perf_event_context *ctx,
2817 struct perf_event_context *next_ctx)
2818{
2819 struct perf_event *event, *next_event;
2820
2821 if (!ctx->nr_stat)
2822 return;
2823
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002824 update_context_time(ctx);
2825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002826 event = list_first_entry(&ctx->event_list,
2827 struct perf_event, event_entry);
2828
2829 next_event = list_first_entry(&next_ctx->event_list,
2830 struct perf_event, event_entry);
2831
2832 while (&event->event_entry != &ctx->event_list &&
2833 &next_event->event_entry != &next_ctx->event_list) {
2834
2835 __perf_event_sync_stat(event, next_event);
2836
2837 event = list_next_entry(event, event_entry);
2838 next_event = list_next_entry(next_event, event_entry);
2839 }
2840}
2841
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002842static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2843 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002844{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002845 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002847 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002848 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849 int do_switch = 1;
2850
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002851 if (likely(!ctx))
2852 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002853
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002854 cpuctx = __get_cpu_context(ctx);
2855 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002856 return;
2857
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002858 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002859 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002860 if (!next_ctx)
2861 goto unlock;
2862
2863 parent = rcu_dereference(ctx->parent_ctx);
2864 next_parent = rcu_dereference(next_ctx->parent_ctx);
2865
2866 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002867 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002868 goto unlock;
2869
2870 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002871 /*
2872 * Looks like the two contexts are clones, so we might be
2873 * able to optimize the context switch. We lock both
2874 * contexts and check that they are clones under the
2875 * lock (including re-checking that neither has been
2876 * uncloned in the meantime). It doesn't matter which
2877 * order we take the locks because no other cpu could
2878 * be trying to lock both of these tasks.
2879 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002880 raw_spin_lock(&ctx->lock);
2881 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002882 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002883 WRITE_ONCE(ctx->task, next);
2884 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002885
2886 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2887
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002888 /*
2889 * RCU_INIT_POINTER here is safe because we've not
2890 * modified the ctx and the above modification of
2891 * ctx->task and ctx->task_ctx_data are immaterial
2892 * since those values are always verified under
2893 * ctx->lock which we're now holding.
2894 */
2895 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2896 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898 do_switch = 0;
2899
2900 perf_event_sync_stat(ctx, next_ctx);
2901 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002902 raw_spin_unlock(&next_ctx->lock);
2903 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002905unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002906 rcu_read_unlock();
2907
2908 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002909 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002910 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002911 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912 }
2913}
2914
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002915static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2916
Yan, Zhengba532502014-11-04 21:55:58 -05002917void perf_sched_cb_dec(struct pmu *pmu)
2918{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002919 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2920
Yan, Zhengba532502014-11-04 21:55:58 -05002921 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002922
2923 if (!--cpuctx->sched_cb_usage)
2924 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002925}
2926
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002927
Yan, Zhengba532502014-11-04 21:55:58 -05002928void perf_sched_cb_inc(struct pmu *pmu)
2929{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002930 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2931
2932 if (!cpuctx->sched_cb_usage++)
2933 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2934
Yan, Zhengba532502014-11-04 21:55:58 -05002935 this_cpu_inc(perf_sched_cb_usages);
2936}
2937
2938/*
2939 * This function provides the context switch callback to the lower code
2940 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002941 *
2942 * This callback is relevant even to per-cpu events; for example multi event
2943 * PEBS requires this to provide PID/TID information. This requires we flush
2944 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002945 */
2946static void perf_pmu_sched_task(struct task_struct *prev,
2947 struct task_struct *next,
2948 bool sched_in)
2949{
2950 struct perf_cpu_context *cpuctx;
2951 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002952
2953 if (prev == next)
2954 return;
2955
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002956 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2957 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002958
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002959 if (WARN_ON_ONCE(!pmu->sched_task))
2960 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002961
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002962 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2963 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002964
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002965 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002966
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002967 perf_pmu_enable(pmu);
2968 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002969 }
Yan, Zhengba532502014-11-04 21:55:58 -05002970}
2971
Adrian Hunter45ac1402015-07-21 12:44:02 +03002972static void perf_event_switch(struct task_struct *task,
2973 struct task_struct *next_prev, bool sched_in);
2974
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002975#define for_each_task_context_nr(ctxn) \
2976 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2977
2978/*
2979 * Called from scheduler to remove the events of the current task,
2980 * with interrupts disabled.
2981 *
2982 * We stop each event and update the event value in event->count.
2983 *
2984 * This does not protect us against NMI, but disable()
2985 * sets the disabled bit in the control field of event _before_
2986 * accessing the event control register. If a NMI hits, then it will
2987 * not restart the event.
2988 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002989void __perf_event_task_sched_out(struct task_struct *task,
2990 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002991{
2992 int ctxn;
2993
Yan, Zhengba532502014-11-04 21:55:58 -05002994 if (__this_cpu_read(perf_sched_cb_usages))
2995 perf_pmu_sched_task(task, next, false);
2996
Adrian Hunter45ac1402015-07-21 12:44:02 +03002997 if (atomic_read(&nr_switch_events))
2998 perf_event_switch(task, next, false);
2999
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003000 for_each_task_context_nr(ctxn)
3001 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003002
3003 /*
3004 * if cgroup events exist on this CPU, then we need
3005 * to check if we have to switch out PMU state.
3006 * cgroup event are system-wide mode only
3007 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003008 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003009 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003010}
3011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012/*
3013 * Called with IRQs disabled
3014 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003015static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3016 enum event_type_t event_type)
3017{
3018 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019}
3020
3021static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003022ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003023 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024{
3025 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003027 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
3028 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02003030 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003031 continue;
3032
Stephane Eraniane5d13672011-02-14 11:20:01 +02003033 /* may need to reset tstamp_enabled */
3034 if (is_cgroup_event(event))
3035 perf_cgroup_mark_enabled(event, ctx);
3036
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003037 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01003038 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003039
3040 /*
3041 * If this pinned group hasn't been scheduled,
3042 * put it in error state.
3043 */
3044 if (event->state == PERF_EVENT_STATE_INACTIVE) {
3045 update_group_times(event);
3046 event->state = PERF_EVENT_STATE_ERROR;
3047 }
3048 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003049}
3050
3051static void
3052ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003053 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003054{
3055 struct perf_event *event;
3056 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003058 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
3059 /* Ignore events in OFF or ERROR state */
3060 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062 /*
3063 * Listen to the 'cpu' scheduling filter constraint
3064 * of events:
3065 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02003066 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003067 continue;
3068
Stephane Eraniane5d13672011-02-14 11:20:01 +02003069 /* may need to reset tstamp_enabled */
3070 if (is_cgroup_event(event))
3071 perf_cgroup_mark_enabled(event, ctx);
3072
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003073 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01003074 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003075 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003076 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003078}
3079
3080static void
3081ctx_sched_in(struct perf_event_context *ctx,
3082 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003083 enum event_type_t event_type,
3084 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003085{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003086 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003087 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003088
Peter Zijlstrac994d612016-01-08 09:20:23 +01003089 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003090
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003091 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003092 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003093
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003094 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003095 if (ctx->task) {
3096 if (!is_active)
3097 cpuctx->task_ctx = ctx;
3098 else
3099 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3100 }
3101
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003102 is_active ^= ctx->is_active; /* changed bits */
3103
3104 if (is_active & EVENT_TIME) {
3105 /* start ctx time */
3106 now = perf_clock();
3107 ctx->timestamp = now;
3108 perf_cgroup_set_timestamp(task, ctx);
3109 }
3110
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003111 /*
3112 * First go through the list and put on any pinned groups
3113 * in order to give them the best chance of going on.
3114 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003115 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003116 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003117
3118 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003119 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003120 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003121}
3122
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003123static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003124 enum event_type_t event_type,
3125 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003126{
3127 struct perf_event_context *ctx = &cpuctx->ctx;
3128
Stephane Eraniane5d13672011-02-14 11:20:01 +02003129 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003130}
3131
Stephane Eraniane5d13672011-02-14 11:20:01 +02003132static void perf_event_context_sched_in(struct perf_event_context *ctx,
3133 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003134{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003135 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003137 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003138 if (cpuctx->task_ctx == ctx)
3139 return;
3140
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003141 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003142 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003143 /*
3144 * We want to keep the following priority order:
3145 * cpu pinned (that don't need to move), task pinned,
3146 * cpu flexible, task flexible.
3147 */
3148 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003149 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003150 perf_pmu_enable(ctx->pmu);
3151 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003152}
3153
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003154/*
3155 * Called from scheduler to add the events of the current task
3156 * with interrupts disabled.
3157 *
3158 * We restore the event value and then enable it.
3159 *
3160 * This does not protect us against NMI, but enable()
3161 * sets the enabled bit in the control field of event _before_
3162 * accessing the event control register. If a NMI hits, then it will
3163 * keep the event running.
3164 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003165void __perf_event_task_sched_in(struct task_struct *prev,
3166 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003167{
3168 struct perf_event_context *ctx;
3169 int ctxn;
3170
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003171 /*
3172 * If cgroup events exist on this CPU, then we need to check if we have
3173 * to switch in PMU state; cgroup event are system-wide mode only.
3174 *
3175 * Since cgroup events are CPU events, we must schedule these in before
3176 * we schedule in the task events.
3177 */
3178 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3179 perf_cgroup_sched_in(prev, task);
3180
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003181 for_each_task_context_nr(ctxn) {
3182 ctx = task->perf_event_ctxp[ctxn];
3183 if (likely(!ctx))
3184 continue;
3185
Stephane Eraniane5d13672011-02-14 11:20:01 +02003186 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003187 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003188
Adrian Hunter45ac1402015-07-21 12:44:02 +03003189 if (atomic_read(&nr_switch_events))
3190 perf_event_switch(task, prev, true);
3191
Yan, Zhengba532502014-11-04 21:55:58 -05003192 if (__this_cpu_read(perf_sched_cb_usages))
3193 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003194}
3195
Peter Zijlstraabd50712010-01-26 18:50:16 +01003196static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3197{
3198 u64 frequency = event->attr.sample_freq;
3199 u64 sec = NSEC_PER_SEC;
3200 u64 divisor, dividend;
3201
3202 int count_fls, nsec_fls, frequency_fls, sec_fls;
3203
3204 count_fls = fls64(count);
3205 nsec_fls = fls64(nsec);
3206 frequency_fls = fls64(frequency);
3207 sec_fls = 30;
3208
3209 /*
3210 * We got @count in @nsec, with a target of sample_freq HZ
3211 * the target period becomes:
3212 *
3213 * @count * 10^9
3214 * period = -------------------
3215 * @nsec * sample_freq
3216 *
3217 */
3218
3219 /*
3220 * Reduce accuracy by one bit such that @a and @b converge
3221 * to a similar magnitude.
3222 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003223#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003224do { \
3225 if (a##_fls > b##_fls) { \
3226 a >>= 1; \
3227 a##_fls--; \
3228 } else { \
3229 b >>= 1; \
3230 b##_fls--; \
3231 } \
3232} while (0)
3233
3234 /*
3235 * Reduce accuracy until either term fits in a u64, then proceed with
3236 * the other, so that finally we can do a u64/u64 division.
3237 */
3238 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3239 REDUCE_FLS(nsec, frequency);
3240 REDUCE_FLS(sec, count);
3241 }
3242
3243 if (count_fls + sec_fls > 64) {
3244 divisor = nsec * frequency;
3245
3246 while (count_fls + sec_fls > 64) {
3247 REDUCE_FLS(count, sec);
3248 divisor >>= 1;
3249 }
3250
3251 dividend = count * sec;
3252 } else {
3253 dividend = count * sec;
3254
3255 while (nsec_fls + frequency_fls > 64) {
3256 REDUCE_FLS(nsec, frequency);
3257 dividend >>= 1;
3258 }
3259
3260 divisor = nsec * frequency;
3261 }
3262
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003263 if (!divisor)
3264 return dividend;
3265
Peter Zijlstraabd50712010-01-26 18:50:16 +01003266 return div64_u64(dividend, divisor);
3267}
3268
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003269static DEFINE_PER_CPU(int, perf_throttled_count);
3270static DEFINE_PER_CPU(u64, perf_throttled_seq);
3271
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003272static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273{
3274 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003275 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003276 s64 delta;
3277
Peter Zijlstraabd50712010-01-26 18:50:16 +01003278 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279
3280 delta = (s64)(period - hwc->sample_period);
3281 delta = (delta + 7) / 8; /* low pass filter */
3282
3283 sample_period = hwc->sample_period + delta;
3284
3285 if (!sample_period)
3286 sample_period = 1;
3287
3288 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003289
Peter Zijlstrae7850592010-05-21 14:43:08 +02003290 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003291 if (disable)
3292 event->pmu->stop(event, PERF_EF_UPDATE);
3293
Peter Zijlstrae7850592010-05-21 14:43:08 +02003294 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003295
3296 if (disable)
3297 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003298 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299}
3300
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003301/*
3302 * combine freq adjustment with unthrottling to avoid two passes over the
3303 * events. At the same time, make sure, having freq events does not change
3304 * the rate of unthrottling as that would introduce bias.
3305 */
3306static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3307 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308{
3309 struct perf_event *event;
3310 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003311 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003312 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003314 /*
3315 * only need to iterate over all events iff:
3316 * - context have events in frequency mode (needs freq adjust)
3317 * - there are events to unthrottle on this cpu
3318 */
3319 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003320 return;
3321
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003322 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003323 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003324
Paul Mackerras03541f82009-10-14 16:58:03 +11003325 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326 if (event->state != PERF_EVENT_STATE_ACTIVE)
3327 continue;
3328
Stephane Eranian5632ab12011-01-03 18:20:01 +02003329 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003330 continue;
3331
Alexander Shishkin44377272013-12-16 14:17:36 +02003332 perf_pmu_disable(event->pmu);
3333
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334 hwc = &event->hw;
3335
Jiri Olsaae23bff2013-08-24 16:45:54 +02003336 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003337 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003339 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 }
3341
3342 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003343 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003345 /*
3346 * stop the event and update event->count
3347 */
3348 event->pmu->stop(event, PERF_EF_UPDATE);
3349
Peter Zijlstrae7850592010-05-21 14:43:08 +02003350 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003351 delta = now - hwc->freq_count_stamp;
3352 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003354 /*
3355 * restart the event
3356 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003357 * we have stopped the event so tell that
3358 * to perf_adjust_period() to avoid stopping it
3359 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003360 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003361 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003362 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003363
3364 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003365 next:
3366 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003368
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003369 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003370 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371}
3372
3373/*
3374 * Round-robin a context's events:
3375 */
3376static void rotate_ctx(struct perf_event_context *ctx)
3377{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003378 /*
3379 * Rotate the first entry last of non-pinned groups. Rotation might be
3380 * disabled by the inheritance code.
3381 */
3382 if (!ctx->rotate_disable)
3383 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384}
3385
Stephane Eranian9e630202013-04-03 14:21:33 +02003386static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003388 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003389 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003391 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003392 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3393 rotate = 1;
3394 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003396 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003397 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003398 if (ctx->nr_events != ctx->nr_active)
3399 rotate = 1;
3400 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003402 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003403 goto done;
3404
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003405 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003406 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003408 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3409 if (ctx)
3410 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003411
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003412 rotate_ctx(&cpuctx->ctx);
3413 if (ctx)
3414 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003415
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003416 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003417
3418 perf_pmu_enable(cpuctx->ctx.pmu);
3419 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003420done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003421
3422 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003423}
3424
3425void perf_event_task_tick(void)
3426{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003427 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3428 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003429 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003430
3431 WARN_ON(!irqs_disabled());
3432
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003433 __this_cpu_inc(perf_throttled_seq);
3434 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003435 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003436
Mark Rutland2fde4f92015-01-07 15:01:54 +00003437 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003438 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003439}
3440
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003441static int event_enable_on_exec(struct perf_event *event,
3442 struct perf_event_context *ctx)
3443{
3444 if (!event->attr.enable_on_exec)
3445 return 0;
3446
3447 event->attr.enable_on_exec = 0;
3448 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3449 return 0;
3450
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003451 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003452
3453 return 1;
3454}
3455
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003456/*
3457 * Enable all of a task's events that have been marked enable-on-exec.
3458 * This expects task == current.
3459 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003460static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003462 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003463 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 struct perf_event *event;
3465 unsigned long flags;
3466 int enabled = 0;
3467
3468 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003469 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003470 if (!ctx || !ctx->nr_events)
3471 goto out;
3472
Peter Zijlstra3e349502016-01-08 10:01:18 +01003473 cpuctx = __get_cpu_context(ctx);
3474 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003475 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003476 list_for_each_entry(event, &ctx->event_list, event_entry)
3477 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478
3479 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003480 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003481 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003482 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003483 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003484 ctx_resched(cpuctx, ctx);
3485 }
3486 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003487
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003488out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003489 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003490
3491 if (clone_ctx)
3492 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003493}
3494
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003495struct perf_read_data {
3496 struct perf_event *event;
3497 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003498 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003499};
3500
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003501static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003502{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003503 u16 local_pkg, event_pkg;
3504
3505 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003506 int local_cpu = smp_processor_id();
3507
3508 event_pkg = topology_physical_package_id(event_cpu);
3509 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003510
3511 if (event_pkg == local_pkg)
3512 return local_cpu;
3513 }
3514
3515 return event_cpu;
3516}
3517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518/*
3519 * Cross CPU call to read the hardware event
3520 */
3521static void __perf_event_read(void *info)
3522{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003523 struct perf_read_data *data = info;
3524 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003526 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003527 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003528
3529 /*
3530 * If this is a task context, we need to check whether it is
3531 * the current task context of this cpu. If not it has been
3532 * scheduled out before the smp call arrived. In that case
3533 * event->count would have been updated to a recent sample
3534 * when the event was scheduled out.
3535 */
3536 if (ctx->task && cpuctx->task_ctx != ctx)
3537 return;
3538
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003539 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003540 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003541 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003542 update_cgrp_time_from_event(event);
3543 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003544
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003545 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003546 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003547 goto unlock;
3548
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003549 if (!data->group) {
3550 pmu->read(event);
3551 data->ret = 0;
3552 goto unlock;
3553 }
3554
3555 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3556
3557 pmu->read(event);
3558
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003559 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3560 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003561 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3562 /*
3563 * Use sibling's PMU rather than @event's since
3564 * sibling could be on different (eg: software) PMU.
3565 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003566 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003567 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003568 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003569
3570 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003571
3572unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003573 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003574}
3575
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003576static inline u64 perf_event_count(struct perf_event *event)
3577{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003578 if (event->pmu->count)
3579 return event->pmu->count(event);
3580
3581 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003582}
3583
Kaixu Xiaffe86902015-08-06 07:02:32 +00003584/*
3585 * NMI-safe method to read a local event, that is an event that
3586 * is:
3587 * - either for the current task, or for this CPU
3588 * - does not have inherit set, for inherited task events
3589 * will not be local and we cannot read them atomically
3590 * - must not have a pmu::count method
3591 */
3592u64 perf_event_read_local(struct perf_event *event)
3593{
3594 unsigned long flags;
3595 u64 val;
3596
3597 /*
3598 * Disabling interrupts avoids all counter scheduling (context
3599 * switches, timer based rotation and IPIs).
3600 */
3601 local_irq_save(flags);
3602
3603 /* If this is a per-task event, it must be for current */
3604 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3605 event->hw.target != current);
3606
3607 /* If this is a per-CPU event, it must be for this CPU */
3608 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3609 event->cpu != smp_processor_id());
3610
3611 /*
3612 * It must not be an event with inherit set, we cannot read
3613 * all child counters from atomic context.
3614 */
3615 WARN_ON_ONCE(event->attr.inherit);
3616
3617 /*
3618 * It must not have a pmu::count method, those are not
3619 * NMI safe.
3620 */
3621 WARN_ON_ONCE(event->pmu->count);
3622
3623 /*
3624 * If the event is currently on this CPU, its either a per-task event,
3625 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3626 * oncpu == -1).
3627 */
3628 if (event->oncpu == smp_processor_id())
3629 event->pmu->read(event);
3630
3631 val = local64_read(&event->count);
3632 local_irq_restore(flags);
3633
3634 return val;
3635}
3636
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003637static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003638{
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003639 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003640
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003641 /*
3642 * If event is enabled and currently active on a CPU, update the
3643 * value in the event structure:
3644 */
3645 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003646 struct perf_read_data data = {
3647 .event = event,
3648 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003649 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003650 };
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003651
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003652 event_cpu = READ_ONCE(event->oncpu);
3653 if ((unsigned)event_cpu >= nr_cpu_ids)
3654 return 0;
3655
3656 preempt_disable();
3657 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003658
Peter Zijlstra58763142016-08-30 10:15:03 +02003659 /*
3660 * Purposely ignore the smp_call_function_single() return
3661 * value.
3662 *
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003663 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02003664 * scheduled out and that will have updated the event count.
3665 *
3666 * Therefore, either way, we'll have an up-to-date event count
3667 * after this.
3668 */
Peter Zijlstrae5c2e512017-01-31 11:27:10 +01003669 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
3670 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02003671 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003673 struct perf_event_context *ctx = event->ctx;
3674 unsigned long flags;
3675
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003676 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003677 /*
3678 * may read while context is not active
3679 * (e.g., thread is blocked), in that case
3680 * we cannot update context time
3681 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003682 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003683 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003684 update_cgrp_time_from_event(event);
3685 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003686 if (group)
3687 update_group_times(event);
3688 else
3689 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003690 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003691 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003692
3693 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003694}
3695
3696/*
3697 * Initialize the perf_event context in a task_struct:
3698 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003699static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003700{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003701 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003703 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003704 INIT_LIST_HEAD(&ctx->pinned_groups);
3705 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706 INIT_LIST_HEAD(&ctx->event_list);
3707 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708}
3709
Peter Zijlstraeb184472010-09-07 15:55:13 +02003710static struct perf_event_context *
3711alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003712{
3713 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003714
3715 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3716 if (!ctx)
3717 return NULL;
3718
3719 __perf_event_init_context(ctx);
3720 if (task) {
3721 ctx->task = task;
3722 get_task_struct(task);
3723 }
3724 ctx->pmu = pmu;
3725
3726 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003727}
3728
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003729static struct task_struct *
3730find_lively_task_by_vpid(pid_t vpid)
3731{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003734 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003735 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003736 task = current;
3737 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003738 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739 if (task)
3740 get_task_struct(task);
3741 rcu_read_unlock();
3742
3743 if (!task)
3744 return ERR_PTR(-ESRCH);
3745
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003746 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003747}
3748
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003749/*
3750 * Returns a matching context with refcount and pincount.
3751 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003752static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003753find_get_context(struct pmu *pmu, struct task_struct *task,
3754 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003755{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003756 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003758 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003760 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003761 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003763 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764 /* Must be root to operate on a CPU event: */
3765 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3766 return ERR_PTR(-EACCES);
3767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003768 /*
3769 * We could be clever and allow to attach a event to an
3770 * offline CPU and activate it when the CPU comes up, but
3771 * that's for later.
3772 */
3773 if (!cpu_online(cpu))
3774 return ERR_PTR(-ENODEV);
3775
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003776 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777 ctx = &cpuctx->ctx;
3778 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003779 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003780
3781 return ctx;
3782 }
3783
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003784 err = -EINVAL;
3785 ctxn = pmu->task_ctx_nr;
3786 if (ctxn < 0)
3787 goto errout;
3788
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003789 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3790 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3791 if (!task_ctx_data) {
3792 err = -ENOMEM;
3793 goto errout;
3794 }
3795 }
3796
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003797retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003798 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003799 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003800 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003801 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003802
3803 if (task_ctx_data && !ctx->task_ctx_data) {
3804 ctx->task_ctx_data = task_ctx_data;
3805 task_ctx_data = NULL;
3806 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003807 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003808
3809 if (clone_ctx)
3810 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003811 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003812 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003813 err = -ENOMEM;
3814 if (!ctx)
3815 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003816
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003817 if (task_ctx_data) {
3818 ctx->task_ctx_data = task_ctx_data;
3819 task_ctx_data = NULL;
3820 }
3821
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003822 err = 0;
3823 mutex_lock(&task->perf_event_mutex);
3824 /*
3825 * If it has already passed perf_event_exit_task().
3826 * we must see PF_EXITING, it takes this mutex too.
3827 */
3828 if (task->flags & PF_EXITING)
3829 err = -ESRCH;
3830 else if (task->perf_event_ctxp[ctxn])
3831 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003832 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003833 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003834 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003835 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003836 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003837 mutex_unlock(&task->perf_event_mutex);
3838
3839 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003840 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003841
3842 if (err == -EAGAIN)
3843 goto retry;
3844 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003845 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003846 }
3847
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003848 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003849 return ctx;
3850
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003851errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003852 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003853 return ERR_PTR(err);
3854}
3855
Li Zefan6fb29152009-10-15 11:21:42 +08003856static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003857static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003859static void free_event_rcu(struct rcu_head *head)
3860{
3861 struct perf_event *event;
3862
3863 event = container_of(head, struct perf_event, rcu_head);
3864 if (event->ns)
3865 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003866 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003867 kfree(event);
3868}
3869
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003870static void ring_buffer_attach(struct perf_event *event,
3871 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872
Kan Liangf2fb6be2016-03-23 11:24:37 -07003873static void detach_sb_event(struct perf_event *event)
3874{
3875 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3876
3877 raw_spin_lock(&pel->lock);
3878 list_del_rcu(&event->sb_list);
3879 raw_spin_unlock(&pel->lock);
3880}
3881
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003882static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003883{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003884 struct perf_event_attr *attr = &event->attr;
3885
Kan Liangf2fb6be2016-03-23 11:24:37 -07003886 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003887 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003888
3889 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003890 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003891
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003892 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3893 attr->comm || attr->comm_exec ||
3894 attr->task ||
3895 attr->context_switch)
3896 return true;
3897 return false;
3898}
3899
3900static void unaccount_pmu_sb_event(struct perf_event *event)
3901{
3902 if (is_sb_event(event))
3903 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003904}
3905
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003906static void unaccount_event_cpu(struct perf_event *event, int cpu)
3907{
3908 if (event->parent)
3909 return;
3910
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003911 if (is_cgroup_event(event))
3912 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3913}
3914
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003915#ifdef CONFIG_NO_HZ_FULL
3916static DEFINE_SPINLOCK(nr_freq_lock);
3917#endif
3918
3919static void unaccount_freq_event_nohz(void)
3920{
3921#ifdef CONFIG_NO_HZ_FULL
3922 spin_lock(&nr_freq_lock);
3923 if (atomic_dec_and_test(&nr_freq_events))
3924 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3925 spin_unlock(&nr_freq_lock);
3926#endif
3927}
3928
3929static void unaccount_freq_event(void)
3930{
3931 if (tick_nohz_full_enabled())
3932 unaccount_freq_event_nohz();
3933 else
3934 atomic_dec(&nr_freq_events);
3935}
3936
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003937static void unaccount_event(struct perf_event *event)
3938{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003939 bool dec = false;
3940
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003941 if (event->parent)
3942 return;
3943
3944 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003945 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003946 if (event->attr.mmap || event->attr.mmap_data)
3947 atomic_dec(&nr_mmap_events);
3948 if (event->attr.comm)
3949 atomic_dec(&nr_comm_events);
3950 if (event->attr.task)
3951 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003952 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003953 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003954 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003955 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003956 atomic_dec(&nr_switch_events);
3957 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003958 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003959 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003960 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003961 dec = true;
3962
Peter Zijlstra9107c892016-02-24 18:45:45 +01003963 if (dec) {
3964 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3965 schedule_delayed_work(&perf_sched_work, HZ);
3966 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003967
3968 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003969
3970 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003971}
3972
Peter Zijlstra9107c892016-02-24 18:45:45 +01003973static void perf_sched_delayed(struct work_struct *work)
3974{
3975 mutex_lock(&perf_sched_mutex);
3976 if (atomic_dec_and_test(&perf_sched_count))
3977 static_branch_disable(&perf_sched_events);
3978 mutex_unlock(&perf_sched_mutex);
3979}
3980
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003981/*
3982 * The following implement mutual exclusion of events on "exclusive" pmus
3983 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3984 * at a time, so we disallow creating events that might conflict, namely:
3985 *
3986 * 1) cpu-wide events in the presence of per-task events,
3987 * 2) per-task events in the presence of cpu-wide events,
3988 * 3) two matching events on the same context.
3989 *
3990 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003991 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003992 */
3993static int exclusive_event_init(struct perf_event *event)
3994{
3995 struct pmu *pmu = event->pmu;
3996
3997 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3998 return 0;
3999
4000 /*
4001 * Prevent co-existence of per-task and cpu-wide events on the
4002 * same exclusive pmu.
4003 *
4004 * Negative pmu::exclusive_cnt means there are cpu-wide
4005 * events on this "exclusive" pmu, positive means there are
4006 * per-task events.
4007 *
4008 * Since this is called in perf_event_alloc() path, event::ctx
4009 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4010 * to mean "per-task event", because unlike other attach states it
4011 * never gets cleared.
4012 */
4013 if (event->attach_state & PERF_ATTACH_TASK) {
4014 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4015 return -EBUSY;
4016 } else {
4017 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4018 return -EBUSY;
4019 }
4020
4021 return 0;
4022}
4023
4024static void exclusive_event_destroy(struct perf_event *event)
4025{
4026 struct pmu *pmu = event->pmu;
4027
4028 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4029 return;
4030
4031 /* see comment in exclusive_event_init() */
4032 if (event->attach_state & PERF_ATTACH_TASK)
4033 atomic_dec(&pmu->exclusive_cnt);
4034 else
4035 atomic_inc(&pmu->exclusive_cnt);
4036}
4037
4038static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4039{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004040 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004041 (e1->cpu == e2->cpu ||
4042 e1->cpu == -1 ||
4043 e2->cpu == -1))
4044 return true;
4045 return false;
4046}
4047
4048/* Called under the same ctx::mutex as perf_install_in_context() */
4049static bool exclusive_event_installable(struct perf_event *event,
4050 struct perf_event_context *ctx)
4051{
4052 struct perf_event *iter_event;
4053 struct pmu *pmu = event->pmu;
4054
4055 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
4056 return true;
4057
4058 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4059 if (exclusive_event_match(iter_event, event))
4060 return false;
4061 }
4062
4063 return true;
4064}
4065
Alexander Shishkin375637b2016-04-27 18:44:46 +03004066static void perf_addr_filters_splice(struct perf_event *event,
4067 struct list_head *head);
4068
Peter Zijlstra683ede42014-05-05 12:11:24 +02004069static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004070{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004071 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004072
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004073 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004074
Frederic Weisbecker76369132011-05-19 19:55:04 +02004075 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004076 /*
4077 * Can happen when we close an event with re-directed output.
4078 *
4079 * Since we have a 0 refcount, perf_mmap_close() will skip
4080 * over us; possibly making our ring_buffer_put() the last.
4081 */
4082 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004083 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004084 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085 }
4086
Stephane Eraniane5d13672011-02-14 11:20:01 +02004087 if (is_cgroup_event(event))
4088 perf_detach_cgroup(event);
4089
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004090 if (!event->parent) {
4091 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4092 put_callchain_buffers();
4093 }
4094
4095 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004096 perf_addr_filters_splice(event, NULL);
4097 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004098
4099 if (event->destroy)
4100 event->destroy(event);
4101
4102 if (event->ctx)
4103 put_ctx(event->ctx);
4104
Prashant Bholeb951ffb2018-04-09 19:03:46 +09004105 if (event->hw.target)
4106 put_task_struct(event->hw.target);
4107
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004108 exclusive_event_destroy(event);
4109 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004110
4111 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112}
4113
Peter Zijlstra683ede42014-05-05 12:11:24 +02004114/*
4115 * Used to free events which have a known refcount of 1, such as in error paths
4116 * where the event isn't exposed yet and inherited events.
4117 */
4118static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004119{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004120 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4121 "unexpected event refcount: %ld; ptr=%p\n",
4122 atomic_long_read(&event->refcount), event)) {
4123 /* leak to avoid use-after-free */
4124 return;
4125 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004126
Peter Zijlstra683ede42014-05-05 12:11:24 +02004127 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004128}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004129
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004130/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004131 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004132 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004133static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004134{
Peter Zijlstra88821352010-11-09 19:01:43 +01004135 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004136
Peter Zijlstra88821352010-11-09 19:01:43 +01004137 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004138 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004139 * Matches the smp_store_release() in perf_event_exit_task(). If we
4140 * observe !owner it means the list deletion is complete and we can
4141 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004142 * owner->perf_event_mutex.
4143 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004144 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004145 if (owner) {
4146 /*
4147 * Since delayed_put_task_struct() also drops the last
4148 * task reference we can safely take a new reference
4149 * while holding the rcu_read_lock().
4150 */
4151 get_task_struct(owner);
4152 }
4153 rcu_read_unlock();
4154
4155 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004156 /*
4157 * If we're here through perf_event_exit_task() we're already
4158 * holding ctx->mutex which would be an inversion wrt. the
4159 * normal lock order.
4160 *
4161 * However we can safely take this lock because its the child
4162 * ctx->mutex.
4163 */
4164 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4165
Peter Zijlstra88821352010-11-09 19:01:43 +01004166 /*
4167 * We have to re-check the event->owner field, if it is cleared
4168 * we raced with perf_event_exit_task(), acquiring the mutex
4169 * ensured they're done, and we can proceed with freeing the
4170 * event.
4171 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004172 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004173 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004174 smp_store_release(&event->owner, NULL);
4175 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004176 mutex_unlock(&owner->perf_event_mutex);
4177 put_task_struct(owner);
4178 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004179}
4180
Jiri Olsaf8697762014-08-01 14:33:01 +02004181static void put_event(struct perf_event *event)
4182{
Jiri Olsaf8697762014-08-01 14:33:01 +02004183 if (!atomic_long_dec_and_test(&event->refcount))
4184 return;
4185
Peter Zijlstra683ede42014-05-05 12:11:24 +02004186 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004187}
4188
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004189/*
4190 * Kill an event dead; while event:refcount will preserve the event
4191 * object, it will not preserve its functionality. Once the last 'user'
4192 * gives up the object, we'll destroy the thing.
4193 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004194int perf_event_release_kernel(struct perf_event *event)
4195{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004196 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004197 struct perf_event *child, *tmp;
4198
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004199 /*
4200 * If we got here through err_file: fput(event_file); we will not have
4201 * attached to a context yet.
4202 */
4203 if (!ctx) {
4204 WARN_ON_ONCE(event->attach_state &
4205 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4206 goto no_ctx;
4207 }
4208
Peter Zijlstra88821352010-11-09 19:01:43 +01004209 if (!is_kernel_event(event))
4210 perf_remove_from_owner(event);
4211
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004212 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004213 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004214 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004215
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004216 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004217 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004218 * Mark this even as STATE_DEAD, there is no external reference to it
4219 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004220 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004221 * Anybody acquiring event->child_mutex after the below loop _must_
4222 * also see this, most importantly inherit_event() which will avoid
4223 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004224 *
4225 * Thus this guarantees that we will in fact observe and kill _ALL_
4226 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004227 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004228 event->state = PERF_EVENT_STATE_DEAD;
4229 raw_spin_unlock_irq(&ctx->lock);
4230
4231 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004232
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004233again:
4234 mutex_lock(&event->child_mutex);
4235 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004236
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004237 /*
4238 * Cannot change, child events are not migrated, see the
4239 * comment with perf_event_ctx_lock_nested().
4240 */
4241 ctx = lockless_dereference(child->ctx);
4242 /*
4243 * Since child_mutex nests inside ctx::mutex, we must jump
4244 * through hoops. We start by grabbing a reference on the ctx.
4245 *
4246 * Since the event cannot get freed while we hold the
4247 * child_mutex, the context must also exist and have a !0
4248 * reference count.
4249 */
4250 get_ctx(ctx);
4251
4252 /*
4253 * Now that we have a ctx ref, we can drop child_mutex, and
4254 * acquire ctx::mutex without fear of it going away. Then we
4255 * can re-acquire child_mutex.
4256 */
4257 mutex_unlock(&event->child_mutex);
4258 mutex_lock(&ctx->mutex);
4259 mutex_lock(&event->child_mutex);
4260
4261 /*
4262 * Now that we hold ctx::mutex and child_mutex, revalidate our
4263 * state, if child is still the first entry, it didn't get freed
4264 * and we can continue doing so.
4265 */
4266 tmp = list_first_entry_or_null(&event->child_list,
4267 struct perf_event, child_list);
4268 if (tmp == child) {
4269 perf_remove_from_context(child, DETACH_GROUP);
4270 list_del(&child->child_list);
4271 free_event(child);
4272 /*
4273 * This matches the refcount bump in inherit_event();
4274 * this can't be the last reference.
4275 */
4276 put_event(event);
4277 }
4278
4279 mutex_unlock(&event->child_mutex);
4280 mutex_unlock(&ctx->mutex);
4281 put_ctx(ctx);
4282 goto again;
4283 }
4284 mutex_unlock(&event->child_mutex);
4285
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004286no_ctx:
4287 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004288 return 0;
4289}
4290EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4291
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004292/*
4293 * Called when the last reference to the file is gone.
4294 */
Al Viroa6fa9412012-08-20 14:59:25 +01004295static int perf_release(struct inode *inode, struct file *file)
4296{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004297 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004298 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004299}
4300
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004301u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302{
4303 struct perf_event *child;
4304 u64 total = 0;
4305
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004306 *enabled = 0;
4307 *running = 0;
4308
Peter Zijlstra6f105812009-11-20 22:19:56 +01004309 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004310
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004311 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004312 total += perf_event_count(event);
4313
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004314 *enabled += event->total_time_enabled +
4315 atomic64_read(&event->child_total_time_enabled);
4316 *running += event->total_time_running +
4317 atomic64_read(&event->child_total_time_running);
4318
4319 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004320 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004321 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004322 *enabled += child->total_time_enabled;
4323 *running += child->total_time_running;
4324 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004325 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326
4327 return total;
4328}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004329EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004330
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004331static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004332 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004333{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004334 struct perf_event *sub;
4335 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004336 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004337
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004338 ret = perf_event_read(leader, true);
4339 if (ret)
4340 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004341
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004342 /*
4343 * Since we co-schedule groups, {enabled,running} times of siblings
4344 * will be identical to those of the leader, so we only publish one
4345 * set.
4346 */
4347 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4348 values[n++] += leader->total_time_enabled +
4349 atomic64_read(&leader->child_total_time_enabled);
4350 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004351
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004352 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4353 values[n++] += leader->total_time_running +
4354 atomic64_read(&leader->child_total_time_running);
4355 }
4356
4357 /*
4358 * Write {count,id} tuples for every sibling.
4359 */
4360 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004361 if (read_format & PERF_FORMAT_ID)
4362 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004364 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004365 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004366 if (read_format & PERF_FORMAT_ID)
4367 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004369
4370 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004371}
4372
4373static int perf_read_group(struct perf_event *event,
4374 u64 read_format, char __user *buf)
4375{
4376 struct perf_event *leader = event->group_leader, *child;
4377 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004378 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004379 u64 *values;
4380
4381 lockdep_assert_held(&ctx->mutex);
4382
4383 values = kzalloc(event->read_size, GFP_KERNEL);
4384 if (!values)
4385 return -ENOMEM;
4386
4387 values[0] = 1 + leader->nr_siblings;
4388
4389 /*
4390 * By locking the child_mutex of the leader we effectively
4391 * lock the child list of all siblings.. XXX explain how.
4392 */
4393 mutex_lock(&leader->child_mutex);
4394
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004395 ret = __perf_read_group_add(leader, read_format, values);
4396 if (ret)
4397 goto unlock;
4398
4399 list_for_each_entry(child, &leader->child_list, child_list) {
4400 ret = __perf_read_group_add(child, read_format, values);
4401 if (ret)
4402 goto unlock;
4403 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004404
4405 mutex_unlock(&leader->child_mutex);
4406
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004407 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004408 if (copy_to_user(buf, values, event->read_size))
4409 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004410 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004411
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004412unlock:
4413 mutex_unlock(&leader->child_mutex);
4414out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004415 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004416 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004417}
4418
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004419static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420 u64 read_format, char __user *buf)
4421{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004422 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423 u64 values[4];
4424 int n = 0;
4425
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004426 values[n++] = perf_event_read_value(event, &enabled, &running);
4427 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4428 values[n++] = enabled;
4429 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4430 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431 if (read_format & PERF_FORMAT_ID)
4432 values[n++] = primary_event_id(event);
4433
4434 if (copy_to_user(buf, values, n * sizeof(u64)))
4435 return -EFAULT;
4436
4437 return n * sizeof(u64);
4438}
4439
Jiri Olsadc633982014-09-12 13:18:26 +02004440static bool is_event_hup(struct perf_event *event)
4441{
4442 bool no_children;
4443
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004444 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004445 return false;
4446
4447 mutex_lock(&event->child_mutex);
4448 no_children = list_empty(&event->child_list);
4449 mutex_unlock(&event->child_mutex);
4450 return no_children;
4451}
4452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004453/*
4454 * Read the performance event - simple non blocking version for now
4455 */
4456static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004457__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458{
4459 u64 read_format = event->attr.read_format;
4460 int ret;
4461
4462 /*
4463 * Return end-of-file for a read on a event that is in
4464 * error state (i.e. because it was pinned but it couldn't be
4465 * scheduled on to the CPU at some point).
4466 */
4467 if (event->state == PERF_EVENT_STATE_ERROR)
4468 return 0;
4469
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004470 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004471 return -ENOSPC;
4472
4473 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004474 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004475 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004477 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004478
4479 return ret;
4480}
4481
4482static ssize_t
4483perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4484{
4485 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004486 struct perf_event_context *ctx;
4487 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004489 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004490 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004491 perf_event_ctx_unlock(event, ctx);
4492
4493 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494}
4495
4496static unsigned int perf_poll(struct file *file, poll_table *wait)
4497{
4498 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004499 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004500 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004502 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004503
Jiri Olsadc633982014-09-12 13:18:26 +02004504 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004505 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004507 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004508 * Pin the event->rb by taking event->mmap_mutex; otherwise
4509 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004510 */
4511 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004512 rb = event->rb;
4513 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004514 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004515 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004516 return events;
4517}
4518
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004519static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004520{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004521 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004522 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004523 perf_event_update_userpage(event);
4524}
4525
4526/*
4527 * Holding the top-level event's child_mutex means that any
4528 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004529 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530 * task existence requirements of perf_event_enable/disable.
4531 */
4532static void perf_event_for_each_child(struct perf_event *event,
4533 void (*func)(struct perf_event *))
4534{
4535 struct perf_event *child;
4536
4537 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004539 mutex_lock(&event->child_mutex);
4540 func(event);
4541 list_for_each_entry(child, &event->child_list, child_list)
4542 func(child);
4543 mutex_unlock(&event->child_mutex);
4544}
4545
4546static void perf_event_for_each(struct perf_event *event,
4547 void (*func)(struct perf_event *))
4548{
4549 struct perf_event_context *ctx = event->ctx;
4550 struct perf_event *sibling;
4551
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004552 lockdep_assert_held(&ctx->mutex);
4553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004554 event = event->group_leader;
4555
4556 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004557 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004558 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004559}
4560
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004561static void __perf_event_period(struct perf_event *event,
4562 struct perf_cpu_context *cpuctx,
4563 struct perf_event_context *ctx,
4564 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004565{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004566 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004567 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004570 event->attr.sample_freq = value;
4571 } else {
4572 event->attr.sample_period = value;
4573 event->hw.sample_period = value;
4574 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004575
4576 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4577 if (active) {
4578 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004579 /*
4580 * We could be throttled; unthrottle now to avoid the tick
4581 * trying to unthrottle while we already re-started the event.
4582 */
4583 if (event->hw.interrupts == MAX_INTERRUPTS) {
4584 event->hw.interrupts = 0;
4585 perf_log_throttle(event, 1);
4586 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004587 event->pmu->stop(event, PERF_EF_UPDATE);
4588 }
4589
4590 local64_set(&event->hw.period_left, 0);
4591
4592 if (active) {
4593 event->pmu->start(event, PERF_EF_RELOAD);
4594 perf_pmu_enable(ctx->pmu);
4595 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004596}
4597
4598static int perf_event_period(struct perf_event *event, u64 __user *arg)
4599{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004600 u64 value;
4601
4602 if (!is_sampling_event(event))
4603 return -EINVAL;
4604
4605 if (copy_from_user(&value, arg, sizeof(value)))
4606 return -EFAULT;
4607
4608 if (!value)
4609 return -EINVAL;
4610
4611 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4612 return -EINVAL;
4613
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004614 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004615
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004616 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004617}
4618
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004619static const struct file_operations perf_fops;
4620
Al Viro2903ff02012-08-28 12:52:22 -04004621static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004622{
Al Viro2903ff02012-08-28 12:52:22 -04004623 struct fd f = fdget(fd);
4624 if (!f.file)
4625 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004626
Al Viro2903ff02012-08-28 12:52:22 -04004627 if (f.file->f_op != &perf_fops) {
4628 fdput(f);
4629 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004630 }
Al Viro2903ff02012-08-28 12:52:22 -04004631 *p = f;
4632 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004633}
4634
4635static int perf_event_set_output(struct perf_event *event,
4636 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004637static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004638static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004639
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004640static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004641{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642 void (*func)(struct perf_event *);
4643 u32 flags = arg;
4644
4645 switch (cmd) {
4646 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004647 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648 break;
4649 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004650 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651 break;
4652 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004653 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654 break;
4655
4656 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004657 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004658
4659 case PERF_EVENT_IOC_PERIOD:
4660 return perf_event_period(event, (u64 __user *)arg);
4661
Jiri Olsacf4957f2012-10-24 13:37:58 +02004662 case PERF_EVENT_IOC_ID:
4663 {
4664 u64 id = primary_event_id(event);
4665
4666 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4667 return -EFAULT;
4668 return 0;
4669 }
4670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004671 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004672 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004673 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004674 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004675 struct perf_event *output_event;
4676 struct fd output;
4677 ret = perf_fget_light(arg, &output);
4678 if (ret)
4679 return ret;
4680 output_event = output.file->private_data;
4681 ret = perf_event_set_output(event, output_event);
4682 fdput(output);
4683 } else {
4684 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004685 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004686 return ret;
4687 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004688
Li Zefan6fb29152009-10-15 11:21:42 +08004689 case PERF_EVENT_IOC_SET_FILTER:
4690 return perf_event_set_filter(event, (void __user *)arg);
4691
Alexei Starovoitov25415172015-03-25 12:49:20 -07004692 case PERF_EVENT_IOC_SET_BPF:
4693 return perf_event_set_bpf_prog(event, arg);
4694
Wang Nan86e79722016-03-28 06:41:29 +00004695 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4696 struct ring_buffer *rb;
4697
4698 rcu_read_lock();
4699 rb = rcu_dereference(event->rb);
4700 if (!rb || !rb->nr_pages) {
4701 rcu_read_unlock();
4702 return -EINVAL;
4703 }
4704 rb_toggle_paused(rb, !!arg);
4705 rcu_read_unlock();
4706 return 0;
4707 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004708 default:
4709 return -ENOTTY;
4710 }
4711
4712 if (flags & PERF_IOC_FLAG_GROUP)
4713 perf_event_for_each(event, func);
4714 else
4715 perf_event_for_each_child(event, func);
4716
4717 return 0;
4718}
4719
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004720static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4721{
4722 struct perf_event *event = file->private_data;
4723 struct perf_event_context *ctx;
4724 long ret;
4725
4726 ctx = perf_event_ctx_lock(event);
4727 ret = _perf_ioctl(event, cmd, arg);
4728 perf_event_ctx_unlock(event, ctx);
4729
4730 return ret;
4731}
4732
Pawel Mollb3f20782014-06-13 16:03:32 +01004733#ifdef CONFIG_COMPAT
4734static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4735 unsigned long arg)
4736{
4737 switch (_IOC_NR(cmd)) {
4738 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4739 case _IOC_NR(PERF_EVENT_IOC_ID):
4740 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4741 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4742 cmd &= ~IOCSIZE_MASK;
4743 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4744 }
4745 break;
4746 }
4747 return perf_ioctl(file, cmd, arg);
4748}
4749#else
4750# define perf_compat_ioctl NULL
4751#endif
4752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004753int perf_event_task_enable(void)
4754{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004755 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004756 struct perf_event *event;
4757
4758 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004759 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4760 ctx = perf_event_ctx_lock(event);
4761 perf_event_for_each_child(event, _perf_event_enable);
4762 perf_event_ctx_unlock(event, ctx);
4763 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 mutex_unlock(&current->perf_event_mutex);
4765
4766 return 0;
4767}
4768
4769int perf_event_task_disable(void)
4770{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004771 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004772 struct perf_event *event;
4773
4774 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004775 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4776 ctx = perf_event_ctx_lock(event);
4777 perf_event_for_each_child(event, _perf_event_disable);
4778 perf_event_ctx_unlock(event, ctx);
4779 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780 mutex_unlock(&current->perf_event_mutex);
4781
4782 return 0;
4783}
4784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785static int perf_event_index(struct perf_event *event)
4786{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004787 if (event->hw.state & PERF_HES_STOPPED)
4788 return 0;
4789
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790 if (event->state != PERF_EVENT_STATE_ACTIVE)
4791 return 0;
4792
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004793 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004794}
4795
Eric B Munsonc4794292011-06-23 16:34:38 -04004796static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004797 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004798 u64 *enabled,
4799 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004800{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004801 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004802
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004803 *now = perf_clock();
4804 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004805 *enabled = ctx_time - event->tstamp_enabled;
4806 *running = ctx_time - event->tstamp_running;
4807}
4808
Peter Zijlstrafa731582013-09-19 10:16:42 +02004809static void perf_event_init_userpage(struct perf_event *event)
4810{
4811 struct perf_event_mmap_page *userpg;
4812 struct ring_buffer *rb;
4813
4814 rcu_read_lock();
4815 rb = rcu_dereference(event->rb);
4816 if (!rb)
4817 goto unlock;
4818
4819 userpg = rb->user_page;
4820
4821 /* Allow new userspace to detect that bit 0 is deprecated */
4822 userpg->cap_bit0_is_deprecated = 1;
4823 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004824 userpg->data_offset = PAGE_SIZE;
4825 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004826
4827unlock:
4828 rcu_read_unlock();
4829}
4830
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004831void __weak arch_perf_update_userpage(
4832 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004833{
4834}
4835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004836/*
4837 * Callers need to ensure there can be no nesting of this function, otherwise
4838 * the seqlock logic goes bad. We can not serialize this because the arch
4839 * code calls this from NMI context.
4840 */
4841void perf_event_update_userpage(struct perf_event *event)
4842{
4843 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004844 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004845 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846
4847 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004848 rb = rcu_dereference(event->rb);
4849 if (!rb)
4850 goto unlock;
4851
Eric B Munson0d641202011-06-24 12:26:26 -04004852 /*
4853 * compute total_time_enabled, total_time_running
4854 * based on snapshot values taken when the event
4855 * was last scheduled in.
4856 *
4857 * we cannot simply called update_context_time()
4858 * because of locking issue as we can be called in
4859 * NMI context
4860 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004861 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004862
Frederic Weisbecker76369132011-05-19 19:55:04 +02004863 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004864 /*
4865 * Disable preemption so as to not let the corresponding user-space
4866 * spin too long if we get preempted.
4867 */
4868 preempt_disable();
4869 ++userpg->lock;
4870 barrier();
4871 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004872 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004873 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004874 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004875
Eric B Munson0d641202011-06-24 12:26:26 -04004876 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877 atomic64_read(&event->child_total_time_enabled);
4878
Eric B Munson0d641202011-06-24 12:26:26 -04004879 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880 atomic64_read(&event->child_total_time_running);
4881
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004882 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004883
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004884 barrier();
4885 ++userpg->lock;
4886 preempt_enable();
4887unlock:
4888 rcu_read_unlock();
4889}
4890
Peter Zijlstra906010b2009-09-21 16:08:49 +02004891static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4892{
4893 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004894 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004895 int ret = VM_FAULT_SIGBUS;
4896
4897 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4898 if (vmf->pgoff == 0)
4899 ret = 0;
4900 return ret;
4901 }
4902
4903 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004904 rb = rcu_dereference(event->rb);
4905 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004906 goto unlock;
4907
4908 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4909 goto unlock;
4910
Frederic Weisbecker76369132011-05-19 19:55:04 +02004911 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004912 if (!vmf->page)
4913 goto unlock;
4914
4915 get_page(vmf->page);
4916 vmf->page->mapping = vma->vm_file->f_mapping;
4917 vmf->page->index = vmf->pgoff;
4918
4919 ret = 0;
4920unlock:
4921 rcu_read_unlock();
4922
4923 return ret;
4924}
4925
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004926static void ring_buffer_attach(struct perf_event *event,
4927 struct ring_buffer *rb)
4928{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004929 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004930 unsigned long flags;
4931
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004932 if (event->rb) {
4933 /*
4934 * Should be impossible, we set this when removing
4935 * event->rb_entry and wait/clear when adding event->rb_entry.
4936 */
4937 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004938
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004939 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004940 spin_lock_irqsave(&old_rb->event_lock, flags);
4941 list_del_rcu(&event->rb_entry);
4942 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004943
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004944 event->rcu_batches = get_state_synchronize_rcu();
4945 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004946 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004947
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004948 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004949 if (event->rcu_pending) {
4950 cond_synchronize_rcu(event->rcu_batches);
4951 event->rcu_pending = 0;
4952 }
4953
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004954 spin_lock_irqsave(&rb->event_lock, flags);
4955 list_add_rcu(&event->rb_entry, &rb->event_list);
4956 spin_unlock_irqrestore(&rb->event_lock, flags);
4957 }
4958
Alexander Shishkin767ae082016-09-06 16:23:49 +03004959 /*
4960 * Avoid racing with perf_mmap_close(AUX): stop the event
4961 * before swizzling the event::rb pointer; if it's getting
4962 * unmapped, its aux_mmap_count will be 0 and it won't
4963 * restart. See the comment in __perf_pmu_output_stop().
4964 *
4965 * Data will inevitably be lost when set_output is done in
4966 * mid-air, but then again, whoever does it like this is
4967 * not in for the data anyway.
4968 */
4969 if (has_aux(event))
4970 perf_event_stop(event, 0);
4971
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004972 rcu_assign_pointer(event->rb, rb);
4973
4974 if (old_rb) {
4975 ring_buffer_put(old_rb);
4976 /*
4977 * Since we detached before setting the new rb, so that we
4978 * could attach the new rb, we could have missed a wakeup.
4979 * Provide it now.
4980 */
4981 wake_up_all(&event->waitq);
4982 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004983}
4984
4985static void ring_buffer_wakeup(struct perf_event *event)
4986{
4987 struct ring_buffer *rb;
4988
4989 rcu_read_lock();
4990 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004991 if (rb) {
4992 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4993 wake_up_all(&event->waitq);
4994 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004995 rcu_read_unlock();
4996}
4997
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004998struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004999{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005000 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005002 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005003 rb = rcu_dereference(event->rb);
5004 if (rb) {
5005 if (!atomic_inc_not_zero(&rb->refcount))
5006 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005007 }
5008 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009
Frederic Weisbecker76369132011-05-19 19:55:04 +02005010 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005011}
5012
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005013void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005014{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005015 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005016 return;
5017
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005018 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005019
Frederic Weisbecker76369132011-05-19 19:55:04 +02005020 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005021}
5022
5023static void perf_mmap_open(struct vm_area_struct *vma)
5024{
5025 struct perf_event *event = vma->vm_file->private_data;
5026
5027 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005028 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005029
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005030 if (vma->vm_pgoff)
5031 atomic_inc(&event->rb->aux_mmap_count);
5032
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005033 if (event->pmu->event_mapped)
5034 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005035}
5036
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005037static void perf_pmu_output_stop(struct perf_event *event);
5038
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005039/*
5040 * A buffer can be mmap()ed multiple times; either directly through the same
5041 * event, or through other events by use of perf_event_set_output().
5042 *
5043 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5044 * the buffer here, where we still have a VM context. This means we need
5045 * to detach all events redirecting to us.
5046 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005047static void perf_mmap_close(struct vm_area_struct *vma)
5048{
5049 struct perf_event *event = vma->vm_file->private_data;
5050
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005051 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005052 struct user_struct *mmap_user = rb->mmap_user;
5053 int mmap_locked = rb->mmap_locked;
5054 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005056 if (event->pmu->event_unmapped)
5057 event->pmu->event_unmapped(event);
5058
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005059 /*
5060 * rb->aux_mmap_count will always drop before rb->mmap_count and
5061 * event->mmap_count, so it is ok to use event->mmap_mutex to
5062 * serialize with perf_mmap here.
5063 */
5064 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5065 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005066 /*
5067 * Stop all AUX events that are writing to this buffer,
5068 * so that we can free its AUX pages and corresponding PMU
5069 * data. Note that after rb::aux_mmap_count dropped to zero,
5070 * they won't start any more (see perf_aux_output_begin()).
5071 */
5072 perf_pmu_output_stop(event);
5073
5074 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005075 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
5076 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
5077
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005078 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005079 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005080 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
5081
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005082 mutex_unlock(&event->mmap_mutex);
5083 }
5084
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005085 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005086
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005087 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005088 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005089
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005090 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005091 mutex_unlock(&event->mmap_mutex);
5092
5093 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005094 if (atomic_read(&rb->mmap_count))
5095 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005096
5097 /*
5098 * No other mmap()s, detach from all other events that might redirect
5099 * into the now unreachable buffer. Somewhat complicated by the
5100 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5101 */
5102again:
5103 rcu_read_lock();
5104 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5105 if (!atomic_long_inc_not_zero(&event->refcount)) {
5106 /*
5107 * This event is en-route to free_event() which will
5108 * detach it and remove it from the list.
5109 */
5110 continue;
5111 }
5112 rcu_read_unlock();
5113
5114 mutex_lock(&event->mmap_mutex);
5115 /*
5116 * Check we didn't race with perf_event_set_output() which can
5117 * swizzle the rb from under us while we were waiting to
5118 * acquire mmap_mutex.
5119 *
5120 * If we find a different rb; ignore this event, a next
5121 * iteration will no longer find it on the list. We have to
5122 * still restart the iteration to make sure we're not now
5123 * iterating the wrong list.
5124 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005125 if (event->rb == rb)
5126 ring_buffer_attach(event, NULL);
5127
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005128 mutex_unlock(&event->mmap_mutex);
5129 put_event(event);
5130
5131 /*
5132 * Restart the iteration; either we're on the wrong list or
5133 * destroyed its integrity by doing a deletion.
5134 */
5135 goto again;
5136 }
5137 rcu_read_unlock();
5138
5139 /*
5140 * It could be there's still a few 0-ref events on the list; they'll
5141 * get cleaned up by free_event() -- they'll also still have their
5142 * ref on the rb and will free it whenever they are done with it.
5143 *
5144 * Aside from that, this buffer is 'fully' detached and unmapped,
5145 * undo the VM accounting.
5146 */
5147
5148 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
5149 vma->vm_mm->pinned_vm -= mmap_locked;
5150 free_uid(mmap_user);
5151
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005152out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005153 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005154}
5155
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04005156static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005158 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 .fault = perf_mmap_fault,
5160 .page_mkwrite = perf_mmap_fault,
5161};
5162
5163static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5164{
5165 struct perf_event *event = file->private_data;
5166 unsigned long user_locked, user_lock_limit;
5167 struct user_struct *user = current_user();
5168 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005169 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005170 unsigned long vma_size;
5171 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005172 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005173 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005174
Peter Zijlstrac7920612010-05-18 10:33:24 +02005175 /*
5176 * Don't allow mmap() of inherited per-task counters. This would
5177 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005178 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005179 */
5180 if (event->cpu == -1 && event->attr.inherit)
5181 return -EINVAL;
5182
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005183 if (!(vma->vm_flags & VM_SHARED))
5184 return -EINVAL;
5185
5186 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005187
5188 if (vma->vm_pgoff == 0) {
5189 nr_pages = (vma_size / PAGE_SIZE) - 1;
5190 } else {
5191 /*
5192 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5193 * mapped, all subsequent mappings should have the same size
5194 * and offset. Must be above the normal perf buffer.
5195 */
5196 u64 aux_offset, aux_size;
5197
5198 if (!event->rb)
5199 return -EINVAL;
5200
5201 nr_pages = vma_size / PAGE_SIZE;
5202
5203 mutex_lock(&event->mmap_mutex);
5204 ret = -EINVAL;
5205
5206 rb = event->rb;
5207 if (!rb)
5208 goto aux_unlock;
5209
5210 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5211 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5212
5213 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5214 goto aux_unlock;
5215
5216 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5217 goto aux_unlock;
5218
5219 /* already mapped with a different offset */
5220 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5221 goto aux_unlock;
5222
5223 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5224 goto aux_unlock;
5225
5226 /* already mapped with a different size */
5227 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5228 goto aux_unlock;
5229
5230 if (!is_power_of_2(nr_pages))
5231 goto aux_unlock;
5232
5233 if (!atomic_inc_not_zero(&rb->mmap_count))
5234 goto aux_unlock;
5235
5236 if (rb_has_aux(rb)) {
5237 atomic_inc(&rb->aux_mmap_count);
5238 ret = 0;
5239 goto unlock;
5240 }
5241
5242 atomic_set(&rb->aux_mmap_count, 1);
5243 user_extra = nr_pages;
5244
5245 goto accounting;
5246 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247
5248 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005249 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250 * can do bitmasks instead of modulo.
5251 */
Kan Liang2ed11312015-03-02 02:14:26 -05005252 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253 return -EINVAL;
5254
5255 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5256 return -EINVAL;
5257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005259again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005261 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005262 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005264 goto unlock;
5265 }
5266
5267 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5268 /*
5269 * Raced against perf_mmap_close() through
5270 * perf_event_set_output(). Try again, hope for better
5271 * luck.
5272 */
5273 mutex_unlock(&event->mmap_mutex);
5274 goto again;
5275 }
5276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005277 goto unlock;
5278 }
5279
5280 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005281
5282accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5284
5285 /*
5286 * Increase the limit linearly with more CPUs:
5287 */
5288 user_lock_limit *= num_online_cpus();
5289
5290 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005292 if (user_locked > user_lock_limit)
5293 extra = user_locked - user_lock_limit;
5294
Jiri Slaby78d7d402010-03-05 13:42:54 -08005295 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005297 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005298
5299 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5300 !capable(CAP_IPC_LOCK)) {
5301 ret = -EPERM;
5302 goto unlock;
5303 }
5304
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005305 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005306
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005307 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005308 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005309
Frederic Weisbecker76369132011-05-19 19:55:04 +02005310 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005311 rb = rb_alloc(nr_pages,
5312 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5313 event->cpu, flags);
5314
5315 if (!rb) {
5316 ret = -ENOMEM;
5317 goto unlock;
5318 }
5319
5320 atomic_set(&rb->mmap_count, 1);
5321 rb->mmap_user = get_current_user();
5322 rb->mmap_locked = extra;
5323
5324 ring_buffer_attach(event, rb);
5325
5326 perf_event_init_userpage(event);
5327 perf_event_update_userpage(event);
5328 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005329 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5330 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005331 if (!ret)
5332 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005333 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005335unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005336 if (!ret) {
5337 atomic_long_add(user_extra, &user->locked_vm);
5338 vma->vm_mm->pinned_vm += extra;
5339
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005340 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005341 } else if (rb) {
5342 atomic_dec(&rb->mmap_count);
5343 }
5344aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345 mutex_unlock(&event->mmap_mutex);
5346
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005347 /*
5348 * Since pinned accounting is per vm we cannot allow fork() to copy our
5349 * vma.
5350 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005351 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005352 vma->vm_ops = &perf_mmap_vmops;
5353
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005354 if (event->pmu->event_mapped)
5355 event->pmu->event_mapped(event);
5356
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005357 return ret;
5358}
5359
5360static int perf_fasync(int fd, struct file *filp, int on)
5361{
Al Viro496ad9a2013-01-23 17:07:38 -05005362 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005363 struct perf_event *event = filp->private_data;
5364 int retval;
5365
Al Viro59551022016-01-22 15:40:57 -05005366 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005367 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005368 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369
5370 if (retval < 0)
5371 return retval;
5372
5373 return 0;
5374}
5375
5376static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005377 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378 .release = perf_release,
5379 .read = perf_read,
5380 .poll = perf_poll,
5381 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005382 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005383 .mmap = perf_mmap,
5384 .fasync = perf_fasync,
5385};
5386
5387/*
5388 * Perf event wakeup
5389 *
5390 * If there's data, ensure we set the poll() state and publish everything
5391 * to user-space before waking everybody up.
5392 */
5393
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005394static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5395{
5396 /* only the parent has fasync state */
5397 if (event->parent)
5398 event = event->parent;
5399 return &event->fasync;
5400}
5401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005402void perf_event_wakeup(struct perf_event *event)
5403{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005404 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005405
5406 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005407 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005408 event->pending_kill = 0;
5409 }
5410}
5411
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005412static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005413{
5414 struct perf_event *event = container_of(entry,
5415 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005416 int rctx;
5417
5418 rctx = perf_swevent_get_recursion_context();
5419 /*
5420 * If we 'fail' here, that's OK, it means recursion is already disabled
5421 * and we won't recurse 'further'.
5422 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423
5424 if (event->pending_disable) {
5425 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005426 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005427 }
5428
5429 if (event->pending_wakeup) {
5430 event->pending_wakeup = 0;
5431 perf_event_wakeup(event);
5432 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005433
5434 if (rctx >= 0)
5435 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436}
5437
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005439 * We assume there is only KVM supporting the callbacks.
5440 * Later on, we might change it to a list if there is
5441 * another virtualization implementation supporting the callbacks.
5442 */
5443struct perf_guest_info_callbacks *perf_guest_cbs;
5444
5445int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5446{
5447 perf_guest_cbs = cbs;
5448 return 0;
5449}
5450EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5451
5452int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5453{
5454 perf_guest_cbs = NULL;
5455 return 0;
5456}
5457EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5458
Jiri Olsa40189942012-08-07 15:20:37 +02005459static void
5460perf_output_sample_regs(struct perf_output_handle *handle,
5461 struct pt_regs *regs, u64 mask)
5462{
5463 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305464 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02005465
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05305466 bitmap_from_u64(_mask, mask);
5467 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02005468 u64 val;
5469
5470 val = perf_reg_value(regs, bit);
5471 perf_output_put(handle, val);
5472 }
5473}
5474
Stephane Eranian60e23642014-09-24 13:48:37 +02005475static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005476 struct pt_regs *regs,
5477 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005478{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005479 if (user_mode(regs)) {
5480 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005481 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005482 } else if (current->mm) {
5483 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005484 } else {
5485 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5486 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005487 }
5488}
5489
Stephane Eranian60e23642014-09-24 13:48:37 +02005490static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5491 struct pt_regs *regs)
5492{
5493 regs_intr->regs = regs;
5494 regs_intr->abi = perf_reg_abi(current);
5495}
5496
5497
Jiri Olsac5ebced2012-08-07 15:20:40 +02005498/*
5499 * Get remaining task size from user stack pointer.
5500 *
5501 * It'd be better to take stack vma map and limit this more
5502 * precisly, but there's no way to get it safely under interrupt,
5503 * so using TASK_SIZE as limit.
5504 */
5505static u64 perf_ustack_task_size(struct pt_regs *regs)
5506{
5507 unsigned long addr = perf_user_stack_pointer(regs);
5508
5509 if (!addr || addr >= TASK_SIZE)
5510 return 0;
5511
5512 return TASK_SIZE - addr;
5513}
5514
5515static u16
5516perf_sample_ustack_size(u16 stack_size, u16 header_size,
5517 struct pt_regs *regs)
5518{
5519 u64 task_size;
5520
5521 /* No regs, no stack pointer, no dump. */
5522 if (!regs)
5523 return 0;
5524
5525 /*
5526 * Check if we fit in with the requested stack size into the:
5527 * - TASK_SIZE
5528 * If we don't, we limit the size to the TASK_SIZE.
5529 *
5530 * - remaining sample size
5531 * If we don't, we customize the stack size to
5532 * fit in to the remaining sample size.
5533 */
5534
5535 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5536 stack_size = min(stack_size, (u16) task_size);
5537
5538 /* Current header size plus static size and dynamic size. */
5539 header_size += 2 * sizeof(u64);
5540
5541 /* Do we fit in with the current stack dump size? */
5542 if ((u16) (header_size + stack_size) < header_size) {
5543 /*
5544 * If we overflow the maximum size for the sample,
5545 * we customize the stack dump size to fit in.
5546 */
5547 stack_size = USHRT_MAX - header_size - sizeof(u64);
5548 stack_size = round_up(stack_size, sizeof(u64));
5549 }
5550
5551 return stack_size;
5552}
5553
5554static void
5555perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5556 struct pt_regs *regs)
5557{
5558 /* Case of a kernel thread, nothing to dump */
5559 if (!regs) {
5560 u64 size = 0;
5561 perf_output_put(handle, size);
5562 } else {
5563 unsigned long sp;
5564 unsigned int rem;
5565 u64 dyn_size;
5566
5567 /*
5568 * We dump:
5569 * static size
5570 * - the size requested by user or the best one we can fit
5571 * in to the sample max size
5572 * data
5573 * - user stack dump data
5574 * dynamic size
5575 * - the actual dumped size
5576 */
5577
5578 /* Static size. */
5579 perf_output_put(handle, dump_size);
5580
5581 /* Data. */
5582 sp = perf_user_stack_pointer(regs);
5583 rem = __output_copy_user(handle, (void *) sp, dump_size);
5584 dyn_size = dump_size - rem;
5585
5586 perf_output_skip(handle, rem);
5587
5588 /* Dynamic size. */
5589 perf_output_put(handle, dyn_size);
5590 }
5591}
5592
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005593static void __perf_event_header__init_id(struct perf_event_header *header,
5594 struct perf_sample_data *data,
5595 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005596{
5597 u64 sample_type = event->attr.sample_type;
5598
5599 data->type = sample_type;
5600 header->size += event->id_header_size;
5601
5602 if (sample_type & PERF_SAMPLE_TID) {
5603 /* namespace issues */
5604 data->tid_entry.pid = perf_event_pid(event, current);
5605 data->tid_entry.tid = perf_event_tid(event, current);
5606 }
5607
5608 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005609 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005610
Adrian Hunterff3d5272013-08-27 11:23:07 +03005611 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005612 data->id = primary_event_id(event);
5613
5614 if (sample_type & PERF_SAMPLE_STREAM_ID)
5615 data->stream_id = event->id;
5616
5617 if (sample_type & PERF_SAMPLE_CPU) {
5618 data->cpu_entry.cpu = raw_smp_processor_id();
5619 data->cpu_entry.reserved = 0;
5620 }
5621}
5622
Frederic Weisbecker76369132011-05-19 19:55:04 +02005623void perf_event_header__init_id(struct perf_event_header *header,
5624 struct perf_sample_data *data,
5625 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005626{
5627 if (event->attr.sample_id_all)
5628 __perf_event_header__init_id(header, data, event);
5629}
5630
5631static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5632 struct perf_sample_data *data)
5633{
5634 u64 sample_type = data->type;
5635
5636 if (sample_type & PERF_SAMPLE_TID)
5637 perf_output_put(handle, data->tid_entry);
5638
5639 if (sample_type & PERF_SAMPLE_TIME)
5640 perf_output_put(handle, data->time);
5641
5642 if (sample_type & PERF_SAMPLE_ID)
5643 perf_output_put(handle, data->id);
5644
5645 if (sample_type & PERF_SAMPLE_STREAM_ID)
5646 perf_output_put(handle, data->stream_id);
5647
5648 if (sample_type & PERF_SAMPLE_CPU)
5649 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005650
5651 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5652 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005653}
5654
Frederic Weisbecker76369132011-05-19 19:55:04 +02005655void perf_event__output_id_sample(struct perf_event *event,
5656 struct perf_output_handle *handle,
5657 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005658{
5659 if (event->attr.sample_id_all)
5660 __perf_event__output_id_sample(handle, sample);
5661}
5662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005663static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005664 struct perf_event *event,
5665 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005666{
5667 u64 read_format = event->attr.read_format;
5668 u64 values[4];
5669 int n = 0;
5670
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005671 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005672 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005673 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005674 atomic64_read(&event->child_total_time_enabled);
5675 }
5676 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005677 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005678 atomic64_read(&event->child_total_time_running);
5679 }
5680 if (read_format & PERF_FORMAT_ID)
5681 values[n++] = primary_event_id(event);
5682
Frederic Weisbecker76369132011-05-19 19:55:04 +02005683 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005684}
5685
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005686static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005687 struct perf_event *event,
5688 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005689{
5690 struct perf_event *leader = event->group_leader, *sub;
5691 u64 read_format = event->attr.read_format;
5692 u64 values[5];
5693 int n = 0;
5694
5695 values[n++] = 1 + leader->nr_siblings;
5696
5697 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005698 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005699
5700 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005701 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005702
Peter Zijlstrabc09bf82018-03-09 12:52:04 +01005703 if ((leader != event) &&
5704 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005705 leader->pmu->read(leader);
5706
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005707 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005708 if (read_format & PERF_FORMAT_ID)
5709 values[n++] = primary_event_id(leader);
5710
Frederic Weisbecker76369132011-05-19 19:55:04 +02005711 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005712
5713 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5714 n = 0;
5715
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005716 if ((sub != event) &&
5717 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005718 sub->pmu->read(sub);
5719
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005720 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005721 if (read_format & PERF_FORMAT_ID)
5722 values[n++] = primary_event_id(sub);
5723
Frederic Weisbecker76369132011-05-19 19:55:04 +02005724 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005725 }
5726}
5727
Stephane Eranianeed01522010-10-26 16:08:01 +02005728#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5729 PERF_FORMAT_TOTAL_TIME_RUNNING)
5730
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02005731/*
5732 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
5733 *
5734 * The problem is that its both hard and excessively expensive to iterate the
5735 * child list, not to mention that its impossible to IPI the children running
5736 * on another CPU, from interrupt/NMI context.
5737 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005738static void perf_output_read(struct perf_output_handle *handle,
5739 struct perf_event *event)
5740{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005741 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005742 u64 read_format = event->attr.read_format;
5743
5744 /*
5745 * compute total_time_enabled, total_time_running
5746 * based on snapshot values taken when the event
5747 * was last scheduled in.
5748 *
5749 * we cannot simply called update_context_time()
5750 * because of locking issue as we are called in
5751 * NMI context
5752 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005753 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005754 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005755
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005756 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005757 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005758 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005759 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760}
5761
5762void perf_output_sample(struct perf_output_handle *handle,
5763 struct perf_event_header *header,
5764 struct perf_sample_data *data,
5765 struct perf_event *event)
5766{
5767 u64 sample_type = data->type;
5768
5769 perf_output_put(handle, *header);
5770
Adrian Hunterff3d5272013-08-27 11:23:07 +03005771 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5772 perf_output_put(handle, data->id);
5773
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005774 if (sample_type & PERF_SAMPLE_IP)
5775 perf_output_put(handle, data->ip);
5776
5777 if (sample_type & PERF_SAMPLE_TID)
5778 perf_output_put(handle, data->tid_entry);
5779
5780 if (sample_type & PERF_SAMPLE_TIME)
5781 perf_output_put(handle, data->time);
5782
5783 if (sample_type & PERF_SAMPLE_ADDR)
5784 perf_output_put(handle, data->addr);
5785
5786 if (sample_type & PERF_SAMPLE_ID)
5787 perf_output_put(handle, data->id);
5788
5789 if (sample_type & PERF_SAMPLE_STREAM_ID)
5790 perf_output_put(handle, data->stream_id);
5791
5792 if (sample_type & PERF_SAMPLE_CPU)
5793 perf_output_put(handle, data->cpu_entry);
5794
5795 if (sample_type & PERF_SAMPLE_PERIOD)
5796 perf_output_put(handle, data->period);
5797
5798 if (sample_type & PERF_SAMPLE_READ)
5799 perf_output_read(handle, event);
5800
5801 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5802 if (data->callchain) {
5803 int size = 1;
5804
5805 if (data->callchain)
5806 size += data->callchain->nr;
5807
5808 size *= sizeof(u64);
5809
Frederic Weisbecker76369132011-05-19 19:55:04 +02005810 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811 } else {
5812 u64 nr = 0;
5813 perf_output_put(handle, nr);
5814 }
5815 }
5816
5817 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005818 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005819
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005820 if (raw) {
5821 struct perf_raw_frag *frag = &raw->frag;
5822
5823 perf_output_put(handle, raw->size);
5824 do {
5825 if (frag->copy) {
5826 __output_custom(handle, frag->copy,
5827 frag->data, frag->size);
5828 } else {
5829 __output_copy(handle, frag->data,
5830 frag->size);
5831 }
5832 if (perf_raw_frag_last(frag))
5833 break;
5834 frag = frag->next;
5835 } while (1);
5836 if (frag->pad)
5837 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005838 } else {
5839 struct {
5840 u32 size;
5841 u32 data;
5842 } raw = {
5843 .size = sizeof(u32),
5844 .data = 0,
5845 };
5846 perf_output_put(handle, raw);
5847 }
5848 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005849
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005850 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5851 if (data->br_stack) {
5852 size_t size;
5853
5854 size = data->br_stack->nr
5855 * sizeof(struct perf_branch_entry);
5856
5857 perf_output_put(handle, data->br_stack->nr);
5858 perf_output_copy(handle, data->br_stack->entries, size);
5859 } else {
5860 /*
5861 * we always store at least the value of nr
5862 */
5863 u64 nr = 0;
5864 perf_output_put(handle, nr);
5865 }
5866 }
Jiri Olsa40189942012-08-07 15:20:37 +02005867
5868 if (sample_type & PERF_SAMPLE_REGS_USER) {
5869 u64 abi = data->regs_user.abi;
5870
5871 /*
5872 * If there are no regs to dump, notice it through
5873 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5874 */
5875 perf_output_put(handle, abi);
5876
5877 if (abi) {
5878 u64 mask = event->attr.sample_regs_user;
5879 perf_output_sample_regs(handle,
5880 data->regs_user.regs,
5881 mask);
5882 }
5883 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005884
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005885 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005886 perf_output_sample_ustack(handle,
5887 data->stack_user_size,
5888 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005889 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005890
5891 if (sample_type & PERF_SAMPLE_WEIGHT)
5892 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005893
5894 if (sample_type & PERF_SAMPLE_DATA_SRC)
5895 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005896
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005897 if (sample_type & PERF_SAMPLE_TRANSACTION)
5898 perf_output_put(handle, data->txn);
5899
Stephane Eranian60e23642014-09-24 13:48:37 +02005900 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5901 u64 abi = data->regs_intr.abi;
5902 /*
5903 * If there are no regs to dump, notice it through
5904 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5905 */
5906 perf_output_put(handle, abi);
5907
5908 if (abi) {
5909 u64 mask = event->attr.sample_regs_intr;
5910
5911 perf_output_sample_regs(handle,
5912 data->regs_intr.regs,
5913 mask);
5914 }
5915 }
5916
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005917 if (!event->attr.watermark) {
5918 int wakeup_events = event->attr.wakeup_events;
5919
5920 if (wakeup_events) {
5921 struct ring_buffer *rb = handle->rb;
5922 int events = local_inc_return(&rb->events);
5923
5924 if (events >= wakeup_events) {
5925 local_sub(wakeup_events, &rb->events);
5926 local_inc(&rb->wakeup);
5927 }
5928 }
5929 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005930}
5931
5932void perf_prepare_sample(struct perf_event_header *header,
5933 struct perf_sample_data *data,
5934 struct perf_event *event,
5935 struct pt_regs *regs)
5936{
5937 u64 sample_type = event->attr.sample_type;
5938
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005939 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005940 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005941
5942 header->misc = 0;
5943 header->misc |= perf_misc_flags(regs);
5944
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005945 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005946
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005947 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005948 data->ip = perf_instruction_pointer(regs);
5949
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005950 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5951 int size = 1;
5952
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005953 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954
5955 if (data->callchain)
5956 size += data->callchain->nr;
5957
5958 header->size += size * sizeof(u64);
5959 }
5960
5961 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005962 struct perf_raw_record *raw = data->raw;
5963 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005965 if (raw) {
5966 struct perf_raw_frag *frag = &raw->frag;
5967 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005968
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005969 do {
5970 sum += frag->size;
5971 if (perf_raw_frag_last(frag))
5972 break;
5973 frag = frag->next;
5974 } while (1);
5975
5976 size = round_up(sum + sizeof(u32), sizeof(u64));
5977 raw->size = size - sizeof(u32);
5978 frag->pad = raw->size - sum;
5979 } else {
5980 size = sizeof(u64);
5981 }
5982
5983 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005985
5986 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5987 int size = sizeof(u64); /* nr */
5988 if (data->br_stack) {
5989 size += data->br_stack->nr
5990 * sizeof(struct perf_branch_entry);
5991 }
5992 header->size += size;
5993 }
Jiri Olsa40189942012-08-07 15:20:37 +02005994
Peter Zijlstra25657112014-09-24 13:48:42 +02005995 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005996 perf_sample_regs_user(&data->regs_user, regs,
5997 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005998
Jiri Olsa40189942012-08-07 15:20:37 +02005999 if (sample_type & PERF_SAMPLE_REGS_USER) {
6000 /* regs dump ABI info */
6001 int size = sizeof(u64);
6002
Jiri Olsa40189942012-08-07 15:20:37 +02006003 if (data->regs_user.regs) {
6004 u64 mask = event->attr.sample_regs_user;
6005 size += hweight64(mask) * sizeof(u64);
6006 }
6007
6008 header->size += size;
6009 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006010
6011 if (sample_type & PERF_SAMPLE_STACK_USER) {
6012 /*
6013 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
6014 * processed as the last one or have additional check added
6015 * in case new sample type is added, because we could eat
6016 * up the rest of the sample size.
6017 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02006018 u16 stack_size = event->attr.sample_stack_user;
6019 u16 size = sizeof(u64);
6020
Jiri Olsac5ebced2012-08-07 15:20:40 +02006021 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02006022 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006023
6024 /*
6025 * If there is something to dump, add space for the dump
6026 * itself and for the field that tells the dynamic size,
6027 * which is how many have been actually dumped.
6028 */
6029 if (stack_size)
6030 size += sizeof(u64) + stack_size;
6031
6032 data->stack_user_size = stack_size;
6033 header->size += size;
6034 }
Stephane Eranian60e23642014-09-24 13:48:37 +02006035
6036 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6037 /* regs dump ABI info */
6038 int size = sizeof(u64);
6039
6040 perf_sample_regs_intr(&data->regs_intr, regs);
6041
6042 if (data->regs_intr.regs) {
6043 u64 mask = event->attr.sample_regs_intr;
6044
6045 size += hweight64(mask) * sizeof(u64);
6046 }
6047
6048 header->size += size;
6049 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006050}
6051
Wang Nan9ecda412016-04-05 14:11:18 +00006052static void __always_inline
6053__perf_event_output(struct perf_event *event,
6054 struct perf_sample_data *data,
6055 struct pt_regs *regs,
6056 int (*output_begin)(struct perf_output_handle *,
6057 struct perf_event *,
6058 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006059{
6060 struct perf_output_handle handle;
6061 struct perf_event_header header;
6062
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006063 /* protect the callchain buffers */
6064 rcu_read_lock();
6065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006066 perf_prepare_sample(&header, data, event, regs);
6067
Wang Nan9ecda412016-04-05 14:11:18 +00006068 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006069 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006070
6071 perf_output_sample(&handle, &header, data, event);
6072
6073 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006074
6075exit:
6076 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006077}
6078
Wang Nan9ecda412016-04-05 14:11:18 +00006079void
6080perf_event_output_forward(struct perf_event *event,
6081 struct perf_sample_data *data,
6082 struct pt_regs *regs)
6083{
6084 __perf_event_output(event, data, regs, perf_output_begin_forward);
6085}
6086
6087void
6088perf_event_output_backward(struct perf_event *event,
6089 struct perf_sample_data *data,
6090 struct pt_regs *regs)
6091{
6092 __perf_event_output(event, data, regs, perf_output_begin_backward);
6093}
6094
6095void
6096perf_event_output(struct perf_event *event,
6097 struct perf_sample_data *data,
6098 struct pt_regs *regs)
6099{
6100 __perf_event_output(event, data, regs, perf_output_begin);
6101}
6102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103/*
6104 * read event_id
6105 */
6106
6107struct perf_read_event {
6108 struct perf_event_header header;
6109
6110 u32 pid;
6111 u32 tid;
6112};
6113
6114static void
6115perf_event_read_event(struct perf_event *event,
6116 struct task_struct *task)
6117{
6118 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006119 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006120 struct perf_read_event read_event = {
6121 .header = {
6122 .type = PERF_RECORD_READ,
6123 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006124 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006125 },
6126 .pid = perf_event_pid(event, task),
6127 .tid = perf_event_tid(event, task),
6128 };
6129 int ret;
6130
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006131 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006132 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006133 if (ret)
6134 return;
6135
6136 perf_output_put(&handle, read_event);
6137 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006138 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006139
6140 perf_output_end(&handle);
6141}
6142
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006143typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006144
6145static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006146perf_iterate_ctx(struct perf_event_context *ctx,
6147 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006148 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02006149{
6150 struct perf_event *event;
6151
6152 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006153 if (!all) {
6154 if (event->state < PERF_EVENT_STATE_INACTIVE)
6155 continue;
6156 if (!event_filter_match(event))
6157 continue;
6158 }
6159
Jiri Olsa67516842013-07-09 18:56:31 +02006160 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006161 }
6162}
6163
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006164static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006165{
6166 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6167 struct perf_event *event;
6168
6169 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006170 /*
6171 * Skip events that are not fully formed yet; ensure that
6172 * if we observe event->ctx, both event and ctx will be
6173 * complete enough. See perf_install_in_context().
6174 */
6175 if (!smp_load_acquire(&event->ctx))
6176 continue;
6177
Kan Liangf2fb6be2016-03-23 11:24:37 -07006178 if (event->state < PERF_EVENT_STATE_INACTIVE)
6179 continue;
6180 if (!event_filter_match(event))
6181 continue;
6182 output(event, data);
6183 }
6184}
6185
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006186/*
6187 * Iterate all events that need to receive side-band events.
6188 *
6189 * For new callers; ensure that account_pmu_sb_event() includes
6190 * your event, otherwise it might not get delivered.
6191 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006192static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006193perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006194 struct perf_event_context *task_ctx)
6195{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006196 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006197 int ctxn;
6198
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006199 rcu_read_lock();
6200 preempt_disable();
6201
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006202 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006203 * If we have task_ctx != NULL we only notify the task context itself.
6204 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006205 * context.
6206 */
6207 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006208 perf_iterate_ctx(task_ctx, output, data, false);
6209 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006210 }
6211
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006212 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006213
6214 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006215 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6216 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006217 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006218 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006219done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006220 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006221 rcu_read_unlock();
6222}
6223
Alexander Shishkin375637b2016-04-27 18:44:46 +03006224/*
6225 * Clear all file-based filters at exec, they'll have to be
6226 * re-instated when/if these objects are mmapped again.
6227 */
6228static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6229{
6230 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6231 struct perf_addr_filter *filter;
6232 unsigned int restart = 0, count = 0;
6233 unsigned long flags;
6234
6235 if (!has_addr_filter(event))
6236 return;
6237
6238 raw_spin_lock_irqsave(&ifh->lock, flags);
6239 list_for_each_entry(filter, &ifh->list, entry) {
6240 if (filter->inode) {
6241 event->addr_filters_offs[count] = 0;
6242 restart++;
6243 }
6244
6245 count++;
6246 }
6247
6248 if (restart)
6249 event->addr_filters_gen++;
6250 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6251
6252 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006253 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006254}
6255
6256void perf_event_exec(void)
6257{
6258 struct perf_event_context *ctx;
6259 int ctxn;
6260
6261 rcu_read_lock();
6262 for_each_task_context_nr(ctxn) {
6263 ctx = current->perf_event_ctxp[ctxn];
6264 if (!ctx)
6265 continue;
6266
6267 perf_event_enable_on_exec(ctxn);
6268
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006269 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006270 true);
6271 }
6272 rcu_read_unlock();
6273}
6274
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006275struct remote_output {
6276 struct ring_buffer *rb;
6277 int err;
6278};
6279
6280static void __perf_event_output_stop(struct perf_event *event, void *data)
6281{
6282 struct perf_event *parent = event->parent;
6283 struct remote_output *ro = data;
6284 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006285 struct stop_event_data sd = {
6286 .event = event,
6287 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006288
6289 if (!has_aux(event))
6290 return;
6291
6292 if (!parent)
6293 parent = event;
6294
6295 /*
6296 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03006297 * ring-buffer, but it will be the child that's actually using it.
6298 *
6299 * We are using event::rb to determine if the event should be stopped,
6300 * however this may race with ring_buffer_attach() (through set_output),
6301 * which will make us skip the event that actually needs to be stopped.
6302 * So ring_buffer_attach() has to stop an aux event before re-assigning
6303 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006304 */
6305 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006306 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006307}
6308
6309static int __perf_pmu_output_stop(void *info)
6310{
6311 struct perf_event *event = info;
6312 struct pmu *pmu = event->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01006313 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006314 struct remote_output ro = {
6315 .rb = event->rb,
6316 };
6317
6318 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006319 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006320 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006321 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006322 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006323 rcu_read_unlock();
6324
6325 return ro.err;
6326}
6327
6328static void perf_pmu_output_stop(struct perf_event *event)
6329{
6330 struct perf_event *iter;
6331 int err, cpu;
6332
6333restart:
6334 rcu_read_lock();
6335 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6336 /*
6337 * For per-CPU events, we need to make sure that neither they
6338 * nor their children are running; for cpu==-1 events it's
6339 * sufficient to stop the event itself if it's active, since
6340 * it can't have children.
6341 */
6342 cpu = iter->cpu;
6343 if (cpu == -1)
6344 cpu = READ_ONCE(iter->oncpu);
6345
6346 if (cpu == -1)
6347 continue;
6348
6349 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6350 if (err == -EAGAIN) {
6351 rcu_read_unlock();
6352 goto restart;
6353 }
6354 }
6355 rcu_read_unlock();
6356}
6357
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006358/*
6359 * task tracking -- fork/exit
6360 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006361 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006362 */
6363
6364struct perf_task_event {
6365 struct task_struct *task;
6366 struct perf_event_context *task_ctx;
6367
6368 struct {
6369 struct perf_event_header header;
6370
6371 u32 pid;
6372 u32 ppid;
6373 u32 tid;
6374 u32 ptid;
6375 u64 time;
6376 } event_id;
6377};
6378
Jiri Olsa67516842013-07-09 18:56:31 +02006379static int perf_event_task_match(struct perf_event *event)
6380{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006381 return event->attr.comm || event->attr.mmap ||
6382 event->attr.mmap2 || event->attr.mmap_data ||
6383 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006384}
6385
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006386static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006387 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006388{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006389 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006390 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006391 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006392 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006393 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006394
Jiri Olsa67516842013-07-09 18:56:31 +02006395 if (!perf_event_task_match(event))
6396 return;
6397
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006398 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006399
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006400 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006401 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006402 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006403 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404
6405 task_event->event_id.pid = perf_event_pid(event, task);
6406 task_event->event_id.ppid = perf_event_pid(event, current);
6407
6408 task_event->event_id.tid = perf_event_tid(event, task);
6409 task_event->event_id.ptid = perf_event_tid(event, current);
6410
Peter Zijlstra34f43922015-02-20 14:05:38 +01006411 task_event->event_id.time = perf_event_clock(event);
6412
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006413 perf_output_put(&handle, task_event->event_id);
6414
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006415 perf_event__output_id_sample(event, &handle, &sample);
6416
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006417 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006418out:
6419 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006420}
6421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422static void perf_event_task(struct task_struct *task,
6423 struct perf_event_context *task_ctx,
6424 int new)
6425{
6426 struct perf_task_event task_event;
6427
6428 if (!atomic_read(&nr_comm_events) &&
6429 !atomic_read(&nr_mmap_events) &&
6430 !atomic_read(&nr_task_events))
6431 return;
6432
6433 task_event = (struct perf_task_event){
6434 .task = task,
6435 .task_ctx = task_ctx,
6436 .event_id = {
6437 .header = {
6438 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6439 .misc = 0,
6440 .size = sizeof(task_event.event_id),
6441 },
6442 /* .pid */
6443 /* .ppid */
6444 /* .tid */
6445 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006446 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447 },
6448 };
6449
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006450 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006451 &task_event,
6452 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006453}
6454
6455void perf_event_fork(struct task_struct *task)
6456{
6457 perf_event_task(task, NULL, 1);
6458}
6459
6460/*
6461 * comm tracking
6462 */
6463
6464struct perf_comm_event {
6465 struct task_struct *task;
6466 char *comm;
6467 int comm_size;
6468
6469 struct {
6470 struct perf_event_header header;
6471
6472 u32 pid;
6473 u32 tid;
6474 } event_id;
6475};
6476
Jiri Olsa67516842013-07-09 18:56:31 +02006477static int perf_event_comm_match(struct perf_event *event)
6478{
6479 return event->attr.comm;
6480}
6481
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006482static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006483 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006484{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006485 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006486 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006487 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006488 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006489 int ret;
6490
Jiri Olsa67516842013-07-09 18:56:31 +02006491 if (!perf_event_comm_match(event))
6492 return;
6493
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006494 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6495 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006496 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006497
6498 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006499 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500
6501 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6502 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6503
6504 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006505 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006506 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006507
6508 perf_event__output_id_sample(event, &handle, &sample);
6509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006511out:
6512 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006513}
6514
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006515static void perf_event_comm_event(struct perf_comm_event *comm_event)
6516{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006517 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006518 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006519
6520 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006521 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006522 size = ALIGN(strlen(comm)+1, sizeof(u64));
6523
6524 comm_event->comm = comm;
6525 comm_event->comm_size = size;
6526
6527 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006528
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006529 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006530 comm_event,
6531 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006532}
6533
Adrian Hunter82b89772014-05-28 11:45:04 +03006534void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535{
6536 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537
6538 if (!atomic_read(&nr_comm_events))
6539 return;
6540
6541 comm_event = (struct perf_comm_event){
6542 .task = task,
6543 /* .comm */
6544 /* .comm_size */
6545 .event_id = {
6546 .header = {
6547 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006548 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006549 /* .size */
6550 },
6551 /* .pid */
6552 /* .tid */
6553 },
6554 };
6555
6556 perf_event_comm_event(&comm_event);
6557}
6558
6559/*
6560 * mmap tracking
6561 */
6562
6563struct perf_mmap_event {
6564 struct vm_area_struct *vma;
6565
6566 const char *file_name;
6567 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006568 int maj, min;
6569 u64 ino;
6570 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006571 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006572
6573 struct {
6574 struct perf_event_header header;
6575
6576 u32 pid;
6577 u32 tid;
6578 u64 start;
6579 u64 len;
6580 u64 pgoff;
6581 } event_id;
6582};
6583
Jiri Olsa67516842013-07-09 18:56:31 +02006584static int perf_event_mmap_match(struct perf_event *event,
6585 void *data)
6586{
6587 struct perf_mmap_event *mmap_event = data;
6588 struct vm_area_struct *vma = mmap_event->vma;
6589 int executable = vma->vm_flags & VM_EXEC;
6590
6591 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006592 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006593}
6594
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006595static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006596 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006597{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006598 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006599 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006600 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006601 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006602 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006603
Jiri Olsa67516842013-07-09 18:56:31 +02006604 if (!perf_event_mmap_match(event, data))
6605 return;
6606
Stephane Eranian13d7a242013-08-21 12:10:24 +02006607 if (event->attr.mmap2) {
6608 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6609 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6610 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6611 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006612 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006613 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6614 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006615 }
6616
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006617 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6618 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006619 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006620 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006621 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006622
6623 mmap_event->event_id.pid = perf_event_pid(event, current);
6624 mmap_event->event_id.tid = perf_event_tid(event, current);
6625
6626 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006627
6628 if (event->attr.mmap2) {
6629 perf_output_put(&handle, mmap_event->maj);
6630 perf_output_put(&handle, mmap_event->min);
6631 perf_output_put(&handle, mmap_event->ino);
6632 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006633 perf_output_put(&handle, mmap_event->prot);
6634 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006635 }
6636
Frederic Weisbecker76369132011-05-19 19:55:04 +02006637 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006639
6640 perf_event__output_id_sample(event, &handle, &sample);
6641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006642 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006643out:
6644 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645}
6646
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006647static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6648{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649 struct vm_area_struct *vma = mmap_event->vma;
6650 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006651 int maj = 0, min = 0;
6652 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006653 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006654 unsigned int size;
6655 char tmp[16];
6656 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006657 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006658
Peter Zijlstrab41615a2017-01-26 23:15:08 +01006659 if (vma->vm_flags & VM_READ)
6660 prot |= PROT_READ;
6661 if (vma->vm_flags & VM_WRITE)
6662 prot |= PROT_WRITE;
6663 if (vma->vm_flags & VM_EXEC)
6664 prot |= PROT_EXEC;
6665
6666 if (vma->vm_flags & VM_MAYSHARE)
6667 flags = MAP_SHARED;
6668 else
6669 flags = MAP_PRIVATE;
6670
6671 if (vma->vm_flags & VM_DENYWRITE)
6672 flags |= MAP_DENYWRITE;
6673 if (vma->vm_flags & VM_MAYEXEC)
6674 flags |= MAP_EXECUTABLE;
6675 if (vma->vm_flags & VM_LOCKED)
6676 flags |= MAP_LOCKED;
6677 if (vma->vm_flags & VM_HUGETLB)
6678 flags |= MAP_HUGETLB;
6679
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006680 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006681 struct inode *inode;
6682 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006683
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006684 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006685 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006686 name = "//enomem";
6687 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006688 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006689 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006690 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006691 * need to add enough zero bytes after the string to handle
6692 * the 64bit alignment we do later.
6693 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006694 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006695 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006696 name = "//toolong";
6697 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006699 inode = file_inode(vma->vm_file);
6700 dev = inode->i_sb->s_dev;
6701 ino = inode->i_ino;
6702 gen = inode->i_generation;
6703 maj = MAJOR(dev);
6704 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006705
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006706 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006707 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006708 if (vma->vm_ops && vma->vm_ops->name) {
6709 name = (char *) vma->vm_ops->name(vma);
6710 if (name)
6711 goto cpy_name;
6712 }
6713
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006714 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006715 if (name)
6716 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006717
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006718 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006719 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006720 name = "[heap]";
6721 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006722 }
6723 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006724 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006725 name = "[stack]";
6726 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006727 }
6728
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006729 name = "//anon";
6730 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006731 }
6732
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006733cpy_name:
6734 strlcpy(tmp, name, sizeof(tmp));
6735 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006736got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006737 /*
6738 * Since our buffer works in 8 byte units we need to align our string
6739 * size to a multiple of 8. However, we must guarantee the tail end is
6740 * zero'd out to avoid leaking random bits to userspace.
6741 */
6742 size = strlen(name)+1;
6743 while (!IS_ALIGNED(size, sizeof(u64)))
6744 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006745
6746 mmap_event->file_name = name;
6747 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006748 mmap_event->maj = maj;
6749 mmap_event->min = min;
6750 mmap_event->ino = ino;
6751 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006752 mmap_event->prot = prot;
6753 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006754
Stephane Eranian2fe85422013-01-24 16:10:39 +01006755 if (!(vma->vm_flags & VM_EXEC))
6756 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6757
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006758 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6759
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006760 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006761 mmap_event,
6762 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006763
6764 kfree(buf);
6765}
6766
Alexander Shishkin375637b2016-04-27 18:44:46 +03006767/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03006768 * Check whether inode and address range match filter criteria.
6769 */
6770static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6771 struct file *file, unsigned long offset,
6772 unsigned long size)
6773{
6774 if (filter->inode != file->f_inode)
6775 return false;
6776
6777 if (filter->offset > offset + size)
6778 return false;
6779
6780 if (filter->offset + filter->size < offset)
6781 return false;
6782
6783 return true;
6784}
6785
6786static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6787{
6788 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6789 struct vm_area_struct *vma = data;
6790 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6791 struct file *file = vma->vm_file;
6792 struct perf_addr_filter *filter;
6793 unsigned int restart = 0, count = 0;
6794
6795 if (!has_addr_filter(event))
6796 return;
6797
6798 if (!file)
6799 return;
6800
6801 raw_spin_lock_irqsave(&ifh->lock, flags);
6802 list_for_each_entry(filter, &ifh->list, entry) {
6803 if (perf_addr_filter_match(filter, file, off,
6804 vma->vm_end - vma->vm_start)) {
6805 event->addr_filters_offs[count] = vma->vm_start;
6806 restart++;
6807 }
6808
6809 count++;
6810 }
6811
6812 if (restart)
6813 event->addr_filters_gen++;
6814 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6815
6816 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03006817 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006818}
6819
6820/*
6821 * Adjust all task's events' filters to the new vma
6822 */
6823static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6824{
6825 struct perf_event_context *ctx;
6826 int ctxn;
6827
Mathieu Poirier12b40a22016-07-18 10:43:06 -06006828 /*
6829 * Data tracing isn't supported yet and as such there is no need
6830 * to keep track of anything that isn't related to executable code:
6831 */
6832 if (!(vma->vm_flags & VM_EXEC))
6833 return;
6834
Alexander Shishkin375637b2016-04-27 18:44:46 +03006835 rcu_read_lock();
6836 for_each_task_context_nr(ctxn) {
6837 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6838 if (!ctx)
6839 continue;
6840
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006841 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006842 }
6843 rcu_read_unlock();
6844}
6845
Eric B Munson3af9e852010-05-18 15:30:49 +01006846void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006847{
6848 struct perf_mmap_event mmap_event;
6849
6850 if (!atomic_read(&nr_mmap_events))
6851 return;
6852
6853 mmap_event = (struct perf_mmap_event){
6854 .vma = vma,
6855 /* .file_name */
6856 /* .file_size */
6857 .event_id = {
6858 .header = {
6859 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006860 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006861 /* .size */
6862 },
6863 /* .pid */
6864 /* .tid */
6865 .start = vma->vm_start,
6866 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006867 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006868 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006869 /* .maj (attr_mmap2 only) */
6870 /* .min (attr_mmap2 only) */
6871 /* .ino (attr_mmap2 only) */
6872 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006873 /* .prot (attr_mmap2 only) */
6874 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875 };
6876
Alexander Shishkin375637b2016-04-27 18:44:46 +03006877 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006878 perf_event_mmap_event(&mmap_event);
6879}
6880
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006881void perf_event_aux_event(struct perf_event *event, unsigned long head,
6882 unsigned long size, u64 flags)
6883{
6884 struct perf_output_handle handle;
6885 struct perf_sample_data sample;
6886 struct perf_aux_event {
6887 struct perf_event_header header;
6888 u64 offset;
6889 u64 size;
6890 u64 flags;
6891 } rec = {
6892 .header = {
6893 .type = PERF_RECORD_AUX,
6894 .misc = 0,
6895 .size = sizeof(rec),
6896 },
6897 .offset = head,
6898 .size = size,
6899 .flags = flags,
6900 };
6901 int ret;
6902
6903 perf_event_header__init_id(&rec.header, &sample, event);
6904 ret = perf_output_begin(&handle, event, rec.header.size);
6905
6906 if (ret)
6907 return;
6908
6909 perf_output_put(&handle, rec);
6910 perf_event__output_id_sample(event, &handle, &sample);
6911
6912 perf_output_end(&handle);
6913}
6914
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006915/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006916 * Lost/dropped samples logging
6917 */
6918void perf_log_lost_samples(struct perf_event *event, u64 lost)
6919{
6920 struct perf_output_handle handle;
6921 struct perf_sample_data sample;
6922 int ret;
6923
6924 struct {
6925 struct perf_event_header header;
6926 u64 lost;
6927 } lost_samples_event = {
6928 .header = {
6929 .type = PERF_RECORD_LOST_SAMPLES,
6930 .misc = 0,
6931 .size = sizeof(lost_samples_event),
6932 },
6933 .lost = lost,
6934 };
6935
6936 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6937
6938 ret = perf_output_begin(&handle, event,
6939 lost_samples_event.header.size);
6940 if (ret)
6941 return;
6942
6943 perf_output_put(&handle, lost_samples_event);
6944 perf_event__output_id_sample(event, &handle, &sample);
6945 perf_output_end(&handle);
6946}
6947
6948/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006949 * context_switch tracking
6950 */
6951
6952struct perf_switch_event {
6953 struct task_struct *task;
6954 struct task_struct *next_prev;
6955
6956 struct {
6957 struct perf_event_header header;
6958 u32 next_prev_pid;
6959 u32 next_prev_tid;
6960 } event_id;
6961};
6962
6963static int perf_event_switch_match(struct perf_event *event)
6964{
6965 return event->attr.context_switch;
6966}
6967
6968static void perf_event_switch_output(struct perf_event *event, void *data)
6969{
6970 struct perf_switch_event *se = data;
6971 struct perf_output_handle handle;
6972 struct perf_sample_data sample;
6973 int ret;
6974
6975 if (!perf_event_switch_match(event))
6976 return;
6977
6978 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6979 if (event->ctx->task) {
6980 se->event_id.header.type = PERF_RECORD_SWITCH;
6981 se->event_id.header.size = sizeof(se->event_id.header);
6982 } else {
6983 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6984 se->event_id.header.size = sizeof(se->event_id);
6985 se->event_id.next_prev_pid =
6986 perf_event_pid(event, se->next_prev);
6987 se->event_id.next_prev_tid =
6988 perf_event_tid(event, se->next_prev);
6989 }
6990
6991 perf_event_header__init_id(&se->event_id.header, &sample, event);
6992
6993 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6994 if (ret)
6995 return;
6996
6997 if (event->ctx->task)
6998 perf_output_put(&handle, se->event_id.header);
6999 else
7000 perf_output_put(&handle, se->event_id);
7001
7002 perf_event__output_id_sample(event, &handle, &sample);
7003
7004 perf_output_end(&handle);
7005}
7006
7007static void perf_event_switch(struct task_struct *task,
7008 struct task_struct *next_prev, bool sched_in)
7009{
7010 struct perf_switch_event switch_event;
7011
7012 /* N.B. caller checks nr_switch_events != 0 */
7013
7014 switch_event = (struct perf_switch_event){
7015 .task = task,
7016 .next_prev = next_prev,
7017 .event_id = {
7018 .header = {
7019 /* .type */
7020 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
7021 /* .size */
7022 },
7023 /* .next_prev_pid */
7024 /* .next_prev_tid */
7025 },
7026 };
7027
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007028 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03007029 &switch_event,
7030 NULL);
7031}
7032
7033/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007034 * IRQ throttle logging
7035 */
7036
7037static void perf_log_throttle(struct perf_event *event, int enable)
7038{
7039 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007040 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007041 int ret;
7042
7043 struct {
7044 struct perf_event_header header;
7045 u64 time;
7046 u64 id;
7047 u64 stream_id;
7048 } throttle_event = {
7049 .header = {
7050 .type = PERF_RECORD_THROTTLE,
7051 .misc = 0,
7052 .size = sizeof(throttle_event),
7053 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01007054 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007055 .id = primary_event_id(event),
7056 .stream_id = event->id,
7057 };
7058
7059 if (enable)
7060 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
7061
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007062 perf_event_header__init_id(&throttle_event.header, &sample, event);
7063
7064 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007065 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007066 if (ret)
7067 return;
7068
7069 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007070 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071 perf_output_end(&handle);
7072}
7073
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007074static void perf_log_itrace_start(struct perf_event *event)
7075{
7076 struct perf_output_handle handle;
7077 struct perf_sample_data sample;
7078 struct perf_aux_event {
7079 struct perf_event_header header;
7080 u32 pid;
7081 u32 tid;
7082 } rec;
7083 int ret;
7084
7085 if (event->parent)
7086 event = event->parent;
7087
7088 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
7089 event->hw.itrace_started)
7090 return;
7091
Alexander Shishkinec0d7722015-01-14 14:18:23 +02007092 rec.header.type = PERF_RECORD_ITRACE_START;
7093 rec.header.misc = 0;
7094 rec.header.size = sizeof(rec);
7095 rec.pid = perf_event_pid(event, current);
7096 rec.tid = perf_event_tid(event, current);
7097
7098 perf_event_header__init_id(&rec.header, &sample, event);
7099 ret = perf_output_begin(&handle, event, rec.header.size);
7100
7101 if (ret)
7102 return;
7103
7104 perf_output_put(&handle, rec);
7105 perf_event__output_id_sample(event, &handle, &sample);
7106
7107 perf_output_end(&handle);
7108}
7109
Jiri Olsaa88ff232016-12-28 14:31:03 +01007110static int
7111__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007112{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007113 struct hw_perf_event *hwc = &event->hw;
7114 int ret = 0;
Jiri Olsaa88ff232016-12-28 14:31:03 +01007115 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01007116
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007117 seq = __this_cpu_read(perf_throttled_seq);
7118 if (seq != hwc->interrupts_seq) {
7119 hwc->interrupts_seq = seq;
7120 hwc->interrupts = 1;
7121 } else {
7122 hwc->interrupts++;
7123 if (unlikely(throttle
7124 && hwc->interrupts >= max_samples_per_tick)) {
7125 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02007126 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01007127 hwc->interrupts = MAX_INTERRUPTS;
7128 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007129 ret = 1;
7130 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01007131 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007132
7133 if (event->attr.freq) {
7134 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01007135 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007136
Peter Zijlstraabd50712010-01-26 18:50:16 +01007137 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007138
Peter Zijlstraabd50712010-01-26 18:50:16 +01007139 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01007140 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141 }
7142
Jiri Olsaa88ff232016-12-28 14:31:03 +01007143 return ret;
7144}
7145
7146int perf_event_account_interrupt(struct perf_event *event)
7147{
7148 return __perf_event_account_interrupt(event, 1);
7149}
7150
7151/*
7152 * Generic event overflow handling, sampling.
7153 */
7154
7155static int __perf_event_overflow(struct perf_event *event,
7156 int throttle, struct perf_sample_data *data,
7157 struct pt_regs *regs)
7158{
7159 int events = atomic_read(&event->event_limit);
7160 int ret = 0;
7161
7162 /*
7163 * Non-sampling counters might still use the PMI to fold short
7164 * hardware counters, ignore those.
7165 */
7166 if (unlikely(!is_sampling_event(event)))
7167 return 0;
7168
7169 ret = __perf_event_account_interrupt(event, throttle);
7170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007171 /*
7172 * XXX event_limit might not quite work as expected on inherited
7173 * events
7174 */
7175
7176 event->pending_kill = POLL_IN;
7177 if (events && atomic_dec_and_test(&event->event_limit)) {
7178 ret = 1;
7179 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02007180
7181 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007182 }
7183
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007184 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007185
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007186 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007187 event->pending_wakeup = 1;
7188 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007189 }
7190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007191 return ret;
7192}
7193
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007194int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007195 struct perf_sample_data *data,
7196 struct pt_regs *regs)
7197{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007198 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199}
7200
7201/*
7202 * Generic software event infrastructure
7203 */
7204
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007205struct swevent_htable {
7206 struct swevent_hlist *swevent_hlist;
7207 struct mutex hlist_mutex;
7208 int hlist_refcount;
7209
7210 /* Recursion avoidance in each contexts */
7211 int recursion[PERF_NR_CONTEXTS];
7212};
7213
7214static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007216/*
7217 * We directly increment event->count and keep a second value in
7218 * event->hw.period_left to count intervals. This period event
7219 * is kept in the range [-sample_period, 0] so that we can use the
7220 * sign as trigger.
7221 */
7222
Jiri Olsaab573842013-05-01 17:25:44 +02007223u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007224{
7225 struct hw_perf_event *hwc = &event->hw;
7226 u64 period = hwc->last_period;
7227 u64 nr, offset;
7228 s64 old, val;
7229
7230 hwc->last_period = hwc->sample_period;
7231
7232again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007233 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007234 if (val < 0)
7235 return 0;
7236
7237 nr = div64_u64(period + val, period);
7238 offset = nr * period;
7239 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007240 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007241 goto again;
7242
7243 return nr;
7244}
7245
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007246static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007247 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007248 struct pt_regs *regs)
7249{
7250 struct hw_perf_event *hwc = &event->hw;
7251 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007252
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007253 if (!overflow)
7254 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007255
7256 if (hwc->interrupts == MAX_INTERRUPTS)
7257 return;
7258
7259 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007260 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007261 data, regs)) {
7262 /*
7263 * We inhibit the overflow from happening when
7264 * hwc->interrupts == MAX_INTERRUPTS.
7265 */
7266 break;
7267 }
7268 throttle = 1;
7269 }
7270}
7271
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007272static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007273 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007274 struct pt_regs *regs)
7275{
7276 struct hw_perf_event *hwc = &event->hw;
7277
Peter Zijlstrae7850592010-05-21 14:43:08 +02007278 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007279
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007280 if (!regs)
7281 return;
7282
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007283 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007284 return;
7285
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007286 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7287 data->period = nr;
7288 return perf_swevent_overflow(event, 1, data, regs);
7289 } else
7290 data->period = event->hw.last_period;
7291
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007292 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007293 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007294
Peter Zijlstrae7850592010-05-21 14:43:08 +02007295 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007296 return;
7297
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007298 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007299}
7300
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007301static int perf_exclude_event(struct perf_event *event,
7302 struct pt_regs *regs)
7303{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007304 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007305 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007306
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007307 if (regs) {
7308 if (event->attr.exclude_user && user_mode(regs))
7309 return 1;
7310
7311 if (event->attr.exclude_kernel && !user_mode(regs))
7312 return 1;
7313 }
7314
7315 return 0;
7316}
7317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007318static int perf_swevent_match(struct perf_event *event,
7319 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007320 u32 event_id,
7321 struct perf_sample_data *data,
7322 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007323{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007324 if (event->attr.type != type)
7325 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007327 if (event->attr.config != event_id)
7328 return 0;
7329
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007330 if (perf_exclude_event(event, regs))
7331 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007332
7333 return 1;
7334}
7335
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007336static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007337{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007338 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007339
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007340 return hash_64(val, SWEVENT_HLIST_BITS);
7341}
7342
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007343static inline struct hlist_head *
7344__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007345{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007346 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007347
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007348 return &hlist->heads[hash];
7349}
7350
7351/* For the read side: events when they trigger */
7352static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007353find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007354{
7355 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007356
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007357 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007358 if (!hlist)
7359 return NULL;
7360
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007361 return __find_swevent_head(hlist, type, event_id);
7362}
7363
7364/* For the event head insertion and removal in the hlist */
7365static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007366find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007367{
7368 struct swevent_hlist *hlist;
7369 u32 event_id = event->attr.config;
7370 u64 type = event->attr.type;
7371
7372 /*
7373 * Event scheduling is always serialized against hlist allocation
7374 * and release. Which makes the protected version suitable here.
7375 * The context lock guarantees that.
7376 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007377 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007378 lockdep_is_held(&event->ctx->lock));
7379 if (!hlist)
7380 return NULL;
7381
7382 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007383}
7384
7385static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007386 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007387 struct perf_sample_data *data,
7388 struct pt_regs *regs)
7389{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007390 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007391 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007392 struct hlist_head *head;
7393
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007394 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007395 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007396 if (!head)
7397 goto end;
7398
Sasha Levinb67bfe02013-02-27 17:06:00 -08007399 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007400 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007401 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007402 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007403end:
7404 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007405}
7406
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007407DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7408
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007409int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007410{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007411 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007412
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007413 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007414}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007415EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007416
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007417void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007418{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007419 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007420
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007421 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007422}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007423
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007424void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007425{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007426 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007427
7428 if (WARN_ON_ONCE(!regs))
7429 return;
7430
7431 perf_sample_data_init(&data, addr, 0);
7432 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7433}
7434
7435void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7436{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007437 int rctx;
7438
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007439 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007440 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007441 if (unlikely(rctx < 0))
7442 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007443
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007444 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007445
7446 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007447fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007448 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007449}
7450
7451static void perf_swevent_read(struct perf_event *event)
7452{
7453}
7454
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007455static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007456{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007457 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007458 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007459 struct hlist_head *head;
7460
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007461 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007462 hwc->last_period = hwc->sample_period;
7463 perf_swevent_set_period(event);
7464 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007465
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007466 hwc->state = !(flags & PERF_EF_START);
7467
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007468 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007469 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007470 return -EINVAL;
7471
7472 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007473 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007475 return 0;
7476}
7477
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007478static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007479{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007480 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007481}
7482
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007483static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007484{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007485 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007486}
7487
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007488static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007489{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007490 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007491}
7492
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007493/* Deref the hlist from the update side */
7494static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007495swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007496{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007497 return rcu_dereference_protected(swhash->swevent_hlist,
7498 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007499}
7500
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007501static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007502{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007503 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007504
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007505 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007506 return;
7507
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007508 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007509 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007510}
7511
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007512static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007513{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007514 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007515
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007516 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007517
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007518 if (!--swhash->hlist_refcount)
7519 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007520
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007521 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007522}
7523
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007524static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007525{
7526 int cpu;
7527
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007528 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007529 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007530}
7531
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007532static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007533{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007534 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007535 int err = 0;
7536
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007537 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007538 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007539 struct swevent_hlist *hlist;
7540
7541 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7542 if (!hlist) {
7543 err = -ENOMEM;
7544 goto exit;
7545 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007546 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007547 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007548 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007549exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007550 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007551
7552 return err;
7553}
7554
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007555static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007556{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007557 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007558
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007559 get_online_cpus();
7560 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007561 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007562 if (err) {
7563 failed_cpu = cpu;
7564 goto fail;
7565 }
7566 }
7567 put_online_cpus();
7568
7569 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007570fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007571 for_each_possible_cpu(cpu) {
7572 if (cpu == failed_cpu)
7573 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007574 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007575 }
7576
7577 put_online_cpus();
7578 return err;
7579}
7580
Ingo Molnarc5905af2012-02-24 08:31:31 +01007581struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007582
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007583static void sw_perf_event_destroy(struct perf_event *event)
7584{
7585 u64 event_id = event->attr.config;
7586
7587 WARN_ON(event->parent);
7588
Ingo Molnarc5905af2012-02-24 08:31:31 +01007589 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007590 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007591}
7592
7593static int perf_swevent_init(struct perf_event *event)
7594{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007595 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007596
7597 if (event->attr.type != PERF_TYPE_SOFTWARE)
7598 return -ENOENT;
7599
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007600 /*
7601 * no branch sampling for software events
7602 */
7603 if (has_branch_stack(event))
7604 return -EOPNOTSUPP;
7605
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007606 switch (event_id) {
7607 case PERF_COUNT_SW_CPU_CLOCK:
7608 case PERF_COUNT_SW_TASK_CLOCK:
7609 return -ENOENT;
7610
7611 default:
7612 break;
7613 }
7614
Dan Carpenterce677832010-10-24 21:50:42 +02007615 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007616 return -ENOENT;
7617
7618 if (!event->parent) {
7619 int err;
7620
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007621 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007622 if (err)
7623 return err;
7624
Ingo Molnarc5905af2012-02-24 08:31:31 +01007625 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007626 event->destroy = sw_perf_event_destroy;
7627 }
7628
7629 return 0;
7630}
7631
7632static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007633 .task_ctx_nr = perf_sw_context,
7634
Peter Zijlstra34f43922015-02-20 14:05:38 +01007635 .capabilities = PERF_PMU_CAP_NO_NMI,
7636
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007637 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007638 .add = perf_swevent_add,
7639 .del = perf_swevent_del,
7640 .start = perf_swevent_start,
7641 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007642 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007643};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007644
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007645#ifdef CONFIG_EVENT_TRACING
7646
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007647static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007648 struct perf_sample_data *data)
7649{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007650 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007651
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007652 /* only top level events have filters set */
7653 if (event->parent)
7654 event = event->parent;
7655
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007656 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7657 return 1;
7658 return 0;
7659}
7660
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007661static int perf_tp_event_match(struct perf_event *event,
7662 struct perf_sample_data *data,
7663 struct pt_regs *regs)
7664{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007665 if (event->hw.state & PERF_HES_STOPPED)
7666 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007667 /*
7668 * All tracepoints are from kernel-space.
7669 */
7670 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007671 return 0;
7672
7673 if (!perf_tp_filter_match(event, data))
7674 return 0;
7675
7676 return 1;
7677}
7678
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007679void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7680 struct trace_event_call *call, u64 count,
7681 struct pt_regs *regs, struct hlist_head *head,
7682 struct task_struct *task)
7683{
7684 struct bpf_prog *prog = call->prog;
7685
7686 if (prog) {
7687 *(struct pt_regs **)raw_data = regs;
7688 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7689 perf_swevent_put_recursion_context(rctx);
7690 return;
7691 }
7692 }
7693 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7694 rctx, task);
7695}
7696EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7697
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007698void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007699 struct pt_regs *regs, struct hlist_head *head, int rctx,
7700 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007701{
7702 struct perf_sample_data data;
7703 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007704
7705 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007706 .frag = {
7707 .size = entry_size,
7708 .data = record,
7709 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007710 };
7711
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007712 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007713 data.raw = &raw;
7714
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007715 perf_trace_buf_update(record, event_type);
7716
Sasha Levinb67bfe02013-02-27 17:06:00 -08007717 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007718 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007719 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007720 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007721
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007722 /*
7723 * If we got specified a target task, also iterate its context and
7724 * deliver this event there too.
7725 */
7726 if (task && task != current) {
7727 struct perf_event_context *ctx;
7728 struct trace_entry *entry = record;
7729
7730 rcu_read_lock();
7731 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7732 if (!ctx)
7733 goto unlock;
7734
7735 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7736 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7737 continue;
7738 if (event->attr.config != entry->type)
7739 continue;
7740 if (perf_tp_event_match(event, &data, regs))
7741 perf_swevent_event(event, count, &data, regs);
7742 }
7743unlock:
7744 rcu_read_unlock();
7745 }
7746
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007747 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007748}
7749EXPORT_SYMBOL_GPL(perf_tp_event);
7750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007751static void tp_perf_event_destroy(struct perf_event *event)
7752{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007753 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007754}
7755
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007756static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007757{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007758 int err;
7759
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007760 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7761 return -ENOENT;
7762
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007763 /*
7764 * no branch sampling for tracepoint events
7765 */
7766 if (has_branch_stack(event))
7767 return -EOPNOTSUPP;
7768
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007769 err = perf_trace_init(event);
7770 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007771 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007772
7773 event->destroy = tp_perf_event_destroy;
7774
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007775 return 0;
7776}
7777
7778static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007779 .task_ctx_nr = perf_sw_context,
7780
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007781 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007782 .add = perf_trace_add,
7783 .del = perf_trace_del,
7784 .start = perf_swevent_start,
7785 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007786 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007787};
7788
7789static inline void perf_tp_register(void)
7790{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007791 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007792}
Li Zefan6fb29152009-10-15 11:21:42 +08007793
Li Zefan6fb29152009-10-15 11:21:42 +08007794static void perf_event_free_filter(struct perf_event *event)
7795{
7796 ftrace_profile_free_filter(event);
7797}
7798
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007799#ifdef CONFIG_BPF_SYSCALL
7800static void bpf_overflow_handler(struct perf_event *event,
7801 struct perf_sample_data *data,
7802 struct pt_regs *regs)
7803{
7804 struct bpf_perf_event_data_kern ctx = {
7805 .data = data,
7806 .regs = regs,
7807 };
7808 int ret = 0;
7809
7810 preempt_disable();
7811 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
7812 goto out;
7813 rcu_read_lock();
7814 ret = BPF_PROG_RUN(event->prog, (void *)&ctx);
7815 rcu_read_unlock();
7816out:
7817 __this_cpu_dec(bpf_prog_active);
7818 preempt_enable();
7819 if (!ret)
7820 return;
7821
7822 event->orig_overflow_handler(event, data, regs);
7823}
7824
7825static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7826{
7827 struct bpf_prog *prog;
7828
7829 if (event->overflow_handler_context)
7830 /* hw breakpoint or kernel counter */
7831 return -EINVAL;
7832
7833 if (event->prog)
7834 return -EEXIST;
7835
7836 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
7837 if (IS_ERR(prog))
7838 return PTR_ERR(prog);
7839
7840 event->prog = prog;
7841 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
7842 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
7843 return 0;
7844}
7845
7846static void perf_event_free_bpf_handler(struct perf_event *event)
7847{
7848 struct bpf_prog *prog = event->prog;
7849
7850 if (!prog)
7851 return;
7852
7853 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
7854 event->prog = NULL;
7855 bpf_prog_put(prog);
7856}
7857#else
7858static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
7859{
7860 return -EOPNOTSUPP;
7861}
7862static void perf_event_free_bpf_handler(struct perf_event *event)
7863{
7864}
7865#endif
7866
Alexei Starovoitov25415172015-03-25 12:49:20 -07007867static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7868{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007869 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007870 struct bpf_prog *prog;
7871
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007872 if (event->attr.type == PERF_TYPE_HARDWARE ||
7873 event->attr.type == PERF_TYPE_SOFTWARE)
7874 return perf_event_set_bpf_handler(event, prog_fd);
7875
Alexei Starovoitov25415172015-03-25 12:49:20 -07007876 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7877 return -EINVAL;
7878
7879 if (event->tp_event->prog)
7880 return -EEXIST;
7881
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007882 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7883 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7884 if (!is_kprobe && !is_tracepoint)
7885 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007886 return -EINVAL;
7887
7888 prog = bpf_prog_get(prog_fd);
7889 if (IS_ERR(prog))
7890 return PTR_ERR(prog);
7891
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007892 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7893 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007894 /* valid fd, but invalid bpf program type */
7895 bpf_prog_put(prog);
7896 return -EINVAL;
7897 }
7898
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007899 if (is_tracepoint) {
7900 int off = trace_event_get_offsets(event->tp_event);
7901
7902 if (prog->aux->max_ctx_offset > off) {
7903 bpf_prog_put(prog);
7904 return -EACCES;
7905 }
7906 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007907 event->tp_event->prog = prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007908 event->tp_event->bpf_prog_owner = event;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007909
7910 return 0;
7911}
7912
7913static void perf_event_free_bpf_prog(struct perf_event *event)
7914{
7915 struct bpf_prog *prog;
7916
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07007917 perf_event_free_bpf_handler(event);
7918
Alexei Starovoitov25415172015-03-25 12:49:20 -07007919 if (!event->tp_event)
7920 return;
7921
7922 prog = event->tp_event->prog;
Yonghong Song0dee5492017-09-18 16:38:36 -07007923 if (prog && event->tp_event->bpf_prog_owner == event) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007924 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007925 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007926 }
7927}
7928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007929#else
Li Zefan6fb29152009-10-15 11:21:42 +08007930
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007931static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007932{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007933}
Li Zefan6fb29152009-10-15 11:21:42 +08007934
Li Zefan6fb29152009-10-15 11:21:42 +08007935static void perf_event_free_filter(struct perf_event *event)
7936{
7937}
7938
Alexei Starovoitov25415172015-03-25 12:49:20 -07007939static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7940{
7941 return -ENOENT;
7942}
7943
7944static void perf_event_free_bpf_prog(struct perf_event *event)
7945{
7946}
Li Zefan07b139c2009-12-21 14:27:35 +08007947#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007948
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007949#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007950void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007951{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007952 struct perf_sample_data sample;
7953 struct pt_regs *regs = data;
7954
Robert Richterfd0d0002012-04-02 20:19:08 +02007955 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007956
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007957 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007958 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007959}
7960#endif
7961
Alexander Shishkin375637b2016-04-27 18:44:46 +03007962/*
7963 * Allocate a new address filter
7964 */
7965static struct perf_addr_filter *
7966perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7967{
7968 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7969 struct perf_addr_filter *filter;
7970
7971 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7972 if (!filter)
7973 return NULL;
7974
7975 INIT_LIST_HEAD(&filter->entry);
7976 list_add_tail(&filter->entry, filters);
7977
7978 return filter;
7979}
7980
7981static void free_filters_list(struct list_head *filters)
7982{
7983 struct perf_addr_filter *filter, *iter;
7984
7985 list_for_each_entry_safe(filter, iter, filters, entry) {
7986 if (filter->inode)
7987 iput(filter->inode);
7988 list_del(&filter->entry);
7989 kfree(filter);
7990 }
7991}
7992
7993/*
7994 * Free existing address filters and optionally install new ones
7995 */
7996static void perf_addr_filters_splice(struct perf_event *event,
7997 struct list_head *head)
7998{
7999 unsigned long flags;
8000 LIST_HEAD(list);
8001
8002 if (!has_addr_filter(event))
8003 return;
8004
8005 /* don't bother with children, they don't have their own filters */
8006 if (event->parent)
8007 return;
8008
8009 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
8010
8011 list_splice_init(&event->addr_filters.list, &list);
8012 if (head)
8013 list_splice(head, &event->addr_filters.list);
8014
8015 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
8016
8017 free_filters_list(&list);
8018}
8019
8020/*
8021 * Scan through mm's vmas and see if one of them matches the
8022 * @filter; if so, adjust filter's address range.
8023 * Called with mm::mmap_sem down for reading.
8024 */
8025static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
8026 struct mm_struct *mm)
8027{
8028 struct vm_area_struct *vma;
8029
8030 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8031 struct file *file = vma->vm_file;
8032 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8033 unsigned long vma_size = vma->vm_end - vma->vm_start;
8034
8035 if (!file)
8036 continue;
8037
8038 if (!perf_addr_filter_match(filter, file, off, vma_size))
8039 continue;
8040
8041 return vma->vm_start;
8042 }
8043
8044 return 0;
8045}
8046
8047/*
8048 * Update event's address range filters based on the
8049 * task's existing mappings, if any.
8050 */
8051static void perf_event_addr_filters_apply(struct perf_event *event)
8052{
8053 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8054 struct task_struct *task = READ_ONCE(event->ctx->task);
8055 struct perf_addr_filter *filter;
8056 struct mm_struct *mm = NULL;
8057 unsigned int count = 0;
8058 unsigned long flags;
8059
8060 /*
8061 * We may observe TASK_TOMBSTONE, which means that the event tear-down
8062 * will stop on the parent's child_mutex that our caller is also holding
8063 */
8064 if (task == TASK_TOMBSTONE)
8065 return;
8066
8067 mm = get_task_mm(event->ctx->task);
8068 if (!mm)
8069 goto restart;
8070
8071 down_read(&mm->mmap_sem);
8072
8073 raw_spin_lock_irqsave(&ifh->lock, flags);
8074 list_for_each_entry(filter, &ifh->list, entry) {
8075 event->addr_filters_offs[count] = 0;
8076
Mathieu Poirier99f5bc92016-07-18 10:43:07 -06008077 /*
8078 * Adjust base offset if the filter is associated to a binary
8079 * that needs to be mapped:
8080 */
8081 if (filter->inode)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008082 event->addr_filters_offs[count] =
8083 perf_addr_filter_apply(filter, mm);
8084
8085 count++;
8086 }
8087
8088 event->addr_filters_gen++;
8089 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8090
8091 up_read(&mm->mmap_sem);
8092
8093 mmput(mm);
8094
8095restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03008096 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008097}
8098
8099/*
8100 * Address range filtering: limiting the data to certain
8101 * instruction address ranges. Filters are ioctl()ed to us from
8102 * userspace as ascii strings.
8103 *
8104 * Filter string format:
8105 *
8106 * ACTION RANGE_SPEC
8107 * where ACTION is one of the
8108 * * "filter": limit the trace to this region
8109 * * "start": start tracing from this address
8110 * * "stop": stop tracing at this address/region;
8111 * RANGE_SPEC is
8112 * * for kernel addresses: <start address>[/<size>]
8113 * * for object files: <start address>[/<size>]@</path/to/object/file>
8114 *
8115 * if <size> is not specified, the range is treated as a single address.
8116 */
8117enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02008118 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03008119 IF_ACT_FILTER,
8120 IF_ACT_START,
8121 IF_ACT_STOP,
8122 IF_SRC_FILE,
8123 IF_SRC_KERNEL,
8124 IF_SRC_FILEADDR,
8125 IF_SRC_KERNELADDR,
8126};
8127
8128enum {
8129 IF_STATE_ACTION = 0,
8130 IF_STATE_SOURCE,
8131 IF_STATE_END,
8132};
8133
8134static const match_table_t if_tokens = {
8135 { IF_ACT_FILTER, "filter" },
8136 { IF_ACT_START, "start" },
8137 { IF_ACT_STOP, "stop" },
8138 { IF_SRC_FILE, "%u/%u@%s" },
8139 { IF_SRC_KERNEL, "%u/%u" },
8140 { IF_SRC_FILEADDR, "%u@%s" },
8141 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02008142 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03008143};
8144
8145/*
8146 * Address filter string parser
8147 */
8148static int
8149perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
8150 struct list_head *filters)
8151{
8152 struct perf_addr_filter *filter = NULL;
8153 char *start, *orig, *filename = NULL;
8154 struct path path;
8155 substring_t args[MAX_OPT_ARGS];
8156 int state = IF_STATE_ACTION, token;
8157 unsigned int kernel = 0;
8158 int ret = -EINVAL;
8159
8160 orig = fstr = kstrdup(fstr, GFP_KERNEL);
8161 if (!fstr)
8162 return -ENOMEM;
8163
8164 while ((start = strsep(&fstr, " ,\n")) != NULL) {
8165 ret = -EINVAL;
8166
8167 if (!*start)
8168 continue;
8169
8170 /* filter definition begins */
8171 if (state == IF_STATE_ACTION) {
8172 filter = perf_addr_filter_new(event, filters);
8173 if (!filter)
8174 goto fail;
8175 }
8176
8177 token = match_token(start, if_tokens, args);
8178 switch (token) {
8179 case IF_ACT_FILTER:
8180 case IF_ACT_START:
8181 filter->filter = 1;
8182
8183 case IF_ACT_STOP:
8184 if (state != IF_STATE_ACTION)
8185 goto fail;
8186
8187 state = IF_STATE_SOURCE;
8188 break;
8189
8190 case IF_SRC_KERNELADDR:
8191 case IF_SRC_KERNEL:
8192 kernel = 1;
8193
8194 case IF_SRC_FILEADDR:
8195 case IF_SRC_FILE:
8196 if (state != IF_STATE_SOURCE)
8197 goto fail;
8198
8199 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
8200 filter->range = 1;
8201
8202 *args[0].to = 0;
8203 ret = kstrtoul(args[0].from, 0, &filter->offset);
8204 if (ret)
8205 goto fail;
8206
8207 if (filter->range) {
8208 *args[1].to = 0;
8209 ret = kstrtoul(args[1].from, 0, &filter->size);
8210 if (ret)
8211 goto fail;
8212 }
8213
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06008214 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
8215 int fpos = filter->range ? 2 : 1;
8216
8217 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008218 if (!filename) {
8219 ret = -ENOMEM;
8220 goto fail;
8221 }
8222 }
8223
8224 state = IF_STATE_END;
8225 break;
8226
8227 default:
8228 goto fail;
8229 }
8230
8231 /*
8232 * Filter definition is fully parsed, validate and install it.
8233 * Make sure that it doesn't contradict itself or the event's
8234 * attribute.
8235 */
8236 if (state == IF_STATE_END) {
8237 if (kernel && event->attr.exclude_kernel)
8238 goto fail;
8239
8240 if (!kernel) {
8241 if (!filename)
8242 goto fail;
8243
8244 /* look up the path and grab its inode */
8245 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
8246 if (ret)
8247 goto fail_free_name;
8248
8249 filter->inode = igrab(d_inode(path.dentry));
8250 path_put(&path);
8251 kfree(filename);
8252 filename = NULL;
8253
8254 ret = -EINVAL;
8255 if (!filter->inode ||
8256 !S_ISREG(filter->inode->i_mode))
8257 /* free_filters_list() will iput() */
8258 goto fail;
8259 }
8260
8261 /* ready to consume more filters */
8262 state = IF_STATE_ACTION;
8263 filter = NULL;
8264 }
8265 }
8266
8267 if (state != IF_STATE_ACTION)
8268 goto fail;
8269
8270 kfree(orig);
8271
8272 return 0;
8273
8274fail_free_name:
8275 kfree(filename);
8276fail:
8277 free_filters_list(filters);
8278 kfree(orig);
8279
8280 return ret;
8281}
8282
8283static int
8284perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8285{
8286 LIST_HEAD(filters);
8287 int ret;
8288
8289 /*
8290 * Since this is called in perf_ioctl() path, we're already holding
8291 * ctx::mutex.
8292 */
8293 lockdep_assert_held(&event->ctx->mutex);
8294
8295 if (WARN_ON_ONCE(event->parent))
8296 return -EINVAL;
8297
8298 /*
8299 * For now, we only support filtering in per-task events; doing so
8300 * for CPU-wide events requires additional context switching trickery,
8301 * since same object code will be mapped at different virtual
8302 * addresses in different processes.
8303 */
8304 if (!event->ctx->task)
8305 return -EOPNOTSUPP;
8306
8307 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8308 if (ret)
8309 return ret;
8310
8311 ret = event->pmu->addr_filters_validate(&filters);
8312 if (ret) {
8313 free_filters_list(&filters);
8314 return ret;
8315 }
8316
8317 /* remove existing filters, if any */
8318 perf_addr_filters_splice(event, &filters);
8319
8320 /* install new filters */
8321 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8322
8323 return ret;
8324}
8325
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008326static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8327{
8328 char *filter_str;
8329 int ret = -EINVAL;
8330
Alexander Shishkin375637b2016-04-27 18:44:46 +03008331 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8332 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8333 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008334 return -EINVAL;
8335
8336 filter_str = strndup_user(arg, PAGE_SIZE);
8337 if (IS_ERR(filter_str))
8338 return PTR_ERR(filter_str);
8339
8340 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8341 event->attr.type == PERF_TYPE_TRACEPOINT)
8342 ret = ftrace_profile_set_filter(event, event->attr.config,
8343 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008344 else if (has_addr_filter(event))
8345 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008346
8347 kfree(filter_str);
8348 return ret;
8349}
8350
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008351/*
8352 * hrtimer based swevent callback
8353 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008354
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008355static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008356{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008357 enum hrtimer_restart ret = HRTIMER_RESTART;
8358 struct perf_sample_data data;
8359 struct pt_regs *regs;
8360 struct perf_event *event;
8361 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008362
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008363 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008364
8365 if (event->state != PERF_EVENT_STATE_ACTIVE)
8366 return HRTIMER_NORESTART;
8367
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008368 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008369
Robert Richterfd0d0002012-04-02 20:19:08 +02008370 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008371 regs = get_irq_regs();
8372
8373 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008374 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008375 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008376 ret = HRTIMER_NORESTART;
8377 }
8378
8379 period = max_t(u64, 10000, event->hw.sample_period);
8380 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8381
8382 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008383}
8384
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008385static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008386{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008387 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008388 s64 period;
8389
8390 if (!is_sampling_event(event))
8391 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008392
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008393 period = local64_read(&hwc->period_left);
8394 if (period) {
8395 if (period < 0)
8396 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008397
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008398 local64_set(&hwc->period_left, 0);
8399 } else {
8400 period = max_t(u64, 10000, hwc->sample_period);
8401 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008402 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8403 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008404}
8405
8406static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8407{
8408 struct hw_perf_event *hwc = &event->hw;
8409
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008410 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008411 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008412 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008413
8414 hrtimer_cancel(&hwc->hrtimer);
8415 }
8416}
8417
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008418static void perf_swevent_init_hrtimer(struct perf_event *event)
8419{
8420 struct hw_perf_event *hwc = &event->hw;
8421
8422 if (!is_sampling_event(event))
8423 return;
8424
8425 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8426 hwc->hrtimer.function = perf_swevent_hrtimer;
8427
8428 /*
8429 * Since hrtimers have a fixed rate, we can do a static freq->period
8430 * mapping and avoid the whole period adjust feedback stuff.
8431 */
8432 if (event->attr.freq) {
8433 long freq = event->attr.sample_freq;
8434
8435 event->attr.sample_period = NSEC_PER_SEC / freq;
8436 hwc->sample_period = event->attr.sample_period;
8437 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008438 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008439 event->attr.freq = 0;
8440 }
8441}
8442
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008443/*
8444 * Software event: cpu wall time clock
8445 */
8446
8447static void cpu_clock_event_update(struct perf_event *event)
8448{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008449 s64 prev;
8450 u64 now;
8451
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008452 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008453 prev = local64_xchg(&event->hw.prev_count, now);
8454 local64_add(now - prev, &event->count);
8455}
8456
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008457static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008458{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008459 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008460 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008461}
8462
8463static void cpu_clock_event_stop(struct perf_event *event, int flags)
8464{
8465 perf_swevent_cancel_hrtimer(event);
8466 cpu_clock_event_update(event);
8467}
8468
8469static int cpu_clock_event_add(struct perf_event *event, int flags)
8470{
8471 if (flags & PERF_EF_START)
8472 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008473 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008474
8475 return 0;
8476}
8477
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008478static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008479{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008480 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008481}
8482
8483static void cpu_clock_event_read(struct perf_event *event)
8484{
8485 cpu_clock_event_update(event);
8486}
8487
8488static int cpu_clock_event_init(struct perf_event *event)
8489{
8490 if (event->attr.type != PERF_TYPE_SOFTWARE)
8491 return -ENOENT;
8492
8493 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8494 return -ENOENT;
8495
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008496 /*
8497 * no branch sampling for software events
8498 */
8499 if (has_branch_stack(event))
8500 return -EOPNOTSUPP;
8501
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008502 perf_swevent_init_hrtimer(event);
8503
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008504 return 0;
8505}
8506
8507static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008508 .task_ctx_nr = perf_sw_context,
8509
Peter Zijlstra34f43922015-02-20 14:05:38 +01008510 .capabilities = PERF_PMU_CAP_NO_NMI,
8511
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008512 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008513 .add = cpu_clock_event_add,
8514 .del = cpu_clock_event_del,
8515 .start = cpu_clock_event_start,
8516 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008517 .read = cpu_clock_event_read,
8518};
8519
8520/*
8521 * Software event: task time clock
8522 */
8523
8524static void task_clock_event_update(struct perf_event *event, u64 now)
8525{
8526 u64 prev;
8527 s64 delta;
8528
8529 prev = local64_xchg(&event->hw.prev_count, now);
8530 delta = now - prev;
8531 local64_add(delta, &event->count);
8532}
8533
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008534static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008535{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008536 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008537 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008538}
8539
8540static void task_clock_event_stop(struct perf_event *event, int flags)
8541{
8542 perf_swevent_cancel_hrtimer(event);
8543 task_clock_event_update(event, event->ctx->time);
8544}
8545
8546static int task_clock_event_add(struct perf_event *event, int flags)
8547{
8548 if (flags & PERF_EF_START)
8549 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008550 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008551
8552 return 0;
8553}
8554
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008555static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008556{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008557 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008558}
8559
8560static void task_clock_event_read(struct perf_event *event)
8561{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008562 u64 now = perf_clock();
8563 u64 delta = now - event->ctx->timestamp;
8564 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008565
8566 task_clock_event_update(event, time);
8567}
8568
8569static int task_clock_event_init(struct perf_event *event)
8570{
8571 if (event->attr.type != PERF_TYPE_SOFTWARE)
8572 return -ENOENT;
8573
8574 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8575 return -ENOENT;
8576
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008577 /*
8578 * no branch sampling for software events
8579 */
8580 if (has_branch_stack(event))
8581 return -EOPNOTSUPP;
8582
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008583 perf_swevent_init_hrtimer(event);
8584
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008585 return 0;
8586}
8587
8588static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008589 .task_ctx_nr = perf_sw_context,
8590
Peter Zijlstra34f43922015-02-20 14:05:38 +01008591 .capabilities = PERF_PMU_CAP_NO_NMI,
8592
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008593 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008594 .add = task_clock_event_add,
8595 .del = task_clock_event_del,
8596 .start = task_clock_event_start,
8597 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008598 .read = task_clock_event_read,
8599};
8600
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008601static void perf_pmu_nop_void(struct pmu *pmu)
8602{
8603}
8604
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008605static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8606{
8607}
8608
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008609static int perf_pmu_nop_int(struct pmu *pmu)
8610{
8611 return 0;
8612}
8613
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008614static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008615
8616static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008617{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008618 __this_cpu_write(nop_txn_flags, flags);
8619
8620 if (flags & ~PERF_PMU_TXN_ADD)
8621 return;
8622
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008623 perf_pmu_disable(pmu);
8624}
8625
8626static int perf_pmu_commit_txn(struct pmu *pmu)
8627{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008628 unsigned int flags = __this_cpu_read(nop_txn_flags);
8629
8630 __this_cpu_write(nop_txn_flags, 0);
8631
8632 if (flags & ~PERF_PMU_TXN_ADD)
8633 return 0;
8634
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008635 perf_pmu_enable(pmu);
8636 return 0;
8637}
8638
8639static void perf_pmu_cancel_txn(struct pmu *pmu)
8640{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008641 unsigned int flags = __this_cpu_read(nop_txn_flags);
8642
8643 __this_cpu_write(nop_txn_flags, 0);
8644
8645 if (flags & ~PERF_PMU_TXN_ADD)
8646 return;
8647
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008648 perf_pmu_enable(pmu);
8649}
8650
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008651static int perf_event_idx_default(struct perf_event *event)
8652{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008653 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008654}
8655
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008656/*
8657 * Ensures all contexts with the same task_ctx_nr have the same
8658 * pmu_cpu_context too.
8659 */
Mark Rutland9e317042014-02-10 17:44:18 +00008660static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008661{
8662 struct pmu *pmu;
8663
8664 if (ctxn < 0)
8665 return NULL;
8666
8667 list_for_each_entry(pmu, &pmus, entry) {
8668 if (pmu->task_ctx_nr == ctxn)
8669 return pmu->pmu_cpu_context;
8670 }
8671
8672 return NULL;
8673}
8674
Peter Zijlstra51676952010-12-07 14:18:20 +01008675static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008676{
Peter Zijlstra51676952010-12-07 14:18:20 +01008677 int cpu;
8678
8679 for_each_possible_cpu(cpu) {
8680 struct perf_cpu_context *cpuctx;
8681
8682 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8683
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008684 if (cpuctx->unique_pmu == old_pmu)
8685 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008686 }
8687}
8688
8689static void free_pmu_context(struct pmu *pmu)
8690{
8691 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008692
8693 mutex_lock(&pmus_lock);
8694 /*
8695 * Like a real lame refcount.
8696 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008697 list_for_each_entry(i, &pmus, entry) {
8698 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8699 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008700 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008701 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008702 }
8703
Peter Zijlstra51676952010-12-07 14:18:20 +01008704 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008705out:
8706 mutex_unlock(&pmus_lock);
8707}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008708
8709/*
8710 * Let userspace know that this PMU supports address range filtering:
8711 */
8712static ssize_t nr_addr_filters_show(struct device *dev,
8713 struct device_attribute *attr,
8714 char *page)
8715{
8716 struct pmu *pmu = dev_get_drvdata(dev);
8717
8718 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8719}
8720DEVICE_ATTR_RO(nr_addr_filters);
8721
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008722static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008723
Peter Zijlstraabe43402010-11-17 23:17:37 +01008724static ssize_t
8725type_show(struct device *dev, struct device_attribute *attr, char *page)
8726{
8727 struct pmu *pmu = dev_get_drvdata(dev);
8728
8729 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8730}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008731static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008732
Stephane Eranian62b85632013-04-03 14:21:34 +02008733static ssize_t
8734perf_event_mux_interval_ms_show(struct device *dev,
8735 struct device_attribute *attr,
8736 char *page)
8737{
8738 struct pmu *pmu = dev_get_drvdata(dev);
8739
8740 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8741}
8742
Peter Zijlstra272325c2015-04-15 11:41:58 +02008743static DEFINE_MUTEX(mux_interval_mutex);
8744
Stephane Eranian62b85632013-04-03 14:21:34 +02008745static ssize_t
8746perf_event_mux_interval_ms_store(struct device *dev,
8747 struct device_attribute *attr,
8748 const char *buf, size_t count)
8749{
8750 struct pmu *pmu = dev_get_drvdata(dev);
8751 int timer, cpu, ret;
8752
8753 ret = kstrtoint(buf, 0, &timer);
8754 if (ret)
8755 return ret;
8756
8757 if (timer < 1)
8758 return -EINVAL;
8759
8760 /* same value, noting to do */
8761 if (timer == pmu->hrtimer_interval_ms)
8762 return count;
8763
Peter Zijlstra272325c2015-04-15 11:41:58 +02008764 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008765 pmu->hrtimer_interval_ms = timer;
8766
8767 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008768 get_online_cpus();
8769 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008770 struct perf_cpu_context *cpuctx;
8771 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8772 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8773
Peter Zijlstra272325c2015-04-15 11:41:58 +02008774 cpu_function_call(cpu,
8775 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008776 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008777 put_online_cpus();
8778 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008779
8780 return count;
8781}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008782static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008783
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008784static struct attribute *pmu_dev_attrs[] = {
8785 &dev_attr_type.attr,
8786 &dev_attr_perf_event_mux_interval_ms.attr,
8787 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008788};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008789ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008790
8791static int pmu_bus_running;
8792static struct bus_type pmu_bus = {
8793 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008794 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008795};
8796
8797static void pmu_dev_release(struct device *dev)
8798{
8799 kfree(dev);
8800}
8801
8802static int pmu_dev_alloc(struct pmu *pmu)
8803{
8804 int ret = -ENOMEM;
8805
8806 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8807 if (!pmu->dev)
8808 goto out;
8809
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008810 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008811 device_initialize(pmu->dev);
8812 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8813 if (ret)
8814 goto free_dev;
8815
8816 dev_set_drvdata(pmu->dev, pmu);
8817 pmu->dev->bus = &pmu_bus;
8818 pmu->dev->release = pmu_dev_release;
8819 ret = device_add(pmu->dev);
8820 if (ret)
8821 goto free_dev;
8822
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008823 /* For PMUs with address filters, throw in an extra attribute: */
8824 if (pmu->nr_addr_filters)
8825 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8826
8827 if (ret)
8828 goto del_dev;
8829
Peter Zijlstraabe43402010-11-17 23:17:37 +01008830out:
8831 return ret;
8832
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008833del_dev:
8834 device_del(pmu->dev);
8835
Peter Zijlstraabe43402010-11-17 23:17:37 +01008836free_dev:
8837 put_device(pmu->dev);
8838 goto out;
8839}
8840
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008841static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008842static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008843
Mischa Jonker03d8e802013-06-04 11:45:48 +02008844int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008845{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008846 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008847
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008848 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008849 ret = -ENOMEM;
8850 pmu->pmu_disable_count = alloc_percpu(int);
8851 if (!pmu->pmu_disable_count)
8852 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008853
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008854 pmu->type = -1;
8855 if (!name)
8856 goto skip_type;
8857 pmu->name = name;
8858
8859 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008860 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8861 if (type < 0) {
8862 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008863 goto free_pdc;
8864 }
8865 }
8866 pmu->type = type;
8867
Peter Zijlstraabe43402010-11-17 23:17:37 +01008868 if (pmu_bus_running) {
8869 ret = pmu_dev_alloc(pmu);
8870 if (ret)
8871 goto free_idr;
8872 }
8873
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008874skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008875 if (pmu->task_ctx_nr == perf_hw_context) {
8876 static int hw_context_taken = 0;
8877
Mark Rutland5101ef22016-04-26 11:33:46 +01008878 /*
8879 * Other than systems with heterogeneous CPUs, it never makes
8880 * sense for two PMUs to share perf_hw_context. PMUs which are
8881 * uncore must use perf_invalid_context.
8882 */
8883 if (WARN_ON_ONCE(hw_context_taken &&
8884 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008885 pmu->task_ctx_nr = perf_invalid_context;
8886
8887 hw_context_taken = 1;
8888 }
8889
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008890 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8891 if (pmu->pmu_cpu_context)
8892 goto got_cpu_context;
8893
Wei Yongjunc4814202013-04-12 11:05:54 +08008894 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008895 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8896 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008897 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008898
8899 for_each_possible_cpu(cpu) {
8900 struct perf_cpu_context *cpuctx;
8901
8902 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008903 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008904 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008905 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008906 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008907
Peter Zijlstra272325c2015-04-15 11:41:58 +02008908 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008909
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008910 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008911 }
8912
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008913got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008914 if (!pmu->start_txn) {
8915 if (pmu->pmu_enable) {
8916 /*
8917 * If we have pmu_enable/pmu_disable calls, install
8918 * transaction stubs that use that to try and batch
8919 * hardware accesses.
8920 */
8921 pmu->start_txn = perf_pmu_start_txn;
8922 pmu->commit_txn = perf_pmu_commit_txn;
8923 pmu->cancel_txn = perf_pmu_cancel_txn;
8924 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008925 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008926 pmu->commit_txn = perf_pmu_nop_int;
8927 pmu->cancel_txn = perf_pmu_nop_void;
8928 }
8929 }
8930
8931 if (!pmu->pmu_enable) {
8932 pmu->pmu_enable = perf_pmu_nop_void;
8933 pmu->pmu_disable = perf_pmu_nop_void;
8934 }
8935
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008936 if (!pmu->event_idx)
8937 pmu->event_idx = perf_event_idx_default;
8938
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008939 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008940 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008941 ret = 0;
8942unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008943 mutex_unlock(&pmus_lock);
8944
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008945 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008946
Peter Zijlstraabe43402010-11-17 23:17:37 +01008947free_dev:
8948 device_del(pmu->dev);
8949 put_device(pmu->dev);
8950
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008951free_idr:
8952 if (pmu->type >= PERF_TYPE_MAX)
8953 idr_remove(&pmu_idr, pmu->type);
8954
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008955free_pdc:
8956 free_percpu(pmu->pmu_disable_count);
8957 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008958}
Yan, Zhengc464c762014-03-18 16:56:41 +08008959EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008960
8961void perf_pmu_unregister(struct pmu *pmu)
8962{
Jiri Olsa09338402016-10-20 13:10:11 +02008963 int remove_device;
8964
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008965 mutex_lock(&pmus_lock);
Jiri Olsa09338402016-10-20 13:10:11 +02008966 remove_device = pmu_bus_running;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008967 list_del_rcu(&pmu->entry);
8968 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008969
8970 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008971 * We dereference the pmu list under both SRCU and regular RCU, so
8972 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008973 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008974 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008975 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008976
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008977 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008978 if (pmu->type >= PERF_TYPE_MAX)
8979 idr_remove(&pmu_idr, pmu->type);
Jiri Olsa09338402016-10-20 13:10:11 +02008980 if (remove_device) {
8981 if (pmu->nr_addr_filters)
8982 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
8983 device_del(pmu->dev);
8984 put_device(pmu->dev);
8985 }
Peter Zijlstra51676952010-12-07 14:18:20 +01008986 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008987}
Yan, Zhengc464c762014-03-18 16:56:41 +08008988EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008989
Mark Rutlandcc34b982015-01-07 14:56:51 +00008990static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8991{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008992 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008993 int ret;
8994
8995 if (!try_module_get(pmu->module))
8996 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008997
8998 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02008999 /*
9000 * This ctx->mutex can nest when we're called through
9001 * inheritance. See the perf_event_ctx_lock_nested() comment.
9002 */
9003 ctx = perf_event_ctx_lock_nested(event->group_leader,
9004 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009005 BUG_ON(!ctx);
9006 }
9007
Mark Rutlandcc34b982015-01-07 14:56:51 +00009008 event->pmu = pmu;
9009 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01009010
9011 if (ctx)
9012 perf_event_ctx_unlock(event->group_leader, ctx);
9013
Mark Rutlandcc34b982015-01-07 14:56:51 +00009014 if (ret)
9015 module_put(pmu->module);
9016
9017 return ret;
9018}
9019
Geliang Tang18ab2cd2015-09-27 23:25:50 +08009020static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009021{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009022 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009023 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08009024 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009025
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009026 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009027
9028 rcu_read_lock();
9029 pmu = idr_find(&pmu_idr, event->attr.type);
9030 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08009031 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009032 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08009033 if (ret)
9034 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009035 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08009036 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009037
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009038 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00009039 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009040 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009041 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009042
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009043 if (ret != -ENOENT) {
9044 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009045 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009046 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009047 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02009048 pmu = ERR_PTR(-ENOENT);
9049unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009050 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009051
9052 return pmu;
9053}
9054
Kan Liangf2fb6be2016-03-23 11:24:37 -07009055static void attach_sb_event(struct perf_event *event)
9056{
9057 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
9058
9059 raw_spin_lock(&pel->lock);
9060 list_add_rcu(&event->sb_list, &pel->list);
9061 raw_spin_unlock(&pel->lock);
9062}
9063
Peter Zijlstraaab5b712016-05-12 17:26:46 +02009064/*
9065 * We keep a list of all !task (and therefore per-cpu) events
9066 * that need to receive side-band records.
9067 *
9068 * This avoids having to scan all the various PMU per-cpu contexts
9069 * looking for them.
9070 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07009071static void account_pmu_sb_event(struct perf_event *event)
9072{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07009073 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07009074 attach_sb_event(event);
9075}
9076
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009077static void account_event_cpu(struct perf_event *event, int cpu)
9078{
9079 if (event->parent)
9080 return;
9081
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009082 if (is_cgroup_event(event))
9083 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
9084}
9085
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009086/* Freq events need the tick to stay alive (see perf_event_task_tick). */
9087static void account_freq_event_nohz(void)
9088{
9089#ifdef CONFIG_NO_HZ_FULL
9090 /* Lock so we don't race with concurrent unaccount */
9091 spin_lock(&nr_freq_lock);
9092 if (atomic_inc_return(&nr_freq_events) == 1)
9093 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
9094 spin_unlock(&nr_freq_lock);
9095#endif
9096}
9097
9098static void account_freq_event(void)
9099{
9100 if (tick_nohz_full_enabled())
9101 account_freq_event_nohz();
9102 else
9103 atomic_inc(&nr_freq_events);
9104}
9105
9106
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009107static void account_event(struct perf_event *event)
9108{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009109 bool inc = false;
9110
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009111 if (event->parent)
9112 return;
9113
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009114 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009115 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009116 if (event->attr.mmap || event->attr.mmap_data)
9117 atomic_inc(&nr_mmap_events);
9118 if (event->attr.comm)
9119 atomic_inc(&nr_comm_events);
9120 if (event->attr.task)
9121 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02009122 if (event->attr.freq)
9123 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03009124 if (event->attr.context_switch) {
9125 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009126 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03009127 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009128 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009129 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009130 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01009131 inc = true;
9132
Peter Zijlstra9107c892016-02-24 18:45:45 +01009133 if (inc) {
9134 if (atomic_inc_not_zero(&perf_sched_count))
9135 goto enabled;
9136
9137 mutex_lock(&perf_sched_mutex);
9138 if (!atomic_read(&perf_sched_count)) {
9139 static_branch_enable(&perf_sched_events);
9140 /*
9141 * Guarantee that all CPUs observe they key change and
9142 * call the perf scheduling hooks before proceeding to
9143 * install events that need them.
9144 */
9145 synchronize_sched();
9146 }
9147 /*
9148 * Now that we have waited for the sync_sched(), allow further
9149 * increments to by-pass the mutex.
9150 */
9151 atomic_inc(&perf_sched_count);
9152 mutex_unlock(&perf_sched_mutex);
9153 }
9154enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009155
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02009156 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07009157
9158 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02009159}
9160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009161/*
9162 * Allocate and initialize a event structure
9163 */
9164static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009165perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009166 struct task_struct *task,
9167 struct perf_event *group_leader,
9168 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009169 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00009170 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009171{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02009172 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009173 struct perf_event *event;
9174 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009175 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009176
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009177 if ((unsigned)cpu >= nr_cpu_ids) {
9178 if (!task || cpu != -1)
9179 return ERR_PTR(-EINVAL);
9180 }
9181
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009182 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009183 if (!event)
9184 return ERR_PTR(-ENOMEM);
9185
9186 /*
9187 * Single events are their own group leaders, with an
9188 * empty sibling list:
9189 */
9190 if (!group_leader)
9191 group_leader = event;
9192
9193 mutex_init(&event->child_mutex);
9194 INIT_LIST_HEAD(&event->child_list);
9195
9196 INIT_LIST_HEAD(&event->group_entry);
9197 INIT_LIST_HEAD(&event->event_entry);
9198 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009199 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01009200 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009201 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01009202 INIT_HLIST_NODE(&event->hlist_entry);
9203
Peter Zijlstra10c6db12011-11-26 02:47:31 +01009204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009205 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009206 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009207
9208 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009209 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009210
Al Viroa6fa9412012-08-20 14:59:25 +01009211 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009212 event->cpu = cpu;
9213 event->attr = *attr;
9214 event->group_leader = group_leader;
9215 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009216 event->oncpu = -1;
9217
9218 event->parent = parent_event;
9219
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08009220 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009221 event->id = atomic64_inc_return(&perf_event_id);
9222
9223 event->state = PERF_EVENT_STATE_INACTIVE;
9224
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009225 if (task) {
9226 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009227 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009228 * XXX pmu::event_init needs to know what task to account to
9229 * and we cannot use the ctx information because we need the
9230 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009231 */
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009232 get_task_struct(task);
Peter Zijlstra50f16a82015-03-05 22:10:19 +01009233 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02009234 }
9235
Peter Zijlstra34f43922015-02-20 14:05:38 +01009236 event->clock = &local_clock;
9237 if (parent_event)
9238 event->clock = parent_event->clock;
9239
Avi Kivity4dc0da82011-06-29 18:42:35 +03009240 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01009241 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03009242 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +02009243#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009244 if (overflow_handler == bpf_overflow_handler) {
9245 struct bpf_prog *prog = bpf_prog_inc(parent_event->prog);
9246
9247 if (IS_ERR(prog)) {
9248 err = PTR_ERR(prog);
9249 goto err_ns;
9250 }
9251 event->prog = prog;
9252 event->orig_overflow_handler =
9253 parent_event->orig_overflow_handler;
9254 }
9255#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +03009256 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01009257
Wang Nan18794452016-03-28 06:41:30 +00009258 if (overflow_handler) {
9259 event->overflow_handler = overflow_handler;
9260 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00009261 } else if (is_write_backward(event)){
9262 event->overflow_handler = perf_event_output_backward;
9263 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00009264 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00009265 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00009266 event->overflow_handler_context = NULL;
9267 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02009268
Jiri Olsa0231bb52013-02-01 11:23:45 +01009269 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009270
9271 pmu = NULL;
9272
9273 hwc = &event->hw;
9274 hwc->sample_period = attr->sample_period;
9275 if (attr->freq && attr->sample_freq)
9276 hwc->sample_period = 1;
9277 hwc->last_period = hwc->sample_period;
9278
Peter Zijlstrae7850592010-05-21 14:43:08 +02009279 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009280
9281 /*
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009282 * We currently do not support PERF_SAMPLE_READ on inherited events.
9283 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009284 */
Peter Zijlstra50fe37e2017-05-30 11:45:12 +02009285 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009286 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009287
Yan, Zhenga46a2302014-11-04 21:56:06 -05009288 if (!has_branch_stack(event))
9289 event->attr.branch_sample_type = 0;
9290
Matt Fleming79dff512015-01-23 18:45:42 +00009291 if (cgroup_fd != -1) {
9292 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9293 if (err)
9294 goto err_ns;
9295 }
9296
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009297 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009298 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009299 goto err_ns;
9300 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009301 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009302 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009303 }
9304
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009305 err = exclusive_event_init(event);
9306 if (err)
9307 goto err_pmu;
9308
Alexander Shishkin375637b2016-04-27 18:44:46 +03009309 if (has_addr_filter(event)) {
9310 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9311 sizeof(unsigned long),
9312 GFP_KERNEL);
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009313 if (!event->addr_filters_offs) {
9314 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009315 goto err_per_task;
Dan Carpenterf02bfec2017-05-22 12:04:18 +03009316 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009317
9318 /* force hw sync on the address filters */
9319 event->addr_filters_gen = 1;
9320 }
9321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009322 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009323 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009324 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009325 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009326 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009327 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009328 }
9329
Alexander Shishkin927a5572016-03-02 13:24:14 +02009330 /* symmetric to unaccount_event() in _free_event() */
9331 account_event(event);
9332
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009333 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009334
Alexander Shishkin375637b2016-04-27 18:44:46 +03009335err_addr_filters:
9336 kfree(event->addr_filters_offs);
9337
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009338err_per_task:
9339 exclusive_event_destroy(event);
9340
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009341err_pmu:
9342 if (event->destroy)
9343 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009344 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009345err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009346 if (is_cgroup_event(event))
9347 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009348 if (event->ns)
9349 put_pid_ns(event->ns);
Prashant Bholeb951ffb2018-04-09 19:03:46 +09009350 if (event->hw.target)
9351 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009352 kfree(event);
9353
9354 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009355}
9356
9357static int perf_copy_attr(struct perf_event_attr __user *uattr,
9358 struct perf_event_attr *attr)
9359{
9360 u32 size;
9361 int ret;
9362
9363 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9364 return -EFAULT;
9365
9366 /*
9367 * zero the full structure, so that a short copy will be nice.
9368 */
9369 memset(attr, 0, sizeof(*attr));
9370
9371 ret = get_user(size, &uattr->size);
9372 if (ret)
9373 return ret;
9374
9375 if (size > PAGE_SIZE) /* silly large */
9376 goto err_size;
9377
9378 if (!size) /* abi compat */
9379 size = PERF_ATTR_SIZE_VER0;
9380
9381 if (size < PERF_ATTR_SIZE_VER0)
9382 goto err_size;
9383
9384 /*
9385 * If we're handed a bigger struct than we know of,
9386 * ensure all the unknown bits are 0 - i.e. new
9387 * user-space does not rely on any kernel feature
9388 * extensions we dont know about yet.
9389 */
9390 if (size > sizeof(*attr)) {
9391 unsigned char __user *addr;
9392 unsigned char __user *end;
9393 unsigned char val;
9394
9395 addr = (void __user *)uattr + sizeof(*attr);
9396 end = (void __user *)uattr + size;
9397
9398 for (; addr < end; addr++) {
9399 ret = get_user(val, addr);
9400 if (ret)
9401 return ret;
9402 if (val)
9403 goto err_size;
9404 }
9405 size = sizeof(*attr);
9406 }
9407
9408 ret = copy_from_user(attr, uattr, size);
9409 if (ret)
9410 return -EFAULT;
9411
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309412 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009413 return -EINVAL;
9414
9415 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9416 return -EINVAL;
9417
9418 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9419 return -EINVAL;
9420
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009421 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9422 u64 mask = attr->branch_sample_type;
9423
9424 /* only using defined bits */
9425 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9426 return -EINVAL;
9427
9428 /* at least one branch bit must be set */
9429 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9430 return -EINVAL;
9431
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009432 /* propagate priv level, when not set for branch */
9433 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9434
9435 /* exclude_kernel checked on syscall entry */
9436 if (!attr->exclude_kernel)
9437 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9438
9439 if (!attr->exclude_user)
9440 mask |= PERF_SAMPLE_BRANCH_USER;
9441
9442 if (!attr->exclude_hv)
9443 mask |= PERF_SAMPLE_BRANCH_HV;
9444 /*
9445 * adjust user setting (for HW filter setup)
9446 */
9447 attr->branch_sample_type = mask;
9448 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009449 /* privileged levels capture (kernel, hv): check permissions */
9450 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009451 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9452 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009453 }
Jiri Olsa40189942012-08-07 15:20:37 +02009454
Jiri Olsac5ebced2012-08-07 15:20:40 +02009455 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009456 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009457 if (ret)
9458 return ret;
9459 }
9460
9461 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9462 if (!arch_perf_have_user_stack_dump())
9463 return -ENOSYS;
9464
9465 /*
9466 * We have __u32 type for the size, but so far
9467 * we can only use __u16 as maximum due to the
9468 * __u16 sample size limit.
9469 */
9470 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009471 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009472 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa9acdfe42018-04-15 11:23:50 +02009473 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +02009474 }
Jiri Olsa40189942012-08-07 15:20:37 +02009475
Stephane Eranian60e23642014-09-24 13:48:37 +02009476 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9477 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009478out:
9479 return ret;
9480
9481err_size:
9482 put_user(sizeof(*attr), &uattr->size);
9483 ret = -E2BIG;
9484 goto out;
9485}
9486
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009487static int
9488perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009489{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009490 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009491 int ret = -EINVAL;
9492
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009493 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009494 goto set;
9495
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009496 /* don't allow circular references */
9497 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009498 goto out;
9499
Peter Zijlstra0f139302010-05-20 14:35:15 +02009500 /*
9501 * Don't allow cross-cpu buffers
9502 */
9503 if (output_event->cpu != event->cpu)
9504 goto out;
9505
9506 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009507 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009508 */
9509 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9510 goto out;
9511
Peter Zijlstra34f43922015-02-20 14:05:38 +01009512 /*
9513 * Mixing clocks in the same buffer is trouble you don't need.
9514 */
9515 if (output_event->clock != event->clock)
9516 goto out;
9517
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009518 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009519 * Either writing ring buffer from beginning or from end.
9520 * Mixing is not allowed.
9521 */
9522 if (is_write_backward(output_event) != is_write_backward(event))
9523 goto out;
9524
9525 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009526 * If both events generate aux data, they must be on the same PMU
9527 */
9528 if (has_aux(event) && has_aux(output_event) &&
9529 event->pmu != output_event->pmu)
9530 goto out;
9531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009532set:
9533 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009534 /* Can't redirect output if we've got an active mmap() */
9535 if (atomic_read(&event->mmap_count))
9536 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009537
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009538 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009539 /* get the rb we want to redirect to */
9540 rb = ring_buffer_get(output_event);
9541 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009542 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009543 }
9544
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009545 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009547 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009548unlock:
9549 mutex_unlock(&event->mmap_mutex);
9550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009551out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009552 return ret;
9553}
9554
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009555static void mutex_lock_double(struct mutex *a, struct mutex *b)
9556{
9557 if (b < a)
9558 swap(a, b);
9559
9560 mutex_lock(a);
9561 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9562}
9563
Peter Zijlstra34f43922015-02-20 14:05:38 +01009564static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9565{
9566 bool nmi_safe = false;
9567
9568 switch (clk_id) {
9569 case CLOCK_MONOTONIC:
9570 event->clock = &ktime_get_mono_fast_ns;
9571 nmi_safe = true;
9572 break;
9573
9574 case CLOCK_MONOTONIC_RAW:
9575 event->clock = &ktime_get_raw_fast_ns;
9576 nmi_safe = true;
9577 break;
9578
9579 case CLOCK_REALTIME:
9580 event->clock = &ktime_get_real_ns;
9581 break;
9582
9583 case CLOCK_BOOTTIME:
9584 event->clock = &ktime_get_boot_ns;
9585 break;
9586
9587 case CLOCK_TAI:
9588 event->clock = &ktime_get_tai_ns;
9589 break;
9590
9591 default:
9592 return -EINVAL;
9593 }
9594
9595 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9596 return -EINVAL;
9597
9598 return 0;
9599}
9600
Peter Zijlstra922813f2017-01-11 21:09:50 +01009601/*
9602 * Variation on perf_event_ctx_lock_nested(), except we take two context
9603 * mutexes.
9604 */
9605static struct perf_event_context *
9606__perf_event_ctx_lock_double(struct perf_event *group_leader,
9607 struct perf_event_context *ctx)
9608{
9609 struct perf_event_context *gctx;
9610
9611again:
9612 rcu_read_lock();
9613 gctx = READ_ONCE(group_leader->ctx);
9614 if (!atomic_inc_not_zero(&gctx->refcount)) {
9615 rcu_read_unlock();
9616 goto again;
9617 }
9618 rcu_read_unlock();
9619
9620 mutex_lock_double(&gctx->mutex, &ctx->mutex);
9621
9622 if (group_leader->ctx != gctx) {
9623 mutex_unlock(&ctx->mutex);
9624 mutex_unlock(&gctx->mutex);
9625 put_ctx(gctx);
9626 goto again;
9627 }
9628
9629 return gctx;
9630}
9631
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009632/**
9633 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9634 *
9635 * @attr_uptr: event_id type attributes for monitoring/sampling
9636 * @pid: target pid
9637 * @cpu: target cpu
9638 * @group_fd: group leader event fd
9639 */
9640SYSCALL_DEFINE5(perf_event_open,
9641 struct perf_event_attr __user *, attr_uptr,
9642 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9643{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009644 struct perf_event *group_leader = NULL, *output_event = NULL;
9645 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009646 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009647 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009648 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009649 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009650 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009651 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009652 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009653 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009654 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009655 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009656 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009657
9658 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009659 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009660 return -EINVAL;
9661
9662 err = perf_copy_attr(attr_uptr, &attr);
9663 if (err)
9664 return err;
9665
9666 if (!attr.exclude_kernel) {
9667 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9668 return -EACCES;
9669 }
9670
9671 if (attr.freq) {
9672 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9673 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009674 } else {
9675 if (attr.sample_period & (1ULL << 63))
9676 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009677 }
9678
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009679 if (!attr.sample_max_stack)
9680 attr.sample_max_stack = sysctl_perf_event_max_stack;
9681
Stephane Eraniane5d13672011-02-14 11:20:01 +02009682 /*
9683 * In cgroup mode, the pid argument is used to pass the fd
9684 * opened to the cgroup directory in cgroupfs. The cpu argument
9685 * designates the cpu on which to monitor threads from that
9686 * cgroup.
9687 */
9688 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9689 return -EINVAL;
9690
Yann Droneauda21b0b32014-01-05 21:36:33 +01009691 if (flags & PERF_FLAG_FD_CLOEXEC)
9692 f_flags |= O_CLOEXEC;
9693
9694 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009695 if (event_fd < 0)
9696 return event_fd;
9697
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009698 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009699 err = perf_fget_light(group_fd, &group);
9700 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009701 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009702 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009703 if (flags & PERF_FLAG_FD_OUTPUT)
9704 output_event = group_leader;
9705 if (flags & PERF_FLAG_FD_NO_GROUP)
9706 group_leader = NULL;
9707 }
9708
Stephane Eraniane5d13672011-02-14 11:20:01 +02009709 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009710 task = find_lively_task_by_vpid(pid);
9711 if (IS_ERR(task)) {
9712 err = PTR_ERR(task);
9713 goto err_group_fd;
9714 }
9715 }
9716
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009717 if (task && group_leader &&
9718 group_leader->attr.inherit != attr.inherit) {
9719 err = -EINVAL;
9720 goto err_task;
9721 }
9722
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009723 get_online_cpus();
9724
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009725 if (task) {
9726 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9727 if (err)
9728 goto err_cpus;
9729
9730 /*
9731 * Reuse ptrace permission checks for now.
9732 *
9733 * We must hold cred_guard_mutex across this and any potential
9734 * perf_install_in_context() call for this new event to
9735 * serialize against exec() altering our credentials (and the
9736 * perf_event_exit_task() that could imply).
9737 */
9738 err = -EACCES;
9739 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9740 goto err_cred;
9741 }
9742
Matt Fleming79dff512015-01-23 18:45:42 +00009743 if (flags & PERF_FLAG_PID_CGROUP)
9744 cgroup_fd = pid;
9745
Avi Kivity4dc0da82011-06-29 18:42:35 +03009746 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009747 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009748 if (IS_ERR(event)) {
9749 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009750 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009751 }
9752
Vince Weaver53b25332014-05-16 17:12:12 -04009753 if (is_sampling_event(event)) {
9754 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309755 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009756 goto err_alloc;
9757 }
9758 }
9759
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009760 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009761 * Special case software events and allow them to be part of
9762 * any hardware group.
9763 */
9764 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009765
Peter Zijlstra34f43922015-02-20 14:05:38 +01009766 if (attr.use_clockid) {
9767 err = perf_event_set_clock(event, attr.clockid);
9768 if (err)
9769 goto err_alloc;
9770 }
9771
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009772 if (pmu->task_ctx_nr == perf_sw_context)
9773 event->event_caps |= PERF_EV_CAP_SOFTWARE;
9774
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009775 if (group_leader &&
9776 (is_software_event(event) != is_software_event(group_leader))) {
9777 if (is_software_event(event)) {
9778 /*
9779 * If event and group_leader are not both a software
9780 * event, and event is, then group leader is not.
9781 *
9782 * Allow the addition of software events to !software
9783 * groups, this is safe because software events never
9784 * fail to schedule.
9785 */
9786 pmu = group_leader->pmu;
9787 } else if (is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07009788 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009789 /*
9790 * In case the group is a pure software group, and we
9791 * try to add a hardware event, move the whole group to
9792 * the hardware context.
9793 */
9794 move_group = 1;
9795 }
9796 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009797
9798 /*
9799 * Get the target context (task or percpu):
9800 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009801 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009802 if (IS_ERR(ctx)) {
9803 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009804 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009805 }
9806
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009807 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9808 err = -EBUSY;
9809 goto err_context;
9810 }
9811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009812 /*
9813 * Look up the group leader (we will attach this event to it):
9814 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009815 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009816 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009817
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009818 /*
9819 * Do not allow a recursive hierarchy (this new sibling
9820 * becoming part of another group-sibling):
9821 */
9822 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009823 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009824
9825 /* All events in a group should have the same clock */
9826 if (group_leader->clock != event->clock)
9827 goto err_context;
9828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009829 /*
Mark Rutlandbde66082017-06-22 15:41:38 +01009830 * Make sure we're both events for the same CPU;
9831 * grouping events for different CPUs is broken; since
9832 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009833 */
Mark Rutlandbde66082017-06-22 15:41:38 +01009834 if (group_leader->cpu != event->cpu)
9835 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009836
Mark Rutlandbde66082017-06-22 15:41:38 +01009837 /*
9838 * Make sure we're both on the same task, or both
9839 * per-CPU events.
9840 */
9841 if (group_leader->ctx->task != ctx->task)
9842 goto err_context;
9843
9844 /*
9845 * Do not allow to attach to a group in a different task
9846 * or CPU context. If we're moving SW events, we'll fix
9847 * this up later, so allow that.
9848 */
9849 if (!move_group && group_leader->ctx != ctx)
9850 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009851
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009852 /*
9853 * Only a group leader can be exclusive or pinned
9854 */
9855 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009856 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009857 }
9858
9859 if (output_event) {
9860 err = perf_event_set_output(event, output_event);
9861 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009862 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009863 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009864
Yann Droneauda21b0b32014-01-05 21:36:33 +01009865 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9866 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009867 if (IS_ERR(event_file)) {
9868 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009869 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009870 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009871 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009872
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009873 if (move_group) {
Peter Zijlstra922813f2017-01-11 21:09:50 +01009874 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
9875
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009876 if (gctx->task == TASK_TOMBSTONE) {
9877 err = -ESRCH;
9878 goto err_locked;
9879 }
Peter Zijlstra922813f2017-01-11 21:09:50 +01009880
9881 /*
9882 * Check if we raced against another sys_perf_event_open() call
9883 * moving the software group underneath us.
9884 */
9885 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
9886 /*
9887 * If someone moved the group out from under us, check
9888 * if this new event wound up on the same ctx, if so
9889 * its the regular !move_group case, otherwise fail.
9890 */
9891 if (gctx != ctx) {
9892 err = -EINVAL;
9893 goto err_locked;
9894 } else {
9895 perf_event_ctx_unlock(group_leader, gctx);
9896 move_group = 0;
9897 }
9898 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009899 } else {
9900 mutex_lock(&ctx->mutex);
9901 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009902
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009903 if (ctx->task == TASK_TOMBSTONE) {
9904 err = -ESRCH;
9905 goto err_locked;
9906 }
9907
Peter Zijlstraa7239682015-09-09 19:06:33 +02009908 if (!perf_event_validate_size(event)) {
9909 err = -E2BIG;
9910 goto err_locked;
9911 }
9912
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009913 /*
9914 * Must be under the same ctx::mutex as perf_install_in_context(),
9915 * because we need to serialize with concurrent event creation.
9916 */
9917 if (!exclusive_event_installable(event, ctx)) {
9918 /* exclusive and group stuff are assumed mutually exclusive */
9919 WARN_ON_ONCE(move_group);
9920
9921 err = -EBUSY;
9922 goto err_locked;
9923 }
9924
9925 WARN_ON_ONCE(ctx->parent_ctx);
9926
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009927 /*
9928 * This is the point on no return; we cannot fail hereafter. This is
9929 * where we start modifying current state.
9930 */
9931
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009932 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009933 /*
9934 * See perf_event_ctx_lock() for comments on the details
9935 * of swizzling perf_event::ctx.
9936 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009937 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009938
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009939 list_for_each_entry(sibling, &group_leader->sibling_list,
9940 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009941 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009942 put_ctx(gctx);
9943 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009944
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009945 /*
9946 * Wait for everybody to stop referencing the events through
9947 * the old lists, before installing it on new lists.
9948 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009949 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009950
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009951 /*
9952 * Install the group siblings before the group leader.
9953 *
9954 * Because a group leader will try and install the entire group
9955 * (through the sibling list, which is still in-tact), we can
9956 * end up with siblings installed in the wrong context.
9957 *
9958 * By installing siblings first we NO-OP because they're not
9959 * reachable through the group lists.
9960 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009961 list_for_each_entry(sibling, &group_leader->sibling_list,
9962 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009963 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009964 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009965 get_ctx(ctx);
9966 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009967
9968 /*
9969 * Removing from the context ends up with disabled
9970 * event. What we want here is event in the initial
9971 * startup state, ready to be add into new context.
9972 */
9973 perf_event__state_init(group_leader);
9974 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9975 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009976
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009977 /*
9978 * Now that all events are installed in @ctx, nothing
9979 * references @gctx anymore, so drop the last reference we have
9980 * on it.
9981 */
9982 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009983 }
9984
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009985 /*
9986 * Precalculate sample_data sizes; do while holding ctx::mutex such
9987 * that we're serialized against further additions and before
9988 * perf_install_in_context() which is the point the event is active and
9989 * can use these values.
9990 */
9991 perf_event__header_size(event);
9992 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009993
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009994 event->owner = current;
9995
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08009996 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009997 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009998
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009999 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010000 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010001 mutex_unlock(&ctx->mutex);
10002
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010003 if (task) {
10004 mutex_unlock(&task->signal->cred_guard_mutex);
10005 put_task_struct(task);
10006 }
10007
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010008 put_online_cpus();
10009
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010010 mutex_lock(&current->perf_event_mutex);
10011 list_add_tail(&event->owner_entry, &current->perf_event_list);
10012 mutex_unlock(&current->perf_event_mutex);
10013
Peter Zijlstra8a495422010-05-27 15:47:49 +020010014 /*
10015 * Drop the reference on the group_event after placing the
10016 * new event on the sibling_list. This ensures destruction
10017 * of the group leader will find the pointer to itself in
10018 * perf_group_detach().
10019 */
Al Viro2903ff02012-08-28 12:52:22 -040010020 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010021 fd_install(event_fd, event_file);
10022 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010023
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010024err_locked:
10025 if (move_group)
Peter Zijlstra922813f2017-01-11 21:09:50 +010010026 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020010027 mutex_unlock(&ctx->mutex);
10028/* err_file: */
10029 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010030err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010031 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040010032 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020010033err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010010034 /*
10035 * If event_file is set, the fput() above will have called ->release()
10036 * and that will take care of freeing the event.
10037 */
10038 if (!event_file)
10039 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010040err_cred:
10041 if (task)
10042 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010043err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +080010044 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020010045err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020010046 if (task)
10047 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010048err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040010049 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040010050err_fd:
10051 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010052 return err;
10053}
10054
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010055/**
10056 * perf_event_create_kernel_counter
10057 *
10058 * @attr: attributes of the counter to create
10059 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070010060 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010061 */
10062struct perf_event *
10063perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070010064 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030010065 perf_overflow_handler_t overflow_handler,
10066 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010067{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010068 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010069 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010070 int err;
10071
10072 /*
10073 * Get the target context (task or percpu):
10074 */
10075
Avi Kivity4dc0da82011-06-29 18:42:35 +030010076 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000010077 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010078 if (IS_ERR(event)) {
10079 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010080 goto err;
10081 }
10082
Jiri Olsaf8697762014-08-01 14:33:01 +020010083 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010084 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020010085
Yan, Zheng4af57ef282014-11-04 21:56:01 -050010086 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010087 if (IS_ERR(ctx)) {
10088 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010089 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010090 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010091
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010092 WARN_ON_ONCE(ctx->parent_ctx);
10093 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010094 if (ctx->task == TASK_TOMBSTONE) {
10095 err = -ESRCH;
10096 goto err_unlock;
10097 }
10098
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010099 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010100 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010101 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010102 }
10103
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010104 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010105 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010106 mutex_unlock(&ctx->mutex);
10107
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010108 return event;
10109
Peter Zijlstra84c4e622016-02-24 18:45:40 +010010110err_unlock:
10111 mutex_unlock(&ctx->mutex);
10112 perf_unpin_context(ctx);
10113 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010114err_free:
10115 free_event(event);
10116err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010010117 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020010118}
10119EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
10120
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010121void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
10122{
10123 struct perf_event_context *src_ctx;
10124 struct perf_event_context *dst_ctx;
10125 struct perf_event *event, *tmp;
10126 LIST_HEAD(events);
10127
10128 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
10129 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
10130
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010131 /*
10132 * See perf_event_ctx_lock() for comments on the details
10133 * of swizzling perf_event::ctx.
10134 */
10135 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010136 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
10137 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010138 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010139 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010140 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020010141 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010142 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010143
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010144 /*
10145 * Wait for the events to quiesce before re-instating them.
10146 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010147 synchronize_rcu();
10148
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010010149 /*
10150 * Re-instate events in 2 passes.
10151 *
10152 * Skip over group leaders and only install siblings on this first
10153 * pass, siblings will not get enabled without a leader, however a
10154 * leader will enable its siblings, even if those are still on the old
10155 * context.
10156 */
10157 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10158 if (event->group_leader == event)
10159 continue;
10160
10161 list_del(&event->migrate_entry);
10162 if (event->state >= PERF_EVENT_STATE_OFF)
10163 event->state = PERF_EVENT_STATE_INACTIVE;
10164 account_event_cpu(event, dst_cpu);
10165 perf_install_in_context(dst_ctx, event, dst_cpu);
10166 get_ctx(dst_ctx);
10167 }
10168
10169 /*
10170 * Once all the siblings are setup properly, install the group leaders
10171 * to make it go.
10172 */
Peter Zijlstra98861672013-10-03 16:02:23 +020010173 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
10174 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010175 if (event->state >= PERF_EVENT_STATE_OFF)
10176 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020010177 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010178 perf_install_in_context(dst_ctx, event, dst_cpu);
10179 get_ctx(dst_ctx);
10180 }
10181 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010010182 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080010183}
10184EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
10185
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010186static void sync_child_event(struct perf_event *child_event,
10187 struct task_struct *child)
10188{
10189 struct perf_event *parent_event = child_event->parent;
10190 u64 child_val;
10191
10192 if (child_event->attr.inherit_stat)
10193 perf_event_read_event(child_event, child);
10194
Peter Zijlstrab5e58792010-05-21 14:43:12 +020010195 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010196
10197 /*
10198 * Add back the child's count to the parent's count:
10199 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020010200 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010201 atomic64_add(child_event->total_time_enabled,
10202 &parent_event->child_total_time_enabled);
10203 atomic64_add(child_event->total_time_running,
10204 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010205}
10206
10207static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010208perf_event_exit_event(struct perf_event *child_event,
10209 struct perf_event_context *child_ctx,
10210 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010211{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010212 struct perf_event *parent_event = child_event->parent;
10213
Peter Zijlstra1903d502014-07-15 17:27:27 +020010214 /*
10215 * Do not destroy the 'original' grouping; because of the context
10216 * switch optimization the original events could've ended up in a
10217 * random child task.
10218 *
10219 * If we were to destroy the original group, all group related
10220 * operations would cease to function properly after this random
10221 * child dies.
10222 *
10223 * Do destroy all inherited groups, we don't care about those
10224 * and being thorough is better.
10225 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010226 raw_spin_lock_irq(&child_ctx->lock);
10227 WARN_ON_ONCE(child_ctx->is_active);
10228
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010229 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010010230 perf_group_detach(child_event);
10231 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +010010232 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010010233 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010234
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010235 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010236 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010237 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010238 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040010239 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010240 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010241 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010242 /*
10243 * Child events can be cleaned up.
10244 */
10245
10246 sync_child_event(child_event, child);
10247
10248 /*
10249 * Remove this event from the parent's list
10250 */
10251 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
10252 mutex_lock(&parent_event->child_mutex);
10253 list_del_init(&child_event->child_list);
10254 mutex_unlock(&parent_event->child_mutex);
10255
10256 /*
10257 * Kick perf_poll() for is_event_hup().
10258 */
10259 perf_event_wakeup(parent_event);
10260 free_event(child_event);
10261 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010262}
10263
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010264static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010265{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010266 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010267 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010268
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010269 WARN_ON_ONCE(child != current);
10270
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010271 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010272 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010273 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010274
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010275 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010276 * In order to reduce the amount of tricky in ctx tear-down, we hold
10277 * ctx::mutex over the entire thing. This serializes against almost
10278 * everything that wants to access the ctx.
10279 *
10280 * The exception is sys_perf_event_open() /
10281 * perf_event_create_kernel_count() which does find_get_context()
10282 * without ctx::mutex (it cannot because of the move_group double mutex
10283 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010284 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010285 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010286
10287 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010288 * In a single ctx::lock section, de-schedule the events and detach the
10289 * context from the task such that we cannot ever get it scheduled back
10290 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010291 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010292 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010293 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010294
10295 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010296 * Now that the context is inactive, destroy the task <-> ctx relation
10297 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010298 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010010299 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
10300 put_ctx(child_ctx); /* cannot be last */
10301 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
10302 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010303
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010304 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010010305 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010306
Peter Zijlstra211de6e2014-09-30 19:23:08 +020010307 if (clone_ctx)
10308 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020010309
10310 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010311 * Report the task dead after unscheduling the events so that we
10312 * won't get any samples after PERF_RECORD_EXIT. We can however still
10313 * get a few PERF_RECORD_READ events.
10314 */
10315 perf_event_task(child, child_ctx, 0);
10316
Peter Zijlstraebf905f2014-05-29 19:00:24 +020010317 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010010318 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010319
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010320 mutex_unlock(&child_ctx->mutex);
10321
10322 put_ctx(child_ctx);
10323}
10324
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010325/*
10326 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020010327 *
10328 * Can be called with cred_guard_mutex held when called from
10329 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010330 */
10331void perf_event_exit_task(struct task_struct *child)
10332{
Peter Zijlstra88821352010-11-09 19:01:43 +010010333 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010334 int ctxn;
10335
Peter Zijlstra88821352010-11-09 19:01:43 +010010336 mutex_lock(&child->perf_event_mutex);
10337 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
10338 owner_entry) {
10339 list_del_init(&event->owner_entry);
10340
10341 /*
10342 * Ensure the list deletion is visible before we clear
10343 * the owner, closes a race against perf_release() where
10344 * we need to serialize on the owner->perf_event_mutex.
10345 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010346 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010347 }
10348 mutex_unlock(&child->perf_event_mutex);
10349
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010350 for_each_task_context_nr(ctxn)
10351 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010352
10353 /*
10354 * The perf_event_exit_task_context calls perf_event_task
10355 * with child's task_ctx, which generates EXIT events for
10356 * child contexts and sets child->perf_event_ctxp[] to NULL.
10357 * At this point we need to send EXIT events to cpu contexts.
10358 */
10359 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010360}
10361
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010362static void perf_free_event(struct perf_event *event,
10363 struct perf_event_context *ctx)
10364{
10365 struct perf_event *parent = event->parent;
10366
10367 if (WARN_ON_ONCE(!parent))
10368 return;
10369
10370 mutex_lock(&parent->child_mutex);
10371 list_del_init(&event->child_list);
10372 mutex_unlock(&parent->child_mutex);
10373
Al Viroa6fa9412012-08-20 14:59:25 +010010374 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010375
Peter Zijlstra652884f2015-01-23 11:20:10 +010010376 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010377 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010378 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010379 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010380 free_event(event);
10381}
10382
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010383/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010384 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010385 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010386 *
10387 * Not all locks are strictly required, but take them anyway to be nice and
10388 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010389 */
10390void perf_event_free_task(struct task_struct *task)
10391{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010392 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010393 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010394 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010395
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010396 for_each_task_context_nr(ctxn) {
10397 ctx = task->perf_event_ctxp[ctxn];
10398 if (!ctx)
10399 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010400
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010401 mutex_lock(&ctx->mutex);
Peter Zijlstrac04a9382017-03-16 13:47:48 +010010402 raw_spin_lock_irq(&ctx->lock);
10403 /*
10404 * Destroy the task <-> ctx relation and mark the context dead.
10405 *
10406 * This is important because even though the task hasn't been
10407 * exposed yet the context has been (through child_list).
10408 */
10409 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
10410 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
10411 put_task_struct(task); /* cannot be last */
10412 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010413again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010414 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10415 group_entry)
10416 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010417
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010418 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10419 group_entry)
10420 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010421
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010422 if (!list_empty(&ctx->pinned_groups) ||
10423 !list_empty(&ctx->flexible_groups))
10424 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010425
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010426 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010427
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010428 put_ctx(ctx);
10429 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010430}
10431
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010432void perf_event_delayed_put(struct task_struct *task)
10433{
10434 int ctxn;
10435
10436 for_each_task_context_nr(ctxn)
10437 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10438}
10439
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010440struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010441{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010442 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010443
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010444 file = fget_raw(fd);
10445 if (!file)
10446 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010447
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010448 if (file->f_op != &perf_fops) {
10449 fput(file);
10450 return ERR_PTR(-EBADF);
10451 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010452
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010453 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010454}
10455
10456const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10457{
10458 if (!event)
10459 return ERR_PTR(-EINVAL);
10460
10461 return &event->attr;
10462}
10463
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010464/*
10465 * inherit a event from parent task to child task:
10466 */
10467static struct perf_event *
10468inherit_event(struct perf_event *parent_event,
10469 struct task_struct *parent,
10470 struct perf_event_context *parent_ctx,
10471 struct task_struct *child,
10472 struct perf_event *group_leader,
10473 struct perf_event_context *child_ctx)
10474{
Jiri Olsa1929def2014-09-12 13:18:27 +020010475 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010476 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010477 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010478
10479 /*
10480 * Instead of creating recursive hierarchies of events,
10481 * we link inherited events back to the original parent,
10482 * which has a filp for sure, which we use as the reference
10483 * count:
10484 */
10485 if (parent_event->parent)
10486 parent_event = parent_event->parent;
10487
10488 child_event = perf_event_alloc(&parent_event->attr,
10489 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010490 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010491 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010492 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010493 if (IS_ERR(child_event))
10494 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010495
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010496 /*
10497 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10498 * must be under the same lock in order to serialize against
10499 * perf_event_release_kernel(), such that either we must observe
10500 * is_orphaned_event() or they will observe us on the child_list.
10501 */
10502 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010503 if (is_orphaned_event(parent_event) ||
10504 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010505 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010506 free_event(child_event);
10507 return NULL;
10508 }
10509
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010510 get_ctx(child_ctx);
10511
10512 /*
10513 * Make the child state follow the state of the parent event,
10514 * not its attr.disabled bit. We hold the parent's mutex,
10515 * so we won't race with perf_event_{en, dis}able_family.
10516 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010517 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010518 child_event->state = PERF_EVENT_STATE_INACTIVE;
10519 else
10520 child_event->state = PERF_EVENT_STATE_OFF;
10521
10522 if (parent_event->attr.freq) {
10523 u64 sample_period = parent_event->hw.sample_period;
10524 struct hw_perf_event *hwc = &child_event->hw;
10525
10526 hwc->sample_period = sample_period;
10527 hwc->last_period = sample_period;
10528
10529 local64_set(&hwc->period_left, sample_period);
10530 }
10531
10532 child_event->ctx = child_ctx;
10533 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010534 child_event->overflow_handler_context
10535 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010536
10537 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010538 * Precalculate sample_data sizes
10539 */
10540 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010541 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010542
10543 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010544 * Link it up in the child's context:
10545 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010546 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010547 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010548 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010549
10550 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010551 * Link this into the parent event's child list
10552 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010553 list_add_tail(&child_event->child_list, &parent_event->child_list);
10554 mutex_unlock(&parent_event->child_mutex);
10555
10556 return child_event;
10557}
10558
10559static int inherit_group(struct perf_event *parent_event,
10560 struct task_struct *parent,
10561 struct perf_event_context *parent_ctx,
10562 struct task_struct *child,
10563 struct perf_event_context *child_ctx)
10564{
10565 struct perf_event *leader;
10566 struct perf_event *sub;
10567 struct perf_event *child_ctr;
10568
10569 leader = inherit_event(parent_event, parent, parent_ctx,
10570 child, NULL, child_ctx);
10571 if (IS_ERR(leader))
10572 return PTR_ERR(leader);
10573 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10574 child_ctr = inherit_event(sub, parent, parent_ctx,
10575 child, leader, child_ctx);
10576 if (IS_ERR(child_ctr))
10577 return PTR_ERR(child_ctr);
10578 }
10579 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010580}
10581
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010582static int
10583inherit_task_group(struct perf_event *event, struct task_struct *parent,
10584 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010585 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010586 int *inherited_all)
10587{
10588 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010589 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010590
10591 if (!event->attr.inherit) {
10592 *inherited_all = 0;
10593 return 0;
10594 }
10595
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010596 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010597 if (!child_ctx) {
10598 /*
10599 * This is executed from the parent task context, so
10600 * inherit events that have been marked for cloning.
10601 * First allocate and initialize a context for the
10602 * child.
10603 */
10604
Jiri Olsa734df5a2013-07-09 17:44:10 +020010605 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010606 if (!child_ctx)
10607 return -ENOMEM;
10608
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010609 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010610 }
10611
10612 ret = inherit_group(event, parent, parent_ctx,
10613 child, child_ctx);
10614
10615 if (ret)
10616 *inherited_all = 0;
10617
10618 return ret;
10619}
10620
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010621/*
10622 * Initialize the perf_event context in task_struct
10623 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010624static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010625{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010626 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010627 struct perf_event_context *cloned_ctx;
10628 struct perf_event *event;
10629 struct task_struct *parent = current;
10630 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010631 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010632 int ret = 0;
10633
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010634 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010635 return 0;
10636
10637 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010638 * If the parent's context is a clone, pin it so it won't get
10639 * swapped under us.
10640 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010641 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010642 if (!parent_ctx)
10643 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010644
10645 /*
10646 * No need to check if parent_ctx != NULL here; since we saw
10647 * it non-NULL earlier, the only reason for it to become NULL
10648 * is if we exit, and since we're currently in the middle of
10649 * a fork we can't be exiting at the same time.
10650 */
10651
10652 /*
10653 * Lock the parent list. No need to lock the child - not PID
10654 * hashed yet and not running, so nobody can access it.
10655 */
10656 mutex_lock(&parent_ctx->mutex);
10657
10658 /*
10659 * We dont have to disable NMIs - we are only looking at
10660 * the list, not manipulating it:
10661 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010662 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010663 ret = inherit_task_group(event, parent, parent_ctx,
10664 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010665 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010666 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010667 }
10668
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010669 /*
10670 * We can't hold ctx->lock when iterating the ->flexible_group list due
10671 * to allocations, but we need to prevent rotation because
10672 * rotate_ctx() will change the list from interrupt context.
10673 */
10674 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10675 parent_ctx->rotate_disable = 1;
10676 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10677
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010678 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010679 ret = inherit_task_group(event, parent, parent_ctx,
10680 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010681 if (ret)
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010682 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010683 }
10684
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010685 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10686 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010687
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010688 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010689
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010690 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010691 /*
10692 * Mark the child context as a clone of the parent
10693 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010694 *
10695 * Note that if the parent is a clone, the holding of
10696 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010697 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010698 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010699 if (cloned_ctx) {
10700 child_ctx->parent_ctx = cloned_ctx;
10701 child_ctx->parent_gen = parent_ctx->parent_gen;
10702 } else {
10703 child_ctx->parent_ctx = parent_ctx;
10704 child_ctx->parent_gen = parent_ctx->generation;
10705 }
10706 get_ctx(child_ctx->parent_ctx);
10707 }
10708
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010709 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstra69efd8e2017-03-16 13:47:49 +010010710out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010711 mutex_unlock(&parent_ctx->mutex);
10712
10713 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010714 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010715
10716 return ret;
10717}
10718
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010719/*
10720 * Initialize the perf_event context in task_struct
10721 */
10722int perf_event_init_task(struct task_struct *child)
10723{
10724 int ctxn, ret;
10725
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010726 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10727 mutex_init(&child->perf_event_mutex);
10728 INIT_LIST_HEAD(&child->perf_event_list);
10729
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010730 for_each_task_context_nr(ctxn) {
10731 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010732 if (ret) {
10733 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010734 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010735 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010736 }
10737
10738 return 0;
10739}
10740
Paul Mackerras220b1402010-03-10 20:45:52 +110010741static void __init perf_event_init_all_cpus(void)
10742{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010743 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010744 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010745
10746 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010747 swhash = &per_cpu(swevent_htable, cpu);
10748 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010749 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010750
10751 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10752 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010753
10754 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010755 }
10756}
10757
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010758int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010759{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010760 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010761
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010762 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010763 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010764 struct swevent_hlist *hlist;
10765
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010766 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10767 WARN_ON(!hlist);
10768 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010769 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010770 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010771 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010772}
10773
Dave Young2965faa2015-09-09 15:38:55 -070010774#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010775static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010776{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010777 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010778 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10779 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010780
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010781 raw_spin_lock(&ctx->lock);
10782 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010783 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010784 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010785}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010786
10787static void perf_event_exit_cpu_context(int cpu)
10788{
10789 struct perf_event_context *ctx;
10790 struct pmu *pmu;
10791 int idx;
10792
10793 idx = srcu_read_lock(&pmus_srcu);
10794 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010795 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010796
10797 mutex_lock(&ctx->mutex);
10798 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10799 mutex_unlock(&ctx->mutex);
10800 }
10801 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010802}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010803#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010804
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010805static void perf_event_exit_cpu_context(int cpu) { }
10806
10807#endif
10808
10809int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010810{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010811 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010812 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010813}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010814
Peter Zijlstrac2774432010-12-08 15:29:02 +010010815static int
10816perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10817{
10818 int cpu;
10819
10820 for_each_online_cpu(cpu)
10821 perf_event_exit_cpu(cpu);
10822
10823 return NOTIFY_OK;
10824}
10825
10826/*
10827 * Run the perf reboot notifier at the very last possible moment so that
10828 * the generic watchdog code runs as long as possible.
10829 */
10830static struct notifier_block perf_reboot_notifier = {
10831 .notifier_call = perf_reboot,
10832 .priority = INT_MIN,
10833};
10834
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010835void __init perf_event_init(void)
10836{
Jason Wessel3c502e72010-11-04 17:33:01 -050010837 int ret;
10838
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010839 idr_init(&pmu_idr);
10840
Paul Mackerras220b1402010-03-10 20:45:52 +110010841 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010842 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010843 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10844 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10845 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010846 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010847 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010848 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010849
10850 ret = init_hw_breakpoint();
10851 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010852
Jiri Olsab01c3a02012-03-23 15:41:20 +010010853 /*
10854 * Build time assertion that we keep the data_head at the intended
10855 * location. IOW, validation we got the __reserved[] size right.
10856 */
10857 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10858 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010859}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010860
Cody P Schaferfd979c02015-01-30 13:45:57 -080010861ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10862 char *page)
10863{
10864 struct perf_pmu_events_attr *pmu_attr =
10865 container_of(attr, struct perf_pmu_events_attr, attr);
10866
10867 if (pmu_attr->event_str)
10868 return sprintf(page, "%s\n", pmu_attr->event_str);
10869
10870 return 0;
10871}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010872EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010873
Peter Zijlstraabe43402010-11-17 23:17:37 +010010874static int __init perf_event_sysfs_init(void)
10875{
10876 struct pmu *pmu;
10877 int ret;
10878
10879 mutex_lock(&pmus_lock);
10880
10881 ret = bus_register(&pmu_bus);
10882 if (ret)
10883 goto unlock;
10884
10885 list_for_each_entry(pmu, &pmus, entry) {
10886 if (!pmu->name || pmu->type < 0)
10887 continue;
10888
10889 ret = pmu_dev_alloc(pmu);
10890 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10891 }
10892 pmu_bus_running = 1;
10893 ret = 0;
10894
10895unlock:
10896 mutex_unlock(&pmus_lock);
10897
10898 return ret;
10899}
10900device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010901
10902#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010903static struct cgroup_subsys_state *
10904perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010905{
10906 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010907
Li Zefan1b15d052011-03-03 14:26:06 +080010908 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010909 if (!jc)
10910 return ERR_PTR(-ENOMEM);
10911
Stephane Eraniane5d13672011-02-14 11:20:01 +020010912 jc->info = alloc_percpu(struct perf_cgroup_info);
10913 if (!jc->info) {
10914 kfree(jc);
10915 return ERR_PTR(-ENOMEM);
10916 }
10917
Stephane Eraniane5d13672011-02-14 11:20:01 +020010918 return &jc->css;
10919}
10920
Tejun Heoeb954192013-08-08 20:11:23 -040010921static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010922{
Tejun Heoeb954192013-08-08 20:11:23 -040010923 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10924
Stephane Eraniane5d13672011-02-14 11:20:01 +020010925 free_percpu(jc->info);
10926 kfree(jc);
10927}
10928
10929static int __perf_cgroup_move(void *info)
10930{
10931 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010932 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010933 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010934 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010935 return 0;
10936}
10937
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010938static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010939{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010940 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010941 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010942
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010943 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010944 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010945}
10946
Tejun Heo073219e2014-02-08 10:36:58 -050010947struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010948 .css_alloc = perf_cgroup_css_alloc,
10949 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010950 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010951};
10952#endif /* CONFIG_CGROUP_PERF */