blob: 57aff715039fe42c4b2ea812000b798ec7070cc1 [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
245static void event_function_local(struct perf_event *event, event_f func, void *data)
246{
247 struct event_function_struct efs = {
248 .event = event,
249 .func = func,
250 .data = data,
251 };
252
253 int ret = event_function(&efs);
254 WARN_ON_ONCE(ret);
255}
256
257static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100258{
259 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100260 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100261 struct event_function_struct efs = {
262 .event = event,
263 .func = func,
264 .data = data,
265 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100266
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100267 if (!event->parent) {
268 /*
269 * If this is a !child event, we must hold ctx::mutex to
270 * stabilize the the event->ctx relation. See
271 * perf_event_ctx_lock().
272 */
273 lockdep_assert_held(&ctx->mutex);
274 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100275
276 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100277 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100278 return;
279 }
280
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100281 if (task == TASK_TOMBSTONE)
282 return;
283
Peter Zijlstraa0963092016-02-24 18:45:50 +0100284again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100285 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100286 return;
287
288 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100289 /*
290 * Reload the task pointer, it might have been changed by
291 * a concurrent perf_event_context_sched_out().
292 */
293 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100294 if (task == TASK_TOMBSTONE) {
295 raw_spin_unlock_irq(&ctx->lock);
296 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100297 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100298 if (ctx->is_active) {
299 raw_spin_unlock_irq(&ctx->lock);
300 goto again;
301 }
302 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100303 raw_spin_unlock_irq(&ctx->lock);
304}
305
Stephane Eraniane5d13672011-02-14 11:20:01 +0200306#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
307 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100308 PERF_FLAG_PID_CGROUP |\
309 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200310
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100311/*
312 * branch priv levels that need permission checks
313 */
314#define PERF_SAMPLE_BRANCH_PERM_PLM \
315 (PERF_SAMPLE_BRANCH_KERNEL |\
316 PERF_SAMPLE_BRANCH_HV)
317
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200318enum event_type_t {
319 EVENT_FLEXIBLE = 0x1,
320 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100321 EVENT_TIME = 0x4,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200322 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
323};
324
Stephane Eraniane5d13672011-02-14 11:20:01 +0200325/*
326 * perf_sched_events : >0 events exist
327 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
328 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100329
330static void perf_sched_delayed(struct work_struct *work);
331DEFINE_STATIC_KEY_FALSE(perf_sched_events);
332static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
333static DEFINE_MUTEX(perf_sched_mutex);
334static atomic_t perf_sched_count;
335
Stephane Eraniane5d13672011-02-14 11:20:01 +0200336static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500337static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Kan Liangf2fb6be2016-03-23 11:24:37 -0700338static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200339
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200340static atomic_t nr_mmap_events __read_mostly;
341static atomic_t nr_comm_events __read_mostly;
342static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200343static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300344static atomic_t nr_switch_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200345
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200346static LIST_HEAD(pmus);
347static DEFINE_MUTEX(pmus_lock);
348static struct srcu_struct pmus_srcu;
349
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200350/*
351 * perf event paranoia level:
352 * -1 - not paranoid at all
353 * 0 - disallow raw tracepoint access for unpriv
354 * 1 - disallow cpu events for unpriv
355 * 2 - disallow kernel profiling for unpriv
356 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700357int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200358
Frederic Weisbecker20443382011-03-31 03:33:29 +0200359/* Minimum for 512 kiB + 1 user control page */
360int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200361
362/*
363 * max perf event sample rate
364 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700365#define DEFAULT_MAX_SAMPLE_RATE 100000
366#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
367#define DEFAULT_CPU_TIME_MAX_PERCENT 25
368
369int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
370
371static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
372static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
373
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200374static int perf_sample_allowed_ns __read_mostly =
375 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700376
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800377static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700378{
379 u64 tmp = perf_sample_period_ns;
380
381 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100382 tmp = div_u64(tmp, 100);
383 if (!tmp)
384 tmp = 1;
385
386 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700387}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100388
Stephane Eranian9e630202013-04-03 14:21:33 +0200389static int perf_rotate_context(struct perf_cpu_context *cpuctx);
390
Peter Zijlstra163ec432011-02-16 11:22:34 +0100391int perf_proc_update_handler(struct ctl_table *table, int write,
392 void __user *buffer, size_t *lenp,
393 loff_t *ppos)
394{
Knut Petersen723478c2013-09-25 14:29:37 +0200395 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100396
397 if (ret || !write)
398 return ret;
399
Kan Liangab7fdef2016-05-03 00:26:06 -0700400 /*
401 * If throttling is disabled don't allow the write:
402 */
403 if (sysctl_perf_cpu_time_max_percent == 100 ||
404 sysctl_perf_cpu_time_max_percent == 0)
405 return -EINVAL;
406
Peter Zijlstra163ec432011-02-16 11:22:34 +0100407 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700408 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
409 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100410
411 return 0;
412}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200413
Dave Hansen14c63f12013-06-21 08:51:36 -0700414int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
415
416int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
417 void __user *buffer, size_t *lenp,
418 loff_t *ppos)
419{
420 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
421
422 if (ret || !write)
423 return ret;
424
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200425 if (sysctl_perf_cpu_time_max_percent == 100 ||
426 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100427 printk(KERN_WARNING
428 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
429 WRITE_ONCE(perf_sample_allowed_ns, 0);
430 } else {
431 update_perf_cpu_limits();
432 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700433
434 return 0;
435}
436
437/*
438 * perf samples are done in some very critical code paths (NMIs).
439 * If they take too much CPU time, the system can lock up and not
440 * get any real work done. This will drop the sample rate when
441 * we detect that events are taking too long.
442 */
443#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200444static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700445
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100446static u64 __report_avg;
447static u64 __report_allowed;
448
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100449static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700450{
David Ahern0d87d7e2016-08-01 13:49:29 -0700451 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100452 "perf: interrupt took too long (%lld > %lld), lowering "
453 "kernel.perf_event_max_sample_rate to %d\n",
454 __report_avg, __report_allowed,
455 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100456}
457
458static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
459
460void perf_sample_event_took(u64 sample_len_ns)
461{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100462 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
463 u64 running_len;
464 u64 avg_len;
465 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700466
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100467 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700468 return;
469
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100470 /* Decay the counter by 1 average sample. */
471 running_len = __this_cpu_read(running_sample_length);
472 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
473 running_len += sample_len_ns;
474 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700475
476 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100477 * Note: this will be biased artifically low until we have
478 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700479 * from having to maintain a count.
480 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100481 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
482 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700483 return;
484
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100485 __report_avg = avg_len;
486 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700487
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100488 /*
489 * Compute a throttle threshold 25% below the current duration.
490 */
491 avg_len += avg_len / 4;
492 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
493 if (avg_len < max)
494 max /= (u32)avg_len;
495 else
496 max = 1;
497
498 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
499 WRITE_ONCE(max_samples_per_tick, max);
500
501 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700502 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
503
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100504 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100505 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100506 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100507 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100508 sysctl_perf_event_sample_rate);
509 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700510}
511
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200512static atomic64_t perf_event_id;
513
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200514static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
515 enum event_type_t event_type);
516
517static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200518 enum event_type_t event_type,
519 struct task_struct *task);
520
521static void update_context_time(struct perf_event_context *ctx);
522static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200523
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200524void __weak perf_event_print_debug(void) { }
525
Matt Fleming84c79912010-10-03 21:41:13 +0100526extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200527{
Matt Fleming84c79912010-10-03 21:41:13 +0100528 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200529}
530
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200531static inline u64 perf_clock(void)
532{
533 return local_clock();
534}
535
Peter Zijlstra34f43922015-02-20 14:05:38 +0100536static inline u64 perf_event_clock(struct perf_event *event)
537{
538 return event->clock();
539}
540
Stephane Eraniane5d13672011-02-14 11:20:01 +0200541#ifdef CONFIG_CGROUP_PERF
542
Stephane Eraniane5d13672011-02-14 11:20:01 +0200543static inline bool
544perf_cgroup_match(struct perf_event *event)
545{
546 struct perf_event_context *ctx = event->ctx;
547 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
548
Tejun Heoef824fa2013-04-08 19:00:38 -0700549 /* @event doesn't care about cgroup */
550 if (!event->cgrp)
551 return true;
552
553 /* wants specific cgroup scope but @cpuctx isn't associated with any */
554 if (!cpuctx->cgrp)
555 return false;
556
557 /*
558 * Cgroup scoping is recursive. An event enabled for a cgroup is
559 * also enabled for all its descendant cgroups. If @cpuctx's
560 * cgroup is a descendant of @event's (the test covers identity
561 * case), it's a match.
562 */
563 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
564 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200565}
566
Stephane Eraniane5d13672011-02-14 11:20:01 +0200567static inline void perf_detach_cgroup(struct perf_event *event)
568{
Zefan Li4e2ba652014-09-19 16:53:14 +0800569 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200570 event->cgrp = NULL;
571}
572
573static inline int is_cgroup_event(struct perf_event *event)
574{
575 return event->cgrp != NULL;
576}
577
578static inline u64 perf_cgroup_event_time(struct perf_event *event)
579{
580 struct perf_cgroup_info *t;
581
582 t = per_cpu_ptr(event->cgrp->info, event->cpu);
583 return t->time;
584}
585
586static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
587{
588 struct perf_cgroup_info *info;
589 u64 now;
590
591 now = perf_clock();
592
593 info = this_cpu_ptr(cgrp->info);
594
595 info->time += now - info->timestamp;
596 info->timestamp = now;
597}
598
599static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
600{
601 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
602 if (cgrp_out)
603 __update_cgrp_time(cgrp_out);
604}
605
606static inline void update_cgrp_time_from_event(struct perf_event *event)
607{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200608 struct perf_cgroup *cgrp;
609
Stephane Eraniane5d13672011-02-14 11:20:01 +0200610 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200611 * ensure we access cgroup data only when needed and
612 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200613 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200614 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615 return;
616
Stephane Eranian614e4c42015-11-12 11:00:04 +0100617 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200618 /*
619 * Do not update time when cgroup is not active
620 */
621 if (cgrp == event->cgrp)
622 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200623}
624
625static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200626perf_cgroup_set_timestamp(struct task_struct *task,
627 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200628{
629 struct perf_cgroup *cgrp;
630 struct perf_cgroup_info *info;
631
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200632 /*
633 * ctx->lock held by caller
634 * ensure we do not access cgroup data
635 * unless we have the cgroup pinned (css_get)
636 */
637 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200638 return;
639
Stephane Eranian614e4c42015-11-12 11:00:04 +0100640 cgrp = perf_cgroup_from_task(task, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200641 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200642 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200643}
644
645#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
646#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
647
648/*
649 * reschedule events based on the cgroup constraint of task.
650 *
651 * mode SWOUT : schedule out everything
652 * mode SWIN : schedule in based on cgroup for next
653 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800654static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200655{
656 struct perf_cpu_context *cpuctx;
657 struct pmu *pmu;
658 unsigned long flags;
659
660 /*
661 * disable interrupts to avoid geting nr_cgroup
662 * changes via __perf_event_disable(). Also
663 * avoids preemption.
664 */
665 local_irq_save(flags);
666
667 /*
668 * we reschedule only in the presence of cgroup
669 * constrained events.
670 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200671
672 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200673 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200674 if (cpuctx->unique_pmu != pmu)
675 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200676
Stephane Eraniane5d13672011-02-14 11:20:01 +0200677 /*
678 * perf_cgroup_events says at least one
679 * context on this CPU has cgroup events.
680 *
681 * ctx->nr_cgroups reports the number of cgroup
682 * events for a context.
683 */
684 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200685 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
686 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200687
688 if (mode & PERF_CGROUP_SWOUT) {
689 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
690 /*
691 * must not be done before ctxswout due
692 * to event_filter_match() in event_sched_out()
693 */
694 cpuctx->cgrp = NULL;
695 }
696
697 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200698 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200699 /*
700 * set cgrp before ctxsw in to allow
701 * event_filter_match() to not have to pass
702 * task around
Stephane Eranian614e4c42015-11-12 11:00:04 +0100703 * we pass the cpuctx->ctx to perf_cgroup_from_task()
704 * because cgorup events are only per-cpu
Stephane Eraniane5d13672011-02-14 11:20:01 +0200705 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100706 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200707 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
708 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200709 perf_pmu_enable(cpuctx->ctx.pmu);
710 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200711 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200712 }
713
Stephane Eraniane5d13672011-02-14 11:20:01 +0200714 local_irq_restore(flags);
715}
716
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200717static inline void perf_cgroup_sched_out(struct task_struct *task,
718 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200719{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200720 struct perf_cgroup *cgrp1;
721 struct perf_cgroup *cgrp2 = NULL;
722
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100723 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200724 /*
725 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100726 * we do not need to pass the ctx here because we know
727 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200728 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100729 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100730 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200731
732 /*
733 * only schedule out current cgroup events if we know
734 * that we are switching to a different cgroup. Otherwise,
735 * do no touch the cgroup events.
736 */
737 if (cgrp1 != cgrp2)
738 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100739
740 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200741}
742
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200743static inline void perf_cgroup_sched_in(struct task_struct *prev,
744 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200745{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200746 struct perf_cgroup *cgrp1;
747 struct perf_cgroup *cgrp2 = NULL;
748
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100749 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200750 /*
751 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100752 * we do not need to pass the ctx here because we know
753 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200754 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100755 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100756 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200757
758 /*
759 * only need to schedule in cgroup events if we are changing
760 * cgroup during ctxsw. Cgroup events were not scheduled
761 * out of ctxsw out if that was not the case.
762 */
763 if (cgrp1 != cgrp2)
764 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100765
766 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200767}
768
769static inline int perf_cgroup_connect(int fd, struct perf_event *event,
770 struct perf_event_attr *attr,
771 struct perf_event *group_leader)
772{
773 struct perf_cgroup *cgrp;
774 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400775 struct fd f = fdget(fd);
776 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200777
Al Viro2903ff02012-08-28 12:52:22 -0400778 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200779 return -EBADF;
780
Al Virob5830432014-10-31 01:22:04 -0400781 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400782 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800783 if (IS_ERR(css)) {
784 ret = PTR_ERR(css);
785 goto out;
786 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200787
788 cgrp = container_of(css, struct perf_cgroup, css);
789 event->cgrp = cgrp;
790
791 /*
792 * all events in a group must monitor
793 * the same cgroup because a task belongs
794 * to only one perf cgroup at a time
795 */
796 if (group_leader && group_leader->cgrp != cgrp) {
797 perf_detach_cgroup(event);
798 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200799 }
Li Zefan3db272c2011-03-03 14:25:37 +0800800out:
Al Viro2903ff02012-08-28 12:52:22 -0400801 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200802 return ret;
803}
804
805static inline void
806perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
807{
808 struct perf_cgroup_info *t;
809 t = per_cpu_ptr(event->cgrp->info, event->cpu);
810 event->shadow_ctx_time = now - t->timestamp;
811}
812
813static inline void
814perf_cgroup_defer_enabled(struct perf_event *event)
815{
816 /*
817 * when the current task's perf cgroup does not match
818 * the event's, we need to remember to call the
819 * perf_mark_enable() function the first time a task with
820 * a matching perf cgroup is scheduled in.
821 */
822 if (is_cgroup_event(event) && !perf_cgroup_match(event))
823 event->cgrp_defer_enabled = 1;
824}
825
826static inline void
827perf_cgroup_mark_enabled(struct perf_event *event,
828 struct perf_event_context *ctx)
829{
830 struct perf_event *sub;
831 u64 tstamp = perf_event_time(event);
832
833 if (!event->cgrp_defer_enabled)
834 return;
835
836 event->cgrp_defer_enabled = 0;
837
838 event->tstamp_enabled = tstamp - event->total_time_enabled;
839 list_for_each_entry(sub, &event->sibling_list, group_entry) {
840 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
841 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
842 sub->cgrp_defer_enabled = 0;
843 }
844 }
845}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700846
847/*
848 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
849 * cleared when last cgroup event is removed.
850 */
851static inline void
852list_update_cgroup_event(struct perf_event *event,
853 struct perf_event_context *ctx, bool add)
854{
855 struct perf_cpu_context *cpuctx;
856
857 if (!is_cgroup_event(event))
858 return;
859
860 if (add && ctx->nr_cgroups++)
861 return;
862 else if (!add && --ctx->nr_cgroups)
863 return;
864 /*
865 * Because cgroup events are always per-cpu events,
866 * this will always be called from the right CPU.
867 */
868 cpuctx = __get_cpu_context(ctx);
869 cpuctx->cgrp = add ? event->cgrp : NULL;
870}
871
Stephane Eraniane5d13672011-02-14 11:20:01 +0200872#else /* !CONFIG_CGROUP_PERF */
873
874static inline bool
875perf_cgroup_match(struct perf_event *event)
876{
877 return true;
878}
879
880static inline void perf_detach_cgroup(struct perf_event *event)
881{}
882
883static inline int is_cgroup_event(struct perf_event *event)
884{
885 return 0;
886}
887
888static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
889{
890 return 0;
891}
892
893static inline void update_cgrp_time_from_event(struct perf_event *event)
894{
895}
896
897static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
898{
899}
900
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200901static inline void perf_cgroup_sched_out(struct task_struct *task,
902 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200903{
904}
905
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200906static inline void perf_cgroup_sched_in(struct task_struct *prev,
907 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200908{
909}
910
911static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
912 struct perf_event_attr *attr,
913 struct perf_event *group_leader)
914{
915 return -EINVAL;
916}
917
918static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200919perf_cgroup_set_timestamp(struct task_struct *task,
920 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200921{
922}
923
924void
925perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
926{
927}
928
929static inline void
930perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
931{
932}
933
934static inline u64 perf_cgroup_event_time(struct perf_event *event)
935{
936 return 0;
937}
938
939static inline void
940perf_cgroup_defer_enabled(struct perf_event *event)
941{
942}
943
944static inline void
945perf_cgroup_mark_enabled(struct perf_event *event,
946 struct perf_event_context *ctx)
947{
948}
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700949
950static inline void
951list_update_cgroup_event(struct perf_event *event,
952 struct perf_event_context *ctx, bool add)
953{
954}
955
Stephane Eraniane5d13672011-02-14 11:20:01 +0200956#endif
957
Stephane Eranian9e630202013-04-03 14:21:33 +0200958/*
959 * set default to be dependent on timer tick just
960 * like original code
961 */
962#define PERF_CPU_HRTIMER (1000 / HZ)
963/*
964 * function must be called with interrupts disbled
965 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200966static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +0200967{
968 struct perf_cpu_context *cpuctx;
Stephane Eranian9e630202013-04-03 14:21:33 +0200969 int rotations = 0;
970
971 WARN_ON(!irqs_disabled());
972
973 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200974 rotations = perf_rotate_context(cpuctx);
975
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200976 raw_spin_lock(&cpuctx->hrtimer_lock);
977 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +0200978 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200979 else
980 cpuctx->hrtimer_active = 0;
981 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +0200982
Peter Zijlstra4cfafd32015-05-14 12:23:11 +0200983 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +0200984}
985
Peter Zijlstra272325c2015-04-15 11:41:58 +0200986static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +0200987{
Peter Zijlstra272325c2015-04-15 11:41:58 +0200988 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200989 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +0200990 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +0200991
992 /* no multiplexing needed for SW PMU */
993 if (pmu->task_ctx_nr == perf_sw_context)
994 return;
995
Stephane Eranian62b85632013-04-03 14:21:34 +0200996 /*
997 * check default is sane, if not set then force to
998 * default interval (1/tick)
999 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001000 interval = pmu->hrtimer_interval_ms;
1001 if (interval < 1)
1002 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001003
Peter Zijlstra272325c2015-04-15 11:41:58 +02001004 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001005
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001006 raw_spin_lock_init(&cpuctx->hrtimer_lock);
1007 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001008 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001009}
1010
Peter Zijlstra272325c2015-04-15 11:41:58 +02001011static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001012{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001013 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001014 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001015 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001016
1017 /* not for SW PMU */
1018 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001019 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001020
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001021 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1022 if (!cpuctx->hrtimer_active) {
1023 cpuctx->hrtimer_active = 1;
1024 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
1025 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
1026 }
1027 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001028
Peter Zijlstra272325c2015-04-15 11:41:58 +02001029 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001030}
1031
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001032void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001033{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001034 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1035 if (!(*count)++)
1036 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037}
1038
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001039void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001041 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1042 if (!--(*count))
1043 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001044}
1045
Mark Rutland2fde4f92015-01-07 15:01:54 +00001046static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001047
1048/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001049 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1050 * perf_event_task_tick() are fully serialized because they're strictly cpu
1051 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1052 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001053 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001054static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001055{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001056 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001057
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001058 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001059
Mark Rutland2fde4f92015-01-07 15:01:54 +00001060 WARN_ON(!list_empty(&ctx->active_ctx_list));
1061
1062 list_add(&ctx->active_ctx_list, head);
1063}
1064
1065static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1066{
1067 WARN_ON(!irqs_disabled());
1068
1069 WARN_ON(list_empty(&ctx->active_ctx_list));
1070
1071 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001072}
1073
1074static void get_ctx(struct perf_event_context *ctx)
1075{
1076 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
1077}
1078
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001079static void free_ctx(struct rcu_head *head)
1080{
1081 struct perf_event_context *ctx;
1082
1083 ctx = container_of(head, struct perf_event_context, rcu_head);
1084 kfree(ctx->task_ctx_data);
1085 kfree(ctx);
1086}
1087
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001088static void put_ctx(struct perf_event_context *ctx)
1089{
1090 if (atomic_dec_and_test(&ctx->refcount)) {
1091 if (ctx->parent_ctx)
1092 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001093 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001094 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -05001095 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 }
1097}
1098
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001099/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001100 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1101 * perf_pmu_migrate_context() we need some magic.
1102 *
1103 * Those places that change perf_event::ctx will hold both
1104 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1105 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001106 * Lock ordering is by mutex address. There are two other sites where
1107 * perf_event_context::mutex nests and those are:
1108 *
1109 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001110 * perf_event_exit_event()
1111 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001112 *
1113 * - perf_event_init_context() [ parent, 0 ]
1114 * inherit_task_group()
1115 * inherit_group()
1116 * inherit_event()
1117 * perf_event_alloc()
1118 * perf_init_event()
1119 * perf_try_init_event() [ child , 1 ]
1120 *
1121 * While it appears there is an obvious deadlock here -- the parent and child
1122 * nesting levels are inverted between the two. This is in fact safe because
1123 * life-time rules separate them. That is an exiting task cannot fork, and a
1124 * spawning task cannot (yet) exit.
1125 *
1126 * But remember that that these are parent<->child context relations, and
1127 * migration does not affect children, therefore these two orderings should not
1128 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001129 *
1130 * The change in perf_event::ctx does not affect children (as claimed above)
1131 * because the sys_perf_event_open() case will install a new event and break
1132 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1133 * concerned with cpuctx and that doesn't have children.
1134 *
1135 * The places that change perf_event::ctx will issue:
1136 *
1137 * perf_remove_from_context();
1138 * synchronize_rcu();
1139 * perf_install_in_context();
1140 *
1141 * to affect the change. The remove_from_context() + synchronize_rcu() should
1142 * quiesce the event, after which we can install it in the new location. This
1143 * means that only external vectors (perf_fops, prctl) can perturb the event
1144 * while in transit. Therefore all such accessors should also acquire
1145 * perf_event_context::mutex to serialize against this.
1146 *
1147 * However; because event->ctx can change while we're waiting to acquire
1148 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1149 * function.
1150 *
1151 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001152 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001153 * task_struct::perf_event_mutex
1154 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001155 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001156 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001157 * perf_event::mmap_mutex
1158 * mmap_sem
1159 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001160static struct perf_event_context *
1161perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001162{
1163 struct perf_event_context *ctx;
1164
1165again:
1166 rcu_read_lock();
1167 ctx = ACCESS_ONCE(event->ctx);
1168 if (!atomic_inc_not_zero(&ctx->refcount)) {
1169 rcu_read_unlock();
1170 goto again;
1171 }
1172 rcu_read_unlock();
1173
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001174 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001175 if (event->ctx != ctx) {
1176 mutex_unlock(&ctx->mutex);
1177 put_ctx(ctx);
1178 goto again;
1179 }
1180
1181 return ctx;
1182}
1183
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001184static inline struct perf_event_context *
1185perf_event_ctx_lock(struct perf_event *event)
1186{
1187 return perf_event_ctx_lock_nested(event, 0);
1188}
1189
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001190static void perf_event_ctx_unlock(struct perf_event *event,
1191 struct perf_event_context *ctx)
1192{
1193 mutex_unlock(&ctx->mutex);
1194 put_ctx(ctx);
1195}
1196
1197/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001198 * This must be done under the ctx->lock, such as to serialize against
1199 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1200 * calling scheduler related locks and ctx->lock nests inside those.
1201 */
1202static __must_check struct perf_event_context *
1203unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001205 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1206
1207 lockdep_assert_held(&ctx->lock);
1208
1209 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001210 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001211 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001212
1213 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001214}
1215
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001216static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1217{
1218 /*
1219 * only top level events have the pid namespace they were created in
1220 */
1221 if (event->parent)
1222 event = event->parent;
1223
1224 return task_tgid_nr_ns(p, event->ns);
1225}
1226
1227static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1228{
1229 /*
1230 * only top level events have the pid namespace they were created in
1231 */
1232 if (event->parent)
1233 event = event->parent;
1234
1235 return task_pid_nr_ns(p, event->ns);
1236}
1237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238/*
1239 * If we inherit events we want to return the parent event id
1240 * to userspace.
1241 */
1242static u64 primary_event_id(struct perf_event *event)
1243{
1244 u64 id = event->id;
1245
1246 if (event->parent)
1247 id = event->parent->id;
1248
1249 return id;
1250}
1251
1252/*
1253 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001254 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255 * This has to cope with with the fact that until it is locked,
1256 * the context could get moved to another task.
1257 */
1258static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001259perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260{
1261 struct perf_event_context *ctx;
1262
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001263retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001264 /*
1265 * One of the few rules of preemptible RCU is that one cannot do
1266 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001267 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001268 * rcu_read_unlock_special().
1269 *
1270 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001271 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001272 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001273 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001274 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001275 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001276 if (ctx) {
1277 /*
1278 * If this context is a clone of another, it might
1279 * get swapped for another underneath us by
1280 * perf_event_task_sched_out, though the
1281 * rcu_read_lock() protects us from any context
1282 * getting freed. Lock the context and check if it
1283 * got swapped before we could get the lock, and retry
1284 * if so. If we locked the right context, then it
1285 * can't get swapped on us any more.
1286 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001287 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001288 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001289 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001290 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001291 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001292 goto retry;
1293 }
1294
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001295 if (ctx->task == TASK_TOMBSTONE ||
1296 !atomic_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001297 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001298 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001299 } else {
1300 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301 }
1302 }
1303 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001304 if (!ctx)
1305 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 return ctx;
1307}
1308
1309/*
1310 * Get the context for a task and increment its pin_count so it
1311 * can't get swapped to another task. This also increments its
1312 * reference count so that the context can't get freed.
1313 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001314static struct perf_event_context *
1315perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316{
1317 struct perf_event_context *ctx;
1318 unsigned long flags;
1319
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001320 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001321 if (ctx) {
1322 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001323 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 }
1325 return ctx;
1326}
1327
1328static void perf_unpin_context(struct perf_event_context *ctx)
1329{
1330 unsigned long flags;
1331
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001332 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001333 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001334 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001335}
1336
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001337/*
1338 * Update the record of the current time in a context.
1339 */
1340static void update_context_time(struct perf_event_context *ctx)
1341{
1342 u64 now = perf_clock();
1343
1344 ctx->time += now - ctx->timestamp;
1345 ctx->timestamp = now;
1346}
1347
Stephane Eranian41587552011-01-03 18:20:01 +02001348static u64 perf_event_time(struct perf_event *event)
1349{
1350 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001351
1352 if (is_cgroup_event(event))
1353 return perf_cgroup_event_time(event);
1354
Stephane Eranian41587552011-01-03 18:20:01 +02001355 return ctx ? ctx->time : 0;
1356}
1357
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001358/*
1359 * Update the total_time_enabled and total_time_running fields for a event.
1360 */
1361static void update_event_times(struct perf_event *event)
1362{
1363 struct perf_event_context *ctx = event->ctx;
1364 u64 run_end;
1365
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001366 lockdep_assert_held(&ctx->lock);
1367
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001368 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1369 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1370 return;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01001371
Stephane Eraniane5d13672011-02-14 11:20:01 +02001372 /*
1373 * in cgroup mode, time_enabled represents
1374 * the time the event was enabled AND active
1375 * tasks were in the monitored cgroup. This is
1376 * independent of the activity of the context as
1377 * there may be a mix of cgroup and non-cgroup events.
1378 *
1379 * That is why we treat cgroup events differently
1380 * here.
1381 */
1382 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001383 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001384 else if (ctx->is_active)
1385 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001386 else
1387 run_end = event->tstamp_stopped;
1388
1389 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001390
1391 if (event->state == PERF_EVENT_STATE_INACTIVE)
1392 run_end = event->tstamp_stopped;
1393 else
Stephane Eranian41587552011-01-03 18:20:01 +02001394 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001395
1396 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001397
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001398}
1399
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001400/*
1401 * Update total_time_enabled and total_time_running for all events in a group.
1402 */
1403static void update_group_times(struct perf_event *leader)
1404{
1405 struct perf_event *event;
1406
1407 update_event_times(leader);
1408 list_for_each_entry(event, &leader->sibling_list, group_entry)
1409 update_event_times(event);
1410}
1411
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001412static struct list_head *
1413ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1414{
1415 if (event->attr.pinned)
1416 return &ctx->pinned_groups;
1417 else
1418 return &ctx->flexible_groups;
1419}
1420
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421/*
1422 * Add a event from the lists for its context.
1423 * Must be called with ctx->mutex and ctx->lock held.
1424 */
1425static void
1426list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1427{
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001428
Peter Zijlstrac994d612016-01-08 09:20:23 +01001429 lockdep_assert_held(&ctx->lock);
1430
Peter Zijlstra8a495422010-05-27 15:47:49 +02001431 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1432 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001433
1434 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001435 * If we're a stand alone event or group leader, we go to the context
1436 * list, group events are kept attached to the group so that
1437 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001438 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001439 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001440 struct list_head *list;
1441
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001442 if (is_software_event(event))
1443 event->group_flags |= PERF_GROUP_SOFTWARE;
1444
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001445 list = ctx_group_list(event, ctx);
1446 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447 }
1448
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001449 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001450
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001451 list_add_rcu(&event->event_entry, &ctx->event_list);
1452 ctx->nr_events++;
1453 if (event->attr.inherit_stat)
1454 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001455
1456 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001457}
1458
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001459/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001460 * Initialize event state based on the perf_event_attr::disabled.
1461 */
1462static inline void perf_event__state_init(struct perf_event *event)
1463{
1464 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1465 PERF_EVENT_STATE_INACTIVE;
1466}
1467
Peter Zijlstraa7239682015-09-09 19:06:33 +02001468static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001469{
1470 int entry = sizeof(u64); /* value */
1471 int size = 0;
1472 int nr = 1;
1473
1474 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1475 size += sizeof(u64);
1476
1477 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1478 size += sizeof(u64);
1479
1480 if (event->attr.read_format & PERF_FORMAT_ID)
1481 entry += sizeof(u64);
1482
1483 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001484 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001485 size += sizeof(u64);
1486 }
1487
1488 size += entry * nr;
1489 event->read_size = size;
1490}
1491
Peter Zijlstraa7239682015-09-09 19:06:33 +02001492static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001493{
1494 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001495 u16 size = 0;
1496
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001497 if (sample_type & PERF_SAMPLE_IP)
1498 size += sizeof(data->ip);
1499
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001500 if (sample_type & PERF_SAMPLE_ADDR)
1501 size += sizeof(data->addr);
1502
1503 if (sample_type & PERF_SAMPLE_PERIOD)
1504 size += sizeof(data->period);
1505
Andi Kleenc3feedf2013-01-24 16:10:28 +01001506 if (sample_type & PERF_SAMPLE_WEIGHT)
1507 size += sizeof(data->weight);
1508
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001509 if (sample_type & PERF_SAMPLE_READ)
1510 size += event->read_size;
1511
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001512 if (sample_type & PERF_SAMPLE_DATA_SRC)
1513 size += sizeof(data->data_src.val);
1514
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001515 if (sample_type & PERF_SAMPLE_TRANSACTION)
1516 size += sizeof(data->txn);
1517
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001518 event->header_size = size;
1519}
1520
Peter Zijlstraa7239682015-09-09 19:06:33 +02001521/*
1522 * Called at perf_event creation and when events are attached/detached from a
1523 * group.
1524 */
1525static void perf_event__header_size(struct perf_event *event)
1526{
1527 __perf_event_read_size(event,
1528 event->group_leader->nr_siblings);
1529 __perf_event_header_size(event, event->attr.sample_type);
1530}
1531
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001532static void perf_event__id_header_size(struct perf_event *event)
1533{
1534 struct perf_sample_data *data;
1535 u64 sample_type = event->attr.sample_type;
1536 u16 size = 0;
1537
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001538 if (sample_type & PERF_SAMPLE_TID)
1539 size += sizeof(data->tid_entry);
1540
1541 if (sample_type & PERF_SAMPLE_TIME)
1542 size += sizeof(data->time);
1543
Adrian Hunterff3d5272013-08-27 11:23:07 +03001544 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1545 size += sizeof(data->id);
1546
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001547 if (sample_type & PERF_SAMPLE_ID)
1548 size += sizeof(data->id);
1549
1550 if (sample_type & PERF_SAMPLE_STREAM_ID)
1551 size += sizeof(data->stream_id);
1552
1553 if (sample_type & PERF_SAMPLE_CPU)
1554 size += sizeof(data->cpu_entry);
1555
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001556 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001557}
1558
Peter Zijlstraa7239682015-09-09 19:06:33 +02001559static bool perf_event_validate_size(struct perf_event *event)
1560{
1561 /*
1562 * The values computed here will be over-written when we actually
1563 * attach the event.
1564 */
1565 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1566 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1567 perf_event__id_header_size(event);
1568
1569 /*
1570 * Sum the lot; should not exceed the 64k limit we have on records.
1571 * Conservative limit to allow for callchains and other variable fields.
1572 */
1573 if (event->read_size + event->header_size +
1574 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1575 return false;
1576
1577 return true;
1578}
1579
Peter Zijlstra8a495422010-05-27 15:47:49 +02001580static void perf_group_attach(struct perf_event *event)
1581{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001582 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001583
Peter Zijlstra74c33372010-10-15 11:40:29 +02001584 /*
1585 * We can have double attach due to group movement in perf_event_open.
1586 */
1587 if (event->attach_state & PERF_ATTACH_GROUP)
1588 return;
1589
Peter Zijlstra8a495422010-05-27 15:47:49 +02001590 event->attach_state |= PERF_ATTACH_GROUP;
1591
1592 if (group_leader == event)
1593 return;
1594
Peter Zijlstra652884f2015-01-23 11:20:10 +01001595 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1596
Peter Zijlstra8a495422010-05-27 15:47:49 +02001597 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1598 !is_software_event(event))
1599 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1600
1601 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1602 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001603
1604 perf_event__header_size(group_leader);
1605
1606 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1607 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001608}
1609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610/*
1611 * Remove a event from the lists for its context.
1612 * Must be called with ctx->mutex and ctx->lock held.
1613 */
1614static void
1615list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1616{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001617 WARN_ON_ONCE(event->ctx != ctx);
1618 lockdep_assert_held(&ctx->lock);
1619
Peter Zijlstra8a495422010-05-27 15:47:49 +02001620 /*
1621 * We can have double detach due to exit/hot-unplug + close.
1622 */
1623 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001624 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001625
1626 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1627
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001628 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001629
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001630 ctx->nr_events--;
1631 if (event->attr.inherit_stat)
1632 ctx->nr_stat--;
1633
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001634 list_del_rcu(&event->event_entry);
1635
Peter Zijlstra8a495422010-05-27 15:47:49 +02001636 if (event->group_leader == event)
1637 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001639 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001640
1641 /*
1642 * If event was in error state, then keep it
1643 * that way, otherwise bogus counts will be
1644 * returned on read(). The only way to get out
1645 * of error state is by explicit re-enabling
1646 * of the event
1647 */
1648 if (event->state > PERF_EVENT_STATE_OFF)
1649 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001650
1651 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001652}
1653
Peter Zijlstra8a495422010-05-27 15:47:49 +02001654static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001655{
1656 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001657 struct list_head *list = NULL;
1658
1659 /*
1660 * We can have double detach due to exit/hot-unplug + close.
1661 */
1662 if (!(event->attach_state & PERF_ATTACH_GROUP))
1663 return;
1664
1665 event->attach_state &= ~PERF_ATTACH_GROUP;
1666
1667 /*
1668 * If this is a sibling, remove it from its group.
1669 */
1670 if (event->group_leader != event) {
1671 list_del_init(&event->group_entry);
1672 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001673 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001674 }
1675
1676 if (!list_empty(&event->group_entry))
1677 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001678
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679 /*
1680 * If this was a group event with sibling events then
1681 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001682 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683 */
1684 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001685 if (list)
1686 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001687 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001688
1689 /* Inherit group flags from the previous leader */
1690 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001691
1692 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001694
1695out:
1696 perf_event__header_size(event->group_leader);
1697
1698 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1699 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700}
1701
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001702static bool is_orphaned_event(struct perf_event *event)
1703{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01001704 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001705}
1706
Mark Rutland2c81a642016-06-14 16:10:41 +01001707static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01001708{
1709 struct pmu *pmu = event->pmu;
1710 return pmu->filter_match ? pmu->filter_match(event) : 1;
1711}
1712
Mark Rutland2c81a642016-06-14 16:10:41 +01001713/*
1714 * Check whether we should attempt to schedule an event group based on
1715 * PMU-specific filtering. An event group can consist of HW and SW events,
1716 * potentially with a SW leader, so we must check all the filters, to
1717 * determine whether a group is schedulable:
1718 */
1719static inline int pmu_filter_match(struct perf_event *event)
1720{
1721 struct perf_event *child;
1722
1723 if (!__pmu_filter_match(event))
1724 return 0;
1725
1726 list_for_each_entry(child, &event->sibling_list, group_entry) {
1727 if (!__pmu_filter_match(child))
1728 return 0;
1729 }
1730
1731 return 1;
1732}
1733
Stephane Eranianfa66f072010-08-26 16:40:01 +02001734static inline int
1735event_filter_match(struct perf_event *event)
1736{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001737 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
1738 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001739}
1740
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001741static void
1742event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743 struct perf_cpu_context *cpuctx,
1744 struct perf_event_context *ctx)
1745{
Stephane Eranian41587552011-01-03 18:20:01 +02001746 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001747 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001748
1749 WARN_ON_ONCE(event->ctx != ctx);
1750 lockdep_assert_held(&ctx->lock);
1751
Stephane Eranianfa66f072010-08-26 16:40:01 +02001752 /*
1753 * An event which could not be activated because of
1754 * filter mismatch still needs to have its timings
1755 * maintained, otherwise bogus information is return
1756 * via read() for time_enabled, time_running:
1757 */
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02001758 if (event->state == PERF_EVENT_STATE_INACTIVE &&
1759 !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001760 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001761 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001762 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001763 }
1764
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001766 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767
Alexander Shishkin44377272013-12-16 14:17:36 +02001768 perf_pmu_disable(event->pmu);
1769
Peter Zijlstra28a967c2016-02-24 18:45:46 +01001770 event->tstamp_stopped = tstamp;
1771 event->pmu->del(event, 0);
1772 event->oncpu = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773 event->state = PERF_EVENT_STATE_INACTIVE;
1774 if (event->pending_disable) {
1775 event->pending_disable = 0;
1776 event->state = PERF_EVENT_STATE_OFF;
1777 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001778
1779 if (!is_software_event(event))
1780 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001781 if (!--ctx->nr_active)
1782 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001783 if (event->attr.freq && event->attr.sample_freq)
1784 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785 if (event->attr.exclusive || !cpuctx->active_oncpu)
1786 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001787
1788 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789}
1790
1791static void
1792group_sched_out(struct perf_event *group_event,
1793 struct perf_cpu_context *cpuctx,
1794 struct perf_event_context *ctx)
1795{
1796 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001797 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001798
Mark Rutland3f005e72016-07-26 18:12:21 +01001799 perf_pmu_disable(ctx->pmu);
1800
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001801 event_sched_out(group_event, cpuctx, ctx);
1802
1803 /*
1804 * Schedule out siblings (if any):
1805 */
1806 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1807 event_sched_out(event, cpuctx, ctx);
1808
Mark Rutland3f005e72016-07-26 18:12:21 +01001809 perf_pmu_enable(ctx->pmu);
1810
Stephane Eranianfa66f072010-08-26 16:40:01 +02001811 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812 cpuctx->exclusive = 0;
1813}
1814
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001815#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01001816
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001817/*
1818 * Cross CPU call to remove a performance event
1819 *
1820 * We disable the event on the hardware level first. After that we
1821 * remove it from the context list.
1822 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001823static void
1824__perf_remove_from_context(struct perf_event *event,
1825 struct perf_cpu_context *cpuctx,
1826 struct perf_event_context *ctx,
1827 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001829 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001832 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001833 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001834 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001835
Peter Zijlstra39a43642016-01-11 12:46:35 +01001836 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001837 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01001838 if (ctx->task) {
1839 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1840 cpuctx->task_ctx = NULL;
1841 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843}
1844
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845/*
1846 * Remove the event from a task's (or a CPU's) list of events.
1847 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848 * If event->ctx is a cloned context, callers must make sure that
1849 * every task struct that event->ctx->task could possibly point to
1850 * remains valid. This is OK when called from perf_release since
1851 * that only calls us on the top-level context, which can't be a clone.
1852 * When called from perf_event_exit_task, it's OK because the
1853 * context has been detached from its task.
1854 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001855static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001857 lockdep_assert_held(&event->ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
Peter Zijlstra45a0e072016-01-26 13:09:48 +01001859 event_function_call(event, __perf_remove_from_context, (void *)flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860}
1861
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001862/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 * Cross CPU call to disable a performance event
1864 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001865static void __perf_event_disable(struct perf_event *event,
1866 struct perf_cpu_context *cpuctx,
1867 struct perf_event_context *ctx,
1868 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001870 if (event->state < PERF_EVENT_STATE_INACTIVE)
1871 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001873 update_context_time(ctx);
1874 update_cgrp_time_from_event(event);
1875 update_group_times(event);
1876 if (event == event->group_leader)
1877 group_sched_out(event, cpuctx, ctx);
1878 else
1879 event_sched_out(event, cpuctx, ctx);
1880 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra7b648012015-12-03 18:35:21 +01001881}
1882
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883/*
1884 * Disable a event.
1885 *
1886 * If event->ctx is a cloned context, callers must make sure that
1887 * every task struct that event->ctx->task could possibly point to
1888 * remains valid. This condition is satisifed when called through
1889 * perf_event_for_each_child or perf_event_for_each because they
1890 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001891 * goes to exit will block in perf_event_exit_event().
1892 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001893 * When called from perf_pending_event it's OK because event->ctx
1894 * is the current context on this CPU and preemption is disabled,
1895 * hence we can't get into perf_event_task_sched_out for this context.
1896 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001897static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001898{
1899 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001901 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001902 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001903 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001904 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001905 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001906 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01001907
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01001908 event_function_call(event, __perf_event_disable, NULL);
1909}
1910
1911void perf_event_disable_local(struct perf_event *event)
1912{
1913 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001914}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001915
1916/*
1917 * Strictly speaking kernel users cannot create groups and therefore this
1918 * interface does not need the perf_event_ctx_lock() magic.
1919 */
1920void perf_event_disable(struct perf_event *event)
1921{
1922 struct perf_event_context *ctx;
1923
1924 ctx = perf_event_ctx_lock(event);
1925 _perf_event_disable(event);
1926 perf_event_ctx_unlock(event, ctx);
1927}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001928EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001929
Stephane Eraniane5d13672011-02-14 11:20:01 +02001930static void perf_set_shadow_time(struct perf_event *event,
1931 struct perf_event_context *ctx,
1932 u64 tstamp)
1933{
1934 /*
1935 * use the correct time source for the time snapshot
1936 *
1937 * We could get by without this by leveraging the
1938 * fact that to get to this function, the caller
1939 * has most likely already called update_context_time()
1940 * and update_cgrp_time_xx() and thus both timestamp
1941 * are identical (or very close). Given that tstamp is,
1942 * already adjusted for cgroup, we could say that:
1943 * tstamp - ctx->timestamp
1944 * is equivalent to
1945 * tstamp - cgrp->timestamp.
1946 *
1947 * Then, in perf_output_read(), the calculation would
1948 * work with no changes because:
1949 * - event is guaranteed scheduled in
1950 * - no scheduled out in between
1951 * - thus the timestamp would be the same
1952 *
1953 * But this is a bit hairy.
1954 *
1955 * So instead, we have an explicit cgroup call to remain
1956 * within the time time source all along. We believe it
1957 * is cleaner and simpler to understand.
1958 */
1959 if (is_cgroup_event(event))
1960 perf_cgroup_set_shadow_time(event, tstamp);
1961 else
1962 event->shadow_ctx_time = tstamp - ctx->timestamp;
1963}
1964
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001965#define MAX_INTERRUPTS (~0ULL)
1966
1967static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02001968static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001969
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001971event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001973 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974{
Stephane Eranian41587552011-01-03 18:20:01 +02001975 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001976 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001977
Peter Zijlstra63342412014-05-05 11:49:16 +02001978 lockdep_assert_held(&ctx->lock);
1979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 if (event->state <= PERF_EVENT_STATE_OFF)
1981 return 0;
1982
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02001983 WRITE_ONCE(event->oncpu, smp_processor_id());
1984 /*
1985 * Order event::oncpu write to happen before the ACTIVE state
1986 * is visible.
1987 */
1988 smp_wmb();
1989 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001990
1991 /*
1992 * Unthrottle events, since we scheduled we might have missed several
1993 * ticks already, also for a heavily scheduling task there is little
1994 * guarantee it'll get a tick in a timely manner.
1995 */
1996 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1997 perf_log_throttle(event, 1);
1998 event->hw.interrupts = 0;
1999 }
2000
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001 /*
2002 * The new state must be visible before we turn it on in the hardware:
2003 */
2004 smp_wmb();
2005
Alexander Shishkin44377272013-12-16 14:17:36 +02002006 perf_pmu_disable(event->pmu);
2007
Shaohua Li72f669c2015-02-05 15:55:31 -08002008 perf_set_shadow_time(event, ctx, tstamp);
2009
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002010 perf_log_itrace_start(event);
2011
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002012 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013 event->state = PERF_EVENT_STATE_INACTIVE;
2014 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002015 ret = -EAGAIN;
2016 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002017 }
2018
Peter Zijlstra00a29162015-07-27 10:35:07 +02002019 event->tstamp_running += tstamp - event->tstamp_stopped;
2020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 if (!is_software_event(event))
2022 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002023 if (!ctx->nr_active++)
2024 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002025 if (event->attr.freq && event->attr.sample_freq)
2026 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002027
2028 if (event->attr.exclusive)
2029 cpuctx->exclusive = 1;
2030
Alexander Shishkin44377272013-12-16 14:17:36 +02002031out:
2032 perf_pmu_enable(event->pmu);
2033
2034 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002035}
2036
2037static int
2038group_sched_in(struct perf_event *group_event,
2039 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002040 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002041{
Lin Ming6bde9b62010-04-23 13:56:00 +08002042 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002043 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002044 u64 now = ctx->time;
2045 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002046
2047 if (group_event->state == PERF_EVENT_STATE_OFF)
2048 return 0;
2049
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002050 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002051
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002052 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002053 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002054 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002056 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002057
2058 /*
2059 * Schedule in siblings as one group (if any):
2060 */
2061 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002062 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063 partial_group = event;
2064 goto group_error;
2065 }
2066 }
2067
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002068 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002069 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002071group_error:
2072 /*
2073 * Groups can be scheduled in as one unit only, so undo any
2074 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02002075 * The events up to the failed event are scheduled out normally,
2076 * tstamp_stopped will be updated.
2077 *
2078 * The failed events and the remaining siblings need to have
2079 * their timings updated as if they had gone thru event_sched_in()
2080 * and event_sched_out(). This is required to get consistent timings
2081 * across the group. This also takes care of the case where the group
2082 * could never be scheduled by ensuring tstamp_stopped is set to mark
2083 * the time the event was actually stopped, such that time delta
2084 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085 */
2086 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2087 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02002088 simulate = true;
2089
2090 if (simulate) {
2091 event->tstamp_running += now - event->tstamp_stopped;
2092 event->tstamp_stopped = now;
2093 } else {
2094 event_sched_out(event, cpuctx, ctx);
2095 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002097 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002099 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002100
Peter Zijlstra272325c2015-04-15 11:41:58 +02002101 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002103 return -EAGAIN;
2104}
2105
2106/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002107 * Work out whether we can put this event group on the CPU now.
2108 */
2109static int group_can_go_on(struct perf_event *event,
2110 struct perf_cpu_context *cpuctx,
2111 int can_add_hw)
2112{
2113 /*
2114 * Groups consisting entirely of software events can always go on.
2115 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002116 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117 return 1;
2118 /*
2119 * If an exclusive group is already on, no other hardware
2120 * events can go on.
2121 */
2122 if (cpuctx->exclusive)
2123 return 0;
2124 /*
2125 * If this group is exclusive and there are already
2126 * events on the CPU, it can't go on.
2127 */
2128 if (event->attr.exclusive && cpuctx->active_oncpu)
2129 return 0;
2130 /*
2131 * Otherwise, try to add it if all previous groups were able
2132 * to go on.
2133 */
2134 return can_add_hw;
2135}
2136
2137static void add_event_to_ctx(struct perf_event *event,
2138 struct perf_event_context *ctx)
2139{
Stephane Eranian41587552011-01-03 18:20:01 +02002140 u64 tstamp = perf_event_time(event);
2141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002143 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002144 event->tstamp_enabled = tstamp;
2145 event->tstamp_running = tstamp;
2146 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147}
2148
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002149static void ctx_sched_out(struct perf_event_context *ctx,
2150 struct perf_cpu_context *cpuctx,
2151 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002152static void
2153ctx_sched_in(struct perf_event_context *ctx,
2154 struct perf_cpu_context *cpuctx,
2155 enum event_type_t event_type,
2156 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002157
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002158static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2159 struct perf_event_context *ctx)
2160{
2161 if (!cpuctx->task_ctx)
2162 return;
2163
2164 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2165 return;
2166
2167 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2168}
2169
Peter Zijlstradce58552011-04-09 21:17:46 +02002170static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2171 struct perf_event_context *ctx,
2172 struct task_struct *task)
2173{
2174 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2175 if (ctx)
2176 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2177 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2178 if (ctx)
2179 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2180}
2181
Peter Zijlstra3e349502016-01-08 10:01:18 +01002182static void ctx_resched(struct perf_cpu_context *cpuctx,
2183 struct perf_event_context *task_ctx)
Peter Zijlstra00179602015-11-30 16:26:35 +01002184{
Peter Zijlstra3e349502016-01-08 10:01:18 +01002185 perf_pmu_disable(cpuctx->ctx.pmu);
2186 if (task_ctx)
2187 task_ctx_sched_out(cpuctx, task_ctx);
2188 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2189 perf_event_sched_in(cpuctx, task_ctx, current);
2190 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002191}
2192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002193/*
2194 * Cross CPU call to install and enable a performance event
2195 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002196 * Very similar to remote_function() + event_function() but cannot assume that
2197 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002198 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002199static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002200{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002201 struct perf_event *event = info;
2202 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002203 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002204 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002205 bool activate = true;
2206 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002207
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002208 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002209 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002210 raw_spin_lock(&ctx->lock);
2211 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002212
2213 /* If we're on the wrong CPU, try again */
2214 if (task_cpu(ctx->task) != smp_processor_id()) {
2215 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002216 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002217 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002218
Peter Zijlstra39a43642016-01-11 12:46:35 +01002219 /*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002220 * If we're on the right CPU, see if the task we target is
2221 * current, if not we don't have to activate the ctx, a future
2222 * context switch will do that for us.
Peter Zijlstra39a43642016-01-11 12:46:35 +01002223 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002224 if (ctx->task != current)
2225 activate = false;
2226 else
2227 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2228
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002229 } else if (task_ctx) {
2230 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002231 }
2232
Peter Zijlstraa0963092016-02-24 18:45:50 +01002233 if (activate) {
2234 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2235 add_event_to_ctx(event, ctx);
2236 ctx_resched(cpuctx, task_ctx);
2237 } else {
2238 add_event_to_ctx(event, ctx);
2239 }
2240
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002241unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002242 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002243
Peter Zijlstraa0963092016-02-24 18:45:50 +01002244 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245}
2246
2247/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002248 * Attach a performance event to a context.
2249 *
2250 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251 */
2252static void
2253perf_install_in_context(struct perf_event_context *ctx,
2254 struct perf_event *event,
2255 int cpu)
2256{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002257 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002258
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002259 lockdep_assert_held(&ctx->mutex);
2260
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002261 if (event->cpu != -1)
2262 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002263
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002264 /*
2265 * Ensures that if we can observe event->ctx, both the event and ctx
2266 * will be 'complete'. See perf_iterate_sb_cpu().
2267 */
2268 smp_store_release(&event->ctx, ctx);
2269
Peter Zijlstraa0963092016-02-24 18:45:50 +01002270 if (!task) {
2271 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002272 return;
2273 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002274
Peter Zijlstraa0963092016-02-24 18:45:50 +01002275 /*
2276 * Should not happen, we validate the ctx is still alive before calling.
2277 */
2278 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2279 return;
2280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281 /*
2282 * Installing events is tricky because we cannot rely on ctx->is_active
2283 * to be set in case this is the nr_events 0 -> 1 transition.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284 */
Peter Zijlstraa0963092016-02-24 18:45:50 +01002285again:
2286 /*
2287 * Cannot use task_function_call() because we need to run on the task's
2288 * CPU regardless of whether its current or not.
2289 */
2290 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2291 return;
2292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293 raw_spin_lock_irq(&ctx->lock);
2294 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002295 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2296 /*
2297 * Cannot happen because we already checked above (which also
2298 * cannot happen), and we hold ctx->mutex, which serializes us
2299 * against perf_event_exit_task_context().
2300 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002301 raw_spin_unlock_irq(&ctx->lock);
2302 return;
2303 }
Peter Zijlstra39a43642016-01-11 12:46:35 +01002304 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstraa0963092016-02-24 18:45:50 +01002305 /*
2306 * Since !ctx->is_active doesn't mean anything, we must IPI
2307 * unconditionally.
2308 */
2309 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002310}
2311
2312/*
2313 * Put a event into inactive state and update time fields.
2314 * Enabling the leader of a group effectively enables all
2315 * the group members that aren't explicitly disabled, so we
2316 * have to update their ->tstamp_enabled also.
2317 * Note: this works for group members as well as group leaders
2318 * since the non-leader members' sibling_lists will be empty.
2319 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002320static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321{
2322 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002323 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002324
2325 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002326 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002327 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002328 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2329 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002330 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002331}
2332
2333/*
2334 * Cross CPU call to enable a performance event
2335 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002336static void __perf_event_enable(struct perf_event *event,
2337 struct perf_cpu_context *cpuctx,
2338 struct perf_event_context *ctx,
2339 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002341 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002342 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002343
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002344 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2345 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002346 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002348 if (ctx->is_active)
2349 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2350
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002351 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002353 if (!ctx->is_active)
2354 return;
2355
Stephane Eraniane5d13672011-02-14 11:20:01 +02002356 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002357 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02002358 perf_cgroup_defer_enabled(event);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002359 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002360 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002361 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363 /*
2364 * If the event is in a group and isn't the group leader,
2365 * then don't put it on unless the group is on.
2366 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002367 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2368 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002369 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002370 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002372 task_ctx = cpuctx->task_ctx;
2373 if (ctx->task)
2374 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375
Peter Zijlstraaee7dbc2016-01-08 10:45:11 +01002376 ctx_resched(cpuctx, task_ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002377}
2378
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002379/*
2380 * Enable a event.
2381 *
2382 * If event->ctx is a cloned context, callers must make sure that
2383 * every task struct that event->ctx->task could possibly point to
2384 * remains valid. This condition is satisfied when called through
2385 * perf_event_for_each_child or perf_event_for_each as described
2386 * for perf_event_disable.
2387 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002388static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002389{
2390 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002392 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002393 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2394 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002395 raw_spin_unlock_irq(&ctx->lock);
2396 return;
2397 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002398
2399 /*
2400 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002401 *
2402 * That way, if we see the event in error state below, we know that it
2403 * has gone back into error state, as distinct from the task having
2404 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002405 */
2406 if (event->state == PERF_EVENT_STATE_ERROR)
2407 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002408 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002409
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002410 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002411}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002412
2413/*
2414 * See perf_event_disable();
2415 */
2416void perf_event_enable(struct perf_event *event)
2417{
2418 struct perf_event_context *ctx;
2419
2420 ctx = perf_event_ctx_lock(event);
2421 _perf_event_enable(event);
2422 perf_event_ctx_unlock(event, ctx);
2423}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002424EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002425
Alexander Shishkin375637b2016-04-27 18:44:46 +03002426struct stop_event_data {
2427 struct perf_event *event;
2428 unsigned int restart;
2429};
2430
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002431static int __perf_event_stop(void *info)
2432{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002433 struct stop_event_data *sd = info;
2434 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002435
Alexander Shishkin375637b2016-04-27 18:44:46 +03002436 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002437 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2438 return 0;
2439
2440 /* matches smp_wmb() in event_sched_in() */
2441 smp_rmb();
2442
2443 /*
2444 * There is a window with interrupts enabled before we get here,
2445 * so we need to check again lest we try to stop another CPU's event.
2446 */
2447 if (READ_ONCE(event->oncpu) != smp_processor_id())
2448 return -EAGAIN;
2449
2450 event->pmu->stop(event, PERF_EF_UPDATE);
2451
Alexander Shishkin375637b2016-04-27 18:44:46 +03002452 /*
2453 * May race with the actual stop (through perf_pmu_output_stop()),
2454 * but it is only used for events with AUX ring buffer, and such
2455 * events will refuse to restart because of rb::aux_mmap_count==0,
2456 * see comments in perf_aux_output_begin().
2457 *
2458 * Since this is happening on a event-local CPU, no trace is lost
2459 * while restarting.
2460 */
2461 if (sd->restart)
2462 event->pmu->start(event, PERF_EF_START);
2463
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002464 return 0;
2465}
2466
Alexander Shishkin375637b2016-04-27 18:44:46 +03002467static int perf_event_restart(struct perf_event *event)
2468{
2469 struct stop_event_data sd = {
2470 .event = event,
2471 .restart = 1,
2472 };
2473 int ret = 0;
2474
2475 do {
2476 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2477 return 0;
2478
2479 /* matches smp_wmb() in event_sched_in() */
2480 smp_rmb();
2481
2482 /*
2483 * We only want to restart ACTIVE events, so if the event goes
2484 * inactive here (event->oncpu==-1), there's nothing more to do;
2485 * fall through with ret==-ENXIO.
2486 */
2487 ret = cpu_function_call(READ_ONCE(event->oncpu),
2488 __perf_event_stop, &sd);
2489 } while (ret == -EAGAIN);
2490
2491 return ret;
2492}
2493
2494/*
2495 * In order to contain the amount of racy and tricky in the address filter
2496 * configuration management, it is a two part process:
2497 *
2498 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2499 * we update the addresses of corresponding vmas in
2500 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2501 * (p2) when an event is scheduled in (pmu::add), it calls
2502 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2503 * if the generation has changed since the previous call.
2504 *
2505 * If (p1) happens while the event is active, we restart it to force (p2).
2506 *
2507 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2508 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2509 * ioctl;
2510 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2511 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2512 * for reading;
2513 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2514 * of exec.
2515 */
2516void perf_event_addr_filters_sync(struct perf_event *event)
2517{
2518 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2519
2520 if (!has_addr_filter(event))
2521 return;
2522
2523 raw_spin_lock(&ifh->lock);
2524 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2525 event->pmu->addr_filters_sync(event);
2526 event->hw.addr_filters_gen = event->addr_filters_gen;
2527 }
2528 raw_spin_unlock(&ifh->lock);
2529}
2530EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2531
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002532static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002533{
2534 /*
2535 * not supported on inherited events
2536 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002537 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002538 return -EINVAL;
2539
2540 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002541 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002542
2543 return 0;
2544}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002545
2546/*
2547 * See perf_event_disable()
2548 */
2549int perf_event_refresh(struct perf_event *event, int refresh)
2550{
2551 struct perf_event_context *ctx;
2552 int ret;
2553
2554 ctx = perf_event_ctx_lock(event);
2555 ret = _perf_event_refresh(event, refresh);
2556 perf_event_ctx_unlock(event, ctx);
2557
2558 return ret;
2559}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002560EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002561
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002562static void ctx_sched_out(struct perf_event_context *ctx,
2563 struct perf_cpu_context *cpuctx,
2564 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002565{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002566 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002567 struct perf_event *event;
2568
2569 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002570
Peter Zijlstra39a43642016-01-11 12:46:35 +01002571 if (likely(!ctx->nr_events)) {
2572 /*
2573 * See __perf_remove_from_context().
2574 */
2575 WARN_ON_ONCE(ctx->is_active);
2576 if (ctx->task)
2577 WARN_ON_ONCE(cpuctx->task_ctx);
2578 return;
2579 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002580
Peter Zijlstradb24d332011-04-09 21:17:45 +02002581 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002582 if (!(ctx->is_active & EVENT_ALL))
2583 ctx->is_active = 0;
2584
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002585 if (ctx->task) {
2586 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2587 if (!ctx->is_active)
2588 cpuctx->task_ctx = NULL;
2589 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002590
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002591 /*
2592 * Always update time if it was set; not only when it changes.
2593 * Otherwise we can 'forget' to update time for any but the last
2594 * context we sched out. For example:
2595 *
2596 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2597 * ctx_sched_out(.event_type = EVENT_PINNED)
2598 *
2599 * would only update time for the pinned events.
2600 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002601 if (is_active & EVENT_TIME) {
2602 /* update (and stop) ctx time */
2603 update_context_time(ctx);
2604 update_cgrp_time_from_cpuctx(cpuctx);
2605 }
2606
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02002607 is_active ^= ctx->is_active; /* changed bits */
2608
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002609 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002610 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002611
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002612 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002613 if (is_active & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002614 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2615 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002616 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002617
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002618 if (is_active & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002619 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002620 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002621 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002622 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002623}
2624
2625/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002626 * Test whether two contexts are equivalent, i.e. whether they have both been
2627 * cloned from the same version of the same context.
2628 *
2629 * Equivalence is measured using a generation number in the context that is
2630 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2631 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632 */
2633static int context_equiv(struct perf_event_context *ctx1,
2634 struct perf_event_context *ctx2)
2635{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002636 lockdep_assert_held(&ctx1->lock);
2637 lockdep_assert_held(&ctx2->lock);
2638
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002639 /* Pinning disables the swap optimization */
2640 if (ctx1->pin_count || ctx2->pin_count)
2641 return 0;
2642
2643 /* If ctx1 is the parent of ctx2 */
2644 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2645 return 1;
2646
2647 /* If ctx2 is the parent of ctx1 */
2648 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2649 return 1;
2650
2651 /*
2652 * If ctx1 and ctx2 have the same parent; we flatten the parent
2653 * hierarchy, see perf_event_init_context().
2654 */
2655 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2656 ctx1->parent_gen == ctx2->parent_gen)
2657 return 1;
2658
2659 /* Unmatched */
2660 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002661}
2662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002663static void __perf_event_sync_stat(struct perf_event *event,
2664 struct perf_event *next_event)
2665{
2666 u64 value;
2667
2668 if (!event->attr.inherit_stat)
2669 return;
2670
2671 /*
2672 * Update the event value, we cannot use perf_event_read()
2673 * because we're in the middle of a context switch and have IRQs
2674 * disabled, which upsets smp_call_function_single(), however
2675 * we know the event must be on the current CPU, therefore we
2676 * don't need to use it.
2677 */
2678 switch (event->state) {
2679 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002680 event->pmu->read(event);
2681 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
2683 case PERF_EVENT_STATE_INACTIVE:
2684 update_event_times(event);
2685 break;
2686
2687 default:
2688 break;
2689 }
2690
2691 /*
2692 * In order to keep per-task stats reliable we need to flip the event
2693 * values when we flip the contexts.
2694 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002695 value = local64_read(&next_event->count);
2696 value = local64_xchg(&event->count, value);
2697 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002698
2699 swap(event->total_time_enabled, next_event->total_time_enabled);
2700 swap(event->total_time_running, next_event->total_time_running);
2701
2702 /*
2703 * Since we swizzled the values, update the user visible data too.
2704 */
2705 perf_event_update_userpage(event);
2706 perf_event_update_userpage(next_event);
2707}
2708
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709static void perf_event_sync_stat(struct perf_event_context *ctx,
2710 struct perf_event_context *next_ctx)
2711{
2712 struct perf_event *event, *next_event;
2713
2714 if (!ctx->nr_stat)
2715 return;
2716
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002717 update_context_time(ctx);
2718
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719 event = list_first_entry(&ctx->event_list,
2720 struct perf_event, event_entry);
2721
2722 next_event = list_first_entry(&next_ctx->event_list,
2723 struct perf_event, event_entry);
2724
2725 while (&event->event_entry != &ctx->event_list &&
2726 &next_event->event_entry != &next_ctx->event_list) {
2727
2728 __perf_event_sync_stat(event, next_event);
2729
2730 event = list_next_entry(event, event_entry);
2731 next_event = list_next_entry(next_event, event_entry);
2732 }
2733}
2734
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002735static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2736 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002738 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002740 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002741 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742 int do_switch = 1;
2743
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002744 if (likely(!ctx))
2745 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002746
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002747 cpuctx = __get_cpu_context(ctx);
2748 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002749 return;
2750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002752 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002753 if (!next_ctx)
2754 goto unlock;
2755
2756 parent = rcu_dereference(ctx->parent_ctx);
2757 next_parent = rcu_dereference(next_ctx->parent_ctx);
2758
2759 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002760 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002761 goto unlock;
2762
2763 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 /*
2765 * Looks like the two contexts are clones, so we might be
2766 * able to optimize the context switch. We lock both
2767 * contexts and check that they are clones under the
2768 * lock (including re-checking that neither has been
2769 * uncloned in the meantime). It doesn't matter which
2770 * order we take the locks because no other cpu could
2771 * be trying to lock both of these tasks.
2772 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002773 raw_spin_lock(&ctx->lock);
2774 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775 if (context_equiv(ctx, next_ctx)) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002776 WRITE_ONCE(ctx->task, next);
2777 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05002778
2779 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2780
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002781 /*
2782 * RCU_INIT_POINTER here is safe because we've not
2783 * modified the ctx and the above modification of
2784 * ctx->task and ctx->task_ctx_data are immaterial
2785 * since those values are always verified under
2786 * ctx->lock which we're now holding.
2787 */
2788 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2789 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2790
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791 do_switch = 0;
2792
2793 perf_event_sync_stat(ctx, next_ctx);
2794 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002795 raw_spin_unlock(&next_ctx->lock);
2796 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002797 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002798unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002799 rcu_read_unlock();
2800
2801 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002802 raw_spin_lock(&ctx->lock);
Peter Zijlstra8833d0e2016-01-08 10:02:37 +01002803 task_ctx_sched_out(cpuctx, ctx);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002804 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805 }
2806}
2807
Yan, Zhengba532502014-11-04 21:55:58 -05002808void perf_sched_cb_dec(struct pmu *pmu)
2809{
2810 this_cpu_dec(perf_sched_cb_usages);
2811}
2812
2813void perf_sched_cb_inc(struct pmu *pmu)
2814{
2815 this_cpu_inc(perf_sched_cb_usages);
2816}
2817
2818/*
2819 * This function provides the context switch callback to the lower code
2820 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002821 *
2822 * This callback is relevant even to per-cpu events; for example multi event
2823 * PEBS requires this to provide PID/TID information. This requires we flush
2824 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002825 */
2826static void perf_pmu_sched_task(struct task_struct *prev,
2827 struct task_struct *next,
2828 bool sched_in)
2829{
2830 struct perf_cpu_context *cpuctx;
2831 struct pmu *pmu;
2832 unsigned long flags;
2833
2834 if (prev == next)
2835 return;
2836
2837 local_irq_save(flags);
2838
2839 rcu_read_lock();
2840
2841 list_for_each_entry_rcu(pmu, &pmus, entry) {
2842 if (pmu->sched_task) {
2843 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2844
2845 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2846
2847 perf_pmu_disable(pmu);
2848
2849 pmu->sched_task(cpuctx->task_ctx, sched_in);
2850
2851 perf_pmu_enable(pmu);
2852
2853 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2854 }
2855 }
2856
2857 rcu_read_unlock();
2858
2859 local_irq_restore(flags);
2860}
2861
Adrian Hunter45ac1402015-07-21 12:44:02 +03002862static void perf_event_switch(struct task_struct *task,
2863 struct task_struct *next_prev, bool sched_in);
2864
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002865#define for_each_task_context_nr(ctxn) \
2866 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2867
2868/*
2869 * Called from scheduler to remove the events of the current task,
2870 * with interrupts disabled.
2871 *
2872 * We stop each event and update the event value in event->count.
2873 *
2874 * This does not protect us against NMI, but disable()
2875 * sets the disabled bit in the control field of event _before_
2876 * accessing the event control register. If a NMI hits, then it will
2877 * not restart the event.
2878 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002879void __perf_event_task_sched_out(struct task_struct *task,
2880 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002881{
2882 int ctxn;
2883
Yan, Zhengba532502014-11-04 21:55:58 -05002884 if (__this_cpu_read(perf_sched_cb_usages))
2885 perf_pmu_sched_task(task, next, false);
2886
Adrian Hunter45ac1402015-07-21 12:44:02 +03002887 if (atomic_read(&nr_switch_events))
2888 perf_event_switch(task, next, false);
2889
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002890 for_each_task_context_nr(ctxn)
2891 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002892
2893 /*
2894 * if cgroup events exist on this CPU, then we need
2895 * to check if we have to switch out PMU state.
2896 * cgroup event are system-wide mode only
2897 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002898 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002899 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002900}
2901
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002902/*
2903 * Called with IRQs disabled
2904 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002905static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2906 enum event_type_t event_type)
2907{
2908 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909}
2910
2911static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002912ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002913 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914{
2915 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002916
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002917 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2918 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002920 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002921 continue;
2922
Stephane Eraniane5d13672011-02-14 11:20:01 +02002923 /* may need to reset tstamp_enabled */
2924 if (is_cgroup_event(event))
2925 perf_cgroup_mark_enabled(event, ctx);
2926
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002927 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002928 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002929
2930 /*
2931 * If this pinned group hasn't been scheduled,
2932 * put it in error state.
2933 */
2934 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2935 update_group_times(event);
2936 event->state = PERF_EVENT_STATE_ERROR;
2937 }
2938 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002939}
2940
2941static void
2942ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002943 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002944{
2945 struct perf_event *event;
2946 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002948 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2949 /* Ignore events in OFF or ERROR state */
2950 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952 /*
2953 * Listen to the 'cpu' scheduling filter constraint
2954 * of events:
2955 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002956 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002957 continue;
2958
Stephane Eraniane5d13672011-02-14 11:20:01 +02002959 /* may need to reset tstamp_enabled */
2960 if (is_cgroup_event(event))
2961 perf_cgroup_mark_enabled(event, ctx);
2962
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002963 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002964 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002965 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002966 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002967 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002968}
2969
2970static void
2971ctx_sched_in(struct perf_event_context *ctx,
2972 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002973 enum event_type_t event_type,
2974 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002975{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002976 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002977 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002978
Peter Zijlstrac994d612016-01-08 09:20:23 +01002979 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002980
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002981 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002982 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002983
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002984 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002985 if (ctx->task) {
2986 if (!is_active)
2987 cpuctx->task_ctx = ctx;
2988 else
2989 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2990 }
2991
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002992 is_active ^= ctx->is_active; /* changed bits */
2993
2994 if (is_active & EVENT_TIME) {
2995 /* start ctx time */
2996 now = perf_clock();
2997 ctx->timestamp = now;
2998 perf_cgroup_set_timestamp(task, ctx);
2999 }
3000
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003001 /*
3002 * First go through the list and put on any pinned groups
3003 * in order to give them the best chance of going on.
3004 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003005 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003006 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003007
3008 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003009 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003010 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003011}
3012
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003013static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003014 enum event_type_t event_type,
3015 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003016{
3017 struct perf_event_context *ctx = &cpuctx->ctx;
3018
Stephane Eraniane5d13672011-02-14 11:20:01 +02003019 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003020}
3021
Stephane Eraniane5d13672011-02-14 11:20:01 +02003022static void perf_event_context_sched_in(struct perf_event_context *ctx,
3023 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003025 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003027 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003028 if (cpuctx->task_ctx == ctx)
3029 return;
3030
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003031 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003032 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003033 /*
3034 * We want to keep the following priority order:
3035 * cpu pinned (that don't need to move), task pinned,
3036 * cpu flexible, task flexible.
3037 */
3038 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003039 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003040 perf_pmu_enable(ctx->pmu);
3041 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042}
3043
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003044/*
3045 * Called from scheduler to add the events of the current task
3046 * with interrupts disabled.
3047 *
3048 * We restore the event value and then enable it.
3049 *
3050 * This does not protect us against NMI, but enable()
3051 * sets the enabled bit in the control field of event _before_
3052 * accessing the event control register. If a NMI hits, then it will
3053 * keep the event running.
3054 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003055void __perf_event_task_sched_in(struct task_struct *prev,
3056 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003057{
3058 struct perf_event_context *ctx;
3059 int ctxn;
3060
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003061 /*
3062 * If cgroup events exist on this CPU, then we need to check if we have
3063 * to switch in PMU state; cgroup event are system-wide mode only.
3064 *
3065 * Since cgroup events are CPU events, we must schedule these in before
3066 * we schedule in the task events.
3067 */
3068 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3069 perf_cgroup_sched_in(prev, task);
3070
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003071 for_each_task_context_nr(ctxn) {
3072 ctx = task->perf_event_ctxp[ctxn];
3073 if (likely(!ctx))
3074 continue;
3075
Stephane Eraniane5d13672011-02-14 11:20:01 +02003076 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003077 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003078
Adrian Hunter45ac1402015-07-21 12:44:02 +03003079 if (atomic_read(&nr_switch_events))
3080 perf_event_switch(task, prev, true);
3081
Yan, Zhengba532502014-11-04 21:55:58 -05003082 if (__this_cpu_read(perf_sched_cb_usages))
3083 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084}
3085
Peter Zijlstraabd50712010-01-26 18:50:16 +01003086static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3087{
3088 u64 frequency = event->attr.sample_freq;
3089 u64 sec = NSEC_PER_SEC;
3090 u64 divisor, dividend;
3091
3092 int count_fls, nsec_fls, frequency_fls, sec_fls;
3093
3094 count_fls = fls64(count);
3095 nsec_fls = fls64(nsec);
3096 frequency_fls = fls64(frequency);
3097 sec_fls = 30;
3098
3099 /*
3100 * We got @count in @nsec, with a target of sample_freq HZ
3101 * the target period becomes:
3102 *
3103 * @count * 10^9
3104 * period = -------------------
3105 * @nsec * sample_freq
3106 *
3107 */
3108
3109 /*
3110 * Reduce accuracy by one bit such that @a and @b converge
3111 * to a similar magnitude.
3112 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003113#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003114do { \
3115 if (a##_fls > b##_fls) { \
3116 a >>= 1; \
3117 a##_fls--; \
3118 } else { \
3119 b >>= 1; \
3120 b##_fls--; \
3121 } \
3122} while (0)
3123
3124 /*
3125 * Reduce accuracy until either term fits in a u64, then proceed with
3126 * the other, so that finally we can do a u64/u64 division.
3127 */
3128 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3129 REDUCE_FLS(nsec, frequency);
3130 REDUCE_FLS(sec, count);
3131 }
3132
3133 if (count_fls + sec_fls > 64) {
3134 divisor = nsec * frequency;
3135
3136 while (count_fls + sec_fls > 64) {
3137 REDUCE_FLS(count, sec);
3138 divisor >>= 1;
3139 }
3140
3141 dividend = count * sec;
3142 } else {
3143 dividend = count * sec;
3144
3145 while (nsec_fls + frequency_fls > 64) {
3146 REDUCE_FLS(nsec, frequency);
3147 dividend >>= 1;
3148 }
3149
3150 divisor = nsec * frequency;
3151 }
3152
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003153 if (!divisor)
3154 return dividend;
3155
Peter Zijlstraabd50712010-01-26 18:50:16 +01003156 return div64_u64(dividend, divisor);
3157}
3158
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003159static DEFINE_PER_CPU(int, perf_throttled_count);
3160static DEFINE_PER_CPU(u64, perf_throttled_seq);
3161
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003162static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003163{
3164 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003165 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003166 s64 delta;
3167
Peter Zijlstraabd50712010-01-26 18:50:16 +01003168 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003169
3170 delta = (s64)(period - hwc->sample_period);
3171 delta = (delta + 7) / 8; /* low pass filter */
3172
3173 sample_period = hwc->sample_period + delta;
3174
3175 if (!sample_period)
3176 sample_period = 1;
3177
3178 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003179
Peter Zijlstrae7850592010-05-21 14:43:08 +02003180 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003181 if (disable)
3182 event->pmu->stop(event, PERF_EF_UPDATE);
3183
Peter Zijlstrae7850592010-05-21 14:43:08 +02003184 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003185
3186 if (disable)
3187 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003188 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189}
3190
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003191/*
3192 * combine freq adjustment with unthrottling to avoid two passes over the
3193 * events. At the same time, make sure, having freq events does not change
3194 * the rate of unthrottling as that would introduce bias.
3195 */
3196static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3197 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198{
3199 struct perf_event *event;
3200 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003201 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003202 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003204 /*
3205 * only need to iterate over all events iff:
3206 * - context have events in frequency mode (needs freq adjust)
3207 * - there are events to unthrottle on this cpu
3208 */
3209 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003210 return;
3211
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003212 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003213 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003214
Paul Mackerras03541f82009-10-14 16:58:03 +11003215 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216 if (event->state != PERF_EVENT_STATE_ACTIVE)
3217 continue;
3218
Stephane Eranian5632ab12011-01-03 18:20:01 +02003219 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003220 continue;
3221
Alexander Shishkin44377272013-12-16 14:17:36 +02003222 perf_pmu_disable(event->pmu);
3223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 hwc = &event->hw;
3225
Jiri Olsaae23bff2013-08-24 16:45:54 +02003226 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003227 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003228 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003229 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003230 }
3231
3232 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003233 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003235 /*
3236 * stop the event and update event->count
3237 */
3238 event->pmu->stop(event, PERF_EF_UPDATE);
3239
Peter Zijlstrae7850592010-05-21 14:43:08 +02003240 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003241 delta = now - hwc->freq_count_stamp;
3242 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003243
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003244 /*
3245 * restart the event
3246 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003247 * we have stopped the event so tell that
3248 * to perf_adjust_period() to avoid stopping it
3249 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003250 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003251 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003252 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003253
3254 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003255 next:
3256 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003257 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003258
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003259 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003260 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261}
3262
3263/*
3264 * Round-robin a context's events:
3265 */
3266static void rotate_ctx(struct perf_event_context *ctx)
3267{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003268 /*
3269 * Rotate the first entry last of non-pinned groups. Rotation might be
3270 * disabled by the inheritance code.
3271 */
3272 if (!ctx->rotate_disable)
3273 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274}
3275
Stephane Eranian9e630202013-04-03 14:21:33 +02003276static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003278 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003279 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003281 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003282 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3283 rotate = 1;
3284 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003285
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003286 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003287 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003288 if (ctx->nr_events != ctx->nr_active)
3289 rotate = 1;
3290 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003291
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003292 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003293 goto done;
3294
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003295 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003296 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003298 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3299 if (ctx)
3300 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003301
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003302 rotate_ctx(&cpuctx->ctx);
3303 if (ctx)
3304 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003306 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003307
3308 perf_pmu_enable(cpuctx->ctx.pmu);
3309 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003310done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003311
3312 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003313}
3314
3315void perf_event_task_tick(void)
3316{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003317 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3318 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003319 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003320
3321 WARN_ON(!irqs_disabled());
3322
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003323 __this_cpu_inc(perf_throttled_seq);
3324 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003325 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003326
Mark Rutland2fde4f92015-01-07 15:01:54 +00003327 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003328 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003329}
3330
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003331static int event_enable_on_exec(struct perf_event *event,
3332 struct perf_event_context *ctx)
3333{
3334 if (!event->attr.enable_on_exec)
3335 return 0;
3336
3337 event->attr.enable_on_exec = 0;
3338 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3339 return 0;
3340
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003341 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003342
3343 return 1;
3344}
3345
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003346/*
3347 * Enable all of a task's events that have been marked enable-on-exec.
3348 * This expects task == current.
3349 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003350static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003352 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003353 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003354 struct perf_event *event;
3355 unsigned long flags;
3356 int enabled = 0;
3357
3358 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003359 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003360 if (!ctx || !ctx->nr_events)
3361 goto out;
3362
Peter Zijlstra3e349502016-01-08 10:01:18 +01003363 cpuctx = __get_cpu_context(ctx);
3364 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003365 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003366 list_for_each_entry(event, &ctx->event_list, event_entry)
3367 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003368
3369 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003370 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003372 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003373 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003374 ctx_resched(cpuctx, ctx);
3375 }
3376 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003378out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003379 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003380
3381 if (clone_ctx)
3382 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003383}
3384
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003385struct perf_read_data {
3386 struct perf_event *event;
3387 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003388 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003389};
3390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391/*
3392 * Cross CPU call to read the hardware event
3393 */
3394static void __perf_event_read(void *info)
3395{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003396 struct perf_read_data *data = info;
3397 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003399 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003400 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401
3402 /*
3403 * If this is a task context, we need to check whether it is
3404 * the current task context of this cpu. If not it has been
3405 * scheduled out before the smp call arrived. In that case
3406 * event->count would have been updated to a recent sample
3407 * when the event was scheduled out.
3408 */
3409 if (ctx->task && cpuctx->task_ctx != ctx)
3410 return;
3411
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003412 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003413 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003414 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003415 update_cgrp_time_from_event(event);
3416 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003419 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003420 goto unlock;
3421
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003422 if (!data->group) {
3423 pmu->read(event);
3424 data->ret = 0;
3425 goto unlock;
3426 }
3427
3428 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3429
3430 pmu->read(event);
3431
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003432 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3433 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003434 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3435 /*
3436 * Use sibling's PMU rather than @event's since
3437 * sibling could be on different (eg: software) PMU.
3438 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003439 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003440 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003441 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003442
3443 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003444
3445unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003446 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003447}
3448
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003449static inline u64 perf_event_count(struct perf_event *event)
3450{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003451 if (event->pmu->count)
3452 return event->pmu->count(event);
3453
3454 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003455}
3456
Kaixu Xiaffe86902015-08-06 07:02:32 +00003457/*
3458 * NMI-safe method to read a local event, that is an event that
3459 * is:
3460 * - either for the current task, or for this CPU
3461 * - does not have inherit set, for inherited task events
3462 * will not be local and we cannot read them atomically
3463 * - must not have a pmu::count method
3464 */
3465u64 perf_event_read_local(struct perf_event *event)
3466{
3467 unsigned long flags;
3468 u64 val;
3469
3470 /*
3471 * Disabling interrupts avoids all counter scheduling (context
3472 * switches, timer based rotation and IPIs).
3473 */
3474 local_irq_save(flags);
3475
3476 /* If this is a per-task event, it must be for current */
3477 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3478 event->hw.target != current);
3479
3480 /* If this is a per-CPU event, it must be for this CPU */
3481 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3482 event->cpu != smp_processor_id());
3483
3484 /*
3485 * It must not be an event with inherit set, we cannot read
3486 * all child counters from atomic context.
3487 */
3488 WARN_ON_ONCE(event->attr.inherit);
3489
3490 /*
3491 * It must not have a pmu::count method, those are not
3492 * NMI safe.
3493 */
3494 WARN_ON_ONCE(event->pmu->count);
3495
3496 /*
3497 * If the event is currently on this CPU, its either a per-task event,
3498 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3499 * oncpu == -1).
3500 */
3501 if (event->oncpu == smp_processor_id())
3502 event->pmu->read(event);
3503
3504 val = local64_read(&event->count);
3505 local_irq_restore(flags);
3506
3507 return val;
3508}
3509
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003510static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003511{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003512 int ret = 0;
3513
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003514 /*
3515 * If event is enabled and currently active on a CPU, update the
3516 * value in the event structure:
3517 */
3518 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003519 struct perf_read_data data = {
3520 .event = event,
3521 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003522 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003523 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003524 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003525 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003526 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003527 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003528 struct perf_event_context *ctx = event->ctx;
3529 unsigned long flags;
3530
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003531 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003532 /*
3533 * may read while context is not active
3534 * (e.g., thread is blocked), in that case
3535 * we cannot update context time
3536 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003537 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003538 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003539 update_cgrp_time_from_event(event);
3540 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003541 if (group)
3542 update_group_times(event);
3543 else
3544 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003545 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003547
3548 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003549}
3550
3551/*
3552 * Initialize the perf_event context in a task_struct:
3553 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003554static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003555{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003556 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003557 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003558 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003559 INIT_LIST_HEAD(&ctx->pinned_groups);
3560 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003561 INIT_LIST_HEAD(&ctx->event_list);
3562 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563}
3564
Peter Zijlstraeb184472010-09-07 15:55:13 +02003565static struct perf_event_context *
3566alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003567{
3568 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003569
3570 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3571 if (!ctx)
3572 return NULL;
3573
3574 __perf_event_init_context(ctx);
3575 if (task) {
3576 ctx->task = task;
3577 get_task_struct(task);
3578 }
3579 ctx->pmu = pmu;
3580
3581 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582}
3583
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003584static struct task_struct *
3585find_lively_task_by_vpid(pid_t vpid)
3586{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003587 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003589 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003590 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591 task = current;
3592 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003593 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594 if (task)
3595 get_task_struct(task);
3596 rcu_read_unlock();
3597
3598 if (!task)
3599 return ERR_PTR(-ESRCH);
3600
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003601 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003602}
3603
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003604/*
3605 * Returns a matching context with refcount and pincount.
3606 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003607static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003608find_get_context(struct pmu *pmu, struct task_struct *task,
3609 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003610{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003611 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003612 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003613 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003614 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003615 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003616 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003617
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003618 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003619 /* Must be root to operate on a CPU event: */
3620 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3621 return ERR_PTR(-EACCES);
3622
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003623 /*
3624 * We could be clever and allow to attach a event to an
3625 * offline CPU and activate it when the CPU comes up, but
3626 * that's for later.
3627 */
3628 if (!cpu_online(cpu))
3629 return ERR_PTR(-ENODEV);
3630
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003631 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003632 ctx = &cpuctx->ctx;
3633 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003634 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003635
3636 return ctx;
3637 }
3638
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003639 err = -EINVAL;
3640 ctxn = pmu->task_ctx_nr;
3641 if (ctxn < 0)
3642 goto errout;
3643
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003644 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3645 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3646 if (!task_ctx_data) {
3647 err = -ENOMEM;
3648 goto errout;
3649 }
3650 }
3651
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003652retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003653 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003655 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003656 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003657
3658 if (task_ctx_data && !ctx->task_ctx_data) {
3659 ctx->task_ctx_data = task_ctx_data;
3660 task_ctx_data = NULL;
3661 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003662 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003663
3664 if (clone_ctx)
3665 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003666 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003667 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003668 err = -ENOMEM;
3669 if (!ctx)
3670 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003671
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003672 if (task_ctx_data) {
3673 ctx->task_ctx_data = task_ctx_data;
3674 task_ctx_data = NULL;
3675 }
3676
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003677 err = 0;
3678 mutex_lock(&task->perf_event_mutex);
3679 /*
3680 * If it has already passed perf_event_exit_task().
3681 * we must see PF_EXITING, it takes this mutex too.
3682 */
3683 if (task->flags & PF_EXITING)
3684 err = -ESRCH;
3685 else if (task->perf_event_ctxp[ctxn])
3686 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003687 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003688 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003689 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003690 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003691 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003692 mutex_unlock(&task->perf_event_mutex);
3693
3694 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003695 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003696
3697 if (err == -EAGAIN)
3698 goto retry;
3699 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003700 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003701 }
3702
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003703 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003704 return ctx;
3705
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003706errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003707 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708 return ERR_PTR(err);
3709}
3710
Li Zefan6fb29152009-10-15 11:21:42 +08003711static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003712static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003713
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003714static void free_event_rcu(struct rcu_head *head)
3715{
3716 struct perf_event *event;
3717
3718 event = container_of(head, struct perf_event, rcu_head);
3719 if (event->ns)
3720 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003721 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003722 kfree(event);
3723}
3724
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003725static void ring_buffer_attach(struct perf_event *event,
3726 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003727
Kan Liangf2fb6be2016-03-23 11:24:37 -07003728static void detach_sb_event(struct perf_event *event)
3729{
3730 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3731
3732 raw_spin_lock(&pel->lock);
3733 list_del_rcu(&event->sb_list);
3734 raw_spin_unlock(&pel->lock);
3735}
3736
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003737static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003738{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003739 struct perf_event_attr *attr = &event->attr;
3740
Kan Liangf2fb6be2016-03-23 11:24:37 -07003741 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003742 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003743
3744 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003745 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003746
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003747 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3748 attr->comm || attr->comm_exec ||
3749 attr->task ||
3750 attr->context_switch)
3751 return true;
3752 return false;
3753}
3754
3755static void unaccount_pmu_sb_event(struct perf_event *event)
3756{
3757 if (is_sb_event(event))
3758 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003759}
3760
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003761static void unaccount_event_cpu(struct perf_event *event, int cpu)
3762{
3763 if (event->parent)
3764 return;
3765
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003766 if (is_cgroup_event(event))
3767 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3768}
3769
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003770#ifdef CONFIG_NO_HZ_FULL
3771static DEFINE_SPINLOCK(nr_freq_lock);
3772#endif
3773
3774static void unaccount_freq_event_nohz(void)
3775{
3776#ifdef CONFIG_NO_HZ_FULL
3777 spin_lock(&nr_freq_lock);
3778 if (atomic_dec_and_test(&nr_freq_events))
3779 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3780 spin_unlock(&nr_freq_lock);
3781#endif
3782}
3783
3784static void unaccount_freq_event(void)
3785{
3786 if (tick_nohz_full_enabled())
3787 unaccount_freq_event_nohz();
3788 else
3789 atomic_dec(&nr_freq_events);
3790}
3791
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003792static void unaccount_event(struct perf_event *event)
3793{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003794 bool dec = false;
3795
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003796 if (event->parent)
3797 return;
3798
3799 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003800 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003801 if (event->attr.mmap || event->attr.mmap_data)
3802 atomic_dec(&nr_mmap_events);
3803 if (event->attr.comm)
3804 atomic_dec(&nr_comm_events);
3805 if (event->attr.task)
3806 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003807 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003808 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003809 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003810 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003811 atomic_dec(&nr_switch_events);
3812 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003813 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003814 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003815 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003816 dec = true;
3817
Peter Zijlstra9107c892016-02-24 18:45:45 +01003818 if (dec) {
3819 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3820 schedule_delayed_work(&perf_sched_work, HZ);
3821 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003822
3823 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003824
3825 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003826}
3827
Peter Zijlstra9107c892016-02-24 18:45:45 +01003828static void perf_sched_delayed(struct work_struct *work)
3829{
3830 mutex_lock(&perf_sched_mutex);
3831 if (atomic_dec_and_test(&perf_sched_count))
3832 static_branch_disable(&perf_sched_events);
3833 mutex_unlock(&perf_sched_mutex);
3834}
3835
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003836/*
3837 * The following implement mutual exclusion of events on "exclusive" pmus
3838 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3839 * at a time, so we disallow creating events that might conflict, namely:
3840 *
3841 * 1) cpu-wide events in the presence of per-task events,
3842 * 2) per-task events in the presence of cpu-wide events,
3843 * 3) two matching events on the same context.
3844 *
3845 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003846 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003847 */
3848static int exclusive_event_init(struct perf_event *event)
3849{
3850 struct pmu *pmu = event->pmu;
3851
3852 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3853 return 0;
3854
3855 /*
3856 * Prevent co-existence of per-task and cpu-wide events on the
3857 * same exclusive pmu.
3858 *
3859 * Negative pmu::exclusive_cnt means there are cpu-wide
3860 * events on this "exclusive" pmu, positive means there are
3861 * per-task events.
3862 *
3863 * Since this is called in perf_event_alloc() path, event::ctx
3864 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3865 * to mean "per-task event", because unlike other attach states it
3866 * never gets cleared.
3867 */
3868 if (event->attach_state & PERF_ATTACH_TASK) {
3869 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3870 return -EBUSY;
3871 } else {
3872 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3873 return -EBUSY;
3874 }
3875
3876 return 0;
3877}
3878
3879static void exclusive_event_destroy(struct perf_event *event)
3880{
3881 struct pmu *pmu = event->pmu;
3882
3883 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3884 return;
3885
3886 /* see comment in exclusive_event_init() */
3887 if (event->attach_state & PERF_ATTACH_TASK)
3888 atomic_dec(&pmu->exclusive_cnt);
3889 else
3890 atomic_inc(&pmu->exclusive_cnt);
3891}
3892
3893static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3894{
3895 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3896 (e1->cpu == e2->cpu ||
3897 e1->cpu == -1 ||
3898 e2->cpu == -1))
3899 return true;
3900 return false;
3901}
3902
3903/* Called under the same ctx::mutex as perf_install_in_context() */
3904static bool exclusive_event_installable(struct perf_event *event,
3905 struct perf_event_context *ctx)
3906{
3907 struct perf_event *iter_event;
3908 struct pmu *pmu = event->pmu;
3909
3910 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3911 return true;
3912
3913 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3914 if (exclusive_event_match(iter_event, event))
3915 return false;
3916 }
3917
3918 return true;
3919}
3920
Alexander Shishkin375637b2016-04-27 18:44:46 +03003921static void perf_addr_filters_splice(struct perf_event *event,
3922 struct list_head *head);
3923
Peter Zijlstra683ede42014-05-05 12:11:24 +02003924static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003926 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003927
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003928 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003929
Frederic Weisbecker76369132011-05-19 19:55:04 +02003930 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003931 /*
3932 * Can happen when we close an event with re-directed output.
3933 *
3934 * Since we have a 0 refcount, perf_mmap_close() will skip
3935 * over us; possibly making our ring_buffer_put() the last.
3936 */
3937 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003938 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003939 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003940 }
3941
Stephane Eraniane5d13672011-02-14 11:20:01 +02003942 if (is_cgroup_event(event))
3943 perf_detach_cgroup(event);
3944
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003945 if (!event->parent) {
3946 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3947 put_callchain_buffers();
3948 }
3949
3950 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003951 perf_addr_filters_splice(event, NULL);
3952 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003953
3954 if (event->destroy)
3955 event->destroy(event);
3956
3957 if (event->ctx)
3958 put_ctx(event->ctx);
3959
Alexander Shishkin62a92c82016-06-07 15:44:15 +03003960 exclusive_event_destroy(event);
3961 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003962
3963 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964}
3965
Peter Zijlstra683ede42014-05-05 12:11:24 +02003966/*
3967 * Used to free events which have a known refcount of 1, such as in error paths
3968 * where the event isn't exposed yet and inherited events.
3969 */
3970static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003971{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003972 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3973 "unexpected event refcount: %ld; ptr=%p\n",
3974 atomic_long_read(&event->refcount), event)) {
3975 /* leak to avoid use-after-free */
3976 return;
3977 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003978
Peter Zijlstra683ede42014-05-05 12:11:24 +02003979 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003980}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003981
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003982/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003983 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003984 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003985static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003986{
Peter Zijlstra88821352010-11-09 19:01:43 +01003987 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003988
Peter Zijlstra88821352010-11-09 19:01:43 +01003989 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003990 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003991 * Matches the smp_store_release() in perf_event_exit_task(). If we
3992 * observe !owner it means the list deletion is complete and we can
3993 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003994 * owner->perf_event_mutex.
3995 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003996 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01003997 if (owner) {
3998 /*
3999 * Since delayed_put_task_struct() also drops the last
4000 * task reference we can safely take a new reference
4001 * while holding the rcu_read_lock().
4002 */
4003 get_task_struct(owner);
4004 }
4005 rcu_read_unlock();
4006
4007 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004008 /*
4009 * If we're here through perf_event_exit_task() we're already
4010 * holding ctx->mutex which would be an inversion wrt. the
4011 * normal lock order.
4012 *
4013 * However we can safely take this lock because its the child
4014 * ctx->mutex.
4015 */
4016 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4017
Peter Zijlstra88821352010-11-09 19:01:43 +01004018 /*
4019 * We have to re-check the event->owner field, if it is cleared
4020 * we raced with perf_event_exit_task(), acquiring the mutex
4021 * ensured they're done, and we can proceed with freeing the
4022 * event.
4023 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004024 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004025 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004026 smp_store_release(&event->owner, NULL);
4027 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004028 mutex_unlock(&owner->perf_event_mutex);
4029 put_task_struct(owner);
4030 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004031}
4032
Jiri Olsaf8697762014-08-01 14:33:01 +02004033static void put_event(struct perf_event *event)
4034{
Jiri Olsaf8697762014-08-01 14:33:01 +02004035 if (!atomic_long_dec_and_test(&event->refcount))
4036 return;
4037
Peter Zijlstra683ede42014-05-05 12:11:24 +02004038 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004039}
4040
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004041/*
4042 * Kill an event dead; while event:refcount will preserve the event
4043 * object, it will not preserve its functionality. Once the last 'user'
4044 * gives up the object, we'll destroy the thing.
4045 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004046int perf_event_release_kernel(struct perf_event *event)
4047{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004048 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004049 struct perf_event *child, *tmp;
4050
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004051 /*
4052 * If we got here through err_file: fput(event_file); we will not have
4053 * attached to a context yet.
4054 */
4055 if (!ctx) {
4056 WARN_ON_ONCE(event->attach_state &
4057 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4058 goto no_ctx;
4059 }
4060
Peter Zijlstra88821352010-11-09 19:01:43 +01004061 if (!is_kernel_event(event))
4062 perf_remove_from_owner(event);
4063
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004064 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004065 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004066 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004067
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004068 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004069 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004070 * Mark this even as STATE_DEAD, there is no external reference to it
4071 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004072 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004073 * Anybody acquiring event->child_mutex after the below loop _must_
4074 * also see this, most importantly inherit_event() which will avoid
4075 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004076 *
4077 * Thus this guarantees that we will in fact observe and kill _ALL_
4078 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004079 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004080 event->state = PERF_EVENT_STATE_DEAD;
4081 raw_spin_unlock_irq(&ctx->lock);
4082
4083 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004084
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004085again:
4086 mutex_lock(&event->child_mutex);
4087 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004088
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004089 /*
4090 * Cannot change, child events are not migrated, see the
4091 * comment with perf_event_ctx_lock_nested().
4092 */
4093 ctx = lockless_dereference(child->ctx);
4094 /*
4095 * Since child_mutex nests inside ctx::mutex, we must jump
4096 * through hoops. We start by grabbing a reference on the ctx.
4097 *
4098 * Since the event cannot get freed while we hold the
4099 * child_mutex, the context must also exist and have a !0
4100 * reference count.
4101 */
4102 get_ctx(ctx);
4103
4104 /*
4105 * Now that we have a ctx ref, we can drop child_mutex, and
4106 * acquire ctx::mutex without fear of it going away. Then we
4107 * can re-acquire child_mutex.
4108 */
4109 mutex_unlock(&event->child_mutex);
4110 mutex_lock(&ctx->mutex);
4111 mutex_lock(&event->child_mutex);
4112
4113 /*
4114 * Now that we hold ctx::mutex and child_mutex, revalidate our
4115 * state, if child is still the first entry, it didn't get freed
4116 * and we can continue doing so.
4117 */
4118 tmp = list_first_entry_or_null(&event->child_list,
4119 struct perf_event, child_list);
4120 if (tmp == child) {
4121 perf_remove_from_context(child, DETACH_GROUP);
4122 list_del(&child->child_list);
4123 free_event(child);
4124 /*
4125 * This matches the refcount bump in inherit_event();
4126 * this can't be the last reference.
4127 */
4128 put_event(event);
4129 }
4130
4131 mutex_unlock(&event->child_mutex);
4132 mutex_unlock(&ctx->mutex);
4133 put_ctx(ctx);
4134 goto again;
4135 }
4136 mutex_unlock(&event->child_mutex);
4137
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004138no_ctx:
4139 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004140 return 0;
4141}
4142EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4143
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004144/*
4145 * Called when the last reference to the file is gone.
4146 */
Al Viroa6fa9412012-08-20 14:59:25 +01004147static int perf_release(struct inode *inode, struct file *file)
4148{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004149 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004150 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004151}
4152
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004153u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154{
4155 struct perf_event *child;
4156 u64 total = 0;
4157
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004158 *enabled = 0;
4159 *running = 0;
4160
Peter Zijlstra6f105812009-11-20 22:19:56 +01004161 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004162
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004163 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004164 total += perf_event_count(event);
4165
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004166 *enabled += event->total_time_enabled +
4167 atomic64_read(&event->child_total_time_enabled);
4168 *running += event->total_time_running +
4169 atomic64_read(&event->child_total_time_running);
4170
4171 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004172 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004173 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004174 *enabled += child->total_time_enabled;
4175 *running += child->total_time_running;
4176 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004177 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178
4179 return total;
4180}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004181EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004183static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004184 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004186 struct perf_event *sub;
4187 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004188 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004189
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004190 ret = perf_event_read(leader, true);
4191 if (ret)
4192 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004193
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004194 /*
4195 * Since we co-schedule groups, {enabled,running} times of siblings
4196 * will be identical to those of the leader, so we only publish one
4197 * set.
4198 */
4199 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4200 values[n++] += leader->total_time_enabled +
4201 atomic64_read(&leader->child_total_time_enabled);
4202 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004203
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004204 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4205 values[n++] += leader->total_time_running +
4206 atomic64_read(&leader->child_total_time_running);
4207 }
4208
4209 /*
4210 * Write {count,id} tuples for every sibling.
4211 */
4212 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004213 if (read_format & PERF_FORMAT_ID)
4214 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004216 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004217 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004218 if (read_format & PERF_FORMAT_ID)
4219 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004220 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004221
4222 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004223}
4224
4225static int perf_read_group(struct perf_event *event,
4226 u64 read_format, char __user *buf)
4227{
4228 struct perf_event *leader = event->group_leader, *child;
4229 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004230 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004231 u64 *values;
4232
4233 lockdep_assert_held(&ctx->mutex);
4234
4235 values = kzalloc(event->read_size, GFP_KERNEL);
4236 if (!values)
4237 return -ENOMEM;
4238
4239 values[0] = 1 + leader->nr_siblings;
4240
4241 /*
4242 * By locking the child_mutex of the leader we effectively
4243 * lock the child list of all siblings.. XXX explain how.
4244 */
4245 mutex_lock(&leader->child_mutex);
4246
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004247 ret = __perf_read_group_add(leader, read_format, values);
4248 if (ret)
4249 goto unlock;
4250
4251 list_for_each_entry(child, &leader->child_list, child_list) {
4252 ret = __perf_read_group_add(child, read_format, values);
4253 if (ret)
4254 goto unlock;
4255 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004256
4257 mutex_unlock(&leader->child_mutex);
4258
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004259 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004260 if (copy_to_user(buf, values, event->read_size))
4261 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004262 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004263
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004264unlock:
4265 mutex_unlock(&leader->child_mutex);
4266out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004267 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004268 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269}
4270
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004271static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004272 u64 read_format, char __user *buf)
4273{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004274 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275 u64 values[4];
4276 int n = 0;
4277
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004278 values[n++] = perf_event_read_value(event, &enabled, &running);
4279 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4280 values[n++] = enabled;
4281 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4282 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004283 if (read_format & PERF_FORMAT_ID)
4284 values[n++] = primary_event_id(event);
4285
4286 if (copy_to_user(buf, values, n * sizeof(u64)))
4287 return -EFAULT;
4288
4289 return n * sizeof(u64);
4290}
4291
Jiri Olsadc633982014-09-12 13:18:26 +02004292static bool is_event_hup(struct perf_event *event)
4293{
4294 bool no_children;
4295
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004296 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004297 return false;
4298
4299 mutex_lock(&event->child_mutex);
4300 no_children = list_empty(&event->child_list);
4301 mutex_unlock(&event->child_mutex);
4302 return no_children;
4303}
4304
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004305/*
4306 * Read the performance event - simple non blocking version for now
4307 */
4308static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004309__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004310{
4311 u64 read_format = event->attr.read_format;
4312 int ret;
4313
4314 /*
4315 * Return end-of-file for a read on a event that is in
4316 * error state (i.e. because it was pinned but it couldn't be
4317 * scheduled on to the CPU at some point).
4318 */
4319 if (event->state == PERF_EVENT_STATE_ERROR)
4320 return 0;
4321
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004322 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004323 return -ENOSPC;
4324
4325 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004327 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004329 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004330
4331 return ret;
4332}
4333
4334static ssize_t
4335perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4336{
4337 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004338 struct perf_event_context *ctx;
4339 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004340
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004341 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004342 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004343 perf_event_ctx_unlock(event, ctx);
4344
4345 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346}
4347
4348static unsigned int perf_poll(struct file *file, poll_table *wait)
4349{
4350 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004351 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004352 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004353
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004354 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004355
Jiri Olsadc633982014-09-12 13:18:26 +02004356 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004357 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004359 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004360 * Pin the event->rb by taking event->mmap_mutex; otherwise
4361 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004362 */
4363 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004364 rb = event->rb;
4365 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004366 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004367 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 return events;
4369}
4370
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004371static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004372{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004373 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004374 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004375 perf_event_update_userpage(event);
4376}
4377
4378/*
4379 * Holding the top-level event's child_mutex means that any
4380 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004381 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004382 * task existence requirements of perf_event_enable/disable.
4383 */
4384static void perf_event_for_each_child(struct perf_event *event,
4385 void (*func)(struct perf_event *))
4386{
4387 struct perf_event *child;
4388
4389 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004391 mutex_lock(&event->child_mutex);
4392 func(event);
4393 list_for_each_entry(child, &event->child_list, child_list)
4394 func(child);
4395 mutex_unlock(&event->child_mutex);
4396}
4397
4398static void perf_event_for_each(struct perf_event *event,
4399 void (*func)(struct perf_event *))
4400{
4401 struct perf_event_context *ctx = event->ctx;
4402 struct perf_event *sibling;
4403
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004404 lockdep_assert_held(&ctx->mutex);
4405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406 event = event->group_leader;
4407
4408 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004410 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411}
4412
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004413static void __perf_event_period(struct perf_event *event,
4414 struct perf_cpu_context *cpuctx,
4415 struct perf_event_context *ctx,
4416 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004417{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004418 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004419 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004421 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422 event->attr.sample_freq = value;
4423 } else {
4424 event->attr.sample_period = value;
4425 event->hw.sample_period = value;
4426 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004427
4428 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4429 if (active) {
4430 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004431 /*
4432 * We could be throttled; unthrottle now to avoid the tick
4433 * trying to unthrottle while we already re-started the event.
4434 */
4435 if (event->hw.interrupts == MAX_INTERRUPTS) {
4436 event->hw.interrupts = 0;
4437 perf_log_throttle(event, 1);
4438 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004439 event->pmu->stop(event, PERF_EF_UPDATE);
4440 }
4441
4442 local64_set(&event->hw.period_left, 0);
4443
4444 if (active) {
4445 event->pmu->start(event, PERF_EF_RELOAD);
4446 perf_pmu_enable(ctx->pmu);
4447 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004448}
4449
4450static int perf_event_period(struct perf_event *event, u64 __user *arg)
4451{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004452 u64 value;
4453
4454 if (!is_sampling_event(event))
4455 return -EINVAL;
4456
4457 if (copy_from_user(&value, arg, sizeof(value)))
4458 return -EFAULT;
4459
4460 if (!value)
4461 return -EINVAL;
4462
4463 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4464 return -EINVAL;
4465
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004466 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004468 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004469}
4470
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004471static const struct file_operations perf_fops;
4472
Al Viro2903ff02012-08-28 12:52:22 -04004473static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004474{
Al Viro2903ff02012-08-28 12:52:22 -04004475 struct fd f = fdget(fd);
4476 if (!f.file)
4477 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004478
Al Viro2903ff02012-08-28 12:52:22 -04004479 if (f.file->f_op != &perf_fops) {
4480 fdput(f);
4481 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004482 }
Al Viro2903ff02012-08-28 12:52:22 -04004483 *p = f;
4484 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004485}
4486
4487static int perf_event_set_output(struct perf_event *event,
4488 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004489static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004490static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004491
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004492static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494 void (*func)(struct perf_event *);
4495 u32 flags = arg;
4496
4497 switch (cmd) {
4498 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004499 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004500 break;
4501 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004502 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004503 break;
4504 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004505 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506 break;
4507
4508 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004509 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510
4511 case PERF_EVENT_IOC_PERIOD:
4512 return perf_event_period(event, (u64 __user *)arg);
4513
Jiri Olsacf4957f2012-10-24 13:37:58 +02004514 case PERF_EVENT_IOC_ID:
4515 {
4516 u64 id = primary_event_id(event);
4517
4518 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4519 return -EFAULT;
4520 return 0;
4521 }
4522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004523 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004524 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004525 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004526 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004527 struct perf_event *output_event;
4528 struct fd output;
4529 ret = perf_fget_light(arg, &output);
4530 if (ret)
4531 return ret;
4532 output_event = output.file->private_data;
4533 ret = perf_event_set_output(event, output_event);
4534 fdput(output);
4535 } else {
4536 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004537 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004538 return ret;
4539 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004540
Li Zefan6fb29152009-10-15 11:21:42 +08004541 case PERF_EVENT_IOC_SET_FILTER:
4542 return perf_event_set_filter(event, (void __user *)arg);
4543
Alexei Starovoitov25415172015-03-25 12:49:20 -07004544 case PERF_EVENT_IOC_SET_BPF:
4545 return perf_event_set_bpf_prog(event, arg);
4546
Wang Nan86e79722016-03-28 06:41:29 +00004547 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4548 struct ring_buffer *rb;
4549
4550 rcu_read_lock();
4551 rb = rcu_dereference(event->rb);
4552 if (!rb || !rb->nr_pages) {
4553 rcu_read_unlock();
4554 return -EINVAL;
4555 }
4556 rb_toggle_paused(rb, !!arg);
4557 rcu_read_unlock();
4558 return 0;
4559 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004560 default:
4561 return -ENOTTY;
4562 }
4563
4564 if (flags & PERF_IOC_FLAG_GROUP)
4565 perf_event_for_each(event, func);
4566 else
4567 perf_event_for_each_child(event, func);
4568
4569 return 0;
4570}
4571
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004572static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4573{
4574 struct perf_event *event = file->private_data;
4575 struct perf_event_context *ctx;
4576 long ret;
4577
4578 ctx = perf_event_ctx_lock(event);
4579 ret = _perf_ioctl(event, cmd, arg);
4580 perf_event_ctx_unlock(event, ctx);
4581
4582 return ret;
4583}
4584
Pawel Mollb3f20782014-06-13 16:03:32 +01004585#ifdef CONFIG_COMPAT
4586static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4587 unsigned long arg)
4588{
4589 switch (_IOC_NR(cmd)) {
4590 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4591 case _IOC_NR(PERF_EVENT_IOC_ID):
4592 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4593 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4594 cmd &= ~IOCSIZE_MASK;
4595 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4596 }
4597 break;
4598 }
4599 return perf_ioctl(file, cmd, arg);
4600}
4601#else
4602# define perf_compat_ioctl NULL
4603#endif
4604
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004605int perf_event_task_enable(void)
4606{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004607 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004608 struct perf_event *event;
4609
4610 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004611 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4612 ctx = perf_event_ctx_lock(event);
4613 perf_event_for_each_child(event, _perf_event_enable);
4614 perf_event_ctx_unlock(event, ctx);
4615 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004616 mutex_unlock(&current->perf_event_mutex);
4617
4618 return 0;
4619}
4620
4621int perf_event_task_disable(void)
4622{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004623 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004624 struct perf_event *event;
4625
4626 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004627 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4628 ctx = perf_event_ctx_lock(event);
4629 perf_event_for_each_child(event, _perf_event_disable);
4630 perf_event_ctx_unlock(event, ctx);
4631 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004632 mutex_unlock(&current->perf_event_mutex);
4633
4634 return 0;
4635}
4636
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004637static int perf_event_index(struct perf_event *event)
4638{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004639 if (event->hw.state & PERF_HES_STOPPED)
4640 return 0;
4641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642 if (event->state != PERF_EVENT_STATE_ACTIVE)
4643 return 0;
4644
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004645 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004646}
4647
Eric B Munsonc4794292011-06-23 16:34:38 -04004648static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004649 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004650 u64 *enabled,
4651 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004652{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004653 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004654
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004655 *now = perf_clock();
4656 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004657 *enabled = ctx_time - event->tstamp_enabled;
4658 *running = ctx_time - event->tstamp_running;
4659}
4660
Peter Zijlstrafa731582013-09-19 10:16:42 +02004661static void perf_event_init_userpage(struct perf_event *event)
4662{
4663 struct perf_event_mmap_page *userpg;
4664 struct ring_buffer *rb;
4665
4666 rcu_read_lock();
4667 rb = rcu_dereference(event->rb);
4668 if (!rb)
4669 goto unlock;
4670
4671 userpg = rb->user_page;
4672
4673 /* Allow new userspace to detect that bit 0 is deprecated */
4674 userpg->cap_bit0_is_deprecated = 1;
4675 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004676 userpg->data_offset = PAGE_SIZE;
4677 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004678
4679unlock:
4680 rcu_read_unlock();
4681}
4682
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004683void __weak arch_perf_update_userpage(
4684 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004685{
4686}
4687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004688/*
4689 * Callers need to ensure there can be no nesting of this function, otherwise
4690 * the seqlock logic goes bad. We can not serialize this because the arch
4691 * code calls this from NMI context.
4692 */
4693void perf_event_update_userpage(struct perf_event *event)
4694{
4695 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004696 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004697 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
4699 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004700 rb = rcu_dereference(event->rb);
4701 if (!rb)
4702 goto unlock;
4703
Eric B Munson0d641202011-06-24 12:26:26 -04004704 /*
4705 * compute total_time_enabled, total_time_running
4706 * based on snapshot values taken when the event
4707 * was last scheduled in.
4708 *
4709 * we cannot simply called update_context_time()
4710 * because of locking issue as we can be called in
4711 * NMI context
4712 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004713 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004714
Frederic Weisbecker76369132011-05-19 19:55:04 +02004715 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004716 /*
4717 * Disable preemption so as to not let the corresponding user-space
4718 * spin too long if we get preempted.
4719 */
4720 preempt_disable();
4721 ++userpg->lock;
4722 barrier();
4723 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004724 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004725 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004726 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004727
Eric B Munson0d641202011-06-24 12:26:26 -04004728 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004729 atomic64_read(&event->child_total_time_enabled);
4730
Eric B Munson0d641202011-06-24 12:26:26 -04004731 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732 atomic64_read(&event->child_total_time_running);
4733
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004734 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004735
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004736 barrier();
4737 ++userpg->lock;
4738 preempt_enable();
4739unlock:
4740 rcu_read_unlock();
4741}
4742
Peter Zijlstra906010b2009-09-21 16:08:49 +02004743static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4744{
4745 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004746 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004747 int ret = VM_FAULT_SIGBUS;
4748
4749 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4750 if (vmf->pgoff == 0)
4751 ret = 0;
4752 return ret;
4753 }
4754
4755 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004756 rb = rcu_dereference(event->rb);
4757 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004758 goto unlock;
4759
4760 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4761 goto unlock;
4762
Frederic Weisbecker76369132011-05-19 19:55:04 +02004763 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004764 if (!vmf->page)
4765 goto unlock;
4766
4767 get_page(vmf->page);
4768 vmf->page->mapping = vma->vm_file->f_mapping;
4769 vmf->page->index = vmf->pgoff;
4770
4771 ret = 0;
4772unlock:
4773 rcu_read_unlock();
4774
4775 return ret;
4776}
4777
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004778static void ring_buffer_attach(struct perf_event *event,
4779 struct ring_buffer *rb)
4780{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004781 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004782 unsigned long flags;
4783
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004784 if (event->rb) {
4785 /*
4786 * Should be impossible, we set this when removing
4787 * event->rb_entry and wait/clear when adding event->rb_entry.
4788 */
4789 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004790
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004791 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004792 spin_lock_irqsave(&old_rb->event_lock, flags);
4793 list_del_rcu(&event->rb_entry);
4794 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004795
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004796 event->rcu_batches = get_state_synchronize_rcu();
4797 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004798 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004799
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004800 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004801 if (event->rcu_pending) {
4802 cond_synchronize_rcu(event->rcu_batches);
4803 event->rcu_pending = 0;
4804 }
4805
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004806 spin_lock_irqsave(&rb->event_lock, flags);
4807 list_add_rcu(&event->rb_entry, &rb->event_list);
4808 spin_unlock_irqrestore(&rb->event_lock, flags);
4809 }
4810
4811 rcu_assign_pointer(event->rb, rb);
4812
4813 if (old_rb) {
4814 ring_buffer_put(old_rb);
4815 /*
4816 * Since we detached before setting the new rb, so that we
4817 * could attach the new rb, we could have missed a wakeup.
4818 * Provide it now.
4819 */
4820 wake_up_all(&event->waitq);
4821 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004822}
4823
4824static void ring_buffer_wakeup(struct perf_event *event)
4825{
4826 struct ring_buffer *rb;
4827
4828 rcu_read_lock();
4829 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004830 if (rb) {
4831 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4832 wake_up_all(&event->waitq);
4833 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004834 rcu_read_unlock();
4835}
4836
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004837struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004838{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004839 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004840
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004841 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004842 rb = rcu_dereference(event->rb);
4843 if (rb) {
4844 if (!atomic_inc_not_zero(&rb->refcount))
4845 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004846 }
4847 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848
Frederic Weisbecker76369132011-05-19 19:55:04 +02004849 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004850}
4851
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004852void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004853{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004854 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004855 return;
4856
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004857 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004858
Frederic Weisbecker76369132011-05-19 19:55:04 +02004859 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004860}
4861
4862static void perf_mmap_open(struct vm_area_struct *vma)
4863{
4864 struct perf_event *event = vma->vm_file->private_data;
4865
4866 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004867 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004868
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004869 if (vma->vm_pgoff)
4870 atomic_inc(&event->rb->aux_mmap_count);
4871
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004872 if (event->pmu->event_mapped)
4873 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004874}
4875
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004876static void perf_pmu_output_stop(struct perf_event *event);
4877
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004878/*
4879 * A buffer can be mmap()ed multiple times; either directly through the same
4880 * event, or through other events by use of perf_event_set_output().
4881 *
4882 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4883 * the buffer here, where we still have a VM context. This means we need
4884 * to detach all events redirecting to us.
4885 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004886static void perf_mmap_close(struct vm_area_struct *vma)
4887{
4888 struct perf_event *event = vma->vm_file->private_data;
4889
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004890 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004891 struct user_struct *mmap_user = rb->mmap_user;
4892 int mmap_locked = rb->mmap_locked;
4893 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004895 if (event->pmu->event_unmapped)
4896 event->pmu->event_unmapped(event);
4897
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004898 /*
4899 * rb->aux_mmap_count will always drop before rb->mmap_count and
4900 * event->mmap_count, so it is ok to use event->mmap_mutex to
4901 * serialize with perf_mmap here.
4902 */
4903 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4904 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004905 /*
4906 * Stop all AUX events that are writing to this buffer,
4907 * so that we can free its AUX pages and corresponding PMU
4908 * data. Note that after rb::aux_mmap_count dropped to zero,
4909 * they won't start any more (see perf_aux_output_begin()).
4910 */
4911 perf_pmu_output_stop(event);
4912
4913 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004914 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4915 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4916
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004917 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004918 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004919 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
4920
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004921 mutex_unlock(&event->mmap_mutex);
4922 }
4923
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004924 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004925
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004926 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004927 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004928
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004929 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004930 mutex_unlock(&event->mmap_mutex);
4931
4932 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004933 if (atomic_read(&rb->mmap_count))
4934 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004935
4936 /*
4937 * No other mmap()s, detach from all other events that might redirect
4938 * into the now unreachable buffer. Somewhat complicated by the
4939 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4940 */
4941again:
4942 rcu_read_lock();
4943 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4944 if (!atomic_long_inc_not_zero(&event->refcount)) {
4945 /*
4946 * This event is en-route to free_event() which will
4947 * detach it and remove it from the list.
4948 */
4949 continue;
4950 }
4951 rcu_read_unlock();
4952
4953 mutex_lock(&event->mmap_mutex);
4954 /*
4955 * Check we didn't race with perf_event_set_output() which can
4956 * swizzle the rb from under us while we were waiting to
4957 * acquire mmap_mutex.
4958 *
4959 * If we find a different rb; ignore this event, a next
4960 * iteration will no longer find it on the list. We have to
4961 * still restart the iteration to make sure we're not now
4962 * iterating the wrong list.
4963 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004964 if (event->rb == rb)
4965 ring_buffer_attach(event, NULL);
4966
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004967 mutex_unlock(&event->mmap_mutex);
4968 put_event(event);
4969
4970 /*
4971 * Restart the iteration; either we're on the wrong list or
4972 * destroyed its integrity by doing a deletion.
4973 */
4974 goto again;
4975 }
4976 rcu_read_unlock();
4977
4978 /*
4979 * It could be there's still a few 0-ref events on the list; they'll
4980 * get cleaned up by free_event() -- they'll also still have their
4981 * ref on the rb and will free it whenever they are done with it.
4982 *
4983 * Aside from that, this buffer is 'fully' detached and unmapped,
4984 * undo the VM accounting.
4985 */
4986
4987 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4988 vma->vm_mm->pinned_vm -= mmap_locked;
4989 free_uid(mmap_user);
4990
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004991out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004992 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004993}
4994
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004995static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004996 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004997 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998 .fault = perf_mmap_fault,
4999 .page_mkwrite = perf_mmap_fault,
5000};
5001
5002static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5003{
5004 struct perf_event *event = file->private_data;
5005 unsigned long user_locked, user_lock_limit;
5006 struct user_struct *user = current_user();
5007 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005008 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009 unsigned long vma_size;
5010 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005011 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005012 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005013
Peter Zijlstrac7920612010-05-18 10:33:24 +02005014 /*
5015 * Don't allow mmap() of inherited per-task counters. This would
5016 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005017 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005018 */
5019 if (event->cpu == -1 && event->attr.inherit)
5020 return -EINVAL;
5021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022 if (!(vma->vm_flags & VM_SHARED))
5023 return -EINVAL;
5024
5025 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005026
5027 if (vma->vm_pgoff == 0) {
5028 nr_pages = (vma_size / PAGE_SIZE) - 1;
5029 } else {
5030 /*
5031 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5032 * mapped, all subsequent mappings should have the same size
5033 * and offset. Must be above the normal perf buffer.
5034 */
5035 u64 aux_offset, aux_size;
5036
5037 if (!event->rb)
5038 return -EINVAL;
5039
5040 nr_pages = vma_size / PAGE_SIZE;
5041
5042 mutex_lock(&event->mmap_mutex);
5043 ret = -EINVAL;
5044
5045 rb = event->rb;
5046 if (!rb)
5047 goto aux_unlock;
5048
5049 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5050 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5051
5052 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5053 goto aux_unlock;
5054
5055 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5056 goto aux_unlock;
5057
5058 /* already mapped with a different offset */
5059 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5060 goto aux_unlock;
5061
5062 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5063 goto aux_unlock;
5064
5065 /* already mapped with a different size */
5066 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5067 goto aux_unlock;
5068
5069 if (!is_power_of_2(nr_pages))
5070 goto aux_unlock;
5071
5072 if (!atomic_inc_not_zero(&rb->mmap_count))
5073 goto aux_unlock;
5074
5075 if (rb_has_aux(rb)) {
5076 atomic_inc(&rb->aux_mmap_count);
5077 ret = 0;
5078 goto unlock;
5079 }
5080
5081 atomic_set(&rb->aux_mmap_count, 1);
5082 user_extra = nr_pages;
5083
5084 goto accounting;
5085 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086
5087 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005088 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005089 * can do bitmasks instead of modulo.
5090 */
Kan Liang2ed11312015-03-02 02:14:26 -05005091 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005092 return -EINVAL;
5093
5094 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5095 return -EINVAL;
5096
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005097 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005098again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005099 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005100 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005101 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005102 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005103 goto unlock;
5104 }
5105
5106 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5107 /*
5108 * Raced against perf_mmap_close() through
5109 * perf_event_set_output(). Try again, hope for better
5110 * luck.
5111 */
5112 mutex_unlock(&event->mmap_mutex);
5113 goto again;
5114 }
5115
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005116 goto unlock;
5117 }
5118
5119 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005120
5121accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005122 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5123
5124 /*
5125 * Increase the limit linearly with more CPUs:
5126 */
5127 user_lock_limit *= num_online_cpus();
5128
5129 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5130
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005131 if (user_locked > user_lock_limit)
5132 extra = user_locked - user_lock_limit;
5133
Jiri Slaby78d7d402010-03-05 13:42:54 -08005134 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005135 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005136 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005137
5138 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5139 !capable(CAP_IPC_LOCK)) {
5140 ret = -EPERM;
5141 goto unlock;
5142 }
5143
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005144 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005145
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005146 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005147 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005148
Frederic Weisbecker76369132011-05-19 19:55:04 +02005149 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005150 rb = rb_alloc(nr_pages,
5151 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5152 event->cpu, flags);
5153
5154 if (!rb) {
5155 ret = -ENOMEM;
5156 goto unlock;
5157 }
5158
5159 atomic_set(&rb->mmap_count, 1);
5160 rb->mmap_user = get_current_user();
5161 rb->mmap_locked = extra;
5162
5163 ring_buffer_attach(event, rb);
5164
5165 perf_event_init_userpage(event);
5166 perf_event_update_userpage(event);
5167 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005168 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5169 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005170 if (!ret)
5171 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005172 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005174unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005175 if (!ret) {
5176 atomic_long_add(user_extra, &user->locked_vm);
5177 vma->vm_mm->pinned_vm += extra;
5178
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005179 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005180 } else if (rb) {
5181 atomic_dec(&rb->mmap_count);
5182 }
5183aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 mutex_unlock(&event->mmap_mutex);
5185
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005186 /*
5187 * Since pinned accounting is per vm we cannot allow fork() to copy our
5188 * vma.
5189 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005190 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005191 vma->vm_ops = &perf_mmap_vmops;
5192
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005193 if (event->pmu->event_mapped)
5194 event->pmu->event_mapped(event);
5195
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005196 return ret;
5197}
5198
5199static int perf_fasync(int fd, struct file *filp, int on)
5200{
Al Viro496ad9a2013-01-23 17:07:38 -05005201 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005202 struct perf_event *event = filp->private_data;
5203 int retval;
5204
Al Viro59551022016-01-22 15:40:57 -05005205 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005206 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005207 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005208
5209 if (retval < 0)
5210 return retval;
5211
5212 return 0;
5213}
5214
5215static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005216 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005217 .release = perf_release,
5218 .read = perf_read,
5219 .poll = perf_poll,
5220 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005221 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222 .mmap = perf_mmap,
5223 .fasync = perf_fasync,
5224};
5225
5226/*
5227 * Perf event wakeup
5228 *
5229 * If there's data, ensure we set the poll() state and publish everything
5230 * to user-space before waking everybody up.
5231 */
5232
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005233static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5234{
5235 /* only the parent has fasync state */
5236 if (event->parent)
5237 event = event->parent;
5238 return &event->fasync;
5239}
5240
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241void perf_event_wakeup(struct perf_event *event)
5242{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005243 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005244
5245 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005246 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247 event->pending_kill = 0;
5248 }
5249}
5250
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005251static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005252{
5253 struct perf_event *event = container_of(entry,
5254 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005255 int rctx;
5256
5257 rctx = perf_swevent_get_recursion_context();
5258 /*
5259 * If we 'fail' here, that's OK, it means recursion is already disabled
5260 * and we won't recurse 'further'.
5261 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005262
5263 if (event->pending_disable) {
5264 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005265 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266 }
5267
5268 if (event->pending_wakeup) {
5269 event->pending_wakeup = 0;
5270 perf_event_wakeup(event);
5271 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005272
5273 if (rctx >= 0)
5274 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005275}
5276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005277/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005278 * We assume there is only KVM supporting the callbacks.
5279 * Later on, we might change it to a list if there is
5280 * another virtualization implementation supporting the callbacks.
5281 */
5282struct perf_guest_info_callbacks *perf_guest_cbs;
5283
5284int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5285{
5286 perf_guest_cbs = cbs;
5287 return 0;
5288}
5289EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5290
5291int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5292{
5293 perf_guest_cbs = NULL;
5294 return 0;
5295}
5296EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5297
Jiri Olsa40189942012-08-07 15:20:37 +02005298static void
5299perf_output_sample_regs(struct perf_output_handle *handle,
5300 struct pt_regs *regs, u64 mask)
5301{
5302 int bit;
5303
5304 for_each_set_bit(bit, (const unsigned long *) &mask,
5305 sizeof(mask) * BITS_PER_BYTE) {
5306 u64 val;
5307
5308 val = perf_reg_value(regs, bit);
5309 perf_output_put(handle, val);
5310 }
5311}
5312
Stephane Eranian60e23642014-09-24 13:48:37 +02005313static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005314 struct pt_regs *regs,
5315 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005316{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005317 if (user_mode(regs)) {
5318 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005319 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005320 } else if (current->mm) {
5321 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005322 } else {
5323 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5324 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005325 }
5326}
5327
Stephane Eranian60e23642014-09-24 13:48:37 +02005328static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5329 struct pt_regs *regs)
5330{
5331 regs_intr->regs = regs;
5332 regs_intr->abi = perf_reg_abi(current);
5333}
5334
5335
Jiri Olsac5ebced2012-08-07 15:20:40 +02005336/*
5337 * Get remaining task size from user stack pointer.
5338 *
5339 * It'd be better to take stack vma map and limit this more
5340 * precisly, but there's no way to get it safely under interrupt,
5341 * so using TASK_SIZE as limit.
5342 */
5343static u64 perf_ustack_task_size(struct pt_regs *regs)
5344{
5345 unsigned long addr = perf_user_stack_pointer(regs);
5346
5347 if (!addr || addr >= TASK_SIZE)
5348 return 0;
5349
5350 return TASK_SIZE - addr;
5351}
5352
5353static u16
5354perf_sample_ustack_size(u16 stack_size, u16 header_size,
5355 struct pt_regs *regs)
5356{
5357 u64 task_size;
5358
5359 /* No regs, no stack pointer, no dump. */
5360 if (!regs)
5361 return 0;
5362
5363 /*
5364 * Check if we fit in with the requested stack size into the:
5365 * - TASK_SIZE
5366 * If we don't, we limit the size to the TASK_SIZE.
5367 *
5368 * - remaining sample size
5369 * If we don't, we customize the stack size to
5370 * fit in to the remaining sample size.
5371 */
5372
5373 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5374 stack_size = min(stack_size, (u16) task_size);
5375
5376 /* Current header size plus static size and dynamic size. */
5377 header_size += 2 * sizeof(u64);
5378
5379 /* Do we fit in with the current stack dump size? */
5380 if ((u16) (header_size + stack_size) < header_size) {
5381 /*
5382 * If we overflow the maximum size for the sample,
5383 * we customize the stack dump size to fit in.
5384 */
5385 stack_size = USHRT_MAX - header_size - sizeof(u64);
5386 stack_size = round_up(stack_size, sizeof(u64));
5387 }
5388
5389 return stack_size;
5390}
5391
5392static void
5393perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5394 struct pt_regs *regs)
5395{
5396 /* Case of a kernel thread, nothing to dump */
5397 if (!regs) {
5398 u64 size = 0;
5399 perf_output_put(handle, size);
5400 } else {
5401 unsigned long sp;
5402 unsigned int rem;
5403 u64 dyn_size;
5404
5405 /*
5406 * We dump:
5407 * static size
5408 * - the size requested by user or the best one we can fit
5409 * in to the sample max size
5410 * data
5411 * - user stack dump data
5412 * dynamic size
5413 * - the actual dumped size
5414 */
5415
5416 /* Static size. */
5417 perf_output_put(handle, dump_size);
5418
5419 /* Data. */
5420 sp = perf_user_stack_pointer(regs);
5421 rem = __output_copy_user(handle, (void *) sp, dump_size);
5422 dyn_size = dump_size - rem;
5423
5424 perf_output_skip(handle, rem);
5425
5426 /* Dynamic size. */
5427 perf_output_put(handle, dyn_size);
5428 }
5429}
5430
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005431static void __perf_event_header__init_id(struct perf_event_header *header,
5432 struct perf_sample_data *data,
5433 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005434{
5435 u64 sample_type = event->attr.sample_type;
5436
5437 data->type = sample_type;
5438 header->size += event->id_header_size;
5439
5440 if (sample_type & PERF_SAMPLE_TID) {
5441 /* namespace issues */
5442 data->tid_entry.pid = perf_event_pid(event, current);
5443 data->tid_entry.tid = perf_event_tid(event, current);
5444 }
5445
5446 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005447 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005448
Adrian Hunterff3d5272013-08-27 11:23:07 +03005449 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005450 data->id = primary_event_id(event);
5451
5452 if (sample_type & PERF_SAMPLE_STREAM_ID)
5453 data->stream_id = event->id;
5454
5455 if (sample_type & PERF_SAMPLE_CPU) {
5456 data->cpu_entry.cpu = raw_smp_processor_id();
5457 data->cpu_entry.reserved = 0;
5458 }
5459}
5460
Frederic Weisbecker76369132011-05-19 19:55:04 +02005461void perf_event_header__init_id(struct perf_event_header *header,
5462 struct perf_sample_data *data,
5463 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005464{
5465 if (event->attr.sample_id_all)
5466 __perf_event_header__init_id(header, data, event);
5467}
5468
5469static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5470 struct perf_sample_data *data)
5471{
5472 u64 sample_type = data->type;
5473
5474 if (sample_type & PERF_SAMPLE_TID)
5475 perf_output_put(handle, data->tid_entry);
5476
5477 if (sample_type & PERF_SAMPLE_TIME)
5478 perf_output_put(handle, data->time);
5479
5480 if (sample_type & PERF_SAMPLE_ID)
5481 perf_output_put(handle, data->id);
5482
5483 if (sample_type & PERF_SAMPLE_STREAM_ID)
5484 perf_output_put(handle, data->stream_id);
5485
5486 if (sample_type & PERF_SAMPLE_CPU)
5487 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005488
5489 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5490 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005491}
5492
Frederic Weisbecker76369132011-05-19 19:55:04 +02005493void perf_event__output_id_sample(struct perf_event *event,
5494 struct perf_output_handle *handle,
5495 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005496{
5497 if (event->attr.sample_id_all)
5498 __perf_event__output_id_sample(handle, sample);
5499}
5500
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005501static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005502 struct perf_event *event,
5503 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005504{
5505 u64 read_format = event->attr.read_format;
5506 u64 values[4];
5507 int n = 0;
5508
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005509 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005510 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005511 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005512 atomic64_read(&event->child_total_time_enabled);
5513 }
5514 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005515 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005516 atomic64_read(&event->child_total_time_running);
5517 }
5518 if (read_format & PERF_FORMAT_ID)
5519 values[n++] = primary_event_id(event);
5520
Frederic Weisbecker76369132011-05-19 19:55:04 +02005521 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005522}
5523
5524/*
5525 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5526 */
5527static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005528 struct perf_event *event,
5529 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005530{
5531 struct perf_event *leader = event->group_leader, *sub;
5532 u64 read_format = event->attr.read_format;
5533 u64 values[5];
5534 int n = 0;
5535
5536 values[n++] = 1 + leader->nr_siblings;
5537
5538 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005539 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005540
5541 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005542 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005543
5544 if (leader != event)
5545 leader->pmu->read(leader);
5546
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005547 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005548 if (read_format & PERF_FORMAT_ID)
5549 values[n++] = primary_event_id(leader);
5550
Frederic Weisbecker76369132011-05-19 19:55:04 +02005551 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005552
5553 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5554 n = 0;
5555
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005556 if ((sub != event) &&
5557 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005558 sub->pmu->read(sub);
5559
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005560 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005561 if (read_format & PERF_FORMAT_ID)
5562 values[n++] = primary_event_id(sub);
5563
Frederic Weisbecker76369132011-05-19 19:55:04 +02005564 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005565 }
5566}
5567
Stephane Eranianeed01522010-10-26 16:08:01 +02005568#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5569 PERF_FORMAT_TOTAL_TIME_RUNNING)
5570
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005571static void perf_output_read(struct perf_output_handle *handle,
5572 struct perf_event *event)
5573{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005574 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005575 u64 read_format = event->attr.read_format;
5576
5577 /*
5578 * compute total_time_enabled, total_time_running
5579 * based on snapshot values taken when the event
5580 * was last scheduled in.
5581 *
5582 * we cannot simply called update_context_time()
5583 * because of locking issue as we are called in
5584 * NMI context
5585 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005586 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005587 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005589 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005590 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005591 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005592 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593}
5594
5595void perf_output_sample(struct perf_output_handle *handle,
5596 struct perf_event_header *header,
5597 struct perf_sample_data *data,
5598 struct perf_event *event)
5599{
5600 u64 sample_type = data->type;
5601
5602 perf_output_put(handle, *header);
5603
Adrian Hunterff3d5272013-08-27 11:23:07 +03005604 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5605 perf_output_put(handle, data->id);
5606
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005607 if (sample_type & PERF_SAMPLE_IP)
5608 perf_output_put(handle, data->ip);
5609
5610 if (sample_type & PERF_SAMPLE_TID)
5611 perf_output_put(handle, data->tid_entry);
5612
5613 if (sample_type & PERF_SAMPLE_TIME)
5614 perf_output_put(handle, data->time);
5615
5616 if (sample_type & PERF_SAMPLE_ADDR)
5617 perf_output_put(handle, data->addr);
5618
5619 if (sample_type & PERF_SAMPLE_ID)
5620 perf_output_put(handle, data->id);
5621
5622 if (sample_type & PERF_SAMPLE_STREAM_ID)
5623 perf_output_put(handle, data->stream_id);
5624
5625 if (sample_type & PERF_SAMPLE_CPU)
5626 perf_output_put(handle, data->cpu_entry);
5627
5628 if (sample_type & PERF_SAMPLE_PERIOD)
5629 perf_output_put(handle, data->period);
5630
5631 if (sample_type & PERF_SAMPLE_READ)
5632 perf_output_read(handle, event);
5633
5634 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5635 if (data->callchain) {
5636 int size = 1;
5637
5638 if (data->callchain)
5639 size += data->callchain->nr;
5640
5641 size *= sizeof(u64);
5642
Frederic Weisbecker76369132011-05-19 19:55:04 +02005643 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644 } else {
5645 u64 nr = 0;
5646 perf_output_put(handle, nr);
5647 }
5648 }
5649
5650 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005651 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005652
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005653 if (raw) {
5654 struct perf_raw_frag *frag = &raw->frag;
5655
5656 perf_output_put(handle, raw->size);
5657 do {
5658 if (frag->copy) {
5659 __output_custom(handle, frag->copy,
5660 frag->data, frag->size);
5661 } else {
5662 __output_copy(handle, frag->data,
5663 frag->size);
5664 }
5665 if (perf_raw_frag_last(frag))
5666 break;
5667 frag = frag->next;
5668 } while (1);
5669 if (frag->pad)
5670 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005671 } else {
5672 struct {
5673 u32 size;
5674 u32 data;
5675 } raw = {
5676 .size = sizeof(u32),
5677 .data = 0,
5678 };
5679 perf_output_put(handle, raw);
5680 }
5681 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005682
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005683 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5684 if (data->br_stack) {
5685 size_t size;
5686
5687 size = data->br_stack->nr
5688 * sizeof(struct perf_branch_entry);
5689
5690 perf_output_put(handle, data->br_stack->nr);
5691 perf_output_copy(handle, data->br_stack->entries, size);
5692 } else {
5693 /*
5694 * we always store at least the value of nr
5695 */
5696 u64 nr = 0;
5697 perf_output_put(handle, nr);
5698 }
5699 }
Jiri Olsa40189942012-08-07 15:20:37 +02005700
5701 if (sample_type & PERF_SAMPLE_REGS_USER) {
5702 u64 abi = data->regs_user.abi;
5703
5704 /*
5705 * If there are no regs to dump, notice it through
5706 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5707 */
5708 perf_output_put(handle, abi);
5709
5710 if (abi) {
5711 u64 mask = event->attr.sample_regs_user;
5712 perf_output_sample_regs(handle,
5713 data->regs_user.regs,
5714 mask);
5715 }
5716 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005717
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005718 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005719 perf_output_sample_ustack(handle,
5720 data->stack_user_size,
5721 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005722 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005723
5724 if (sample_type & PERF_SAMPLE_WEIGHT)
5725 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005726
5727 if (sample_type & PERF_SAMPLE_DATA_SRC)
5728 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005729
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005730 if (sample_type & PERF_SAMPLE_TRANSACTION)
5731 perf_output_put(handle, data->txn);
5732
Stephane Eranian60e23642014-09-24 13:48:37 +02005733 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5734 u64 abi = data->regs_intr.abi;
5735 /*
5736 * If there are no regs to dump, notice it through
5737 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5738 */
5739 perf_output_put(handle, abi);
5740
5741 if (abi) {
5742 u64 mask = event->attr.sample_regs_intr;
5743
5744 perf_output_sample_regs(handle,
5745 data->regs_intr.regs,
5746 mask);
5747 }
5748 }
5749
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005750 if (!event->attr.watermark) {
5751 int wakeup_events = event->attr.wakeup_events;
5752
5753 if (wakeup_events) {
5754 struct ring_buffer *rb = handle->rb;
5755 int events = local_inc_return(&rb->events);
5756
5757 if (events >= wakeup_events) {
5758 local_sub(wakeup_events, &rb->events);
5759 local_inc(&rb->wakeup);
5760 }
5761 }
5762 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005763}
5764
5765void perf_prepare_sample(struct perf_event_header *header,
5766 struct perf_sample_data *data,
5767 struct perf_event *event,
5768 struct pt_regs *regs)
5769{
5770 u64 sample_type = event->attr.sample_type;
5771
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005772 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005773 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005774
5775 header->misc = 0;
5776 header->misc |= perf_misc_flags(regs);
5777
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005778 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005779
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005780 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005781 data->ip = perf_instruction_pointer(regs);
5782
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005783 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5784 int size = 1;
5785
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005786 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005787
5788 if (data->callchain)
5789 size += data->callchain->nr;
5790
5791 header->size += size * sizeof(u64);
5792 }
5793
5794 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005795 struct perf_raw_record *raw = data->raw;
5796 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005797
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005798 if (raw) {
5799 struct perf_raw_frag *frag = &raw->frag;
5800 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005801
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005802 do {
5803 sum += frag->size;
5804 if (perf_raw_frag_last(frag))
5805 break;
5806 frag = frag->next;
5807 } while (1);
5808
5809 size = round_up(sum + sizeof(u32), sizeof(u64));
5810 raw->size = size - sizeof(u32);
5811 frag->pad = raw->size - sum;
5812 } else {
5813 size = sizeof(u64);
5814 }
5815
5816 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005817 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005818
5819 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5820 int size = sizeof(u64); /* nr */
5821 if (data->br_stack) {
5822 size += data->br_stack->nr
5823 * sizeof(struct perf_branch_entry);
5824 }
5825 header->size += size;
5826 }
Jiri Olsa40189942012-08-07 15:20:37 +02005827
Peter Zijlstra25657112014-09-24 13:48:42 +02005828 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005829 perf_sample_regs_user(&data->regs_user, regs,
5830 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005831
Jiri Olsa40189942012-08-07 15:20:37 +02005832 if (sample_type & PERF_SAMPLE_REGS_USER) {
5833 /* regs dump ABI info */
5834 int size = sizeof(u64);
5835
Jiri Olsa40189942012-08-07 15:20:37 +02005836 if (data->regs_user.regs) {
5837 u64 mask = event->attr.sample_regs_user;
5838 size += hweight64(mask) * sizeof(u64);
5839 }
5840
5841 header->size += size;
5842 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005843
5844 if (sample_type & PERF_SAMPLE_STACK_USER) {
5845 /*
5846 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5847 * processed as the last one or have additional check added
5848 * in case new sample type is added, because we could eat
5849 * up the rest of the sample size.
5850 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005851 u16 stack_size = event->attr.sample_stack_user;
5852 u16 size = sizeof(u64);
5853
Jiri Olsac5ebced2012-08-07 15:20:40 +02005854 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005855 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005856
5857 /*
5858 * If there is something to dump, add space for the dump
5859 * itself and for the field that tells the dynamic size,
5860 * which is how many have been actually dumped.
5861 */
5862 if (stack_size)
5863 size += sizeof(u64) + stack_size;
5864
5865 data->stack_user_size = stack_size;
5866 header->size += size;
5867 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005868
5869 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5870 /* regs dump ABI info */
5871 int size = sizeof(u64);
5872
5873 perf_sample_regs_intr(&data->regs_intr, regs);
5874
5875 if (data->regs_intr.regs) {
5876 u64 mask = event->attr.sample_regs_intr;
5877
5878 size += hweight64(mask) * sizeof(u64);
5879 }
5880
5881 header->size += size;
5882 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005883}
5884
Wang Nan9ecda412016-04-05 14:11:18 +00005885static void __always_inline
5886__perf_event_output(struct perf_event *event,
5887 struct perf_sample_data *data,
5888 struct pt_regs *regs,
5889 int (*output_begin)(struct perf_output_handle *,
5890 struct perf_event *,
5891 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005892{
5893 struct perf_output_handle handle;
5894 struct perf_event_header header;
5895
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005896 /* protect the callchain buffers */
5897 rcu_read_lock();
5898
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005899 perf_prepare_sample(&header, data, event, regs);
5900
Wang Nan9ecda412016-04-05 14:11:18 +00005901 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005902 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005903
5904 perf_output_sample(&handle, &header, data, event);
5905
5906 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005907
5908exit:
5909 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005910}
5911
Wang Nan9ecda412016-04-05 14:11:18 +00005912void
5913perf_event_output_forward(struct perf_event *event,
5914 struct perf_sample_data *data,
5915 struct pt_regs *regs)
5916{
5917 __perf_event_output(event, data, regs, perf_output_begin_forward);
5918}
5919
5920void
5921perf_event_output_backward(struct perf_event *event,
5922 struct perf_sample_data *data,
5923 struct pt_regs *regs)
5924{
5925 __perf_event_output(event, data, regs, perf_output_begin_backward);
5926}
5927
5928void
5929perf_event_output(struct perf_event *event,
5930 struct perf_sample_data *data,
5931 struct pt_regs *regs)
5932{
5933 __perf_event_output(event, data, regs, perf_output_begin);
5934}
5935
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005936/*
5937 * read event_id
5938 */
5939
5940struct perf_read_event {
5941 struct perf_event_header header;
5942
5943 u32 pid;
5944 u32 tid;
5945};
5946
5947static void
5948perf_event_read_event(struct perf_event *event,
5949 struct task_struct *task)
5950{
5951 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005952 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005953 struct perf_read_event read_event = {
5954 .header = {
5955 .type = PERF_RECORD_READ,
5956 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005957 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005958 },
5959 .pid = perf_event_pid(event, task),
5960 .tid = perf_event_tid(event, task),
5961 };
5962 int ret;
5963
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005964 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005965 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005966 if (ret)
5967 return;
5968
5969 perf_output_put(&handle, read_event);
5970 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005971 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972
5973 perf_output_end(&handle);
5974}
5975
Peter Zijlstraaab5b712016-05-12 17:26:46 +02005976typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005977
5978static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02005979perf_iterate_ctx(struct perf_event_context *ctx,
5980 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005981 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02005982{
5983 struct perf_event *event;
5984
5985 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005986 if (!all) {
5987 if (event->state < PERF_EVENT_STATE_INACTIVE)
5988 continue;
5989 if (!event_filter_match(event))
5990 continue;
5991 }
5992
Jiri Olsa67516842013-07-09 18:56:31 +02005993 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005994 }
5995}
5996
Peter Zijlstraaab5b712016-05-12 17:26:46 +02005997static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07005998{
5999 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6000 struct perf_event *event;
6001
6002 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006003 /*
6004 * Skip events that are not fully formed yet; ensure that
6005 * if we observe event->ctx, both event and ctx will be
6006 * complete enough. See perf_install_in_context().
6007 */
6008 if (!smp_load_acquire(&event->ctx))
6009 continue;
6010
Kan Liangf2fb6be2016-03-23 11:24:37 -07006011 if (event->state < PERF_EVENT_STATE_INACTIVE)
6012 continue;
6013 if (!event_filter_match(event))
6014 continue;
6015 output(event, data);
6016 }
6017}
6018
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006019/*
6020 * Iterate all events that need to receive side-band events.
6021 *
6022 * For new callers; ensure that account_pmu_sb_event() includes
6023 * your event, otherwise it might not get delivered.
6024 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006025static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006026perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006027 struct perf_event_context *task_ctx)
6028{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006029 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006030 int ctxn;
6031
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006032 rcu_read_lock();
6033 preempt_disable();
6034
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006035 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006036 * If we have task_ctx != NULL we only notify the task context itself.
6037 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006038 * context.
6039 */
6040 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006041 perf_iterate_ctx(task_ctx, output, data, false);
6042 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006043 }
6044
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006045 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006046
6047 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006048 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6049 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006050 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006051 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006052done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006053 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006054 rcu_read_unlock();
6055}
6056
Alexander Shishkin375637b2016-04-27 18:44:46 +03006057/*
6058 * Clear all file-based filters at exec, they'll have to be
6059 * re-instated when/if these objects are mmapped again.
6060 */
6061static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6062{
6063 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6064 struct perf_addr_filter *filter;
6065 unsigned int restart = 0, count = 0;
6066 unsigned long flags;
6067
6068 if (!has_addr_filter(event))
6069 return;
6070
6071 raw_spin_lock_irqsave(&ifh->lock, flags);
6072 list_for_each_entry(filter, &ifh->list, entry) {
6073 if (filter->inode) {
6074 event->addr_filters_offs[count] = 0;
6075 restart++;
6076 }
6077
6078 count++;
6079 }
6080
6081 if (restart)
6082 event->addr_filters_gen++;
6083 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6084
6085 if (restart)
6086 perf_event_restart(event);
6087}
6088
6089void perf_event_exec(void)
6090{
6091 struct perf_event_context *ctx;
6092 int ctxn;
6093
6094 rcu_read_lock();
6095 for_each_task_context_nr(ctxn) {
6096 ctx = current->perf_event_ctxp[ctxn];
6097 if (!ctx)
6098 continue;
6099
6100 perf_event_enable_on_exec(ctxn);
6101
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006102 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006103 true);
6104 }
6105 rcu_read_unlock();
6106}
6107
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006108struct remote_output {
6109 struct ring_buffer *rb;
6110 int err;
6111};
6112
6113static void __perf_event_output_stop(struct perf_event *event, void *data)
6114{
6115 struct perf_event *parent = event->parent;
6116 struct remote_output *ro = data;
6117 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006118 struct stop_event_data sd = {
6119 .event = event,
6120 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006121
6122 if (!has_aux(event))
6123 return;
6124
6125 if (!parent)
6126 parent = event;
6127
6128 /*
6129 * In case of inheritance, it will be the parent that links to the
6130 * ring-buffer, but it will be the child that's actually using it:
6131 */
6132 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006133 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006134}
6135
6136static int __perf_pmu_output_stop(void *info)
6137{
6138 struct perf_event *event = info;
6139 struct pmu *pmu = event->pmu;
6140 struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
6141 struct remote_output ro = {
6142 .rb = event->rb,
6143 };
6144
6145 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006146 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006147 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006148 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006149 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006150 rcu_read_unlock();
6151
6152 return ro.err;
6153}
6154
6155static void perf_pmu_output_stop(struct perf_event *event)
6156{
6157 struct perf_event *iter;
6158 int err, cpu;
6159
6160restart:
6161 rcu_read_lock();
6162 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6163 /*
6164 * For per-CPU events, we need to make sure that neither they
6165 * nor their children are running; for cpu==-1 events it's
6166 * sufficient to stop the event itself if it's active, since
6167 * it can't have children.
6168 */
6169 cpu = iter->cpu;
6170 if (cpu == -1)
6171 cpu = READ_ONCE(iter->oncpu);
6172
6173 if (cpu == -1)
6174 continue;
6175
6176 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6177 if (err == -EAGAIN) {
6178 rcu_read_unlock();
6179 goto restart;
6180 }
6181 }
6182 rcu_read_unlock();
6183}
6184
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006185/*
6186 * task tracking -- fork/exit
6187 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006188 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006189 */
6190
6191struct perf_task_event {
6192 struct task_struct *task;
6193 struct perf_event_context *task_ctx;
6194
6195 struct {
6196 struct perf_event_header header;
6197
6198 u32 pid;
6199 u32 ppid;
6200 u32 tid;
6201 u32 ptid;
6202 u64 time;
6203 } event_id;
6204};
6205
Jiri Olsa67516842013-07-09 18:56:31 +02006206static int perf_event_task_match(struct perf_event *event)
6207{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006208 return event->attr.comm || event->attr.mmap ||
6209 event->attr.mmap2 || event->attr.mmap_data ||
6210 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006211}
6212
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006213static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006214 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006215{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006216 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006217 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006218 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006219 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006220 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006221
Jiri Olsa67516842013-07-09 18:56:31 +02006222 if (!perf_event_task_match(event))
6223 return;
6224
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006225 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006226
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006227 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006228 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006229 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006230 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231
6232 task_event->event_id.pid = perf_event_pid(event, task);
6233 task_event->event_id.ppid = perf_event_pid(event, current);
6234
6235 task_event->event_id.tid = perf_event_tid(event, task);
6236 task_event->event_id.ptid = perf_event_tid(event, current);
6237
Peter Zijlstra34f43922015-02-20 14:05:38 +01006238 task_event->event_id.time = perf_event_clock(event);
6239
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006240 perf_output_put(&handle, task_event->event_id);
6241
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006242 perf_event__output_id_sample(event, &handle, &sample);
6243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006244 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006245out:
6246 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006247}
6248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006249static void perf_event_task(struct task_struct *task,
6250 struct perf_event_context *task_ctx,
6251 int new)
6252{
6253 struct perf_task_event task_event;
6254
6255 if (!atomic_read(&nr_comm_events) &&
6256 !atomic_read(&nr_mmap_events) &&
6257 !atomic_read(&nr_task_events))
6258 return;
6259
6260 task_event = (struct perf_task_event){
6261 .task = task,
6262 .task_ctx = task_ctx,
6263 .event_id = {
6264 .header = {
6265 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6266 .misc = 0,
6267 .size = sizeof(task_event.event_id),
6268 },
6269 /* .pid */
6270 /* .ppid */
6271 /* .tid */
6272 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006273 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006274 },
6275 };
6276
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006277 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006278 &task_event,
6279 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006280}
6281
6282void perf_event_fork(struct task_struct *task)
6283{
6284 perf_event_task(task, NULL, 1);
6285}
6286
6287/*
6288 * comm tracking
6289 */
6290
6291struct perf_comm_event {
6292 struct task_struct *task;
6293 char *comm;
6294 int comm_size;
6295
6296 struct {
6297 struct perf_event_header header;
6298
6299 u32 pid;
6300 u32 tid;
6301 } event_id;
6302};
6303
Jiri Olsa67516842013-07-09 18:56:31 +02006304static int perf_event_comm_match(struct perf_event *event)
6305{
6306 return event->attr.comm;
6307}
6308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006309static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006310 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006311{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006312 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006313 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006314 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006315 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006316 int ret;
6317
Jiri Olsa67516842013-07-09 18:56:31 +02006318 if (!perf_event_comm_match(event))
6319 return;
6320
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006321 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6322 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006323 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006324
6325 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006326 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006327
6328 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6329 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6330
6331 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006332 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006333 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006334
6335 perf_event__output_id_sample(event, &handle, &sample);
6336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006337 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006338out:
6339 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006340}
6341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006342static void perf_event_comm_event(struct perf_comm_event *comm_event)
6343{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006344 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006345 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006346
6347 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006348 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349 size = ALIGN(strlen(comm)+1, sizeof(u64));
6350
6351 comm_event->comm = comm;
6352 comm_event->comm_size = size;
6353
6354 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006355
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006356 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006357 comm_event,
6358 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006359}
6360
Adrian Hunter82b89772014-05-28 11:45:04 +03006361void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006362{
6363 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006364
6365 if (!atomic_read(&nr_comm_events))
6366 return;
6367
6368 comm_event = (struct perf_comm_event){
6369 .task = task,
6370 /* .comm */
6371 /* .comm_size */
6372 .event_id = {
6373 .header = {
6374 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006375 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006376 /* .size */
6377 },
6378 /* .pid */
6379 /* .tid */
6380 },
6381 };
6382
6383 perf_event_comm_event(&comm_event);
6384}
6385
6386/*
6387 * mmap tracking
6388 */
6389
6390struct perf_mmap_event {
6391 struct vm_area_struct *vma;
6392
6393 const char *file_name;
6394 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006395 int maj, min;
6396 u64 ino;
6397 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006398 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006399
6400 struct {
6401 struct perf_event_header header;
6402
6403 u32 pid;
6404 u32 tid;
6405 u64 start;
6406 u64 len;
6407 u64 pgoff;
6408 } event_id;
6409};
6410
Jiri Olsa67516842013-07-09 18:56:31 +02006411static int perf_event_mmap_match(struct perf_event *event,
6412 void *data)
6413{
6414 struct perf_mmap_event *mmap_event = data;
6415 struct vm_area_struct *vma = mmap_event->vma;
6416 int executable = vma->vm_flags & VM_EXEC;
6417
6418 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006419 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006420}
6421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006423 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006424{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006425 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006426 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006427 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006428 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006429 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006430
Jiri Olsa67516842013-07-09 18:56:31 +02006431 if (!perf_event_mmap_match(event, data))
6432 return;
6433
Stephane Eranian13d7a242013-08-21 12:10:24 +02006434 if (event->attr.mmap2) {
6435 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6436 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6437 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6438 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006439 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006440 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6441 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006442 }
6443
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006444 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6445 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006446 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006448 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006449
6450 mmap_event->event_id.pid = perf_event_pid(event, current);
6451 mmap_event->event_id.tid = perf_event_tid(event, current);
6452
6453 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006454
6455 if (event->attr.mmap2) {
6456 perf_output_put(&handle, mmap_event->maj);
6457 perf_output_put(&handle, mmap_event->min);
6458 perf_output_put(&handle, mmap_event->ino);
6459 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006460 perf_output_put(&handle, mmap_event->prot);
6461 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006462 }
6463
Frederic Weisbecker76369132011-05-19 19:55:04 +02006464 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006465 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006466
6467 perf_event__output_id_sample(event, &handle, &sample);
6468
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006469 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006470out:
6471 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006472}
6473
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006474static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6475{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476 struct vm_area_struct *vma = mmap_event->vma;
6477 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006478 int maj = 0, min = 0;
6479 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006480 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006481 unsigned int size;
6482 char tmp[16];
6483 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006484 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006485
6486 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006487 struct inode *inode;
6488 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006489
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006490 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006491 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006492 name = "//enomem";
6493 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006494 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006495 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006496 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006497 * need to add enough zero bytes after the string to handle
6498 * the 64bit alignment we do later.
6499 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006500 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006501 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006502 name = "//toolong";
6503 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006505 inode = file_inode(vma->vm_file);
6506 dev = inode->i_sb->s_dev;
6507 ino = inode->i_ino;
6508 gen = inode->i_generation;
6509 maj = MAJOR(dev);
6510 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006511
6512 if (vma->vm_flags & VM_READ)
6513 prot |= PROT_READ;
6514 if (vma->vm_flags & VM_WRITE)
6515 prot |= PROT_WRITE;
6516 if (vma->vm_flags & VM_EXEC)
6517 prot |= PROT_EXEC;
6518
6519 if (vma->vm_flags & VM_MAYSHARE)
6520 flags = MAP_SHARED;
6521 else
6522 flags = MAP_PRIVATE;
6523
6524 if (vma->vm_flags & VM_DENYWRITE)
6525 flags |= MAP_DENYWRITE;
6526 if (vma->vm_flags & VM_MAYEXEC)
6527 flags |= MAP_EXECUTABLE;
6528 if (vma->vm_flags & VM_LOCKED)
6529 flags |= MAP_LOCKED;
6530 if (vma->vm_flags & VM_HUGETLB)
6531 flags |= MAP_HUGETLB;
6532
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006534 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006535 if (vma->vm_ops && vma->vm_ops->name) {
6536 name = (char *) vma->vm_ops->name(vma);
6537 if (name)
6538 goto cpy_name;
6539 }
6540
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006541 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006542 if (name)
6543 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006544
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006545 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006546 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006547 name = "[heap]";
6548 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006549 }
6550 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006551 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006552 name = "[stack]";
6553 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554 }
6555
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006556 name = "//anon";
6557 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006558 }
6559
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006560cpy_name:
6561 strlcpy(tmp, name, sizeof(tmp));
6562 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006563got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006564 /*
6565 * Since our buffer works in 8 byte units we need to align our string
6566 * size to a multiple of 8. However, we must guarantee the tail end is
6567 * zero'd out to avoid leaking random bits to userspace.
6568 */
6569 size = strlen(name)+1;
6570 while (!IS_ALIGNED(size, sizeof(u64)))
6571 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006572
6573 mmap_event->file_name = name;
6574 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006575 mmap_event->maj = maj;
6576 mmap_event->min = min;
6577 mmap_event->ino = ino;
6578 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006579 mmap_event->prot = prot;
6580 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006581
Stephane Eranian2fe85422013-01-24 16:10:39 +01006582 if (!(vma->vm_flags & VM_EXEC))
6583 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006585 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6586
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006587 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006588 mmap_event,
6589 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006590
6591 kfree(buf);
6592}
6593
Alexander Shishkin375637b2016-04-27 18:44:46 +03006594/*
6595 * Whether this @filter depends on a dynamic object which is not loaded
6596 * yet or its load addresses are not known.
6597 */
6598static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter)
6599{
6600 return filter->filter && filter->inode;
6601}
6602
6603/*
6604 * Check whether inode and address range match filter criteria.
6605 */
6606static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6607 struct file *file, unsigned long offset,
6608 unsigned long size)
6609{
6610 if (filter->inode != file->f_inode)
6611 return false;
6612
6613 if (filter->offset > offset + size)
6614 return false;
6615
6616 if (filter->offset + filter->size < offset)
6617 return false;
6618
6619 return true;
6620}
6621
6622static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6623{
6624 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6625 struct vm_area_struct *vma = data;
6626 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6627 struct file *file = vma->vm_file;
6628 struct perf_addr_filter *filter;
6629 unsigned int restart = 0, count = 0;
6630
6631 if (!has_addr_filter(event))
6632 return;
6633
6634 if (!file)
6635 return;
6636
6637 raw_spin_lock_irqsave(&ifh->lock, flags);
6638 list_for_each_entry(filter, &ifh->list, entry) {
6639 if (perf_addr_filter_match(filter, file, off,
6640 vma->vm_end - vma->vm_start)) {
6641 event->addr_filters_offs[count] = vma->vm_start;
6642 restart++;
6643 }
6644
6645 count++;
6646 }
6647
6648 if (restart)
6649 event->addr_filters_gen++;
6650 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6651
6652 if (restart)
6653 perf_event_restart(event);
6654}
6655
6656/*
6657 * Adjust all task's events' filters to the new vma
6658 */
6659static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6660{
6661 struct perf_event_context *ctx;
6662 int ctxn;
6663
6664 rcu_read_lock();
6665 for_each_task_context_nr(ctxn) {
6666 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6667 if (!ctx)
6668 continue;
6669
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006670 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006671 }
6672 rcu_read_unlock();
6673}
6674
Eric B Munson3af9e852010-05-18 15:30:49 +01006675void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006676{
6677 struct perf_mmap_event mmap_event;
6678
6679 if (!atomic_read(&nr_mmap_events))
6680 return;
6681
6682 mmap_event = (struct perf_mmap_event){
6683 .vma = vma,
6684 /* .file_name */
6685 /* .file_size */
6686 .event_id = {
6687 .header = {
6688 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006689 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690 /* .size */
6691 },
6692 /* .pid */
6693 /* .tid */
6694 .start = vma->vm_start,
6695 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006696 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006697 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006698 /* .maj (attr_mmap2 only) */
6699 /* .min (attr_mmap2 only) */
6700 /* .ino (attr_mmap2 only) */
6701 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006702 /* .prot (attr_mmap2 only) */
6703 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006704 };
6705
Alexander Shishkin375637b2016-04-27 18:44:46 +03006706 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006707 perf_event_mmap_event(&mmap_event);
6708}
6709
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006710void perf_event_aux_event(struct perf_event *event, unsigned long head,
6711 unsigned long size, u64 flags)
6712{
6713 struct perf_output_handle handle;
6714 struct perf_sample_data sample;
6715 struct perf_aux_event {
6716 struct perf_event_header header;
6717 u64 offset;
6718 u64 size;
6719 u64 flags;
6720 } rec = {
6721 .header = {
6722 .type = PERF_RECORD_AUX,
6723 .misc = 0,
6724 .size = sizeof(rec),
6725 },
6726 .offset = head,
6727 .size = size,
6728 .flags = flags,
6729 };
6730 int ret;
6731
6732 perf_event_header__init_id(&rec.header, &sample, event);
6733 ret = perf_output_begin(&handle, event, rec.header.size);
6734
6735 if (ret)
6736 return;
6737
6738 perf_output_put(&handle, rec);
6739 perf_event__output_id_sample(event, &handle, &sample);
6740
6741 perf_output_end(&handle);
6742}
6743
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006745 * Lost/dropped samples logging
6746 */
6747void perf_log_lost_samples(struct perf_event *event, u64 lost)
6748{
6749 struct perf_output_handle handle;
6750 struct perf_sample_data sample;
6751 int ret;
6752
6753 struct {
6754 struct perf_event_header header;
6755 u64 lost;
6756 } lost_samples_event = {
6757 .header = {
6758 .type = PERF_RECORD_LOST_SAMPLES,
6759 .misc = 0,
6760 .size = sizeof(lost_samples_event),
6761 },
6762 .lost = lost,
6763 };
6764
6765 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6766
6767 ret = perf_output_begin(&handle, event,
6768 lost_samples_event.header.size);
6769 if (ret)
6770 return;
6771
6772 perf_output_put(&handle, lost_samples_event);
6773 perf_event__output_id_sample(event, &handle, &sample);
6774 perf_output_end(&handle);
6775}
6776
6777/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006778 * context_switch tracking
6779 */
6780
6781struct perf_switch_event {
6782 struct task_struct *task;
6783 struct task_struct *next_prev;
6784
6785 struct {
6786 struct perf_event_header header;
6787 u32 next_prev_pid;
6788 u32 next_prev_tid;
6789 } event_id;
6790};
6791
6792static int perf_event_switch_match(struct perf_event *event)
6793{
6794 return event->attr.context_switch;
6795}
6796
6797static void perf_event_switch_output(struct perf_event *event, void *data)
6798{
6799 struct perf_switch_event *se = data;
6800 struct perf_output_handle handle;
6801 struct perf_sample_data sample;
6802 int ret;
6803
6804 if (!perf_event_switch_match(event))
6805 return;
6806
6807 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6808 if (event->ctx->task) {
6809 se->event_id.header.type = PERF_RECORD_SWITCH;
6810 se->event_id.header.size = sizeof(se->event_id.header);
6811 } else {
6812 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6813 se->event_id.header.size = sizeof(se->event_id);
6814 se->event_id.next_prev_pid =
6815 perf_event_pid(event, se->next_prev);
6816 se->event_id.next_prev_tid =
6817 perf_event_tid(event, se->next_prev);
6818 }
6819
6820 perf_event_header__init_id(&se->event_id.header, &sample, event);
6821
6822 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6823 if (ret)
6824 return;
6825
6826 if (event->ctx->task)
6827 perf_output_put(&handle, se->event_id.header);
6828 else
6829 perf_output_put(&handle, se->event_id);
6830
6831 perf_event__output_id_sample(event, &handle, &sample);
6832
6833 perf_output_end(&handle);
6834}
6835
6836static void perf_event_switch(struct task_struct *task,
6837 struct task_struct *next_prev, bool sched_in)
6838{
6839 struct perf_switch_event switch_event;
6840
6841 /* N.B. caller checks nr_switch_events != 0 */
6842
6843 switch_event = (struct perf_switch_event){
6844 .task = task,
6845 .next_prev = next_prev,
6846 .event_id = {
6847 .header = {
6848 /* .type */
6849 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6850 /* .size */
6851 },
6852 /* .next_prev_pid */
6853 /* .next_prev_tid */
6854 },
6855 };
6856
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006857 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03006858 &switch_event,
6859 NULL);
6860}
6861
6862/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006863 * IRQ throttle logging
6864 */
6865
6866static void perf_log_throttle(struct perf_event *event, int enable)
6867{
6868 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006869 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006870 int ret;
6871
6872 struct {
6873 struct perf_event_header header;
6874 u64 time;
6875 u64 id;
6876 u64 stream_id;
6877 } throttle_event = {
6878 .header = {
6879 .type = PERF_RECORD_THROTTLE,
6880 .misc = 0,
6881 .size = sizeof(throttle_event),
6882 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006883 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006884 .id = primary_event_id(event),
6885 .stream_id = event->id,
6886 };
6887
6888 if (enable)
6889 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6890
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006891 perf_event_header__init_id(&throttle_event.header, &sample, event);
6892
6893 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006894 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006895 if (ret)
6896 return;
6897
6898 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006899 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006900 perf_output_end(&handle);
6901}
6902
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006903static void perf_log_itrace_start(struct perf_event *event)
6904{
6905 struct perf_output_handle handle;
6906 struct perf_sample_data sample;
6907 struct perf_aux_event {
6908 struct perf_event_header header;
6909 u32 pid;
6910 u32 tid;
6911 } rec;
6912 int ret;
6913
6914 if (event->parent)
6915 event = event->parent;
6916
6917 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6918 event->hw.itrace_started)
6919 return;
6920
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006921 rec.header.type = PERF_RECORD_ITRACE_START;
6922 rec.header.misc = 0;
6923 rec.header.size = sizeof(rec);
6924 rec.pid = perf_event_pid(event, current);
6925 rec.tid = perf_event_tid(event, current);
6926
6927 perf_event_header__init_id(&rec.header, &sample, event);
6928 ret = perf_output_begin(&handle, event, rec.header.size);
6929
6930 if (ret)
6931 return;
6932
6933 perf_output_put(&handle, rec);
6934 perf_event__output_id_sample(event, &handle, &sample);
6935
6936 perf_output_end(&handle);
6937}
6938
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006939/*
6940 * Generic event overflow handling, sampling.
6941 */
6942
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006943static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006944 int throttle, struct perf_sample_data *data,
6945 struct pt_regs *regs)
6946{
6947 int events = atomic_read(&event->event_limit);
6948 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006949 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950 int ret = 0;
6951
Peter Zijlstra96398822010-11-24 18:55:29 +01006952 /*
6953 * Non-sampling counters might still use the PMI to fold short
6954 * hardware counters, ignore those.
6955 */
6956 if (unlikely(!is_sampling_event(event)))
6957 return 0;
6958
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006959 seq = __this_cpu_read(perf_throttled_seq);
6960 if (seq != hwc->interrupts_seq) {
6961 hwc->interrupts_seq = seq;
6962 hwc->interrupts = 1;
6963 } else {
6964 hwc->interrupts++;
6965 if (unlikely(throttle
6966 && hwc->interrupts >= max_samples_per_tick)) {
6967 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02006968 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006969 hwc->interrupts = MAX_INTERRUPTS;
6970 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006971 ret = 1;
6972 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006973 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006974
6975 if (event->attr.freq) {
6976 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006977 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006978
Peter Zijlstraabd50712010-01-26 18:50:16 +01006979 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006980
Peter Zijlstraabd50712010-01-26 18:50:16 +01006981 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006982 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983 }
6984
6985 /*
6986 * XXX event_limit might not quite work as expected on inherited
6987 * events
6988 */
6989
6990 event->pending_kill = POLL_IN;
6991 if (events && atomic_dec_and_test(&event->event_limit)) {
6992 ret = 1;
6993 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006994 event->pending_disable = 1;
6995 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006996 }
6997
Wang Nan18794452016-03-28 06:41:30 +00006998 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01006999
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007000 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007001 event->pending_wakeup = 1;
7002 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007003 }
7004
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007005 return ret;
7006}
7007
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007008int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007009 struct perf_sample_data *data,
7010 struct pt_regs *regs)
7011{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007012 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007013}
7014
7015/*
7016 * Generic software event infrastructure
7017 */
7018
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007019struct swevent_htable {
7020 struct swevent_hlist *swevent_hlist;
7021 struct mutex hlist_mutex;
7022 int hlist_refcount;
7023
7024 /* Recursion avoidance in each contexts */
7025 int recursion[PERF_NR_CONTEXTS];
7026};
7027
7028static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7029
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007030/*
7031 * We directly increment event->count and keep a second value in
7032 * event->hw.period_left to count intervals. This period event
7033 * is kept in the range [-sample_period, 0] so that we can use the
7034 * sign as trigger.
7035 */
7036
Jiri Olsaab573842013-05-01 17:25:44 +02007037u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007038{
7039 struct hw_perf_event *hwc = &event->hw;
7040 u64 period = hwc->last_period;
7041 u64 nr, offset;
7042 s64 old, val;
7043
7044 hwc->last_period = hwc->sample_period;
7045
7046again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007047 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007048 if (val < 0)
7049 return 0;
7050
7051 nr = div64_u64(period + val, period);
7052 offset = nr * period;
7053 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007054 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007055 goto again;
7056
7057 return nr;
7058}
7059
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007060static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007061 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007062 struct pt_regs *regs)
7063{
7064 struct hw_perf_event *hwc = &event->hw;
7065 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007066
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007067 if (!overflow)
7068 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007069
7070 if (hwc->interrupts == MAX_INTERRUPTS)
7071 return;
7072
7073 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007074 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075 data, regs)) {
7076 /*
7077 * We inhibit the overflow from happening when
7078 * hwc->interrupts == MAX_INTERRUPTS.
7079 */
7080 break;
7081 }
7082 throttle = 1;
7083 }
7084}
7085
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007086static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007087 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007088 struct pt_regs *regs)
7089{
7090 struct hw_perf_event *hwc = &event->hw;
7091
Peter Zijlstrae7850592010-05-21 14:43:08 +02007092 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007093
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007094 if (!regs)
7095 return;
7096
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007097 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007098 return;
7099
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007100 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7101 data->period = nr;
7102 return perf_swevent_overflow(event, 1, data, regs);
7103 } else
7104 data->period = event->hw.last_period;
7105
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007106 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007107 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007108
Peter Zijlstrae7850592010-05-21 14:43:08 +02007109 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007110 return;
7111
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007112 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007113}
7114
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007115static int perf_exclude_event(struct perf_event *event,
7116 struct pt_regs *regs)
7117{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007118 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007119 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007120
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007121 if (regs) {
7122 if (event->attr.exclude_user && user_mode(regs))
7123 return 1;
7124
7125 if (event->attr.exclude_kernel && !user_mode(regs))
7126 return 1;
7127 }
7128
7129 return 0;
7130}
7131
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007132static int perf_swevent_match(struct perf_event *event,
7133 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007134 u32 event_id,
7135 struct perf_sample_data *data,
7136 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007137{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007138 if (event->attr.type != type)
7139 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007140
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141 if (event->attr.config != event_id)
7142 return 0;
7143
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007144 if (perf_exclude_event(event, regs))
7145 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146
7147 return 1;
7148}
7149
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007150static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007151{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007152 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007153
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007154 return hash_64(val, SWEVENT_HLIST_BITS);
7155}
7156
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007157static inline struct hlist_head *
7158__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007159{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007160 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007161
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007162 return &hlist->heads[hash];
7163}
7164
7165/* For the read side: events when they trigger */
7166static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007167find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007168{
7169 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007170
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007171 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007172 if (!hlist)
7173 return NULL;
7174
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007175 return __find_swevent_head(hlist, type, event_id);
7176}
7177
7178/* For the event head insertion and removal in the hlist */
7179static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007180find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007181{
7182 struct swevent_hlist *hlist;
7183 u32 event_id = event->attr.config;
7184 u64 type = event->attr.type;
7185
7186 /*
7187 * Event scheduling is always serialized against hlist allocation
7188 * and release. Which makes the protected version suitable here.
7189 * The context lock guarantees that.
7190 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007191 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007192 lockdep_is_held(&event->ctx->lock));
7193 if (!hlist)
7194 return NULL;
7195
7196 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007197}
7198
7199static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007200 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007201 struct perf_sample_data *data,
7202 struct pt_regs *regs)
7203{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007204 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007205 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007206 struct hlist_head *head;
7207
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007208 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007209 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007210 if (!head)
7211 goto end;
7212
Sasha Levinb67bfe02013-02-27 17:06:00 -08007213 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007214 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007215 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007216 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007217end:
7218 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219}
7220
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007221DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7222
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007223int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007224{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007225 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007226
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007227 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007228}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007229EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007230
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007231void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007232{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007233 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007234
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007235 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007236}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007237
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007238void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007239{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007240 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007241
7242 if (WARN_ON_ONCE(!regs))
7243 return;
7244
7245 perf_sample_data_init(&data, addr, 0);
7246 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7247}
7248
7249void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7250{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007251 int rctx;
7252
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007253 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007254 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007255 if (unlikely(rctx < 0))
7256 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007257
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007258 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007259
7260 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007261fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007262 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007263}
7264
7265static void perf_swevent_read(struct perf_event *event)
7266{
7267}
7268
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007269static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007270{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007271 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007272 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007273 struct hlist_head *head;
7274
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007275 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007276 hwc->last_period = hwc->sample_period;
7277 perf_swevent_set_period(event);
7278 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007279
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007280 hwc->state = !(flags & PERF_EF_START);
7281
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007282 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007283 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007284 return -EINVAL;
7285
7286 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007287 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007288
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007289 return 0;
7290}
7291
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007292static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007293{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007294 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007295}
7296
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007297static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007298{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007299 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007300}
7301
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007302static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007303{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007304 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007305}
7306
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007307/* Deref the hlist from the update side */
7308static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007309swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007310{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007311 return rcu_dereference_protected(swhash->swevent_hlist,
7312 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007313}
7314
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007315static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007316{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007317 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007318
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007319 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007320 return;
7321
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007322 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007323 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007324}
7325
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007326static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007327{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007328 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007329
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007330 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007331
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007332 if (!--swhash->hlist_refcount)
7333 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007334
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007335 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007336}
7337
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007338static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007339{
7340 int cpu;
7341
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007342 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007343 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007344}
7345
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007346static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007347{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007348 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007349 int err = 0;
7350
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007351 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007352 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007353 struct swevent_hlist *hlist;
7354
7355 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7356 if (!hlist) {
7357 err = -ENOMEM;
7358 goto exit;
7359 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007360 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007361 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007362 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007363exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007364 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007365
7366 return err;
7367}
7368
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007369static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007370{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007371 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007372
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007373 get_online_cpus();
7374 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007375 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007376 if (err) {
7377 failed_cpu = cpu;
7378 goto fail;
7379 }
7380 }
7381 put_online_cpus();
7382
7383 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007384fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007385 for_each_possible_cpu(cpu) {
7386 if (cpu == failed_cpu)
7387 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007388 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007389 }
7390
7391 put_online_cpus();
7392 return err;
7393}
7394
Ingo Molnarc5905af2012-02-24 08:31:31 +01007395struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007396
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007397static void sw_perf_event_destroy(struct perf_event *event)
7398{
7399 u64 event_id = event->attr.config;
7400
7401 WARN_ON(event->parent);
7402
Ingo Molnarc5905af2012-02-24 08:31:31 +01007403 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007404 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007405}
7406
7407static int perf_swevent_init(struct perf_event *event)
7408{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007409 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007410
7411 if (event->attr.type != PERF_TYPE_SOFTWARE)
7412 return -ENOENT;
7413
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007414 /*
7415 * no branch sampling for software events
7416 */
7417 if (has_branch_stack(event))
7418 return -EOPNOTSUPP;
7419
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007420 switch (event_id) {
7421 case PERF_COUNT_SW_CPU_CLOCK:
7422 case PERF_COUNT_SW_TASK_CLOCK:
7423 return -ENOENT;
7424
7425 default:
7426 break;
7427 }
7428
Dan Carpenterce677832010-10-24 21:50:42 +02007429 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007430 return -ENOENT;
7431
7432 if (!event->parent) {
7433 int err;
7434
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007435 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007436 if (err)
7437 return err;
7438
Ingo Molnarc5905af2012-02-24 08:31:31 +01007439 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007440 event->destroy = sw_perf_event_destroy;
7441 }
7442
7443 return 0;
7444}
7445
7446static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007447 .task_ctx_nr = perf_sw_context,
7448
Peter Zijlstra34f43922015-02-20 14:05:38 +01007449 .capabilities = PERF_PMU_CAP_NO_NMI,
7450
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007451 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007452 .add = perf_swevent_add,
7453 .del = perf_swevent_del,
7454 .start = perf_swevent_start,
7455 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007456 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007457};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007458
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007459#ifdef CONFIG_EVENT_TRACING
7460
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007461static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007462 struct perf_sample_data *data)
7463{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007464 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007465
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007466 /* only top level events have filters set */
7467 if (event->parent)
7468 event = event->parent;
7469
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007470 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7471 return 1;
7472 return 0;
7473}
7474
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007475static int perf_tp_event_match(struct perf_event *event,
7476 struct perf_sample_data *data,
7477 struct pt_regs *regs)
7478{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007479 if (event->hw.state & PERF_HES_STOPPED)
7480 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007481 /*
7482 * All tracepoints are from kernel-space.
7483 */
7484 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007485 return 0;
7486
7487 if (!perf_tp_filter_match(event, data))
7488 return 0;
7489
7490 return 1;
7491}
7492
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007493void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7494 struct trace_event_call *call, u64 count,
7495 struct pt_regs *regs, struct hlist_head *head,
7496 struct task_struct *task)
7497{
7498 struct bpf_prog *prog = call->prog;
7499
7500 if (prog) {
7501 *(struct pt_regs **)raw_data = regs;
7502 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7503 perf_swevent_put_recursion_context(rctx);
7504 return;
7505 }
7506 }
7507 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7508 rctx, task);
7509}
7510EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7511
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007512void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007513 struct pt_regs *regs, struct hlist_head *head, int rctx,
7514 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007515{
7516 struct perf_sample_data data;
7517 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007518
7519 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007520 .frag = {
7521 .size = entry_size,
7522 .data = record,
7523 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007524 };
7525
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007526 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007527 data.raw = &raw;
7528
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007529 perf_trace_buf_update(record, event_type);
7530
Sasha Levinb67bfe02013-02-27 17:06:00 -08007531 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007532 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007533 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007534 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007535
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007536 /*
7537 * If we got specified a target task, also iterate its context and
7538 * deliver this event there too.
7539 */
7540 if (task && task != current) {
7541 struct perf_event_context *ctx;
7542 struct trace_entry *entry = record;
7543
7544 rcu_read_lock();
7545 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7546 if (!ctx)
7547 goto unlock;
7548
7549 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7550 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7551 continue;
7552 if (event->attr.config != entry->type)
7553 continue;
7554 if (perf_tp_event_match(event, &data, regs))
7555 perf_swevent_event(event, count, &data, regs);
7556 }
7557unlock:
7558 rcu_read_unlock();
7559 }
7560
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007561 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007562}
7563EXPORT_SYMBOL_GPL(perf_tp_event);
7564
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007565static void tp_perf_event_destroy(struct perf_event *event)
7566{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007567 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007568}
7569
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007570static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007571{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007572 int err;
7573
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007574 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7575 return -ENOENT;
7576
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007577 /*
7578 * no branch sampling for tracepoint events
7579 */
7580 if (has_branch_stack(event))
7581 return -EOPNOTSUPP;
7582
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007583 err = perf_trace_init(event);
7584 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007585 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007586
7587 event->destroy = tp_perf_event_destroy;
7588
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007589 return 0;
7590}
7591
7592static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007593 .task_ctx_nr = perf_sw_context,
7594
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007595 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007596 .add = perf_trace_add,
7597 .del = perf_trace_del,
7598 .start = perf_swevent_start,
7599 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007600 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007601};
7602
7603static inline void perf_tp_register(void)
7604{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007605 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007606}
Li Zefan6fb29152009-10-15 11:21:42 +08007607
Li Zefan6fb29152009-10-15 11:21:42 +08007608static void perf_event_free_filter(struct perf_event *event)
7609{
7610 ftrace_profile_free_filter(event);
7611}
7612
Alexei Starovoitov25415172015-03-25 12:49:20 -07007613static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7614{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007615 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007616 struct bpf_prog *prog;
7617
7618 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7619 return -EINVAL;
7620
7621 if (event->tp_event->prog)
7622 return -EEXIST;
7623
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007624 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7625 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7626 if (!is_kprobe && !is_tracepoint)
7627 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007628 return -EINVAL;
7629
7630 prog = bpf_prog_get(prog_fd);
7631 if (IS_ERR(prog))
7632 return PTR_ERR(prog);
7633
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007634 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7635 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007636 /* valid fd, but invalid bpf program type */
7637 bpf_prog_put(prog);
7638 return -EINVAL;
7639 }
7640
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007641 if (is_tracepoint) {
7642 int off = trace_event_get_offsets(event->tp_event);
7643
7644 if (prog->aux->max_ctx_offset > off) {
7645 bpf_prog_put(prog);
7646 return -EACCES;
7647 }
7648 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007649 event->tp_event->prog = prog;
7650
7651 return 0;
7652}
7653
7654static void perf_event_free_bpf_prog(struct perf_event *event)
7655{
7656 struct bpf_prog *prog;
7657
7658 if (!event->tp_event)
7659 return;
7660
7661 prog = event->tp_event->prog;
7662 if (prog) {
7663 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007664 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007665 }
7666}
7667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007668#else
Li Zefan6fb29152009-10-15 11:21:42 +08007669
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007670static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007671{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007672}
Li Zefan6fb29152009-10-15 11:21:42 +08007673
Li Zefan6fb29152009-10-15 11:21:42 +08007674static void perf_event_free_filter(struct perf_event *event)
7675{
7676}
7677
Alexei Starovoitov25415172015-03-25 12:49:20 -07007678static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7679{
7680 return -ENOENT;
7681}
7682
7683static void perf_event_free_bpf_prog(struct perf_event *event)
7684{
7685}
Li Zefan07b139c2009-12-21 14:27:35 +08007686#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007687
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007688#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007689void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007690{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007691 struct perf_sample_data sample;
7692 struct pt_regs *regs = data;
7693
Robert Richterfd0d0002012-04-02 20:19:08 +02007694 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007695
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007696 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007697 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007698}
7699#endif
7700
Alexander Shishkin375637b2016-04-27 18:44:46 +03007701/*
7702 * Allocate a new address filter
7703 */
7704static struct perf_addr_filter *
7705perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7706{
7707 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7708 struct perf_addr_filter *filter;
7709
7710 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7711 if (!filter)
7712 return NULL;
7713
7714 INIT_LIST_HEAD(&filter->entry);
7715 list_add_tail(&filter->entry, filters);
7716
7717 return filter;
7718}
7719
7720static void free_filters_list(struct list_head *filters)
7721{
7722 struct perf_addr_filter *filter, *iter;
7723
7724 list_for_each_entry_safe(filter, iter, filters, entry) {
7725 if (filter->inode)
7726 iput(filter->inode);
7727 list_del(&filter->entry);
7728 kfree(filter);
7729 }
7730}
7731
7732/*
7733 * Free existing address filters and optionally install new ones
7734 */
7735static void perf_addr_filters_splice(struct perf_event *event,
7736 struct list_head *head)
7737{
7738 unsigned long flags;
7739 LIST_HEAD(list);
7740
7741 if (!has_addr_filter(event))
7742 return;
7743
7744 /* don't bother with children, they don't have their own filters */
7745 if (event->parent)
7746 return;
7747
7748 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
7749
7750 list_splice_init(&event->addr_filters.list, &list);
7751 if (head)
7752 list_splice(head, &event->addr_filters.list);
7753
7754 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
7755
7756 free_filters_list(&list);
7757}
7758
7759/*
7760 * Scan through mm's vmas and see if one of them matches the
7761 * @filter; if so, adjust filter's address range.
7762 * Called with mm::mmap_sem down for reading.
7763 */
7764static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
7765 struct mm_struct *mm)
7766{
7767 struct vm_area_struct *vma;
7768
7769 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7770 struct file *file = vma->vm_file;
7771 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7772 unsigned long vma_size = vma->vm_end - vma->vm_start;
7773
7774 if (!file)
7775 continue;
7776
7777 if (!perf_addr_filter_match(filter, file, off, vma_size))
7778 continue;
7779
7780 return vma->vm_start;
7781 }
7782
7783 return 0;
7784}
7785
7786/*
7787 * Update event's address range filters based on the
7788 * task's existing mappings, if any.
7789 */
7790static void perf_event_addr_filters_apply(struct perf_event *event)
7791{
7792 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7793 struct task_struct *task = READ_ONCE(event->ctx->task);
7794 struct perf_addr_filter *filter;
7795 struct mm_struct *mm = NULL;
7796 unsigned int count = 0;
7797 unsigned long flags;
7798
7799 /*
7800 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7801 * will stop on the parent's child_mutex that our caller is also holding
7802 */
7803 if (task == TASK_TOMBSTONE)
7804 return;
7805
7806 mm = get_task_mm(event->ctx->task);
7807 if (!mm)
7808 goto restart;
7809
7810 down_read(&mm->mmap_sem);
7811
7812 raw_spin_lock_irqsave(&ifh->lock, flags);
7813 list_for_each_entry(filter, &ifh->list, entry) {
7814 event->addr_filters_offs[count] = 0;
7815
7816 if (perf_addr_filter_needs_mmap(filter))
7817 event->addr_filters_offs[count] =
7818 perf_addr_filter_apply(filter, mm);
7819
7820 count++;
7821 }
7822
7823 event->addr_filters_gen++;
7824 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7825
7826 up_read(&mm->mmap_sem);
7827
7828 mmput(mm);
7829
7830restart:
7831 perf_event_restart(event);
7832}
7833
7834/*
7835 * Address range filtering: limiting the data to certain
7836 * instruction address ranges. Filters are ioctl()ed to us from
7837 * userspace as ascii strings.
7838 *
7839 * Filter string format:
7840 *
7841 * ACTION RANGE_SPEC
7842 * where ACTION is one of the
7843 * * "filter": limit the trace to this region
7844 * * "start": start tracing from this address
7845 * * "stop": stop tracing at this address/region;
7846 * RANGE_SPEC is
7847 * * for kernel addresses: <start address>[/<size>]
7848 * * for object files: <start address>[/<size>]@</path/to/object/file>
7849 *
7850 * if <size> is not specified, the range is treated as a single address.
7851 */
7852enum {
7853 IF_ACT_FILTER,
7854 IF_ACT_START,
7855 IF_ACT_STOP,
7856 IF_SRC_FILE,
7857 IF_SRC_KERNEL,
7858 IF_SRC_FILEADDR,
7859 IF_SRC_KERNELADDR,
7860};
7861
7862enum {
7863 IF_STATE_ACTION = 0,
7864 IF_STATE_SOURCE,
7865 IF_STATE_END,
7866};
7867
7868static const match_table_t if_tokens = {
7869 { IF_ACT_FILTER, "filter" },
7870 { IF_ACT_START, "start" },
7871 { IF_ACT_STOP, "stop" },
7872 { IF_SRC_FILE, "%u/%u@%s" },
7873 { IF_SRC_KERNEL, "%u/%u" },
7874 { IF_SRC_FILEADDR, "%u@%s" },
7875 { IF_SRC_KERNELADDR, "%u" },
7876};
7877
7878/*
7879 * Address filter string parser
7880 */
7881static int
7882perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
7883 struct list_head *filters)
7884{
7885 struct perf_addr_filter *filter = NULL;
7886 char *start, *orig, *filename = NULL;
7887 struct path path;
7888 substring_t args[MAX_OPT_ARGS];
7889 int state = IF_STATE_ACTION, token;
7890 unsigned int kernel = 0;
7891 int ret = -EINVAL;
7892
7893 orig = fstr = kstrdup(fstr, GFP_KERNEL);
7894 if (!fstr)
7895 return -ENOMEM;
7896
7897 while ((start = strsep(&fstr, " ,\n")) != NULL) {
7898 ret = -EINVAL;
7899
7900 if (!*start)
7901 continue;
7902
7903 /* filter definition begins */
7904 if (state == IF_STATE_ACTION) {
7905 filter = perf_addr_filter_new(event, filters);
7906 if (!filter)
7907 goto fail;
7908 }
7909
7910 token = match_token(start, if_tokens, args);
7911 switch (token) {
7912 case IF_ACT_FILTER:
7913 case IF_ACT_START:
7914 filter->filter = 1;
7915
7916 case IF_ACT_STOP:
7917 if (state != IF_STATE_ACTION)
7918 goto fail;
7919
7920 state = IF_STATE_SOURCE;
7921 break;
7922
7923 case IF_SRC_KERNELADDR:
7924 case IF_SRC_KERNEL:
7925 kernel = 1;
7926
7927 case IF_SRC_FILEADDR:
7928 case IF_SRC_FILE:
7929 if (state != IF_STATE_SOURCE)
7930 goto fail;
7931
7932 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
7933 filter->range = 1;
7934
7935 *args[0].to = 0;
7936 ret = kstrtoul(args[0].from, 0, &filter->offset);
7937 if (ret)
7938 goto fail;
7939
7940 if (filter->range) {
7941 *args[1].to = 0;
7942 ret = kstrtoul(args[1].from, 0, &filter->size);
7943 if (ret)
7944 goto fail;
7945 }
7946
7947 if (token == IF_SRC_FILE) {
7948 filename = match_strdup(&args[2]);
7949 if (!filename) {
7950 ret = -ENOMEM;
7951 goto fail;
7952 }
7953 }
7954
7955 state = IF_STATE_END;
7956 break;
7957
7958 default:
7959 goto fail;
7960 }
7961
7962 /*
7963 * Filter definition is fully parsed, validate and install it.
7964 * Make sure that it doesn't contradict itself or the event's
7965 * attribute.
7966 */
7967 if (state == IF_STATE_END) {
7968 if (kernel && event->attr.exclude_kernel)
7969 goto fail;
7970
7971 if (!kernel) {
7972 if (!filename)
7973 goto fail;
7974
7975 /* look up the path and grab its inode */
7976 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
7977 if (ret)
7978 goto fail_free_name;
7979
7980 filter->inode = igrab(d_inode(path.dentry));
7981 path_put(&path);
7982 kfree(filename);
7983 filename = NULL;
7984
7985 ret = -EINVAL;
7986 if (!filter->inode ||
7987 !S_ISREG(filter->inode->i_mode))
7988 /* free_filters_list() will iput() */
7989 goto fail;
7990 }
7991
7992 /* ready to consume more filters */
7993 state = IF_STATE_ACTION;
7994 filter = NULL;
7995 }
7996 }
7997
7998 if (state != IF_STATE_ACTION)
7999 goto fail;
8000
8001 kfree(orig);
8002
8003 return 0;
8004
8005fail_free_name:
8006 kfree(filename);
8007fail:
8008 free_filters_list(filters);
8009 kfree(orig);
8010
8011 return ret;
8012}
8013
8014static int
8015perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8016{
8017 LIST_HEAD(filters);
8018 int ret;
8019
8020 /*
8021 * Since this is called in perf_ioctl() path, we're already holding
8022 * ctx::mutex.
8023 */
8024 lockdep_assert_held(&event->ctx->mutex);
8025
8026 if (WARN_ON_ONCE(event->parent))
8027 return -EINVAL;
8028
8029 /*
8030 * For now, we only support filtering in per-task events; doing so
8031 * for CPU-wide events requires additional context switching trickery,
8032 * since same object code will be mapped at different virtual
8033 * addresses in different processes.
8034 */
8035 if (!event->ctx->task)
8036 return -EOPNOTSUPP;
8037
8038 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8039 if (ret)
8040 return ret;
8041
8042 ret = event->pmu->addr_filters_validate(&filters);
8043 if (ret) {
8044 free_filters_list(&filters);
8045 return ret;
8046 }
8047
8048 /* remove existing filters, if any */
8049 perf_addr_filters_splice(event, &filters);
8050
8051 /* install new filters */
8052 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8053
8054 return ret;
8055}
8056
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008057static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8058{
8059 char *filter_str;
8060 int ret = -EINVAL;
8061
Alexander Shishkin375637b2016-04-27 18:44:46 +03008062 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8063 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8064 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008065 return -EINVAL;
8066
8067 filter_str = strndup_user(arg, PAGE_SIZE);
8068 if (IS_ERR(filter_str))
8069 return PTR_ERR(filter_str);
8070
8071 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8072 event->attr.type == PERF_TYPE_TRACEPOINT)
8073 ret = ftrace_profile_set_filter(event, event->attr.config,
8074 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008075 else if (has_addr_filter(event))
8076 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008077
8078 kfree(filter_str);
8079 return ret;
8080}
8081
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008082/*
8083 * hrtimer based swevent callback
8084 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008085
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008086static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008087{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008088 enum hrtimer_restart ret = HRTIMER_RESTART;
8089 struct perf_sample_data data;
8090 struct pt_regs *regs;
8091 struct perf_event *event;
8092 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008093
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008094 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008095
8096 if (event->state != PERF_EVENT_STATE_ACTIVE)
8097 return HRTIMER_NORESTART;
8098
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008099 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008100
Robert Richterfd0d0002012-04-02 20:19:08 +02008101 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008102 regs = get_irq_regs();
8103
8104 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008105 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008106 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008107 ret = HRTIMER_NORESTART;
8108 }
8109
8110 period = max_t(u64, 10000, event->hw.sample_period);
8111 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8112
8113 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008114}
8115
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008116static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008117{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008118 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008119 s64 period;
8120
8121 if (!is_sampling_event(event))
8122 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008123
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008124 period = local64_read(&hwc->period_left);
8125 if (period) {
8126 if (period < 0)
8127 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008128
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008129 local64_set(&hwc->period_left, 0);
8130 } else {
8131 period = max_t(u64, 10000, hwc->sample_period);
8132 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008133 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8134 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008135}
8136
8137static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8138{
8139 struct hw_perf_event *hwc = &event->hw;
8140
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008141 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008142 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008143 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008144
8145 hrtimer_cancel(&hwc->hrtimer);
8146 }
8147}
8148
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008149static void perf_swevent_init_hrtimer(struct perf_event *event)
8150{
8151 struct hw_perf_event *hwc = &event->hw;
8152
8153 if (!is_sampling_event(event))
8154 return;
8155
8156 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8157 hwc->hrtimer.function = perf_swevent_hrtimer;
8158
8159 /*
8160 * Since hrtimers have a fixed rate, we can do a static freq->period
8161 * mapping and avoid the whole period adjust feedback stuff.
8162 */
8163 if (event->attr.freq) {
8164 long freq = event->attr.sample_freq;
8165
8166 event->attr.sample_period = NSEC_PER_SEC / freq;
8167 hwc->sample_period = event->attr.sample_period;
8168 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008169 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008170 event->attr.freq = 0;
8171 }
8172}
8173
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008174/*
8175 * Software event: cpu wall time clock
8176 */
8177
8178static void cpu_clock_event_update(struct perf_event *event)
8179{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008180 s64 prev;
8181 u64 now;
8182
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008183 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008184 prev = local64_xchg(&event->hw.prev_count, now);
8185 local64_add(now - prev, &event->count);
8186}
8187
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008188static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008189{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008190 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008191 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008192}
8193
8194static void cpu_clock_event_stop(struct perf_event *event, int flags)
8195{
8196 perf_swevent_cancel_hrtimer(event);
8197 cpu_clock_event_update(event);
8198}
8199
8200static int cpu_clock_event_add(struct perf_event *event, int flags)
8201{
8202 if (flags & PERF_EF_START)
8203 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008204 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008205
8206 return 0;
8207}
8208
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008209static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008210{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008211 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008212}
8213
8214static void cpu_clock_event_read(struct perf_event *event)
8215{
8216 cpu_clock_event_update(event);
8217}
8218
8219static int cpu_clock_event_init(struct perf_event *event)
8220{
8221 if (event->attr.type != PERF_TYPE_SOFTWARE)
8222 return -ENOENT;
8223
8224 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8225 return -ENOENT;
8226
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008227 /*
8228 * no branch sampling for software events
8229 */
8230 if (has_branch_stack(event))
8231 return -EOPNOTSUPP;
8232
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008233 perf_swevent_init_hrtimer(event);
8234
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008235 return 0;
8236}
8237
8238static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008239 .task_ctx_nr = perf_sw_context,
8240
Peter Zijlstra34f43922015-02-20 14:05:38 +01008241 .capabilities = PERF_PMU_CAP_NO_NMI,
8242
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008243 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008244 .add = cpu_clock_event_add,
8245 .del = cpu_clock_event_del,
8246 .start = cpu_clock_event_start,
8247 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008248 .read = cpu_clock_event_read,
8249};
8250
8251/*
8252 * Software event: task time clock
8253 */
8254
8255static void task_clock_event_update(struct perf_event *event, u64 now)
8256{
8257 u64 prev;
8258 s64 delta;
8259
8260 prev = local64_xchg(&event->hw.prev_count, now);
8261 delta = now - prev;
8262 local64_add(delta, &event->count);
8263}
8264
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008265static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008266{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008267 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008268 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008269}
8270
8271static void task_clock_event_stop(struct perf_event *event, int flags)
8272{
8273 perf_swevent_cancel_hrtimer(event);
8274 task_clock_event_update(event, event->ctx->time);
8275}
8276
8277static int task_clock_event_add(struct perf_event *event, int flags)
8278{
8279 if (flags & PERF_EF_START)
8280 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008281 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008282
8283 return 0;
8284}
8285
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008286static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008287{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008288 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008289}
8290
8291static void task_clock_event_read(struct perf_event *event)
8292{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008293 u64 now = perf_clock();
8294 u64 delta = now - event->ctx->timestamp;
8295 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008296
8297 task_clock_event_update(event, time);
8298}
8299
8300static int task_clock_event_init(struct perf_event *event)
8301{
8302 if (event->attr.type != PERF_TYPE_SOFTWARE)
8303 return -ENOENT;
8304
8305 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8306 return -ENOENT;
8307
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008308 /*
8309 * no branch sampling for software events
8310 */
8311 if (has_branch_stack(event))
8312 return -EOPNOTSUPP;
8313
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008314 perf_swevent_init_hrtimer(event);
8315
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008316 return 0;
8317}
8318
8319static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008320 .task_ctx_nr = perf_sw_context,
8321
Peter Zijlstra34f43922015-02-20 14:05:38 +01008322 .capabilities = PERF_PMU_CAP_NO_NMI,
8323
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008324 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008325 .add = task_clock_event_add,
8326 .del = task_clock_event_del,
8327 .start = task_clock_event_start,
8328 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008329 .read = task_clock_event_read,
8330};
8331
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008332static void perf_pmu_nop_void(struct pmu *pmu)
8333{
8334}
8335
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008336static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8337{
8338}
8339
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008340static int perf_pmu_nop_int(struct pmu *pmu)
8341{
8342 return 0;
8343}
8344
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008345static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008346
8347static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008348{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008349 __this_cpu_write(nop_txn_flags, flags);
8350
8351 if (flags & ~PERF_PMU_TXN_ADD)
8352 return;
8353
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008354 perf_pmu_disable(pmu);
8355}
8356
8357static int perf_pmu_commit_txn(struct pmu *pmu)
8358{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008359 unsigned int flags = __this_cpu_read(nop_txn_flags);
8360
8361 __this_cpu_write(nop_txn_flags, 0);
8362
8363 if (flags & ~PERF_PMU_TXN_ADD)
8364 return 0;
8365
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008366 perf_pmu_enable(pmu);
8367 return 0;
8368}
8369
8370static void perf_pmu_cancel_txn(struct pmu *pmu)
8371{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008372 unsigned int flags = __this_cpu_read(nop_txn_flags);
8373
8374 __this_cpu_write(nop_txn_flags, 0);
8375
8376 if (flags & ~PERF_PMU_TXN_ADD)
8377 return;
8378
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008379 perf_pmu_enable(pmu);
8380}
8381
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008382static int perf_event_idx_default(struct perf_event *event)
8383{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008384 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008385}
8386
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008387/*
8388 * Ensures all contexts with the same task_ctx_nr have the same
8389 * pmu_cpu_context too.
8390 */
Mark Rutland9e317042014-02-10 17:44:18 +00008391static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008392{
8393 struct pmu *pmu;
8394
8395 if (ctxn < 0)
8396 return NULL;
8397
8398 list_for_each_entry(pmu, &pmus, entry) {
8399 if (pmu->task_ctx_nr == ctxn)
8400 return pmu->pmu_cpu_context;
8401 }
8402
8403 return NULL;
8404}
8405
Peter Zijlstra51676952010-12-07 14:18:20 +01008406static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008407{
Peter Zijlstra51676952010-12-07 14:18:20 +01008408 int cpu;
8409
8410 for_each_possible_cpu(cpu) {
8411 struct perf_cpu_context *cpuctx;
8412
8413 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8414
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008415 if (cpuctx->unique_pmu == old_pmu)
8416 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008417 }
8418}
8419
8420static void free_pmu_context(struct pmu *pmu)
8421{
8422 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008423
8424 mutex_lock(&pmus_lock);
8425 /*
8426 * Like a real lame refcount.
8427 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008428 list_for_each_entry(i, &pmus, entry) {
8429 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8430 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008431 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008432 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008433 }
8434
Peter Zijlstra51676952010-12-07 14:18:20 +01008435 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008436out:
8437 mutex_unlock(&pmus_lock);
8438}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008439
8440/*
8441 * Let userspace know that this PMU supports address range filtering:
8442 */
8443static ssize_t nr_addr_filters_show(struct device *dev,
8444 struct device_attribute *attr,
8445 char *page)
8446{
8447 struct pmu *pmu = dev_get_drvdata(dev);
8448
8449 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8450}
8451DEVICE_ATTR_RO(nr_addr_filters);
8452
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008453static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008454
Peter Zijlstraabe43402010-11-17 23:17:37 +01008455static ssize_t
8456type_show(struct device *dev, struct device_attribute *attr, char *page)
8457{
8458 struct pmu *pmu = dev_get_drvdata(dev);
8459
8460 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8461}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008462static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008463
Stephane Eranian62b85632013-04-03 14:21:34 +02008464static ssize_t
8465perf_event_mux_interval_ms_show(struct device *dev,
8466 struct device_attribute *attr,
8467 char *page)
8468{
8469 struct pmu *pmu = dev_get_drvdata(dev);
8470
8471 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8472}
8473
Peter Zijlstra272325c2015-04-15 11:41:58 +02008474static DEFINE_MUTEX(mux_interval_mutex);
8475
Stephane Eranian62b85632013-04-03 14:21:34 +02008476static ssize_t
8477perf_event_mux_interval_ms_store(struct device *dev,
8478 struct device_attribute *attr,
8479 const char *buf, size_t count)
8480{
8481 struct pmu *pmu = dev_get_drvdata(dev);
8482 int timer, cpu, ret;
8483
8484 ret = kstrtoint(buf, 0, &timer);
8485 if (ret)
8486 return ret;
8487
8488 if (timer < 1)
8489 return -EINVAL;
8490
8491 /* same value, noting to do */
8492 if (timer == pmu->hrtimer_interval_ms)
8493 return count;
8494
Peter Zijlstra272325c2015-04-15 11:41:58 +02008495 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008496 pmu->hrtimer_interval_ms = timer;
8497
8498 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008499 get_online_cpus();
8500 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008501 struct perf_cpu_context *cpuctx;
8502 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8503 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8504
Peter Zijlstra272325c2015-04-15 11:41:58 +02008505 cpu_function_call(cpu,
8506 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008507 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008508 put_online_cpus();
8509 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008510
8511 return count;
8512}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008513static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008514
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008515static struct attribute *pmu_dev_attrs[] = {
8516 &dev_attr_type.attr,
8517 &dev_attr_perf_event_mux_interval_ms.attr,
8518 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008519};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008520ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008521
8522static int pmu_bus_running;
8523static struct bus_type pmu_bus = {
8524 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008525 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008526};
8527
8528static void pmu_dev_release(struct device *dev)
8529{
8530 kfree(dev);
8531}
8532
8533static int pmu_dev_alloc(struct pmu *pmu)
8534{
8535 int ret = -ENOMEM;
8536
8537 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8538 if (!pmu->dev)
8539 goto out;
8540
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008541 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008542 device_initialize(pmu->dev);
8543 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8544 if (ret)
8545 goto free_dev;
8546
8547 dev_set_drvdata(pmu->dev, pmu);
8548 pmu->dev->bus = &pmu_bus;
8549 pmu->dev->release = pmu_dev_release;
8550 ret = device_add(pmu->dev);
8551 if (ret)
8552 goto free_dev;
8553
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008554 /* For PMUs with address filters, throw in an extra attribute: */
8555 if (pmu->nr_addr_filters)
8556 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8557
8558 if (ret)
8559 goto del_dev;
8560
Peter Zijlstraabe43402010-11-17 23:17:37 +01008561out:
8562 return ret;
8563
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008564del_dev:
8565 device_del(pmu->dev);
8566
Peter Zijlstraabe43402010-11-17 23:17:37 +01008567free_dev:
8568 put_device(pmu->dev);
8569 goto out;
8570}
8571
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008572static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008573static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008574
Mischa Jonker03d8e802013-06-04 11:45:48 +02008575int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008576{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008577 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008578
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008579 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008580 ret = -ENOMEM;
8581 pmu->pmu_disable_count = alloc_percpu(int);
8582 if (!pmu->pmu_disable_count)
8583 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008584
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008585 pmu->type = -1;
8586 if (!name)
8587 goto skip_type;
8588 pmu->name = name;
8589
8590 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008591 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8592 if (type < 0) {
8593 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008594 goto free_pdc;
8595 }
8596 }
8597 pmu->type = type;
8598
Peter Zijlstraabe43402010-11-17 23:17:37 +01008599 if (pmu_bus_running) {
8600 ret = pmu_dev_alloc(pmu);
8601 if (ret)
8602 goto free_idr;
8603 }
8604
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008605skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008606 if (pmu->task_ctx_nr == perf_hw_context) {
8607 static int hw_context_taken = 0;
8608
Mark Rutland5101ef22016-04-26 11:33:46 +01008609 /*
8610 * Other than systems with heterogeneous CPUs, it never makes
8611 * sense for two PMUs to share perf_hw_context. PMUs which are
8612 * uncore must use perf_invalid_context.
8613 */
8614 if (WARN_ON_ONCE(hw_context_taken &&
8615 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008616 pmu->task_ctx_nr = perf_invalid_context;
8617
8618 hw_context_taken = 1;
8619 }
8620
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008621 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8622 if (pmu->pmu_cpu_context)
8623 goto got_cpu_context;
8624
Wei Yongjunc4814202013-04-12 11:05:54 +08008625 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008626 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8627 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008628 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008629
8630 for_each_possible_cpu(cpu) {
8631 struct perf_cpu_context *cpuctx;
8632
8633 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008634 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008635 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008636 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008637 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008638
Peter Zijlstra272325c2015-04-15 11:41:58 +02008639 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008640
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008641 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008642 }
8643
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008644got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008645 if (!pmu->start_txn) {
8646 if (pmu->pmu_enable) {
8647 /*
8648 * If we have pmu_enable/pmu_disable calls, install
8649 * transaction stubs that use that to try and batch
8650 * hardware accesses.
8651 */
8652 pmu->start_txn = perf_pmu_start_txn;
8653 pmu->commit_txn = perf_pmu_commit_txn;
8654 pmu->cancel_txn = perf_pmu_cancel_txn;
8655 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008656 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008657 pmu->commit_txn = perf_pmu_nop_int;
8658 pmu->cancel_txn = perf_pmu_nop_void;
8659 }
8660 }
8661
8662 if (!pmu->pmu_enable) {
8663 pmu->pmu_enable = perf_pmu_nop_void;
8664 pmu->pmu_disable = perf_pmu_nop_void;
8665 }
8666
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008667 if (!pmu->event_idx)
8668 pmu->event_idx = perf_event_idx_default;
8669
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008670 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008671 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008672 ret = 0;
8673unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008674 mutex_unlock(&pmus_lock);
8675
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008676 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008677
Peter Zijlstraabe43402010-11-17 23:17:37 +01008678free_dev:
8679 device_del(pmu->dev);
8680 put_device(pmu->dev);
8681
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008682free_idr:
8683 if (pmu->type >= PERF_TYPE_MAX)
8684 idr_remove(&pmu_idr, pmu->type);
8685
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008686free_pdc:
8687 free_percpu(pmu->pmu_disable_count);
8688 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008689}
Yan, Zhengc464c762014-03-18 16:56:41 +08008690EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008691
8692void perf_pmu_unregister(struct pmu *pmu)
8693{
8694 mutex_lock(&pmus_lock);
8695 list_del_rcu(&pmu->entry);
8696 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008697
8698 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008699 * We dereference the pmu list under both SRCU and regular RCU, so
8700 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008701 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008702 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008703 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008704
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008705 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008706 if (pmu->type >= PERF_TYPE_MAX)
8707 idr_remove(&pmu_idr, pmu->type);
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008708 if (pmu->nr_addr_filters)
8709 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008710 device_del(pmu->dev);
8711 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01008712 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008713}
Yan, Zhengc464c762014-03-18 16:56:41 +08008714EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008715
Mark Rutlandcc34b982015-01-07 14:56:51 +00008716static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8717{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008718 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008719 int ret;
8720
8721 if (!try_module_get(pmu->module))
8722 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008723
8724 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02008725 /*
8726 * This ctx->mutex can nest when we're called through
8727 * inheritance. See the perf_event_ctx_lock_nested() comment.
8728 */
8729 ctx = perf_event_ctx_lock_nested(event->group_leader,
8730 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008731 BUG_ON(!ctx);
8732 }
8733
Mark Rutlandcc34b982015-01-07 14:56:51 +00008734 event->pmu = pmu;
8735 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008736
8737 if (ctx)
8738 perf_event_ctx_unlock(event->group_leader, ctx);
8739
Mark Rutlandcc34b982015-01-07 14:56:51 +00008740 if (ret)
8741 module_put(pmu->module);
8742
8743 return ret;
8744}
8745
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008746static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008747{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008748 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008749 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08008750 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008751
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008752 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008753
8754 rcu_read_lock();
8755 pmu = idr_find(&pmu_idr, event->attr.type);
8756 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08008757 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008758 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08008759 if (ret)
8760 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008761 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08008762 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008763
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008764 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008765 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008766 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008767 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008768
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008769 if (ret != -ENOENT) {
8770 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008771 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008772 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008773 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008774 pmu = ERR_PTR(-ENOENT);
8775unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008776 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008777
8778 return pmu;
8779}
8780
Kan Liangf2fb6be2016-03-23 11:24:37 -07008781static void attach_sb_event(struct perf_event *event)
8782{
8783 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
8784
8785 raw_spin_lock(&pel->lock);
8786 list_add_rcu(&event->sb_list, &pel->list);
8787 raw_spin_unlock(&pel->lock);
8788}
8789
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008790/*
8791 * We keep a list of all !task (and therefore per-cpu) events
8792 * that need to receive side-band records.
8793 *
8794 * This avoids having to scan all the various PMU per-cpu contexts
8795 * looking for them.
8796 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07008797static void account_pmu_sb_event(struct perf_event *event)
8798{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07008799 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07008800 attach_sb_event(event);
8801}
8802
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008803static void account_event_cpu(struct perf_event *event, int cpu)
8804{
8805 if (event->parent)
8806 return;
8807
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008808 if (is_cgroup_event(event))
8809 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
8810}
8811
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008812/* Freq events need the tick to stay alive (see perf_event_task_tick). */
8813static void account_freq_event_nohz(void)
8814{
8815#ifdef CONFIG_NO_HZ_FULL
8816 /* Lock so we don't race with concurrent unaccount */
8817 spin_lock(&nr_freq_lock);
8818 if (atomic_inc_return(&nr_freq_events) == 1)
8819 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
8820 spin_unlock(&nr_freq_lock);
8821#endif
8822}
8823
8824static void account_freq_event(void)
8825{
8826 if (tick_nohz_full_enabled())
8827 account_freq_event_nohz();
8828 else
8829 atomic_inc(&nr_freq_events);
8830}
8831
8832
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008833static void account_event(struct perf_event *event)
8834{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008835 bool inc = false;
8836
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008837 if (event->parent)
8838 return;
8839
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008840 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008841 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008842 if (event->attr.mmap || event->attr.mmap_data)
8843 atomic_inc(&nr_mmap_events);
8844 if (event->attr.comm)
8845 atomic_inc(&nr_comm_events);
8846 if (event->attr.task)
8847 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008848 if (event->attr.freq)
8849 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03008850 if (event->attr.context_switch) {
8851 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008852 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03008853 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008854 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008855 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008856 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008857 inc = true;
8858
Peter Zijlstra9107c892016-02-24 18:45:45 +01008859 if (inc) {
8860 if (atomic_inc_not_zero(&perf_sched_count))
8861 goto enabled;
8862
8863 mutex_lock(&perf_sched_mutex);
8864 if (!atomic_read(&perf_sched_count)) {
8865 static_branch_enable(&perf_sched_events);
8866 /*
8867 * Guarantee that all CPUs observe they key change and
8868 * call the perf scheduling hooks before proceeding to
8869 * install events that need them.
8870 */
8871 synchronize_sched();
8872 }
8873 /*
8874 * Now that we have waited for the sync_sched(), allow further
8875 * increments to by-pass the mutex.
8876 */
8877 atomic_inc(&perf_sched_count);
8878 mutex_unlock(&perf_sched_mutex);
8879 }
8880enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008881
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008882 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07008883
8884 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008885}
8886
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008887/*
8888 * Allocate and initialize a event structure
8889 */
8890static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008891perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008892 struct task_struct *task,
8893 struct perf_event *group_leader,
8894 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008895 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00008896 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008897{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008898 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008899 struct perf_event *event;
8900 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008901 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008902
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008903 if ((unsigned)cpu >= nr_cpu_ids) {
8904 if (!task || cpu != -1)
8905 return ERR_PTR(-EINVAL);
8906 }
8907
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008908 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008909 if (!event)
8910 return ERR_PTR(-ENOMEM);
8911
8912 /*
8913 * Single events are their own group leaders, with an
8914 * empty sibling list:
8915 */
8916 if (!group_leader)
8917 group_leader = event;
8918
8919 mutex_init(&event->child_mutex);
8920 INIT_LIST_HEAD(&event->child_list);
8921
8922 INIT_LIST_HEAD(&event->group_entry);
8923 INIT_LIST_HEAD(&event->event_entry);
8924 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008925 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01008926 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008927 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01008928 INIT_HLIST_NODE(&event->hlist_entry);
8929
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008930
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008931 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08008932 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008933
8934 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008935 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008936
Al Viroa6fa9412012-08-20 14:59:25 +01008937 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008938 event->cpu = cpu;
8939 event->attr = *attr;
8940 event->group_leader = group_leader;
8941 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008942 event->oncpu = -1;
8943
8944 event->parent = parent_event;
8945
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08008946 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008947 event->id = atomic64_inc_return(&perf_event_id);
8948
8949 event->state = PERF_EVENT_STATE_INACTIVE;
8950
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008951 if (task) {
8952 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008953 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008954 * XXX pmu::event_init needs to know what task to account to
8955 * and we cannot use the ctx information because we need the
8956 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008957 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008958 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008959 }
8960
Peter Zijlstra34f43922015-02-20 14:05:38 +01008961 event->clock = &local_clock;
8962 if (parent_event)
8963 event->clock = parent_event->clock;
8964
Avi Kivity4dc0da82011-06-29 18:42:35 +03008965 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008966 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008967 context = parent_event->overflow_handler_context;
8968 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008969
Wang Nan18794452016-03-28 06:41:30 +00008970 if (overflow_handler) {
8971 event->overflow_handler = overflow_handler;
8972 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00008973 } else if (is_write_backward(event)){
8974 event->overflow_handler = perf_event_output_backward;
8975 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00008976 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00008977 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00008978 event->overflow_handler_context = NULL;
8979 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02008980
Jiri Olsa0231bb52013-02-01 11:23:45 +01008981 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008982
8983 pmu = NULL;
8984
8985 hwc = &event->hw;
8986 hwc->sample_period = attr->sample_period;
8987 if (attr->freq && attr->sample_freq)
8988 hwc->sample_period = 1;
8989 hwc->last_period = hwc->sample_period;
8990
Peter Zijlstrae7850592010-05-21 14:43:08 +02008991 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008992
8993 /*
8994 * we currently do not support PERF_FORMAT_GROUP on inherited events
8995 */
8996 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008997 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008998
Yan, Zhenga46a2302014-11-04 21:56:06 -05008999 if (!has_branch_stack(event))
9000 event->attr.branch_sample_type = 0;
9001
Matt Fleming79dff512015-01-23 18:45:42 +00009002 if (cgroup_fd != -1) {
9003 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9004 if (err)
9005 goto err_ns;
9006 }
9007
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009008 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009009 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009010 goto err_ns;
9011 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009012 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009013 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009014 }
9015
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009016 err = exclusive_event_init(event);
9017 if (err)
9018 goto err_pmu;
9019
Alexander Shishkin375637b2016-04-27 18:44:46 +03009020 if (has_addr_filter(event)) {
9021 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9022 sizeof(unsigned long),
9023 GFP_KERNEL);
9024 if (!event->addr_filters_offs)
9025 goto err_per_task;
9026
9027 /* force hw sync on the address filters */
9028 event->addr_filters_gen = 1;
9029 }
9030
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009031 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009032 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009033 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009034 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009035 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009036 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009037 }
9038
Alexander Shishkin927a5572016-03-02 13:24:14 +02009039 /* symmetric to unaccount_event() in _free_event() */
9040 account_event(event);
9041
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009042 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009043
Alexander Shishkin375637b2016-04-27 18:44:46 +03009044err_addr_filters:
9045 kfree(event->addr_filters_offs);
9046
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009047err_per_task:
9048 exclusive_event_destroy(event);
9049
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009050err_pmu:
9051 if (event->destroy)
9052 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009053 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009054err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009055 if (is_cgroup_event(event))
9056 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009057 if (event->ns)
9058 put_pid_ns(event->ns);
9059 kfree(event);
9060
9061 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009062}
9063
9064static int perf_copy_attr(struct perf_event_attr __user *uattr,
9065 struct perf_event_attr *attr)
9066{
9067 u32 size;
9068 int ret;
9069
9070 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9071 return -EFAULT;
9072
9073 /*
9074 * zero the full structure, so that a short copy will be nice.
9075 */
9076 memset(attr, 0, sizeof(*attr));
9077
9078 ret = get_user(size, &uattr->size);
9079 if (ret)
9080 return ret;
9081
9082 if (size > PAGE_SIZE) /* silly large */
9083 goto err_size;
9084
9085 if (!size) /* abi compat */
9086 size = PERF_ATTR_SIZE_VER0;
9087
9088 if (size < PERF_ATTR_SIZE_VER0)
9089 goto err_size;
9090
9091 /*
9092 * If we're handed a bigger struct than we know of,
9093 * ensure all the unknown bits are 0 - i.e. new
9094 * user-space does not rely on any kernel feature
9095 * extensions we dont know about yet.
9096 */
9097 if (size > sizeof(*attr)) {
9098 unsigned char __user *addr;
9099 unsigned char __user *end;
9100 unsigned char val;
9101
9102 addr = (void __user *)uattr + sizeof(*attr);
9103 end = (void __user *)uattr + size;
9104
9105 for (; addr < end; addr++) {
9106 ret = get_user(val, addr);
9107 if (ret)
9108 return ret;
9109 if (val)
9110 goto err_size;
9111 }
9112 size = sizeof(*attr);
9113 }
9114
9115 ret = copy_from_user(attr, uattr, size);
9116 if (ret)
9117 return -EFAULT;
9118
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309119 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009120 return -EINVAL;
9121
9122 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9123 return -EINVAL;
9124
9125 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9126 return -EINVAL;
9127
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009128 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9129 u64 mask = attr->branch_sample_type;
9130
9131 /* only using defined bits */
9132 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9133 return -EINVAL;
9134
9135 /* at least one branch bit must be set */
9136 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9137 return -EINVAL;
9138
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009139 /* propagate priv level, when not set for branch */
9140 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9141
9142 /* exclude_kernel checked on syscall entry */
9143 if (!attr->exclude_kernel)
9144 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9145
9146 if (!attr->exclude_user)
9147 mask |= PERF_SAMPLE_BRANCH_USER;
9148
9149 if (!attr->exclude_hv)
9150 mask |= PERF_SAMPLE_BRANCH_HV;
9151 /*
9152 * adjust user setting (for HW filter setup)
9153 */
9154 attr->branch_sample_type = mask;
9155 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009156 /* privileged levels capture (kernel, hv): check permissions */
9157 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009158 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9159 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009160 }
Jiri Olsa40189942012-08-07 15:20:37 +02009161
Jiri Olsac5ebced2012-08-07 15:20:40 +02009162 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009163 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009164 if (ret)
9165 return ret;
9166 }
9167
9168 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9169 if (!arch_perf_have_user_stack_dump())
9170 return -ENOSYS;
9171
9172 /*
9173 * We have __u32 type for the size, but so far
9174 * we can only use __u16 as maximum due to the
9175 * __u16 sample size limit.
9176 */
9177 if (attr->sample_stack_user >= USHRT_MAX)
9178 ret = -EINVAL;
9179 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
9180 ret = -EINVAL;
9181 }
Jiri Olsa40189942012-08-07 15:20:37 +02009182
Stephane Eranian60e23642014-09-24 13:48:37 +02009183 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9184 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009185out:
9186 return ret;
9187
9188err_size:
9189 put_user(sizeof(*attr), &uattr->size);
9190 ret = -E2BIG;
9191 goto out;
9192}
9193
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009194static int
9195perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009196{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009197 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009198 int ret = -EINVAL;
9199
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009200 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009201 goto set;
9202
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009203 /* don't allow circular references */
9204 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009205 goto out;
9206
Peter Zijlstra0f139302010-05-20 14:35:15 +02009207 /*
9208 * Don't allow cross-cpu buffers
9209 */
9210 if (output_event->cpu != event->cpu)
9211 goto out;
9212
9213 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009214 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009215 */
9216 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9217 goto out;
9218
Peter Zijlstra34f43922015-02-20 14:05:38 +01009219 /*
9220 * Mixing clocks in the same buffer is trouble you don't need.
9221 */
9222 if (output_event->clock != event->clock)
9223 goto out;
9224
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009225 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009226 * Either writing ring buffer from beginning or from end.
9227 * Mixing is not allowed.
9228 */
9229 if (is_write_backward(output_event) != is_write_backward(event))
9230 goto out;
9231
9232 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009233 * If both events generate aux data, they must be on the same PMU
9234 */
9235 if (has_aux(event) && has_aux(output_event) &&
9236 event->pmu != output_event->pmu)
9237 goto out;
9238
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009239set:
9240 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009241 /* Can't redirect output if we've got an active mmap() */
9242 if (atomic_read(&event->mmap_count))
9243 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009244
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009245 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009246 /* get the rb we want to redirect to */
9247 rb = ring_buffer_get(output_event);
9248 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009249 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009250 }
9251
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009252 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009253
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009254 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009255unlock:
9256 mutex_unlock(&event->mmap_mutex);
9257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009258out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009259 return ret;
9260}
9261
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009262static void mutex_lock_double(struct mutex *a, struct mutex *b)
9263{
9264 if (b < a)
9265 swap(a, b);
9266
9267 mutex_lock(a);
9268 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9269}
9270
Peter Zijlstra34f43922015-02-20 14:05:38 +01009271static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9272{
9273 bool nmi_safe = false;
9274
9275 switch (clk_id) {
9276 case CLOCK_MONOTONIC:
9277 event->clock = &ktime_get_mono_fast_ns;
9278 nmi_safe = true;
9279 break;
9280
9281 case CLOCK_MONOTONIC_RAW:
9282 event->clock = &ktime_get_raw_fast_ns;
9283 nmi_safe = true;
9284 break;
9285
9286 case CLOCK_REALTIME:
9287 event->clock = &ktime_get_real_ns;
9288 break;
9289
9290 case CLOCK_BOOTTIME:
9291 event->clock = &ktime_get_boot_ns;
9292 break;
9293
9294 case CLOCK_TAI:
9295 event->clock = &ktime_get_tai_ns;
9296 break;
9297
9298 default:
9299 return -EINVAL;
9300 }
9301
9302 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9303 return -EINVAL;
9304
9305 return 0;
9306}
9307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009308/**
9309 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9310 *
9311 * @attr_uptr: event_id type attributes for monitoring/sampling
9312 * @pid: target pid
9313 * @cpu: target cpu
9314 * @group_fd: group leader event fd
9315 */
9316SYSCALL_DEFINE5(perf_event_open,
9317 struct perf_event_attr __user *, attr_uptr,
9318 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9319{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009320 struct perf_event *group_leader = NULL, *output_event = NULL;
9321 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009322 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009323 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009324 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009325 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009326 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009327 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009328 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009329 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009330 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009331 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009332 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009333
9334 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009335 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009336 return -EINVAL;
9337
9338 err = perf_copy_attr(attr_uptr, &attr);
9339 if (err)
9340 return err;
9341
9342 if (!attr.exclude_kernel) {
9343 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9344 return -EACCES;
9345 }
9346
9347 if (attr.freq) {
9348 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9349 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009350 } else {
9351 if (attr.sample_period & (1ULL << 63))
9352 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009353 }
9354
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009355 if (!attr.sample_max_stack)
9356 attr.sample_max_stack = sysctl_perf_event_max_stack;
9357
Stephane Eraniane5d13672011-02-14 11:20:01 +02009358 /*
9359 * In cgroup mode, the pid argument is used to pass the fd
9360 * opened to the cgroup directory in cgroupfs. The cpu argument
9361 * designates the cpu on which to monitor threads from that
9362 * cgroup.
9363 */
9364 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9365 return -EINVAL;
9366
Yann Droneauda21b0b32014-01-05 21:36:33 +01009367 if (flags & PERF_FLAG_FD_CLOEXEC)
9368 f_flags |= O_CLOEXEC;
9369
9370 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009371 if (event_fd < 0)
9372 return event_fd;
9373
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009374 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009375 err = perf_fget_light(group_fd, &group);
9376 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009377 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009378 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009379 if (flags & PERF_FLAG_FD_OUTPUT)
9380 output_event = group_leader;
9381 if (flags & PERF_FLAG_FD_NO_GROUP)
9382 group_leader = NULL;
9383 }
9384
Stephane Eraniane5d13672011-02-14 11:20:01 +02009385 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009386 task = find_lively_task_by_vpid(pid);
9387 if (IS_ERR(task)) {
9388 err = PTR_ERR(task);
9389 goto err_group_fd;
9390 }
9391 }
9392
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009393 if (task && group_leader &&
9394 group_leader->attr.inherit != attr.inherit) {
9395 err = -EINVAL;
9396 goto err_task;
9397 }
9398
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009399 get_online_cpus();
9400
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009401 if (task) {
9402 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9403 if (err)
9404 goto err_cpus;
9405
9406 /*
9407 * Reuse ptrace permission checks for now.
9408 *
9409 * We must hold cred_guard_mutex across this and any potential
9410 * perf_install_in_context() call for this new event to
9411 * serialize against exec() altering our credentials (and the
9412 * perf_event_exit_task() that could imply).
9413 */
9414 err = -EACCES;
9415 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9416 goto err_cred;
9417 }
9418
Matt Fleming79dff512015-01-23 18:45:42 +00009419 if (flags & PERF_FLAG_PID_CGROUP)
9420 cgroup_fd = pid;
9421
Avi Kivity4dc0da82011-06-29 18:42:35 +03009422 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009423 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009424 if (IS_ERR(event)) {
9425 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009426 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009427 }
9428
Vince Weaver53b25332014-05-16 17:12:12 -04009429 if (is_sampling_event(event)) {
9430 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309431 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009432 goto err_alloc;
9433 }
9434 }
9435
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009436 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009437 * Special case software events and allow them to be part of
9438 * any hardware group.
9439 */
9440 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009441
Peter Zijlstra34f43922015-02-20 14:05:38 +01009442 if (attr.use_clockid) {
9443 err = perf_event_set_clock(event, attr.clockid);
9444 if (err)
9445 goto err_alloc;
9446 }
9447
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009448 if (group_leader &&
9449 (is_software_event(event) != is_software_event(group_leader))) {
9450 if (is_software_event(event)) {
9451 /*
9452 * If event and group_leader are not both a software
9453 * event, and event is, then group leader is not.
9454 *
9455 * Allow the addition of software events to !software
9456 * groups, this is safe because software events never
9457 * fail to schedule.
9458 */
9459 pmu = group_leader->pmu;
9460 } else if (is_software_event(group_leader) &&
9461 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
9462 /*
9463 * In case the group is a pure software group, and we
9464 * try to add a hardware event, move the whole group to
9465 * the hardware context.
9466 */
9467 move_group = 1;
9468 }
9469 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009470
9471 /*
9472 * Get the target context (task or percpu):
9473 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009474 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009475 if (IS_ERR(ctx)) {
9476 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009477 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009478 }
9479
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009480 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9481 err = -EBUSY;
9482 goto err_context;
9483 }
9484
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009485 /*
9486 * Look up the group leader (we will attach this event to it):
9487 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009488 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009489 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009491 /*
9492 * Do not allow a recursive hierarchy (this new sibling
9493 * becoming part of another group-sibling):
9494 */
9495 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009496 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009497
9498 /* All events in a group should have the same clock */
9499 if (group_leader->clock != event->clock)
9500 goto err_context;
9501
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009502 /*
9503 * Do not allow to attach to a group in a different
9504 * task or CPU context:
9505 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009506 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009507 /*
9508 * Make sure we're both on the same task, or both
9509 * per-cpu events.
9510 */
9511 if (group_leader->ctx->task != ctx->task)
9512 goto err_context;
9513
9514 /*
9515 * Make sure we're both events for the same CPU;
9516 * grouping events for different CPUs is broken; since
9517 * you can never concurrently schedule them anyhow.
9518 */
9519 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009520 goto err_context;
9521 } else {
9522 if (group_leader->ctx != ctx)
9523 goto err_context;
9524 }
9525
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009526 /*
9527 * Only a group leader can be exclusive or pinned
9528 */
9529 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009530 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009531 }
9532
9533 if (output_event) {
9534 err = perf_event_set_output(event, output_event);
9535 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009536 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009537 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009538
Yann Droneauda21b0b32014-01-05 21:36:33 +01009539 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9540 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009541 if (IS_ERR(event_file)) {
9542 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009543 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009544 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009545 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009546
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009547 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009548 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009549 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009550 if (gctx->task == TASK_TOMBSTONE) {
9551 err = -ESRCH;
9552 goto err_locked;
9553 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009554 } else {
9555 mutex_lock(&ctx->mutex);
9556 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009557
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009558 if (ctx->task == TASK_TOMBSTONE) {
9559 err = -ESRCH;
9560 goto err_locked;
9561 }
9562
Peter Zijlstraa7239682015-09-09 19:06:33 +02009563 if (!perf_event_validate_size(event)) {
9564 err = -E2BIG;
9565 goto err_locked;
9566 }
9567
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009568 /*
9569 * Must be under the same ctx::mutex as perf_install_in_context(),
9570 * because we need to serialize with concurrent event creation.
9571 */
9572 if (!exclusive_event_installable(event, ctx)) {
9573 /* exclusive and group stuff are assumed mutually exclusive */
9574 WARN_ON_ONCE(move_group);
9575
9576 err = -EBUSY;
9577 goto err_locked;
9578 }
9579
9580 WARN_ON_ONCE(ctx->parent_ctx);
9581
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009582 /*
9583 * This is the point on no return; we cannot fail hereafter. This is
9584 * where we start modifying current state.
9585 */
9586
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009587 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009588 /*
9589 * See perf_event_ctx_lock() for comments on the details
9590 * of swizzling perf_event::ctx.
9591 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009592 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009593
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009594 list_for_each_entry(sibling, &group_leader->sibling_list,
9595 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009596 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009597 put_ctx(gctx);
9598 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009599
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009600 /*
9601 * Wait for everybody to stop referencing the events through
9602 * the old lists, before installing it on new lists.
9603 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009604 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009605
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009606 /*
9607 * Install the group siblings before the group leader.
9608 *
9609 * Because a group leader will try and install the entire group
9610 * (through the sibling list, which is still in-tact), we can
9611 * end up with siblings installed in the wrong context.
9612 *
9613 * By installing siblings first we NO-OP because they're not
9614 * reachable through the group lists.
9615 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009616 list_for_each_entry(sibling, &group_leader->sibling_list,
9617 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009618 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009619 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009620 get_ctx(ctx);
9621 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009622
9623 /*
9624 * Removing from the context ends up with disabled
9625 * event. What we want here is event in the initial
9626 * startup state, ready to be add into new context.
9627 */
9628 perf_event__state_init(group_leader);
9629 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9630 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009631
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009632 /*
9633 * Now that all events are installed in @ctx, nothing
9634 * references @gctx anymore, so drop the last reference we have
9635 * on it.
9636 */
9637 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009638 }
9639
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009640 /*
9641 * Precalculate sample_data sizes; do while holding ctx::mutex such
9642 * that we're serialized against further additions and before
9643 * perf_install_in_context() which is the point the event is active and
9644 * can use these values.
9645 */
9646 perf_event__header_size(event);
9647 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009648
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009649 event->owner = current;
9650
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08009651 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009652 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009653
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009654 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009655 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009656 mutex_unlock(&ctx->mutex);
9657
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009658 if (task) {
9659 mutex_unlock(&task->signal->cred_guard_mutex);
9660 put_task_struct(task);
9661 }
9662
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009663 put_online_cpus();
9664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009665 mutex_lock(&current->perf_event_mutex);
9666 list_add_tail(&event->owner_entry, &current->perf_event_list);
9667 mutex_unlock(&current->perf_event_mutex);
9668
Peter Zijlstra8a495422010-05-27 15:47:49 +02009669 /*
9670 * Drop the reference on the group_event after placing the
9671 * new event on the sibling_list. This ensures destruction
9672 * of the group leader will find the pointer to itself in
9673 * perf_group_detach().
9674 */
Al Viro2903ff02012-08-28 12:52:22 -04009675 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009676 fd_install(event_fd, event_file);
9677 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009678
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009679err_locked:
9680 if (move_group)
9681 mutex_unlock(&gctx->mutex);
9682 mutex_unlock(&ctx->mutex);
9683/* err_file: */
9684 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009685err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009686 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04009687 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009688err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01009689 /*
9690 * If event_file is set, the fput() above will have called ->release()
9691 * and that will take care of freeing the event.
9692 */
9693 if (!event_file)
9694 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009695err_cred:
9696 if (task)
9697 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009698err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009699 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009700err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02009701 if (task)
9702 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009703err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04009704 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009705err_fd:
9706 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009707 return err;
9708}
9709
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009710/**
9711 * perf_event_create_kernel_counter
9712 *
9713 * @attr: attributes of the counter to create
9714 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07009715 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009716 */
9717struct perf_event *
9718perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07009719 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009720 perf_overflow_handler_t overflow_handler,
9721 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009722{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009723 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009724 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009725 int err;
9726
9727 /*
9728 * Get the target context (task or percpu):
9729 */
9730
Avi Kivity4dc0da82011-06-29 18:42:35 +03009731 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009732 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009733 if (IS_ERR(event)) {
9734 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009735 goto err;
9736 }
9737
Jiri Olsaf8697762014-08-01 14:33:01 +02009738 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009739 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02009740
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009741 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009742 if (IS_ERR(ctx)) {
9743 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009744 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009745 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009746
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009747 WARN_ON_ONCE(ctx->parent_ctx);
9748 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009749 if (ctx->task == TASK_TOMBSTONE) {
9750 err = -ESRCH;
9751 goto err_unlock;
9752 }
9753
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009754 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009755 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009756 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009757 }
9758
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009759 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009760 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009761 mutex_unlock(&ctx->mutex);
9762
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009763 return event;
9764
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009765err_unlock:
9766 mutex_unlock(&ctx->mutex);
9767 perf_unpin_context(ctx);
9768 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009769err_free:
9770 free_event(event);
9771err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009772 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009773}
9774EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9775
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009776void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
9777{
9778 struct perf_event_context *src_ctx;
9779 struct perf_event_context *dst_ctx;
9780 struct perf_event *event, *tmp;
9781 LIST_HEAD(events);
9782
9783 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
9784 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
9785
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009786 /*
9787 * See perf_event_ctx_lock() for comments on the details
9788 * of swizzling perf_event::ctx.
9789 */
9790 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009791 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
9792 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009793 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009794 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009795 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02009796 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009797 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009798
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009799 /*
9800 * Wait for the events to quiesce before re-instating them.
9801 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009802 synchronize_rcu();
9803
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009804 /*
9805 * Re-instate events in 2 passes.
9806 *
9807 * Skip over group leaders and only install siblings on this first
9808 * pass, siblings will not get enabled without a leader, however a
9809 * leader will enable its siblings, even if those are still on the old
9810 * context.
9811 */
9812 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9813 if (event->group_leader == event)
9814 continue;
9815
9816 list_del(&event->migrate_entry);
9817 if (event->state >= PERF_EVENT_STATE_OFF)
9818 event->state = PERF_EVENT_STATE_INACTIVE;
9819 account_event_cpu(event, dst_cpu);
9820 perf_install_in_context(dst_ctx, event, dst_cpu);
9821 get_ctx(dst_ctx);
9822 }
9823
9824 /*
9825 * Once all the siblings are setup properly, install the group leaders
9826 * to make it go.
9827 */
Peter Zijlstra98861672013-10-03 16:02:23 +02009828 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9829 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009830 if (event->state >= PERF_EVENT_STATE_OFF)
9831 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009832 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009833 perf_install_in_context(dst_ctx, event, dst_cpu);
9834 get_ctx(dst_ctx);
9835 }
9836 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009837 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009838}
9839EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
9840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009841static void sync_child_event(struct perf_event *child_event,
9842 struct task_struct *child)
9843{
9844 struct perf_event *parent_event = child_event->parent;
9845 u64 child_val;
9846
9847 if (child_event->attr.inherit_stat)
9848 perf_event_read_event(child_event, child);
9849
Peter Zijlstrab5e58792010-05-21 14:43:12 +02009850 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009851
9852 /*
9853 * Add back the child's count to the parent's count:
9854 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02009855 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009856 atomic64_add(child_event->total_time_enabled,
9857 &parent_event->child_total_time_enabled);
9858 atomic64_add(child_event->total_time_running,
9859 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009860}
9861
9862static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009863perf_event_exit_event(struct perf_event *child_event,
9864 struct perf_event_context *child_ctx,
9865 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009866{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009867 struct perf_event *parent_event = child_event->parent;
9868
Peter Zijlstra1903d502014-07-15 17:27:27 +02009869 /*
9870 * Do not destroy the 'original' grouping; because of the context
9871 * switch optimization the original events could've ended up in a
9872 * random child task.
9873 *
9874 * If we were to destroy the original group, all group related
9875 * operations would cease to function properly after this random
9876 * child dies.
9877 *
9878 * Do destroy all inherited groups, we don't care about those
9879 * and being thorough is better.
9880 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009881 raw_spin_lock_irq(&child_ctx->lock);
9882 WARN_ON_ONCE(child_ctx->is_active);
9883
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009884 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01009885 perf_group_detach(child_event);
9886 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01009887 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009888 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009890 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009891 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009892 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009893 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04009894 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009895 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009896 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009897 /*
9898 * Child events can be cleaned up.
9899 */
9900
9901 sync_child_event(child_event, child);
9902
9903 /*
9904 * Remove this event from the parent's list
9905 */
9906 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9907 mutex_lock(&parent_event->child_mutex);
9908 list_del_init(&child_event->child_list);
9909 mutex_unlock(&parent_event->child_mutex);
9910
9911 /*
9912 * Kick perf_poll() for is_event_hup().
9913 */
9914 perf_event_wakeup(parent_event);
9915 free_event(child_event);
9916 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009917}
9918
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009919static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009920{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009921 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009922 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009923
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009924 WARN_ON_ONCE(child != current);
9925
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009926 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009927 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009928 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009930 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009931 * In order to reduce the amount of tricky in ctx tear-down, we hold
9932 * ctx::mutex over the entire thing. This serializes against almost
9933 * everything that wants to access the ctx.
9934 *
9935 * The exception is sys_perf_event_open() /
9936 * perf_event_create_kernel_count() which does find_get_context()
9937 * without ctx::mutex (it cannot because of the move_group double mutex
9938 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009939 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009940 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009941
9942 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009943 * In a single ctx::lock section, de-schedule the events and detach the
9944 * context from the task such that we cannot ever get it scheduled back
9945 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009946 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009947 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009948 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009949
9950 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009951 * Now that the context is inactive, destroy the task <-> ctx relation
9952 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009953 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009954 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
9955 put_ctx(child_ctx); /* cannot be last */
9956 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
9957 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009958
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009959 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009960 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009961
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009962 if (clone_ctx)
9963 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009964
9965 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009966 * Report the task dead after unscheduling the events so that we
9967 * won't get any samples after PERF_RECORD_EXIT. We can however still
9968 * get a few PERF_RECORD_READ events.
9969 */
9970 perf_event_task(child, child_ctx, 0);
9971
Peter Zijlstraebf905f2014-05-29 19:00:24 +02009972 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009973 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009974
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009975 mutex_unlock(&child_ctx->mutex);
9976
9977 put_ctx(child_ctx);
9978}
9979
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009980/*
9981 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009982 *
9983 * Can be called with cred_guard_mutex held when called from
9984 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009985 */
9986void perf_event_exit_task(struct task_struct *child)
9987{
Peter Zijlstra88821352010-11-09 19:01:43 +01009988 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009989 int ctxn;
9990
Peter Zijlstra88821352010-11-09 19:01:43 +01009991 mutex_lock(&child->perf_event_mutex);
9992 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
9993 owner_entry) {
9994 list_del_init(&event->owner_entry);
9995
9996 /*
9997 * Ensure the list deletion is visible before we clear
9998 * the owner, closes a race against perf_release() where
9999 * we need to serialize on the owner->perf_event_mutex.
10000 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010001 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010002 }
10003 mutex_unlock(&child->perf_event_mutex);
10004
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010005 for_each_task_context_nr(ctxn)
10006 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010007
10008 /*
10009 * The perf_event_exit_task_context calls perf_event_task
10010 * with child's task_ctx, which generates EXIT events for
10011 * child contexts and sets child->perf_event_ctxp[] to NULL.
10012 * At this point we need to send EXIT events to cpu contexts.
10013 */
10014 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010015}
10016
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010017static void perf_free_event(struct perf_event *event,
10018 struct perf_event_context *ctx)
10019{
10020 struct perf_event *parent = event->parent;
10021
10022 if (WARN_ON_ONCE(!parent))
10023 return;
10024
10025 mutex_lock(&parent->child_mutex);
10026 list_del_init(&event->child_list);
10027 mutex_unlock(&parent->child_mutex);
10028
Al Viroa6fa9412012-08-20 14:59:25 +010010029 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010030
Peter Zijlstra652884f2015-01-23 11:20:10 +010010031 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010032 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010033 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010034 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010035 free_event(event);
10036}
10037
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010038/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010039 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010040 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010041 *
10042 * Not all locks are strictly required, but take them anyway to be nice and
10043 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010044 */
10045void perf_event_free_task(struct task_struct *task)
10046{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010047 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010048 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010049 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010050
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010051 for_each_task_context_nr(ctxn) {
10052 ctx = task->perf_event_ctxp[ctxn];
10053 if (!ctx)
10054 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010055
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010056 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010057again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010058 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10059 group_entry)
10060 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010061
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010062 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10063 group_entry)
10064 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010065
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010066 if (!list_empty(&ctx->pinned_groups) ||
10067 !list_empty(&ctx->flexible_groups))
10068 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010069
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010070 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010071
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010072 put_ctx(ctx);
10073 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010074}
10075
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010076void perf_event_delayed_put(struct task_struct *task)
10077{
10078 int ctxn;
10079
10080 for_each_task_context_nr(ctxn)
10081 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10082}
10083
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010084struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010085{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010086 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010087
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010088 file = fget_raw(fd);
10089 if (!file)
10090 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010091
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010092 if (file->f_op != &perf_fops) {
10093 fput(file);
10094 return ERR_PTR(-EBADF);
10095 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010096
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010097 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010098}
10099
10100const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10101{
10102 if (!event)
10103 return ERR_PTR(-EINVAL);
10104
10105 return &event->attr;
10106}
10107
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010108/*
10109 * inherit a event from parent task to child task:
10110 */
10111static struct perf_event *
10112inherit_event(struct perf_event *parent_event,
10113 struct task_struct *parent,
10114 struct perf_event_context *parent_ctx,
10115 struct task_struct *child,
10116 struct perf_event *group_leader,
10117 struct perf_event_context *child_ctx)
10118{
Jiri Olsa1929def2014-09-12 13:18:27 +020010119 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010120 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010121 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010122
10123 /*
10124 * Instead of creating recursive hierarchies of events,
10125 * we link inherited events back to the original parent,
10126 * which has a filp for sure, which we use as the reference
10127 * count:
10128 */
10129 if (parent_event->parent)
10130 parent_event = parent_event->parent;
10131
10132 child_event = perf_event_alloc(&parent_event->attr,
10133 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010134 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010135 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010136 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010137 if (IS_ERR(child_event))
10138 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010139
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010140 /*
10141 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10142 * must be under the same lock in order to serialize against
10143 * perf_event_release_kernel(), such that either we must observe
10144 * is_orphaned_event() or they will observe us on the child_list.
10145 */
10146 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010147 if (is_orphaned_event(parent_event) ||
10148 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010149 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010150 free_event(child_event);
10151 return NULL;
10152 }
10153
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010154 get_ctx(child_ctx);
10155
10156 /*
10157 * Make the child state follow the state of the parent event,
10158 * not its attr.disabled bit. We hold the parent's mutex,
10159 * so we won't race with perf_event_{en, dis}able_family.
10160 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010161 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010162 child_event->state = PERF_EVENT_STATE_INACTIVE;
10163 else
10164 child_event->state = PERF_EVENT_STATE_OFF;
10165
10166 if (parent_event->attr.freq) {
10167 u64 sample_period = parent_event->hw.sample_period;
10168 struct hw_perf_event *hwc = &child_event->hw;
10169
10170 hwc->sample_period = sample_period;
10171 hwc->last_period = sample_period;
10172
10173 local64_set(&hwc->period_left, sample_period);
10174 }
10175
10176 child_event->ctx = child_ctx;
10177 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010178 child_event->overflow_handler_context
10179 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010180
10181 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010182 * Precalculate sample_data sizes
10183 */
10184 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010185 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010186
10187 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010188 * Link it up in the child's context:
10189 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010190 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010191 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010192 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010193
10194 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010195 * Link this into the parent event's child list
10196 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010197 list_add_tail(&child_event->child_list, &parent_event->child_list);
10198 mutex_unlock(&parent_event->child_mutex);
10199
10200 return child_event;
10201}
10202
10203static int inherit_group(struct perf_event *parent_event,
10204 struct task_struct *parent,
10205 struct perf_event_context *parent_ctx,
10206 struct task_struct *child,
10207 struct perf_event_context *child_ctx)
10208{
10209 struct perf_event *leader;
10210 struct perf_event *sub;
10211 struct perf_event *child_ctr;
10212
10213 leader = inherit_event(parent_event, parent, parent_ctx,
10214 child, NULL, child_ctx);
10215 if (IS_ERR(leader))
10216 return PTR_ERR(leader);
10217 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10218 child_ctr = inherit_event(sub, parent, parent_ctx,
10219 child, leader, child_ctx);
10220 if (IS_ERR(child_ctr))
10221 return PTR_ERR(child_ctr);
10222 }
10223 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010224}
10225
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010226static int
10227inherit_task_group(struct perf_event *event, struct task_struct *parent,
10228 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010229 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010230 int *inherited_all)
10231{
10232 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010233 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010234
10235 if (!event->attr.inherit) {
10236 *inherited_all = 0;
10237 return 0;
10238 }
10239
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010240 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010241 if (!child_ctx) {
10242 /*
10243 * This is executed from the parent task context, so
10244 * inherit events that have been marked for cloning.
10245 * First allocate and initialize a context for the
10246 * child.
10247 */
10248
Jiri Olsa734df5a2013-07-09 17:44:10 +020010249 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010250 if (!child_ctx)
10251 return -ENOMEM;
10252
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010253 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010254 }
10255
10256 ret = inherit_group(event, parent, parent_ctx,
10257 child, child_ctx);
10258
10259 if (ret)
10260 *inherited_all = 0;
10261
10262 return ret;
10263}
10264
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010265/*
10266 * Initialize the perf_event context in task_struct
10267 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010268static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010269{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010270 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010271 struct perf_event_context *cloned_ctx;
10272 struct perf_event *event;
10273 struct task_struct *parent = current;
10274 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010275 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010276 int ret = 0;
10277
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010278 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010279 return 0;
10280
10281 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010282 * If the parent's context is a clone, pin it so it won't get
10283 * swapped under us.
10284 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010285 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010286 if (!parent_ctx)
10287 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010288
10289 /*
10290 * No need to check if parent_ctx != NULL here; since we saw
10291 * it non-NULL earlier, the only reason for it to become NULL
10292 * is if we exit, and since we're currently in the middle of
10293 * a fork we can't be exiting at the same time.
10294 */
10295
10296 /*
10297 * Lock the parent list. No need to lock the child - not PID
10298 * hashed yet and not running, so nobody can access it.
10299 */
10300 mutex_lock(&parent_ctx->mutex);
10301
10302 /*
10303 * We dont have to disable NMIs - we are only looking at
10304 * the list, not manipulating it:
10305 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010306 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010307 ret = inherit_task_group(event, parent, parent_ctx,
10308 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010309 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010310 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010311 }
10312
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010313 /*
10314 * We can't hold ctx->lock when iterating the ->flexible_group list due
10315 * to allocations, but we need to prevent rotation because
10316 * rotate_ctx() will change the list from interrupt context.
10317 */
10318 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10319 parent_ctx->rotate_disable = 1;
10320 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10321
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010322 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010323 ret = inherit_task_group(event, parent, parent_ctx,
10324 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010325 if (ret)
10326 break;
10327 }
10328
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010329 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10330 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010331
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010332 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010333
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010334 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010335 /*
10336 * Mark the child context as a clone of the parent
10337 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010338 *
10339 * Note that if the parent is a clone, the holding of
10340 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010341 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010342 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010343 if (cloned_ctx) {
10344 child_ctx->parent_ctx = cloned_ctx;
10345 child_ctx->parent_gen = parent_ctx->parent_gen;
10346 } else {
10347 child_ctx->parent_ctx = parent_ctx;
10348 child_ctx->parent_gen = parent_ctx->generation;
10349 }
10350 get_ctx(child_ctx->parent_ctx);
10351 }
10352
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010353 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010354 mutex_unlock(&parent_ctx->mutex);
10355
10356 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010357 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010358
10359 return ret;
10360}
10361
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010362/*
10363 * Initialize the perf_event context in task_struct
10364 */
10365int perf_event_init_task(struct task_struct *child)
10366{
10367 int ctxn, ret;
10368
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010369 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10370 mutex_init(&child->perf_event_mutex);
10371 INIT_LIST_HEAD(&child->perf_event_list);
10372
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010373 for_each_task_context_nr(ctxn) {
10374 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010375 if (ret) {
10376 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010377 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010378 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010379 }
10380
10381 return 0;
10382}
10383
Paul Mackerras220b1402010-03-10 20:45:52 +110010384static void __init perf_event_init_all_cpus(void)
10385{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010386 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010387 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010388
10389 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010390 swhash = &per_cpu(swevent_htable, cpu);
10391 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010392 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010393
10394 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10395 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010396 }
10397}
10398
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010399int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010400{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010401 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010402
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010403 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010404 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010405 struct swevent_hlist *hlist;
10406
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010407 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10408 WARN_ON(!hlist);
10409 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010410 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010411 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010412 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010413}
10414
Dave Young2965faa2015-09-09 15:38:55 -070010415#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010416static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010417{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010418 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010419 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10420 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010421
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010422 raw_spin_lock(&ctx->lock);
10423 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010424 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010425 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010426}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010427
10428static void perf_event_exit_cpu_context(int cpu)
10429{
10430 struct perf_event_context *ctx;
10431 struct pmu *pmu;
10432 int idx;
10433
10434 idx = srcu_read_lock(&pmus_srcu);
10435 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010436 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010437
10438 mutex_lock(&ctx->mutex);
10439 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10440 mutex_unlock(&ctx->mutex);
10441 }
10442 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010443}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010444#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010445
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010446static void perf_event_exit_cpu_context(int cpu) { }
10447
10448#endif
10449
10450int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010451{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010452 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010453 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010454}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010455
Peter Zijlstrac2774432010-12-08 15:29:02 +010010456static int
10457perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10458{
10459 int cpu;
10460
10461 for_each_online_cpu(cpu)
10462 perf_event_exit_cpu(cpu);
10463
10464 return NOTIFY_OK;
10465}
10466
10467/*
10468 * Run the perf reboot notifier at the very last possible moment so that
10469 * the generic watchdog code runs as long as possible.
10470 */
10471static struct notifier_block perf_reboot_notifier = {
10472 .notifier_call = perf_reboot,
10473 .priority = INT_MIN,
10474};
10475
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010476void __init perf_event_init(void)
10477{
Jason Wessel3c502e72010-11-04 17:33:01 -050010478 int ret;
10479
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010480 idr_init(&pmu_idr);
10481
Paul Mackerras220b1402010-03-10 20:45:52 +110010482 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010483 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010484 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10485 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10486 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010487 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010488 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010489 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010490
10491 ret = init_hw_breakpoint();
10492 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010493
Jiri Olsab01c3a02012-03-23 15:41:20 +010010494 /*
10495 * Build time assertion that we keep the data_head at the intended
10496 * location. IOW, validation we got the __reserved[] size right.
10497 */
10498 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10499 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010500}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010501
Cody P Schaferfd979c02015-01-30 13:45:57 -080010502ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10503 char *page)
10504{
10505 struct perf_pmu_events_attr *pmu_attr =
10506 container_of(attr, struct perf_pmu_events_attr, attr);
10507
10508 if (pmu_attr->event_str)
10509 return sprintf(page, "%s\n", pmu_attr->event_str);
10510
10511 return 0;
10512}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010513EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010514
Peter Zijlstraabe43402010-11-17 23:17:37 +010010515static int __init perf_event_sysfs_init(void)
10516{
10517 struct pmu *pmu;
10518 int ret;
10519
10520 mutex_lock(&pmus_lock);
10521
10522 ret = bus_register(&pmu_bus);
10523 if (ret)
10524 goto unlock;
10525
10526 list_for_each_entry(pmu, &pmus, entry) {
10527 if (!pmu->name || pmu->type < 0)
10528 continue;
10529
10530 ret = pmu_dev_alloc(pmu);
10531 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10532 }
10533 pmu_bus_running = 1;
10534 ret = 0;
10535
10536unlock:
10537 mutex_unlock(&pmus_lock);
10538
10539 return ret;
10540}
10541device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010542
10543#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010544static struct cgroup_subsys_state *
10545perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010546{
10547 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010548
Li Zefan1b15d052011-03-03 14:26:06 +080010549 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010550 if (!jc)
10551 return ERR_PTR(-ENOMEM);
10552
Stephane Eraniane5d13672011-02-14 11:20:01 +020010553 jc->info = alloc_percpu(struct perf_cgroup_info);
10554 if (!jc->info) {
10555 kfree(jc);
10556 return ERR_PTR(-ENOMEM);
10557 }
10558
Stephane Eraniane5d13672011-02-14 11:20:01 +020010559 return &jc->css;
10560}
10561
Tejun Heoeb954192013-08-08 20:11:23 -040010562static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010563{
Tejun Heoeb954192013-08-08 20:11:23 -040010564 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10565
Stephane Eraniane5d13672011-02-14 11:20:01 +020010566 free_percpu(jc->info);
10567 kfree(jc);
10568}
10569
10570static int __perf_cgroup_move(void *info)
10571{
10572 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010573 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010574 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010575 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010576 return 0;
10577}
10578
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010579static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010580{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010581 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010582 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010583
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010584 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010585 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010586}
10587
Tejun Heo073219e2014-02-08 10:36:58 -050010588struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010589 .css_alloc = perf_cgroup_css_alloc,
10590 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010591 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010592};
10593#endif /* CONFIG_CGROUP_PERF */