blob: 803481cb6cbd57d5b75272011dc9a1497b695480 [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
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002808static DEFINE_PER_CPU(struct list_head, sched_cb_list);
2809
Yan, Zhengba532502014-11-04 21:55:58 -05002810void perf_sched_cb_dec(struct pmu *pmu)
2811{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002812 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2813
Yan, Zhengba532502014-11-04 21:55:58 -05002814 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002815
2816 if (!--cpuctx->sched_cb_usage)
2817 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05002818}
2819
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002820
Yan, Zhengba532502014-11-04 21:55:58 -05002821void perf_sched_cb_inc(struct pmu *pmu)
2822{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002823 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2824
2825 if (!cpuctx->sched_cb_usage++)
2826 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
2827
Yan, Zhengba532502014-11-04 21:55:58 -05002828 this_cpu_inc(perf_sched_cb_usages);
2829}
2830
2831/*
2832 * This function provides the context switch callback to the lower code
2833 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02002834 *
2835 * This callback is relevant even to per-cpu events; for example multi event
2836 * PEBS requires this to provide PID/TID information. This requires we flush
2837 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05002838 */
2839static void perf_pmu_sched_task(struct task_struct *prev,
2840 struct task_struct *next,
2841 bool sched_in)
2842{
2843 struct perf_cpu_context *cpuctx;
2844 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05002845
2846 if (prev == next)
2847 return;
2848
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002849 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
2850 pmu = cpuctx->unique_pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05002851
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002852 if (WARN_ON_ONCE(!pmu->sched_task))
2853 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05002854
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002855 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2856 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05002857
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002858 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05002859
Peter Zijlstrae48c1782016-07-06 09:18:30 +02002860 perf_pmu_enable(pmu);
2861 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05002862 }
Yan, Zhengba532502014-11-04 21:55:58 -05002863}
2864
Adrian Hunter45ac1402015-07-21 12:44:02 +03002865static void perf_event_switch(struct task_struct *task,
2866 struct task_struct *next_prev, bool sched_in);
2867
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002868#define for_each_task_context_nr(ctxn) \
2869 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2870
2871/*
2872 * Called from scheduler to remove the events of the current task,
2873 * with interrupts disabled.
2874 *
2875 * We stop each event and update the event value in event->count.
2876 *
2877 * This does not protect us against NMI, but disable()
2878 * sets the disabled bit in the control field of event _before_
2879 * accessing the event control register. If a NMI hits, then it will
2880 * not restart the event.
2881 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002882void __perf_event_task_sched_out(struct task_struct *task,
2883 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002884{
2885 int ctxn;
2886
Yan, Zhengba532502014-11-04 21:55:58 -05002887 if (__this_cpu_read(perf_sched_cb_usages))
2888 perf_pmu_sched_task(task, next, false);
2889
Adrian Hunter45ac1402015-07-21 12:44:02 +03002890 if (atomic_read(&nr_switch_events))
2891 perf_event_switch(task, next, false);
2892
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002893 for_each_task_context_nr(ctxn)
2894 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002895
2896 /*
2897 * if cgroup events exist on this CPU, then we need
2898 * to check if we have to switch out PMU state.
2899 * cgroup event are system-wide mode only
2900 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002901 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002902 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002903}
2904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905/*
2906 * Called with IRQs disabled
2907 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002908static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2909 enum event_type_t event_type)
2910{
2911 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912}
2913
2914static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002915ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002916 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002917{
2918 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002920 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2921 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002922 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002923 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002924 continue;
2925
Stephane Eraniane5d13672011-02-14 11:20:01 +02002926 /* may need to reset tstamp_enabled */
2927 if (is_cgroup_event(event))
2928 perf_cgroup_mark_enabled(event, ctx);
2929
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002930 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002931 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002932
2933 /*
2934 * If this pinned group hasn't been scheduled,
2935 * put it in error state.
2936 */
2937 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2938 update_group_times(event);
2939 event->state = PERF_EVENT_STATE_ERROR;
2940 }
2941 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002942}
2943
2944static void
2945ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002946 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002947{
2948 struct perf_event *event;
2949 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002951 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2952 /* Ignore events in OFF or ERROR state */
2953 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002954 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002955 /*
2956 * Listen to the 'cpu' scheduling filter constraint
2957 * of events:
2958 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002959 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002960 continue;
2961
Stephane Eraniane5d13672011-02-14 11:20:01 +02002962 /* may need to reset tstamp_enabled */
2963 if (is_cgroup_event(event))
2964 perf_cgroup_mark_enabled(event, ctx);
2965
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002966 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002967 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002969 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002971}
2972
2973static void
2974ctx_sched_in(struct perf_event_context *ctx,
2975 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002976 enum event_type_t event_type,
2977 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002978{
Peter Zijlstradb24d332011-04-09 21:17:45 +02002979 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01002980 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002981
Peter Zijlstrac994d612016-01-08 09:20:23 +01002982 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002983
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002984 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002985 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002986
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002987 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01002988 if (ctx->task) {
2989 if (!is_active)
2990 cpuctx->task_ctx = ctx;
2991 else
2992 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2993 }
2994
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01002995 is_active ^= ctx->is_active; /* changed bits */
2996
2997 if (is_active & EVENT_TIME) {
2998 /* start ctx time */
2999 now = perf_clock();
3000 ctx->timestamp = now;
3001 perf_cgroup_set_timestamp(task, ctx);
3002 }
3003
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003004 /*
3005 * First go through the list and put on any pinned groups
3006 * in order to give them the best chance of going on.
3007 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003008 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003009 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003010
3011 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003012 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003013 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014}
3015
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003016static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003017 enum event_type_t event_type,
3018 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003019{
3020 struct perf_event_context *ctx = &cpuctx->ctx;
3021
Stephane Eraniane5d13672011-02-14 11:20:01 +02003022 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003023}
3024
Stephane Eraniane5d13672011-02-14 11:20:01 +02003025static void perf_event_context_sched_in(struct perf_event_context *ctx,
3026 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003027{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003028 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003030 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003031 if (cpuctx->task_ctx == ctx)
3032 return;
3033
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003034 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003035 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003036 /*
3037 * We want to keep the following priority order:
3038 * cpu pinned (that don't need to move), task pinned,
3039 * cpu flexible, task flexible.
3040 */
3041 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003042 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003043 perf_pmu_enable(ctx->pmu);
3044 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045}
3046
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003047/*
3048 * Called from scheduler to add the events of the current task
3049 * with interrupts disabled.
3050 *
3051 * We restore the event value and then enable it.
3052 *
3053 * This does not protect us against NMI, but enable()
3054 * sets the enabled bit in the control field of event _before_
3055 * accessing the event control register. If a NMI hits, then it will
3056 * keep the event running.
3057 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003058void __perf_event_task_sched_in(struct task_struct *prev,
3059 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003060{
3061 struct perf_event_context *ctx;
3062 int ctxn;
3063
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003064 /*
3065 * If cgroup events exist on this CPU, then we need to check if we have
3066 * to switch in PMU state; cgroup event are system-wide mode only.
3067 *
3068 * Since cgroup events are CPU events, we must schedule these in before
3069 * we schedule in the task events.
3070 */
3071 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3072 perf_cgroup_sched_in(prev, task);
3073
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003074 for_each_task_context_nr(ctxn) {
3075 ctx = task->perf_event_ctxp[ctxn];
3076 if (likely(!ctx))
3077 continue;
3078
Stephane Eraniane5d13672011-02-14 11:20:01 +02003079 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003080 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003081
Adrian Hunter45ac1402015-07-21 12:44:02 +03003082 if (atomic_read(&nr_switch_events))
3083 perf_event_switch(task, prev, true);
3084
Yan, Zhengba532502014-11-04 21:55:58 -05003085 if (__this_cpu_read(perf_sched_cb_usages))
3086 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003087}
3088
Peter Zijlstraabd50712010-01-26 18:50:16 +01003089static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3090{
3091 u64 frequency = event->attr.sample_freq;
3092 u64 sec = NSEC_PER_SEC;
3093 u64 divisor, dividend;
3094
3095 int count_fls, nsec_fls, frequency_fls, sec_fls;
3096
3097 count_fls = fls64(count);
3098 nsec_fls = fls64(nsec);
3099 frequency_fls = fls64(frequency);
3100 sec_fls = 30;
3101
3102 /*
3103 * We got @count in @nsec, with a target of sample_freq HZ
3104 * the target period becomes:
3105 *
3106 * @count * 10^9
3107 * period = -------------------
3108 * @nsec * sample_freq
3109 *
3110 */
3111
3112 /*
3113 * Reduce accuracy by one bit such that @a and @b converge
3114 * to a similar magnitude.
3115 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003116#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003117do { \
3118 if (a##_fls > b##_fls) { \
3119 a >>= 1; \
3120 a##_fls--; \
3121 } else { \
3122 b >>= 1; \
3123 b##_fls--; \
3124 } \
3125} while (0)
3126
3127 /*
3128 * Reduce accuracy until either term fits in a u64, then proceed with
3129 * the other, so that finally we can do a u64/u64 division.
3130 */
3131 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3132 REDUCE_FLS(nsec, frequency);
3133 REDUCE_FLS(sec, count);
3134 }
3135
3136 if (count_fls + sec_fls > 64) {
3137 divisor = nsec * frequency;
3138
3139 while (count_fls + sec_fls > 64) {
3140 REDUCE_FLS(count, sec);
3141 divisor >>= 1;
3142 }
3143
3144 dividend = count * sec;
3145 } else {
3146 dividend = count * sec;
3147
3148 while (nsec_fls + frequency_fls > 64) {
3149 REDUCE_FLS(nsec, frequency);
3150 dividend >>= 1;
3151 }
3152
3153 divisor = nsec * frequency;
3154 }
3155
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003156 if (!divisor)
3157 return dividend;
3158
Peter Zijlstraabd50712010-01-26 18:50:16 +01003159 return div64_u64(dividend, divisor);
3160}
3161
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003162static DEFINE_PER_CPU(int, perf_throttled_count);
3163static DEFINE_PER_CPU(u64, perf_throttled_seq);
3164
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003165static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003166{
3167 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02003168 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003169 s64 delta;
3170
Peter Zijlstraabd50712010-01-26 18:50:16 +01003171 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003172
3173 delta = (s64)(period - hwc->sample_period);
3174 delta = (delta + 7) / 8; /* low pass filter */
3175
3176 sample_period = hwc->sample_period + delta;
3177
3178 if (!sample_period)
3179 sample_period = 1;
3180
3181 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003182
Peter Zijlstrae7850592010-05-21 14:43:08 +02003183 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003184 if (disable)
3185 event->pmu->stop(event, PERF_EF_UPDATE);
3186
Peter Zijlstrae7850592010-05-21 14:43:08 +02003187 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003188
3189 if (disable)
3190 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003191 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192}
3193
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003194/*
3195 * combine freq adjustment with unthrottling to avoid two passes over the
3196 * events. At the same time, make sure, having freq events does not change
3197 * the rate of unthrottling as that would introduce bias.
3198 */
3199static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3200 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201{
3202 struct perf_event *event;
3203 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003204 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003205 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003207 /*
3208 * only need to iterate over all events iff:
3209 * - context have events in frequency mode (needs freq adjust)
3210 * - there are events to unthrottle on this cpu
3211 */
3212 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003213 return;
3214
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003215 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003216 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003217
Paul Mackerras03541f82009-10-14 16:58:03 +11003218 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003219 if (event->state != PERF_EVENT_STATE_ACTIVE)
3220 continue;
3221
Stephane Eranian5632ab12011-01-03 18:20:01 +02003222 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003223 continue;
3224
Alexander Shishkin44377272013-12-16 14:17:36 +02003225 perf_pmu_disable(event->pmu);
3226
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227 hwc = &event->hw;
3228
Jiri Olsaae23bff2013-08-24 16:45:54 +02003229 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003230 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003232 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003233 }
3234
3235 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003236 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003237
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003238 /*
3239 * stop the event and update event->count
3240 */
3241 event->pmu->stop(event, PERF_EF_UPDATE);
3242
Peter Zijlstrae7850592010-05-21 14:43:08 +02003243 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003244 delta = now - hwc->freq_count_stamp;
3245 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003246
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003247 /*
3248 * restart the event
3249 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003250 * we have stopped the event so tell that
3251 * to perf_adjust_period() to avoid stopping it
3252 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003253 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003254 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003255 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003256
3257 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003258 next:
3259 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003261
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003262 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003263 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003264}
3265
3266/*
3267 * Round-robin a context's events:
3268 */
3269static void rotate_ctx(struct perf_event_context *ctx)
3270{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003271 /*
3272 * Rotate the first entry last of non-pinned groups. Rotation might be
3273 * disabled by the inheritance code.
3274 */
3275 if (!ctx->rotate_disable)
3276 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277}
3278
Stephane Eranian9e630202013-04-03 14:21:33 +02003279static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003281 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003282 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003284 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003285 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3286 rotate = 1;
3287 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003288
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003289 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003290 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003291 if (ctx->nr_events != ctx->nr_active)
3292 rotate = 1;
3293 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003294
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003295 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003296 goto done;
3297
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003298 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003299 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003300
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003301 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3302 if (ctx)
3303 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003304
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003305 rotate_ctx(&cpuctx->ctx);
3306 if (ctx)
3307 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003309 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003310
3311 perf_pmu_enable(cpuctx->ctx.pmu);
3312 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003313done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003314
3315 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003316}
3317
3318void perf_event_task_tick(void)
3319{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003320 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3321 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003322 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003323
3324 WARN_ON(!irqs_disabled());
3325
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003326 __this_cpu_inc(perf_throttled_seq);
3327 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003328 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003329
Mark Rutland2fde4f92015-01-07 15:01:54 +00003330 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003331 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332}
3333
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003334static int event_enable_on_exec(struct perf_event *event,
3335 struct perf_event_context *ctx)
3336{
3337 if (!event->attr.enable_on_exec)
3338 return 0;
3339
3340 event->attr.enable_on_exec = 0;
3341 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3342 return 0;
3343
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003344 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003345
3346 return 1;
3347}
3348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003349/*
3350 * Enable all of a task's events that have been marked enable-on-exec.
3351 * This expects task == current.
3352 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003353static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003354{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003355 struct perf_event_context *ctx, *clone_ctx = NULL;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003356 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357 struct perf_event *event;
3358 unsigned long flags;
3359 int enabled = 0;
3360
3361 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003362 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 if (!ctx || !ctx->nr_events)
3364 goto out;
3365
Peter Zijlstra3e349502016-01-08 10:01:18 +01003366 cpuctx = __get_cpu_context(ctx);
3367 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003368 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003369 list_for_each_entry(event, &ctx->event_list, event_entry)
3370 enabled |= event_enable_on_exec(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371
3372 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003373 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003374 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003375 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003376 clone_ctx = unclone_ctx(ctx);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003377 ctx_resched(cpuctx, ctx);
3378 }
3379 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003380
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003381out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003383
3384 if (clone_ctx)
3385 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003386}
3387
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003388struct perf_read_data {
3389 struct perf_event *event;
3390 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003391 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003392};
3393
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003394/*
3395 * Cross CPU call to read the hardware event
3396 */
3397static void __perf_event_read(void *info)
3398{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003399 struct perf_read_data *data = info;
3400 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003402 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003403 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003404
3405 /*
3406 * If this is a task context, we need to check whether it is
3407 * the current task context of this cpu. If not it has been
3408 * scheduled out before the smp call arrived. In that case
3409 * event->count would have been updated to a recent sample
3410 * when the event was scheduled out.
3411 */
3412 if (ctx->task && cpuctx->task_ctx != ctx)
3413 return;
3414
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003415 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003416 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003417 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003418 update_cgrp_time_from_event(event);
3419 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003420
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003421 update_event_times(event);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003422 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003423 goto unlock;
3424
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003425 if (!data->group) {
3426 pmu->read(event);
3427 data->ret = 0;
3428 goto unlock;
3429 }
3430
3431 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3432
3433 pmu->read(event);
3434
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003435 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3436 update_event_times(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003437 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3438 /*
3439 * Use sibling's PMU rather than @event's since
3440 * sibling could be on different (eg: software) PMU.
3441 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003442 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003443 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003444 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07003445
3446 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003447
3448unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003449 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003450}
3451
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003452static inline u64 perf_event_count(struct perf_event *event)
3453{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003454 if (event->pmu->count)
3455 return event->pmu->count(event);
3456
3457 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003458}
3459
Kaixu Xiaffe86902015-08-06 07:02:32 +00003460/*
3461 * NMI-safe method to read a local event, that is an event that
3462 * is:
3463 * - either for the current task, or for this CPU
3464 * - does not have inherit set, for inherited task events
3465 * will not be local and we cannot read them atomically
3466 * - must not have a pmu::count method
3467 */
3468u64 perf_event_read_local(struct perf_event *event)
3469{
3470 unsigned long flags;
3471 u64 val;
3472
3473 /*
3474 * Disabling interrupts avoids all counter scheduling (context
3475 * switches, timer based rotation and IPIs).
3476 */
3477 local_irq_save(flags);
3478
3479 /* If this is a per-task event, it must be for current */
3480 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3481 event->hw.target != current);
3482
3483 /* If this is a per-CPU event, it must be for this CPU */
3484 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3485 event->cpu != smp_processor_id());
3486
3487 /*
3488 * It must not be an event with inherit set, we cannot read
3489 * all child counters from atomic context.
3490 */
3491 WARN_ON_ONCE(event->attr.inherit);
3492
3493 /*
3494 * It must not have a pmu::count method, those are not
3495 * NMI safe.
3496 */
3497 WARN_ON_ONCE(event->pmu->count);
3498
3499 /*
3500 * If the event is currently on this CPU, its either a per-task event,
3501 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3502 * oncpu == -1).
3503 */
3504 if (event->oncpu == smp_processor_id())
3505 event->pmu->read(event);
3506
3507 val = local64_read(&event->count);
3508 local_irq_restore(flags);
3509
3510 return val;
3511}
3512
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003513static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003514{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003515 int ret = 0;
3516
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003517 /*
3518 * If event is enabled and currently active on a CPU, update the
3519 * value in the event structure:
3520 */
3521 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003522 struct perf_read_data data = {
3523 .event = event,
3524 .group = group,
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003525 .ret = 0,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003526 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003527 smp_call_function_single(event->oncpu,
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003528 __perf_event_read, &data, 1);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003529 ret = data.ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003531 struct perf_event_context *ctx = event->ctx;
3532 unsigned long flags;
3533
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003534 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003535 /*
3536 * may read while context is not active
3537 * (e.g., thread is blocked), in that case
3538 * we cannot update context time
3539 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003540 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003541 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003542 update_cgrp_time_from_event(event);
3543 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003544 if (group)
3545 update_group_times(event);
3546 else
3547 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003548 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003549 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003550
3551 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003552}
3553
3554/*
3555 * Initialize the perf_event context in a task_struct:
3556 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003557static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003558{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003559 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003560 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003561 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003562 INIT_LIST_HEAD(&ctx->pinned_groups);
3563 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003564 INIT_LIST_HEAD(&ctx->event_list);
3565 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003566}
3567
Peter Zijlstraeb184472010-09-07 15:55:13 +02003568static struct perf_event_context *
3569alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003570{
3571 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003572
3573 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3574 if (!ctx)
3575 return NULL;
3576
3577 __perf_event_init_context(ctx);
3578 if (task) {
3579 ctx->task = task;
3580 get_task_struct(task);
3581 }
3582 ctx->pmu = pmu;
3583
3584 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003585}
3586
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003587static struct task_struct *
3588find_lively_task_by_vpid(pid_t vpid)
3589{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003590 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003592 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003593 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594 task = current;
3595 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003596 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003597 if (task)
3598 get_task_struct(task);
3599 rcu_read_unlock();
3600
3601 if (!task)
3602 return ERR_PTR(-ESRCH);
3603
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003604 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003605}
3606
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003607/*
3608 * Returns a matching context with refcount and pincount.
3609 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003610static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003611find_get_context(struct pmu *pmu, struct task_struct *task,
3612 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003613{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003614 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003615 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003616 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003617 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003618 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003619 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003620
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003621 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003622 /* Must be root to operate on a CPU event: */
3623 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3624 return ERR_PTR(-EACCES);
3625
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003626 /*
3627 * We could be clever and allow to attach a event to an
3628 * offline CPU and activate it when the CPU comes up, but
3629 * that's for later.
3630 */
3631 if (!cpu_online(cpu))
3632 return ERR_PTR(-ENODEV);
3633
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003634 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003635 ctx = &cpuctx->ctx;
3636 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003637 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003638
3639 return ctx;
3640 }
3641
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003642 err = -EINVAL;
3643 ctxn = pmu->task_ctx_nr;
3644 if (ctxn < 0)
3645 goto errout;
3646
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003647 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3648 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3649 if (!task_ctx_data) {
3650 err = -ENOMEM;
3651 goto errout;
3652 }
3653 }
3654
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003655retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003656 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003657 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003658 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003659 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003660
3661 if (task_ctx_data && !ctx->task_ctx_data) {
3662 ctx->task_ctx_data = task_ctx_data;
3663 task_ctx_data = NULL;
3664 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003665 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003666
3667 if (clone_ctx)
3668 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003669 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003670 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003671 err = -ENOMEM;
3672 if (!ctx)
3673 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003674
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003675 if (task_ctx_data) {
3676 ctx->task_ctx_data = task_ctx_data;
3677 task_ctx_data = NULL;
3678 }
3679
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003680 err = 0;
3681 mutex_lock(&task->perf_event_mutex);
3682 /*
3683 * If it has already passed perf_event_exit_task().
3684 * we must see PF_EXITING, it takes this mutex too.
3685 */
3686 if (task->flags & PF_EXITING)
3687 err = -ESRCH;
3688 else if (task->perf_event_ctxp[ctxn])
3689 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003690 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003691 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003692 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003693 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003694 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003695 mutex_unlock(&task->perf_event_mutex);
3696
3697 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003698 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003699
3700 if (err == -EAGAIN)
3701 goto retry;
3702 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003703 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003704 }
3705
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003706 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 return ctx;
3708
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003709errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003710 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711 return ERR_PTR(err);
3712}
3713
Li Zefan6fb29152009-10-15 11:21:42 +08003714static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07003715static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08003716
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717static void free_event_rcu(struct rcu_head *head)
3718{
3719 struct perf_event *event;
3720
3721 event = container_of(head, struct perf_event, rcu_head);
3722 if (event->ns)
3723 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003724 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003725 kfree(event);
3726}
3727
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003728static void ring_buffer_attach(struct perf_event *event,
3729 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003730
Kan Liangf2fb6be2016-03-23 11:24:37 -07003731static void detach_sb_event(struct perf_event *event)
3732{
3733 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
3734
3735 raw_spin_lock(&pel->lock);
3736 list_del_rcu(&event->sb_list);
3737 raw_spin_unlock(&pel->lock);
3738}
3739
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003740static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07003741{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003742 struct perf_event_attr *attr = &event->attr;
3743
Kan Liangf2fb6be2016-03-23 11:24:37 -07003744 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003745 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003746
3747 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003748 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07003749
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07003750 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
3751 attr->comm || attr->comm_exec ||
3752 attr->task ||
3753 attr->context_switch)
3754 return true;
3755 return false;
3756}
3757
3758static void unaccount_pmu_sb_event(struct perf_event *event)
3759{
3760 if (is_sb_event(event))
3761 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003762}
3763
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003764static void unaccount_event_cpu(struct perf_event *event, int cpu)
3765{
3766 if (event->parent)
3767 return;
3768
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003769 if (is_cgroup_event(event))
3770 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3771}
3772
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003773#ifdef CONFIG_NO_HZ_FULL
3774static DEFINE_SPINLOCK(nr_freq_lock);
3775#endif
3776
3777static void unaccount_freq_event_nohz(void)
3778{
3779#ifdef CONFIG_NO_HZ_FULL
3780 spin_lock(&nr_freq_lock);
3781 if (atomic_dec_and_test(&nr_freq_events))
3782 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3783 spin_unlock(&nr_freq_lock);
3784#endif
3785}
3786
3787static void unaccount_freq_event(void)
3788{
3789 if (tick_nohz_full_enabled())
3790 unaccount_freq_event_nohz();
3791 else
3792 atomic_dec(&nr_freq_events);
3793}
3794
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003795static void unaccount_event(struct perf_event *event)
3796{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003797 bool dec = false;
3798
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003799 if (event->parent)
3800 return;
3801
3802 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003803 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003804 if (event->attr.mmap || event->attr.mmap_data)
3805 atomic_dec(&nr_mmap_events);
3806 if (event->attr.comm)
3807 atomic_dec(&nr_comm_events);
3808 if (event->attr.task)
3809 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003810 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003811 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03003812 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003813 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03003814 atomic_dec(&nr_switch_events);
3815 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003816 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003817 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003818 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01003819 dec = true;
3820
Peter Zijlstra9107c892016-02-24 18:45:45 +01003821 if (dec) {
3822 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3823 schedule_delayed_work(&perf_sched_work, HZ);
3824 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003825
3826 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07003827
3828 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003829}
3830
Peter Zijlstra9107c892016-02-24 18:45:45 +01003831static void perf_sched_delayed(struct work_struct *work)
3832{
3833 mutex_lock(&perf_sched_mutex);
3834 if (atomic_dec_and_test(&perf_sched_count))
3835 static_branch_disable(&perf_sched_events);
3836 mutex_unlock(&perf_sched_mutex);
3837}
3838
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003839/*
3840 * The following implement mutual exclusion of events on "exclusive" pmus
3841 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3842 * at a time, so we disallow creating events that might conflict, namely:
3843 *
3844 * 1) cpu-wide events in the presence of per-task events,
3845 * 2) per-task events in the presence of cpu-wide events,
3846 * 3) two matching events on the same context.
3847 *
3848 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003849 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02003850 */
3851static int exclusive_event_init(struct perf_event *event)
3852{
3853 struct pmu *pmu = event->pmu;
3854
3855 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3856 return 0;
3857
3858 /*
3859 * Prevent co-existence of per-task and cpu-wide events on the
3860 * same exclusive pmu.
3861 *
3862 * Negative pmu::exclusive_cnt means there are cpu-wide
3863 * events on this "exclusive" pmu, positive means there are
3864 * per-task events.
3865 *
3866 * Since this is called in perf_event_alloc() path, event::ctx
3867 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3868 * to mean "per-task event", because unlike other attach states it
3869 * never gets cleared.
3870 */
3871 if (event->attach_state & PERF_ATTACH_TASK) {
3872 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3873 return -EBUSY;
3874 } else {
3875 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3876 return -EBUSY;
3877 }
3878
3879 return 0;
3880}
3881
3882static void exclusive_event_destroy(struct perf_event *event)
3883{
3884 struct pmu *pmu = event->pmu;
3885
3886 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3887 return;
3888
3889 /* see comment in exclusive_event_init() */
3890 if (event->attach_state & PERF_ATTACH_TASK)
3891 atomic_dec(&pmu->exclusive_cnt);
3892 else
3893 atomic_inc(&pmu->exclusive_cnt);
3894}
3895
3896static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3897{
3898 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3899 (e1->cpu == e2->cpu ||
3900 e1->cpu == -1 ||
3901 e2->cpu == -1))
3902 return true;
3903 return false;
3904}
3905
3906/* Called under the same ctx::mutex as perf_install_in_context() */
3907static bool exclusive_event_installable(struct perf_event *event,
3908 struct perf_event_context *ctx)
3909{
3910 struct perf_event *iter_event;
3911 struct pmu *pmu = event->pmu;
3912
3913 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3914 return true;
3915
3916 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3917 if (exclusive_event_match(iter_event, event))
3918 return false;
3919 }
3920
3921 return true;
3922}
3923
Alexander Shishkin375637b2016-04-27 18:44:46 +03003924static void perf_addr_filters_splice(struct perf_event *event,
3925 struct list_head *head);
3926
Peter Zijlstra683ede42014-05-05 12:11:24 +02003927static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003928{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003929 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003930
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003931 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003932
Frederic Weisbecker76369132011-05-19 19:55:04 +02003933 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003934 /*
3935 * Can happen when we close an event with re-directed output.
3936 *
3937 * Since we have a 0 refcount, perf_mmap_close() will skip
3938 * over us; possibly making our ring_buffer_put() the last.
3939 */
3940 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003941 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003942 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003943 }
3944
Stephane Eraniane5d13672011-02-14 11:20:01 +02003945 if (is_cgroup_event(event))
3946 perf_detach_cgroup(event);
3947
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003948 if (!event->parent) {
3949 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3950 put_callchain_buffers();
3951 }
3952
3953 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003954 perf_addr_filters_splice(event, NULL);
3955 kfree(event->addr_filters_offs);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003956
3957 if (event->destroy)
3958 event->destroy(event);
3959
3960 if (event->ctx)
3961 put_ctx(event->ctx);
3962
Alexander Shishkin62a92c82016-06-07 15:44:15 +03003963 exclusive_event_destroy(event);
3964 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01003965
3966 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003967}
3968
Peter Zijlstra683ede42014-05-05 12:11:24 +02003969/*
3970 * Used to free events which have a known refcount of 1, such as in error paths
3971 * where the event isn't exposed yet and inherited events.
3972 */
3973static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003974{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003975 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3976 "unexpected event refcount: %ld; ptr=%p\n",
3977 atomic_long_read(&event->refcount), event)) {
3978 /* leak to avoid use-after-free */
3979 return;
3980 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003981
Peter Zijlstra683ede42014-05-05 12:11:24 +02003982 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003983}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003984
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003985/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003986 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003987 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003988static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003989{
Peter Zijlstra88821352010-11-09 19:01:43 +01003990 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003991
Peter Zijlstra88821352010-11-09 19:01:43 +01003992 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01003993 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003994 * Matches the smp_store_release() in perf_event_exit_task(). If we
3995 * observe !owner it means the list deletion is complete and we can
3996 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01003997 * owner->perf_event_mutex.
3998 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01003999 owner = lockless_dereference(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004000 if (owner) {
4001 /*
4002 * Since delayed_put_task_struct() also drops the last
4003 * task reference we can safely take a new reference
4004 * while holding the rcu_read_lock().
4005 */
4006 get_task_struct(owner);
4007 }
4008 rcu_read_unlock();
4009
4010 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004011 /*
4012 * If we're here through perf_event_exit_task() we're already
4013 * holding ctx->mutex which would be an inversion wrt. the
4014 * normal lock order.
4015 *
4016 * However we can safely take this lock because its the child
4017 * ctx->mutex.
4018 */
4019 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4020
Peter Zijlstra88821352010-11-09 19:01:43 +01004021 /*
4022 * We have to re-check the event->owner field, if it is cleared
4023 * we raced with perf_event_exit_task(), acquiring the mutex
4024 * ensured they're done, and we can proceed with freeing the
4025 * event.
4026 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004027 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004028 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004029 smp_store_release(&event->owner, NULL);
4030 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004031 mutex_unlock(&owner->perf_event_mutex);
4032 put_task_struct(owner);
4033 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004034}
4035
Jiri Olsaf8697762014-08-01 14:33:01 +02004036static void put_event(struct perf_event *event)
4037{
Jiri Olsaf8697762014-08-01 14:33:01 +02004038 if (!atomic_long_dec_and_test(&event->refcount))
4039 return;
4040
Peter Zijlstra683ede42014-05-05 12:11:24 +02004041 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004042}
4043
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004044/*
4045 * Kill an event dead; while event:refcount will preserve the event
4046 * object, it will not preserve its functionality. Once the last 'user'
4047 * gives up the object, we'll destroy the thing.
4048 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004049int perf_event_release_kernel(struct perf_event *event)
4050{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004051 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004052 struct perf_event *child, *tmp;
4053
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004054 /*
4055 * If we got here through err_file: fput(event_file); we will not have
4056 * attached to a context yet.
4057 */
4058 if (!ctx) {
4059 WARN_ON_ONCE(event->attach_state &
4060 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4061 goto no_ctx;
4062 }
4063
Peter Zijlstra88821352010-11-09 19:01:43 +01004064 if (!is_kernel_event(event))
4065 perf_remove_from_owner(event);
4066
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004067 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004068 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004069 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004070
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004071 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004072 /*
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004073 * Mark this even as STATE_DEAD, there is no external reference to it
4074 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004075 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004076 * Anybody acquiring event->child_mutex after the below loop _must_
4077 * also see this, most importantly inherit_event() which will avoid
4078 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004079 *
4080 * Thus this guarantees that we will in fact observe and kill _ALL_
4081 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004082 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004083 event->state = PERF_EVENT_STATE_DEAD;
4084 raw_spin_unlock_irq(&ctx->lock);
4085
4086 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004087
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004088again:
4089 mutex_lock(&event->child_mutex);
4090 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004091
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004092 /*
4093 * Cannot change, child events are not migrated, see the
4094 * comment with perf_event_ctx_lock_nested().
4095 */
4096 ctx = lockless_dereference(child->ctx);
4097 /*
4098 * Since child_mutex nests inside ctx::mutex, we must jump
4099 * through hoops. We start by grabbing a reference on the ctx.
4100 *
4101 * Since the event cannot get freed while we hold the
4102 * child_mutex, the context must also exist and have a !0
4103 * reference count.
4104 */
4105 get_ctx(ctx);
4106
4107 /*
4108 * Now that we have a ctx ref, we can drop child_mutex, and
4109 * acquire ctx::mutex without fear of it going away. Then we
4110 * can re-acquire child_mutex.
4111 */
4112 mutex_unlock(&event->child_mutex);
4113 mutex_lock(&ctx->mutex);
4114 mutex_lock(&event->child_mutex);
4115
4116 /*
4117 * Now that we hold ctx::mutex and child_mutex, revalidate our
4118 * state, if child is still the first entry, it didn't get freed
4119 * and we can continue doing so.
4120 */
4121 tmp = list_first_entry_or_null(&event->child_list,
4122 struct perf_event, child_list);
4123 if (tmp == child) {
4124 perf_remove_from_context(child, DETACH_GROUP);
4125 list_del(&child->child_list);
4126 free_event(child);
4127 /*
4128 * This matches the refcount bump in inherit_event();
4129 * this can't be the last reference.
4130 */
4131 put_event(event);
4132 }
4133
4134 mutex_unlock(&event->child_mutex);
4135 mutex_unlock(&ctx->mutex);
4136 put_ctx(ctx);
4137 goto again;
4138 }
4139 mutex_unlock(&event->child_mutex);
4140
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004141no_ctx:
4142 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004143 return 0;
4144}
4145EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4146
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004147/*
4148 * Called when the last reference to the file is gone.
4149 */
Al Viroa6fa9412012-08-20 14:59:25 +01004150static int perf_release(struct inode *inode, struct file *file)
4151{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004152 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004153 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004154}
4155
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004156u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004157{
4158 struct perf_event *child;
4159 u64 total = 0;
4160
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004161 *enabled = 0;
4162 *running = 0;
4163
Peter Zijlstra6f105812009-11-20 22:19:56 +01004164 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004165
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004166 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004167 total += perf_event_count(event);
4168
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004169 *enabled += event->total_time_enabled +
4170 atomic64_read(&event->child_total_time_enabled);
4171 *running += event->total_time_running +
4172 atomic64_read(&event->child_total_time_running);
4173
4174 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004175 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004176 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004177 *enabled += child->total_time_enabled;
4178 *running += child->total_time_running;
4179 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004180 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004181
4182 return total;
4183}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004184EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004186static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004187 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004188{
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004189 struct perf_event *sub;
4190 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004191 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004192
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004193 ret = perf_event_read(leader, true);
4194 if (ret)
4195 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004196
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004197 /*
4198 * Since we co-schedule groups, {enabled,running} times of siblings
4199 * will be identical to those of the leader, so we only publish one
4200 * set.
4201 */
4202 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4203 values[n++] += leader->total_time_enabled +
4204 atomic64_read(&leader->child_total_time_enabled);
4205 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004206
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004207 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4208 values[n++] += leader->total_time_running +
4209 atomic64_read(&leader->child_total_time_running);
4210 }
4211
4212 /*
4213 * Write {count,id} tuples for every sibling.
4214 */
4215 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004216 if (read_format & PERF_FORMAT_ID)
4217 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004220 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004221 if (read_format & PERF_FORMAT_ID)
4222 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004223 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004224
4225 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004226}
4227
4228static int perf_read_group(struct perf_event *event,
4229 u64 read_format, char __user *buf)
4230{
4231 struct perf_event *leader = event->group_leader, *child;
4232 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004233 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004234 u64 *values;
4235
4236 lockdep_assert_held(&ctx->mutex);
4237
4238 values = kzalloc(event->read_size, GFP_KERNEL);
4239 if (!values)
4240 return -ENOMEM;
4241
4242 values[0] = 1 + leader->nr_siblings;
4243
4244 /*
4245 * By locking the child_mutex of the leader we effectively
4246 * lock the child list of all siblings.. XXX explain how.
4247 */
4248 mutex_lock(&leader->child_mutex);
4249
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004250 ret = __perf_read_group_add(leader, read_format, values);
4251 if (ret)
4252 goto unlock;
4253
4254 list_for_each_entry(child, &leader->child_list, child_list) {
4255 ret = __perf_read_group_add(child, read_format, values);
4256 if (ret)
4257 goto unlock;
4258 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004259
4260 mutex_unlock(&leader->child_mutex);
4261
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004262 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004263 if (copy_to_user(buf, values, event->read_size))
4264 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004265 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004266
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004267unlock:
4268 mutex_unlock(&leader->child_mutex);
4269out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004270 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004271 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004272}
4273
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004274static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275 u64 read_format, char __user *buf)
4276{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004277 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004278 u64 values[4];
4279 int n = 0;
4280
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004281 values[n++] = perf_event_read_value(event, &enabled, &running);
4282 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4283 values[n++] = enabled;
4284 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4285 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004286 if (read_format & PERF_FORMAT_ID)
4287 values[n++] = primary_event_id(event);
4288
4289 if (copy_to_user(buf, values, n * sizeof(u64)))
4290 return -EFAULT;
4291
4292 return n * sizeof(u64);
4293}
4294
Jiri Olsadc633982014-09-12 13:18:26 +02004295static bool is_event_hup(struct perf_event *event)
4296{
4297 bool no_children;
4298
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004299 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004300 return false;
4301
4302 mutex_lock(&event->child_mutex);
4303 no_children = list_empty(&event->child_list);
4304 mutex_unlock(&event->child_mutex);
4305 return no_children;
4306}
4307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004308/*
4309 * Read the performance event - simple non blocking version for now
4310 */
4311static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004312__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004313{
4314 u64 read_format = event->attr.read_format;
4315 int ret;
4316
4317 /*
4318 * Return end-of-file for a read on a event that is in
4319 * error state (i.e. because it was pinned but it couldn't be
4320 * scheduled on to the CPU at some point).
4321 */
4322 if (event->state == PERF_EVENT_STATE_ERROR)
4323 return 0;
4324
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004325 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326 return -ENOSPC;
4327
4328 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004329 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004330 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004331 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004332 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004333
4334 return ret;
4335}
4336
4337static ssize_t
4338perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4339{
4340 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004341 struct perf_event_context *ctx;
4342 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004344 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004345 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004346 perf_event_ctx_unlock(event, ctx);
4347
4348 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004349}
4350
4351static unsigned int perf_poll(struct file *file, poll_table *wait)
4352{
4353 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004354 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02004355 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004356
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02004357 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04004358
Jiri Olsadc633982014-09-12 13:18:26 +02004359 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04004360 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004361
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004362 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004363 * Pin the event->rb by taking event->mmap_mutex; otherwise
4364 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004365 */
4366 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004367 rb = event->rb;
4368 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004369 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004370 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004371 return events;
4372}
4373
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004374static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004375{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004376 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004377 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004378 perf_event_update_userpage(event);
4379}
4380
4381/*
4382 * Holding the top-level event's child_mutex means that any
4383 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01004384 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004385 * task existence requirements of perf_event_enable/disable.
4386 */
4387static void perf_event_for_each_child(struct perf_event *event,
4388 void (*func)(struct perf_event *))
4389{
4390 struct perf_event *child;
4391
4392 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004393
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004394 mutex_lock(&event->child_mutex);
4395 func(event);
4396 list_for_each_entry(child, &event->child_list, child_list)
4397 func(child);
4398 mutex_unlock(&event->child_mutex);
4399}
4400
4401static void perf_event_for_each(struct perf_event *event,
4402 void (*func)(struct perf_event *))
4403{
4404 struct perf_event_context *ctx = event->ctx;
4405 struct perf_event *sibling;
4406
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004407 lockdep_assert_held(&ctx->mutex);
4408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409 event = event->group_leader;
4410
4411 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004412 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10004413 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004414}
4415
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004416static void __perf_event_period(struct perf_event *event,
4417 struct perf_cpu_context *cpuctx,
4418 struct perf_event_context *ctx,
4419 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01004420{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004421 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004422 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004425 event->attr.sample_freq = value;
4426 } else {
4427 event->attr.sample_period = value;
4428 event->hw.sample_period = value;
4429 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004430
4431 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4432 if (active) {
4433 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01004434 /*
4435 * We could be throttled; unthrottle now to avoid the tick
4436 * trying to unthrottle while we already re-started the event.
4437 */
4438 if (event->hw.interrupts == MAX_INTERRUPTS) {
4439 event->hw.interrupts = 0;
4440 perf_log_throttle(event, 1);
4441 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00004442 event->pmu->stop(event, PERF_EF_UPDATE);
4443 }
4444
4445 local64_set(&event->hw.period_left, 0);
4446
4447 if (active) {
4448 event->pmu->start(event, PERF_EF_RELOAD);
4449 perf_pmu_enable(ctx->pmu);
4450 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004451}
4452
4453static int perf_event_period(struct perf_event *event, u64 __user *arg)
4454{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004455 u64 value;
4456
4457 if (!is_sampling_event(event))
4458 return -EINVAL;
4459
4460 if (copy_from_user(&value, arg, sizeof(value)))
4461 return -EFAULT;
4462
4463 if (!value)
4464 return -EINVAL;
4465
4466 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4467 return -EINVAL;
4468
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01004469 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004470
Peter Zijlstrac7999c62015-08-04 19:22:49 +02004471 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004472}
4473
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004474static const struct file_operations perf_fops;
4475
Al Viro2903ff02012-08-28 12:52:22 -04004476static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004477{
Al Viro2903ff02012-08-28 12:52:22 -04004478 struct fd f = fdget(fd);
4479 if (!f.file)
4480 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004481
Al Viro2903ff02012-08-28 12:52:22 -04004482 if (f.file->f_op != &perf_fops) {
4483 fdput(f);
4484 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004485 }
Al Viro2903ff02012-08-28 12:52:22 -04004486 *p = f;
4487 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004488}
4489
4490static int perf_event_set_output(struct perf_event *event,
4491 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08004492static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004493static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004495static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004496{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497 void (*func)(struct perf_event *);
4498 u32 flags = arg;
4499
4500 switch (cmd) {
4501 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004502 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004503 break;
4504 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004505 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506 break;
4507 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004508 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004509 break;
4510
4511 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004512 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513
4514 case PERF_EVENT_IOC_PERIOD:
4515 return perf_event_period(event, (u64 __user *)arg);
4516
Jiri Olsacf4957f2012-10-24 13:37:58 +02004517 case PERF_EVENT_IOC_ID:
4518 {
4519 u64 id = primary_event_id(event);
4520
4521 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4522 return -EFAULT;
4523 return 0;
4524 }
4525
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004527 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004528 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004529 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004530 struct perf_event *output_event;
4531 struct fd output;
4532 ret = perf_fget_light(arg, &output);
4533 if (ret)
4534 return ret;
4535 output_event = output.file->private_data;
4536 ret = perf_event_set_output(event, output_event);
4537 fdput(output);
4538 } else {
4539 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004540 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004541 return ret;
4542 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004543
Li Zefan6fb29152009-10-15 11:21:42 +08004544 case PERF_EVENT_IOC_SET_FILTER:
4545 return perf_event_set_filter(event, (void __user *)arg);
4546
Alexei Starovoitov25415172015-03-25 12:49:20 -07004547 case PERF_EVENT_IOC_SET_BPF:
4548 return perf_event_set_bpf_prog(event, arg);
4549
Wang Nan86e79722016-03-28 06:41:29 +00004550 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4551 struct ring_buffer *rb;
4552
4553 rcu_read_lock();
4554 rb = rcu_dereference(event->rb);
4555 if (!rb || !rb->nr_pages) {
4556 rcu_read_unlock();
4557 return -EINVAL;
4558 }
4559 rb_toggle_paused(rb, !!arg);
4560 rcu_read_unlock();
4561 return 0;
4562 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004563 default:
4564 return -ENOTTY;
4565 }
4566
4567 if (flags & PERF_IOC_FLAG_GROUP)
4568 perf_event_for_each(event, func);
4569 else
4570 perf_event_for_each_child(event, func);
4571
4572 return 0;
4573}
4574
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004575static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4576{
4577 struct perf_event *event = file->private_data;
4578 struct perf_event_context *ctx;
4579 long ret;
4580
4581 ctx = perf_event_ctx_lock(event);
4582 ret = _perf_ioctl(event, cmd, arg);
4583 perf_event_ctx_unlock(event, ctx);
4584
4585 return ret;
4586}
4587
Pawel Mollb3f20782014-06-13 16:03:32 +01004588#ifdef CONFIG_COMPAT
4589static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4590 unsigned long arg)
4591{
4592 switch (_IOC_NR(cmd)) {
4593 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4594 case _IOC_NR(PERF_EVENT_IOC_ID):
4595 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4596 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4597 cmd &= ~IOCSIZE_MASK;
4598 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4599 }
4600 break;
4601 }
4602 return perf_ioctl(file, cmd, arg);
4603}
4604#else
4605# define perf_compat_ioctl NULL
4606#endif
4607
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004608int perf_event_task_enable(void)
4609{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004610 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004611 struct perf_event *event;
4612
4613 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004614 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4615 ctx = perf_event_ctx_lock(event);
4616 perf_event_for_each_child(event, _perf_event_enable);
4617 perf_event_ctx_unlock(event, ctx);
4618 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004619 mutex_unlock(&current->perf_event_mutex);
4620
4621 return 0;
4622}
4623
4624int perf_event_task_disable(void)
4625{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004626 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004627 struct perf_event *event;
4628
4629 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004630 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4631 ctx = perf_event_ctx_lock(event);
4632 perf_event_for_each_child(event, _perf_event_disable);
4633 perf_event_ctx_unlock(event, ctx);
4634 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004635 mutex_unlock(&current->perf_event_mutex);
4636
4637 return 0;
4638}
4639
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004640static int perf_event_index(struct perf_event *event)
4641{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004642 if (event->hw.state & PERF_HES_STOPPED)
4643 return 0;
4644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004645 if (event->state != PERF_EVENT_STATE_ACTIVE)
4646 return 0;
4647
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004648 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004649}
4650
Eric B Munsonc4794292011-06-23 16:34:38 -04004651static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004652 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004653 u64 *enabled,
4654 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004655{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004656 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004657
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004658 *now = perf_clock();
4659 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004660 *enabled = ctx_time - event->tstamp_enabled;
4661 *running = ctx_time - event->tstamp_running;
4662}
4663
Peter Zijlstrafa731582013-09-19 10:16:42 +02004664static void perf_event_init_userpage(struct perf_event *event)
4665{
4666 struct perf_event_mmap_page *userpg;
4667 struct ring_buffer *rb;
4668
4669 rcu_read_lock();
4670 rb = rcu_dereference(event->rb);
4671 if (!rb)
4672 goto unlock;
4673
4674 userpg = rb->user_page;
4675
4676 /* Allow new userspace to detect that bit 0 is deprecated */
4677 userpg->cap_bit0_is_deprecated = 1;
4678 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02004679 userpg->data_offset = PAGE_SIZE;
4680 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa731582013-09-19 10:16:42 +02004681
4682unlock:
4683 rcu_read_unlock();
4684}
4685
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004686void __weak arch_perf_update_userpage(
4687 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004688{
4689}
4690
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004691/*
4692 * Callers need to ensure there can be no nesting of this function, otherwise
4693 * the seqlock logic goes bad. We can not serialize this because the arch
4694 * code calls this from NMI context.
4695 */
4696void perf_event_update_userpage(struct perf_event *event)
4697{
4698 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004699 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004700 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004701
4702 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004703 rb = rcu_dereference(event->rb);
4704 if (!rb)
4705 goto unlock;
4706
Eric B Munson0d641202011-06-24 12:26:26 -04004707 /*
4708 * compute total_time_enabled, total_time_running
4709 * based on snapshot values taken when the event
4710 * was last scheduled in.
4711 *
4712 * we cannot simply called update_context_time()
4713 * because of locking issue as we can be called in
4714 * NMI context
4715 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004716 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004717
Frederic Weisbecker76369132011-05-19 19:55:04 +02004718 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004719 /*
4720 * Disable preemption so as to not let the corresponding user-space
4721 * spin too long if we get preempted.
4722 */
4723 preempt_disable();
4724 ++userpg->lock;
4725 barrier();
4726 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004727 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004728 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004729 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730
Eric B Munson0d641202011-06-24 12:26:26 -04004731 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732 atomic64_read(&event->child_total_time_enabled);
4733
Eric B Munson0d641202011-06-24 12:26:26 -04004734 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004735 atomic64_read(&event->child_total_time_running);
4736
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004737 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004739 barrier();
4740 ++userpg->lock;
4741 preempt_enable();
4742unlock:
4743 rcu_read_unlock();
4744}
4745
Peter Zijlstra906010b2009-09-21 16:08:49 +02004746static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4747{
4748 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004749 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004750 int ret = VM_FAULT_SIGBUS;
4751
4752 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4753 if (vmf->pgoff == 0)
4754 ret = 0;
4755 return ret;
4756 }
4757
4758 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004759 rb = rcu_dereference(event->rb);
4760 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004761 goto unlock;
4762
4763 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4764 goto unlock;
4765
Frederic Weisbecker76369132011-05-19 19:55:04 +02004766 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004767 if (!vmf->page)
4768 goto unlock;
4769
4770 get_page(vmf->page);
4771 vmf->page->mapping = vma->vm_file->f_mapping;
4772 vmf->page->index = vmf->pgoff;
4773
4774 ret = 0;
4775unlock:
4776 rcu_read_unlock();
4777
4778 return ret;
4779}
4780
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004781static void ring_buffer_attach(struct perf_event *event,
4782 struct ring_buffer *rb)
4783{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004784 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004785 unsigned long flags;
4786
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004787 if (event->rb) {
4788 /*
4789 * Should be impossible, we set this when removing
4790 * event->rb_entry and wait/clear when adding event->rb_entry.
4791 */
4792 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004793
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004794 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004795 spin_lock_irqsave(&old_rb->event_lock, flags);
4796 list_del_rcu(&event->rb_entry);
4797 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004798
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004799 event->rcu_batches = get_state_synchronize_rcu();
4800 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004801 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004802
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004803 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02004804 if (event->rcu_pending) {
4805 cond_synchronize_rcu(event->rcu_batches);
4806 event->rcu_pending = 0;
4807 }
4808
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004809 spin_lock_irqsave(&rb->event_lock, flags);
4810 list_add_rcu(&event->rb_entry, &rb->event_list);
4811 spin_unlock_irqrestore(&rb->event_lock, flags);
4812 }
4813
4814 rcu_assign_pointer(event->rb, rb);
4815
4816 if (old_rb) {
4817 ring_buffer_put(old_rb);
4818 /*
4819 * Since we detached before setting the new rb, so that we
4820 * could attach the new rb, we could have missed a wakeup.
4821 * Provide it now.
4822 */
4823 wake_up_all(&event->waitq);
4824 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004825}
4826
4827static void ring_buffer_wakeup(struct perf_event *event)
4828{
4829 struct ring_buffer *rb;
4830
4831 rcu_read_lock();
4832 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004833 if (rb) {
4834 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4835 wake_up_all(&event->waitq);
4836 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004837 rcu_read_unlock();
4838}
4839
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004840struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004841{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004842 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004844 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004845 rb = rcu_dereference(event->rb);
4846 if (rb) {
4847 if (!atomic_inc_not_zero(&rb->refcount))
4848 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004849 }
4850 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851
Frederic Weisbecker76369132011-05-19 19:55:04 +02004852 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004853}
4854
Alexander Shishkinfdc26702015-01-14 14:18:16 +02004855void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004856{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004857 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004858 return;
4859
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004860 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004861
Frederic Weisbecker76369132011-05-19 19:55:04 +02004862 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863}
4864
4865static void perf_mmap_open(struct vm_area_struct *vma)
4866{
4867 struct perf_event *event = vma->vm_file->private_data;
4868
4869 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004870 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004871
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004872 if (vma->vm_pgoff)
4873 atomic_inc(&event->rb->aux_mmap_count);
4874
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004875 if (event->pmu->event_mapped)
4876 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877}
4878
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004879static void perf_pmu_output_stop(struct perf_event *event);
4880
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004881/*
4882 * A buffer can be mmap()ed multiple times; either directly through the same
4883 * event, or through other events by use of perf_event_set_output().
4884 *
4885 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4886 * the buffer here, where we still have a VM context. This means we need
4887 * to detach all events redirecting to us.
4888 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889static void perf_mmap_close(struct vm_area_struct *vma)
4890{
4891 struct perf_event *event = vma->vm_file->private_data;
4892
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004893 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004894 struct user_struct *mmap_user = rb->mmap_user;
4895 int mmap_locked = rb->mmap_locked;
4896 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004897
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004898 if (event->pmu->event_unmapped)
4899 event->pmu->event_unmapped(event);
4900
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004901 /*
4902 * rb->aux_mmap_count will always drop before rb->mmap_count and
4903 * event->mmap_count, so it is ok to use event->mmap_mutex to
4904 * serialize with perf_mmap here.
4905 */
4906 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4907 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004908 /*
4909 * Stop all AUX events that are writing to this buffer,
4910 * so that we can free its AUX pages and corresponding PMU
4911 * data. Note that after rb::aux_mmap_count dropped to zero,
4912 * they won't start any more (see perf_aux_output_begin()).
4913 */
4914 perf_pmu_output_stop(event);
4915
4916 /* now it's safe to free the pages */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004917 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4918 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4919
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004920 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004921 rb_free_aux(rb);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02004922 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
4923
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02004924 mutex_unlock(&event->mmap_mutex);
4925 }
4926
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004927 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004928
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004929 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004930 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004931
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004932 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004933 mutex_unlock(&event->mmap_mutex);
4934
4935 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004936 if (atomic_read(&rb->mmap_count))
4937 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004938
4939 /*
4940 * No other mmap()s, detach from all other events that might redirect
4941 * into the now unreachable buffer. Somewhat complicated by the
4942 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4943 */
4944again:
4945 rcu_read_lock();
4946 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4947 if (!atomic_long_inc_not_zero(&event->refcount)) {
4948 /*
4949 * This event is en-route to free_event() which will
4950 * detach it and remove it from the list.
4951 */
4952 continue;
4953 }
4954 rcu_read_unlock();
4955
4956 mutex_lock(&event->mmap_mutex);
4957 /*
4958 * Check we didn't race with perf_event_set_output() which can
4959 * swizzle the rb from under us while we were waiting to
4960 * acquire mmap_mutex.
4961 *
4962 * If we find a different rb; ignore this event, a next
4963 * iteration will no longer find it on the list. We have to
4964 * still restart the iteration to make sure we're not now
4965 * iterating the wrong list.
4966 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004967 if (event->rb == rb)
4968 ring_buffer_attach(event, NULL);
4969
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004970 mutex_unlock(&event->mmap_mutex);
4971 put_event(event);
4972
4973 /*
4974 * Restart the iteration; either we're on the wrong list or
4975 * destroyed its integrity by doing a deletion.
4976 */
4977 goto again;
4978 }
4979 rcu_read_unlock();
4980
4981 /*
4982 * It could be there's still a few 0-ref events on the list; they'll
4983 * get cleaned up by free_event() -- they'll also still have their
4984 * ref on the rb and will free it whenever they are done with it.
4985 *
4986 * Aside from that, this buffer is 'fully' detached and unmapped,
4987 * undo the VM accounting.
4988 */
4989
4990 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4991 vma->vm_mm->pinned_vm -= mmap_locked;
4992 free_uid(mmap_user);
4993
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004994out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004995 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004996}
4997
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004998static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004999 .open = perf_mmap_open,
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005000 .close = perf_mmap_close, /* non mergable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001 .fault = perf_mmap_fault,
5002 .page_mkwrite = perf_mmap_fault,
5003};
5004
5005static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5006{
5007 struct perf_event *event = file->private_data;
5008 unsigned long user_locked, user_lock_limit;
5009 struct user_struct *user = current_user();
5010 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005011 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012 unsigned long vma_size;
5013 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005014 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005015 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005016
Peter Zijlstrac7920612010-05-18 10:33:24 +02005017 /*
5018 * Don't allow mmap() of inherited per-task counters. This would
5019 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005020 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005021 */
5022 if (event->cpu == -1 && event->attr.inherit)
5023 return -EINVAL;
5024
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025 if (!(vma->vm_flags & VM_SHARED))
5026 return -EINVAL;
5027
5028 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005029
5030 if (vma->vm_pgoff == 0) {
5031 nr_pages = (vma_size / PAGE_SIZE) - 1;
5032 } else {
5033 /*
5034 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5035 * mapped, all subsequent mappings should have the same size
5036 * and offset. Must be above the normal perf buffer.
5037 */
5038 u64 aux_offset, aux_size;
5039
5040 if (!event->rb)
5041 return -EINVAL;
5042
5043 nr_pages = vma_size / PAGE_SIZE;
5044
5045 mutex_lock(&event->mmap_mutex);
5046 ret = -EINVAL;
5047
5048 rb = event->rb;
5049 if (!rb)
5050 goto aux_unlock;
5051
5052 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
5053 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
5054
5055 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5056 goto aux_unlock;
5057
5058 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5059 goto aux_unlock;
5060
5061 /* already mapped with a different offset */
5062 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5063 goto aux_unlock;
5064
5065 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5066 goto aux_unlock;
5067
5068 /* already mapped with a different size */
5069 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5070 goto aux_unlock;
5071
5072 if (!is_power_of_2(nr_pages))
5073 goto aux_unlock;
5074
5075 if (!atomic_inc_not_zero(&rb->mmap_count))
5076 goto aux_unlock;
5077
5078 if (rb_has_aux(rb)) {
5079 atomic_inc(&rb->aux_mmap_count);
5080 ret = 0;
5081 goto unlock;
5082 }
5083
5084 atomic_set(&rb->aux_mmap_count, 1);
5085 user_extra = nr_pages;
5086
5087 goto accounting;
5088 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005089
5090 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005091 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005092 * can do bitmasks instead of modulo.
5093 */
Kan Liang2ed11312015-03-02 02:14:26 -05005094 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005095 return -EINVAL;
5096
5097 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5098 return -EINVAL;
5099
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005100 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005101again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005102 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005103 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005104 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005105 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005106 goto unlock;
5107 }
5108
5109 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5110 /*
5111 * Raced against perf_mmap_close() through
5112 * perf_event_set_output(). Try again, hope for better
5113 * luck.
5114 */
5115 mutex_unlock(&event->mmap_mutex);
5116 goto again;
5117 }
5118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005119 goto unlock;
5120 }
5121
5122 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005123
5124accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005125 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5126
5127 /*
5128 * Increase the limit linearly with more CPUs:
5129 */
5130 user_lock_limit *= num_online_cpus();
5131
5132 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
5133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134 if (user_locked > user_lock_limit)
5135 extra = user_locked - user_lock_limit;
5136
Jiri Slaby78d7d402010-03-05 13:42:54 -08005137 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005138 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07005139 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005140
5141 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5142 !capable(CAP_IPC_LOCK)) {
5143 ret = -EPERM;
5144 goto unlock;
5145 }
5146
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005147 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005148
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005149 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005150 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005151
Frederic Weisbecker76369132011-05-19 19:55:04 +02005152 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005153 rb = rb_alloc(nr_pages,
5154 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5155 event->cpu, flags);
5156
5157 if (!rb) {
5158 ret = -ENOMEM;
5159 goto unlock;
5160 }
5161
5162 atomic_set(&rb->mmap_count, 1);
5163 rb->mmap_user = get_current_user();
5164 rb->mmap_locked = extra;
5165
5166 ring_buffer_attach(event, rb);
5167
5168 perf_event_init_userpage(event);
5169 perf_event_update_userpage(event);
5170 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005171 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5172 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005173 if (!ret)
5174 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005175 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005178 if (!ret) {
5179 atomic_long_add(user_extra, &user->locked_vm);
5180 vma->vm_mm->pinned_vm += extra;
5181
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005182 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005183 } else if (rb) {
5184 atomic_dec(&rb->mmap_count);
5185 }
5186aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005187 mutex_unlock(&event->mmap_mutex);
5188
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005189 /*
5190 * Since pinned accounting is per vm we cannot allow fork() to copy our
5191 * vma.
5192 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005193 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005194 vma->vm_ops = &perf_mmap_vmops;
5195
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005196 if (event->pmu->event_mapped)
5197 event->pmu->event_mapped(event);
5198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005199 return ret;
5200}
5201
5202static int perf_fasync(int fd, struct file *filp, int on)
5203{
Al Viro496ad9a2013-01-23 17:07:38 -05005204 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205 struct perf_event *event = filp->private_data;
5206 int retval;
5207
Al Viro59551022016-01-22 15:40:57 -05005208 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05005210 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005211
5212 if (retval < 0)
5213 return retval;
5214
5215 return 0;
5216}
5217
5218static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01005219 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220 .release = perf_release,
5221 .read = perf_read,
5222 .poll = perf_poll,
5223 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01005224 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005225 .mmap = perf_mmap,
5226 .fasync = perf_fasync,
5227};
5228
5229/*
5230 * Perf event wakeup
5231 *
5232 * If there's data, ensure we set the poll() state and publish everything
5233 * to user-space before waking everybody up.
5234 */
5235
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005236static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5237{
5238 /* only the parent has fasync state */
5239 if (event->parent)
5240 event = event->parent;
5241 return &event->fasync;
5242}
5243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005244void perf_event_wakeup(struct perf_event *event)
5245{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005246 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247
5248 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02005249 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250 event->pending_kill = 0;
5251 }
5252}
5253
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005254static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005255{
5256 struct perf_event *event = container_of(entry,
5257 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01005258 int rctx;
5259
5260 rctx = perf_swevent_get_recursion_context();
5261 /*
5262 * If we 'fail' here, that's OK, it means recursion is already disabled
5263 * and we won't recurse 'further'.
5264 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265
5266 if (event->pending_disable) {
5267 event->pending_disable = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005268 perf_event_disable_local(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269 }
5270
5271 if (event->pending_wakeup) {
5272 event->pending_wakeup = 0;
5273 perf_event_wakeup(event);
5274 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01005275
5276 if (rctx >= 0)
5277 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278}
5279
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005280/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005281 * We assume there is only KVM supporting the callbacks.
5282 * Later on, we might change it to a list if there is
5283 * another virtualization implementation supporting the callbacks.
5284 */
5285struct perf_guest_info_callbacks *perf_guest_cbs;
5286
5287int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5288{
5289 perf_guest_cbs = cbs;
5290 return 0;
5291}
5292EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5293
5294int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5295{
5296 perf_guest_cbs = NULL;
5297 return 0;
5298}
5299EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5300
Jiri Olsa40189942012-08-07 15:20:37 +02005301static void
5302perf_output_sample_regs(struct perf_output_handle *handle,
5303 struct pt_regs *regs, u64 mask)
5304{
5305 int bit;
5306
5307 for_each_set_bit(bit, (const unsigned long *) &mask,
5308 sizeof(mask) * BITS_PER_BYTE) {
5309 u64 val;
5310
5311 val = perf_reg_value(regs, bit);
5312 perf_output_put(handle, val);
5313 }
5314}
5315
Stephane Eranian60e23642014-09-24 13:48:37 +02005316static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005317 struct pt_regs *regs,
5318 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02005319{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005320 if (user_mode(regs)) {
5321 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02005322 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005323 } else if (current->mm) {
5324 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005325 } else {
5326 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5327 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02005328 }
5329}
5330
Stephane Eranian60e23642014-09-24 13:48:37 +02005331static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5332 struct pt_regs *regs)
5333{
5334 regs_intr->regs = regs;
5335 regs_intr->abi = perf_reg_abi(current);
5336}
5337
5338
Jiri Olsac5ebced2012-08-07 15:20:40 +02005339/*
5340 * Get remaining task size from user stack pointer.
5341 *
5342 * It'd be better to take stack vma map and limit this more
5343 * precisly, but there's no way to get it safely under interrupt,
5344 * so using TASK_SIZE as limit.
5345 */
5346static u64 perf_ustack_task_size(struct pt_regs *regs)
5347{
5348 unsigned long addr = perf_user_stack_pointer(regs);
5349
5350 if (!addr || addr >= TASK_SIZE)
5351 return 0;
5352
5353 return TASK_SIZE - addr;
5354}
5355
5356static u16
5357perf_sample_ustack_size(u16 stack_size, u16 header_size,
5358 struct pt_regs *regs)
5359{
5360 u64 task_size;
5361
5362 /* No regs, no stack pointer, no dump. */
5363 if (!regs)
5364 return 0;
5365
5366 /*
5367 * Check if we fit in with the requested stack size into the:
5368 * - TASK_SIZE
5369 * If we don't, we limit the size to the TASK_SIZE.
5370 *
5371 * - remaining sample size
5372 * If we don't, we customize the stack size to
5373 * fit in to the remaining sample size.
5374 */
5375
5376 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5377 stack_size = min(stack_size, (u16) task_size);
5378
5379 /* Current header size plus static size and dynamic size. */
5380 header_size += 2 * sizeof(u64);
5381
5382 /* Do we fit in with the current stack dump size? */
5383 if ((u16) (header_size + stack_size) < header_size) {
5384 /*
5385 * If we overflow the maximum size for the sample,
5386 * we customize the stack dump size to fit in.
5387 */
5388 stack_size = USHRT_MAX - header_size - sizeof(u64);
5389 stack_size = round_up(stack_size, sizeof(u64));
5390 }
5391
5392 return stack_size;
5393}
5394
5395static void
5396perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5397 struct pt_regs *regs)
5398{
5399 /* Case of a kernel thread, nothing to dump */
5400 if (!regs) {
5401 u64 size = 0;
5402 perf_output_put(handle, size);
5403 } else {
5404 unsigned long sp;
5405 unsigned int rem;
5406 u64 dyn_size;
5407
5408 /*
5409 * We dump:
5410 * static size
5411 * - the size requested by user or the best one we can fit
5412 * in to the sample max size
5413 * data
5414 * - user stack dump data
5415 * dynamic size
5416 * - the actual dumped size
5417 */
5418
5419 /* Static size. */
5420 perf_output_put(handle, dump_size);
5421
5422 /* Data. */
5423 sp = perf_user_stack_pointer(regs);
5424 rem = __output_copy_user(handle, (void *) sp, dump_size);
5425 dyn_size = dump_size - rem;
5426
5427 perf_output_skip(handle, rem);
5428
5429 /* Dynamic size. */
5430 perf_output_put(handle, dyn_size);
5431 }
5432}
5433
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005434static void __perf_event_header__init_id(struct perf_event_header *header,
5435 struct perf_sample_data *data,
5436 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005437{
5438 u64 sample_type = event->attr.sample_type;
5439
5440 data->type = sample_type;
5441 header->size += event->id_header_size;
5442
5443 if (sample_type & PERF_SAMPLE_TID) {
5444 /* namespace issues */
5445 data->tid_entry.pid = perf_event_pid(event, current);
5446 data->tid_entry.tid = perf_event_tid(event, current);
5447 }
5448
5449 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01005450 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005451
Adrian Hunterff3d5272013-08-27 11:23:07 +03005452 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005453 data->id = primary_event_id(event);
5454
5455 if (sample_type & PERF_SAMPLE_STREAM_ID)
5456 data->stream_id = event->id;
5457
5458 if (sample_type & PERF_SAMPLE_CPU) {
5459 data->cpu_entry.cpu = raw_smp_processor_id();
5460 data->cpu_entry.reserved = 0;
5461 }
5462}
5463
Frederic Weisbecker76369132011-05-19 19:55:04 +02005464void perf_event_header__init_id(struct perf_event_header *header,
5465 struct perf_sample_data *data,
5466 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005467{
5468 if (event->attr.sample_id_all)
5469 __perf_event_header__init_id(header, data, event);
5470}
5471
5472static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5473 struct perf_sample_data *data)
5474{
5475 u64 sample_type = data->type;
5476
5477 if (sample_type & PERF_SAMPLE_TID)
5478 perf_output_put(handle, data->tid_entry);
5479
5480 if (sample_type & PERF_SAMPLE_TIME)
5481 perf_output_put(handle, data->time);
5482
5483 if (sample_type & PERF_SAMPLE_ID)
5484 perf_output_put(handle, data->id);
5485
5486 if (sample_type & PERF_SAMPLE_STREAM_ID)
5487 perf_output_put(handle, data->stream_id);
5488
5489 if (sample_type & PERF_SAMPLE_CPU)
5490 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03005491
5492 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5493 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005494}
5495
Frederic Weisbecker76369132011-05-19 19:55:04 +02005496void perf_event__output_id_sample(struct perf_event *event,
5497 struct perf_output_handle *handle,
5498 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005499{
5500 if (event->attr.sample_id_all)
5501 __perf_event__output_id_sample(handle, sample);
5502}
5503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005504static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005505 struct perf_event *event,
5506 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507{
5508 u64 read_format = event->attr.read_format;
5509 u64 values[4];
5510 int n = 0;
5511
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005512 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005513 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005514 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005515 atomic64_read(&event->child_total_time_enabled);
5516 }
5517 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02005518 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005519 atomic64_read(&event->child_total_time_running);
5520 }
5521 if (read_format & PERF_FORMAT_ID)
5522 values[n++] = primary_event_id(event);
5523
Frederic Weisbecker76369132011-05-19 19:55:04 +02005524 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005525}
5526
5527/*
5528 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
5529 */
5530static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02005531 struct perf_event *event,
5532 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005533{
5534 struct perf_event *leader = event->group_leader, *sub;
5535 u64 read_format = event->attr.read_format;
5536 u64 values[5];
5537 int n = 0;
5538
5539 values[n++] = 1 + leader->nr_siblings;
5540
5541 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02005542 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005543
5544 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02005545 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005546
5547 if (leader != event)
5548 leader->pmu->read(leader);
5549
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005550 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005551 if (read_format & PERF_FORMAT_ID)
5552 values[n++] = primary_event_id(leader);
5553
Frederic Weisbecker76369132011-05-19 19:55:04 +02005554 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005555
5556 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
5557 n = 0;
5558
Jiri Olsa6f5ab002012-10-15 20:13:45 +02005559 if ((sub != event) &&
5560 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005561 sub->pmu->read(sub);
5562
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005563 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005564 if (read_format & PERF_FORMAT_ID)
5565 values[n++] = primary_event_id(sub);
5566
Frederic Weisbecker76369132011-05-19 19:55:04 +02005567 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005568 }
5569}
5570
Stephane Eranianeed01522010-10-26 16:08:01 +02005571#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5572 PERF_FORMAT_TOTAL_TIME_RUNNING)
5573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005574static void perf_output_read(struct perf_output_handle *handle,
5575 struct perf_event *event)
5576{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005577 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02005578 u64 read_format = event->attr.read_format;
5579
5580 /*
5581 * compute total_time_enabled, total_time_running
5582 * based on snapshot values taken when the event
5583 * was last scheduled in.
5584 *
5585 * we cannot simply called update_context_time()
5586 * because of locking issue as we are called in
5587 * NMI context
5588 */
Eric B Munsonc4794292011-06-23 16:34:38 -04005589 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005590 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02005591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005592 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02005593 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594 else
Stephane Eranianeed01522010-10-26 16:08:01 +02005595 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005596}
5597
5598void perf_output_sample(struct perf_output_handle *handle,
5599 struct perf_event_header *header,
5600 struct perf_sample_data *data,
5601 struct perf_event *event)
5602{
5603 u64 sample_type = data->type;
5604
5605 perf_output_put(handle, *header);
5606
Adrian Hunterff3d5272013-08-27 11:23:07 +03005607 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5608 perf_output_put(handle, data->id);
5609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005610 if (sample_type & PERF_SAMPLE_IP)
5611 perf_output_put(handle, data->ip);
5612
5613 if (sample_type & PERF_SAMPLE_TID)
5614 perf_output_put(handle, data->tid_entry);
5615
5616 if (sample_type & PERF_SAMPLE_TIME)
5617 perf_output_put(handle, data->time);
5618
5619 if (sample_type & PERF_SAMPLE_ADDR)
5620 perf_output_put(handle, data->addr);
5621
5622 if (sample_type & PERF_SAMPLE_ID)
5623 perf_output_put(handle, data->id);
5624
5625 if (sample_type & PERF_SAMPLE_STREAM_ID)
5626 perf_output_put(handle, data->stream_id);
5627
5628 if (sample_type & PERF_SAMPLE_CPU)
5629 perf_output_put(handle, data->cpu_entry);
5630
5631 if (sample_type & PERF_SAMPLE_PERIOD)
5632 perf_output_put(handle, data->period);
5633
5634 if (sample_type & PERF_SAMPLE_READ)
5635 perf_output_read(handle, event);
5636
5637 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5638 if (data->callchain) {
5639 int size = 1;
5640
5641 if (data->callchain)
5642 size += data->callchain->nr;
5643
5644 size *= sizeof(u64);
5645
Frederic Weisbecker76369132011-05-19 19:55:04 +02005646 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647 } else {
5648 u64 nr = 0;
5649 perf_output_put(handle, nr);
5650 }
5651 }
5652
5653 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005654 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07005655
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005656 if (raw) {
5657 struct perf_raw_frag *frag = &raw->frag;
5658
5659 perf_output_put(handle, raw->size);
5660 do {
5661 if (frag->copy) {
5662 __output_custom(handle, frag->copy,
5663 frag->data, frag->size);
5664 } else {
5665 __output_copy(handle, frag->data,
5666 frag->size);
5667 }
5668 if (perf_raw_frag_last(frag))
5669 break;
5670 frag = frag->next;
5671 } while (1);
5672 if (frag->pad)
5673 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005674 } else {
5675 struct {
5676 u32 size;
5677 u32 data;
5678 } raw = {
5679 .size = sizeof(u32),
5680 .data = 0,
5681 };
5682 perf_output_put(handle, raw);
5683 }
5684 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005685
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005686 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5687 if (data->br_stack) {
5688 size_t size;
5689
5690 size = data->br_stack->nr
5691 * sizeof(struct perf_branch_entry);
5692
5693 perf_output_put(handle, data->br_stack->nr);
5694 perf_output_copy(handle, data->br_stack->entries, size);
5695 } else {
5696 /*
5697 * we always store at least the value of nr
5698 */
5699 u64 nr = 0;
5700 perf_output_put(handle, nr);
5701 }
5702 }
Jiri Olsa40189942012-08-07 15:20:37 +02005703
5704 if (sample_type & PERF_SAMPLE_REGS_USER) {
5705 u64 abi = data->regs_user.abi;
5706
5707 /*
5708 * If there are no regs to dump, notice it through
5709 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5710 */
5711 perf_output_put(handle, abi);
5712
5713 if (abi) {
5714 u64 mask = event->attr.sample_regs_user;
5715 perf_output_sample_regs(handle,
5716 data->regs_user.regs,
5717 mask);
5718 }
5719 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005720
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005721 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005722 perf_output_sample_ustack(handle,
5723 data->stack_user_size,
5724 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005725 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005726
5727 if (sample_type & PERF_SAMPLE_WEIGHT)
5728 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005729
5730 if (sample_type & PERF_SAMPLE_DATA_SRC)
5731 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005732
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005733 if (sample_type & PERF_SAMPLE_TRANSACTION)
5734 perf_output_put(handle, data->txn);
5735
Stephane Eranian60e23642014-09-24 13:48:37 +02005736 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5737 u64 abi = data->regs_intr.abi;
5738 /*
5739 * If there are no regs to dump, notice it through
5740 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5741 */
5742 perf_output_put(handle, abi);
5743
5744 if (abi) {
5745 u64 mask = event->attr.sample_regs_intr;
5746
5747 perf_output_sample_regs(handle,
5748 data->regs_intr.regs,
5749 mask);
5750 }
5751 }
5752
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005753 if (!event->attr.watermark) {
5754 int wakeup_events = event->attr.wakeup_events;
5755
5756 if (wakeup_events) {
5757 struct ring_buffer *rb = handle->rb;
5758 int events = local_inc_return(&rb->events);
5759
5760 if (events >= wakeup_events) {
5761 local_sub(wakeup_events, &rb->events);
5762 local_inc(&rb->wakeup);
5763 }
5764 }
5765 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005766}
5767
5768void perf_prepare_sample(struct perf_event_header *header,
5769 struct perf_sample_data *data,
5770 struct perf_event *event,
5771 struct pt_regs *regs)
5772{
5773 u64 sample_type = event->attr.sample_type;
5774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005775 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005776 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005777
5778 header->misc = 0;
5779 header->misc |= perf_misc_flags(regs);
5780
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005781 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005782
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005783 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005784 data->ip = perf_instruction_pointer(regs);
5785
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005786 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5787 int size = 1;
5788
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005789 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005790
5791 if (data->callchain)
5792 size += data->callchain->nr;
5793
5794 header->size += size * sizeof(u64);
5795 }
5796
5797 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005798 struct perf_raw_record *raw = data->raw;
5799 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005801 if (raw) {
5802 struct perf_raw_frag *frag = &raw->frag;
5803 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005804
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02005805 do {
5806 sum += frag->size;
5807 if (perf_raw_frag_last(frag))
5808 break;
5809 frag = frag->next;
5810 } while (1);
5811
5812 size = round_up(sum + sizeof(u32), sizeof(u64));
5813 raw->size = size - sizeof(u32);
5814 frag->pad = raw->size - sum;
5815 } else {
5816 size = sizeof(u64);
5817 }
5818
5819 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005821
5822 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5823 int size = sizeof(u64); /* nr */
5824 if (data->br_stack) {
5825 size += data->br_stack->nr
5826 * sizeof(struct perf_branch_entry);
5827 }
5828 header->size += size;
5829 }
Jiri Olsa40189942012-08-07 15:20:37 +02005830
Peter Zijlstra25657112014-09-24 13:48:42 +02005831 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005832 perf_sample_regs_user(&data->regs_user, regs,
5833 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005834
Jiri Olsa40189942012-08-07 15:20:37 +02005835 if (sample_type & PERF_SAMPLE_REGS_USER) {
5836 /* regs dump ABI info */
5837 int size = sizeof(u64);
5838
Jiri Olsa40189942012-08-07 15:20:37 +02005839 if (data->regs_user.regs) {
5840 u64 mask = event->attr.sample_regs_user;
5841 size += hweight64(mask) * sizeof(u64);
5842 }
5843
5844 header->size += size;
5845 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005846
5847 if (sample_type & PERF_SAMPLE_STACK_USER) {
5848 /*
5849 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5850 * processed as the last one or have additional check added
5851 * in case new sample type is added, because we could eat
5852 * up the rest of the sample size.
5853 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005854 u16 stack_size = event->attr.sample_stack_user;
5855 u16 size = sizeof(u64);
5856
Jiri Olsac5ebced2012-08-07 15:20:40 +02005857 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005858 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005859
5860 /*
5861 * If there is something to dump, add space for the dump
5862 * itself and for the field that tells the dynamic size,
5863 * which is how many have been actually dumped.
5864 */
5865 if (stack_size)
5866 size += sizeof(u64) + stack_size;
5867
5868 data->stack_user_size = stack_size;
5869 header->size += size;
5870 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005871
5872 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5873 /* regs dump ABI info */
5874 int size = sizeof(u64);
5875
5876 perf_sample_regs_intr(&data->regs_intr, regs);
5877
5878 if (data->regs_intr.regs) {
5879 u64 mask = event->attr.sample_regs_intr;
5880
5881 size += hweight64(mask) * sizeof(u64);
5882 }
5883
5884 header->size += size;
5885 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005886}
5887
Wang Nan9ecda412016-04-05 14:11:18 +00005888static void __always_inline
5889__perf_event_output(struct perf_event *event,
5890 struct perf_sample_data *data,
5891 struct pt_regs *regs,
5892 int (*output_begin)(struct perf_output_handle *,
5893 struct perf_event *,
5894 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005895{
5896 struct perf_output_handle handle;
5897 struct perf_event_header header;
5898
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005899 /* protect the callchain buffers */
5900 rcu_read_lock();
5901
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005902 perf_prepare_sample(&header, data, event, regs);
5903
Wang Nan9ecda412016-04-05 14:11:18 +00005904 if (output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005905 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005906
5907 perf_output_sample(&handle, &header, data, event);
5908
5909 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005910
5911exit:
5912 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005913}
5914
Wang Nan9ecda412016-04-05 14:11:18 +00005915void
5916perf_event_output_forward(struct perf_event *event,
5917 struct perf_sample_data *data,
5918 struct pt_regs *regs)
5919{
5920 __perf_event_output(event, data, regs, perf_output_begin_forward);
5921}
5922
5923void
5924perf_event_output_backward(struct perf_event *event,
5925 struct perf_sample_data *data,
5926 struct pt_regs *regs)
5927{
5928 __perf_event_output(event, data, regs, perf_output_begin_backward);
5929}
5930
5931void
5932perf_event_output(struct perf_event *event,
5933 struct perf_sample_data *data,
5934 struct pt_regs *regs)
5935{
5936 __perf_event_output(event, data, regs, perf_output_begin);
5937}
5938
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005939/*
5940 * read event_id
5941 */
5942
5943struct perf_read_event {
5944 struct perf_event_header header;
5945
5946 u32 pid;
5947 u32 tid;
5948};
5949
5950static void
5951perf_event_read_event(struct perf_event *event,
5952 struct task_struct *task)
5953{
5954 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005955 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005956 struct perf_read_event read_event = {
5957 .header = {
5958 .type = PERF_RECORD_READ,
5959 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005960 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005961 },
5962 .pid = perf_event_pid(event, task),
5963 .tid = perf_event_tid(event, task),
5964 };
5965 int ret;
5966
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005967 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005968 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969 if (ret)
5970 return;
5971
5972 perf_output_put(&handle, read_event);
5973 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005974 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005975
5976 perf_output_end(&handle);
5977}
5978
Peter Zijlstraaab5b712016-05-12 17:26:46 +02005979typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005980
5981static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02005982perf_iterate_ctx(struct perf_event_context *ctx,
5983 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005984 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02005985{
5986 struct perf_event *event;
5987
5988 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03005989 if (!all) {
5990 if (event->state < PERF_EVENT_STATE_INACTIVE)
5991 continue;
5992 if (!event_filter_match(event))
5993 continue;
5994 }
5995
Jiri Olsa67516842013-07-09 18:56:31 +02005996 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005997 }
5998}
5999
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006000static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07006001{
6002 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
6003 struct perf_event *event;
6004
6005 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02006006 /*
6007 * Skip events that are not fully formed yet; ensure that
6008 * if we observe event->ctx, both event and ctx will be
6009 * complete enough. See perf_install_in_context().
6010 */
6011 if (!smp_load_acquire(&event->ctx))
6012 continue;
6013
Kan Liangf2fb6be2016-03-23 11:24:37 -07006014 if (event->state < PERF_EVENT_STATE_INACTIVE)
6015 continue;
6016 if (!event_filter_match(event))
6017 continue;
6018 output(event, data);
6019 }
6020}
6021
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006022/*
6023 * Iterate all events that need to receive side-band events.
6024 *
6025 * For new callers; ensure that account_pmu_sb_event() includes
6026 * your event, otherwise it might not get delivered.
6027 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006028static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006029perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006030 struct perf_event_context *task_ctx)
6031{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006032 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02006033 int ctxn;
6034
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006035 rcu_read_lock();
6036 preempt_disable();
6037
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006038 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006039 * If we have task_ctx != NULL we only notify the task context itself.
6040 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006041 * context.
6042 */
6043 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006044 perf_iterate_ctx(task_ctx, output, data, false);
6045 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01006046 }
6047
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006048 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07006049
6050 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02006051 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6052 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006053 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02006054 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006055done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07006056 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02006057 rcu_read_unlock();
6058}
6059
Alexander Shishkin375637b2016-04-27 18:44:46 +03006060/*
6061 * Clear all file-based filters at exec, they'll have to be
6062 * re-instated when/if these objects are mmapped again.
6063 */
6064static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
6065{
6066 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6067 struct perf_addr_filter *filter;
6068 unsigned int restart = 0, count = 0;
6069 unsigned long flags;
6070
6071 if (!has_addr_filter(event))
6072 return;
6073
6074 raw_spin_lock_irqsave(&ifh->lock, flags);
6075 list_for_each_entry(filter, &ifh->list, entry) {
6076 if (filter->inode) {
6077 event->addr_filters_offs[count] = 0;
6078 restart++;
6079 }
6080
6081 count++;
6082 }
6083
6084 if (restart)
6085 event->addr_filters_gen++;
6086 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6087
6088 if (restart)
6089 perf_event_restart(event);
6090}
6091
6092void perf_event_exec(void)
6093{
6094 struct perf_event_context *ctx;
6095 int ctxn;
6096
6097 rcu_read_lock();
6098 for_each_task_context_nr(ctxn) {
6099 ctx = current->perf_event_ctxp[ctxn];
6100 if (!ctx)
6101 continue;
6102
6103 perf_event_enable_on_exec(ctxn);
6104
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006105 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03006106 true);
6107 }
6108 rcu_read_unlock();
6109}
6110
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006111struct remote_output {
6112 struct ring_buffer *rb;
6113 int err;
6114};
6115
6116static void __perf_event_output_stop(struct perf_event *event, void *data)
6117{
6118 struct perf_event *parent = event->parent;
6119 struct remote_output *ro = data;
6120 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03006121 struct stop_event_data sd = {
6122 .event = event,
6123 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006124
6125 if (!has_aux(event))
6126 return;
6127
6128 if (!parent)
6129 parent = event;
6130
6131 /*
6132 * In case of inheritance, it will be the parent that links to the
6133 * ring-buffer, but it will be the child that's actually using it:
6134 */
6135 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03006136 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006137}
6138
6139static int __perf_pmu_output_stop(void *info)
6140{
6141 struct perf_event *event = info;
6142 struct pmu *pmu = event->pmu;
6143 struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
6144 struct remote_output ro = {
6145 .rb = event->rb,
6146 };
6147
6148 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006149 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006150 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006151 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03006152 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02006153 rcu_read_unlock();
6154
6155 return ro.err;
6156}
6157
6158static void perf_pmu_output_stop(struct perf_event *event)
6159{
6160 struct perf_event *iter;
6161 int err, cpu;
6162
6163restart:
6164 rcu_read_lock();
6165 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6166 /*
6167 * For per-CPU events, we need to make sure that neither they
6168 * nor their children are running; for cpu==-1 events it's
6169 * sufficient to stop the event itself if it's active, since
6170 * it can't have children.
6171 */
6172 cpu = iter->cpu;
6173 if (cpu == -1)
6174 cpu = READ_ONCE(iter->oncpu);
6175
6176 if (cpu == -1)
6177 continue;
6178
6179 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6180 if (err == -EAGAIN) {
6181 rcu_read_unlock();
6182 goto restart;
6183 }
6184 }
6185 rcu_read_unlock();
6186}
6187
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006188/*
6189 * task tracking -- fork/exit
6190 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02006191 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006192 */
6193
6194struct perf_task_event {
6195 struct task_struct *task;
6196 struct perf_event_context *task_ctx;
6197
6198 struct {
6199 struct perf_event_header header;
6200
6201 u32 pid;
6202 u32 ppid;
6203 u32 tid;
6204 u32 ptid;
6205 u64 time;
6206 } event_id;
6207};
6208
Jiri Olsa67516842013-07-09 18:56:31 +02006209static int perf_event_task_match(struct perf_event *event)
6210{
Stephane Eranian13d7a242013-08-21 12:10:24 +02006211 return event->attr.comm || event->attr.mmap ||
6212 event->attr.mmap2 || event->attr.mmap_data ||
6213 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02006214}
6215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006216static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006217 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006218{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006219 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006220 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006221 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006222 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006223 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01006224
Jiri Olsa67516842013-07-09 18:56:31 +02006225 if (!perf_event_task_match(event))
6226 return;
6227
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006228 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006229
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006230 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006231 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02006232 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006233 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006234
6235 task_event->event_id.pid = perf_event_pid(event, task);
6236 task_event->event_id.ppid = perf_event_pid(event, current);
6237
6238 task_event->event_id.tid = perf_event_tid(event, task);
6239 task_event->event_id.ptid = perf_event_tid(event, current);
6240
Peter Zijlstra34f43922015-02-20 14:05:38 +01006241 task_event->event_id.time = perf_event_clock(event);
6242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006243 perf_output_put(&handle, task_event->event_id);
6244
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006245 perf_event__output_id_sample(event, &handle, &sample);
6246
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006247 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006248out:
6249 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006250}
6251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006252static void perf_event_task(struct task_struct *task,
6253 struct perf_event_context *task_ctx,
6254 int new)
6255{
6256 struct perf_task_event task_event;
6257
6258 if (!atomic_read(&nr_comm_events) &&
6259 !atomic_read(&nr_mmap_events) &&
6260 !atomic_read(&nr_task_events))
6261 return;
6262
6263 task_event = (struct perf_task_event){
6264 .task = task,
6265 .task_ctx = task_ctx,
6266 .event_id = {
6267 .header = {
6268 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
6269 .misc = 0,
6270 .size = sizeof(task_event.event_id),
6271 },
6272 /* .pid */
6273 /* .ppid */
6274 /* .tid */
6275 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01006276 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006277 },
6278 };
6279
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006280 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006281 &task_event,
6282 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006283}
6284
6285void perf_event_fork(struct task_struct *task)
6286{
6287 perf_event_task(task, NULL, 1);
6288}
6289
6290/*
6291 * comm tracking
6292 */
6293
6294struct perf_comm_event {
6295 struct task_struct *task;
6296 char *comm;
6297 int comm_size;
6298
6299 struct {
6300 struct perf_event_header header;
6301
6302 u32 pid;
6303 u32 tid;
6304 } event_id;
6305};
6306
Jiri Olsa67516842013-07-09 18:56:31 +02006307static int perf_event_comm_match(struct perf_event *event)
6308{
6309 return event->attr.comm;
6310}
6311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006312static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006313 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006314{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006315 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006316 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006317 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006318 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006319 int ret;
6320
Jiri Olsa67516842013-07-09 18:56:31 +02006321 if (!perf_event_comm_match(event))
6322 return;
6323
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006324 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6325 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006326 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006327
6328 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006329 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006330
6331 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6332 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
6333
6334 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006335 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006336 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006337
6338 perf_event__output_id_sample(event, &handle, &sample);
6339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006340 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006341out:
6342 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006343}
6344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006345static void perf_event_comm_event(struct perf_comm_event *comm_event)
6346{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006347 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006348 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349
6350 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01006351 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006352 size = ALIGN(strlen(comm)+1, sizeof(u64));
6353
6354 comm_event->comm = comm;
6355 comm_event->comm_size = size;
6356
6357 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006358
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006359 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006360 comm_event,
6361 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006362}
6363
Adrian Hunter82b89772014-05-28 11:45:04 +03006364void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006365{
6366 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367
6368 if (!atomic_read(&nr_comm_events))
6369 return;
6370
6371 comm_event = (struct perf_comm_event){
6372 .task = task,
6373 /* .comm */
6374 /* .comm_size */
6375 .event_id = {
6376 .header = {
6377 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03006378 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006379 /* .size */
6380 },
6381 /* .pid */
6382 /* .tid */
6383 },
6384 };
6385
6386 perf_event_comm_event(&comm_event);
6387}
6388
6389/*
6390 * mmap tracking
6391 */
6392
6393struct perf_mmap_event {
6394 struct vm_area_struct *vma;
6395
6396 const char *file_name;
6397 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006398 int maj, min;
6399 u64 ino;
6400 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006401 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006402
6403 struct {
6404 struct perf_event_header header;
6405
6406 u32 pid;
6407 u32 tid;
6408 u64 start;
6409 u64 len;
6410 u64 pgoff;
6411 } event_id;
6412};
6413
Jiri Olsa67516842013-07-09 18:56:31 +02006414static int perf_event_mmap_match(struct perf_event *event,
6415 void *data)
6416{
6417 struct perf_mmap_event *mmap_event = data;
6418 struct vm_area_struct *vma = mmap_event->vma;
6419 int executable = vma->vm_flags & VM_EXEC;
6420
6421 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02006422 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02006423}
6424
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006425static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006426 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006427{
Jiri Olsa52d857a2013-05-06 18:27:18 +02006428 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006429 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006430 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006431 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006432 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006433
Jiri Olsa67516842013-07-09 18:56:31 +02006434 if (!perf_event_mmap_match(event, data))
6435 return;
6436
Stephane Eranian13d7a242013-08-21 12:10:24 +02006437 if (event->attr.mmap2) {
6438 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6439 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6440 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6441 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03006442 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006443 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6444 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006445 }
6446
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006447 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6448 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006449 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006450 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006451 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006452
6453 mmap_event->event_id.pid = perf_event_pid(event, current);
6454 mmap_event->event_id.tid = perf_event_tid(event, current);
6455
6456 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006457
6458 if (event->attr.mmap2) {
6459 perf_output_put(&handle, mmap_event->maj);
6460 perf_output_put(&handle, mmap_event->min);
6461 perf_output_put(&handle, mmap_event->ino);
6462 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006463 perf_output_put(&handle, mmap_event->prot);
6464 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02006465 }
6466
Frederic Weisbecker76369132011-05-19 19:55:04 +02006467 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006468 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006469
6470 perf_event__output_id_sample(event, &handle, &sample);
6471
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006472 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006473out:
6474 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006475}
6476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006477static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
6478{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006479 struct vm_area_struct *vma = mmap_event->vma;
6480 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006481 int maj = 0, min = 0;
6482 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006483 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006484 unsigned int size;
6485 char tmp[16];
6486 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006487 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006488
6489 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02006490 struct inode *inode;
6491 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006492
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006493 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006494 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006495 name = "//enomem";
6496 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006497 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006498 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02006499 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500 * need to add enough zero bytes after the string to handle
6501 * the 64bit alignment we do later.
6502 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02006503 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006505 name = "//toolong";
6506 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006507 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02006508 inode = file_inode(vma->vm_file);
6509 dev = inode->i_sb->s_dev;
6510 ino = inode->i_ino;
6511 gen = inode->i_generation;
6512 maj = MAJOR(dev);
6513 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006514
6515 if (vma->vm_flags & VM_READ)
6516 prot |= PROT_READ;
6517 if (vma->vm_flags & VM_WRITE)
6518 prot |= PROT_WRITE;
6519 if (vma->vm_flags & VM_EXEC)
6520 prot |= PROT_EXEC;
6521
6522 if (vma->vm_flags & VM_MAYSHARE)
6523 flags = MAP_SHARED;
6524 else
6525 flags = MAP_PRIVATE;
6526
6527 if (vma->vm_flags & VM_DENYWRITE)
6528 flags |= MAP_DENYWRITE;
6529 if (vma->vm_flags & VM_MAYEXEC)
6530 flags |= MAP_EXECUTABLE;
6531 if (vma->vm_flags & VM_LOCKED)
6532 flags |= MAP_LOCKED;
6533 if (vma->vm_flags & VM_HUGETLB)
6534 flags |= MAP_HUGETLB;
6535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006536 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02006538 if (vma->vm_ops && vma->vm_ops->name) {
6539 name = (char *) vma->vm_ops->name(vma);
6540 if (name)
6541 goto cpy_name;
6542 }
6543
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006544 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006545 if (name)
6546 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006547
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006548 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006549 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006550 name = "[heap]";
6551 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02006552 }
6553 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006555 name = "[stack]";
6556 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006557 }
6558
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006559 name = "//anon";
6560 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006561 }
6562
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02006563cpy_name:
6564 strlcpy(tmp, name, sizeof(tmp));
6565 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006566got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02006567 /*
6568 * Since our buffer works in 8 byte units we need to align our string
6569 * size to a multiple of 8. However, we must guarantee the tail end is
6570 * zero'd out to avoid leaking random bits to userspace.
6571 */
6572 size = strlen(name)+1;
6573 while (!IS_ALIGNED(size, sizeof(u64)))
6574 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006575
6576 mmap_event->file_name = name;
6577 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02006578 mmap_event->maj = maj;
6579 mmap_event->min = min;
6580 mmap_event->ino = ino;
6581 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006582 mmap_event->prot = prot;
6583 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006584
Stephane Eranian2fe85422013-01-24 16:10:39 +01006585 if (!(vma->vm_flags & VM_EXEC))
6586 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6587
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006588 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
6589
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006590 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02006591 mmap_event,
6592 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006593
6594 kfree(buf);
6595}
6596
Alexander Shishkin375637b2016-04-27 18:44:46 +03006597/*
6598 * Whether this @filter depends on a dynamic object which is not loaded
6599 * yet or its load addresses are not known.
6600 */
6601static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter)
6602{
6603 return filter->filter && filter->inode;
6604}
6605
6606/*
6607 * Check whether inode and address range match filter criteria.
6608 */
6609static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6610 struct file *file, unsigned long offset,
6611 unsigned long size)
6612{
6613 if (filter->inode != file->f_inode)
6614 return false;
6615
6616 if (filter->offset > offset + size)
6617 return false;
6618
6619 if (filter->offset + filter->size < offset)
6620 return false;
6621
6622 return true;
6623}
6624
6625static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6626{
6627 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6628 struct vm_area_struct *vma = data;
6629 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6630 struct file *file = vma->vm_file;
6631 struct perf_addr_filter *filter;
6632 unsigned int restart = 0, count = 0;
6633
6634 if (!has_addr_filter(event))
6635 return;
6636
6637 if (!file)
6638 return;
6639
6640 raw_spin_lock_irqsave(&ifh->lock, flags);
6641 list_for_each_entry(filter, &ifh->list, entry) {
6642 if (perf_addr_filter_match(filter, file, off,
6643 vma->vm_end - vma->vm_start)) {
6644 event->addr_filters_offs[count] = vma->vm_start;
6645 restart++;
6646 }
6647
6648 count++;
6649 }
6650
6651 if (restart)
6652 event->addr_filters_gen++;
6653 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6654
6655 if (restart)
6656 perf_event_restart(event);
6657}
6658
6659/*
6660 * Adjust all task's events' filters to the new vma
6661 */
6662static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6663{
6664 struct perf_event_context *ctx;
6665 int ctxn;
6666
6667 rcu_read_lock();
6668 for_each_task_context_nr(ctxn) {
6669 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6670 if (!ctx)
6671 continue;
6672
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006673 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03006674 }
6675 rcu_read_unlock();
6676}
6677
Eric B Munson3af9e852010-05-18 15:30:49 +01006678void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006679{
6680 struct perf_mmap_event mmap_event;
6681
6682 if (!atomic_read(&nr_mmap_events))
6683 return;
6684
6685 mmap_event = (struct perf_mmap_event){
6686 .vma = vma,
6687 /* .file_name */
6688 /* .file_size */
6689 .event_id = {
6690 .header = {
6691 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006692 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006693 /* .size */
6694 },
6695 /* .pid */
6696 /* .tid */
6697 .start = vma->vm_start,
6698 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01006699 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006700 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02006701 /* .maj (attr_mmap2 only) */
6702 /* .min (attr_mmap2 only) */
6703 /* .ino (attr_mmap2 only) */
6704 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04006705 /* .prot (attr_mmap2 only) */
6706 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006707 };
6708
Alexander Shishkin375637b2016-04-27 18:44:46 +03006709 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006710 perf_event_mmap_event(&mmap_event);
6711}
6712
Alexander Shishkin68db7e92015-01-14 14:18:15 +02006713void perf_event_aux_event(struct perf_event *event, unsigned long head,
6714 unsigned long size, u64 flags)
6715{
6716 struct perf_output_handle handle;
6717 struct perf_sample_data sample;
6718 struct perf_aux_event {
6719 struct perf_event_header header;
6720 u64 offset;
6721 u64 size;
6722 u64 flags;
6723 } rec = {
6724 .header = {
6725 .type = PERF_RECORD_AUX,
6726 .misc = 0,
6727 .size = sizeof(rec),
6728 },
6729 .offset = head,
6730 .size = size,
6731 .flags = flags,
6732 };
6733 int ret;
6734
6735 perf_event_header__init_id(&rec.header, &sample, event);
6736 ret = perf_output_begin(&handle, event, rec.header.size);
6737
6738 if (ret)
6739 return;
6740
6741 perf_output_put(&handle, rec);
6742 perf_event__output_id_sample(event, &handle, &sample);
6743
6744 perf_output_end(&handle);
6745}
6746
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006747/*
Kan Liangf38b0db2015-05-10 15:13:14 -04006748 * Lost/dropped samples logging
6749 */
6750void perf_log_lost_samples(struct perf_event *event, u64 lost)
6751{
6752 struct perf_output_handle handle;
6753 struct perf_sample_data sample;
6754 int ret;
6755
6756 struct {
6757 struct perf_event_header header;
6758 u64 lost;
6759 } lost_samples_event = {
6760 .header = {
6761 .type = PERF_RECORD_LOST_SAMPLES,
6762 .misc = 0,
6763 .size = sizeof(lost_samples_event),
6764 },
6765 .lost = lost,
6766 };
6767
6768 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6769
6770 ret = perf_output_begin(&handle, event,
6771 lost_samples_event.header.size);
6772 if (ret)
6773 return;
6774
6775 perf_output_put(&handle, lost_samples_event);
6776 perf_event__output_id_sample(event, &handle, &sample);
6777 perf_output_end(&handle);
6778}
6779
6780/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03006781 * context_switch tracking
6782 */
6783
6784struct perf_switch_event {
6785 struct task_struct *task;
6786 struct task_struct *next_prev;
6787
6788 struct {
6789 struct perf_event_header header;
6790 u32 next_prev_pid;
6791 u32 next_prev_tid;
6792 } event_id;
6793};
6794
6795static int perf_event_switch_match(struct perf_event *event)
6796{
6797 return event->attr.context_switch;
6798}
6799
6800static void perf_event_switch_output(struct perf_event *event, void *data)
6801{
6802 struct perf_switch_event *se = data;
6803 struct perf_output_handle handle;
6804 struct perf_sample_data sample;
6805 int ret;
6806
6807 if (!perf_event_switch_match(event))
6808 return;
6809
6810 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6811 if (event->ctx->task) {
6812 se->event_id.header.type = PERF_RECORD_SWITCH;
6813 se->event_id.header.size = sizeof(se->event_id.header);
6814 } else {
6815 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6816 se->event_id.header.size = sizeof(se->event_id);
6817 se->event_id.next_prev_pid =
6818 perf_event_pid(event, se->next_prev);
6819 se->event_id.next_prev_tid =
6820 perf_event_tid(event, se->next_prev);
6821 }
6822
6823 perf_event_header__init_id(&se->event_id.header, &sample, event);
6824
6825 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6826 if (ret)
6827 return;
6828
6829 if (event->ctx->task)
6830 perf_output_put(&handle, se->event_id.header);
6831 else
6832 perf_output_put(&handle, se->event_id);
6833
6834 perf_event__output_id_sample(event, &handle, &sample);
6835
6836 perf_output_end(&handle);
6837}
6838
6839static void perf_event_switch(struct task_struct *task,
6840 struct task_struct *next_prev, bool sched_in)
6841{
6842 struct perf_switch_event switch_event;
6843
6844 /* N.B. caller checks nr_switch_events != 0 */
6845
6846 switch_event = (struct perf_switch_event){
6847 .task = task,
6848 .next_prev = next_prev,
6849 .event_id = {
6850 .header = {
6851 /* .type */
6852 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6853 /* .size */
6854 },
6855 /* .next_prev_pid */
6856 /* .next_prev_tid */
6857 },
6858 };
6859
Peter Zijlstraaab5b712016-05-12 17:26:46 +02006860 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03006861 &switch_event,
6862 NULL);
6863}
6864
6865/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006866 * IRQ throttle logging
6867 */
6868
6869static void perf_log_throttle(struct perf_event *event, int enable)
6870{
6871 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006872 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006873 int ret;
6874
6875 struct {
6876 struct perf_event_header header;
6877 u64 time;
6878 u64 id;
6879 u64 stream_id;
6880 } throttle_event = {
6881 .header = {
6882 .type = PERF_RECORD_THROTTLE,
6883 .misc = 0,
6884 .size = sizeof(throttle_event),
6885 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01006886 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006887 .id = primary_event_id(event),
6888 .stream_id = event->id,
6889 };
6890
6891 if (enable)
6892 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
6893
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006894 perf_event_header__init_id(&throttle_event.header, &sample, event);
6895
6896 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006897 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006898 if (ret)
6899 return;
6900
6901 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006902 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006903 perf_output_end(&handle);
6904}
6905
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006906static void perf_log_itrace_start(struct perf_event *event)
6907{
6908 struct perf_output_handle handle;
6909 struct perf_sample_data sample;
6910 struct perf_aux_event {
6911 struct perf_event_header header;
6912 u32 pid;
6913 u32 tid;
6914 } rec;
6915 int ret;
6916
6917 if (event->parent)
6918 event = event->parent;
6919
6920 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6921 event->hw.itrace_started)
6922 return;
6923
Alexander Shishkinec0d7722015-01-14 14:18:23 +02006924 rec.header.type = PERF_RECORD_ITRACE_START;
6925 rec.header.misc = 0;
6926 rec.header.size = sizeof(rec);
6927 rec.pid = perf_event_pid(event, current);
6928 rec.tid = perf_event_tid(event, current);
6929
6930 perf_event_header__init_id(&rec.header, &sample, event);
6931 ret = perf_output_begin(&handle, event, rec.header.size);
6932
6933 if (ret)
6934 return;
6935
6936 perf_output_put(&handle, rec);
6937 perf_event__output_id_sample(event, &handle, &sample);
6938
6939 perf_output_end(&handle);
6940}
6941
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006942/*
6943 * Generic event overflow handling, sampling.
6944 */
6945
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006946static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006947 int throttle, struct perf_sample_data *data,
6948 struct pt_regs *regs)
6949{
6950 int events = atomic_read(&event->event_limit);
6951 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006952 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006953 int ret = 0;
6954
Peter Zijlstra96398822010-11-24 18:55:29 +01006955 /*
6956 * Non-sampling counters might still use the PMI to fold short
6957 * hardware counters, ignore those.
6958 */
6959 if (unlikely(!is_sampling_event(event)))
6960 return 0;
6961
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006962 seq = __this_cpu_read(perf_throttled_seq);
6963 if (seq != hwc->interrupts_seq) {
6964 hwc->interrupts_seq = seq;
6965 hwc->interrupts = 1;
6966 } else {
6967 hwc->interrupts++;
6968 if (unlikely(throttle
6969 && hwc->interrupts >= max_samples_per_tick)) {
6970 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02006971 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01006972 hwc->interrupts = MAX_INTERRUPTS;
6973 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006974 ret = 1;
6975 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01006976 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006977
6978 if (event->attr.freq) {
6979 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01006980 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006981
Peter Zijlstraabd50712010-01-26 18:50:16 +01006982 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983
Peter Zijlstraabd50712010-01-26 18:50:16 +01006984 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01006985 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006986 }
6987
6988 /*
6989 * XXX event_limit might not quite work as expected on inherited
6990 * events
6991 */
6992
6993 event->pending_kill = POLL_IN;
6994 if (events && atomic_dec_and_test(&event->event_limit)) {
6995 ret = 1;
6996 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006997 event->pending_disable = 1;
6998 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006999 }
7000
Wang Nan18794452016-03-28 06:41:30 +00007001 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01007002
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02007003 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007004 event->pending_wakeup = 1;
7005 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02007006 }
7007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007008 return ret;
7009}
7010
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007011int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007012 struct perf_sample_data *data,
7013 struct pt_regs *regs)
7014{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007015 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007016}
7017
7018/*
7019 * Generic software event infrastructure
7020 */
7021
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007022struct swevent_htable {
7023 struct swevent_hlist *swevent_hlist;
7024 struct mutex hlist_mutex;
7025 int hlist_refcount;
7026
7027 /* Recursion avoidance in each contexts */
7028 int recursion[PERF_NR_CONTEXTS];
7029};
7030
7031static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7032
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007033/*
7034 * We directly increment event->count and keep a second value in
7035 * event->hw.period_left to count intervals. This period event
7036 * is kept in the range [-sample_period, 0] so that we can use the
7037 * sign as trigger.
7038 */
7039
Jiri Olsaab573842013-05-01 17:25:44 +02007040u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007041{
7042 struct hw_perf_event *hwc = &event->hw;
7043 u64 period = hwc->last_period;
7044 u64 nr, offset;
7045 s64 old, val;
7046
7047 hwc->last_period = hwc->sample_period;
7048
7049again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02007050 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007051 if (val < 0)
7052 return 0;
7053
7054 nr = div64_u64(period + val, period);
7055 offset = nr * period;
7056 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02007057 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007058 goto again;
7059
7060 return nr;
7061}
7062
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007063static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007064 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007065 struct pt_regs *regs)
7066{
7067 struct hw_perf_event *hwc = &event->hw;
7068 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007069
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007070 if (!overflow)
7071 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007072
7073 if (hwc->interrupts == MAX_INTERRUPTS)
7074 return;
7075
7076 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007077 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007078 data, regs)) {
7079 /*
7080 * We inhibit the overflow from happening when
7081 * hwc->interrupts == MAX_INTERRUPTS.
7082 */
7083 break;
7084 }
7085 throttle = 1;
7086 }
7087}
7088
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007089static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007090 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007091 struct pt_regs *regs)
7092{
7093 struct hw_perf_event *hwc = &event->hw;
7094
Peter Zijlstrae7850592010-05-21 14:43:08 +02007095 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007096
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007097 if (!regs)
7098 return;
7099
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007100 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007101 return;
7102
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03007103 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
7104 data->period = nr;
7105 return perf_swevent_overflow(event, 1, data, regs);
7106 } else
7107 data->period = event->hw.last_period;
7108
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007109 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007110 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007111
Peter Zijlstrae7850592010-05-21 14:43:08 +02007112 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01007113 return;
7114
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007115 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007116}
7117
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007118static int perf_exclude_event(struct perf_event *event,
7119 struct pt_regs *regs)
7120{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007121 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01007122 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007123
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007124 if (regs) {
7125 if (event->attr.exclude_user && user_mode(regs))
7126 return 1;
7127
7128 if (event->attr.exclude_kernel && !user_mode(regs))
7129 return 1;
7130 }
7131
7132 return 0;
7133}
7134
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007135static int perf_swevent_match(struct perf_event *event,
7136 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08007137 u32 event_id,
7138 struct perf_sample_data *data,
7139 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007140{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141 if (event->attr.type != type)
7142 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007144 if (event->attr.config != event_id)
7145 return 0;
7146
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007147 if (perf_exclude_event(event, regs))
7148 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007149
7150 return 1;
7151}
7152
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007153static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007155 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007156
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007157 return hash_64(val, SWEVENT_HLIST_BITS);
7158}
7159
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007160static inline struct hlist_head *
7161__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007162{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007163 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007164
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007165 return &hlist->heads[hash];
7166}
7167
7168/* For the read side: events when they trigger */
7169static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007170find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007171{
7172 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007173
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007174 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007175 if (!hlist)
7176 return NULL;
7177
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007178 return __find_swevent_head(hlist, type, event_id);
7179}
7180
7181/* For the event head insertion and removal in the hlist */
7182static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007183find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007184{
7185 struct swevent_hlist *hlist;
7186 u32 event_id = event->attr.config;
7187 u64 type = event->attr.type;
7188
7189 /*
7190 * Event scheduling is always serialized against hlist allocation
7191 * and release. Which makes the protected version suitable here.
7192 * The context lock guarantees that.
7193 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007194 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007195 lockdep_is_held(&event->ctx->lock));
7196 if (!hlist)
7197 return NULL;
7198
7199 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007200}
7201
7202static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007203 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007204 struct perf_sample_data *data,
7205 struct pt_regs *regs)
7206{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007207 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007208 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007209 struct hlist_head *head;
7210
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007211 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007212 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007213 if (!head)
7214 goto end;
7215
Sasha Levinb67bfe02013-02-27 17:06:00 -08007216 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08007217 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007218 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007220end:
7221 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007222}
7223
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007224DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7225
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007226int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007227{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007228 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007229
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007230 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007231}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01007232EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007233
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007234void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007235{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007236 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007237
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007238 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01007239}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007240
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007241void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007242{
Ingo Molnara4234bf2009-11-23 10:57:59 +01007243 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007244
7245 if (WARN_ON_ONCE(!regs))
7246 return;
7247
7248 perf_sample_data_init(&data, addr, 0);
7249 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
7250}
7251
7252void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7253{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007254 int rctx;
7255
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007256 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007257 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007258 if (unlikely(rctx < 0))
7259 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007260
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007261 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01007262
7263 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01007264fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007265 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007266}
7267
7268static void perf_swevent_read(struct perf_event *event)
7269{
7270}
7271
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007272static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007273{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05007274 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007275 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007276 struct hlist_head *head;
7277
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01007278 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007279 hwc->last_period = hwc->sample_period;
7280 perf_swevent_set_period(event);
7281 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007282
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007283 hwc->state = !(flags & PERF_EF_START);
7284
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007285 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01007286 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007287 return -EINVAL;
7288
7289 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08007290 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007292 return 0;
7293}
7294
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007295static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007296{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007297 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007298}
7299
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007300static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007301{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007302 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007303}
7304
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007305static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007306{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007307 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02007308}
7309
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007310/* Deref the hlist from the update side */
7311static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007312swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007313{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007314 return rcu_dereference_protected(swhash->swevent_hlist,
7315 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007316}
7317
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007318static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007319{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007320 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007321
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02007322 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007323 return;
7324
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03007325 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08007326 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007327}
7328
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007329static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007330{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007331 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007332
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007333 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007334
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007335 if (!--swhash->hlist_refcount)
7336 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007337
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007338 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007339}
7340
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007341static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007342{
7343 int cpu;
7344
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007345 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007346 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007347}
7348
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007349static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007350{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007351 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007352 int err = 0;
7353
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007354 mutex_lock(&swhash->hlist_mutex);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007355 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007356 struct swevent_hlist *hlist;
7357
7358 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7359 if (!hlist) {
7360 err = -ENOMEM;
7361 goto exit;
7362 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007363 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007364 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007365 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007366exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007367 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007368
7369 return err;
7370}
7371
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007372static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007373{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007374 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007375
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007376 get_online_cpus();
7377 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007378 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007379 if (err) {
7380 failed_cpu = cpu;
7381 goto fail;
7382 }
7383 }
7384 put_online_cpus();
7385
7386 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02007387fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007388 for_each_possible_cpu(cpu) {
7389 if (cpu == failed_cpu)
7390 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007391 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007392 }
7393
7394 put_online_cpus();
7395 return err;
7396}
7397
Ingo Molnarc5905af2012-02-24 08:31:31 +01007398struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007399
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007400static void sw_perf_event_destroy(struct perf_event *event)
7401{
7402 u64 event_id = event->attr.config;
7403
7404 WARN_ON(event->parent);
7405
Ingo Molnarc5905af2012-02-24 08:31:31 +01007406 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007407 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007408}
7409
7410static int perf_swevent_init(struct perf_event *event)
7411{
Tommi Rantala8176cce2013-04-13 22:49:14 +03007412 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007413
7414 if (event->attr.type != PERF_TYPE_SOFTWARE)
7415 return -ENOENT;
7416
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007417 /*
7418 * no branch sampling for software events
7419 */
7420 if (has_branch_stack(event))
7421 return -EOPNOTSUPP;
7422
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007423 switch (event_id) {
7424 case PERF_COUNT_SW_CPU_CLOCK:
7425 case PERF_COUNT_SW_TASK_CLOCK:
7426 return -ENOENT;
7427
7428 default:
7429 break;
7430 }
7431
Dan Carpenterce677832010-10-24 21:50:42 +02007432 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007433 return -ENOENT;
7434
7435 if (!event->parent) {
7436 int err;
7437
Thomas Gleixner3b364d72016-02-09 20:11:40 +00007438 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007439 if (err)
7440 return err;
7441
Ingo Molnarc5905af2012-02-24 08:31:31 +01007442 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007443 event->destroy = sw_perf_event_destroy;
7444 }
7445
7446 return 0;
7447}
7448
7449static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007450 .task_ctx_nr = perf_sw_context,
7451
Peter Zijlstra34f43922015-02-20 14:05:38 +01007452 .capabilities = PERF_PMU_CAP_NO_NMI,
7453
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007454 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007455 .add = perf_swevent_add,
7456 .del = perf_swevent_del,
7457 .start = perf_swevent_start,
7458 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007459 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007460};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007461
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007462#ifdef CONFIG_EVENT_TRACING
7463
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007464static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007465 struct perf_sample_data *data)
7466{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007467 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007468
Peter Zijlstrab71b4372015-11-02 10:50:51 +01007469 /* only top level events have filters set */
7470 if (event->parent)
7471 event = event->parent;
7472
Frederic Weisbecker95476b62010-04-14 23:42:18 +02007473 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7474 return 1;
7475 return 0;
7476}
7477
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007478static int perf_tp_event_match(struct perf_event *event,
7479 struct perf_sample_data *data,
7480 struct pt_regs *regs)
7481{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01007482 if (event->hw.state & PERF_HES_STOPPED)
7483 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02007484 /*
7485 * All tracepoints are from kernel-space.
7486 */
7487 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007488 return 0;
7489
7490 if (!perf_tp_filter_match(event, data))
7491 return 0;
7492
7493 return 1;
7494}
7495
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07007496void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7497 struct trace_event_call *call, u64 count,
7498 struct pt_regs *regs, struct hlist_head *head,
7499 struct task_struct *task)
7500{
7501 struct bpf_prog *prog = call->prog;
7502
7503 if (prog) {
7504 *(struct pt_regs **)raw_data = regs;
7505 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7506 perf_swevent_put_recursion_context(rctx);
7507 return;
7508 }
7509 }
7510 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7511 rctx, task);
7512}
7513EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7514
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007515void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007516 struct pt_regs *regs, struct hlist_head *head, int rctx,
7517 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007518{
7519 struct perf_sample_data data;
7520 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007521
7522 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007523 .frag = {
7524 .size = entry_size,
7525 .data = record,
7526 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007527 };
7528
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007529 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007530 data.raw = &raw;
7531
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07007532 perf_trace_buf_update(record, event_type);
7533
Sasha Levinb67bfe02013-02-27 17:06:00 -08007534 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007535 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007536 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007537 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007538
Andrew Vagine6dab5f2012-07-11 18:14:58 +04007539 /*
7540 * If we got specified a target task, also iterate its context and
7541 * deliver this event there too.
7542 */
7543 if (task && task != current) {
7544 struct perf_event_context *ctx;
7545 struct trace_entry *entry = record;
7546
7547 rcu_read_lock();
7548 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7549 if (!ctx)
7550 goto unlock;
7551
7552 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7553 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7554 continue;
7555 if (event->attr.config != entry->type)
7556 continue;
7557 if (perf_tp_event_match(event, &data, regs))
7558 perf_swevent_event(event, count, &data, regs);
7559 }
7560unlock:
7561 rcu_read_unlock();
7562 }
7563
Peter Zijlstraecc55f82010-05-21 15:11:34 +02007564 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007565}
7566EXPORT_SYMBOL_GPL(perf_tp_event);
7567
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007568static void tp_perf_event_destroy(struct perf_event *event)
7569{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007570 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007571}
7572
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007573static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007574{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007575 int err;
7576
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007577 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7578 return -ENOENT;
7579
Stephane Eranian2481c5f2012-02-09 23:20:59 +01007580 /*
7581 * no branch sampling for tracepoint events
7582 */
7583 if (has_branch_stack(event))
7584 return -EOPNOTSUPP;
7585
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02007586 err = perf_trace_init(event);
7587 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007588 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007589
7590 event->destroy = tp_perf_event_destroy;
7591
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007592 return 0;
7593}
7594
7595static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007596 .task_ctx_nr = perf_sw_context,
7597
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007598 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007599 .add = perf_trace_add,
7600 .del = perf_trace_del,
7601 .start = perf_swevent_start,
7602 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007603 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007604};
7605
7606static inline void perf_tp_register(void)
7607{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007608 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007609}
Li Zefan6fb29152009-10-15 11:21:42 +08007610
Li Zefan6fb29152009-10-15 11:21:42 +08007611static void perf_event_free_filter(struct perf_event *event)
7612{
7613 ftrace_profile_free_filter(event);
7614}
7615
Alexei Starovoitov25415172015-03-25 12:49:20 -07007616static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7617{
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007618 bool is_kprobe, is_tracepoint;
Alexei Starovoitov25415172015-03-25 12:49:20 -07007619 struct bpf_prog *prog;
7620
7621 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7622 return -EINVAL;
7623
7624 if (event->tp_event->prog)
7625 return -EEXIST;
7626
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007627 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7628 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7629 if (!is_kprobe && !is_tracepoint)
7630 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07007631 return -EINVAL;
7632
7633 prog = bpf_prog_get(prog_fd);
7634 if (IS_ERR(prog))
7635 return PTR_ERR(prog);
7636
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07007637 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7638 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07007639 /* valid fd, but invalid bpf program type */
7640 bpf_prog_put(prog);
7641 return -EINVAL;
7642 }
7643
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07007644 if (is_tracepoint) {
7645 int off = trace_event_get_offsets(event->tp_event);
7646
7647 if (prog->aux->max_ctx_offset > off) {
7648 bpf_prog_put(prog);
7649 return -EACCES;
7650 }
7651 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07007652 event->tp_event->prog = prog;
7653
7654 return 0;
7655}
7656
7657static void perf_event_free_bpf_prog(struct perf_event *event)
7658{
7659 struct bpf_prog *prog;
7660
7661 if (!event->tp_event)
7662 return;
7663
7664 prog = event->tp_event->prog;
7665 if (prog) {
7666 event->tp_event->prog = NULL;
Daniel Borkmann1aacde32016-06-30 17:24:43 +02007667 bpf_prog_put(prog);
Alexei Starovoitov25415172015-03-25 12:49:20 -07007668 }
7669}
7670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007671#else
Li Zefan6fb29152009-10-15 11:21:42 +08007672
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007673static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007674{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007675}
Li Zefan6fb29152009-10-15 11:21:42 +08007676
Li Zefan6fb29152009-10-15 11:21:42 +08007677static void perf_event_free_filter(struct perf_event *event)
7678{
7679}
7680
Alexei Starovoitov25415172015-03-25 12:49:20 -07007681static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7682{
7683 return -ENOENT;
7684}
7685
7686static void perf_event_free_bpf_prog(struct perf_event *event)
7687{
7688}
Li Zefan07b139c2009-12-21 14:27:35 +08007689#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007690
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007691#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007692void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007693{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007694 struct perf_sample_data sample;
7695 struct pt_regs *regs = data;
7696
Robert Richterfd0d0002012-04-02 20:19:08 +02007697 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01007698
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02007699 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02007700 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02007701}
7702#endif
7703
Alexander Shishkin375637b2016-04-27 18:44:46 +03007704/*
7705 * Allocate a new address filter
7706 */
7707static struct perf_addr_filter *
7708perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7709{
7710 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7711 struct perf_addr_filter *filter;
7712
7713 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7714 if (!filter)
7715 return NULL;
7716
7717 INIT_LIST_HEAD(&filter->entry);
7718 list_add_tail(&filter->entry, filters);
7719
7720 return filter;
7721}
7722
7723static void free_filters_list(struct list_head *filters)
7724{
7725 struct perf_addr_filter *filter, *iter;
7726
7727 list_for_each_entry_safe(filter, iter, filters, entry) {
7728 if (filter->inode)
7729 iput(filter->inode);
7730 list_del(&filter->entry);
7731 kfree(filter);
7732 }
7733}
7734
7735/*
7736 * Free existing address filters and optionally install new ones
7737 */
7738static void perf_addr_filters_splice(struct perf_event *event,
7739 struct list_head *head)
7740{
7741 unsigned long flags;
7742 LIST_HEAD(list);
7743
7744 if (!has_addr_filter(event))
7745 return;
7746
7747 /* don't bother with children, they don't have their own filters */
7748 if (event->parent)
7749 return;
7750
7751 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
7752
7753 list_splice_init(&event->addr_filters.list, &list);
7754 if (head)
7755 list_splice(head, &event->addr_filters.list);
7756
7757 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
7758
7759 free_filters_list(&list);
7760}
7761
7762/*
7763 * Scan through mm's vmas and see if one of them matches the
7764 * @filter; if so, adjust filter's address range.
7765 * Called with mm::mmap_sem down for reading.
7766 */
7767static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
7768 struct mm_struct *mm)
7769{
7770 struct vm_area_struct *vma;
7771
7772 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7773 struct file *file = vma->vm_file;
7774 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7775 unsigned long vma_size = vma->vm_end - vma->vm_start;
7776
7777 if (!file)
7778 continue;
7779
7780 if (!perf_addr_filter_match(filter, file, off, vma_size))
7781 continue;
7782
7783 return vma->vm_start;
7784 }
7785
7786 return 0;
7787}
7788
7789/*
7790 * Update event's address range filters based on the
7791 * task's existing mappings, if any.
7792 */
7793static void perf_event_addr_filters_apply(struct perf_event *event)
7794{
7795 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7796 struct task_struct *task = READ_ONCE(event->ctx->task);
7797 struct perf_addr_filter *filter;
7798 struct mm_struct *mm = NULL;
7799 unsigned int count = 0;
7800 unsigned long flags;
7801
7802 /*
7803 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7804 * will stop on the parent's child_mutex that our caller is also holding
7805 */
7806 if (task == TASK_TOMBSTONE)
7807 return;
7808
7809 mm = get_task_mm(event->ctx->task);
7810 if (!mm)
7811 goto restart;
7812
7813 down_read(&mm->mmap_sem);
7814
7815 raw_spin_lock_irqsave(&ifh->lock, flags);
7816 list_for_each_entry(filter, &ifh->list, entry) {
7817 event->addr_filters_offs[count] = 0;
7818
7819 if (perf_addr_filter_needs_mmap(filter))
7820 event->addr_filters_offs[count] =
7821 perf_addr_filter_apply(filter, mm);
7822
7823 count++;
7824 }
7825
7826 event->addr_filters_gen++;
7827 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7828
7829 up_read(&mm->mmap_sem);
7830
7831 mmput(mm);
7832
7833restart:
7834 perf_event_restart(event);
7835}
7836
7837/*
7838 * Address range filtering: limiting the data to certain
7839 * instruction address ranges. Filters are ioctl()ed to us from
7840 * userspace as ascii strings.
7841 *
7842 * Filter string format:
7843 *
7844 * ACTION RANGE_SPEC
7845 * where ACTION is one of the
7846 * * "filter": limit the trace to this region
7847 * * "start": start tracing from this address
7848 * * "stop": stop tracing at this address/region;
7849 * RANGE_SPEC is
7850 * * for kernel addresses: <start address>[/<size>]
7851 * * for object files: <start address>[/<size>]@</path/to/object/file>
7852 *
7853 * if <size> is not specified, the range is treated as a single address.
7854 */
7855enum {
7856 IF_ACT_FILTER,
7857 IF_ACT_START,
7858 IF_ACT_STOP,
7859 IF_SRC_FILE,
7860 IF_SRC_KERNEL,
7861 IF_SRC_FILEADDR,
7862 IF_SRC_KERNELADDR,
7863};
7864
7865enum {
7866 IF_STATE_ACTION = 0,
7867 IF_STATE_SOURCE,
7868 IF_STATE_END,
7869};
7870
7871static const match_table_t if_tokens = {
7872 { IF_ACT_FILTER, "filter" },
7873 { IF_ACT_START, "start" },
7874 { IF_ACT_STOP, "stop" },
7875 { IF_SRC_FILE, "%u/%u@%s" },
7876 { IF_SRC_KERNEL, "%u/%u" },
7877 { IF_SRC_FILEADDR, "%u@%s" },
7878 { IF_SRC_KERNELADDR, "%u" },
7879};
7880
7881/*
7882 * Address filter string parser
7883 */
7884static int
7885perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
7886 struct list_head *filters)
7887{
7888 struct perf_addr_filter *filter = NULL;
7889 char *start, *orig, *filename = NULL;
7890 struct path path;
7891 substring_t args[MAX_OPT_ARGS];
7892 int state = IF_STATE_ACTION, token;
7893 unsigned int kernel = 0;
7894 int ret = -EINVAL;
7895
7896 orig = fstr = kstrdup(fstr, GFP_KERNEL);
7897 if (!fstr)
7898 return -ENOMEM;
7899
7900 while ((start = strsep(&fstr, " ,\n")) != NULL) {
7901 ret = -EINVAL;
7902
7903 if (!*start)
7904 continue;
7905
7906 /* filter definition begins */
7907 if (state == IF_STATE_ACTION) {
7908 filter = perf_addr_filter_new(event, filters);
7909 if (!filter)
7910 goto fail;
7911 }
7912
7913 token = match_token(start, if_tokens, args);
7914 switch (token) {
7915 case IF_ACT_FILTER:
7916 case IF_ACT_START:
7917 filter->filter = 1;
7918
7919 case IF_ACT_STOP:
7920 if (state != IF_STATE_ACTION)
7921 goto fail;
7922
7923 state = IF_STATE_SOURCE;
7924 break;
7925
7926 case IF_SRC_KERNELADDR:
7927 case IF_SRC_KERNEL:
7928 kernel = 1;
7929
7930 case IF_SRC_FILEADDR:
7931 case IF_SRC_FILE:
7932 if (state != IF_STATE_SOURCE)
7933 goto fail;
7934
7935 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
7936 filter->range = 1;
7937
7938 *args[0].to = 0;
7939 ret = kstrtoul(args[0].from, 0, &filter->offset);
7940 if (ret)
7941 goto fail;
7942
7943 if (filter->range) {
7944 *args[1].to = 0;
7945 ret = kstrtoul(args[1].from, 0, &filter->size);
7946 if (ret)
7947 goto fail;
7948 }
7949
7950 if (token == IF_SRC_FILE) {
7951 filename = match_strdup(&args[2]);
7952 if (!filename) {
7953 ret = -ENOMEM;
7954 goto fail;
7955 }
7956 }
7957
7958 state = IF_STATE_END;
7959 break;
7960
7961 default:
7962 goto fail;
7963 }
7964
7965 /*
7966 * Filter definition is fully parsed, validate and install it.
7967 * Make sure that it doesn't contradict itself or the event's
7968 * attribute.
7969 */
7970 if (state == IF_STATE_END) {
7971 if (kernel && event->attr.exclude_kernel)
7972 goto fail;
7973
7974 if (!kernel) {
7975 if (!filename)
7976 goto fail;
7977
7978 /* look up the path and grab its inode */
7979 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
7980 if (ret)
7981 goto fail_free_name;
7982
7983 filter->inode = igrab(d_inode(path.dentry));
7984 path_put(&path);
7985 kfree(filename);
7986 filename = NULL;
7987
7988 ret = -EINVAL;
7989 if (!filter->inode ||
7990 !S_ISREG(filter->inode->i_mode))
7991 /* free_filters_list() will iput() */
7992 goto fail;
7993 }
7994
7995 /* ready to consume more filters */
7996 state = IF_STATE_ACTION;
7997 filter = NULL;
7998 }
7999 }
8000
8001 if (state != IF_STATE_ACTION)
8002 goto fail;
8003
8004 kfree(orig);
8005
8006 return 0;
8007
8008fail_free_name:
8009 kfree(filename);
8010fail:
8011 free_filters_list(filters);
8012 kfree(orig);
8013
8014 return ret;
8015}
8016
8017static int
8018perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
8019{
8020 LIST_HEAD(filters);
8021 int ret;
8022
8023 /*
8024 * Since this is called in perf_ioctl() path, we're already holding
8025 * ctx::mutex.
8026 */
8027 lockdep_assert_held(&event->ctx->mutex);
8028
8029 if (WARN_ON_ONCE(event->parent))
8030 return -EINVAL;
8031
8032 /*
8033 * For now, we only support filtering in per-task events; doing so
8034 * for CPU-wide events requires additional context switching trickery,
8035 * since same object code will be mapped at different virtual
8036 * addresses in different processes.
8037 */
8038 if (!event->ctx->task)
8039 return -EOPNOTSUPP;
8040
8041 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
8042 if (ret)
8043 return ret;
8044
8045 ret = event->pmu->addr_filters_validate(&filters);
8046 if (ret) {
8047 free_filters_list(&filters);
8048 return ret;
8049 }
8050
8051 /* remove existing filters, if any */
8052 perf_addr_filters_splice(event, &filters);
8053
8054 /* install new filters */
8055 perf_event_for_each_child(event, perf_event_addr_filters_apply);
8056
8057 return ret;
8058}
8059
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008060static int perf_event_set_filter(struct perf_event *event, void __user *arg)
8061{
8062 char *filter_str;
8063 int ret = -EINVAL;
8064
Alexander Shishkin375637b2016-04-27 18:44:46 +03008065 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
8066 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
8067 !has_addr_filter(event))
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008068 return -EINVAL;
8069
8070 filter_str = strndup_user(arg, PAGE_SIZE);
8071 if (IS_ERR(filter_str))
8072 return PTR_ERR(filter_str);
8073
8074 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
8075 event->attr.type == PERF_TYPE_TRACEPOINT)
8076 ret = ftrace_profile_set_filter(event, event->attr.config,
8077 filter_str);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008078 else if (has_addr_filter(event))
8079 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03008080
8081 kfree(filter_str);
8082 return ret;
8083}
8084
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008085/*
8086 * hrtimer based swevent callback
8087 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008088
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008089static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008090{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008091 enum hrtimer_restart ret = HRTIMER_RESTART;
8092 struct perf_sample_data data;
8093 struct pt_regs *regs;
8094 struct perf_event *event;
8095 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008096
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008097 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008098
8099 if (event->state != PERF_EVENT_STATE_ACTIVE)
8100 return HRTIMER_NORESTART;
8101
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008102 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008103
Robert Richterfd0d0002012-04-02 20:19:08 +02008104 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008105 regs = get_irq_regs();
8106
8107 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08008108 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02008109 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008110 ret = HRTIMER_NORESTART;
8111 }
8112
8113 period = max_t(u64, 10000, event->hw.sample_period);
8114 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
8115
8116 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008117}
8118
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008119static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008120{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008121 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008122 s64 period;
8123
8124 if (!is_sampling_event(event))
8125 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008126
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008127 period = local64_read(&hwc->period_left);
8128 if (period) {
8129 if (period < 0)
8130 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008131
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01008132 local64_set(&hwc->period_left, 0);
8133 } else {
8134 period = max_t(u64, 10000, hwc->sample_period);
8135 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00008136 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8137 HRTIMER_MODE_REL_PINNED);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008138}
8139
8140static void perf_swevent_cancel_hrtimer(struct perf_event *event)
8141{
8142 struct hw_perf_event *hwc = &event->hw;
8143
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008144 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008145 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02008146 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008147
8148 hrtimer_cancel(&hwc->hrtimer);
8149 }
8150}
8151
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008152static void perf_swevent_init_hrtimer(struct perf_event *event)
8153{
8154 struct hw_perf_event *hwc = &event->hw;
8155
8156 if (!is_sampling_event(event))
8157 return;
8158
8159 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8160 hwc->hrtimer.function = perf_swevent_hrtimer;
8161
8162 /*
8163 * Since hrtimers have a fixed rate, we can do a static freq->period
8164 * mapping and avoid the whole period adjust feedback stuff.
8165 */
8166 if (event->attr.freq) {
8167 long freq = event->attr.sample_freq;
8168
8169 event->attr.sample_period = NSEC_PER_SEC / freq;
8170 hwc->sample_period = event->attr.sample_period;
8171 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09008172 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008173 event->attr.freq = 0;
8174 }
8175}
8176
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008177/*
8178 * Software event: cpu wall time clock
8179 */
8180
8181static void cpu_clock_event_update(struct perf_event *event)
8182{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008183 s64 prev;
8184 u64 now;
8185
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008186 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008187 prev = local64_xchg(&event->hw.prev_count, now);
8188 local64_add(now - prev, &event->count);
8189}
8190
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008191static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008192{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008193 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008194 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008195}
8196
8197static void cpu_clock_event_stop(struct perf_event *event, int flags)
8198{
8199 perf_swevent_cancel_hrtimer(event);
8200 cpu_clock_event_update(event);
8201}
8202
8203static int cpu_clock_event_add(struct perf_event *event, int flags)
8204{
8205 if (flags & PERF_EF_START)
8206 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008207 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008208
8209 return 0;
8210}
8211
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008212static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008213{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008214 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008215}
8216
8217static void cpu_clock_event_read(struct perf_event *event)
8218{
8219 cpu_clock_event_update(event);
8220}
8221
8222static int cpu_clock_event_init(struct perf_event *event)
8223{
8224 if (event->attr.type != PERF_TYPE_SOFTWARE)
8225 return -ENOENT;
8226
8227 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8228 return -ENOENT;
8229
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008230 /*
8231 * no branch sampling for software events
8232 */
8233 if (has_branch_stack(event))
8234 return -EOPNOTSUPP;
8235
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008236 perf_swevent_init_hrtimer(event);
8237
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008238 return 0;
8239}
8240
8241static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008242 .task_ctx_nr = perf_sw_context,
8243
Peter Zijlstra34f43922015-02-20 14:05:38 +01008244 .capabilities = PERF_PMU_CAP_NO_NMI,
8245
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008246 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008247 .add = cpu_clock_event_add,
8248 .del = cpu_clock_event_del,
8249 .start = cpu_clock_event_start,
8250 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008251 .read = cpu_clock_event_read,
8252};
8253
8254/*
8255 * Software event: task time clock
8256 */
8257
8258static void task_clock_event_update(struct perf_event *event, u64 now)
8259{
8260 u64 prev;
8261 s64 delta;
8262
8263 prev = local64_xchg(&event->hw.prev_count, now);
8264 delta = now - prev;
8265 local64_add(delta, &event->count);
8266}
8267
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008268static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008269{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008270 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008271 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008272}
8273
8274static void task_clock_event_stop(struct perf_event *event, int flags)
8275{
8276 perf_swevent_cancel_hrtimer(event);
8277 task_clock_event_update(event, event->ctx->time);
8278}
8279
8280static int task_clock_event_add(struct perf_event *event, int flags)
8281{
8282 if (flags & PERF_EF_START)
8283 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08008284 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008285
8286 return 0;
8287}
8288
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008289static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008290{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008291 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008292}
8293
8294static void task_clock_event_read(struct perf_event *event)
8295{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01008296 u64 now = perf_clock();
8297 u64 delta = now - event->ctx->timestamp;
8298 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008299
8300 task_clock_event_update(event, time);
8301}
8302
8303static int task_clock_event_init(struct perf_event *event)
8304{
8305 if (event->attr.type != PERF_TYPE_SOFTWARE)
8306 return -ENOENT;
8307
8308 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8309 return -ENOENT;
8310
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008311 /*
8312 * no branch sampling for software events
8313 */
8314 if (has_branch_stack(event))
8315 return -EOPNOTSUPP;
8316
Peter Zijlstraba3dd362011-02-15 12:41:46 +01008317 perf_swevent_init_hrtimer(event);
8318
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008319 return 0;
8320}
8321
8322static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008323 .task_ctx_nr = perf_sw_context,
8324
Peter Zijlstra34f43922015-02-20 14:05:38 +01008325 .capabilities = PERF_PMU_CAP_NO_NMI,
8326
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008327 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008328 .add = task_clock_event_add,
8329 .del = task_clock_event_del,
8330 .start = task_clock_event_start,
8331 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008332 .read = task_clock_event_read,
8333};
8334
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008335static void perf_pmu_nop_void(struct pmu *pmu)
8336{
8337}
8338
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008339static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8340{
8341}
8342
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008343static int perf_pmu_nop_int(struct pmu *pmu)
8344{
8345 return 0;
8346}
8347
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008348static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008349
8350static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008351{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008352 __this_cpu_write(nop_txn_flags, flags);
8353
8354 if (flags & ~PERF_PMU_TXN_ADD)
8355 return;
8356
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008357 perf_pmu_disable(pmu);
8358}
8359
8360static int perf_pmu_commit_txn(struct pmu *pmu)
8361{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008362 unsigned int flags = __this_cpu_read(nop_txn_flags);
8363
8364 __this_cpu_write(nop_txn_flags, 0);
8365
8366 if (flags & ~PERF_PMU_TXN_ADD)
8367 return 0;
8368
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008369 perf_pmu_enable(pmu);
8370 return 0;
8371}
8372
8373static void perf_pmu_cancel_txn(struct pmu *pmu)
8374{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008375 unsigned int flags = __this_cpu_read(nop_txn_flags);
8376
8377 __this_cpu_write(nop_txn_flags, 0);
8378
8379 if (flags & ~PERF_PMU_TXN_ADD)
8380 return;
8381
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008382 perf_pmu_enable(pmu);
8383}
8384
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008385static int perf_event_idx_default(struct perf_event *event)
8386{
Peter Zijlstrac719f562014-10-21 11:10:21 +02008387 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008388}
8389
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008390/*
8391 * Ensures all contexts with the same task_ctx_nr have the same
8392 * pmu_cpu_context too.
8393 */
Mark Rutland9e317042014-02-10 17:44:18 +00008394static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008395{
8396 struct pmu *pmu;
8397
8398 if (ctxn < 0)
8399 return NULL;
8400
8401 list_for_each_entry(pmu, &pmus, entry) {
8402 if (pmu->task_ctx_nr == ctxn)
8403 return pmu->pmu_cpu_context;
8404 }
8405
8406 return NULL;
8407}
8408
Peter Zijlstra51676952010-12-07 14:18:20 +01008409static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008410{
Peter Zijlstra51676952010-12-07 14:18:20 +01008411 int cpu;
8412
8413 for_each_possible_cpu(cpu) {
8414 struct perf_cpu_context *cpuctx;
8415
8416 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8417
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008418 if (cpuctx->unique_pmu == old_pmu)
8419 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01008420 }
8421}
8422
8423static void free_pmu_context(struct pmu *pmu)
8424{
8425 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008426
8427 mutex_lock(&pmus_lock);
8428 /*
8429 * Like a real lame refcount.
8430 */
Peter Zijlstra51676952010-12-07 14:18:20 +01008431 list_for_each_entry(i, &pmus, entry) {
8432 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8433 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008434 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01008435 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008436 }
8437
Peter Zijlstra51676952010-12-07 14:18:20 +01008438 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008439out:
8440 mutex_unlock(&pmus_lock);
8441}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008442
8443/*
8444 * Let userspace know that this PMU supports address range filtering:
8445 */
8446static ssize_t nr_addr_filters_show(struct device *dev,
8447 struct device_attribute *attr,
8448 char *page)
8449{
8450 struct pmu *pmu = dev_get_drvdata(dev);
8451
8452 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8453}
8454DEVICE_ATTR_RO(nr_addr_filters);
8455
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008456static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008457
Peter Zijlstraabe43402010-11-17 23:17:37 +01008458static ssize_t
8459type_show(struct device *dev, struct device_attribute *attr, char *page)
8460{
8461 struct pmu *pmu = dev_get_drvdata(dev);
8462
8463 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8464}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008465static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008466
Stephane Eranian62b85632013-04-03 14:21:34 +02008467static ssize_t
8468perf_event_mux_interval_ms_show(struct device *dev,
8469 struct device_attribute *attr,
8470 char *page)
8471{
8472 struct pmu *pmu = dev_get_drvdata(dev);
8473
8474 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8475}
8476
Peter Zijlstra272325c2015-04-15 11:41:58 +02008477static DEFINE_MUTEX(mux_interval_mutex);
8478
Stephane Eranian62b85632013-04-03 14:21:34 +02008479static ssize_t
8480perf_event_mux_interval_ms_store(struct device *dev,
8481 struct device_attribute *attr,
8482 const char *buf, size_t count)
8483{
8484 struct pmu *pmu = dev_get_drvdata(dev);
8485 int timer, cpu, ret;
8486
8487 ret = kstrtoint(buf, 0, &timer);
8488 if (ret)
8489 return ret;
8490
8491 if (timer < 1)
8492 return -EINVAL;
8493
8494 /* same value, noting to do */
8495 if (timer == pmu->hrtimer_interval_ms)
8496 return count;
8497
Peter Zijlstra272325c2015-04-15 11:41:58 +02008498 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008499 pmu->hrtimer_interval_ms = timer;
8500
8501 /* update all cpuctx for this PMU */
Peter Zijlstra272325c2015-04-15 11:41:58 +02008502 get_online_cpus();
8503 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +02008504 struct perf_cpu_context *cpuctx;
8505 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8506 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8507
Peter Zijlstra272325c2015-04-15 11:41:58 +02008508 cpu_function_call(cpu,
8509 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +02008510 }
Peter Zijlstra272325c2015-04-15 11:41:58 +02008511 put_online_cpus();
8512 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +02008513
8514 return count;
8515}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008516static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02008517
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008518static struct attribute *pmu_dev_attrs[] = {
8519 &dev_attr_type.attr,
8520 &dev_attr_perf_event_mux_interval_ms.attr,
8521 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008522};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008523ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008524
8525static int pmu_bus_running;
8526static struct bus_type pmu_bus = {
8527 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07008528 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01008529};
8530
8531static void pmu_dev_release(struct device *dev)
8532{
8533 kfree(dev);
8534}
8535
8536static int pmu_dev_alloc(struct pmu *pmu)
8537{
8538 int ret = -ENOMEM;
8539
8540 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8541 if (!pmu->dev)
8542 goto out;
8543
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01008544 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01008545 device_initialize(pmu->dev);
8546 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8547 if (ret)
8548 goto free_dev;
8549
8550 dev_set_drvdata(pmu->dev, pmu);
8551 pmu->dev->bus = &pmu_bus;
8552 pmu->dev->release = pmu_dev_release;
8553 ret = device_add(pmu->dev);
8554 if (ret)
8555 goto free_dev;
8556
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008557 /* For PMUs with address filters, throw in an extra attribute: */
8558 if (pmu->nr_addr_filters)
8559 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8560
8561 if (ret)
8562 goto del_dev;
8563
Peter Zijlstraabe43402010-11-17 23:17:37 +01008564out:
8565 return ret;
8566
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008567del_dev:
8568 device_del(pmu->dev);
8569
Peter Zijlstraabe43402010-11-17 23:17:37 +01008570free_dev:
8571 put_device(pmu->dev);
8572 goto out;
8573}
8574
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008575static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008576static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008577
Mischa Jonker03d8e802013-06-04 11:45:48 +02008578int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008579{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008580 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008581
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008582 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008583 ret = -ENOMEM;
8584 pmu->pmu_disable_count = alloc_percpu(int);
8585 if (!pmu->pmu_disable_count)
8586 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008587
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008588 pmu->type = -1;
8589 if (!name)
8590 goto skip_type;
8591 pmu->name = name;
8592
8593 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08008594 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8595 if (type < 0) {
8596 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008597 goto free_pdc;
8598 }
8599 }
8600 pmu->type = type;
8601
Peter Zijlstraabe43402010-11-17 23:17:37 +01008602 if (pmu_bus_running) {
8603 ret = pmu_dev_alloc(pmu);
8604 if (ret)
8605 goto free_idr;
8606 }
8607
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008608skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +01008609 if (pmu->task_ctx_nr == perf_hw_context) {
8610 static int hw_context_taken = 0;
8611
Mark Rutland5101ef22016-04-26 11:33:46 +01008612 /*
8613 * Other than systems with heterogeneous CPUs, it never makes
8614 * sense for two PMUs to share perf_hw_context. PMUs which are
8615 * uncore must use perf_invalid_context.
8616 */
8617 if (WARN_ON_ONCE(hw_context_taken &&
8618 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +01008619 pmu->task_ctx_nr = perf_invalid_context;
8620
8621 hw_context_taken = 1;
8622 }
8623
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008624 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8625 if (pmu->pmu_cpu_context)
8626 goto got_cpu_context;
8627
Wei Yongjunc4814202013-04-12 11:05:54 +08008628 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008629 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8630 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01008631 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008632
8633 for_each_possible_cpu(cpu) {
8634 struct perf_cpu_context *cpuctx;
8635
8636 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02008637 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01008638 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02008639 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008640 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02008641
Peter Zijlstra272325c2015-04-15 11:41:58 +02008642 __perf_mux_hrtimer_init(cpuctx, cpu);
Stephane Eranian9e630202013-04-03 14:21:33 +02008643
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02008644 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008645 }
8646
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008647got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008648 if (!pmu->start_txn) {
8649 if (pmu->pmu_enable) {
8650 /*
8651 * If we have pmu_enable/pmu_disable calls, install
8652 * transaction stubs that use that to try and batch
8653 * hardware accesses.
8654 */
8655 pmu->start_txn = perf_pmu_start_txn;
8656 pmu->commit_txn = perf_pmu_commit_txn;
8657 pmu->cancel_txn = perf_pmu_cancel_txn;
8658 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07008659 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02008660 pmu->commit_txn = perf_pmu_nop_int;
8661 pmu->cancel_txn = perf_pmu_nop_void;
8662 }
8663 }
8664
8665 if (!pmu->pmu_enable) {
8666 pmu->pmu_enable = perf_pmu_nop_void;
8667 pmu->pmu_disable = perf_pmu_nop_void;
8668 }
8669
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01008670 if (!pmu->event_idx)
8671 pmu->event_idx = perf_event_idx_default;
8672
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008673 list_add_rcu(&pmu->entry, &pmus);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02008674 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008675 ret = 0;
8676unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008677 mutex_unlock(&pmus_lock);
8678
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008679 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008680
Peter Zijlstraabe43402010-11-17 23:17:37 +01008681free_dev:
8682 device_del(pmu->dev);
8683 put_device(pmu->dev);
8684
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008685free_idr:
8686 if (pmu->type >= PERF_TYPE_MAX)
8687 idr_remove(&pmu_idr, pmu->type);
8688
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008689free_pdc:
8690 free_percpu(pmu->pmu_disable_count);
8691 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008692}
Yan, Zhengc464c762014-03-18 16:56:41 +08008693EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008694
8695void perf_pmu_unregister(struct pmu *pmu)
8696{
8697 mutex_lock(&pmus_lock);
8698 list_del_rcu(&pmu->entry);
8699 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008700
8701 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02008702 * We dereference the pmu list under both SRCU and regular RCU, so
8703 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008704 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008705 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02008706 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008707
Peter Zijlstra33696fc2010-06-14 08:49:00 +02008708 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008709 if (pmu->type >= PERF_TYPE_MAX)
8710 idr_remove(&pmu_idr, pmu->type);
Alexander Shishkin6e855cd2016-04-27 18:44:48 +03008711 if (pmu->nr_addr_filters)
8712 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
Peter Zijlstraabe43402010-11-17 23:17:37 +01008713 device_del(pmu->dev);
8714 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01008715 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008716}
Yan, Zhengc464c762014-03-18 16:56:41 +08008717EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008718
Mark Rutlandcc34b982015-01-07 14:56:51 +00008719static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8720{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008721 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00008722 int ret;
8723
8724 if (!try_module_get(pmu->module))
8725 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008726
8727 if (event->group_leader != event) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02008728 /*
8729 * This ctx->mutex can nest when we're called through
8730 * inheritance. See the perf_event_ctx_lock_nested() comment.
8731 */
8732 ctx = perf_event_ctx_lock_nested(event->group_leader,
8733 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008734 BUG_ON(!ctx);
8735 }
8736
Mark Rutlandcc34b982015-01-07 14:56:51 +00008737 event->pmu = pmu;
8738 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01008739
8740 if (ctx)
8741 perf_event_ctx_unlock(event->group_leader, ctx);
8742
Mark Rutlandcc34b982015-01-07 14:56:51 +00008743 if (ret)
8744 module_put(pmu->module);
8745
8746 return ret;
8747}
8748
Geliang Tang18ab2cd2015-09-27 23:25:50 +08008749static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008750{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008751 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008752 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08008753 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008754
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008755 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008756
8757 rcu_read_lock();
8758 pmu = idr_find(&pmu_idr, event->attr.type);
8759 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08008760 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008761 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08008762 if (ret)
8763 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008764 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08008765 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008766
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008767 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00008768 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008769 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008770 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008771
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008772 if (ret != -ENOENT) {
8773 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008774 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008775 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008776 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02008777 pmu = ERR_PTR(-ENOENT);
8778unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008779 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008780
8781 return pmu;
8782}
8783
Kan Liangf2fb6be2016-03-23 11:24:37 -07008784static void attach_sb_event(struct perf_event *event)
8785{
8786 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
8787
8788 raw_spin_lock(&pel->lock);
8789 list_add_rcu(&event->sb_list, &pel->list);
8790 raw_spin_unlock(&pel->lock);
8791}
8792
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008793/*
8794 * We keep a list of all !task (and therefore per-cpu) events
8795 * that need to receive side-band records.
8796 *
8797 * This avoids having to scan all the various PMU per-cpu contexts
8798 * looking for them.
8799 */
Kan Liangf2fb6be2016-03-23 11:24:37 -07008800static void account_pmu_sb_event(struct perf_event *event)
8801{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07008802 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -07008803 attach_sb_event(event);
8804}
8805
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008806static void account_event_cpu(struct perf_event *event, int cpu)
8807{
8808 if (event->parent)
8809 return;
8810
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008811 if (is_cgroup_event(event))
8812 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
8813}
8814
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008815/* Freq events need the tick to stay alive (see perf_event_task_tick). */
8816static void account_freq_event_nohz(void)
8817{
8818#ifdef CONFIG_NO_HZ_FULL
8819 /* Lock so we don't race with concurrent unaccount */
8820 spin_lock(&nr_freq_lock);
8821 if (atomic_inc_return(&nr_freq_events) == 1)
8822 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
8823 spin_unlock(&nr_freq_lock);
8824#endif
8825}
8826
8827static void account_freq_event(void)
8828{
8829 if (tick_nohz_full_enabled())
8830 account_freq_event_nohz();
8831 else
8832 atomic_inc(&nr_freq_events);
8833}
8834
8835
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008836static void account_event(struct perf_event *event)
8837{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008838 bool inc = false;
8839
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008840 if (event->parent)
8841 return;
8842
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008843 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008844 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008845 if (event->attr.mmap || event->attr.mmap_data)
8846 atomic_inc(&nr_mmap_events);
8847 if (event->attr.comm)
8848 atomic_inc(&nr_comm_events);
8849 if (event->attr.task)
8850 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008851 if (event->attr.freq)
8852 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03008853 if (event->attr.context_switch) {
8854 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008855 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03008856 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008857 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008858 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008859 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01008860 inc = true;
8861
Peter Zijlstra9107c892016-02-24 18:45:45 +01008862 if (inc) {
8863 if (atomic_inc_not_zero(&perf_sched_count))
8864 goto enabled;
8865
8866 mutex_lock(&perf_sched_mutex);
8867 if (!atomic_read(&perf_sched_count)) {
8868 static_branch_enable(&perf_sched_events);
8869 /*
8870 * Guarantee that all CPUs observe they key change and
8871 * call the perf scheduling hooks before proceeding to
8872 * install events that need them.
8873 */
8874 synchronize_sched();
8875 }
8876 /*
8877 * Now that we have waited for the sync_sched(), allow further
8878 * increments to by-pass the mutex.
8879 */
8880 atomic_inc(&perf_sched_count);
8881 mutex_unlock(&perf_sched_mutex);
8882 }
8883enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008884
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02008885 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07008886
8887 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02008888}
8889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008890/*
8891 * Allocate and initialize a event structure
8892 */
8893static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008894perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008895 struct task_struct *task,
8896 struct perf_event *group_leader,
8897 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008898 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00008899 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008900{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02008901 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008902 struct perf_event *event;
8903 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02008904 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008905
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008906 if ((unsigned)cpu >= nr_cpu_ids) {
8907 if (!task || cpu != -1)
8908 return ERR_PTR(-EINVAL);
8909 }
8910
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02008911 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008912 if (!event)
8913 return ERR_PTR(-ENOMEM);
8914
8915 /*
8916 * Single events are their own group leaders, with an
8917 * empty sibling list:
8918 */
8919 if (!group_leader)
8920 group_leader = event;
8921
8922 mutex_init(&event->child_mutex);
8923 INIT_LIST_HEAD(&event->child_list);
8924
8925 INIT_LIST_HEAD(&event->group_entry);
8926 INIT_LIST_HEAD(&event->event_entry);
8927 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008928 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01008929 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008930 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01008931 INIT_HLIST_NODE(&event->hlist_entry);
8932
Peter Zijlstra10c6db12011-11-26 02:47:31 +01008933
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008934 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08008935 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008936
8937 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008938 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008939
Al Viroa6fa9412012-08-20 14:59:25 +01008940 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008941 event->cpu = cpu;
8942 event->attr = *attr;
8943 event->group_leader = group_leader;
8944 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008945 event->oncpu = -1;
8946
8947 event->parent = parent_event;
8948
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08008949 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008950 event->id = atomic64_inc_return(&perf_event_id);
8951
8952 event->state = PERF_EVENT_STATE_INACTIVE;
8953
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008954 if (task) {
8955 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008956 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008957 * XXX pmu::event_init needs to know what task to account to
8958 * and we cannot use the ctx information because we need the
8959 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008960 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01008961 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008962 }
8963
Peter Zijlstra34f43922015-02-20 14:05:38 +01008964 event->clock = &local_clock;
8965 if (parent_event)
8966 event->clock = parent_event->clock;
8967
Avi Kivity4dc0da82011-06-29 18:42:35 +03008968 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01008969 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008970 context = parent_event->overflow_handler_context;
8971 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01008972
Wang Nan18794452016-03-28 06:41:30 +00008973 if (overflow_handler) {
8974 event->overflow_handler = overflow_handler;
8975 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +00008976 } else if (is_write_backward(event)){
8977 event->overflow_handler = perf_event_output_backward;
8978 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +00008979 } else {
Wang Nan9ecda412016-04-05 14:11:18 +00008980 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +00008981 event->overflow_handler_context = NULL;
8982 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02008983
Jiri Olsa0231bb52013-02-01 11:23:45 +01008984 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008985
8986 pmu = NULL;
8987
8988 hwc = &event->hw;
8989 hwc->sample_period = attr->sample_period;
8990 if (attr->freq && attr->sample_freq)
8991 hwc->sample_period = 1;
8992 hwc->last_period = hwc->sample_period;
8993
Peter Zijlstrae7850592010-05-21 14:43:08 +02008994 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008995
8996 /*
8997 * we currently do not support PERF_FORMAT_GROUP on inherited events
8998 */
8999 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009000 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009001
Yan, Zhenga46a2302014-11-04 21:56:06 -05009002 if (!has_branch_stack(event))
9003 event->attr.branch_sample_type = 0;
9004
Matt Fleming79dff512015-01-23 18:45:42 +00009005 if (cgroup_fd != -1) {
9006 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
9007 if (err)
9008 goto err_ns;
9009 }
9010
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009011 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009012 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009013 goto err_ns;
9014 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009015 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009016 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009017 }
9018
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009019 err = exclusive_event_init(event);
9020 if (err)
9021 goto err_pmu;
9022
Alexander Shishkin375637b2016-04-27 18:44:46 +03009023 if (has_addr_filter(event)) {
9024 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
9025 sizeof(unsigned long),
9026 GFP_KERNEL);
9027 if (!event->addr_filters_offs)
9028 goto err_per_task;
9029
9030 /* force hw sync on the address filters */
9031 event->addr_filters_gen = 1;
9032 }
9033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009034 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009035 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009036 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009037 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009038 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +01009039 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009040 }
9041
Alexander Shishkin927a5572016-03-02 13:24:14 +02009042 /* symmetric to unaccount_event() in _free_event() */
9043 account_event(event);
9044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009045 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009046
Alexander Shishkin375637b2016-04-27 18:44:46 +03009047err_addr_filters:
9048 kfree(event->addr_filters_offs);
9049
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009050err_per_task:
9051 exclusive_event_destroy(event);
9052
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009053err_pmu:
9054 if (event->destroy)
9055 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08009056 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009057err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00009058 if (is_cgroup_event(event))
9059 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02009060 if (event->ns)
9061 put_pid_ns(event->ns);
9062 kfree(event);
9063
9064 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009065}
9066
9067static int perf_copy_attr(struct perf_event_attr __user *uattr,
9068 struct perf_event_attr *attr)
9069{
9070 u32 size;
9071 int ret;
9072
9073 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
9074 return -EFAULT;
9075
9076 /*
9077 * zero the full structure, so that a short copy will be nice.
9078 */
9079 memset(attr, 0, sizeof(*attr));
9080
9081 ret = get_user(size, &uattr->size);
9082 if (ret)
9083 return ret;
9084
9085 if (size > PAGE_SIZE) /* silly large */
9086 goto err_size;
9087
9088 if (!size) /* abi compat */
9089 size = PERF_ATTR_SIZE_VER0;
9090
9091 if (size < PERF_ATTR_SIZE_VER0)
9092 goto err_size;
9093
9094 /*
9095 * If we're handed a bigger struct than we know of,
9096 * ensure all the unknown bits are 0 - i.e. new
9097 * user-space does not rely on any kernel feature
9098 * extensions we dont know about yet.
9099 */
9100 if (size > sizeof(*attr)) {
9101 unsigned char __user *addr;
9102 unsigned char __user *end;
9103 unsigned char val;
9104
9105 addr = (void __user *)uattr + sizeof(*attr);
9106 end = (void __user *)uattr + size;
9107
9108 for (; addr < end; addr++) {
9109 ret = get_user(val, addr);
9110 if (ret)
9111 return ret;
9112 if (val)
9113 goto err_size;
9114 }
9115 size = sizeof(*attr);
9116 }
9117
9118 ret = copy_from_user(attr, uattr, size);
9119 if (ret)
9120 return -EFAULT;
9121
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05309122 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009123 return -EINVAL;
9124
9125 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
9126 return -EINVAL;
9127
9128 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
9129 return -EINVAL;
9130
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009131 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
9132 u64 mask = attr->branch_sample_type;
9133
9134 /* only using defined bits */
9135 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
9136 return -EINVAL;
9137
9138 /* at least one branch bit must be set */
9139 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9140 return -EINVAL;
9141
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009142 /* propagate priv level, when not set for branch */
9143 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9144
9145 /* exclude_kernel checked on syscall entry */
9146 if (!attr->exclude_kernel)
9147 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9148
9149 if (!attr->exclude_user)
9150 mask |= PERF_SAMPLE_BRANCH_USER;
9151
9152 if (!attr->exclude_hv)
9153 mask |= PERF_SAMPLE_BRANCH_HV;
9154 /*
9155 * adjust user setting (for HW filter setup)
9156 */
9157 attr->branch_sample_type = mask;
9158 }
Stephane Eraniane7122092013-06-06 11:02:04 +02009159 /* privileged levels capture (kernel, hv): check permissions */
9160 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02009161 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9162 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01009163 }
Jiri Olsa40189942012-08-07 15:20:37 +02009164
Jiri Olsac5ebced2012-08-07 15:20:40 +02009165 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02009166 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02009167 if (ret)
9168 return ret;
9169 }
9170
9171 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9172 if (!arch_perf_have_user_stack_dump())
9173 return -ENOSYS;
9174
9175 /*
9176 * We have __u32 type for the size, but so far
9177 * we can only use __u16 as maximum due to the
9178 * __u16 sample size limit.
9179 */
9180 if (attr->sample_stack_user >= USHRT_MAX)
9181 ret = -EINVAL;
9182 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
9183 ret = -EINVAL;
9184 }
Jiri Olsa40189942012-08-07 15:20:37 +02009185
Stephane Eranian60e23642014-09-24 13:48:37 +02009186 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9187 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009188out:
9189 return ret;
9190
9191err_size:
9192 put_user(sizeof(*attr), &uattr->size);
9193 ret = -E2BIG;
9194 goto out;
9195}
9196
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009197static int
9198perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009199{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009200 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009201 int ret = -EINVAL;
9202
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009203 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009204 goto set;
9205
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009206 /* don't allow circular references */
9207 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009208 goto out;
9209
Peter Zijlstra0f139302010-05-20 14:35:15 +02009210 /*
9211 * Don't allow cross-cpu buffers
9212 */
9213 if (output_event->cpu != event->cpu)
9214 goto out;
9215
9216 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02009217 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02009218 */
9219 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9220 goto out;
9221
Peter Zijlstra34f43922015-02-20 14:05:38 +01009222 /*
9223 * Mixing clocks in the same buffer is trouble you don't need.
9224 */
9225 if (output_event->clock != event->clock)
9226 goto out;
9227
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009228 /*
Wang Nan9ecda412016-04-05 14:11:18 +00009229 * Either writing ring buffer from beginning or from end.
9230 * Mixing is not allowed.
9231 */
9232 if (is_write_backward(output_event) != is_write_backward(event))
9233 goto out;
9234
9235 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02009236 * If both events generate aux data, they must be on the same PMU
9237 */
9238 if (has_aux(event) && has_aux(output_event) &&
9239 event->pmu != output_event->pmu)
9240 goto out;
9241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009242set:
9243 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009244 /* Can't redirect output if we've got an active mmap() */
9245 if (atomic_read(&event->mmap_count))
9246 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009247
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009248 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02009249 /* get the rb we want to redirect to */
9250 rb = ring_buffer_get(output_event);
9251 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009252 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009253 }
9254
Peter Zijlstrab69cf532014-03-14 10:50:33 +01009255 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02009256
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009257 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009258unlock:
9259 mutex_unlock(&event->mmap_mutex);
9260
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009261out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009262 return ret;
9263}
9264
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009265static void mutex_lock_double(struct mutex *a, struct mutex *b)
9266{
9267 if (b < a)
9268 swap(a, b);
9269
9270 mutex_lock(a);
9271 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9272}
9273
Peter Zijlstra34f43922015-02-20 14:05:38 +01009274static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9275{
9276 bool nmi_safe = false;
9277
9278 switch (clk_id) {
9279 case CLOCK_MONOTONIC:
9280 event->clock = &ktime_get_mono_fast_ns;
9281 nmi_safe = true;
9282 break;
9283
9284 case CLOCK_MONOTONIC_RAW:
9285 event->clock = &ktime_get_raw_fast_ns;
9286 nmi_safe = true;
9287 break;
9288
9289 case CLOCK_REALTIME:
9290 event->clock = &ktime_get_real_ns;
9291 break;
9292
9293 case CLOCK_BOOTTIME:
9294 event->clock = &ktime_get_boot_ns;
9295 break;
9296
9297 case CLOCK_TAI:
9298 event->clock = &ktime_get_tai_ns;
9299 break;
9300
9301 default:
9302 return -EINVAL;
9303 }
9304
9305 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9306 return -EINVAL;
9307
9308 return 0;
9309}
9310
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009311/**
9312 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9313 *
9314 * @attr_uptr: event_id type attributes for monitoring/sampling
9315 * @pid: target pid
9316 * @cpu: target cpu
9317 * @group_fd: group leader event fd
9318 */
9319SYSCALL_DEFINE5(perf_event_open,
9320 struct perf_event_attr __user *, attr_uptr,
9321 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
9322{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009323 struct perf_event *group_leader = NULL, *output_event = NULL;
9324 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009325 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009326 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009327 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04009328 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07009329 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009330 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04009331 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009332 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009333 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01009334 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00009335 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009336
9337 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02009338 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009339 return -EINVAL;
9340
9341 err = perf_copy_attr(attr_uptr, &attr);
9342 if (err)
9343 return err;
9344
9345 if (!attr.exclude_kernel) {
9346 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9347 return -EACCES;
9348 }
9349
9350 if (attr.freq) {
9351 if (attr.sample_freq > sysctl_perf_event_sample_rate)
9352 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02009353 } else {
9354 if (attr.sample_period & (1ULL << 63))
9355 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009356 }
9357
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -03009358 if (!attr.sample_max_stack)
9359 attr.sample_max_stack = sysctl_perf_event_max_stack;
9360
Stephane Eraniane5d13672011-02-14 11:20:01 +02009361 /*
9362 * In cgroup mode, the pid argument is used to pass the fd
9363 * opened to the cgroup directory in cgroupfs. The cpu argument
9364 * designates the cpu on which to monitor threads from that
9365 * cgroup.
9366 */
9367 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9368 return -EINVAL;
9369
Yann Droneauda21b0b32014-01-05 21:36:33 +01009370 if (flags & PERF_FLAG_FD_CLOEXEC)
9371 f_flags |= O_CLOEXEC;
9372
9373 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009374 if (event_fd < 0)
9375 return event_fd;
9376
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009377 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04009378 err = perf_fget_light(group_fd, &group);
9379 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009380 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04009381 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009382 if (flags & PERF_FLAG_FD_OUTPUT)
9383 output_event = group_leader;
9384 if (flags & PERF_FLAG_FD_NO_GROUP)
9385 group_leader = NULL;
9386 }
9387
Stephane Eraniane5d13672011-02-14 11:20:01 +02009388 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009389 task = find_lively_task_by_vpid(pid);
9390 if (IS_ERR(task)) {
9391 err = PTR_ERR(task);
9392 goto err_group_fd;
9393 }
9394 }
9395
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009396 if (task && group_leader &&
9397 group_leader->attr.inherit != attr.inherit) {
9398 err = -EINVAL;
9399 goto err_task;
9400 }
9401
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009402 get_online_cpus();
9403
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009404 if (task) {
9405 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9406 if (err)
9407 goto err_cpus;
9408
9409 /*
9410 * Reuse ptrace permission checks for now.
9411 *
9412 * We must hold cred_guard_mutex across this and any potential
9413 * perf_install_in_context() call for this new event to
9414 * serialize against exec() altering our credentials (and the
9415 * perf_event_exit_task() that could imply).
9416 */
9417 err = -EACCES;
9418 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9419 goto err_cred;
9420 }
9421
Matt Fleming79dff512015-01-23 18:45:42 +00009422 if (flags & PERF_FLAG_PID_CGROUP)
9423 cgroup_fd = pid;
9424
Avi Kivity4dc0da82011-06-29 18:42:35 +03009425 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009426 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009427 if (IS_ERR(event)) {
9428 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009429 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02009430 }
9431
Vince Weaver53b25332014-05-16 17:12:12 -04009432 if (is_sampling_event(event)) {
9433 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +05309434 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -04009435 goto err_alloc;
9436 }
9437 }
9438
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009439 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009440 * Special case software events and allow them to be part of
9441 * any hardware group.
9442 */
9443 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009444
Peter Zijlstra34f43922015-02-20 14:05:38 +01009445 if (attr.use_clockid) {
9446 err = perf_event_set_clock(event, attr.clockid);
9447 if (err)
9448 goto err_alloc;
9449 }
9450
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009451 if (group_leader &&
9452 (is_software_event(event) != is_software_event(group_leader))) {
9453 if (is_software_event(event)) {
9454 /*
9455 * If event and group_leader are not both a software
9456 * event, and event is, then group leader is not.
9457 *
9458 * Allow the addition of software events to !software
9459 * groups, this is safe because software events never
9460 * fail to schedule.
9461 */
9462 pmu = group_leader->pmu;
9463 } else if (is_software_event(group_leader) &&
9464 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
9465 /*
9466 * In case the group is a pure software group, and we
9467 * try to add a hardware event, move the whole group to
9468 * the hardware context.
9469 */
9470 move_group = 1;
9471 }
9472 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009473
9474 /*
9475 * Get the target context (task or percpu):
9476 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009477 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009478 if (IS_ERR(ctx)) {
9479 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009480 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009481 }
9482
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009483 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9484 err = -EBUSY;
9485 goto err_context;
9486 }
9487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009488 /*
9489 * Look up the group leader (we will attach this event to it):
9490 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009491 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009492 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009494 /*
9495 * Do not allow a recursive hierarchy (this new sibling
9496 * becoming part of another group-sibling):
9497 */
9498 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009499 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01009500
9501 /* All events in a group should have the same clock */
9502 if (group_leader->clock != event->clock)
9503 goto err_context;
9504
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009505 /*
9506 * Do not allow to attach to a group in a different
9507 * task or CPU context:
9508 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009509 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01009510 /*
9511 * Make sure we're both on the same task, or both
9512 * per-cpu events.
9513 */
9514 if (group_leader->ctx->task != ctx->task)
9515 goto err_context;
9516
9517 /*
9518 * Make sure we're both events for the same CPU;
9519 * grouping events for different CPUs is broken; since
9520 * you can never concurrently schedule them anyhow.
9521 */
9522 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009523 goto err_context;
9524 } else {
9525 if (group_leader->ctx != ctx)
9526 goto err_context;
9527 }
9528
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009529 /*
9530 * Only a group leader can be exclusive or pinned
9531 */
9532 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009533 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009534 }
9535
9536 if (output_event) {
9537 err = perf_event_set_output(event, output_event);
9538 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009539 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02009540 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009541
Yann Droneauda21b0b32014-01-05 21:36:33 +01009542 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9543 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04009544 if (IS_ERR(event_file)) {
9545 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +02009546 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009547 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04009548 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009549
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009550 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009551 gctx = group_leader->ctx;
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009552 mutex_lock_double(&gctx->mutex, &ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009553 if (gctx->task == TASK_TOMBSTONE) {
9554 err = -ESRCH;
9555 goto err_locked;
9556 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009557 } else {
9558 mutex_lock(&ctx->mutex);
9559 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009560
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009561 if (ctx->task == TASK_TOMBSTONE) {
9562 err = -ESRCH;
9563 goto err_locked;
9564 }
9565
Peter Zijlstraa7239682015-09-09 19:06:33 +02009566 if (!perf_event_validate_size(event)) {
9567 err = -E2BIG;
9568 goto err_locked;
9569 }
9570
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009571 /*
9572 * Must be under the same ctx::mutex as perf_install_in_context(),
9573 * because we need to serialize with concurrent event creation.
9574 */
9575 if (!exclusive_event_installable(event, ctx)) {
9576 /* exclusive and group stuff are assumed mutually exclusive */
9577 WARN_ON_ONCE(move_group);
9578
9579 err = -EBUSY;
9580 goto err_locked;
9581 }
9582
9583 WARN_ON_ONCE(ctx->parent_ctx);
9584
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009585 /*
9586 * This is the point on no return; we cannot fail hereafter. This is
9587 * where we start modifying current state.
9588 */
9589
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009590 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009591 /*
9592 * See perf_event_ctx_lock() for comments on the details
9593 * of swizzling perf_event::ctx.
9594 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009595 perf_remove_from_context(group_leader, 0);
Jiri Olsa0231bb52013-02-01 11:23:45 +01009596
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009597 list_for_each_entry(sibling, &group_leader->sibling_list,
9598 group_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009599 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009600 put_ctx(gctx);
9601 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009602
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009603 /*
9604 * Wait for everybody to stop referencing the events through
9605 * the old lists, before installing it on new lists.
9606 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009607 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009608
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009609 /*
9610 * Install the group siblings before the group leader.
9611 *
9612 * Because a group leader will try and install the entire group
9613 * (through the sibling list, which is still in-tact), we can
9614 * end up with siblings installed in the wrong context.
9615 *
9616 * By installing siblings first we NO-OP because they're not
9617 * reachable through the group lists.
9618 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009619 list_for_each_entry(sibling, &group_leader->sibling_list,
9620 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009621 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01009622 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009623 get_ctx(ctx);
9624 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009625
9626 /*
9627 * Removing from the context ends up with disabled
9628 * event. What we want here is event in the initial
9629 * startup state, ready to be add into new context.
9630 */
9631 perf_event__state_init(group_leader);
9632 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9633 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02009634
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009635 /*
9636 * Now that all events are installed in @ctx, nothing
9637 * references @gctx anymore, so drop the last reference we have
9638 * on it.
9639 */
9640 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009641 }
9642
Peter Zijlstraf73e22a2015-09-09 20:48:22 +02009643 /*
9644 * Precalculate sample_data sizes; do while holding ctx::mutex such
9645 * that we're serialized against further additions and before
9646 * perf_install_in_context() which is the point the event is active and
9647 * can use these values.
9648 */
9649 perf_event__header_size(event);
9650 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009651
Peter Zijlstra78cd2c72016-01-25 14:08:45 +01009652 event->owner = current;
9653
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08009654 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009655 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009656
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009657 if (move_group)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009658 mutex_unlock(&gctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009659 mutex_unlock(&ctx->mutex);
9660
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009661 if (task) {
9662 mutex_unlock(&task->signal->cred_guard_mutex);
9663 put_task_struct(task);
9664 }
9665
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009666 put_online_cpus();
9667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009668 mutex_lock(&current->perf_event_mutex);
9669 list_add_tail(&event->owner_entry, &current->perf_event_list);
9670 mutex_unlock(&current->perf_event_mutex);
9671
Peter Zijlstra8a495422010-05-27 15:47:49 +02009672 /*
9673 * Drop the reference on the group_event after placing the
9674 * new event on the sibling_list. This ensures destruction
9675 * of the group leader will find the pointer to itself in
9676 * perf_group_detach().
9677 */
Al Viro2903ff02012-08-28 12:52:22 -04009678 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009679 fd_install(event_fd, event_file);
9680 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009681
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +02009682err_locked:
9683 if (move_group)
9684 mutex_unlock(&gctx->mutex);
9685 mutex_unlock(&ctx->mutex);
9686/* err_file: */
9687 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009688err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009689 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04009690 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02009691err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +01009692 /*
9693 * If event_file is set, the fput() above will have called ->release()
9694 * and that will take care of freeing the event.
9695 */
9696 if (!event_file)
9697 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009698err_cred:
9699 if (task)
9700 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009701err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08009702 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02009703err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02009704 if (task)
9705 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009706err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04009707 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04009708err_fd:
9709 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009710 return err;
9711}
9712
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009713/**
9714 * perf_event_create_kernel_counter
9715 *
9716 * @attr: attributes of the counter to create
9717 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07009718 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009719 */
9720struct perf_event *
9721perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07009722 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03009723 perf_overflow_handler_t overflow_handler,
9724 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009725{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009726 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009727 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009728 int err;
9729
9730 /*
9731 * Get the target context (task or percpu):
9732 */
9733
Avi Kivity4dc0da82011-06-29 18:42:35 +03009734 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00009735 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009736 if (IS_ERR(event)) {
9737 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009738 goto err;
9739 }
9740
Jiri Olsaf8697762014-08-01 14:33:01 +02009741 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009742 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +02009743
Yan, Zheng4af57ef282014-11-04 21:56:01 -05009744 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009745 if (IS_ERR(ctx)) {
9746 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009747 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009748 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009749
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009750 WARN_ON_ONCE(ctx->parent_ctx);
9751 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009752 if (ctx->task == TASK_TOMBSTONE) {
9753 err = -ESRCH;
9754 goto err_unlock;
9755 }
9756
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009757 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009758 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009759 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +02009760 }
9761
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009762 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01009763 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009764 mutex_unlock(&ctx->mutex);
9765
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009766 return event;
9767
Peter Zijlstra84c4e622016-02-24 18:45:40 +01009768err_unlock:
9769 mutex_unlock(&ctx->mutex);
9770 perf_unpin_context(ctx);
9771 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02009772err_free:
9773 free_event(event);
9774err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01009775 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02009776}
9777EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9778
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009779void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
9780{
9781 struct perf_event_context *src_ctx;
9782 struct perf_event_context *dst_ctx;
9783 struct perf_event *event, *tmp;
9784 LIST_HEAD(events);
9785
9786 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
9787 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
9788
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009789 /*
9790 * See perf_event_ctx_lock() for comments on the details
9791 * of swizzling perf_event::ctx.
9792 */
9793 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009794 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
9795 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +01009796 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009797 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009798 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02009799 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009800 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009801
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009802 /*
9803 * Wait for the events to quiesce before re-instating them.
9804 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009805 synchronize_rcu();
9806
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01009807 /*
9808 * Re-instate events in 2 passes.
9809 *
9810 * Skip over group leaders and only install siblings on this first
9811 * pass, siblings will not get enabled without a leader, however a
9812 * leader will enable its siblings, even if those are still on the old
9813 * context.
9814 */
9815 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9816 if (event->group_leader == event)
9817 continue;
9818
9819 list_del(&event->migrate_entry);
9820 if (event->state >= PERF_EVENT_STATE_OFF)
9821 event->state = PERF_EVENT_STATE_INACTIVE;
9822 account_event_cpu(event, dst_cpu);
9823 perf_install_in_context(dst_ctx, event, dst_cpu);
9824 get_ctx(dst_ctx);
9825 }
9826
9827 /*
9828 * Once all the siblings are setup properly, install the group leaders
9829 * to make it go.
9830 */
Peter Zijlstra98861672013-10-03 16:02:23 +02009831 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9832 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009833 if (event->state >= PERF_EVENT_STATE_OFF)
9834 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02009835 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009836 perf_install_in_context(dst_ctx, event, dst_cpu);
9837 get_ctx(dst_ctx);
9838 }
9839 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01009840 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08009841}
9842EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
9843
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009844static void sync_child_event(struct perf_event *child_event,
9845 struct task_struct *child)
9846{
9847 struct perf_event *parent_event = child_event->parent;
9848 u64 child_val;
9849
9850 if (child_event->attr.inherit_stat)
9851 perf_event_read_event(child_event, child);
9852
Peter Zijlstrab5e58792010-05-21 14:43:12 +02009853 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009854
9855 /*
9856 * Add back the child's count to the parent's count:
9857 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02009858 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009859 atomic64_add(child_event->total_time_enabled,
9860 &parent_event->child_total_time_enabled);
9861 atomic64_add(child_event->total_time_running,
9862 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009863}
9864
9865static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009866perf_event_exit_event(struct perf_event *child_event,
9867 struct perf_event_context *child_ctx,
9868 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009869{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009870 struct perf_event *parent_event = child_event->parent;
9871
Peter Zijlstra1903d502014-07-15 17:27:27 +02009872 /*
9873 * Do not destroy the 'original' grouping; because of the context
9874 * switch optimization the original events could've ended up in a
9875 * random child task.
9876 *
9877 * If we were to destroy the original group, all group related
9878 * operations would cease to function properly after this random
9879 * child dies.
9880 *
9881 * Do destroy all inherited groups, we don't care about those
9882 * and being thorough is better.
9883 */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009884 raw_spin_lock_irq(&child_ctx->lock);
9885 WARN_ON_ONCE(child_ctx->is_active);
9886
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009887 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +01009888 perf_group_detach(child_event);
9889 list_del_event(child_event, child_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01009890 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +01009891 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009893 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009894 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009895 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009896 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -04009897 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009898 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009899 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009900 /*
9901 * Child events can be cleaned up.
9902 */
9903
9904 sync_child_event(child_event, child);
9905
9906 /*
9907 * Remove this event from the parent's list
9908 */
9909 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9910 mutex_lock(&parent_event->child_mutex);
9911 list_del_init(&child_event->child_list);
9912 mutex_unlock(&parent_event->child_mutex);
9913
9914 /*
9915 * Kick perf_poll() for is_event_hup().
9916 */
9917 perf_event_wakeup(parent_event);
9918 free_event(child_event);
9919 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009920}
9921
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009922static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009923{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009924 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009925 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009926
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009927 WARN_ON_ONCE(child != current);
9928
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009929 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009930 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009931 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009932
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009933 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009934 * In order to reduce the amount of tricky in ctx tear-down, we hold
9935 * ctx::mutex over the entire thing. This serializes against almost
9936 * everything that wants to access the ctx.
9937 *
9938 * The exception is sys_perf_event_open() /
9939 * perf_event_create_kernel_count() which does find_get_context()
9940 * without ctx::mutex (it cannot because of the move_group double mutex
9941 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009942 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009943 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009944
9945 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009946 * In a single ctx::lock section, de-schedule the events and detach the
9947 * context from the task such that we cannot ever get it scheduled back
9948 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009949 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009950 raw_spin_lock_irq(&child_ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009951 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009952
9953 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009954 * Now that the context is inactive, destroy the task <-> ctx relation
9955 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009956 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +01009957 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
9958 put_ctx(child_ctx); /* cannot be last */
9959 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
9960 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009961
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009962 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +01009963 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009964
Peter Zijlstra211de6e2014-09-30 19:23:08 +02009965 if (clone_ctx)
9966 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02009967
9968 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009969 * Report the task dead after unscheduling the events so that we
9970 * won't get any samples after PERF_RECORD_EXIT. We can however still
9971 * get a few PERF_RECORD_READ events.
9972 */
9973 perf_event_task(child, child_ctx, 0);
9974
Peter Zijlstraebf905f2014-05-29 19:00:24 +02009975 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01009976 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01009977
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009978 mutex_unlock(&child_ctx->mutex);
9979
9980 put_ctx(child_ctx);
9981}
9982
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009983/*
9984 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02009985 *
9986 * Can be called with cred_guard_mutex held when called from
9987 * install_exec_creds().
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009988 */
9989void perf_event_exit_task(struct task_struct *child)
9990{
Peter Zijlstra88821352010-11-09 19:01:43 +01009991 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02009992 int ctxn;
9993
Peter Zijlstra88821352010-11-09 19:01:43 +01009994 mutex_lock(&child->perf_event_mutex);
9995 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
9996 owner_entry) {
9997 list_del_init(&event->owner_entry);
9998
9999 /*
10000 * Ensure the list deletion is visible before we clear
10001 * the owner, closes a race against perf_release() where
10002 * we need to serialize on the owner->perf_event_mutex.
10003 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010010004 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010010005 }
10006 mutex_unlock(&child->perf_event_mutex);
10007
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010008 for_each_task_context_nr(ctxn)
10009 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010010010
10011 /*
10012 * The perf_event_exit_task_context calls perf_event_task
10013 * with child's task_ctx, which generates EXIT events for
10014 * child contexts and sets child->perf_event_ctxp[] to NULL.
10015 * At this point we need to send EXIT events to cpu contexts.
10016 */
10017 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010018}
10019
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010020static void perf_free_event(struct perf_event *event,
10021 struct perf_event_context *ctx)
10022{
10023 struct perf_event *parent = event->parent;
10024
10025 if (WARN_ON_ONCE(!parent))
10026 return;
10027
10028 mutex_lock(&parent->child_mutex);
10029 list_del_init(&event->child_list);
10030 mutex_unlock(&parent->child_mutex);
10031
Al Viroa6fa9412012-08-20 14:59:25 +010010032 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010033
Peter Zijlstra652884f2015-01-23 11:20:10 +010010034 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020010035 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010036 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010010037 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010038 free_event(event);
10039}
10040
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010041/*
Peter Zijlstra652884f2015-01-23 11:20:10 +010010042 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010043 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010010044 *
10045 * Not all locks are strictly required, but take them anyway to be nice and
10046 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010047 */
10048void perf_event_free_task(struct task_struct *task)
10049{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010050 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010051 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010052 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010053
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010054 for_each_task_context_nr(ctxn) {
10055 ctx = task->perf_event_ctxp[ctxn];
10056 if (!ctx)
10057 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010058
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010059 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010060again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010061 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
10062 group_entry)
10063 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010064
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010065 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
10066 group_entry)
10067 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010068
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010069 if (!list_empty(&ctx->pinned_groups) ||
10070 !list_empty(&ctx->flexible_groups))
10071 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010072
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010073 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010074
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010075 put_ctx(ctx);
10076 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010077}
10078
Peter Zijlstra4e231c72010-09-09 21:01:59 +020010079void perf_event_delayed_put(struct task_struct *task)
10080{
10081 int ctxn;
10082
10083 for_each_task_context_nr(ctxn)
10084 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
10085}
10086
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010087struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000010088{
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010089 struct file *file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010090
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010091 file = fget_raw(fd);
10092 if (!file)
10093 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000010094
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010095 if (file->f_op != &perf_fops) {
10096 fput(file);
10097 return ERR_PTR(-EBADF);
10098 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000010099
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080010100 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000010101}
10102
10103const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
10104{
10105 if (!event)
10106 return ERR_PTR(-EINVAL);
10107
10108 return &event->attr;
10109}
10110
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010111/*
10112 * inherit a event from parent task to child task:
10113 */
10114static struct perf_event *
10115inherit_event(struct perf_event *parent_event,
10116 struct task_struct *parent,
10117 struct perf_event_context *parent_ctx,
10118 struct task_struct *child,
10119 struct perf_event *group_leader,
10120 struct perf_event_context *child_ctx)
10121{
Jiri Olsa1929def2014-09-12 13:18:27 +020010122 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010123 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020010124 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010125
10126 /*
10127 * Instead of creating recursive hierarchies of events,
10128 * we link inherited events back to the original parent,
10129 * which has a filp for sure, which we use as the reference
10130 * count:
10131 */
10132 if (parent_event->parent)
10133 parent_event = parent_event->parent;
10134
10135 child_event = perf_event_alloc(&parent_event->attr,
10136 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010137 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010138 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000010139 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010140 if (IS_ERR(child_event))
10141 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010010142
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010143 /*
10144 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10145 * must be under the same lock in order to serialize against
10146 * perf_event_release_kernel(), such that either we must observe
10147 * is_orphaned_event() or they will observe us on the child_list.
10148 */
10149 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020010150 if (is_orphaned_event(parent_event) ||
10151 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020010152 mutex_unlock(&parent_event->child_mutex);
Al Viroa6fa9412012-08-20 14:59:25 +010010153 free_event(child_event);
10154 return NULL;
10155 }
10156
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010157 get_ctx(child_ctx);
10158
10159 /*
10160 * Make the child state follow the state of the parent event,
10161 * not its attr.disabled bit. We hold the parent's mutex,
10162 * so we won't race with perf_event_{en, dis}able_family.
10163 */
Jiri Olsa1929def2014-09-12 13:18:27 +020010164 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010165 child_event->state = PERF_EVENT_STATE_INACTIVE;
10166 else
10167 child_event->state = PERF_EVENT_STATE_OFF;
10168
10169 if (parent_event->attr.freq) {
10170 u64 sample_period = parent_event->hw.sample_period;
10171 struct hw_perf_event *hwc = &child_event->hw;
10172
10173 hwc->sample_period = sample_period;
10174 hwc->last_period = sample_period;
10175
10176 local64_set(&hwc->period_left, sample_period);
10177 }
10178
10179 child_event->ctx = child_ctx;
10180 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010181 child_event->overflow_handler_context
10182 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010183
10184 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020010185 * Precalculate sample_data sizes
10186 */
10187 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020010188 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020010189
10190 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010191 * Link it up in the child's context:
10192 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020010193 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010194 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020010195 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010196
10197 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010198 * Link this into the parent event's child list
10199 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020010200 list_add_tail(&child_event->child_list, &parent_event->child_list);
10201 mutex_unlock(&parent_event->child_mutex);
10202
10203 return child_event;
10204}
10205
10206static int inherit_group(struct perf_event *parent_event,
10207 struct task_struct *parent,
10208 struct perf_event_context *parent_ctx,
10209 struct task_struct *child,
10210 struct perf_event_context *child_ctx)
10211{
10212 struct perf_event *leader;
10213 struct perf_event *sub;
10214 struct perf_event *child_ctr;
10215
10216 leader = inherit_event(parent_event, parent, parent_ctx,
10217 child, NULL, child_ctx);
10218 if (IS_ERR(leader))
10219 return PTR_ERR(leader);
10220 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10221 child_ctr = inherit_event(sub, parent, parent_ctx,
10222 child, leader, child_ctx);
10223 if (IS_ERR(child_ctr))
10224 return PTR_ERR(child_ctr);
10225 }
10226 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010227}
10228
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010229static int
10230inherit_task_group(struct perf_event *event, struct task_struct *parent,
10231 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010232 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010233 int *inherited_all)
10234{
10235 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010236 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010237
10238 if (!event->attr.inherit) {
10239 *inherited_all = 0;
10240 return 0;
10241 }
10242
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010243 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010244 if (!child_ctx) {
10245 /*
10246 * This is executed from the parent task context, so
10247 * inherit events that have been marked for cloning.
10248 * First allocate and initialize a context for the
10249 * child.
10250 */
10251
Jiri Olsa734df5a2013-07-09 17:44:10 +020010252 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010253 if (!child_ctx)
10254 return -ENOMEM;
10255
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010256 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010257 }
10258
10259 ret = inherit_group(event, parent, parent_ctx,
10260 child, child_ctx);
10261
10262 if (ret)
10263 *inherited_all = 0;
10264
10265 return ret;
10266}
10267
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010268/*
10269 * Initialize the perf_event context in task_struct
10270 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020010271static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010272{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010273 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010274 struct perf_event_context *cloned_ctx;
10275 struct perf_event *event;
10276 struct task_struct *parent = current;
10277 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010278 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010279 int ret = 0;
10280
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010281 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010282 return 0;
10283
10284 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010285 * If the parent's context is a clone, pin it so it won't get
10286 * swapped under us.
10287 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010288 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020010289 if (!parent_ctx)
10290 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010291
10292 /*
10293 * No need to check if parent_ctx != NULL here; since we saw
10294 * it non-NULL earlier, the only reason for it to become NULL
10295 * is if we exit, and since we're currently in the middle of
10296 * a fork we can't be exiting at the same time.
10297 */
10298
10299 /*
10300 * Lock the parent list. No need to lock the child - not PID
10301 * hashed yet and not running, so nobody can access it.
10302 */
10303 mutex_lock(&parent_ctx->mutex);
10304
10305 /*
10306 * We dont have to disable NMIs - we are only looking at
10307 * the list, not manipulating it:
10308 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010309 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010310 ret = inherit_task_group(event, parent, parent_ctx,
10311 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010312 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010313 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010314 }
10315
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010316 /*
10317 * We can't hold ctx->lock when iterating the ->flexible_group list due
10318 * to allocations, but we need to prevent rotation because
10319 * rotate_ctx() will change the list from interrupt context.
10320 */
10321 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10322 parent_ctx->rotate_disable = 1;
10323 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10324
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010325 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010326 ret = inherit_task_group(event, parent, parent_ctx,
10327 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010328 if (ret)
10329 break;
10330 }
10331
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010332 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10333 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010010334
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010335 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010010336
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010010337 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010338 /*
10339 * Mark the child context as a clone of the parent
10340 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010341 *
10342 * Note that if the parent is a clone, the holding of
10343 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010344 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010345 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010346 if (cloned_ctx) {
10347 child_ctx->parent_ctx = cloned_ctx;
10348 child_ctx->parent_gen = parent_ctx->parent_gen;
10349 } else {
10350 child_ctx->parent_ctx = parent_ctx;
10351 child_ctx->parent_gen = parent_ctx->generation;
10352 }
10353 get_ctx(child_ctx->parent_ctx);
10354 }
10355
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010010356 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010357 mutex_unlock(&parent_ctx->mutex);
10358
10359 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010010360 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010361
10362 return ret;
10363}
10364
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010365/*
10366 * Initialize the perf_event context in task_struct
10367 */
10368int perf_event_init_task(struct task_struct *child)
10369{
10370 int ctxn, ret;
10371
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010010372 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10373 mutex_init(&child->perf_event_mutex);
10374 INIT_LIST_HEAD(&child->perf_event_list);
10375
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010376 for_each_task_context_nr(ctxn) {
10377 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010378 if (ret) {
10379 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010380 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070010381 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +020010382 }
10383
10384 return 0;
10385}
10386
Paul Mackerras220b1402010-03-10 20:45:52 +110010387static void __init perf_event_init_all_cpus(void)
10388{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010389 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110010390 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110010391
10392 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010393 swhash = &per_cpu(swevent_htable, cpu);
10394 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000010395 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070010396
10397 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
10398 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020010399
10400 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110010401 }
10402}
10403
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010404int perf_event_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010405{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010406 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010407
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010408 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000010409 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010410 struct swevent_hlist *hlist;
10411
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010412 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10413 WARN_ON(!hlist);
10414 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010415 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020010416 mutex_unlock(&swhash->hlist_mutex);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010417 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010418}
10419
Dave Young2965faa2015-09-09 15:38:55 -070010420#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010421static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010422{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010423 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010424 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10425 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010426
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010427 raw_spin_lock(&ctx->lock);
10428 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010010429 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010010430 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010431}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010432
10433static void perf_event_exit_cpu_context(int cpu)
10434{
10435 struct perf_event_context *ctx;
10436 struct pmu *pmu;
10437 int idx;
10438
10439 idx = srcu_read_lock(&pmus_srcu);
10440 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +020010441 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010442
10443 mutex_lock(&ctx->mutex);
10444 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10445 mutex_unlock(&ctx->mutex);
10446 }
10447 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010448}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010449#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010450
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010451static void perf_event_exit_cpu_context(int cpu) { }
10452
10453#endif
10454
10455int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010456{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010010457 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010458 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010459}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010460
Peter Zijlstrac2774432010-12-08 15:29:02 +010010461static int
10462perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10463{
10464 int cpu;
10465
10466 for_each_online_cpu(cpu)
10467 perf_event_exit_cpu(cpu);
10468
10469 return NOTIFY_OK;
10470}
10471
10472/*
10473 * Run the perf reboot notifier at the very last possible moment so that
10474 * the generic watchdog code runs as long as possible.
10475 */
10476static struct notifier_block perf_reboot_notifier = {
10477 .notifier_call = perf_reboot,
10478 .priority = INT_MIN,
10479};
10480
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010481void __init perf_event_init(void)
10482{
Jason Wessel3c502e72010-11-04 17:33:01 -050010483 int ret;
10484
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010485 idr_init(&pmu_idr);
10486
Paul Mackerras220b1402010-03-10 20:45:52 +110010487 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010488 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010489 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10490 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10491 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010492 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000010493 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010010494 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050010495
10496 ret = init_hw_breakpoint();
10497 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020010498
Jiri Olsab01c3a02012-03-23 15:41:20 +010010499 /*
10500 * Build time assertion that we keep the data_head at the intended
10501 * location. IOW, validation we got the __reserved[] size right.
10502 */
10503 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10504 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010505}
Peter Zijlstraabe43402010-11-17 23:17:37 +010010506
Cody P Schaferfd979c02015-01-30 13:45:57 -080010507ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10508 char *page)
10509{
10510 struct perf_pmu_events_attr *pmu_attr =
10511 container_of(attr, struct perf_pmu_events_attr, attr);
10512
10513 if (pmu_attr->event_str)
10514 return sprintf(page, "%s\n", pmu_attr->event_str);
10515
10516 return 0;
10517}
Thomas Gleixner675965b2016-02-22 22:19:27 +000010518EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080010519
Peter Zijlstraabe43402010-11-17 23:17:37 +010010520static int __init perf_event_sysfs_init(void)
10521{
10522 struct pmu *pmu;
10523 int ret;
10524
10525 mutex_lock(&pmus_lock);
10526
10527 ret = bus_register(&pmu_bus);
10528 if (ret)
10529 goto unlock;
10530
10531 list_for_each_entry(pmu, &pmus, entry) {
10532 if (!pmu->name || pmu->type < 0)
10533 continue;
10534
10535 ret = pmu_dev_alloc(pmu);
10536 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10537 }
10538 pmu_bus_running = 1;
10539 ret = 0;
10540
10541unlock:
10542 mutex_unlock(&pmus_lock);
10543
10544 return ret;
10545}
10546device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010547
10548#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040010549static struct cgroup_subsys_state *
10550perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010551{
10552 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020010553
Li Zefan1b15d052011-03-03 14:26:06 +080010554 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010555 if (!jc)
10556 return ERR_PTR(-ENOMEM);
10557
Stephane Eraniane5d13672011-02-14 11:20:01 +020010558 jc->info = alloc_percpu(struct perf_cgroup_info);
10559 if (!jc->info) {
10560 kfree(jc);
10561 return ERR_PTR(-ENOMEM);
10562 }
10563
Stephane Eraniane5d13672011-02-14 11:20:01 +020010564 return &jc->css;
10565}
10566
Tejun Heoeb954192013-08-08 20:11:23 -040010567static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010568{
Tejun Heoeb954192013-08-08 20:11:23 -040010569 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10570
Stephane Eraniane5d13672011-02-14 11:20:01 +020010571 free_percpu(jc->info);
10572 kfree(jc);
10573}
10574
10575static int __perf_cgroup_move(void *info)
10576{
10577 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010578 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010579 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010010580 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020010581 return 0;
10582}
10583
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010584static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020010585{
Tejun Heobb9d97b2011-12-12 18:12:21 -080010586 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010587 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080010588
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050010589 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080010590 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020010591}
10592
Tejun Heo073219e2014-02-08 10:36:58 -050010593struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080010594 .css_alloc = perf_cgroup_css_alloc,
10595 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080010596 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +020010597};
10598#endif /* CONFIG_CGROUP_PERF */