blob: 2d9aeba1f3e206754b732d93ce9b4dc4ae6408ae [file] [log] [blame]
Thomas Gleixner8e86e012019-01-16 12:10:59 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02003 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004 *
5 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02006 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
Peter Zijlstra90eec102015-11-16 11:08:45 +01007 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
Al Virod36b6912011-12-29 17:09:01 -05008 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009 */
10
11#include <linux/fs.h>
12#include <linux/mm.h>
13#include <linux/cpu.h>
14#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010015#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020016#include <linux/file.h>
17#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020019#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020020#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020021#include <linux/sysfs.h>
22#include <linux/dcache.h>
23#include <linux/percpu.h>
24#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010025#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010027#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040028#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020029#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020030#include <linux/hardirq.h>
31#include <linux/rculist.h>
32#include <linux/uaccess.h>
33#include <linux/syscalls.h>
34#include <linux/anon_inodes.h>
35#include <linux/kernel_stat.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000036#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020037#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040038#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080041#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040042#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010043#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070044#include <linux/bpf.h>
45#include <linux/filter.h>
Alexander Shishkin375637b2016-04-27 18:44:46 +030046#include <linux/namei.h>
47#include <linux/parser.h>
Ingo Molnare6017572017-02-01 16:36:40 +010048#include <linux/sched/clock.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010049#include <linux/sched/mm.h>
Hari Bathinie4222672017-03-08 02:11:36 +053050#include <linux/proc_ns.h>
51#include <linux/mount.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020052
Frederic Weisbecker76369132011-05-19 19:55:04 +020053#include "internal.h"
54
Ingo Molnarcdd6c482009-09-21 12:02:48 +020055#include <asm/irq_regs.h>
56
Peter Zijlstra272325c2015-04-15 11:41:58 +020057typedef int (*remote_function_f)(void *);
58
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010059struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020060 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020061 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020062 void *info;
63 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010064};
65
66static void remote_function(void *data)
67{
68 struct remote_function_call *tfc = data;
69 struct task_struct *p = tfc->p;
70
71 if (p) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +010072 /* -EAGAIN */
73 if (task_cpu(p) != smp_processor_id())
74 return;
75
76 /*
77 * Now that we're on right CPU with IRQs disabled, we can test
78 * if we hit the right task without races.
79 */
80
81 tfc->ret = -ESRCH; /* No such (running) process */
82 if (p != current)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010083 return;
84 }
85
86 tfc->ret = tfc->func(tfc->info);
87}
88
89/**
90 * task_function_call - call a function on the cpu on which a task runs
91 * @p: the task to evaluate
92 * @func: the function to be called
93 * @info: the function call argument
94 *
95 * Calls the function @func when the task is currently running. This might
96 * be on the current CPU, which just calls the function directly
97 *
98 * returns: @func return value, or
99 * -ESRCH - when the process isn't running
100 * -EAGAIN - when the process moved away
101 */
102static int
Peter Zijlstra272325c2015-04-15 11:41:58 +0200103task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100104{
105 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200106 .p = p,
107 .func = func,
108 .info = info,
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100109 .ret = -EAGAIN,
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100110 };
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100111 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100112
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100113 do {
114 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
115 if (!ret)
116 ret = data.ret;
117 } while (ret == -EAGAIN);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100118
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100119 return ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100120}
121
122/**
123 * cpu_function_call - call a function on the cpu
124 * @func: the function to be called
125 * @info: the function call argument
126 *
127 * Calls the function @func on the remote cpu.
128 *
129 * returns: @func return value or -ENXIO when the cpu is offline
130 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200131static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100132{
133 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200134 .p = NULL,
135 .func = func,
136 .info = info,
137 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100138 };
139
140 smp_call_function_single(cpu, remote_function, &data, 1);
141
142 return data.ret;
143}
144
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100145static inline struct perf_cpu_context *
146__get_cpu_context(struct perf_event_context *ctx)
147{
148 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
149}
150
151static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
152 struct perf_event_context *ctx)
153{
154 raw_spin_lock(&cpuctx->ctx.lock);
155 if (ctx)
156 raw_spin_lock(&ctx->lock);
157}
158
159static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
160 struct perf_event_context *ctx)
161{
162 if (ctx)
163 raw_spin_unlock(&ctx->lock);
164 raw_spin_unlock(&cpuctx->ctx.lock);
165}
166
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100167#define TASK_TOMBSTONE ((void *)-1L)
168
169static bool is_kernel_event(struct perf_event *event)
170{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100171 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100172}
173
Peter Zijlstra39a43642016-01-11 12:46:35 +0100174/*
175 * On task ctx scheduling...
176 *
177 * When !ctx->nr_events a task context will not be scheduled. This means
178 * we can disable the scheduler hooks (for performance) without leaving
179 * pending task ctx state.
180 *
181 * This however results in two special cases:
182 *
183 * - removing the last event from a task ctx; this is relatively straight
184 * forward and is done in __perf_remove_from_context.
185 *
186 * - adding the first event to a task ctx; this is tricky because we cannot
187 * rely on ctx->is_active and therefore cannot use event_function_call().
188 * See perf_install_in_context().
189 *
Peter Zijlstra39a43642016-01-11 12:46:35 +0100190 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
191 */
192
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100193typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
194 struct perf_event_context *, void *);
195
196struct event_function_struct {
197 struct perf_event *event;
198 event_f func;
199 void *data;
200};
201
202static int event_function(void *info)
203{
204 struct event_function_struct *efs = info;
205 struct perf_event *event = efs->event;
206 struct perf_event_context *ctx = event->ctx;
207 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
208 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100209 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100210
Frederic Weisbecker16444642017-11-06 16:01:24 +0100211 lockdep_assert_irqs_disabled();
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100212
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100213 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100214 /*
215 * Since we do the IPI call without holding ctx->lock things can have
216 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100217 */
218 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100219 if (ctx->task != current) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100220 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100221 goto unlock;
222 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100223
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100224 /*
225 * We only use event_function_call() on established contexts,
226 * and event_function() is only ever called when active (or
227 * rather, we'll have bailed in task_function_call() or the
228 * above ctx->task != current test), therefore we must have
229 * ctx->is_active here.
230 */
231 WARN_ON_ONCE(!ctx->is_active);
232 /*
233 * And since we have ctx->is_active, cpuctx->task_ctx must
234 * match.
235 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100236 WARN_ON_ONCE(task_ctx != ctx);
237 } else {
238 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100239 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100240
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100241 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100242unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100243 perf_ctx_unlock(cpuctx, task_ctx);
244
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100245 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100246}
247
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100248static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100249{
250 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100251 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100252 struct event_function_struct efs = {
253 .event = event,
254 .func = func,
255 .data = data,
256 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100257
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100258 if (!event->parent) {
259 /*
260 * If this is a !child event, we must hold ctx::mutex to
261 * stabilize the the event->ctx relation. See
262 * perf_event_ctx_lock().
263 */
264 lockdep_assert_held(&ctx->mutex);
265 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100266
267 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100268 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100269 return;
270 }
271
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100272 if (task == TASK_TOMBSTONE)
273 return;
274
Peter Zijlstraa0963092016-02-24 18:45:50 +0100275again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100276 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100277 return;
278
279 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100280 /*
281 * Reload the task pointer, it might have been changed by
282 * a concurrent perf_event_context_sched_out().
283 */
284 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100285 if (task == TASK_TOMBSTONE) {
286 raw_spin_unlock_irq(&ctx->lock);
287 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100288 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100289 if (ctx->is_active) {
290 raw_spin_unlock_irq(&ctx->lock);
291 goto again;
292 }
293 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100294 raw_spin_unlock_irq(&ctx->lock);
295}
296
Peter Zijlstracca20942016-08-16 13:33:26 +0200297/*
298 * Similar to event_function_call() + event_function(), but hard assumes IRQs
299 * are already disabled and we're on the right CPU.
300 */
301static void event_function_local(struct perf_event *event, event_f func, void *data)
302{
303 struct perf_event_context *ctx = event->ctx;
304 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
305 struct task_struct *task = READ_ONCE(ctx->task);
306 struct perf_event_context *task_ctx = NULL;
307
Frederic Weisbecker16444642017-11-06 16:01:24 +0100308 lockdep_assert_irqs_disabled();
Peter Zijlstracca20942016-08-16 13:33:26 +0200309
310 if (task) {
311 if (task == TASK_TOMBSTONE)
312 return;
313
314 task_ctx = ctx;
315 }
316
317 perf_ctx_lock(cpuctx, task_ctx);
318
319 task = ctx->task;
320 if (task == TASK_TOMBSTONE)
321 goto unlock;
322
323 if (task) {
324 /*
325 * We must be either inactive or active and the right task,
326 * otherwise we're screwed, since we cannot IPI to somewhere
327 * else.
328 */
329 if (ctx->is_active) {
330 if (WARN_ON_ONCE(task != current))
331 goto unlock;
332
333 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
334 goto unlock;
335 }
336 } else {
337 WARN_ON_ONCE(&cpuctx->ctx != ctx);
338 }
339
340 func(event, cpuctx, ctx, data);
341unlock:
342 perf_ctx_unlock(cpuctx, task_ctx);
343}
344
Stephane Eraniane5d13672011-02-14 11:20:01 +0200345#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
346 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100347 PERF_FLAG_PID_CGROUP |\
348 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200349
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100350/*
351 * branch priv levels that need permission checks
352 */
353#define PERF_SAMPLE_BRANCH_PERM_PLM \
354 (PERF_SAMPLE_BRANCH_KERNEL |\
355 PERF_SAMPLE_BRANCH_HV)
356
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200357enum event_type_t {
358 EVENT_FLEXIBLE = 0x1,
359 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100360 EVENT_TIME = 0x4,
Alexander Shishkin487f05e2017-01-19 18:43:30 +0200361 /* see ctx_resched() for details */
362 EVENT_CPU = 0x8,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200363 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
364};
365
Stephane Eraniane5d13672011-02-14 11:20:01 +0200366/*
367 * perf_sched_events : >0 events exist
368 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
369 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100370
371static void perf_sched_delayed(struct work_struct *work);
372DEFINE_STATIC_KEY_FALSE(perf_sched_events);
373static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
374static DEFINE_MUTEX(perf_sched_mutex);
375static atomic_t perf_sched_count;
376
Stephane Eraniane5d13672011-02-14 11:20:01 +0200377static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500378static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Kan Liangf2fb6be2016-03-23 11:24:37 -0700379static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200380
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381static atomic_t nr_mmap_events __read_mostly;
382static atomic_t nr_comm_events __read_mostly;
Hari Bathinie4222672017-03-08 02:11:36 +0530383static atomic_t nr_namespaces_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200384static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200385static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300386static atomic_t nr_switch_events __read_mostly;
Song Liu76193a92019-01-17 08:15:13 -0800387static atomic_t nr_ksymbol_events __read_mostly;
Song Liu6ee52e22019-01-17 08:15:15 -0800388static atomic_t nr_bpf_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200389
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200390static LIST_HEAD(pmus);
391static DEFINE_MUTEX(pmus_lock);
392static struct srcu_struct pmus_srcu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +0200393static cpumask_var_t perf_online_mask;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200394
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200395/*
396 * perf event paranoia level:
397 * -1 - not paranoid at all
398 * 0 - disallow raw tracepoint access for unpriv
399 * 1 - disallow cpu events for unpriv
400 * 2 - disallow kernel profiling for unpriv
401 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700402int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200403
Frederic Weisbecker20443382011-03-31 03:33:29 +0200404/* Minimum for 512 kiB + 1 user control page */
405int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200406
407/*
408 * max perf event sample rate
409 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700410#define DEFAULT_MAX_SAMPLE_RATE 100000
411#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
412#define DEFAULT_CPU_TIME_MAX_PERCENT 25
413
414int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
415
416static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
417static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
418
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200419static int perf_sample_allowed_ns __read_mostly =
420 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700421
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800422static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700423{
424 u64 tmp = perf_sample_period_ns;
425
426 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100427 tmp = div_u64(tmp, 100);
428 if (!tmp)
429 tmp = 1;
430
431 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700432}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100433
Peter Zijlstra8d5bce02018-03-09 14:56:27 +0100434static bool perf_rotate_context(struct perf_cpu_context *cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +0200435
Peter Zijlstra163ec432011-02-16 11:22:34 +0100436int perf_proc_update_handler(struct ctl_table *table, int write,
437 void __user *buffer, size_t *lenp,
438 loff_t *ppos)
439{
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800440 int ret;
441 int perf_cpu = sysctl_perf_cpu_time_max_percent;
Kan Liangab7fdef2016-05-03 00:26:06 -0700442 /*
443 * If throttling is disabled don't allow the write:
444 */
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800445 if (write && (perf_cpu == 100 || perf_cpu == 0))
Kan Liangab7fdef2016-05-03 00:26:06 -0700446 return -EINVAL;
447
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800448 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
449 if (ret || !write)
450 return ret;
451
Peter Zijlstra163ec432011-02-16 11:22:34 +0100452 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700453 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
454 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100455
456 return 0;
457}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200458
Dave Hansen14c63f12013-06-21 08:51:36 -0700459int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
460
461int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
462 void __user *buffer, size_t *lenp,
463 loff_t *ppos)
464{
Tan Xiaojun1572e452017-02-23 14:04:39 +0800465 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700466
467 if (ret || !write)
468 return ret;
469
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200470 if (sysctl_perf_cpu_time_max_percent == 100 ||
471 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100472 printk(KERN_WARNING
473 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
474 WRITE_ONCE(perf_sample_allowed_ns, 0);
475 } else {
476 update_perf_cpu_limits();
477 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700478
479 return 0;
480}
481
482/*
483 * perf samples are done in some very critical code paths (NMIs).
484 * If they take too much CPU time, the system can lock up and not
485 * get any real work done. This will drop the sample rate when
486 * we detect that events are taking too long.
487 */
488#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200489static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700490
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100491static u64 __report_avg;
492static u64 __report_allowed;
493
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100494static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700495{
David Ahern0d87d7e2016-08-01 13:49:29 -0700496 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100497 "perf: interrupt took too long (%lld > %lld), lowering "
498 "kernel.perf_event_max_sample_rate to %d\n",
499 __report_avg, __report_allowed,
500 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100501}
502
503static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
504
505void perf_sample_event_took(u64 sample_len_ns)
506{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100507 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
508 u64 running_len;
509 u64 avg_len;
510 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700511
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100512 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700513 return;
514
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100515 /* Decay the counter by 1 average sample. */
516 running_len = __this_cpu_read(running_sample_length);
517 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
518 running_len += sample_len_ns;
519 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700520
521 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100522 * Note: this will be biased artifically low until we have
523 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700524 * from having to maintain a count.
525 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100526 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
527 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700528 return;
529
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100530 __report_avg = avg_len;
531 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700532
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100533 /*
534 * Compute a throttle threshold 25% below the current duration.
535 */
536 avg_len += avg_len / 4;
537 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
538 if (avg_len < max)
539 max /= (u32)avg_len;
540 else
541 max = 1;
542
543 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
544 WRITE_ONCE(max_samples_per_tick, max);
545
546 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700547 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
548
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100549 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100550 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100551 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100552 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100553 sysctl_perf_event_sample_rate);
554 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700555}
556
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200557static atomic64_t perf_event_id;
558
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200559static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
560 enum event_type_t event_type);
561
562static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200563 enum event_type_t event_type,
564 struct task_struct *task);
565
566static void update_context_time(struct perf_event_context *ctx);
567static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200568
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200569void __weak perf_event_print_debug(void) { }
570
Matt Fleming84c79912010-10-03 21:41:13 +0100571extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200572{
Matt Fleming84c79912010-10-03 21:41:13 +0100573 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200574}
575
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200576static inline u64 perf_clock(void)
577{
578 return local_clock();
579}
580
Peter Zijlstra34f43922015-02-20 14:05:38 +0100581static inline u64 perf_event_clock(struct perf_event *event)
582{
583 return event->clock();
584}
585
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200586/*
587 * State based event timekeeping...
588 *
589 * The basic idea is to use event->state to determine which (if any) time
590 * fields to increment with the current delta. This means we only need to
591 * update timestamps when we change state or when they are explicitly requested
592 * (read).
593 *
594 * Event groups make things a little more complicated, but not terribly so. The
595 * rules for a group are that if the group leader is OFF the entire group is
596 * OFF, irrespecive of what the group member states are. This results in
597 * __perf_effective_state().
598 *
599 * A futher ramification is that when a group leader flips between OFF and
600 * !OFF, we need to update all group member times.
601 *
602 *
603 * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we
604 * need to make sure the relevant context time is updated before we try and
605 * update our timestamps.
606 */
607
608static __always_inline enum perf_event_state
609__perf_effective_state(struct perf_event *event)
610{
611 struct perf_event *leader = event->group_leader;
612
613 if (leader->state <= PERF_EVENT_STATE_OFF)
614 return leader->state;
615
616 return event->state;
617}
618
619static __always_inline void
620__perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running)
621{
622 enum perf_event_state state = __perf_effective_state(event);
623 u64 delta = now - event->tstamp;
624
625 *enabled = event->total_time_enabled;
626 if (state >= PERF_EVENT_STATE_INACTIVE)
627 *enabled += delta;
628
629 *running = event->total_time_running;
630 if (state >= PERF_EVENT_STATE_ACTIVE)
631 *running += delta;
632}
633
634static void perf_event_update_time(struct perf_event *event)
635{
636 u64 now = perf_event_time(event);
637
638 __perf_update_times(event, now, &event->total_time_enabled,
639 &event->total_time_running);
640 event->tstamp = now;
641}
642
643static void perf_event_update_sibling_time(struct perf_event *leader)
644{
645 struct perf_event *sibling;
646
Peter Zijlstraedb39592018-03-15 17:36:56 +0100647 for_each_sibling_event(sibling, leader)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200648 perf_event_update_time(sibling);
649}
650
651static void
652perf_event_set_state(struct perf_event *event, enum perf_event_state state)
653{
654 if (event->state == state)
655 return;
656
657 perf_event_update_time(event);
658 /*
659 * If a group leader gets enabled/disabled all its siblings
660 * are affected too.
661 */
662 if ((event->state < 0) ^ (state < 0))
663 perf_event_update_sibling_time(event);
664
665 WRITE_ONCE(event->state, state);
666}
667
Stephane Eraniane5d13672011-02-14 11:20:01 +0200668#ifdef CONFIG_CGROUP_PERF
669
Stephane Eraniane5d13672011-02-14 11:20:01 +0200670static inline bool
671perf_cgroup_match(struct perf_event *event)
672{
673 struct perf_event_context *ctx = event->ctx;
674 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
675
Tejun Heoef824fa2013-04-08 19:00:38 -0700676 /* @event doesn't care about cgroup */
677 if (!event->cgrp)
678 return true;
679
680 /* wants specific cgroup scope but @cpuctx isn't associated with any */
681 if (!cpuctx->cgrp)
682 return false;
683
684 /*
685 * Cgroup scoping is recursive. An event enabled for a cgroup is
686 * also enabled for all its descendant cgroups. If @cpuctx's
687 * cgroup is a descendant of @event's (the test covers identity
688 * case), it's a match.
689 */
690 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
691 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200692}
693
Stephane Eraniane5d13672011-02-14 11:20:01 +0200694static inline void perf_detach_cgroup(struct perf_event *event)
695{
Zefan Li4e2ba652014-09-19 16:53:14 +0800696 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200697 event->cgrp = NULL;
698}
699
700static inline int is_cgroup_event(struct perf_event *event)
701{
702 return event->cgrp != NULL;
703}
704
705static inline u64 perf_cgroup_event_time(struct perf_event *event)
706{
707 struct perf_cgroup_info *t;
708
709 t = per_cpu_ptr(event->cgrp->info, event->cpu);
710 return t->time;
711}
712
713static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
714{
715 struct perf_cgroup_info *info;
716 u64 now;
717
718 now = perf_clock();
719
720 info = this_cpu_ptr(cgrp->info);
721
722 info->time += now - info->timestamp;
723 info->timestamp = now;
724}
725
726static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
727{
Song Liuc917e0f22018-03-12 09:59:43 -0700728 struct perf_cgroup *cgrp = cpuctx->cgrp;
729 struct cgroup_subsys_state *css;
730
731 if (cgrp) {
732 for (css = &cgrp->css; css; css = css->parent) {
733 cgrp = container_of(css, struct perf_cgroup, css);
734 __update_cgrp_time(cgrp);
735 }
736 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200737}
738
739static inline void update_cgrp_time_from_event(struct perf_event *event)
740{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200741 struct perf_cgroup *cgrp;
742
Stephane Eraniane5d13672011-02-14 11:20:01 +0200743 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200744 * ensure we access cgroup data only when needed and
745 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200746 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200747 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200748 return;
749
Stephane Eranian614e4c42015-11-12 11:00:04 +0100750 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200751 /*
752 * Do not update time when cgroup is not active
753 */
Colin Ian King28fa7412018-10-29 23:32:11 +0000754 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200755 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200756}
757
758static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200759perf_cgroup_set_timestamp(struct task_struct *task,
760 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200761{
762 struct perf_cgroup *cgrp;
763 struct perf_cgroup_info *info;
Song Liuc917e0f22018-03-12 09:59:43 -0700764 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200765
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200766 /*
767 * ctx->lock held by caller
768 * ensure we do not access cgroup data
769 * unless we have the cgroup pinned (css_get)
770 */
771 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200772 return;
773
Stephane Eranian614e4c42015-11-12 11:00:04 +0100774 cgrp = perf_cgroup_from_task(task, ctx);
Song Liuc917e0f22018-03-12 09:59:43 -0700775
776 for (css = &cgrp->css; css; css = css->parent) {
777 cgrp = container_of(css, struct perf_cgroup, css);
778 info = this_cpu_ptr(cgrp->info);
779 info->timestamp = ctx->timestamp;
780 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200781}
782
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800783static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list);
784
Stephane Eraniane5d13672011-02-14 11:20:01 +0200785#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
786#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
787
788/*
789 * reschedule events based on the cgroup constraint of task.
790 *
791 * mode SWOUT : schedule out everything
792 * mode SWIN : schedule in based on cgroup for next
793 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800794static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200795{
796 struct perf_cpu_context *cpuctx;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800797 struct list_head *list;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200798 unsigned long flags;
799
800 /*
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800801 * Disable interrupts and preemption to avoid this CPU's
802 * cgrp_cpuctx_entry to change under us.
Stephane Eraniane5d13672011-02-14 11:20:01 +0200803 */
804 local_irq_save(flags);
805
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800806 list = this_cpu_ptr(&cgrp_cpuctx_list);
807 list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) {
808 WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200809
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800810 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
811 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200812
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800813 if (mode & PERF_CGROUP_SWOUT) {
814 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
815 /*
816 * must not be done before ctxswout due
817 * to event_filter_match() in event_sched_out()
818 */
819 cpuctx->cgrp = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200820 }
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800821
822 if (mode & PERF_CGROUP_SWIN) {
823 WARN_ON_ONCE(cpuctx->cgrp);
824 /*
825 * set cgrp before ctxsw in to allow
826 * event_filter_match() to not have to pass
827 * task around
828 * we pass the cpuctx->ctx to perf_cgroup_from_task()
829 * because cgorup events are only per-cpu
830 */
831 cpuctx->cgrp = perf_cgroup_from_task(task,
832 &cpuctx->ctx);
833 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
834 }
835 perf_pmu_enable(cpuctx->ctx.pmu);
836 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200837 }
838
Stephane Eraniane5d13672011-02-14 11:20:01 +0200839 local_irq_restore(flags);
840}
841
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200842static inline void perf_cgroup_sched_out(struct task_struct *task,
843 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200844{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200845 struct perf_cgroup *cgrp1;
846 struct perf_cgroup *cgrp2 = NULL;
847
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100848 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200849 /*
850 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100851 * we do not need to pass the ctx here because we know
852 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200853 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100854 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100855 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200856
857 /*
858 * only schedule out current cgroup events if we know
859 * that we are switching to a different cgroup. Otherwise,
860 * do no touch the cgroup events.
861 */
862 if (cgrp1 != cgrp2)
863 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100864
865 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200866}
867
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200868static inline void perf_cgroup_sched_in(struct task_struct *prev,
869 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200870{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200871 struct perf_cgroup *cgrp1;
872 struct perf_cgroup *cgrp2 = NULL;
873
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100874 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200875 /*
876 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100877 * we do not need to pass the ctx here because we know
878 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200879 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100880 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100881 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200882
883 /*
884 * only need to schedule in cgroup events if we are changing
885 * cgroup during ctxsw. Cgroup events were not scheduled
886 * out of ctxsw out if that was not the case.
887 */
888 if (cgrp1 != cgrp2)
889 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100890
891 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200892}
893
894static inline int perf_cgroup_connect(int fd, struct perf_event *event,
895 struct perf_event_attr *attr,
896 struct perf_event *group_leader)
897{
898 struct perf_cgroup *cgrp;
899 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400900 struct fd f = fdget(fd);
901 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200902
Al Viro2903ff02012-08-28 12:52:22 -0400903 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200904 return -EBADF;
905
Al Virob5830432014-10-31 01:22:04 -0400906 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400907 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800908 if (IS_ERR(css)) {
909 ret = PTR_ERR(css);
910 goto out;
911 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200912
913 cgrp = container_of(css, struct perf_cgroup, css);
914 event->cgrp = cgrp;
915
916 /*
917 * all events in a group must monitor
918 * the same cgroup because a task belongs
919 * to only one perf cgroup at a time
920 */
921 if (group_leader && group_leader->cgrp != cgrp) {
922 perf_detach_cgroup(event);
923 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200924 }
Li Zefan3db272c2011-03-03 14:25:37 +0800925out:
Al Viro2903ff02012-08-28 12:52:22 -0400926 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200927 return ret;
928}
929
930static inline void
931perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
932{
933 struct perf_cgroup_info *t;
934 t = per_cpu_ptr(event->cgrp->info, event->cpu);
935 event->shadow_ctx_time = now - t->timestamp;
936}
937
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700938/*
939 * Update cpuctx->cgrp so that it is set when first cgroup event is added and
940 * cleared when last cgroup event is removed.
941 */
942static inline void
943list_update_cgroup_event(struct perf_event *event,
944 struct perf_event_context *ctx, bool add)
945{
946 struct perf_cpu_context *cpuctx;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800947 struct list_head *cpuctx_entry;
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700948
949 if (!is_cgroup_event(event))
950 return;
951
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700952 /*
953 * Because cgroup events are always per-cpu events,
954 * this will always be called from the right CPU.
955 */
956 cpuctx = __get_cpu_context(ctx);
leilei.lin33801b92018-03-06 17:36:37 +0800957
958 /*
959 * Since setting cpuctx->cgrp is conditional on the current @cgrp
960 * matching the event's cgroup, we must do this for every new event,
961 * because if the first would mismatch, the second would not try again
962 * and we would leave cpuctx->cgrp unset.
963 */
964 if (add && !cpuctx->cgrp) {
Tejun Heobe96b312017-10-28 09:49:37 -0700965 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
966
Tejun Heobe96b312017-10-28 09:49:37 -0700967 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
968 cpuctx->cgrp = cgrp;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800969 }
leilei.lin33801b92018-03-06 17:36:37 +0800970
971 if (add && ctx->nr_cgroups++)
972 return;
973 else if (!add && --ctx->nr_cgroups)
974 return;
975
976 /* no cgroup running */
977 if (!add)
978 cpuctx->cgrp = NULL;
979
980 cpuctx_entry = &cpuctx->cgrp_cpuctx_entry;
981 if (add)
982 list_add(cpuctx_entry, this_cpu_ptr(&cgrp_cpuctx_list));
983 else
984 list_del(cpuctx_entry);
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700985}
986
Stephane Eraniane5d13672011-02-14 11:20:01 +0200987#else /* !CONFIG_CGROUP_PERF */
988
989static inline bool
990perf_cgroup_match(struct perf_event *event)
991{
992 return true;
993}
994
995static inline void perf_detach_cgroup(struct perf_event *event)
996{}
997
998static inline int is_cgroup_event(struct perf_event *event)
999{
1000 return 0;
1001}
1002
Stephane Eraniane5d13672011-02-14 11:20:01 +02001003static inline void update_cgrp_time_from_event(struct perf_event *event)
1004{
1005}
1006
1007static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
1008{
1009}
1010
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001011static inline void perf_cgroup_sched_out(struct task_struct *task,
1012 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001013{
1014}
1015
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001016static inline void perf_cgroup_sched_in(struct task_struct *prev,
1017 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001018{
1019}
1020
1021static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
1022 struct perf_event_attr *attr,
1023 struct perf_event *group_leader)
1024{
1025 return -EINVAL;
1026}
1027
1028static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001029perf_cgroup_set_timestamp(struct task_struct *task,
1030 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001031{
1032}
1033
Ben Dooks (Codethink)d00dbd292019-11-06 13:25:27 +00001034static inline void
Stephane Eraniane5d13672011-02-14 11:20:01 +02001035perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
1036{
1037}
1038
1039static inline void
1040perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
1041{
1042}
1043
1044static inline u64 perf_cgroup_event_time(struct perf_event *event)
1045{
1046 return 0;
1047}
1048
1049static inline void
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001050list_update_cgroup_event(struct perf_event *event,
1051 struct perf_event_context *ctx, bool add)
1052{
1053}
1054
Stephane Eraniane5d13672011-02-14 11:20:01 +02001055#endif
1056
Stephane Eranian9e630202013-04-03 14:21:33 +02001057/*
1058 * set default to be dependent on timer tick just
1059 * like original code
1060 */
1061#define PERF_CPU_HRTIMER (1000 / HZ)
1062/*
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08001063 * function must be called with interrupts disabled
Stephane Eranian9e630202013-04-03 14:21:33 +02001064 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001065static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001066{
1067 struct perf_cpu_context *cpuctx;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01001068 bool rotations;
Stephane Eranian9e630202013-04-03 14:21:33 +02001069
Frederic Weisbecker16444642017-11-06 16:01:24 +01001070 lockdep_assert_irqs_disabled();
Stephane Eranian9e630202013-04-03 14:21:33 +02001071
1072 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001073 rotations = perf_rotate_context(cpuctx);
1074
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001075 raw_spin_lock(&cpuctx->hrtimer_lock);
1076 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001077 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001078 else
1079 cpuctx->hrtimer_active = 0;
1080 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001081
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001082 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001083}
1084
Peter Zijlstra272325c2015-04-15 11:41:58 +02001085static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001086{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001087 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001088 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001089 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001090
1091 /* no multiplexing needed for SW PMU */
1092 if (pmu->task_ctx_nr == perf_sw_context)
1093 return;
1094
Stephane Eranian62b85632013-04-03 14:21:34 +02001095 /*
1096 * check default is sane, if not set then force to
1097 * default interval (1/tick)
1098 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001099 interval = pmu->hrtimer_interval_ms;
1100 if (interval < 1)
1101 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001102
Peter Zijlstra272325c2015-04-15 11:41:58 +02001103 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001104
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001105 raw_spin_lock_init(&cpuctx->hrtimer_lock);
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02001106 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001107 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001108}
1109
Peter Zijlstra272325c2015-04-15 11:41:58 +02001110static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001111{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001112 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001113 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001114 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001115
1116 /* not for SW PMU */
1117 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001118 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001119
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001120 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1121 if (!cpuctx->hrtimer_active) {
1122 cpuctx->hrtimer_active = 1;
1123 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02001124 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001125 }
1126 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001127
Peter Zijlstra272325c2015-04-15 11:41:58 +02001128 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001129}
1130
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001131void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001133 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1134 if (!(*count)++)
1135 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001136}
1137
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001138void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001140 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1141 if (!--(*count))
1142 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143}
1144
Mark Rutland2fde4f92015-01-07 15:01:54 +00001145static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001146
1147/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001148 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1149 * perf_event_task_tick() are fully serialized because they're strictly cpu
1150 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1151 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001152 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001153static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001154{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001155 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001156
Frederic Weisbecker16444642017-11-06 16:01:24 +01001157 lockdep_assert_irqs_disabled();
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001158
Mark Rutland2fde4f92015-01-07 15:01:54 +00001159 WARN_ON(!list_empty(&ctx->active_ctx_list));
1160
1161 list_add(&ctx->active_ctx_list, head);
1162}
1163
1164static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1165{
Frederic Weisbecker16444642017-11-06 16:01:24 +01001166 lockdep_assert_irqs_disabled();
Mark Rutland2fde4f92015-01-07 15:01:54 +00001167
1168 WARN_ON(list_empty(&ctx->active_ctx_list));
1169
1170 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171}
1172
1173static void get_ctx(struct perf_event_context *ctx)
1174{
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001175 refcount_inc(&ctx->refcount);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001176}
1177
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001178static void free_ctx(struct rcu_head *head)
1179{
1180 struct perf_event_context *ctx;
1181
1182 ctx = container_of(head, struct perf_event_context, rcu_head);
1183 kfree(ctx->task_ctx_data);
1184 kfree(ctx);
1185}
1186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187static void put_ctx(struct perf_event_context *ctx)
1188{
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001189 if (refcount_dec_and_test(&ctx->refcount)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001190 if (ctx->parent_ctx)
1191 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001192 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193 put_task_struct(ctx->task);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001194 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195 }
1196}
1197
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001198/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001199 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1200 * perf_pmu_migrate_context() we need some magic.
1201 *
1202 * Those places that change perf_event::ctx will hold both
1203 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1204 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001205 * Lock ordering is by mutex address. There are two other sites where
1206 * perf_event_context::mutex nests and those are:
1207 *
1208 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001209 * perf_event_exit_event()
1210 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001211 *
1212 * - perf_event_init_context() [ parent, 0 ]
1213 * inherit_task_group()
1214 * inherit_group()
1215 * inherit_event()
1216 * perf_event_alloc()
1217 * perf_init_event()
1218 * perf_try_init_event() [ child , 1 ]
1219 *
1220 * While it appears there is an obvious deadlock here -- the parent and child
1221 * nesting levels are inverted between the two. This is in fact safe because
1222 * life-time rules separate them. That is an exiting task cannot fork, and a
1223 * spawning task cannot (yet) exit.
1224 *
1225 * But remember that that these are parent<->child context relations, and
1226 * migration does not affect children, therefore these two orderings should not
1227 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001228 *
1229 * The change in perf_event::ctx does not affect children (as claimed above)
1230 * because the sys_perf_event_open() case will install a new event and break
1231 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1232 * concerned with cpuctx and that doesn't have children.
1233 *
1234 * The places that change perf_event::ctx will issue:
1235 *
1236 * perf_remove_from_context();
1237 * synchronize_rcu();
1238 * perf_install_in_context();
1239 *
1240 * to affect the change. The remove_from_context() + synchronize_rcu() should
1241 * quiesce the event, after which we can install it in the new location. This
1242 * means that only external vectors (perf_fops, prctl) can perturb the event
1243 * while in transit. Therefore all such accessors should also acquire
1244 * perf_event_context::mutex to serialize against this.
1245 *
1246 * However; because event->ctx can change while we're waiting to acquire
1247 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1248 * function.
1249 *
1250 * Lock order:
Peter Zijlstra79c9ce52016-04-26 11:36:53 +02001251 * cred_guard_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001252 * task_struct::perf_event_mutex
1253 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001254 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001255 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001256 * perf_event::mmap_mutex
1257 * mmap_sem
Alexander Shishkin18736ee2019-02-15 13:56:54 +02001258 * perf_addr_filters_head::lock
Peter Zijlstra82d94852018-01-09 13:10:30 +01001259 *
1260 * cpu_hotplug_lock
1261 * pmus_lock
1262 * cpuctx->mutex / perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001263 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001264static struct perf_event_context *
1265perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001266{
1267 struct perf_event_context *ctx;
1268
1269again:
1270 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -07001271 ctx = READ_ONCE(event->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001272 if (!refcount_inc_not_zero(&ctx->refcount)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001273 rcu_read_unlock();
1274 goto again;
1275 }
1276 rcu_read_unlock();
1277
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001278 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001279 if (event->ctx != ctx) {
1280 mutex_unlock(&ctx->mutex);
1281 put_ctx(ctx);
1282 goto again;
1283 }
1284
1285 return ctx;
1286}
1287
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001288static inline struct perf_event_context *
1289perf_event_ctx_lock(struct perf_event *event)
1290{
1291 return perf_event_ctx_lock_nested(event, 0);
1292}
1293
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001294static void perf_event_ctx_unlock(struct perf_event *event,
1295 struct perf_event_context *ctx)
1296{
1297 mutex_unlock(&ctx->mutex);
1298 put_ctx(ctx);
1299}
1300
1301/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001302 * This must be done under the ctx->lock, such as to serialize against
1303 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1304 * calling scheduler related locks and ctx->lock nests inside those.
1305 */
1306static __must_check struct perf_event_context *
1307unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001309 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1310
1311 lockdep_assert_held(&ctx->lock);
1312
1313 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001315 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001316
1317 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001318}
1319
Oleg Nesterov1d953112017-08-22 17:59:28 +02001320static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
1321 enum pid_type type)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001322{
Oleg Nesterov1d953112017-08-22 17:59:28 +02001323 u32 nr;
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001324 /*
1325 * only top level events have the pid namespace they were created in
1326 */
1327 if (event->parent)
1328 event = event->parent;
1329
Oleg Nesterov1d953112017-08-22 17:59:28 +02001330 nr = __task_pid_nr_ns(p, type, event->ns);
1331 /* avoid -1 if it is idle thread or runs in another ns */
1332 if (!nr && !pid_alive(p))
1333 nr = -1;
1334 return nr;
1335}
1336
1337static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1338{
Eric W. Biederman6883f812017-06-04 04:32:13 -05001339 return perf_event_pid_type(event, p, PIDTYPE_TGID);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001340}
1341
1342static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1343{
Oleg Nesterov1d953112017-08-22 17:59:28 +02001344 return perf_event_pid_type(event, p, PIDTYPE_PID);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001345}
1346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347/*
1348 * If we inherit events we want to return the parent event id
1349 * to userspace.
1350 */
1351static u64 primary_event_id(struct perf_event *event)
1352{
1353 u64 id = event->id;
1354
1355 if (event->parent)
1356 id = event->parent->id;
1357
1358 return id;
1359}
1360
1361/*
1362 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001363 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001364 * This has to cope with with the fact that until it is locked,
1365 * the context could get moved to another task.
1366 */
1367static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001368perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001369{
1370 struct perf_event_context *ctx;
1371
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001372retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001373 /*
1374 * One of the few rules of preemptible RCU is that one cannot do
1375 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001376 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001377 * rcu_read_unlock_special().
1378 *
1379 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001380 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001381 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001382 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001383 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001384 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385 if (ctx) {
1386 /*
1387 * If this context is a clone of another, it might
1388 * get swapped for another underneath us by
1389 * perf_event_task_sched_out, though the
1390 * rcu_read_lock() protects us from any context
1391 * getting freed. Lock the context and check if it
1392 * got swapped before we could get the lock, and retry
1393 * if so. If we locked the right context, then it
1394 * can't get swapped on us any more.
1395 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001396 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001397 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001398 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001399 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001400 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401 goto retry;
1402 }
1403
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001404 if (ctx->task == TASK_TOMBSTONE ||
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001405 !refcount_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001406 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001408 } else {
1409 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 }
1411 }
1412 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001413 if (!ctx)
1414 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415 return ctx;
1416}
1417
1418/*
1419 * Get the context for a task and increment its pin_count so it
1420 * can't get swapped to another task. This also increments its
1421 * reference count so that the context can't get freed.
1422 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001423static struct perf_event_context *
1424perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425{
1426 struct perf_event_context *ctx;
1427 unsigned long flags;
1428
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001429 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430 if (ctx) {
1431 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001432 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001433 }
1434 return ctx;
1435}
1436
1437static void perf_unpin_context(struct perf_event_context *ctx)
1438{
1439 unsigned long flags;
1440
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001441 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001443 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444}
1445
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001446/*
1447 * Update the record of the current time in a context.
1448 */
1449static void update_context_time(struct perf_event_context *ctx)
1450{
1451 u64 now = perf_clock();
1452
1453 ctx->time += now - ctx->timestamp;
1454 ctx->timestamp = now;
1455}
1456
Stephane Eranian41587552011-01-03 18:20:01 +02001457static u64 perf_event_time(struct perf_event *event)
1458{
1459 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001460
1461 if (is_cgroup_event(event))
1462 return perf_cgroup_event_time(event);
1463
Stephane Eranian41587552011-01-03 18:20:01 +02001464 return ctx ? ctx->time : 0;
1465}
1466
Alexander Shishkin487f05e2017-01-19 18:43:30 +02001467static enum event_type_t get_event_type(struct perf_event *event)
1468{
1469 struct perf_event_context *ctx = event->ctx;
1470 enum event_type_t event_type;
1471
1472 lockdep_assert_held(&ctx->lock);
1473
Alexander Shishkin3bda69c2017-07-18 14:08:34 +03001474 /*
1475 * It's 'group type', really, because if our group leader is
1476 * pinned, so are we.
1477 */
1478 if (event->group_leader != event)
1479 event = event->group_leader;
1480
Alexander Shishkin487f05e2017-01-19 18:43:30 +02001481 event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE;
1482 if (!ctx->task)
1483 event_type |= EVENT_CPU;
1484
1485 return event_type;
1486}
1487
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001488/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001489 * Helper function to initialize event group nodes.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001490 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001491static void init_event_group(struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001492{
1493 RB_CLEAR_NODE(&event->group_node);
1494 event->group_index = 0;
1495}
1496
1497/*
1498 * Extract pinned or flexible groups from the context
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001499 * based on event attrs bits.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001500 */
1501static struct perf_event_groups *
1502get_event_groups(struct perf_event *event, struct perf_event_context *ctx)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001503{
1504 if (event->attr.pinned)
1505 return &ctx->pinned_groups;
1506 else
1507 return &ctx->flexible_groups;
1508}
1509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001510/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001511 * Helper function to initializes perf_event_group trees.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001512 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001513static void perf_event_groups_init(struct perf_event_groups *groups)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001514{
1515 groups->tree = RB_ROOT;
1516 groups->index = 0;
1517}
1518
1519/*
1520 * Compare function for event groups;
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001521 *
1522 * Implements complex key that first sorts by CPU and then by virtual index
1523 * which provides ordering when rotating groups for the same CPU.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001524 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001525static bool
1526perf_event_groups_less(struct perf_event *left, struct perf_event *right)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001527{
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001528 if (left->cpu < right->cpu)
1529 return true;
1530 if (left->cpu > right->cpu)
1531 return false;
1532
1533 if (left->group_index < right->group_index)
1534 return true;
1535 if (left->group_index > right->group_index)
1536 return false;
1537
1538 return false;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001539}
1540
1541/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001542 * Insert @event into @groups' tree; using {@event->cpu, ++@groups->index} for
1543 * key (see perf_event_groups_less). This places it last inside the CPU
1544 * subtree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001545 */
1546static void
1547perf_event_groups_insert(struct perf_event_groups *groups,
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001548 struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001549{
1550 struct perf_event *node_event;
1551 struct rb_node *parent;
1552 struct rb_node **node;
1553
1554 event->group_index = ++groups->index;
1555
1556 node = &groups->tree.rb_node;
1557 parent = *node;
1558
1559 while (*node) {
1560 parent = *node;
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001561 node_event = container_of(*node, struct perf_event, group_node);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001562
1563 if (perf_event_groups_less(event, node_event))
1564 node = &parent->rb_left;
1565 else
1566 node = &parent->rb_right;
1567 }
1568
1569 rb_link_node(&event->group_node, parent, node);
1570 rb_insert_color(&event->group_node, &groups->tree);
1571}
1572
1573/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001574 * Helper function to insert event into the pinned or flexible groups.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001575 */
1576static void
1577add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx)
1578{
1579 struct perf_event_groups *groups;
1580
1581 groups = get_event_groups(event, ctx);
1582 perf_event_groups_insert(groups, event);
1583}
1584
1585/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001586 * Delete a group from a tree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001587 */
1588static void
1589perf_event_groups_delete(struct perf_event_groups *groups,
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001590 struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001591{
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001592 WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) ||
1593 RB_EMPTY_ROOT(&groups->tree));
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001594
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001595 rb_erase(&event->group_node, &groups->tree);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001596 init_event_group(event);
1597}
1598
1599/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001600 * Helper function to delete event from its groups.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001601 */
1602static void
1603del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx)
1604{
1605 struct perf_event_groups *groups;
1606
1607 groups = get_event_groups(event, ctx);
1608 perf_event_groups_delete(groups, event);
1609}
1610
1611/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001612 * Get the leftmost event in the @cpu subtree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001613 */
1614static struct perf_event *
1615perf_event_groups_first(struct perf_event_groups *groups, int cpu)
1616{
1617 struct perf_event *node_event = NULL, *match = NULL;
1618 struct rb_node *node = groups->tree.rb_node;
1619
1620 while (node) {
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001621 node_event = container_of(node, struct perf_event, group_node);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001622
1623 if (cpu < node_event->cpu) {
1624 node = node->rb_left;
1625 } else if (cpu > node_event->cpu) {
1626 node = node->rb_right;
1627 } else {
1628 match = node_event;
1629 node = node->rb_left;
1630 }
1631 }
1632
1633 return match;
1634}
1635
1636/*
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001637 * Like rb_entry_next_safe() for the @cpu subtree.
1638 */
1639static struct perf_event *
1640perf_event_groups_next(struct perf_event *event)
1641{
1642 struct perf_event *next;
1643
1644 next = rb_entry_safe(rb_next(&event->group_node), typeof(*event), group_node);
1645 if (next && next->cpu == event->cpu)
1646 return next;
1647
1648 return NULL;
1649}
1650
1651/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001652 * Iterate through the whole groups tree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001653 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +01001654#define perf_event_groups_for_each(event, groups) \
1655 for (event = rb_entry_safe(rb_first(&((groups)->tree)), \
1656 typeof(*event), group_node); event; \
1657 event = rb_entry_safe(rb_next(&event->group_node), \
1658 typeof(*event), group_node))
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001659
1660/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001661 * Add an event from the lists for its context.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662 * Must be called with ctx->mutex and ctx->lock held.
1663 */
1664static void
1665list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1666{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001667 lockdep_assert_held(&ctx->lock);
1668
Peter Zijlstra8a495422010-05-27 15:47:49 +02001669 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1670 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02001672 event->tstamp = perf_event_time(event);
1673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001674 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001675 * If we're a stand alone event or group leader, we go to the context
1676 * list, group events are kept attached to the group so that
1677 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001679 if (event->group_leader == event) {
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001680 event->group_caps = event->event_caps;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001681 add_event_to_groups(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682 }
1683
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001684 list_update_cgroup_event(event, ctx, true);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001685
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001686 list_add_rcu(&event->event_entry, &ctx->event_list);
1687 ctx->nr_events++;
1688 if (event->attr.inherit_stat)
1689 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001690
1691 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692}
1693
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001694/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001695 * Initialize event state based on the perf_event_attr::disabled.
1696 */
1697static inline void perf_event__state_init(struct perf_event *event)
1698{
1699 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1700 PERF_EVENT_STATE_INACTIVE;
1701}
1702
Peter Zijlstraa7239682015-09-09 19:06:33 +02001703static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001704{
1705 int entry = sizeof(u64); /* value */
1706 int size = 0;
1707 int nr = 1;
1708
1709 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1710 size += sizeof(u64);
1711
1712 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1713 size += sizeof(u64);
1714
1715 if (event->attr.read_format & PERF_FORMAT_ID)
1716 entry += sizeof(u64);
1717
1718 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001719 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001720 size += sizeof(u64);
1721 }
1722
1723 size += entry * nr;
1724 event->read_size = size;
1725}
1726
Peter Zijlstraa7239682015-09-09 19:06:33 +02001727static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001728{
1729 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001730 u16 size = 0;
1731
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001732 if (sample_type & PERF_SAMPLE_IP)
1733 size += sizeof(data->ip);
1734
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001735 if (sample_type & PERF_SAMPLE_ADDR)
1736 size += sizeof(data->addr);
1737
1738 if (sample_type & PERF_SAMPLE_PERIOD)
1739 size += sizeof(data->period);
1740
Andi Kleenc3feedf2013-01-24 16:10:28 +01001741 if (sample_type & PERF_SAMPLE_WEIGHT)
1742 size += sizeof(data->weight);
1743
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001744 if (sample_type & PERF_SAMPLE_READ)
1745 size += event->read_size;
1746
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001747 if (sample_type & PERF_SAMPLE_DATA_SRC)
1748 size += sizeof(data->data_src.val);
1749
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001750 if (sample_type & PERF_SAMPLE_TRANSACTION)
1751 size += sizeof(data->txn);
1752
Kan Liangfc7ce9c2017-08-28 20:52:49 -04001753 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1754 size += sizeof(data->phys_addr);
1755
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001756 event->header_size = size;
1757}
1758
Peter Zijlstraa7239682015-09-09 19:06:33 +02001759/*
1760 * Called at perf_event creation and when events are attached/detached from a
1761 * group.
1762 */
1763static void perf_event__header_size(struct perf_event *event)
1764{
1765 __perf_event_read_size(event,
1766 event->group_leader->nr_siblings);
1767 __perf_event_header_size(event, event->attr.sample_type);
1768}
1769
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001770static void perf_event__id_header_size(struct perf_event *event)
1771{
1772 struct perf_sample_data *data;
1773 u64 sample_type = event->attr.sample_type;
1774 u16 size = 0;
1775
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001776 if (sample_type & PERF_SAMPLE_TID)
1777 size += sizeof(data->tid_entry);
1778
1779 if (sample_type & PERF_SAMPLE_TIME)
1780 size += sizeof(data->time);
1781
Adrian Hunterff3d5272013-08-27 11:23:07 +03001782 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1783 size += sizeof(data->id);
1784
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001785 if (sample_type & PERF_SAMPLE_ID)
1786 size += sizeof(data->id);
1787
1788 if (sample_type & PERF_SAMPLE_STREAM_ID)
1789 size += sizeof(data->stream_id);
1790
1791 if (sample_type & PERF_SAMPLE_CPU)
1792 size += sizeof(data->cpu_entry);
1793
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001794 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001795}
1796
Peter Zijlstraa7239682015-09-09 19:06:33 +02001797static bool perf_event_validate_size(struct perf_event *event)
1798{
1799 /*
1800 * The values computed here will be over-written when we actually
1801 * attach the event.
1802 */
1803 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1804 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1805 perf_event__id_header_size(event);
1806
1807 /*
1808 * Sum the lot; should not exceed the 64k limit we have on records.
1809 * Conservative limit to allow for callchains and other variable fields.
1810 */
1811 if (event->read_size + event->header_size +
1812 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1813 return false;
1814
1815 return true;
1816}
1817
Peter Zijlstra8a495422010-05-27 15:47:49 +02001818static void perf_group_attach(struct perf_event *event)
1819{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001820 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001821
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01001822 lockdep_assert_held(&event->ctx->lock);
1823
Peter Zijlstra74c33372010-10-15 11:40:29 +02001824 /*
1825 * We can have double attach due to group movement in perf_event_open.
1826 */
1827 if (event->attach_state & PERF_ATTACH_GROUP)
1828 return;
1829
Peter Zijlstra8a495422010-05-27 15:47:49 +02001830 event->attach_state |= PERF_ATTACH_GROUP;
1831
1832 if (group_leader == event)
1833 return;
1834
Peter Zijlstra652884f2015-01-23 11:20:10 +01001835 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1836
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001837 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001838
Peter Zijlstra8343aae2017-11-13 14:28:33 +01001839 list_add_tail(&event->sibling_list, &group_leader->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001840 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001841
1842 perf_event__header_size(group_leader);
1843
Peter Zijlstraedb39592018-03-15 17:36:56 +01001844 for_each_sibling_event(pos, group_leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001845 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001846}
1847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001849 * Remove an event from the lists for its context.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850 * Must be called with ctx->mutex and ctx->lock held.
1851 */
1852static void
1853list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1854{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001855 WARN_ON_ONCE(event->ctx != ctx);
1856 lockdep_assert_held(&ctx->lock);
1857
Peter Zijlstra8a495422010-05-27 15:47:49 +02001858 /*
1859 * We can have double detach due to exit/hot-unplug + close.
1860 */
1861 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001862 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001863
1864 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1865
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001866 list_update_cgroup_event(event, ctx, false);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001868 ctx->nr_events--;
1869 if (event->attr.inherit_stat)
1870 ctx->nr_stat--;
1871
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872 list_del_rcu(&event->event_entry);
1873
Peter Zijlstra8a495422010-05-27 15:47:49 +02001874 if (event->group_leader == event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001875 del_event_from_groups(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001877 /*
1878 * If event was in error state, then keep it
1879 * that way, otherwise bogus counts will be
1880 * returned on read(). The only way to get out
1881 * of error state is by explicit re-enabling
1882 * of the event
1883 */
1884 if (event->state > PERF_EVENT_STATE_OFF)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02001885 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001886
1887 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001888}
1889
Alexander Shishkinab437622019-08-06 11:46:00 +03001890static int
1891perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event)
1892{
1893 if (!has_aux(aux_event))
1894 return 0;
1895
1896 if (!event->pmu->aux_output_match)
1897 return 0;
1898
1899 return event->pmu->aux_output_match(aux_event);
1900}
1901
1902static void put_event(struct perf_event *event);
1903static void event_sched_out(struct perf_event *event,
1904 struct perf_cpu_context *cpuctx,
1905 struct perf_event_context *ctx);
1906
1907static void perf_put_aux_event(struct perf_event *event)
1908{
1909 struct perf_event_context *ctx = event->ctx;
1910 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
1911 struct perf_event *iter;
1912
1913 /*
1914 * If event uses aux_event tear down the link
1915 */
1916 if (event->aux_event) {
1917 iter = event->aux_event;
1918 event->aux_event = NULL;
1919 put_event(iter);
1920 return;
1921 }
1922
1923 /*
1924 * If the event is an aux_event, tear down all links to
1925 * it from other events.
1926 */
1927 for_each_sibling_event(iter, event->group_leader) {
1928 if (iter->aux_event != event)
1929 continue;
1930
1931 iter->aux_event = NULL;
1932 put_event(event);
1933
1934 /*
1935 * If it's ACTIVE, schedule it out and put it into ERROR
1936 * state so that we don't try to schedule it again. Note
1937 * that perf_event_enable() will clear the ERROR status.
1938 */
1939 event_sched_out(iter, cpuctx, ctx);
1940 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
1941 }
1942}
1943
Alexander Shishkina4faf002019-10-25 17:08:33 +03001944static bool perf_need_aux_event(struct perf_event *event)
1945{
1946 return !!event->attr.aux_output || !!event->attr.aux_sample_size;
1947}
1948
Alexander Shishkinab437622019-08-06 11:46:00 +03001949static int perf_get_aux_event(struct perf_event *event,
1950 struct perf_event *group_leader)
1951{
1952 /*
1953 * Our group leader must be an aux event if we want to be
1954 * an aux_output. This way, the aux event will precede its
1955 * aux_output events in the group, and therefore will always
1956 * schedule first.
1957 */
1958 if (!group_leader)
1959 return 0;
1960
Alexander Shishkina4faf002019-10-25 17:08:33 +03001961 /*
1962 * aux_output and aux_sample_size are mutually exclusive.
1963 */
1964 if (event->attr.aux_output && event->attr.aux_sample_size)
1965 return 0;
1966
1967 if (event->attr.aux_output &&
1968 !perf_aux_output_match(event, group_leader))
1969 return 0;
1970
1971 if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux)
Alexander Shishkinab437622019-08-06 11:46:00 +03001972 return 0;
1973
1974 if (!atomic_long_inc_not_zero(&group_leader->refcount))
1975 return 0;
1976
1977 /*
1978 * Link aux_outputs to their aux event; this is undone in
1979 * perf_group_detach() by perf_put_aux_event(). When the
1980 * group in torn down, the aux_output events loose their
1981 * link to the aux_event and can't schedule any more.
1982 */
1983 event->aux_event = group_leader;
1984
1985 return 1;
1986}
1987
Peter Zijlstra8a495422010-05-27 15:47:49 +02001988static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001989{
1990 struct perf_event *sibling, *tmp;
Peter Zijlstra66681282017-11-13 14:28:38 +01001991 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001992
Peter Zijlstra66681282017-11-13 14:28:38 +01001993 lockdep_assert_held(&ctx->lock);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01001994
Peter Zijlstra8a495422010-05-27 15:47:49 +02001995 /*
1996 * We can have double detach due to exit/hot-unplug + close.
1997 */
1998 if (!(event->attach_state & PERF_ATTACH_GROUP))
1999 return;
2000
2001 event->attach_state &= ~PERF_ATTACH_GROUP;
2002
Alexander Shishkinab437622019-08-06 11:46:00 +03002003 perf_put_aux_event(event);
2004
Peter Zijlstra8a495422010-05-27 15:47:49 +02002005 /*
2006 * If this is a sibling, remove it from its group.
2007 */
2008 if (event->group_leader != event) {
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002009 list_del_init(&event->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002010 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002011 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002012 }
2013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014 /*
2015 * If this was a group event with sibling events then
2016 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02002017 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 */
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002019 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 sibling->group_leader = sibling;
Mark Rutland24868362018-03-16 12:51:40 +00002022 list_del_init(&sibling->sibling_list);
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002023
2024 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002025 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002026
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002027 if (!RB_EMPTY_NODE(&event->group_node)) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002028 add_event_to_groups(sibling, event->ctx);
Peter Zijlstra66681282017-11-13 14:28:38 +01002029
2030 if (sibling->state == PERF_EVENT_STATE_ACTIVE) {
2031 struct list_head *list = sibling->attr.pinned ?
2032 &ctx->pinned_active : &ctx->flexible_active;
2033
2034 list_add_tail(&sibling->active_list, list);
2035 }
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002036 }
2037
Peter Zijlstra652884f2015-01-23 11:20:10 +01002038 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002039 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002040
2041out:
2042 perf_event__header_size(event->group_leader);
2043
Peter Zijlstraedb39592018-03-15 17:36:56 +01002044 for_each_sibling_event(tmp, event->group_leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002045 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002046}
2047
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002048static bool is_orphaned_event(struct perf_event *event)
2049{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01002050 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002051}
2052
Mark Rutland2c81a642016-06-14 16:10:41 +01002053static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01002054{
2055 struct pmu *pmu = event->pmu;
2056 return pmu->filter_match ? pmu->filter_match(event) : 1;
2057}
2058
Mark Rutland2c81a642016-06-14 16:10:41 +01002059/*
2060 * Check whether we should attempt to schedule an event group based on
2061 * PMU-specific filtering. An event group can consist of HW and SW events,
2062 * potentially with a SW leader, so we must check all the filters, to
2063 * determine whether a group is schedulable:
2064 */
2065static inline int pmu_filter_match(struct perf_event *event)
2066{
Peter Zijlstraedb39592018-03-15 17:36:56 +01002067 struct perf_event *sibling;
Mark Rutland2c81a642016-06-14 16:10:41 +01002068
2069 if (!__pmu_filter_match(event))
2070 return 0;
2071
Peter Zijlstraedb39592018-03-15 17:36:56 +01002072 for_each_sibling_event(sibling, event) {
2073 if (!__pmu_filter_match(sibling))
Mark Rutland2c81a642016-06-14 16:10:41 +01002074 return 0;
2075 }
2076
2077 return 1;
2078}
2079
Stephane Eranianfa66f072010-08-26 16:40:01 +02002080static inline int
2081event_filter_match(struct perf_event *event)
2082{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002083 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
2084 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02002085}
2086
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002087static void
2088event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002089 struct perf_cpu_context *cpuctx,
2090 struct perf_event_context *ctx)
2091{
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002092 enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002093
2094 WARN_ON_ONCE(event->ctx != ctx);
2095 lockdep_assert_held(&ctx->lock);
2096
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002098 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099
Peter Zijlstra66681282017-11-13 14:28:38 +01002100 /*
2101 * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but
2102 * we can schedule events _OUT_ individually through things like
2103 * __perf_remove_from_context().
2104 */
2105 list_del_init(&event->active_list);
2106
Alexander Shishkin44377272013-12-16 14:17:36 +02002107 perf_pmu_disable(event->pmu);
2108
Peter Zijlstra28a967c2016-02-24 18:45:46 +01002109 event->pmu->del(event, 0);
2110 event->oncpu = -1;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002111
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002112 if (READ_ONCE(event->pending_disable) >= 0) {
2113 WRITE_ONCE(event->pending_disable, -1);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002114 state = PERF_EVENT_STATE_OFF;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002115 }
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002116 perf_event_set_state(event, state);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117
2118 if (!is_software_event(event))
2119 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002120 if (!--ctx->nr_active)
2121 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002122 if (event->attr.freq && event->attr.sample_freq)
2123 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002124 if (event->attr.exclusive || !cpuctx->active_oncpu)
2125 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02002126
2127 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002128}
2129
2130static void
2131group_sched_out(struct perf_event *group_event,
2132 struct perf_cpu_context *cpuctx,
2133 struct perf_event_context *ctx)
2134{
2135 struct perf_event *event;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002136
2137 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
2138 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002139
Mark Rutland3f005e72016-07-26 18:12:21 +01002140 perf_pmu_disable(ctx->pmu);
2141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142 event_sched_out(group_event, cpuctx, ctx);
2143
2144 /*
2145 * Schedule out siblings (if any):
2146 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002147 for_each_sibling_event(event, group_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148 event_sched_out(event, cpuctx, ctx);
2149
Mark Rutland3f005e72016-07-26 18:12:21 +01002150 perf_pmu_enable(ctx->pmu);
2151
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002152 if (group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002153 cpuctx->exclusive = 0;
2154}
2155
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002156#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01002157
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002158/*
2159 * Cross CPU call to remove a performance event
2160 *
2161 * We disable the event on the hardware level first. After that we
2162 * remove it from the context list.
2163 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002164static void
2165__perf_remove_from_context(struct perf_event *event,
2166 struct perf_cpu_context *cpuctx,
2167 struct perf_event_context *ctx,
2168 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002170 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002172 if (ctx->is_active & EVENT_TIME) {
2173 update_context_time(ctx);
2174 update_cgrp_time_from_cpuctx(cpuctx);
2175 }
2176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002178 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02002179 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002181
Peter Zijlstra39a43642016-01-11 12:46:35 +01002182 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002183 ctx->is_active = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01002184 if (ctx->task) {
2185 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2186 cpuctx->task_ctx = NULL;
2187 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189}
2190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191/*
2192 * Remove the event from a task's (or a CPU's) list of events.
2193 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002194 * If event->ctx is a cloned context, callers must make sure that
2195 * every task struct that event->ctx->task could possibly point to
2196 * remains valid. This is OK when called from perf_release since
2197 * that only calls us on the top-level context, which can't be a clone.
2198 * When called from perf_event_exit_task, it's OK because the
2199 * context has been detached from its task.
2200 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002201static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002202{
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002203 struct perf_event_context *ctx = event->ctx;
2204
2205 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002206
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002207 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002208
2209 /*
2210 * The above event_function_call() can NO-OP when it hits
2211 * TASK_TOMBSTONE. In that case we must already have been detached
2212 * from the context (by perf_event_exit_event()) but the grouping
2213 * might still be in-tact.
2214 */
2215 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
2216 if ((flags & DETACH_GROUP) &&
2217 (event->attach_state & PERF_ATTACH_GROUP)) {
2218 /*
2219 * Since in that case we cannot possibly be scheduled, simply
2220 * detach now.
2221 */
2222 raw_spin_lock_irq(&ctx->lock);
2223 perf_group_detach(event);
2224 raw_spin_unlock_irq(&ctx->lock);
2225 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002226}
2227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229 * Cross CPU call to disable a performance event
2230 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002231static void __perf_event_disable(struct perf_event *event,
2232 struct perf_cpu_context *cpuctx,
2233 struct perf_event_context *ctx,
2234 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002235{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002236 if (event->state < PERF_EVENT_STATE_INACTIVE)
2237 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002239 if (ctx->is_active & EVENT_TIME) {
2240 update_context_time(ctx);
2241 update_cgrp_time_from_event(event);
2242 }
2243
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002244 if (event == event->group_leader)
2245 group_sched_out(event, cpuctx, ctx);
2246 else
2247 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002248
2249 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002250}
2251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002252/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002253 * Disable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254 *
2255 * If event->ctx is a cloned context, callers must make sure that
2256 * every task struct that event->ctx->task could possibly point to
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03002257 * remains valid. This condition is satisfied when called through
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002258 * perf_event_for_each_child or perf_event_for_each because they
2259 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01002260 * goes to exit will block in perf_event_exit_event().
2261 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262 * When called from perf_pending_event it's OK because event->ctx
2263 * is the current context on this CPU and preemption is disabled,
2264 * hence we can't get into perf_event_task_sched_out for this context.
2265 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002266static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267{
2268 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002270 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002271 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002272 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002273 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002274 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002275 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002276
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002277 event_function_call(event, __perf_event_disable, NULL);
2278}
2279
2280void perf_event_disable_local(struct perf_event *event)
2281{
2282 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002284
2285/*
2286 * Strictly speaking kernel users cannot create groups and therefore this
2287 * interface does not need the perf_event_ctx_lock() magic.
2288 */
2289void perf_event_disable(struct perf_event *event)
2290{
2291 struct perf_event_context *ctx;
2292
2293 ctx = perf_event_ctx_lock(event);
2294 _perf_event_disable(event);
2295 perf_event_ctx_unlock(event, ctx);
2296}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002297EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002298
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002299void perf_event_disable_inatomic(struct perf_event *event)
2300{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002301 WRITE_ONCE(event->pending_disable, smp_processor_id());
2302 /* can fail, see perf_pending_event_disable() */
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002303 irq_work_queue(&event->pending);
2304}
2305
Stephane Eraniane5d13672011-02-14 11:20:01 +02002306static void perf_set_shadow_time(struct perf_event *event,
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002307 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002308{
2309 /*
2310 * use the correct time source for the time snapshot
2311 *
2312 * We could get by without this by leveraging the
2313 * fact that to get to this function, the caller
2314 * has most likely already called update_context_time()
2315 * and update_cgrp_time_xx() and thus both timestamp
2316 * are identical (or very close). Given that tstamp is,
2317 * already adjusted for cgroup, we could say that:
2318 * tstamp - ctx->timestamp
2319 * is equivalent to
2320 * tstamp - cgrp->timestamp.
2321 *
2322 * Then, in perf_output_read(), the calculation would
2323 * work with no changes because:
2324 * - event is guaranteed scheduled in
2325 * - no scheduled out in between
2326 * - thus the timestamp would be the same
2327 *
2328 * But this is a bit hairy.
2329 *
2330 * So instead, we have an explicit cgroup call to remain
2331 * within the time time source all along. We believe it
2332 * is cleaner and simpler to understand.
2333 */
2334 if (is_cgroup_event(event))
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002335 perf_cgroup_set_shadow_time(event, event->tstamp);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002336 else
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002337 event->shadow_ctx_time = event->tstamp - ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002338}
2339
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002340#define MAX_INTERRUPTS (~0ULL)
2341
2342static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002343static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002346event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002348 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002349{
Alexander Shishkin44377272013-12-16 14:17:36 +02002350 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002351
Peter Zijlstra63342412014-05-05 11:49:16 +02002352 lockdep_assert_held(&ctx->lock);
2353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354 if (event->state <= PERF_EVENT_STATE_OFF)
2355 return 0;
2356
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002357 WRITE_ONCE(event->oncpu, smp_processor_id());
2358 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02002359 * Order event::oncpu write to happen before the ACTIVE state is
2360 * visible. This allows perf_event_{stop,read}() to observe the correct
2361 * ->oncpu if it sees ACTIVE.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002362 */
2363 smp_wmb();
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002364 perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002365
2366 /*
2367 * Unthrottle events, since we scheduled we might have missed several
2368 * ticks already, also for a heavily scheduling task there is little
2369 * guarantee it'll get a tick in a timely manner.
2370 */
2371 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2372 perf_log_throttle(event, 1);
2373 event->hw.interrupts = 0;
2374 }
2375
Alexander Shishkin44377272013-12-16 14:17:36 +02002376 perf_pmu_disable(event->pmu);
2377
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002378 perf_set_shadow_time(event, ctx);
Shaohua Li72f669c2015-02-05 15:55:31 -08002379
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002380 perf_log_itrace_start(event);
2381
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002382 if (event->pmu->add(event, PERF_EF_START)) {
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002383 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002385 ret = -EAGAIN;
2386 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002387 }
2388
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002389 if (!is_software_event(event))
2390 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002391 if (!ctx->nr_active++)
2392 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002393 if (event->attr.freq && event->attr.sample_freq)
2394 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395
2396 if (event->attr.exclusive)
2397 cpuctx->exclusive = 1;
2398
Alexander Shishkin44377272013-12-16 14:17:36 +02002399out:
2400 perf_pmu_enable(event->pmu);
2401
2402 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002403}
2404
2405static int
2406group_sched_in(struct perf_event *group_event,
2407 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002408 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002409{
Lin Ming6bde9b62010-04-23 13:56:00 +08002410 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002411 struct pmu *pmu = ctx->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002412
2413 if (group_event->state == PERF_EVENT_STATE_OFF)
2414 return 0;
2415
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002416 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002417
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002418 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002419 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002420 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002422 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002423
2424 /*
2425 * Schedule in siblings as one group (if any):
2426 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002427 for_each_sibling_event(event, group_event) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002428 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002429 partial_group = event;
2430 goto group_error;
2431 }
2432 }
2433
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002434 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002435 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002436
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002437group_error:
2438 /*
2439 * Groups can be scheduled in as one unit only, so undo any
2440 * partial group before returning:
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002441 * The events up to the failed event are scheduled out normally.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002442 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002443 for_each_sibling_event(event, group_event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002444 if (event == partial_group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002445 break;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002446
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002447 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002449 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002451 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002452
Peter Zijlstra272325c2015-04-15 11:41:58 +02002453 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002454
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002455 return -EAGAIN;
2456}
2457
2458/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459 * Work out whether we can put this event group on the CPU now.
2460 */
2461static int group_can_go_on(struct perf_event *event,
2462 struct perf_cpu_context *cpuctx,
2463 int can_add_hw)
2464{
2465 /*
2466 * Groups consisting entirely of software events can always go on.
2467 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002468 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002469 return 1;
2470 /*
2471 * If an exclusive group is already on, no other hardware
2472 * events can go on.
2473 */
2474 if (cpuctx->exclusive)
2475 return 0;
2476 /*
2477 * If this group is exclusive and there are already
2478 * events on the CPU, it can't go on.
2479 */
2480 if (event->attr.exclusive && cpuctx->active_oncpu)
2481 return 0;
2482 /*
2483 * Otherwise, try to add it if all previous groups were able
2484 * to go on.
2485 */
2486 return can_add_hw;
2487}
2488
2489static void add_event_to_ctx(struct perf_event *event,
2490 struct perf_event_context *ctx)
2491{
2492 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002493 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002494}
2495
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002496static void ctx_sched_out(struct perf_event_context *ctx,
2497 struct perf_cpu_context *cpuctx,
2498 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002499static void
2500ctx_sched_in(struct perf_event_context *ctx,
2501 struct perf_cpu_context *cpuctx,
2502 enum event_type_t event_type,
2503 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002504
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002505static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002506 struct perf_event_context *ctx,
2507 enum event_type_t event_type)
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002508{
2509 if (!cpuctx->task_ctx)
2510 return;
2511
2512 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2513 return;
2514
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002515 ctx_sched_out(ctx, cpuctx, event_type);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002516}
2517
Peter Zijlstradce58552011-04-09 21:17:46 +02002518static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2519 struct perf_event_context *ctx,
2520 struct task_struct *task)
2521{
2522 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2523 if (ctx)
2524 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2525 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2526 if (ctx)
2527 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2528}
2529
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002530/*
2531 * We want to maintain the following priority of scheduling:
2532 * - CPU pinned (EVENT_CPU | EVENT_PINNED)
2533 * - task pinned (EVENT_PINNED)
2534 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
2535 * - task flexible (EVENT_FLEXIBLE).
2536 *
2537 * In order to avoid unscheduling and scheduling back in everything every
2538 * time an event is added, only do it for the groups of equal priority and
2539 * below.
2540 *
2541 * This can be called after a batch operation on task events, in which case
2542 * event_type is a bit mask of the types of events involved. For CPU events,
2543 * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
2544 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01002545static void ctx_resched(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002546 struct perf_event_context *task_ctx,
2547 enum event_type_t event_type)
Peter Zijlstra00179602015-11-30 16:26:35 +01002548{
Song Liubd903af2018-03-05 21:55:04 -08002549 enum event_type_t ctx_event_type;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002550 bool cpu_event = !!(event_type & EVENT_CPU);
2551
2552 /*
2553 * If pinned groups are involved, flexible groups also need to be
2554 * scheduled out.
2555 */
2556 if (event_type & EVENT_PINNED)
2557 event_type |= EVENT_FLEXIBLE;
2558
Song Liubd903af2018-03-05 21:55:04 -08002559 ctx_event_type = event_type & EVENT_ALL;
2560
Peter Zijlstra3e349502016-01-08 10:01:18 +01002561 perf_pmu_disable(cpuctx->ctx.pmu);
2562 if (task_ctx)
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002563 task_ctx_sched_out(cpuctx, task_ctx, event_type);
2564
2565 /*
2566 * Decide which cpu ctx groups to schedule out based on the types
2567 * of events that caused rescheduling:
2568 * - EVENT_CPU: schedule out corresponding groups;
2569 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups;
2570 * - otherwise, do nothing more.
2571 */
2572 if (cpu_event)
2573 cpu_ctx_sched_out(cpuctx, ctx_event_type);
2574 else if (ctx_event_type & EVENT_PINNED)
2575 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2576
Peter Zijlstra3e349502016-01-08 10:01:18 +01002577 perf_event_sched_in(cpuctx, task_ctx, current);
2578 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002579}
2580
Stephane Eranianc68d2242019-04-08 10:32:51 -07002581void perf_pmu_resched(struct pmu *pmu)
2582{
2583 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2584 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2585
2586 perf_ctx_lock(cpuctx, task_ctx);
2587 ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU);
2588 perf_ctx_unlock(cpuctx, task_ctx);
2589}
2590
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591/*
2592 * Cross CPU call to install and enable a performance event
2593 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002594 * Very similar to remote_function() + event_function() but cannot assume that
2595 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002596 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002597static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002598{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002599 struct perf_event *event = info;
2600 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002601 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002602 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63cae122016-12-09 14:59:00 +01002603 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002604 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002605
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002606 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002607 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002608 raw_spin_lock(&ctx->lock);
2609 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002610
Peter Zijlstra63cae122016-12-09 14:59:00 +01002611 reprogram = (ctx->task == current);
2612
2613 /*
2614 * If the task is running, it must be running on this CPU,
2615 * otherwise we cannot reprogram things.
2616 *
2617 * If its not running, we don't care, ctx->lock will
2618 * serialize against it becoming runnable.
2619 */
2620 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002621 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002622 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002623 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002624
Peter Zijlstra63cae122016-12-09 14:59:00 +01002625 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002626 } else if (task_ctx) {
2627 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002628 }
2629
leilei.lin33801b92018-03-06 17:36:37 +08002630#ifdef CONFIG_CGROUP_PERF
2631 if (is_cgroup_event(event)) {
2632 /*
2633 * If the current cgroup doesn't match the event's
2634 * cgroup, we should not try to schedule it.
2635 */
2636 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
2637 reprogram = cgroup_is_descendant(cgrp->css.cgroup,
2638 event->cgrp->css.cgroup);
2639 }
2640#endif
2641
Peter Zijlstra63cae122016-12-09 14:59:00 +01002642 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002643 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2644 add_event_to_ctx(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002645 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstraa0963092016-02-24 18:45:50 +01002646 } else {
2647 add_event_to_ctx(event, ctx);
2648 }
2649
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002650unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002651 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002652
Peter Zijlstraa0963092016-02-24 18:45:50 +01002653 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002654}
2655
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002656static bool exclusive_event_installable(struct perf_event *event,
2657 struct perf_event_context *ctx);
2658
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002659/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002660 * Attach a performance event to a context.
2661 *
2662 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002663 */
2664static void
2665perf_install_in_context(struct perf_event_context *ctx,
2666 struct perf_event *event,
2667 int cpu)
2668{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002669 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002670
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002671 lockdep_assert_held(&ctx->mutex);
2672
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002673 WARN_ON_ONCE(!exclusive_event_installable(event, ctx));
2674
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002675 if (event->cpu != -1)
2676 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002677
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002678 /*
2679 * Ensures that if we can observe event->ctx, both the event and ctx
2680 * will be 'complete'. See perf_iterate_sb_cpu().
2681 */
2682 smp_store_release(&event->ctx, ctx);
2683
Peter Zijlstradb0503e2019-10-21 16:02:39 +02002684 /*
2685 * perf_event_attr::disabled events will not run and can be initialized
2686 * without IPI. Except when this is the first event for the context, in
2687 * that case we need the magic of the IPI to set ctx->is_active.
2688 *
2689 * The IOC_ENABLE that is sure to follow the creation of a disabled
2690 * event will issue the IPI and reprogram the hardware.
2691 */
2692 if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && ctx->nr_events) {
2693 raw_spin_lock_irq(&ctx->lock);
2694 if (ctx->task == TASK_TOMBSTONE) {
2695 raw_spin_unlock_irq(&ctx->lock);
2696 return;
2697 }
2698 add_event_to_ctx(event, ctx);
2699 raw_spin_unlock_irq(&ctx->lock);
2700 return;
2701 }
2702
Peter Zijlstraa0963092016-02-24 18:45:50 +01002703 if (!task) {
2704 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002705 return;
2706 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002707
Peter Zijlstraa0963092016-02-24 18:45:50 +01002708 /*
2709 * Should not happen, we validate the ctx is still alive before calling.
2710 */
2711 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2712 return;
2713
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714 /*
2715 * Installing events is tricky because we cannot rely on ctx->is_active
2716 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra63cae122016-12-09 14:59:00 +01002717 *
2718 * Instead we use task_curr(), which tells us if the task is running.
2719 * However, since we use task_curr() outside of rq::lock, we can race
2720 * against the actual state. This means the result can be wrong.
2721 *
2722 * If we get a false positive, we retry, this is harmless.
2723 *
2724 * If we get a false negative, things are complicated. If we are after
2725 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2726 * value must be correct. If we're before, it doesn't matter since
2727 * perf_event_context_sched_in() will program the counter.
2728 *
2729 * However, this hinges on the remote context switch having observed
2730 * our task->perf_event_ctxp[] store, such that it will in fact take
2731 * ctx::lock in perf_event_context_sched_in().
2732 *
2733 * We do this by task_function_call(), if the IPI fails to hit the task
2734 * we know any future context switch of task must see the
2735 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002736 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002737
Peter Zijlstraa0963092016-02-24 18:45:50 +01002738 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002739 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2740 * task_cpu() load, such that if the IPI then does not find the task
2741 * running, a future context switch of that task must observe the
2742 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002743 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002744 smp_mb();
2745again:
2746 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002747 return;
2748
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002749 raw_spin_lock_irq(&ctx->lock);
2750 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002751 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2752 /*
2753 * Cannot happen because we already checked above (which also
2754 * cannot happen), and we hold ctx->mutex, which serializes us
2755 * against perf_event_exit_task_context().
2756 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002757 raw_spin_unlock_irq(&ctx->lock);
2758 return;
2759 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002760 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002761 * If the task is not running, ctx->lock will avoid it becoming so,
2762 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002763 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002764 if (task_curr(task)) {
2765 raw_spin_unlock_irq(&ctx->lock);
2766 goto again;
2767 }
2768 add_event_to_ctx(event, ctx);
2769 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770}
2771
2772/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773 * Cross CPU call to enable a performance event
2774 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002775static void __perf_event_enable(struct perf_event *event,
2776 struct perf_cpu_context *cpuctx,
2777 struct perf_event_context *ctx,
2778 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002779{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002780 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002781 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002783 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2784 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002785 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002786
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002787 if (ctx->is_active)
2788 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2789
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002790 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002792 if (!ctx->is_active)
2793 return;
2794
Stephane Eraniane5d13672011-02-14 11:20:01 +02002795 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002796 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002797 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002798 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002799
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002800 /*
2801 * If the event is in a group and isn't the group leader,
2802 * then don't put it on unless the group is on.
2803 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002804 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2805 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002806 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002807 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002809 task_ctx = cpuctx->task_ctx;
2810 if (ctx->task)
2811 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002812
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002813 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstra7b648012015-12-03 18:35:21 +01002814}
2815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002816/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002817 * Enable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002818 *
2819 * If event->ctx is a cloned context, callers must make sure that
2820 * every task struct that event->ctx->task could possibly point to
2821 * remains valid. This condition is satisfied when called through
2822 * perf_event_for_each_child or perf_event_for_each as described
2823 * for perf_event_disable.
2824 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002825static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002826{
2827 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002828
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002829 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002830 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2831 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002832 raw_spin_unlock_irq(&ctx->lock);
2833 return;
2834 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002835
2836 /*
2837 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002838 *
2839 * That way, if we see the event in error state below, we know that it
2840 * has gone back into error state, as distinct from the task having
2841 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842 */
2843 if (event->state == PERF_EVENT_STATE_ERROR)
2844 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002845 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002846
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002847 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002848}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002849
2850/*
2851 * See perf_event_disable();
2852 */
2853void perf_event_enable(struct perf_event *event)
2854{
2855 struct perf_event_context *ctx;
2856
2857 ctx = perf_event_ctx_lock(event);
2858 _perf_event_enable(event);
2859 perf_event_ctx_unlock(event, ctx);
2860}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002861EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002862
Alexander Shishkin375637b2016-04-27 18:44:46 +03002863struct stop_event_data {
2864 struct perf_event *event;
2865 unsigned int restart;
2866};
2867
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002868static int __perf_event_stop(void *info)
2869{
Alexander Shishkin375637b2016-04-27 18:44:46 +03002870 struct stop_event_data *sd = info;
2871 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002872
Alexander Shishkin375637b2016-04-27 18:44:46 +03002873 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002874 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2875 return 0;
2876
2877 /* matches smp_wmb() in event_sched_in() */
2878 smp_rmb();
2879
2880 /*
2881 * There is a window with interrupts enabled before we get here,
2882 * so we need to check again lest we try to stop another CPU's event.
2883 */
2884 if (READ_ONCE(event->oncpu) != smp_processor_id())
2885 return -EAGAIN;
2886
2887 event->pmu->stop(event, PERF_EF_UPDATE);
2888
Alexander Shishkin375637b2016-04-27 18:44:46 +03002889 /*
2890 * May race with the actual stop (through perf_pmu_output_stop()),
2891 * but it is only used for events with AUX ring buffer, and such
2892 * events will refuse to restart because of rb::aux_mmap_count==0,
2893 * see comments in perf_aux_output_begin().
2894 *
Tobias Tefke788faab2018-07-09 12:57:15 +02002895 * Since this is happening on an event-local CPU, no trace is lost
Alexander Shishkin375637b2016-04-27 18:44:46 +03002896 * while restarting.
2897 */
2898 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01002899 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03002900
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002901 return 0;
2902}
2903
Alexander Shishkin767ae082016-09-06 16:23:49 +03002904static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03002905{
2906 struct stop_event_data sd = {
2907 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03002908 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03002909 };
2910 int ret = 0;
2911
2912 do {
2913 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2914 return 0;
2915
2916 /* matches smp_wmb() in event_sched_in() */
2917 smp_rmb();
2918
2919 /*
2920 * We only want to restart ACTIVE events, so if the event goes
2921 * inactive here (event->oncpu==-1), there's nothing more to do;
2922 * fall through with ret==-ENXIO.
2923 */
2924 ret = cpu_function_call(READ_ONCE(event->oncpu),
2925 __perf_event_stop, &sd);
2926 } while (ret == -EAGAIN);
2927
2928 return ret;
2929}
2930
2931/*
2932 * In order to contain the amount of racy and tricky in the address filter
2933 * configuration management, it is a two part process:
2934 *
2935 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2936 * we update the addresses of corresponding vmas in
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02002937 * event::addr_filter_ranges array and bump the event::addr_filters_gen;
Alexander Shishkin375637b2016-04-27 18:44:46 +03002938 * (p2) when an event is scheduled in (pmu::add), it calls
2939 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2940 * if the generation has changed since the previous call.
2941 *
2942 * If (p1) happens while the event is active, we restart it to force (p2).
2943 *
2944 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2945 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2946 * ioctl;
2947 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2948 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2949 * for reading;
2950 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2951 * of exec.
2952 */
2953void perf_event_addr_filters_sync(struct perf_event *event)
2954{
2955 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2956
2957 if (!has_addr_filter(event))
2958 return;
2959
2960 raw_spin_lock(&ifh->lock);
2961 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2962 event->pmu->addr_filters_sync(event);
2963 event->hw.addr_filters_gen = event->addr_filters_gen;
2964 }
2965 raw_spin_unlock(&ifh->lock);
2966}
2967EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2968
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002969static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970{
2971 /*
2972 * not supported on inherited events
2973 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002974 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002975 return -EINVAL;
2976
2977 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002978 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002979
2980 return 0;
2981}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002982
2983/*
2984 * See perf_event_disable()
2985 */
2986int perf_event_refresh(struct perf_event *event, int refresh)
2987{
2988 struct perf_event_context *ctx;
2989 int ret;
2990
2991 ctx = perf_event_ctx_lock(event);
2992 ret = _perf_event_refresh(event, refresh);
2993 perf_event_ctx_unlock(event, ctx);
2994
2995 return ret;
2996}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002997EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998
Milind Chabbi32ff77e2018-03-12 14:45:47 +01002999static int perf_event_modify_breakpoint(struct perf_event *bp,
3000 struct perf_event_attr *attr)
3001{
3002 int err;
3003
3004 _perf_event_disable(bp);
3005
3006 err = modify_user_hw_breakpoint_check(bp, attr, true);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003007
Jiri Olsabf062782018-08-27 11:12:28 +02003008 if (!bp->attr.disabled)
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003009 _perf_event_enable(bp);
Jiri Olsabf062782018-08-27 11:12:28 +02003010
3011 return err;
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003012}
3013
3014static int perf_event_modify_attr(struct perf_event *event,
3015 struct perf_event_attr *attr)
3016{
3017 if (event->attr.type != attr->type)
3018 return -EINVAL;
3019
3020 switch (event->attr.type) {
3021 case PERF_TYPE_BREAKPOINT:
3022 return perf_event_modify_breakpoint(event, attr);
3023 default:
3024 /* Place holder for future additions. */
3025 return -EOPNOTSUPP;
3026 }
3027}
3028
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003029static void ctx_sched_out(struct perf_event_context *ctx,
3030 struct perf_cpu_context *cpuctx,
3031 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003032{
Peter Zijlstra66681282017-11-13 14:28:38 +01003033 struct perf_event *event, *tmp;
Peter Zijlstradb24d332011-04-09 21:17:45 +02003034 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003035
3036 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037
Peter Zijlstra39a43642016-01-11 12:46:35 +01003038 if (likely(!ctx->nr_events)) {
3039 /*
3040 * See __perf_remove_from_context().
3041 */
3042 WARN_ON_ONCE(ctx->is_active);
3043 if (ctx->task)
3044 WARN_ON_ONCE(cpuctx->task_ctx);
3045 return;
3046 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047
Peter Zijlstradb24d332011-04-09 21:17:45 +02003048 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003049 if (!(ctx->is_active & EVENT_ALL))
3050 ctx->is_active = 0;
3051
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003052 if (ctx->task) {
3053 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3054 if (!ctx->is_active)
3055 cpuctx->task_ctx = NULL;
3056 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003057
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003058 /*
3059 * Always update time if it was set; not only when it changes.
3060 * Otherwise we can 'forget' to update time for any but the last
3061 * context we sched out. For example:
3062 *
3063 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
3064 * ctx_sched_out(.event_type = EVENT_PINNED)
3065 *
3066 * would only update time for the pinned events.
3067 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003068 if (is_active & EVENT_TIME) {
3069 /* update (and stop) ctx time */
3070 update_context_time(ctx);
3071 update_cgrp_time_from_cpuctx(cpuctx);
3072 }
3073
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003074 is_active ^= ctx->is_active; /* changed bits */
3075
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003076 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003077 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003078
Ian Rogersfd7d5512019-06-01 01:27:22 -07003079 /*
3080 * If we had been multiplexing, no rotations are necessary, now no events
3081 * are active.
3082 */
3083 ctx->rotate_necessary = 0;
3084
Peter Zijlstra075e0b02011-04-09 21:17:40 +02003085 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003086 if (is_active & EVENT_PINNED) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003087 list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003088 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003089 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003090
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003091 if (is_active & EVENT_FLEXIBLE) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003092 list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003093 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003094 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003095 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096}
3097
3098/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003099 * Test whether two contexts are equivalent, i.e. whether they have both been
3100 * cloned from the same version of the same context.
3101 *
3102 * Equivalence is measured using a generation number in the context that is
3103 * incremented on each modification to it; see unclone_ctx(), list_add_event()
3104 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105 */
3106static int context_equiv(struct perf_event_context *ctx1,
3107 struct perf_event_context *ctx2)
3108{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003109 lockdep_assert_held(&ctx1->lock);
3110 lockdep_assert_held(&ctx2->lock);
3111
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003112 /* Pinning disables the swap optimization */
3113 if (ctx1->pin_count || ctx2->pin_count)
3114 return 0;
3115
3116 /* If ctx1 is the parent of ctx2 */
3117 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
3118 return 1;
3119
3120 /* If ctx2 is the parent of ctx1 */
3121 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
3122 return 1;
3123
3124 /*
3125 * If ctx1 and ctx2 have the same parent; we flatten the parent
3126 * hierarchy, see perf_event_init_context().
3127 */
3128 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
3129 ctx1->parent_gen == ctx2->parent_gen)
3130 return 1;
3131
3132 /* Unmatched */
3133 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003134}
3135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136static void __perf_event_sync_stat(struct perf_event *event,
3137 struct perf_event *next_event)
3138{
3139 u64 value;
3140
3141 if (!event->attr.inherit_stat)
3142 return;
3143
3144 /*
3145 * Update the event value, we cannot use perf_event_read()
3146 * because we're in the middle of a context switch and have IRQs
3147 * disabled, which upsets smp_call_function_single(), however
3148 * we know the event must be on the current CPU, therefore we
3149 * don't need to use it.
3150 */
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003151 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01003152 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003154 perf_event_update_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003155
3156 /*
3157 * In order to keep per-task stats reliable we need to flip the event
3158 * values when we flip the contexts.
3159 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02003160 value = local64_read(&next_event->count);
3161 value = local64_xchg(&event->count, value);
3162 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003163
3164 swap(event->total_time_enabled, next_event->total_time_enabled);
3165 swap(event->total_time_running, next_event->total_time_running);
3166
3167 /*
3168 * Since we swizzled the values, update the user visible data too.
3169 */
3170 perf_event_update_userpage(event);
3171 perf_event_update_userpage(next_event);
3172}
3173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003174static void perf_event_sync_stat(struct perf_event_context *ctx,
3175 struct perf_event_context *next_ctx)
3176{
3177 struct perf_event *event, *next_event;
3178
3179 if (!ctx->nr_stat)
3180 return;
3181
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01003182 update_context_time(ctx);
3183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003184 event = list_first_entry(&ctx->event_list,
3185 struct perf_event, event_entry);
3186
3187 next_event = list_first_entry(&next_ctx->event_list,
3188 struct perf_event, event_entry);
3189
3190 while (&event->event_entry != &ctx->event_list &&
3191 &next_event->event_entry != &next_ctx->event_list) {
3192
3193 __perf_event_sync_stat(event, next_event);
3194
3195 event = list_next_entry(event, event_entry);
3196 next_event = list_next_entry(next_event, event_entry);
3197 }
3198}
3199
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003200static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
3201 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003203 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003205 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003206 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 int do_switch = 1;
3208
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003209 if (likely(!ctx))
3210 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003212 cpuctx = __get_cpu_context(ctx);
3213 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214 return;
3215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003217 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003218 if (!next_ctx)
3219 goto unlock;
3220
3221 parent = rcu_dereference(ctx->parent_ctx);
3222 next_parent = rcu_dereference(next_ctx->parent_ctx);
3223
3224 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02003225 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003226 goto unlock;
3227
3228 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003229 /*
3230 * Looks like the two contexts are clones, so we might be
3231 * able to optimize the context switch. We lock both
3232 * contexts and check that they are clones under the
3233 * lock (including re-checking that neither has been
3234 * uncloned in the meantime). It doesn't matter which
3235 * order we take the locks because no other cpu could
3236 * be trying to lock both of these tasks.
3237 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003238 raw_spin_lock(&ctx->lock);
3239 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003240 if (context_equiv(ctx, next_ctx)) {
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003241 struct pmu *pmu = ctx->pmu;
3242
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003243 WRITE_ONCE(ctx->task, next);
3244 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003245
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003246 /*
3247 * PMU specific parts of task perf context can require
3248 * additional synchronization. As an example of such
3249 * synchronization see implementation details of Intel
3250 * LBR call stack data profiling;
3251 */
3252 if (pmu->swap_task_ctx)
3253 pmu->swap_task_ctx(ctx, next_ctx);
3254 else
3255 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003256
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003257 /*
3258 * RCU_INIT_POINTER here is safe because we've not
3259 * modified the ctx and the above modification of
3260 * ctx->task and ctx->task_ctx_data are immaterial
3261 * since those values are always verified under
3262 * ctx->lock which we're now holding.
3263 */
3264 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
3265 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
3266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003267 do_switch = 0;
3268
3269 perf_event_sync_stat(ctx, next_ctx);
3270 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003271 raw_spin_unlock(&next_ctx->lock);
3272 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003274unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003275 rcu_read_unlock();
3276
3277 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003278 raw_spin_lock(&ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003279 task_ctx_sched_out(cpuctx, ctx, EVENT_ALL);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003280 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 }
3282}
3283
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003284static DEFINE_PER_CPU(struct list_head, sched_cb_list);
3285
Yan, Zhengba532502014-11-04 21:55:58 -05003286void perf_sched_cb_dec(struct pmu *pmu)
3287{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003288 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3289
Yan, Zhengba532502014-11-04 21:55:58 -05003290 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003291
3292 if (!--cpuctx->sched_cb_usage)
3293 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05003294}
3295
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003296
Yan, Zhengba532502014-11-04 21:55:58 -05003297void perf_sched_cb_inc(struct pmu *pmu)
3298{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003299 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3300
3301 if (!cpuctx->sched_cb_usage++)
3302 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
3303
Yan, Zhengba532502014-11-04 21:55:58 -05003304 this_cpu_inc(perf_sched_cb_usages);
3305}
3306
3307/*
3308 * This function provides the context switch callback to the lower code
3309 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02003310 *
3311 * This callback is relevant even to per-cpu events; for example multi event
3312 * PEBS requires this to provide PID/TID information. This requires we flush
3313 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05003314 */
3315static void perf_pmu_sched_task(struct task_struct *prev,
3316 struct task_struct *next,
3317 bool sched_in)
3318{
3319 struct perf_cpu_context *cpuctx;
3320 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05003321
3322 if (prev == next)
3323 return;
3324
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003325 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
David Carrillo-Cisneros1fd7e412017-01-18 11:24:54 -08003326 pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05003327
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003328 if (WARN_ON_ONCE(!pmu->sched_task))
3329 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05003330
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003331 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
3332 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05003333
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003334 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05003335
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003336 perf_pmu_enable(pmu);
3337 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05003338 }
Yan, Zhengba532502014-11-04 21:55:58 -05003339}
3340
Adrian Hunter45ac1402015-07-21 12:44:02 +03003341static void perf_event_switch(struct task_struct *task,
3342 struct task_struct *next_prev, bool sched_in);
3343
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003344#define for_each_task_context_nr(ctxn) \
3345 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
3346
3347/*
3348 * Called from scheduler to remove the events of the current task,
3349 * with interrupts disabled.
3350 *
3351 * We stop each event and update the event value in event->count.
3352 *
3353 * This does not protect us against NMI, but disable()
3354 * sets the disabled bit in the control field of event _before_
3355 * accessing the event control register. If a NMI hits, then it will
3356 * not restart the event.
3357 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003358void __perf_event_task_sched_out(struct task_struct *task,
3359 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003360{
3361 int ctxn;
3362
Yan, Zhengba532502014-11-04 21:55:58 -05003363 if (__this_cpu_read(perf_sched_cb_usages))
3364 perf_pmu_sched_task(task, next, false);
3365
Adrian Hunter45ac1402015-07-21 12:44:02 +03003366 if (atomic_read(&nr_switch_events))
3367 perf_event_switch(task, next, false);
3368
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003369 for_each_task_context_nr(ctxn)
3370 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003371
3372 /*
3373 * if cgroup events exist on this CPU, then we need
3374 * to check if we have to switch out PMU state.
3375 * cgroup event are system-wide mode only
3376 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003377 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003378 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003379}
3380
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381/*
3382 * Called with IRQs disabled
3383 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003384static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3385 enum event_type_t event_type)
3386{
3387 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003388}
3389
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003390static int visit_groups_merge(struct perf_event_groups *groups, int cpu,
3391 int (*func)(struct perf_event *, void *), void *data)
3392{
3393 struct perf_event **evt, *evt1, *evt2;
3394 int ret;
3395
3396 evt1 = perf_event_groups_first(groups, -1);
3397 evt2 = perf_event_groups_first(groups, cpu);
3398
3399 while (evt1 || evt2) {
3400 if (evt1 && evt2) {
3401 if (evt1->group_index < evt2->group_index)
3402 evt = &evt1;
3403 else
3404 evt = &evt2;
3405 } else if (evt1) {
3406 evt = &evt1;
3407 } else {
3408 evt = &evt2;
3409 }
3410
3411 ret = func(*evt, data);
3412 if (ret)
3413 return ret;
3414
3415 *evt = perf_event_groups_next(*evt);
3416 }
3417
3418 return 0;
3419}
3420
3421struct sched_in_data {
3422 struct perf_event_context *ctx;
3423 struct perf_cpu_context *cpuctx;
3424 int can_add_hw;
3425};
3426
3427static int pinned_sched_in(struct perf_event *event, void *data)
3428{
3429 struct sched_in_data *sid = data;
3430
3431 if (event->state <= PERF_EVENT_STATE_OFF)
3432 return 0;
3433
3434 if (!event_filter_match(event))
3435 return 0;
3436
Peter Zijlstra66681282017-11-13 14:28:38 +01003437 if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) {
3438 if (!group_sched_in(event, sid->cpuctx, sid->ctx))
3439 list_add_tail(&event->active_list, &sid->ctx->pinned_active);
3440 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003441
3442 /*
3443 * If this pinned group hasn't been scheduled,
3444 * put it in error state.
3445 */
3446 if (event->state == PERF_EVENT_STATE_INACTIVE)
3447 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
3448
3449 return 0;
3450}
3451
3452static int flexible_sched_in(struct perf_event *event, void *data)
3453{
3454 struct sched_in_data *sid = data;
3455
3456 if (event->state <= PERF_EVENT_STATE_OFF)
3457 return 0;
3458
3459 if (!event_filter_match(event))
3460 return 0;
3461
3462 if (group_can_go_on(event, sid->cpuctx, sid->can_add_hw)) {
Ian Rogersfd7d5512019-06-01 01:27:22 -07003463 int ret = group_sched_in(event, sid->cpuctx, sid->ctx);
3464 if (ret) {
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003465 sid->can_add_hw = 0;
Ian Rogersfd7d5512019-06-01 01:27:22 -07003466 sid->ctx->rotate_necessary = 1;
3467 return 0;
3468 }
3469 list_add_tail(&event->active_list, &sid->ctx->flexible_active);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003470 }
3471
3472 return 0;
3473}
3474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003475static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003476ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003477 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478{
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003479 struct sched_in_data sid = {
3480 .ctx = ctx,
3481 .cpuctx = cpuctx,
3482 .can_add_hw = 1,
3483 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003484
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003485 visit_groups_merge(&ctx->pinned_groups,
3486 smp_processor_id(),
3487 pinned_sched_in, &sid);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003488}
3489
3490static void
3491ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003492 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003493{
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003494 struct sched_in_data sid = {
3495 .ctx = ctx,
3496 .cpuctx = cpuctx,
3497 .can_add_hw = 1,
3498 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003500 visit_groups_merge(&ctx->flexible_groups,
3501 smp_processor_id(),
3502 flexible_sched_in, &sid);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003503}
3504
3505static void
3506ctx_sched_in(struct perf_event_context *ctx,
3507 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003508 enum event_type_t event_type,
3509 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003510{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003511 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003512 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003513
Peter Zijlstrac994d612016-01-08 09:20:23 +01003514 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003515
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003516 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003517 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003518
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003519 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003520 if (ctx->task) {
3521 if (!is_active)
3522 cpuctx->task_ctx = ctx;
3523 else
3524 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3525 }
3526
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003527 is_active ^= ctx->is_active; /* changed bits */
3528
3529 if (is_active & EVENT_TIME) {
3530 /* start ctx time */
3531 now = perf_clock();
3532 ctx->timestamp = now;
3533 perf_cgroup_set_timestamp(task, ctx);
3534 }
3535
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003536 /*
3537 * First go through the list and put on any pinned groups
3538 * in order to give them the best chance of going on.
3539 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003540 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003541 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003542
3543 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003544 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003545 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546}
3547
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003548static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003549 enum event_type_t event_type,
3550 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003551{
3552 struct perf_event_context *ctx = &cpuctx->ctx;
3553
Stephane Eraniane5d13672011-02-14 11:20:01 +02003554 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003555}
3556
Stephane Eraniane5d13672011-02-14 11:20:01 +02003557static void perf_event_context_sched_in(struct perf_event_context *ctx,
3558 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003560 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003561
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003562 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003563 if (cpuctx->task_ctx == ctx)
3564 return;
3565
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003566 perf_ctx_lock(cpuctx, ctx);
leilei.linfdccc3f2017-08-09 08:29:21 +08003567 /*
3568 * We must check ctx->nr_events while holding ctx->lock, such
3569 * that we serialize against perf_install_in_context().
3570 */
3571 if (!ctx->nr_events)
3572 goto unlock;
3573
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003574 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003575 /*
3576 * We want to keep the following priority order:
3577 * cpu pinned (that don't need to move), task pinned,
3578 * cpu flexible, task flexible.
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003579 *
3580 * However, if task's ctx is not carrying any pinned
3581 * events, no need to flip the cpuctx's events around.
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003582 */
Alexey Budankov8e1a2032017-09-08 11:47:03 +03003583 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree))
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003584 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003585 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003586 perf_pmu_enable(ctx->pmu);
leilei.linfdccc3f2017-08-09 08:29:21 +08003587
3588unlock:
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003589 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003590}
3591
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003592/*
3593 * Called from scheduler to add the events of the current task
3594 * with interrupts disabled.
3595 *
3596 * We restore the event value and then enable it.
3597 *
3598 * This does not protect us against NMI, but enable()
3599 * sets the enabled bit in the control field of event _before_
3600 * accessing the event control register. If a NMI hits, then it will
3601 * keep the event running.
3602 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003603void __perf_event_task_sched_in(struct task_struct *prev,
3604 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003605{
3606 struct perf_event_context *ctx;
3607 int ctxn;
3608
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003609 /*
3610 * If cgroup events exist on this CPU, then we need to check if we have
3611 * to switch in PMU state; cgroup event are system-wide mode only.
3612 *
3613 * Since cgroup events are CPU events, we must schedule these in before
3614 * we schedule in the task events.
3615 */
3616 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3617 perf_cgroup_sched_in(prev, task);
3618
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003619 for_each_task_context_nr(ctxn) {
3620 ctx = task->perf_event_ctxp[ctxn];
3621 if (likely(!ctx))
3622 continue;
3623
Stephane Eraniane5d13672011-02-14 11:20:01 +02003624 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003625 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003626
Adrian Hunter45ac1402015-07-21 12:44:02 +03003627 if (atomic_read(&nr_switch_events))
3628 perf_event_switch(task, prev, true);
3629
Yan, Zhengba532502014-11-04 21:55:58 -05003630 if (__this_cpu_read(perf_sched_cb_usages))
3631 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003632}
3633
Peter Zijlstraabd50712010-01-26 18:50:16 +01003634static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3635{
3636 u64 frequency = event->attr.sample_freq;
3637 u64 sec = NSEC_PER_SEC;
3638 u64 divisor, dividend;
3639
3640 int count_fls, nsec_fls, frequency_fls, sec_fls;
3641
3642 count_fls = fls64(count);
3643 nsec_fls = fls64(nsec);
3644 frequency_fls = fls64(frequency);
3645 sec_fls = 30;
3646
3647 /*
3648 * We got @count in @nsec, with a target of sample_freq HZ
3649 * the target period becomes:
3650 *
3651 * @count * 10^9
3652 * period = -------------------
3653 * @nsec * sample_freq
3654 *
3655 */
3656
3657 /*
3658 * Reduce accuracy by one bit such that @a and @b converge
3659 * to a similar magnitude.
3660 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003661#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003662do { \
3663 if (a##_fls > b##_fls) { \
3664 a >>= 1; \
3665 a##_fls--; \
3666 } else { \
3667 b >>= 1; \
3668 b##_fls--; \
3669 } \
3670} while (0)
3671
3672 /*
3673 * Reduce accuracy until either term fits in a u64, then proceed with
3674 * the other, so that finally we can do a u64/u64 division.
3675 */
3676 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3677 REDUCE_FLS(nsec, frequency);
3678 REDUCE_FLS(sec, count);
3679 }
3680
3681 if (count_fls + sec_fls > 64) {
3682 divisor = nsec * frequency;
3683
3684 while (count_fls + sec_fls > 64) {
3685 REDUCE_FLS(count, sec);
3686 divisor >>= 1;
3687 }
3688
3689 dividend = count * sec;
3690 } else {
3691 dividend = count * sec;
3692
3693 while (nsec_fls + frequency_fls > 64) {
3694 REDUCE_FLS(nsec, frequency);
3695 dividend >>= 1;
3696 }
3697
3698 divisor = nsec * frequency;
3699 }
3700
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003701 if (!divisor)
3702 return dividend;
3703
Peter Zijlstraabd50712010-01-26 18:50:16 +01003704 return div64_u64(dividend, divisor);
3705}
3706
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003707static DEFINE_PER_CPU(int, perf_throttled_count);
3708static DEFINE_PER_CPU(u64, perf_throttled_seq);
3709
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003710static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711{
3712 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003713 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003714 s64 delta;
3715
Peter Zijlstraabd50712010-01-26 18:50:16 +01003716 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717
3718 delta = (s64)(period - hwc->sample_period);
3719 delta = (delta + 7) / 8; /* low pass filter */
3720
3721 sample_period = hwc->sample_period + delta;
3722
3723 if (!sample_period)
3724 sample_period = 1;
3725
3726 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003727
Peter Zijlstrae7850592010-05-21 14:43:08 +02003728 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003729 if (disable)
3730 event->pmu->stop(event, PERF_EF_UPDATE);
3731
Peter Zijlstrae7850592010-05-21 14:43:08 +02003732 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003733
3734 if (disable)
3735 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003736 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737}
3738
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003739/*
3740 * combine freq adjustment with unthrottling to avoid two passes over the
3741 * events. At the same time, make sure, having freq events does not change
3742 * the rate of unthrottling as that would introduce bias.
3743 */
3744static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3745 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746{
3747 struct perf_event *event;
3748 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003749 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003750 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003751
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003752 /*
3753 * only need to iterate over all events iff:
3754 * - context have events in frequency mode (needs freq adjust)
3755 * - there are events to unthrottle on this cpu
3756 */
3757 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003758 return;
3759
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003760 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003761 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003762
Paul Mackerras03541f82009-10-14 16:58:03 +11003763 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764 if (event->state != PERF_EVENT_STATE_ACTIVE)
3765 continue;
3766
Stephane Eranian5632ab12011-01-03 18:20:01 +02003767 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003768 continue;
3769
Alexander Shishkin44377272013-12-16 14:17:36 +02003770 perf_pmu_disable(event->pmu);
3771
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003772 hwc = &event->hw;
3773
Jiri Olsaae23bff2013-08-24 16:45:54 +02003774 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003775 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003776 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003777 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003778 }
3779
3780 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003781 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003783 /*
3784 * stop the event and update event->count
3785 */
3786 event->pmu->stop(event, PERF_EF_UPDATE);
3787
Peter Zijlstrae7850592010-05-21 14:43:08 +02003788 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003789 delta = now - hwc->freq_count_stamp;
3790 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003791
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003792 /*
3793 * restart the event
3794 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003795 * we have stopped the event so tell that
3796 * to perf_adjust_period() to avoid stopping it
3797 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003798 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003799 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003800 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003801
3802 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003803 next:
3804 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003805 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003806
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003807 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003808 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003809}
3810
3811/*
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003812 * Move @event to the tail of the @ctx's elegible events.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003813 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003814static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003815{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003816 /*
3817 * Rotate the first entry last of non-pinned groups. Rotation might be
3818 * disabled by the inheritance code.
3819 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003820 if (ctx->rotate_disable)
3821 return;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03003822
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003823 perf_event_groups_delete(&ctx->flexible_groups, event);
3824 perf_event_groups_insert(&ctx->flexible_groups, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003825}
3826
Song Liu7fa343b72019-10-08 09:59:49 -07003827/* pick an event from the flexible_groups to rotate */
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003828static inline struct perf_event *
Song Liu7fa343b72019-10-08 09:59:49 -07003829ctx_event_to_rotate(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003830{
Song Liu7fa343b72019-10-08 09:59:49 -07003831 struct perf_event *event;
3832
3833 /* pick the first active flexible event */
3834 event = list_first_entry_or_null(&ctx->flexible_active,
3835 struct perf_event, active_list);
3836
3837 /* if no active flexible event, pick the first event */
3838 if (!event) {
3839 event = rb_entry_safe(rb_first(&ctx->flexible_groups.tree),
3840 typeof(*event), group_node);
3841 }
3842
3843 return event;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003844}
3845
3846static bool perf_rotate_context(struct perf_cpu_context *cpuctx)
3847{
3848 struct perf_event *cpu_event = NULL, *task_event = NULL;
Ian Rogersfd7d5512019-06-01 01:27:22 -07003849 struct perf_event_context *task_ctx = NULL;
3850 int cpu_rotate, task_rotate;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003851
3852 /*
3853 * Since we run this from IRQ context, nobody can install new
3854 * events, thus the event count values are stable.
3855 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003856
Ian Rogersfd7d5512019-06-01 01:27:22 -07003857 cpu_rotate = cpuctx->ctx.rotate_necessary;
3858 task_ctx = cpuctx->task_ctx;
3859 task_rotate = task_ctx ? task_ctx->rotate_necessary : 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003860
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003861 if (!(cpu_rotate || task_rotate))
3862 return false;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003863
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003864 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003865 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003866
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003867 if (task_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07003868 task_event = ctx_event_to_rotate(task_ctx);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003869 if (cpu_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07003870 cpu_event = ctx_event_to_rotate(&cpuctx->ctx);
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003871
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003872 /*
3873 * As per the order given at ctx_resched() first 'pop' task flexible
3874 * and then, if needed CPU flexible.
3875 */
Ian Rogersfd7d5512019-06-01 01:27:22 -07003876 if (task_event || (task_ctx && cpu_event))
3877 ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003878 if (cpu_event)
3879 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003880
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003881 if (task_event)
Ian Rogersfd7d5512019-06-01 01:27:22 -07003882 rotate_ctx(task_ctx, task_event);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003883 if (cpu_event)
3884 rotate_ctx(&cpuctx->ctx, cpu_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885
Ian Rogersfd7d5512019-06-01 01:27:22 -07003886 perf_event_sched_in(cpuctx, task_ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003887
3888 perf_pmu_enable(cpuctx->ctx.pmu);
3889 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02003890
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01003891 return true;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003892}
3893
3894void perf_event_task_tick(void)
3895{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003896 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3897 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003898 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003899
Frederic Weisbecker16444642017-11-06 16:01:24 +01003900 lockdep_assert_irqs_disabled();
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003901
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003902 __this_cpu_inc(perf_throttled_seq);
3903 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02003904 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003905
Mark Rutland2fde4f92015-01-07 15:01:54 +00003906 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003907 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003908}
3909
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003910static int event_enable_on_exec(struct perf_event *event,
3911 struct perf_event_context *ctx)
3912{
3913 if (!event->attr.enable_on_exec)
3914 return 0;
3915
3916 event->attr.enable_on_exec = 0;
3917 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3918 return 0;
3919
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003920 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003921
3922 return 1;
3923}
3924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925/*
3926 * Enable all of a task's events that have been marked enable-on-exec.
3927 * This expects task == current.
3928 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01003929static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003930{
Peter Zijlstrac1274492015-12-10 20:57:40 +01003931 struct perf_event_context *ctx, *clone_ctx = NULL;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003932 enum event_type_t event_type = 0;
Peter Zijlstra3e349502016-01-08 10:01:18 +01003933 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003934 struct perf_event *event;
3935 unsigned long flags;
3936 int enabled = 0;
3937
3938 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01003939 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003940 if (!ctx || !ctx->nr_events)
3941 goto out;
3942
Peter Zijlstra3e349502016-01-08 10:01:18 +01003943 cpuctx = __get_cpu_context(ctx);
3944 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01003945 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003946 list_for_each_entry(event, &ctx->event_list, event_entry) {
Peter Zijlstra3e349502016-01-08 10:01:18 +01003947 enabled |= event_enable_on_exec(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003948 event_type |= get_event_type(event);
3949 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003950
3951 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01003952 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003953 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01003954 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003955 clone_ctx = unclone_ctx(ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003956 ctx_resched(cpuctx, ctx, event_type);
Peter Zijlstra7bbba0e2017-02-15 16:12:20 +01003957 } else {
3958 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstra3e349502016-01-08 10:01:18 +01003959 }
3960 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003961
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003962out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003964
3965 if (clone_ctx)
3966 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003967}
3968
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003969struct perf_read_data {
3970 struct perf_event *event;
3971 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07003972 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003973};
3974
Peter Zijlstra451d24d2017-01-31 11:27:10 +01003975static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003976{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003977 u16 local_pkg, event_pkg;
3978
3979 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstra451d24d2017-01-31 11:27:10 +01003980 int local_cpu = smp_processor_id();
3981
3982 event_pkg = topology_physical_package_id(event_cpu);
3983 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07003984
3985 if (event_pkg == local_pkg)
3986 return local_cpu;
3987 }
3988
3989 return event_cpu;
3990}
3991
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003992/*
3993 * Cross CPU call to read the hardware event
3994 */
3995static void __perf_event_read(void *info)
3996{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07003997 struct perf_read_data *data = info;
3998 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003999 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004000 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004001 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002
4003 /*
4004 * If this is a task context, we need to check whether it is
4005 * the current task context of this cpu. If not it has been
4006 * scheduled out before the smp call arrived. In that case
4007 * event->count would have been updated to a recent sample
4008 * when the event was scheduled out.
4009 */
4010 if (ctx->task && cpuctx->task_ctx != ctx)
4011 return;
4012
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004013 raw_spin_lock(&ctx->lock);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004014 if (ctx->is_active & EVENT_TIME) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01004015 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004016 update_cgrp_time_from_event(event);
4017 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004018
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004019 perf_event_update_time(event);
4020 if (data->group)
4021 perf_event_update_sibling_time(event);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004022
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004023 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004024 goto unlock;
4025
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004026 if (!data->group) {
4027 pmu->read(event);
4028 data->ret = 0;
4029 goto unlock;
4030 }
4031
4032 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
4033
4034 pmu->read(event);
4035
Peter Zijlstraedb39592018-03-15 17:36:56 +01004036 for_each_sibling_event(sub, event) {
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004037 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
4038 /*
4039 * Use sibling's PMU rather than @event's since
4040 * sibling could be on different (eg: software) PMU.
4041 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004042 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004043 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004044 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004045
4046 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004047
4048unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004049 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004050}
4051
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004052static inline u64 perf_event_count(struct perf_event *event)
4053{
Vikas Shivappac39a0e22017-07-25 14:14:20 -07004054 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004055}
4056
Kaixu Xiaffe86902015-08-06 07:02:32 +00004057/*
4058 * NMI-safe method to read a local event, that is an event that
4059 * is:
4060 * - either for the current task, or for this CPU
4061 * - does not have inherit set, for inherited task events
4062 * will not be local and we cannot read them atomically
4063 * - must not have a pmu::count method
4064 */
Yonghong Song7d9285e2017-10-05 09:19:19 -07004065int perf_event_read_local(struct perf_event *event, u64 *value,
4066 u64 *enabled, u64 *running)
Kaixu Xiaffe86902015-08-06 07:02:32 +00004067{
4068 unsigned long flags;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004069 int ret = 0;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004070
4071 /*
4072 * Disabling interrupts avoids all counter scheduling (context
4073 * switches, timer based rotation and IPIs).
4074 */
4075 local_irq_save(flags);
4076
Kaixu Xiaffe86902015-08-06 07:02:32 +00004077 /*
4078 * It must not be an event with inherit set, we cannot read
4079 * all child counters from atomic context.
4080 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004081 if (event->attr.inherit) {
4082 ret = -EOPNOTSUPP;
4083 goto out;
4084 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004085
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004086 /* If this is a per-task event, it must be for current */
4087 if ((event->attach_state & PERF_ATTACH_TASK) &&
4088 event->hw.target != current) {
4089 ret = -EINVAL;
4090 goto out;
4091 }
4092
4093 /* If this is a per-CPU event, it must be for this CPU */
4094 if (!(event->attach_state & PERF_ATTACH_TASK) &&
4095 event->cpu != smp_processor_id()) {
4096 ret = -EINVAL;
4097 goto out;
4098 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004099
Reinette Chatrebefb1b32018-09-19 10:29:06 -07004100 /* If this is a pinned event it must be running on this CPU */
4101 if (event->attr.pinned && event->oncpu != smp_processor_id()) {
4102 ret = -EBUSY;
4103 goto out;
4104 }
4105
Kaixu Xiaffe86902015-08-06 07:02:32 +00004106 /*
4107 * If the event is currently on this CPU, its either a per-task event,
4108 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
4109 * oncpu == -1).
4110 */
4111 if (event->oncpu == smp_processor_id())
4112 event->pmu->read(event);
4113
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004114 *value = local64_read(&event->count);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004115 if (enabled || running) {
4116 u64 now = event->shadow_ctx_time + perf_clock();
4117 u64 __enabled, __running;
4118
4119 __perf_update_times(event, now, &__enabled, &__running);
4120 if (enabled)
4121 *enabled = __enabled;
4122 if (running)
4123 *running = __running;
4124 }
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004125out:
Kaixu Xiaffe86902015-08-06 07:02:32 +00004126 local_irq_restore(flags);
4127
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004128 return ret;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004129}
4130
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004131static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004132{
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004133 enum perf_event_state state = READ_ONCE(event->state);
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004134 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136 /*
4137 * If event is enabled and currently active on a CPU, update the
4138 * value in the event structure:
4139 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004140again:
4141 if (state == PERF_EVENT_STATE_ACTIVE) {
4142 struct perf_read_data data;
4143
4144 /*
4145 * Orders the ->state and ->oncpu loads such that if we see
4146 * ACTIVE we must also see the right ->oncpu.
4147 *
4148 * Matches the smp_wmb() from event_sched_in().
4149 */
4150 smp_rmb();
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004151
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004152 event_cpu = READ_ONCE(event->oncpu);
4153 if ((unsigned)event_cpu >= nr_cpu_ids)
4154 return 0;
4155
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004156 data = (struct perf_read_data){
4157 .event = event,
4158 .group = group,
4159 .ret = 0,
4160 };
4161
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004162 preempt_disable();
4163 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004164
Peter Zijlstra58763142016-08-30 10:15:03 +02004165 /*
4166 * Purposely ignore the smp_call_function_single() return
4167 * value.
4168 *
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004169 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02004170 * scheduled out and that will have updated the event count.
4171 *
4172 * Therefore, either way, we'll have an up-to-date event count
4173 * after this.
4174 */
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004175 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
4176 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02004177 ret = data.ret;
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004178
4179 } else if (state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01004180 struct perf_event_context *ctx = event->ctx;
4181 unsigned long flags;
4182
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004183 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004184 state = event->state;
4185 if (state != PERF_EVENT_STATE_INACTIVE) {
4186 raw_spin_unlock_irqrestore(&ctx->lock, flags);
4187 goto again;
4188 }
4189
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004190 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004191 * May read while context is not active (e.g., thread is
4192 * blocked), in that case we cannot update context time
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004193 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004194 if (ctx->is_active & EVENT_TIME) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004195 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004196 update_cgrp_time_from_event(event);
4197 }
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004198
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004199 perf_event_update_time(event);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004200 if (group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004201 perf_event_update_sibling_time(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004203 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004204
4205 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004206}
4207
4208/*
4209 * Initialize the perf_event context in a task_struct:
4210 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02004211static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004212{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004213 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004214 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00004215 INIT_LIST_HEAD(&ctx->active_ctx_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03004216 perf_event_groups_init(&ctx->pinned_groups);
4217 perf_event_groups_init(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004218 INIT_LIST_HEAD(&ctx->event_list);
Peter Zijlstra66681282017-11-13 14:28:38 +01004219 INIT_LIST_HEAD(&ctx->pinned_active);
4220 INIT_LIST_HEAD(&ctx->flexible_active);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02004221 refcount_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222}
4223
Peter Zijlstraeb184472010-09-07 15:55:13 +02004224static struct perf_event_context *
4225alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004226{
4227 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004228
4229 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
4230 if (!ctx)
4231 return NULL;
4232
4233 __perf_event_init_context(ctx);
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -07004234 if (task)
4235 ctx->task = get_task_struct(task);
Peter Zijlstraeb184472010-09-07 15:55:13 +02004236 ctx->pmu = pmu;
4237
4238 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004239}
4240
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004241static struct task_struct *
4242find_lively_task_by_vpid(pid_t vpid)
4243{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004244 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004245
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004246 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004247 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004248 task = current;
4249 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004250 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004251 if (task)
4252 get_task_struct(task);
4253 rcu_read_unlock();
4254
4255 if (!task)
4256 return ERR_PTR(-ESRCH);
4257
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004258 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004259}
4260
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004261/*
4262 * Returns a matching context with refcount and pincount.
4263 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004264static struct perf_event_context *
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004265find_get_context(struct pmu *pmu, struct task_struct *task,
4266 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004268 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004270 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004271 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004272 int ctxn, err;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004273 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004274
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01004275 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004276 /* Must be root to operate on a CPU event: */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004277 err = perf_allow_cpu(&event->attr);
4278 if (err)
4279 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004281 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004282 ctx = &cpuctx->ctx;
4283 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004284 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004285
4286 return ctx;
4287 }
4288
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004289 err = -EINVAL;
4290 ctxn = pmu->task_ctx_nr;
4291 if (ctxn < 0)
4292 goto errout;
4293
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004294 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
4295 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
4296 if (!task_ctx_data) {
4297 err = -ENOMEM;
4298 goto errout;
4299 }
4300 }
4301
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004302retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004303 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004304 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004305 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004306 ++ctx->pin_count;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004307
4308 if (task_ctx_data && !ctx->task_ctx_data) {
4309 ctx->task_ctx_data = task_ctx_data;
4310 task_ctx_data = NULL;
4311 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004312 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004313
4314 if (clone_ctx)
4315 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004316 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02004317 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004318 err = -ENOMEM;
4319 if (!ctx)
4320 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004321
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004322 if (task_ctx_data) {
4323 ctx->task_ctx_data = task_ctx_data;
4324 task_ctx_data = NULL;
4325 }
4326
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004327 err = 0;
4328 mutex_lock(&task->perf_event_mutex);
4329 /*
4330 * If it has already passed perf_event_exit_task().
4331 * we must see PF_EXITING, it takes this mutex too.
4332 */
4333 if (task->flags & PF_EXITING)
4334 err = -ESRCH;
4335 else if (task->perf_event_ctxp[ctxn])
4336 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004337 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004338 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004339 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004340 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004341 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004342 mutex_unlock(&task->perf_event_mutex);
4343
4344 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004345 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004346
4347 if (err == -EAGAIN)
4348 goto retry;
4349 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004351 }
4352
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004353 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354 return ctx;
4355
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004356errout:
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004357 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358 return ERR_PTR(err);
4359}
4360
Li Zefan6fb29152009-10-15 11:21:42 +08004361static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004362static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08004363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004364static void free_event_rcu(struct rcu_head *head)
4365{
4366 struct perf_event *event;
4367
4368 event = container_of(head, struct perf_event, rcu_head);
4369 if (event->ns)
4370 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08004371 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004372 kfree(event);
4373}
4374
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004375static void ring_buffer_attach(struct perf_event *event,
4376 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377
Kan Liangf2fb6be2016-03-23 11:24:37 -07004378static void detach_sb_event(struct perf_event *event)
4379{
4380 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
4381
4382 raw_spin_lock(&pel->lock);
4383 list_del_rcu(&event->sb_list);
4384 raw_spin_unlock(&pel->lock);
4385}
4386
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004387static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07004388{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004389 struct perf_event_attr *attr = &event->attr;
4390
Kan Liangf2fb6be2016-03-23 11:24:37 -07004391 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004392 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004393
4394 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004395 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004396
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004397 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
4398 attr->comm || attr->comm_exec ||
Song Liu76193a92019-01-17 08:15:13 -08004399 attr->task || attr->ksymbol ||
Song Liu21038f22019-02-25 16:20:05 -08004400 attr->context_switch ||
4401 attr->bpf_event)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004402 return true;
4403 return false;
4404}
4405
4406static void unaccount_pmu_sb_event(struct perf_event *event)
4407{
4408 if (is_sb_event(event))
4409 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004410}
4411
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004412static void unaccount_event_cpu(struct perf_event *event, int cpu)
4413{
4414 if (event->parent)
4415 return;
4416
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004417 if (is_cgroup_event(event))
4418 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
4419}
4420
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004421#ifdef CONFIG_NO_HZ_FULL
4422static DEFINE_SPINLOCK(nr_freq_lock);
4423#endif
4424
4425static void unaccount_freq_event_nohz(void)
4426{
4427#ifdef CONFIG_NO_HZ_FULL
4428 spin_lock(&nr_freq_lock);
4429 if (atomic_dec_and_test(&nr_freq_events))
4430 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
4431 spin_unlock(&nr_freq_lock);
4432#endif
4433}
4434
4435static void unaccount_freq_event(void)
4436{
4437 if (tick_nohz_full_enabled())
4438 unaccount_freq_event_nohz();
4439 else
4440 atomic_dec(&nr_freq_events);
4441}
4442
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004443static void unaccount_event(struct perf_event *event)
4444{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004445 bool dec = false;
4446
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004447 if (event->parent)
4448 return;
4449
4450 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004451 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004452 if (event->attr.mmap || event->attr.mmap_data)
4453 atomic_dec(&nr_mmap_events);
4454 if (event->attr.comm)
4455 atomic_dec(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +05304456 if (event->attr.namespaces)
4457 atomic_dec(&nr_namespaces_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004458 if (event->attr.task)
4459 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02004460 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004461 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03004462 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004463 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03004464 atomic_dec(&nr_switch_events);
4465 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004466 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004467 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004468 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004469 dec = true;
Song Liu76193a92019-01-17 08:15:13 -08004470 if (event->attr.ksymbol)
4471 atomic_dec(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -08004472 if (event->attr.bpf_event)
4473 atomic_dec(&nr_bpf_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004474
Peter Zijlstra9107c892016-02-24 18:45:45 +01004475 if (dec) {
4476 if (!atomic_add_unless(&perf_sched_count, -1, 1))
4477 schedule_delayed_work(&perf_sched_work, HZ);
4478 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004479
4480 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004481
4482 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004483}
4484
Peter Zijlstra9107c892016-02-24 18:45:45 +01004485static void perf_sched_delayed(struct work_struct *work)
4486{
4487 mutex_lock(&perf_sched_mutex);
4488 if (atomic_dec_and_test(&perf_sched_count))
4489 static_branch_disable(&perf_sched_events);
4490 mutex_unlock(&perf_sched_mutex);
4491}
4492
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004493/*
4494 * The following implement mutual exclusion of events on "exclusive" pmus
4495 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
4496 * at a time, so we disallow creating events that might conflict, namely:
4497 *
4498 * 1) cpu-wide events in the presence of per-task events,
4499 * 2) per-task events in the presence of cpu-wide events,
4500 * 3) two matching events on the same context.
4501 *
4502 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004503 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004504 */
4505static int exclusive_event_init(struct perf_event *event)
4506{
4507 struct pmu *pmu = event->pmu;
4508
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004509 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004510 return 0;
4511
4512 /*
4513 * Prevent co-existence of per-task and cpu-wide events on the
4514 * same exclusive pmu.
4515 *
4516 * Negative pmu::exclusive_cnt means there are cpu-wide
4517 * events on this "exclusive" pmu, positive means there are
4518 * per-task events.
4519 *
4520 * Since this is called in perf_event_alloc() path, event::ctx
4521 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4522 * to mean "per-task event", because unlike other attach states it
4523 * never gets cleared.
4524 */
4525 if (event->attach_state & PERF_ATTACH_TASK) {
4526 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4527 return -EBUSY;
4528 } else {
4529 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4530 return -EBUSY;
4531 }
4532
4533 return 0;
4534}
4535
4536static void exclusive_event_destroy(struct perf_event *event)
4537{
4538 struct pmu *pmu = event->pmu;
4539
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004540 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004541 return;
4542
4543 /* see comment in exclusive_event_init() */
4544 if (event->attach_state & PERF_ATTACH_TASK)
4545 atomic_dec(&pmu->exclusive_cnt);
4546 else
4547 atomic_inc(&pmu->exclusive_cnt);
4548}
4549
4550static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4551{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004552 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004553 (e1->cpu == e2->cpu ||
4554 e1->cpu == -1 ||
4555 e2->cpu == -1))
4556 return true;
4557 return false;
4558}
4559
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004560static bool exclusive_event_installable(struct perf_event *event,
4561 struct perf_event_context *ctx)
4562{
4563 struct perf_event *iter_event;
4564 struct pmu *pmu = event->pmu;
4565
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004566 lockdep_assert_held(&ctx->mutex);
4567
4568 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004569 return true;
4570
4571 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4572 if (exclusive_event_match(iter_event, event))
4573 return false;
4574 }
4575
4576 return true;
4577}
4578
Alexander Shishkin375637b2016-04-27 18:44:46 +03004579static void perf_addr_filters_splice(struct perf_event *event,
4580 struct list_head *head);
4581
Peter Zijlstra683ede42014-05-05 12:11:24 +02004582static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004583{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004584 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004585
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004586 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004587
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004588 security_perf_event_free(event);
4589
Frederic Weisbecker76369132011-05-19 19:55:04 +02004590 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004591 /*
4592 * Can happen when we close an event with re-directed output.
4593 *
4594 * Since we have a 0 refcount, perf_mmap_close() will skip
4595 * over us; possibly making our ring_buffer_put() the last.
4596 */
4597 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004598 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004599 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004600 }
4601
Stephane Eraniane5d13672011-02-14 11:20:01 +02004602 if (is_cgroup_event(event))
4603 perf_detach_cgroup(event);
4604
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004605 if (!event->parent) {
4606 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4607 put_callchain_buffers();
4608 }
4609
4610 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004611 perf_addr_filters_splice(event, NULL);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02004612 kfree(event->addr_filter_ranges);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004613
4614 if (event->destroy)
4615 event->destroy(event);
4616
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004617 /*
4618 * Must be after ->destroy(), due to uprobe_perf_close() using
4619 * hw.target.
4620 */
Prashant Bhole621b6d22018-04-09 19:03:46 +09004621 if (event->hw.target)
4622 put_task_struct(event->hw.target);
4623
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004624 /*
4625 * perf_event_free_task() relies on put_ctx() being 'last', in particular
4626 * all task references must be cleaned up.
4627 */
4628 if (event->ctx)
4629 put_ctx(event->ctx);
4630
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004631 exclusive_event_destroy(event);
4632 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004633
4634 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004635}
4636
Peter Zijlstra683ede42014-05-05 12:11:24 +02004637/*
4638 * Used to free events which have a known refcount of 1, such as in error paths
4639 * where the event isn't exposed yet and inherited events.
4640 */
4641static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004642{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004643 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4644 "unexpected event refcount: %ld; ptr=%p\n",
4645 atomic_long_read(&event->refcount), event)) {
4646 /* leak to avoid use-after-free */
4647 return;
4648 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004649
Peter Zijlstra683ede42014-05-05 12:11:24 +02004650 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004651}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004652
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004653/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004654 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004655 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004656static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004657{
Peter Zijlstra88821352010-11-09 19:01:43 +01004658 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004659
Peter Zijlstra88821352010-11-09 19:01:43 +01004660 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004661 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004662 * Matches the smp_store_release() in perf_event_exit_task(). If we
4663 * observe !owner it means the list deletion is complete and we can
4664 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004665 * owner->perf_event_mutex.
4666 */
Will Deacon506458e2017-10-24 11:22:48 +01004667 owner = READ_ONCE(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004668 if (owner) {
4669 /*
4670 * Since delayed_put_task_struct() also drops the last
4671 * task reference we can safely take a new reference
4672 * while holding the rcu_read_lock().
4673 */
4674 get_task_struct(owner);
4675 }
4676 rcu_read_unlock();
4677
4678 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004679 /*
4680 * If we're here through perf_event_exit_task() we're already
4681 * holding ctx->mutex which would be an inversion wrt. the
4682 * normal lock order.
4683 *
4684 * However we can safely take this lock because its the child
4685 * ctx->mutex.
4686 */
4687 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4688
Peter Zijlstra88821352010-11-09 19:01:43 +01004689 /*
4690 * We have to re-check the event->owner field, if it is cleared
4691 * we raced with perf_event_exit_task(), acquiring the mutex
4692 * ensured they're done, and we can proceed with freeing the
4693 * event.
4694 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004695 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004696 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004697 smp_store_release(&event->owner, NULL);
4698 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004699 mutex_unlock(&owner->perf_event_mutex);
4700 put_task_struct(owner);
4701 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004702}
4703
Jiri Olsaf8697762014-08-01 14:33:01 +02004704static void put_event(struct perf_event *event)
4705{
Jiri Olsaf8697762014-08-01 14:33:01 +02004706 if (!atomic_long_dec_and_test(&event->refcount))
4707 return;
4708
Peter Zijlstra683ede42014-05-05 12:11:24 +02004709 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004710}
4711
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004712/*
4713 * Kill an event dead; while event:refcount will preserve the event
4714 * object, it will not preserve its functionality. Once the last 'user'
4715 * gives up the object, we'll destroy the thing.
4716 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004717int perf_event_release_kernel(struct perf_event *event)
4718{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004719 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004720 struct perf_event *child, *tmp;
Peter Zijlstra82d94852018-01-09 13:10:30 +01004721 LIST_HEAD(free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004722
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004723 /*
4724 * If we got here through err_file: fput(event_file); we will not have
4725 * attached to a context yet.
4726 */
4727 if (!ctx) {
4728 WARN_ON_ONCE(event->attach_state &
4729 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4730 goto no_ctx;
4731 }
4732
Peter Zijlstra88821352010-11-09 19:01:43 +01004733 if (!is_kernel_event(event))
4734 perf_remove_from_owner(event);
4735
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004736 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004737 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004738 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004739
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004740 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004741 /*
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +01004742 * Mark this event as STATE_DEAD, there is no external reference to it
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004743 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004744 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004745 * Anybody acquiring event->child_mutex after the below loop _must_
4746 * also see this, most importantly inherit_event() which will avoid
4747 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004748 *
4749 * Thus this guarantees that we will in fact observe and kill _ALL_
4750 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004751 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004752 event->state = PERF_EVENT_STATE_DEAD;
4753 raw_spin_unlock_irq(&ctx->lock);
4754
4755 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004756
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004757again:
4758 mutex_lock(&event->child_mutex);
4759 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004760
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004761 /*
4762 * Cannot change, child events are not migrated, see the
4763 * comment with perf_event_ctx_lock_nested().
4764 */
Will Deacon506458e2017-10-24 11:22:48 +01004765 ctx = READ_ONCE(child->ctx);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004766 /*
4767 * Since child_mutex nests inside ctx::mutex, we must jump
4768 * through hoops. We start by grabbing a reference on the ctx.
4769 *
4770 * Since the event cannot get freed while we hold the
4771 * child_mutex, the context must also exist and have a !0
4772 * reference count.
4773 */
4774 get_ctx(ctx);
4775
4776 /*
4777 * Now that we have a ctx ref, we can drop child_mutex, and
4778 * acquire ctx::mutex without fear of it going away. Then we
4779 * can re-acquire child_mutex.
4780 */
4781 mutex_unlock(&event->child_mutex);
4782 mutex_lock(&ctx->mutex);
4783 mutex_lock(&event->child_mutex);
4784
4785 /*
4786 * Now that we hold ctx::mutex and child_mutex, revalidate our
4787 * state, if child is still the first entry, it didn't get freed
4788 * and we can continue doing so.
4789 */
4790 tmp = list_first_entry_or_null(&event->child_list,
4791 struct perf_event, child_list);
4792 if (tmp == child) {
4793 perf_remove_from_context(child, DETACH_GROUP);
Peter Zijlstra82d94852018-01-09 13:10:30 +01004794 list_move(&child->child_list, &free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004795 /*
4796 * This matches the refcount bump in inherit_event();
4797 * this can't be the last reference.
4798 */
4799 put_event(event);
4800 }
4801
4802 mutex_unlock(&event->child_mutex);
4803 mutex_unlock(&ctx->mutex);
4804 put_ctx(ctx);
4805 goto again;
4806 }
4807 mutex_unlock(&event->child_mutex);
4808
Peter Zijlstra82d94852018-01-09 13:10:30 +01004809 list_for_each_entry_safe(child, tmp, &free_list, child_list) {
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004810 void *var = &child->ctx->refcount;
4811
Peter Zijlstra82d94852018-01-09 13:10:30 +01004812 list_del(&child->child_list);
4813 free_event(child);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004814
4815 /*
4816 * Wake any perf_event_free_task() waiting for this event to be
4817 * freed.
4818 */
4819 smp_mb(); /* pairs with wait_var_event() */
4820 wake_up_var(var);
Peter Zijlstra82d94852018-01-09 13:10:30 +01004821 }
4822
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004823no_ctx:
4824 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004825 return 0;
4826}
4827EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4828
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02004829/*
4830 * Called when the last reference to the file is gone.
4831 */
Al Viroa6fa9412012-08-20 14:59:25 +01004832static int perf_release(struct inode *inode, struct file *file)
4833{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004834 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01004835 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004836}
4837
Peter Zijlstraca0dd442017-09-05 13:23:44 +02004838static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004839{
4840 struct perf_event *child;
4841 u64 total = 0;
4842
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004843 *enabled = 0;
4844 *running = 0;
4845
Peter Zijlstra6f105812009-11-20 22:19:56 +01004846 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004847
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004848 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004849 total += perf_event_count(event);
4850
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004851 *enabled += event->total_time_enabled +
4852 atomic64_read(&event->child_total_time_enabled);
4853 *running += event->total_time_running +
4854 atomic64_read(&event->child_total_time_running);
4855
4856 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004857 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07004858 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004859 *enabled += child->total_time_enabled;
4860 *running += child->total_time_running;
4861 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01004862 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863
4864 return total;
4865}
Peter Zijlstraca0dd442017-09-05 13:23:44 +02004866
4867u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
4868{
4869 struct perf_event_context *ctx;
4870 u64 count;
4871
4872 ctx = perf_event_ctx_lock(event);
4873 count = __perf_event_read_value(event, enabled, running);
4874 perf_event_ctx_unlock(event, ctx);
4875
4876 return count;
4877}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004878EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004879
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004880static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004881 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882{
Jiri Olsa2aeb1882017-07-20 16:14:55 +02004883 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004884 struct perf_event *sub;
Jiri Olsa2aeb1882017-07-20 16:14:55 +02004885 unsigned long flags;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004886 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004887 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01004888
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004889 ret = perf_event_read(leader, true);
4890 if (ret)
4891 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004892
Peter Zijlstraa9cd8192017-09-05 13:38:24 +02004893 raw_spin_lock_irqsave(&ctx->lock, flags);
4894
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004895 /*
4896 * Since we co-schedule groups, {enabled,running} times of siblings
4897 * will be identical to those of the leader, so we only publish one
4898 * set.
4899 */
4900 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4901 values[n++] += leader->total_time_enabled +
4902 atomic64_read(&leader->child_total_time_enabled);
4903 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004904
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004905 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4906 values[n++] += leader->total_time_running +
4907 atomic64_read(&leader->child_total_time_running);
4908 }
4909
4910 /*
4911 * Write {count,id} tuples for every sibling.
4912 */
4913 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004914 if (read_format & PERF_FORMAT_ID)
4915 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004916
Peter Zijlstraedb39592018-03-15 17:36:56 +01004917 for_each_sibling_event(sub, leader) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004918 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004919 if (read_format & PERF_FORMAT_ID)
4920 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004921 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004922
Jiri Olsa2aeb1882017-07-20 16:14:55 +02004923 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004924 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004925}
4926
4927static int perf_read_group(struct perf_event *event,
4928 u64 read_format, char __user *buf)
4929{
4930 struct perf_event *leader = event->group_leader, *child;
4931 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004932 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004933 u64 *values;
4934
4935 lockdep_assert_held(&ctx->mutex);
4936
4937 values = kzalloc(event->read_size, GFP_KERNEL);
4938 if (!values)
4939 return -ENOMEM;
4940
4941 values[0] = 1 + leader->nr_siblings;
4942
4943 /*
4944 * By locking the child_mutex of the leader we effectively
4945 * lock the child list of all siblings.. XXX explain how.
4946 */
4947 mutex_lock(&leader->child_mutex);
4948
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004949 ret = __perf_read_group_add(leader, read_format, values);
4950 if (ret)
4951 goto unlock;
4952
4953 list_for_each_entry(child, &leader->child_list, child_list) {
4954 ret = __perf_read_group_add(child, read_format, values);
4955 if (ret)
4956 goto unlock;
4957 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004958
4959 mutex_unlock(&leader->child_mutex);
4960
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004961 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004962 if (copy_to_user(buf, values, event->read_size))
4963 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004964 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004965
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004966unlock:
4967 mutex_unlock(&leader->child_mutex);
4968out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07004969 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01004970 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004971}
4972
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07004973static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004974 u64 read_format, char __user *buf)
4975{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004976 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004977 u64 values[4];
4978 int n = 0;
4979
Peter Zijlstraca0dd442017-09-05 13:23:44 +02004980 values[n++] = __perf_event_read_value(event, &enabled, &running);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01004981 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4982 values[n++] = enabled;
4983 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4984 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004985 if (read_format & PERF_FORMAT_ID)
4986 values[n++] = primary_event_id(event);
4987
4988 if (copy_to_user(buf, values, n * sizeof(u64)))
4989 return -EFAULT;
4990
4991 return n * sizeof(u64);
4992}
4993
Jiri Olsadc633982014-09-12 13:18:26 +02004994static bool is_event_hup(struct perf_event *event)
4995{
4996 bool no_children;
4997
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004998 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02004999 return false;
5000
5001 mutex_lock(&event->child_mutex);
5002 no_children = list_empty(&event->child_list);
5003 mutex_unlock(&event->child_mutex);
5004 return no_children;
5005}
5006
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007/*
5008 * Read the performance event - simple non blocking version for now
5009 */
5010static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005011__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012{
5013 u64 read_format = event->attr.read_format;
5014 int ret;
5015
5016 /*
Tobias Tefke788faab2018-07-09 12:57:15 +02005017 * Return end-of-file for a read on an event that is in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005018 * error state (i.e. because it was pinned but it couldn't be
5019 * scheduled on to the CPU at some point).
5020 */
5021 if (event->state == PERF_EVENT_STATE_ERROR)
5022 return 0;
5023
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005024 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025 return -ENOSPC;
5026
5027 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005028 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005029 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005030 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005031 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005032
5033 return ret;
5034}
5035
5036static ssize_t
5037perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
5038{
5039 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005040 struct perf_event_context *ctx;
5041 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005042
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005043 ret = security_perf_event_read(event);
5044 if (ret)
5045 return ret;
5046
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005047 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005048 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005049 perf_event_ctx_unlock(event, ctx);
5050
5051 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052}
5053
Al Viro9dd95742017-07-03 00:42:43 -04005054static __poll_t perf_poll(struct file *file, poll_table *wait)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055{
5056 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02005057 struct ring_buffer *rb;
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005058 __poll_t events = EPOLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005059
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02005060 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04005061
Jiri Olsadc633982014-09-12 13:18:26 +02005062 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04005063 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005064
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005065 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005066 * Pin the event->rb by taking event->mmap_mutex; otherwise
5067 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005068 */
5069 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005070 rb = event->rb;
5071 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005072 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005073 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005074 return events;
5075}
5076
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005077static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005079 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02005080 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005081 perf_event_update_userpage(event);
5082}
5083
Like Xu52ba4b02019-10-27 18:52:39 +08005084/* Assume it's not an event with inherit set. */
5085u64 perf_event_pause(struct perf_event *event, bool reset)
5086{
5087 struct perf_event_context *ctx;
5088 u64 count;
5089
5090 ctx = perf_event_ctx_lock(event);
5091 WARN_ON_ONCE(event->attr.inherit);
5092 _perf_event_disable(event);
5093 count = local64_read(&event->count);
5094 if (reset)
5095 local64_set(&event->count, 0);
5096 perf_event_ctx_unlock(event, ctx);
5097
5098 return count;
5099}
5100EXPORT_SYMBOL_GPL(perf_event_pause);
5101
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005102/*
5103 * Holding the top-level event's child_mutex means that any
5104 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01005105 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005106 * task existence requirements of perf_event_enable/disable.
5107 */
5108static void perf_event_for_each_child(struct perf_event *event,
5109 void (*func)(struct perf_event *))
5110{
5111 struct perf_event *child;
5112
5113 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115 mutex_lock(&event->child_mutex);
5116 func(event);
5117 list_for_each_entry(child, &event->child_list, child_list)
5118 func(child);
5119 mutex_unlock(&event->child_mutex);
5120}
5121
5122static void perf_event_for_each(struct perf_event *event,
5123 void (*func)(struct perf_event *))
5124{
5125 struct perf_event_context *ctx = event->ctx;
5126 struct perf_event *sibling;
5127
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005128 lockdep_assert_held(&ctx->mutex);
5129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005130 event = event->group_leader;
5131
5132 perf_event_for_each_child(event, func);
Peter Zijlstraedb39592018-03-15 17:36:56 +01005133 for_each_sibling_event(sibling, event)
Michael Ellerman724b6da2012-04-11 11:54:13 +10005134 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005135}
5136
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005137static void __perf_event_period(struct perf_event *event,
5138 struct perf_cpu_context *cpuctx,
5139 struct perf_event_context *ctx,
5140 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01005141{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005142 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005143 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005144
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005145 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005146 event->attr.sample_freq = value;
5147 } else {
5148 event->attr.sample_period = value;
5149 event->hw.sample_period = value;
5150 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005151
5152 active = (event->state == PERF_EVENT_STATE_ACTIVE);
5153 if (active) {
5154 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01005155 /*
5156 * We could be throttled; unthrottle now to avoid the tick
5157 * trying to unthrottle while we already re-started the event.
5158 */
5159 if (event->hw.interrupts == MAX_INTERRUPTS) {
5160 event->hw.interrupts = 0;
5161 perf_log_throttle(event, 1);
5162 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005163 event->pmu->stop(event, PERF_EF_UPDATE);
5164 }
5165
5166 local64_set(&event->hw.period_left, 0);
5167
5168 if (active) {
5169 event->pmu->start(event, PERF_EF_RELOAD);
5170 perf_pmu_enable(ctx->pmu);
5171 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005172}
5173
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005174static int perf_event_check_period(struct perf_event *event, u64 value)
5175{
5176 return event->pmu->check_period(event, value);
5177}
5178
Like Xu3ca270f2019-10-27 18:52:38 +08005179static int _perf_event_period(struct perf_event *event, u64 value)
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005180{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005181 if (!is_sampling_event(event))
5182 return -EINVAL;
5183
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005184 if (!value)
5185 return -EINVAL;
5186
5187 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
5188 return -EINVAL;
5189
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005190 if (perf_event_check_period(event, value))
5191 return -EINVAL;
5192
Ravi Bangoria913a90b2019-06-04 09:59:53 +05305193 if (!event->attr.freq && (value & (1ULL << 63)))
5194 return -EINVAL;
5195
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005196 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005197
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005198 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005199}
5200
Like Xu3ca270f2019-10-27 18:52:38 +08005201int perf_event_period(struct perf_event *event, u64 value)
5202{
5203 struct perf_event_context *ctx;
5204 int ret;
5205
5206 ctx = perf_event_ctx_lock(event);
5207 ret = _perf_event_period(event, value);
5208 perf_event_ctx_unlock(event, ctx);
5209
5210 return ret;
5211}
5212EXPORT_SYMBOL_GPL(perf_event_period);
5213
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005214static const struct file_operations perf_fops;
5215
Al Viro2903ff02012-08-28 12:52:22 -04005216static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005217{
Al Viro2903ff02012-08-28 12:52:22 -04005218 struct fd f = fdget(fd);
5219 if (!f.file)
5220 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005221
Al Viro2903ff02012-08-28 12:52:22 -04005222 if (f.file->f_op != &perf_fops) {
5223 fdput(f);
5224 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005225 }
Al Viro2903ff02012-08-28 12:52:22 -04005226 *p = f;
5227 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005228}
5229
5230static int perf_event_set_output(struct perf_event *event,
5231 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08005232static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07005233static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005234static int perf_copy_attr(struct perf_event_attr __user *uattr,
5235 struct perf_event_attr *attr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005236
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005237static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005238{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239 void (*func)(struct perf_event *);
5240 u32 flags = arg;
5241
5242 switch (cmd) {
5243 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005244 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 break;
5246 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005247 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005248 break;
5249 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005250 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005251 break;
5252
5253 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005254 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005255
5256 case PERF_EVENT_IOC_PERIOD:
Like Xu3ca270f2019-10-27 18:52:38 +08005257 {
5258 u64 value;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259
Like Xu3ca270f2019-10-27 18:52:38 +08005260 if (copy_from_user(&value, (u64 __user *)arg, sizeof(value)))
5261 return -EFAULT;
5262
5263 return _perf_event_period(event, value);
5264 }
Jiri Olsacf4957f2012-10-24 13:37:58 +02005265 case PERF_EVENT_IOC_ID:
5266 {
5267 u64 id = primary_event_id(event);
5268
5269 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
5270 return -EFAULT;
5271 return 0;
5272 }
5273
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005274 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005275 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005276 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005277 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04005278 struct perf_event *output_event;
5279 struct fd output;
5280 ret = perf_fget_light(arg, &output);
5281 if (ret)
5282 return ret;
5283 output_event = output.file->private_data;
5284 ret = perf_event_set_output(event, output_event);
5285 fdput(output);
5286 } else {
5287 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005288 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005289 return ret;
5290 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291
Li Zefan6fb29152009-10-15 11:21:42 +08005292 case PERF_EVENT_IOC_SET_FILTER:
5293 return perf_event_set_filter(event, (void __user *)arg);
5294
Alexei Starovoitov25415172015-03-25 12:49:20 -07005295 case PERF_EVENT_IOC_SET_BPF:
5296 return perf_event_set_bpf_prog(event, arg);
5297
Wang Nan86e79722016-03-28 06:41:29 +00005298 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
5299 struct ring_buffer *rb;
5300
5301 rcu_read_lock();
5302 rb = rcu_dereference(event->rb);
5303 if (!rb || !rb->nr_pages) {
5304 rcu_read_unlock();
5305 return -EINVAL;
5306 }
5307 rb_toggle_paused(rb, !!arg);
5308 rcu_read_unlock();
5309 return 0;
5310 }
Yonghong Songf371b302017-12-11 11:39:02 -08005311
5312 case PERF_EVENT_IOC_QUERY_BPF:
Yonghong Songf4e22982017-12-13 10:35:37 -08005313 return perf_event_query_prog_array(event, (void __user *)arg);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005314
5315 case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
5316 struct perf_event_attr new_attr;
5317 int err = perf_copy_attr((struct perf_event_attr __user *)arg,
5318 &new_attr);
5319
5320 if (err)
5321 return err;
5322
5323 return perf_event_modify_attr(event, &new_attr);
5324 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005325 default:
5326 return -ENOTTY;
5327 }
5328
5329 if (flags & PERF_IOC_FLAG_GROUP)
5330 perf_event_for_each(event, func);
5331 else
5332 perf_event_for_each_child(event, func);
5333
5334 return 0;
5335}
5336
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005337static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5338{
5339 struct perf_event *event = file->private_data;
5340 struct perf_event_context *ctx;
5341 long ret;
5342
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005343 /* Treat ioctl like writes as it is likely a mutating operation. */
5344 ret = security_perf_event_write(event);
5345 if (ret)
5346 return ret;
5347
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005348 ctx = perf_event_ctx_lock(event);
5349 ret = _perf_ioctl(event, cmd, arg);
5350 perf_event_ctx_unlock(event, ctx);
5351
5352 return ret;
5353}
5354
Pawel Mollb3f20782014-06-13 16:03:32 +01005355#ifdef CONFIG_COMPAT
5356static long perf_compat_ioctl(struct file *file, unsigned int cmd,
5357 unsigned long arg)
5358{
5359 switch (_IOC_NR(cmd)) {
5360 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
5361 case _IOC_NR(PERF_EVENT_IOC_ID):
Eugene Syromiatnikov82489c52018-05-21 14:34:20 +02005362 case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF):
5363 case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES):
Pawel Mollb3f20782014-06-13 16:03:32 +01005364 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
5365 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
5366 cmd &= ~IOCSIZE_MASK;
5367 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
5368 }
5369 break;
5370 }
5371 return perf_ioctl(file, cmd, arg);
5372}
5373#else
5374# define perf_compat_ioctl NULL
5375#endif
5376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377int perf_event_task_enable(void)
5378{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005379 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005380 struct perf_event *event;
5381
5382 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005383 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5384 ctx = perf_event_ctx_lock(event);
5385 perf_event_for_each_child(event, _perf_event_enable);
5386 perf_event_ctx_unlock(event, ctx);
5387 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 mutex_unlock(&current->perf_event_mutex);
5389
5390 return 0;
5391}
5392
5393int perf_event_task_disable(void)
5394{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005395 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396 struct perf_event *event;
5397
5398 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005399 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5400 ctx = perf_event_ctx_lock(event);
5401 perf_event_for_each_child(event, _perf_event_disable);
5402 perf_event_ctx_unlock(event, ctx);
5403 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005404 mutex_unlock(&current->perf_event_mutex);
5405
5406 return 0;
5407}
5408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005409static int perf_event_index(struct perf_event *event)
5410{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005411 if (event->hw.state & PERF_HES_STOPPED)
5412 return 0;
5413
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005414 if (event->state != PERF_EVENT_STATE_ACTIVE)
5415 return 0;
5416
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005417 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005418}
5419
Eric B Munsonc4794292011-06-23 16:34:38 -04005420static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005421 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04005422 u64 *enabled,
5423 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04005424{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005425 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04005426
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005427 *now = perf_clock();
5428 ctx_time = event->shadow_ctx_time + *now;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02005429 __perf_update_times(event, ctx_time, enabled, running);
Eric B Munsonc4794292011-06-23 16:34:38 -04005430}
5431
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005432static void perf_event_init_userpage(struct perf_event *event)
5433{
5434 struct perf_event_mmap_page *userpg;
5435 struct ring_buffer *rb;
5436
5437 rcu_read_lock();
5438 rb = rcu_dereference(event->rb);
5439 if (!rb)
5440 goto unlock;
5441
5442 userpg = rb->user_page;
5443
5444 /* Allow new userspace to detect that bit 0 is deprecated */
5445 userpg->cap_bit0_is_deprecated = 1;
5446 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02005447 userpg->data_offset = PAGE_SIZE;
5448 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005449
5450unlock:
5451 rcu_read_unlock();
5452}
5453
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005454void __weak arch_perf_update_userpage(
5455 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005456{
5457}
5458
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005459/*
5460 * Callers need to ensure there can be no nesting of this function, otherwise
5461 * the seqlock logic goes bad. We can not serialize this because the arch
5462 * code calls this from NMI context.
5463 */
5464void perf_event_update_userpage(struct perf_event *event)
5465{
5466 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02005467 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005468 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469
5470 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02005471 rb = rcu_dereference(event->rb);
5472 if (!rb)
5473 goto unlock;
5474
Eric B Munson0d641202011-06-24 12:26:26 -04005475 /*
5476 * compute total_time_enabled, total_time_running
5477 * based on snapshot values taken when the event
5478 * was last scheduled in.
5479 *
5480 * we cannot simply called update_context_time()
5481 * because of locking issue as we can be called in
5482 * NMI context
5483 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005484 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005485
Frederic Weisbecker76369132011-05-19 19:55:04 +02005486 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005487 /*
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07005488 * Disable preemption to guarantee consistent time stamps are stored to
5489 * the user page.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005490 */
5491 preempt_disable();
5492 ++userpg->lock;
5493 barrier();
5494 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005495 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01005496 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02005497 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005498
Eric B Munson0d641202011-06-24 12:26:26 -04005499 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500 atomic64_read(&event->child_total_time_enabled);
5501
Eric B Munson0d641202011-06-24 12:26:26 -04005502 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005503 atomic64_read(&event->child_total_time_running);
5504
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005505 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507 barrier();
5508 ++userpg->lock;
5509 preempt_enable();
5510unlock:
5511 rcu_read_unlock();
5512}
Suzuki K Poulose82975c42018-01-02 11:25:26 +00005513EXPORT_SYMBOL_GPL(perf_event_update_userpage);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005514
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305515static vm_fault_t perf_mmap_fault(struct vm_fault *vmf)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005516{
Dave Jiang11bac802017-02-24 14:56:41 -08005517 struct perf_event *event = vmf->vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02005518 struct ring_buffer *rb;
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305519 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005520
5521 if (vmf->flags & FAULT_FLAG_MKWRITE) {
5522 if (vmf->pgoff == 0)
5523 ret = 0;
5524 return ret;
5525 }
5526
5527 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005528 rb = rcu_dereference(event->rb);
5529 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005530 goto unlock;
5531
5532 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
5533 goto unlock;
5534
Frederic Weisbecker76369132011-05-19 19:55:04 +02005535 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005536 if (!vmf->page)
5537 goto unlock;
5538
5539 get_page(vmf->page);
Dave Jiang11bac802017-02-24 14:56:41 -08005540 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005541 vmf->page->index = vmf->pgoff;
5542
5543 ret = 0;
5544unlock:
5545 rcu_read_unlock();
5546
5547 return ret;
5548}
5549
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005550static void ring_buffer_attach(struct perf_event *event,
5551 struct ring_buffer *rb)
5552{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005553 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005554 unsigned long flags;
5555
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005556 if (event->rb) {
5557 /*
5558 * Should be impossible, we set this when removing
5559 * event->rb_entry and wait/clear when adding event->rb_entry.
5560 */
5561 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005562
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005563 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005564 spin_lock_irqsave(&old_rb->event_lock, flags);
5565 list_del_rcu(&event->rb_entry);
5566 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005567
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005568 event->rcu_batches = get_state_synchronize_rcu();
5569 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005570 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005571
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005572 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005573 if (event->rcu_pending) {
5574 cond_synchronize_rcu(event->rcu_batches);
5575 event->rcu_pending = 0;
5576 }
5577
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005578 spin_lock_irqsave(&rb->event_lock, flags);
5579 list_add_rcu(&event->rb_entry, &rb->event_list);
5580 spin_unlock_irqrestore(&rb->event_lock, flags);
5581 }
5582
Alexander Shishkin767ae082016-09-06 16:23:49 +03005583 /*
5584 * Avoid racing with perf_mmap_close(AUX): stop the event
5585 * before swizzling the event::rb pointer; if it's getting
5586 * unmapped, its aux_mmap_count will be 0 and it won't
5587 * restart. See the comment in __perf_pmu_output_stop().
5588 *
5589 * Data will inevitably be lost when set_output is done in
5590 * mid-air, but then again, whoever does it like this is
5591 * not in for the data anyway.
5592 */
5593 if (has_aux(event))
5594 perf_event_stop(event, 0);
5595
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005596 rcu_assign_pointer(event->rb, rb);
5597
5598 if (old_rb) {
5599 ring_buffer_put(old_rb);
5600 /*
5601 * Since we detached before setting the new rb, so that we
5602 * could attach the new rb, we could have missed a wakeup.
5603 * Provide it now.
5604 */
5605 wake_up_all(&event->waitq);
5606 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005607}
5608
5609static void ring_buffer_wakeup(struct perf_event *event)
5610{
5611 struct ring_buffer *rb;
5612
5613 rcu_read_lock();
5614 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005615 if (rb) {
5616 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
5617 wake_up_all(&event->waitq);
5618 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005619 rcu_read_unlock();
5620}
5621
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005622struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005623{
Frederic Weisbecker76369132011-05-19 19:55:04 +02005624 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005625
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005626 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005627 rb = rcu_dereference(event->rb);
5628 if (rb) {
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005629 if (!refcount_inc_not_zero(&rb->refcount))
Frederic Weisbecker76369132011-05-19 19:55:04 +02005630 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005631 }
5632 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005633
Frederic Weisbecker76369132011-05-19 19:55:04 +02005634 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005635}
5636
Alexander Shishkinfdc26702015-01-14 14:18:16 +02005637void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005638{
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005639 if (!refcount_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005640 return;
5641
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005642 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005643
Frederic Weisbecker76369132011-05-19 19:55:04 +02005644 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645}
5646
5647static void perf_mmap_open(struct vm_area_struct *vma)
5648{
5649 struct perf_event *event = vma->vm_file->private_data;
5650
5651 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005652 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005653
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005654 if (vma->vm_pgoff)
5655 atomic_inc(&event->rb->aux_mmap_count);
5656
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005657 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005658 event->pmu->event_mapped(event, vma->vm_mm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005659}
5660
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005661static void perf_pmu_output_stop(struct perf_event *event);
5662
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005663/*
5664 * A buffer can be mmap()ed multiple times; either directly through the same
5665 * event, or through other events by use of perf_event_set_output().
5666 *
5667 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5668 * the buffer here, where we still have a VM context. This means we need
5669 * to detach all events redirecting to us.
5670 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005671static void perf_mmap_close(struct vm_area_struct *vma)
5672{
5673 struct perf_event *event = vma->vm_file->private_data;
5674
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005675 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005676 struct user_struct *mmap_user = rb->mmap_user;
5677 int mmap_locked = rb->mmap_locked;
5678 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005679
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005680 if (event->pmu->event_unmapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005681 event->pmu->event_unmapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005682
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005683 /*
5684 * rb->aux_mmap_count will always drop before rb->mmap_count and
5685 * event->mmap_count, so it is ok to use event->mmap_mutex to
5686 * serialize with perf_mmap here.
5687 */
5688 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5689 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005690 /*
5691 * Stop all AUX events that are writing to this buffer,
5692 * so that we can free its AUX pages and corresponding PMU
5693 * data. Note that after rb::aux_mmap_count dropped to zero,
5694 * they won't start any more (see perf_aux_output_begin()).
5695 */
5696 perf_pmu_output_stop(event);
5697
5698 /* now it's safe to free the pages */
Alexander Shishkin36b3db02019-11-15 18:08:18 +02005699 atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm);
5700 atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005701
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005702 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005703 rb_free_aux(rb);
Elena Reshetovaca3bb3d2019-01-28 14:27:28 +02005704 WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005705
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005706 mutex_unlock(&event->mmap_mutex);
5707 }
5708
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005709 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005710
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005711 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005712 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005713
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005714 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005715 mutex_unlock(&event->mmap_mutex);
5716
5717 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005718 if (atomic_read(&rb->mmap_count))
5719 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005720
5721 /*
5722 * No other mmap()s, detach from all other events that might redirect
5723 * into the now unreachable buffer. Somewhat complicated by the
5724 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5725 */
5726again:
5727 rcu_read_lock();
5728 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5729 if (!atomic_long_inc_not_zero(&event->refcount)) {
5730 /*
5731 * This event is en-route to free_event() which will
5732 * detach it and remove it from the list.
5733 */
5734 continue;
5735 }
5736 rcu_read_unlock();
5737
5738 mutex_lock(&event->mmap_mutex);
5739 /*
5740 * Check we didn't race with perf_event_set_output() which can
5741 * swizzle the rb from under us while we were waiting to
5742 * acquire mmap_mutex.
5743 *
5744 * If we find a different rb; ignore this event, a next
5745 * iteration will no longer find it on the list. We have to
5746 * still restart the iteration to make sure we're not now
5747 * iterating the wrong list.
5748 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005749 if (event->rb == rb)
5750 ring_buffer_attach(event, NULL);
5751
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005752 mutex_unlock(&event->mmap_mutex);
5753 put_event(event);
5754
5755 /*
5756 * Restart the iteration; either we're on the wrong list or
5757 * destroyed its integrity by doing a deletion.
5758 */
5759 goto again;
5760 }
5761 rcu_read_unlock();
5762
5763 /*
5764 * It could be there's still a few 0-ref events on the list; they'll
5765 * get cleaned up by free_event() -- they'll also still have their
5766 * ref on the rb and will free it whenever they are done with it.
5767 *
5768 * Aside from that, this buffer is 'fully' detached and unmapped,
5769 * undo the VM accounting.
5770 */
5771
Song Liud44248a2019-09-04 14:46:18 -07005772 atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
5773 &mmap_user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08005774 atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005775 free_uid(mmap_user);
5776
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005777out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005778 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005779}
5780
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04005781static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005782 .open = perf_mmap_open,
Ingo Molnarfca0c112018-12-03 10:52:21 +01005783 .close = perf_mmap_close, /* non mergeable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005784 .fault = perf_mmap_fault,
5785 .page_mkwrite = perf_mmap_fault,
5786};
5787
5788static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5789{
5790 struct perf_event *event = file->private_data;
5791 unsigned long user_locked, user_lock_limit;
5792 struct user_struct *user = current_user();
5793 unsigned long locked, lock_limit;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005794 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795 unsigned long vma_size;
5796 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005797 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005798 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005799
Peter Zijlstrac7920612010-05-18 10:33:24 +02005800 /*
5801 * Don't allow mmap() of inherited per-task counters. This would
5802 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005803 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005804 */
5805 if (event->cpu == -1 && event->attr.inherit)
5806 return -EINVAL;
5807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005808 if (!(vma->vm_flags & VM_SHARED))
5809 return -EINVAL;
5810
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005811 ret = security_perf_event_read(event);
5812 if (ret)
5813 return ret;
5814
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005816
5817 if (vma->vm_pgoff == 0) {
5818 nr_pages = (vma_size / PAGE_SIZE) - 1;
5819 } else {
5820 /*
5821 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
5822 * mapped, all subsequent mappings should have the same size
5823 * and offset. Must be above the normal perf buffer.
5824 */
5825 u64 aux_offset, aux_size;
5826
5827 if (!event->rb)
5828 return -EINVAL;
5829
5830 nr_pages = vma_size / PAGE_SIZE;
5831
5832 mutex_lock(&event->mmap_mutex);
5833 ret = -EINVAL;
5834
5835 rb = event->rb;
5836 if (!rb)
5837 goto aux_unlock;
5838
Mark Rutland6aa7de02017-10-23 14:07:29 -07005839 aux_offset = READ_ONCE(rb->user_page->aux_offset);
5840 aux_size = READ_ONCE(rb->user_page->aux_size);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005841
5842 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
5843 goto aux_unlock;
5844
5845 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
5846 goto aux_unlock;
5847
5848 /* already mapped with a different offset */
5849 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
5850 goto aux_unlock;
5851
5852 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
5853 goto aux_unlock;
5854
5855 /* already mapped with a different size */
5856 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
5857 goto aux_unlock;
5858
5859 if (!is_power_of_2(nr_pages))
5860 goto aux_unlock;
5861
5862 if (!atomic_inc_not_zero(&rb->mmap_count))
5863 goto aux_unlock;
5864
5865 if (rb_has_aux(rb)) {
5866 atomic_inc(&rb->aux_mmap_count);
5867 ret = 0;
5868 goto unlock;
5869 }
5870
5871 atomic_set(&rb->aux_mmap_count, 1);
5872 user_extra = nr_pages;
5873
5874 goto accounting;
5875 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005876
5877 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005878 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005879 * can do bitmasks instead of modulo.
5880 */
Kan Liang2ed11312015-03-02 02:14:26 -05005881 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005882 return -EINVAL;
5883
5884 if (vma_size != PAGE_SIZE * (1 + nr_pages))
5885 return -EINVAL;
5886
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005887 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005888again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005889 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005890 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005891 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005892 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005893 goto unlock;
5894 }
5895
5896 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5897 /*
5898 * Raced against perf_mmap_close() through
5899 * perf_event_set_output(). Try again, hope for better
5900 * luck.
5901 */
5902 mutex_unlock(&event->mmap_mutex);
5903 goto again;
5904 }
5905
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005906 goto unlock;
5907 }
5908
5909 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005910
5911accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005912 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
5913
5914 /*
5915 * Increase the limit linearly with more CPUs:
5916 */
5917 user_lock_limit *= num_online_cpus();
5918
Song Liu00346152020-01-23 10:11:46 -08005919 user_locked = atomic_long_read(&user->locked_vm);
5920
5921 /*
5922 * sysctl_perf_event_mlock may have changed, so that
5923 * user->locked_vm > user_lock_limit
5924 */
5925 if (user_locked > user_lock_limit)
5926 user_locked = user_lock_limit;
5927 user_locked += user_extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928
Alexander Shishkinc4b75472019-11-20 19:06:40 +02005929 if (user_locked > user_lock_limit) {
Song Liud44248a2019-09-04 14:46:18 -07005930 /*
5931 * charge locked_vm until it hits user_lock_limit;
5932 * charge the rest from pinned_vm
5933 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005934 extra = user_locked - user_lock_limit;
Song Liud44248a2019-09-04 14:46:18 -07005935 user_extra -= extra;
5936 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005937
Jiri Slaby78d7d402010-03-05 13:42:54 -08005938 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005939 lock_limit >>= PAGE_SHIFT;
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08005940 locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005941
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005942 if ((locked > lock_limit) && perf_is_paranoid() &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005943 !capable(CAP_IPC_LOCK)) {
5944 ret = -EPERM;
5945 goto unlock;
5946 }
5947
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005948 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005949
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005950 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005951 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005952
Frederic Weisbecker76369132011-05-19 19:55:04 +02005953 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005954 rb = rb_alloc(nr_pages,
5955 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5956 event->cpu, flags);
5957
5958 if (!rb) {
5959 ret = -ENOMEM;
5960 goto unlock;
5961 }
5962
5963 atomic_set(&rb->mmap_count, 1);
5964 rb->mmap_user = get_current_user();
5965 rb->mmap_locked = extra;
5966
5967 ring_buffer_attach(event, rb);
5968
5969 perf_event_init_userpage(event);
5970 perf_event_update_userpage(event);
5971 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02005972 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5973 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005974 if (!ret)
5975 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005976 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005977
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005978unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005979 if (!ret) {
5980 atomic_long_add(user_extra, &user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08005981 atomic64_add(extra, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005982
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005983 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005984 } else if (rb) {
5985 atomic_dec(&rb->mmap_count);
5986 }
5987aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988 mutex_unlock(&event->mmap_mutex);
5989
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005990 /*
5991 * Since pinned accounting is per vm we cannot allow fork() to copy our
5992 * vma.
5993 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02005994 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005995 vma->vm_ops = &perf_mmap_vmops;
5996
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005997 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005998 event->pmu->event_mapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005999
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006000 return ret;
6001}
6002
6003static int perf_fasync(int fd, struct file *filp, int on)
6004{
Al Viro496ad9a2013-01-23 17:07:38 -05006005 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006006 struct perf_event *event = filp->private_data;
6007 int retval;
6008
Al Viro59551022016-01-22 15:40:57 -05006009 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05006011 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006012
6013 if (retval < 0)
6014 return retval;
6015
6016 return 0;
6017}
6018
6019static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01006020 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006021 .release = perf_release,
6022 .read = perf_read,
6023 .poll = perf_poll,
6024 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01006025 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026 .mmap = perf_mmap,
6027 .fasync = perf_fasync,
6028};
6029
6030/*
6031 * Perf event wakeup
6032 *
6033 * If there's data, ensure we set the poll() state and publish everything
6034 * to user-space before waking everybody up.
6035 */
6036
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006037static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
6038{
6039 /* only the parent has fasync state */
6040 if (event->parent)
6041 event = event->parent;
6042 return &event->fasync;
6043}
6044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006045void perf_event_wakeup(struct perf_event *event)
6046{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006047 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006048
6049 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006050 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051 event->pending_kill = 0;
6052 }
6053}
6054
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006055static void perf_pending_event_disable(struct perf_event *event)
6056{
6057 int cpu = READ_ONCE(event->pending_disable);
6058
6059 if (cpu < 0)
6060 return;
6061
6062 if (cpu == smp_processor_id()) {
6063 WRITE_ONCE(event->pending_disable, -1);
6064 perf_event_disable_local(event);
6065 return;
6066 }
6067
6068 /*
6069 * CPU-A CPU-B
6070 *
6071 * perf_event_disable_inatomic()
6072 * @pending_disable = CPU-A;
6073 * irq_work_queue();
6074 *
6075 * sched-out
6076 * @pending_disable = -1;
6077 *
6078 * sched-in
6079 * perf_event_disable_inatomic()
6080 * @pending_disable = CPU-B;
6081 * irq_work_queue(); // FAILS
6082 *
6083 * irq_work_run()
6084 * perf_pending_event()
6085 *
6086 * But the event runs on CPU-B and wants disabling there.
6087 */
6088 irq_work_queue_on(&event->pending, cpu);
6089}
6090
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006091static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006093 struct perf_event *event = container_of(entry, struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01006094 int rctx;
6095
6096 rctx = perf_swevent_get_recursion_context();
6097 /*
6098 * If we 'fail' here, that's OK, it means recursion is already disabled
6099 * and we won't recurse 'further'.
6100 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006101
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006102 perf_pending_event_disable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103
6104 if (event->pending_wakeup) {
6105 event->pending_wakeup = 0;
6106 perf_event_wakeup(event);
6107 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01006108
6109 if (rctx >= 0)
6110 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006111}
6112
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006113/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006114 * We assume there is only KVM supporting the callbacks.
6115 * Later on, we might change it to a list if there is
6116 * another virtualization implementation supporting the callbacks.
6117 */
6118struct perf_guest_info_callbacks *perf_guest_cbs;
6119
6120int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6121{
6122 perf_guest_cbs = cbs;
6123 return 0;
6124}
6125EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
6126
6127int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6128{
6129 perf_guest_cbs = NULL;
6130 return 0;
6131}
6132EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
6133
Jiri Olsa40189942012-08-07 15:20:37 +02006134static void
6135perf_output_sample_regs(struct perf_output_handle *handle,
6136 struct pt_regs *regs, u64 mask)
6137{
6138 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306139 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02006140
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306141 bitmap_from_u64(_mask, mask);
6142 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02006143 u64 val;
6144
6145 val = perf_reg_value(regs, bit);
6146 perf_output_put(handle, val);
6147 }
6148}
6149
Stephane Eranian60e23642014-09-24 13:48:37 +02006150static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006151 struct pt_regs *regs,
6152 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02006153{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006154 if (user_mode(regs)) {
6155 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02006156 regs_user->regs = regs;
Peter Zijlstra085ebfe2019-05-29 14:37:24 +02006157 } else if (!(current->flags & PF_KTHREAD)) {
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006158 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006159 } else {
6160 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
6161 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02006162 }
6163}
6164
Stephane Eranian60e23642014-09-24 13:48:37 +02006165static void perf_sample_regs_intr(struct perf_regs *regs_intr,
6166 struct pt_regs *regs)
6167{
6168 regs_intr->regs = regs;
6169 regs_intr->abi = perf_reg_abi(current);
6170}
6171
6172
Jiri Olsac5ebced2012-08-07 15:20:40 +02006173/*
6174 * Get remaining task size from user stack pointer.
6175 *
6176 * It'd be better to take stack vma map and limit this more
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03006177 * precisely, but there's no way to get it safely under interrupt,
Jiri Olsac5ebced2012-08-07 15:20:40 +02006178 * so using TASK_SIZE as limit.
6179 */
6180static u64 perf_ustack_task_size(struct pt_regs *regs)
6181{
6182 unsigned long addr = perf_user_stack_pointer(regs);
6183
6184 if (!addr || addr >= TASK_SIZE)
6185 return 0;
6186
6187 return TASK_SIZE - addr;
6188}
6189
6190static u16
6191perf_sample_ustack_size(u16 stack_size, u16 header_size,
6192 struct pt_regs *regs)
6193{
6194 u64 task_size;
6195
6196 /* No regs, no stack pointer, no dump. */
6197 if (!regs)
6198 return 0;
6199
6200 /*
6201 * Check if we fit in with the requested stack size into the:
6202 * - TASK_SIZE
6203 * If we don't, we limit the size to the TASK_SIZE.
6204 *
6205 * - remaining sample size
6206 * If we don't, we customize the stack size to
6207 * fit in to the remaining sample size.
6208 */
6209
6210 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
6211 stack_size = min(stack_size, (u16) task_size);
6212
6213 /* Current header size plus static size and dynamic size. */
6214 header_size += 2 * sizeof(u64);
6215
6216 /* Do we fit in with the current stack dump size? */
6217 if ((u16) (header_size + stack_size) < header_size) {
6218 /*
6219 * If we overflow the maximum size for the sample,
6220 * we customize the stack dump size to fit in.
6221 */
6222 stack_size = USHRT_MAX - header_size - sizeof(u64);
6223 stack_size = round_up(stack_size, sizeof(u64));
6224 }
6225
6226 return stack_size;
6227}
6228
6229static void
6230perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
6231 struct pt_regs *regs)
6232{
6233 /* Case of a kernel thread, nothing to dump */
6234 if (!regs) {
6235 u64 size = 0;
6236 perf_output_put(handle, size);
6237 } else {
6238 unsigned long sp;
6239 unsigned int rem;
6240 u64 dyn_size;
Yabin Cui02e18442018-08-23 15:59:35 -07006241 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02006242
6243 /*
6244 * We dump:
6245 * static size
6246 * - the size requested by user or the best one we can fit
6247 * in to the sample max size
6248 * data
6249 * - user stack dump data
6250 * dynamic size
6251 * - the actual dumped size
6252 */
6253
6254 /* Static size. */
6255 perf_output_put(handle, dump_size);
6256
6257 /* Data. */
6258 sp = perf_user_stack_pointer(regs);
Yabin Cui02e18442018-08-23 15:59:35 -07006259 fs = get_fs();
6260 set_fs(USER_DS);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006261 rem = __output_copy_user(handle, (void *) sp, dump_size);
Yabin Cui02e18442018-08-23 15:59:35 -07006262 set_fs(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006263 dyn_size = dump_size - rem;
6264
6265 perf_output_skip(handle, rem);
6266
6267 /* Dynamic size. */
6268 perf_output_put(handle, dyn_size);
6269 }
6270}
6271
Alexander Shishkina4faf002019-10-25 17:08:33 +03006272static unsigned long perf_prepare_sample_aux(struct perf_event *event,
6273 struct perf_sample_data *data,
6274 size_t size)
6275{
6276 struct perf_event *sampler = event->aux_event;
6277 struct ring_buffer *rb;
6278
6279 data->aux_size = 0;
6280
6281 if (!sampler)
6282 goto out;
6283
6284 if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE))
6285 goto out;
6286
6287 if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id()))
6288 goto out;
6289
6290 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6291 if (!rb)
6292 goto out;
6293
6294 /*
6295 * If this is an NMI hit inside sampling code, don't take
6296 * the sample. See also perf_aux_sample_output().
6297 */
6298 if (READ_ONCE(rb->aux_in_sampling)) {
6299 data->aux_size = 0;
6300 } else {
6301 size = min_t(size_t, size, perf_aux_size(rb));
6302 data->aux_size = ALIGN(size, sizeof(u64));
6303 }
6304 ring_buffer_put(rb);
6305
6306out:
6307 return data->aux_size;
6308}
6309
6310long perf_pmu_snapshot_aux(struct ring_buffer *rb,
6311 struct perf_event *event,
6312 struct perf_output_handle *handle,
6313 unsigned long size)
6314{
6315 unsigned long flags;
6316 long ret;
6317
6318 /*
6319 * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler
6320 * paths. If we start calling them in NMI context, they may race with
6321 * the IRQ ones, that is, for example, re-starting an event that's just
6322 * been stopped, which is why we're using a separate callback that
6323 * doesn't change the event state.
6324 *
6325 * IRQs need to be disabled to prevent IPIs from racing with us.
6326 */
6327 local_irq_save(flags);
6328 /*
6329 * Guard against NMI hits inside the critical section;
6330 * see also perf_prepare_sample_aux().
6331 */
6332 WRITE_ONCE(rb->aux_in_sampling, 1);
6333 barrier();
6334
6335 ret = event->pmu->snapshot_aux(event, handle, size);
6336
6337 barrier();
6338 WRITE_ONCE(rb->aux_in_sampling, 0);
6339 local_irq_restore(flags);
6340
6341 return ret;
6342}
6343
6344static void perf_aux_sample_output(struct perf_event *event,
6345 struct perf_output_handle *handle,
6346 struct perf_sample_data *data)
6347{
6348 struct perf_event *sampler = event->aux_event;
6349 unsigned long pad;
6350 struct ring_buffer *rb;
6351 long size;
6352
6353 if (WARN_ON_ONCE(!sampler || !data->aux_size))
6354 return;
6355
6356 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6357 if (!rb)
6358 return;
6359
6360 size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size);
6361
6362 /*
6363 * An error here means that perf_output_copy() failed (returned a
6364 * non-zero surplus that it didn't copy), which in its current
6365 * enlightened implementation is not possible. If that changes, we'd
6366 * like to know.
6367 */
6368 if (WARN_ON_ONCE(size < 0))
6369 goto out_put;
6370
6371 /*
6372 * The pad comes from ALIGN()ing data->aux_size up to u64 in
6373 * perf_prepare_sample_aux(), so should not be more than that.
6374 */
6375 pad = data->aux_size - size;
6376 if (WARN_ON_ONCE(pad >= sizeof(u64)))
6377 pad = 8;
6378
6379 if (pad) {
6380 u64 zero = 0;
6381 perf_output_copy(handle, &zero, pad);
6382 }
6383
6384out_put:
6385 ring_buffer_put(rb);
6386}
6387
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006388static void __perf_event_header__init_id(struct perf_event_header *header,
6389 struct perf_sample_data *data,
6390 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006391{
6392 u64 sample_type = event->attr.sample_type;
6393
6394 data->type = sample_type;
6395 header->size += event->id_header_size;
6396
6397 if (sample_type & PERF_SAMPLE_TID) {
6398 /* namespace issues */
6399 data->tid_entry.pid = perf_event_pid(event, current);
6400 data->tid_entry.tid = perf_event_tid(event, current);
6401 }
6402
6403 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01006404 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006405
Adrian Hunterff3d5272013-08-27 11:23:07 +03006406 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006407 data->id = primary_event_id(event);
6408
6409 if (sample_type & PERF_SAMPLE_STREAM_ID)
6410 data->stream_id = event->id;
6411
6412 if (sample_type & PERF_SAMPLE_CPU) {
6413 data->cpu_entry.cpu = raw_smp_processor_id();
6414 data->cpu_entry.reserved = 0;
6415 }
6416}
6417
Frederic Weisbecker76369132011-05-19 19:55:04 +02006418void perf_event_header__init_id(struct perf_event_header *header,
6419 struct perf_sample_data *data,
6420 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006421{
6422 if (event->attr.sample_id_all)
6423 __perf_event_header__init_id(header, data, event);
6424}
6425
6426static void __perf_event__output_id_sample(struct perf_output_handle *handle,
6427 struct perf_sample_data *data)
6428{
6429 u64 sample_type = data->type;
6430
6431 if (sample_type & PERF_SAMPLE_TID)
6432 perf_output_put(handle, data->tid_entry);
6433
6434 if (sample_type & PERF_SAMPLE_TIME)
6435 perf_output_put(handle, data->time);
6436
6437 if (sample_type & PERF_SAMPLE_ID)
6438 perf_output_put(handle, data->id);
6439
6440 if (sample_type & PERF_SAMPLE_STREAM_ID)
6441 perf_output_put(handle, data->stream_id);
6442
6443 if (sample_type & PERF_SAMPLE_CPU)
6444 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03006445
6446 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6447 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006448}
6449
Frederic Weisbecker76369132011-05-19 19:55:04 +02006450void perf_event__output_id_sample(struct perf_event *event,
6451 struct perf_output_handle *handle,
6452 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006453{
6454 if (event->attr.sample_id_all)
6455 __perf_event__output_id_sample(handle, sample);
6456}
6457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006458static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006459 struct perf_event *event,
6460 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006461{
6462 u64 read_format = event->attr.read_format;
6463 u64 values[4];
6464 int n = 0;
6465
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006466 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006468 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006469 atomic64_read(&event->child_total_time_enabled);
6470 }
6471 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006472 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006473 atomic64_read(&event->child_total_time_running);
6474 }
6475 if (read_format & PERF_FORMAT_ID)
6476 values[n++] = primary_event_id(event);
6477
Frederic Weisbecker76369132011-05-19 19:55:04 +02006478 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006479}
6480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006481static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006482 struct perf_event *event,
6483 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006484{
6485 struct perf_event *leader = event->group_leader, *sub;
6486 u64 read_format = event->attr.read_format;
6487 u64 values[5];
6488 int n = 0;
6489
6490 values[n++] = 1 + leader->nr_siblings;
6491
6492 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02006493 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006494
6495 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02006496 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006497
Peter Zijlstra9e5b1272018-03-09 12:52:04 +01006498 if ((leader != event) &&
6499 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500 leader->pmu->read(leader);
6501
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006502 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006503 if (read_format & PERF_FORMAT_ID)
6504 values[n++] = primary_event_id(leader);
6505
Frederic Weisbecker76369132011-05-19 19:55:04 +02006506 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006507
Peter Zijlstraedb39592018-03-15 17:36:56 +01006508 for_each_sibling_event(sub, leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006509 n = 0;
6510
Jiri Olsa6f5ab002012-10-15 20:13:45 +02006511 if ((sub != event) &&
6512 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006513 sub->pmu->read(sub);
6514
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006515 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006516 if (read_format & PERF_FORMAT_ID)
6517 values[n++] = primary_event_id(sub);
6518
Frederic Weisbecker76369132011-05-19 19:55:04 +02006519 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520 }
6521}
6522
Stephane Eranianeed01522010-10-26 16:08:01 +02006523#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
6524 PERF_FORMAT_TOTAL_TIME_RUNNING)
6525
Peter Zijlstraba5213a2017-05-30 11:45:12 +02006526/*
6527 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
6528 *
6529 * The problem is that its both hard and excessively expensive to iterate the
6530 * child list, not to mention that its impossible to IPI the children running
6531 * on another CPU, from interrupt/NMI context.
6532 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533static void perf_output_read(struct perf_output_handle *handle,
6534 struct perf_event *event)
6535{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006536 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02006537 u64 read_format = event->attr.read_format;
6538
6539 /*
6540 * compute total_time_enabled, total_time_running
6541 * based on snapshot values taken when the event
6542 * was last scheduled in.
6543 *
6544 * we cannot simply called update_context_time()
6545 * because of locking issue as we are called in
6546 * NMI context
6547 */
Eric B Munsonc4794292011-06-23 16:34:38 -04006548 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006549 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02006550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006551 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02006552 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006553 else
Stephane Eranianeed01522010-10-26 16:08:01 +02006554 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006555}
6556
6557void perf_output_sample(struct perf_output_handle *handle,
6558 struct perf_event_header *header,
6559 struct perf_sample_data *data,
6560 struct perf_event *event)
6561{
6562 u64 sample_type = data->type;
6563
6564 perf_output_put(handle, *header);
6565
Adrian Hunterff3d5272013-08-27 11:23:07 +03006566 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6567 perf_output_put(handle, data->id);
6568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006569 if (sample_type & PERF_SAMPLE_IP)
6570 perf_output_put(handle, data->ip);
6571
6572 if (sample_type & PERF_SAMPLE_TID)
6573 perf_output_put(handle, data->tid_entry);
6574
6575 if (sample_type & PERF_SAMPLE_TIME)
6576 perf_output_put(handle, data->time);
6577
6578 if (sample_type & PERF_SAMPLE_ADDR)
6579 perf_output_put(handle, data->addr);
6580
6581 if (sample_type & PERF_SAMPLE_ID)
6582 perf_output_put(handle, data->id);
6583
6584 if (sample_type & PERF_SAMPLE_STREAM_ID)
6585 perf_output_put(handle, data->stream_id);
6586
6587 if (sample_type & PERF_SAMPLE_CPU)
6588 perf_output_put(handle, data->cpu_entry);
6589
6590 if (sample_type & PERF_SAMPLE_PERIOD)
6591 perf_output_put(handle, data->period);
6592
6593 if (sample_type & PERF_SAMPLE_READ)
6594 perf_output_read(handle, event);
6595
6596 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Jiri Olsa99e818c2018-01-07 17:03:50 +01006597 int size = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006598
Jiri Olsa99e818c2018-01-07 17:03:50 +01006599 size += data->callchain->nr;
6600 size *= sizeof(u64);
6601 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602 }
6603
6604 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006605 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07006606
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006607 if (raw) {
6608 struct perf_raw_frag *frag = &raw->frag;
6609
6610 perf_output_put(handle, raw->size);
6611 do {
6612 if (frag->copy) {
6613 __output_custom(handle, frag->copy,
6614 frag->data, frag->size);
6615 } else {
6616 __output_copy(handle, frag->data,
6617 frag->size);
6618 }
6619 if (perf_raw_frag_last(frag))
6620 break;
6621 frag = frag->next;
6622 } while (1);
6623 if (frag->pad)
6624 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006625 } else {
6626 struct {
6627 u32 size;
6628 u32 data;
6629 } raw = {
6630 .size = sizeof(u32),
6631 .data = 0,
6632 };
6633 perf_output_put(handle, raw);
6634 }
6635 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006636
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006637 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6638 if (data->br_stack) {
6639 size_t size;
6640
6641 size = data->br_stack->nr
6642 * sizeof(struct perf_branch_entry);
6643
6644 perf_output_put(handle, data->br_stack->nr);
6645 perf_output_copy(handle, data->br_stack->entries, size);
6646 } else {
6647 /*
6648 * we always store at least the value of nr
6649 */
6650 u64 nr = 0;
6651 perf_output_put(handle, nr);
6652 }
6653 }
Jiri Olsa40189942012-08-07 15:20:37 +02006654
6655 if (sample_type & PERF_SAMPLE_REGS_USER) {
6656 u64 abi = data->regs_user.abi;
6657
6658 /*
6659 * If there are no regs to dump, notice it through
6660 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6661 */
6662 perf_output_put(handle, abi);
6663
6664 if (abi) {
6665 u64 mask = event->attr.sample_regs_user;
6666 perf_output_sample_regs(handle,
6667 data->regs_user.regs,
6668 mask);
6669 }
6670 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006671
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006672 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02006673 perf_output_sample_ustack(handle,
6674 data->stack_user_size,
6675 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006676 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01006677
6678 if (sample_type & PERF_SAMPLE_WEIGHT)
6679 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01006680
6681 if (sample_type & PERF_SAMPLE_DATA_SRC)
6682 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006683
Andi Kleenfdfbbd02013-09-20 07:40:39 -07006684 if (sample_type & PERF_SAMPLE_TRANSACTION)
6685 perf_output_put(handle, data->txn);
6686
Stephane Eranian60e23642014-09-24 13:48:37 +02006687 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6688 u64 abi = data->regs_intr.abi;
6689 /*
6690 * If there are no regs to dump, notice it through
6691 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6692 */
6693 perf_output_put(handle, abi);
6694
6695 if (abi) {
6696 u64 mask = event->attr.sample_regs_intr;
6697
6698 perf_output_sample_regs(handle,
6699 data->regs_intr.regs,
6700 mask);
6701 }
6702 }
6703
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006704 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
6705 perf_output_put(handle, data->phys_addr);
6706
Alexander Shishkina4faf002019-10-25 17:08:33 +03006707 if (sample_type & PERF_SAMPLE_AUX) {
6708 perf_output_put(handle, data->aux_size);
6709
6710 if (data->aux_size)
6711 perf_aux_sample_output(event, handle, data);
6712 }
6713
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006714 if (!event->attr.watermark) {
6715 int wakeup_events = event->attr.wakeup_events;
6716
6717 if (wakeup_events) {
6718 struct ring_buffer *rb = handle->rb;
6719 int events = local_inc_return(&rb->events);
6720
6721 if (events >= wakeup_events) {
6722 local_sub(wakeup_events, &rb->events);
6723 local_inc(&rb->wakeup);
6724 }
6725 }
6726 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006727}
6728
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006729static u64 perf_virt_to_phys(u64 virt)
6730{
6731 u64 phys_addr = 0;
6732 struct page *p = NULL;
6733
6734 if (!virt)
6735 return 0;
6736
6737 if (virt >= TASK_SIZE) {
6738 /* If it's vmalloc()d memory, leave phys_addr as 0 */
6739 if (virt_addr_valid((void *)(uintptr_t)virt) &&
6740 !(virt >= VMALLOC_START && virt < VMALLOC_END))
6741 phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt);
6742 } else {
6743 /*
6744 * Walking the pages tables for user address.
6745 * Interrupts are disabled, so it prevents any tear down
6746 * of the page tables.
6747 * Try IRQ-safe __get_user_pages_fast first.
6748 * If failed, leave phys_addr as 0.
6749 */
6750 if ((current->mm != NULL) &&
6751 (__get_user_pages_fast(virt, 1, 0, &p) == 1))
6752 phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
6753
6754 if (p)
6755 put_page(p);
6756 }
6757
6758 return phys_addr;
6759}
6760
Jiri Olsa99e818c2018-01-07 17:03:50 +01006761static struct perf_callchain_entry __empty_callchain = { .nr = 0, };
6762
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02006763struct perf_callchain_entry *
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006764perf_callchain(struct perf_event *event, struct pt_regs *regs)
6765{
6766 bool kernel = !event->attr.exclude_callchain_kernel;
6767 bool user = !event->attr.exclude_callchain_user;
6768 /* Disallow cross-task user callchains. */
6769 bool crosstask = event->ctx->task && event->ctx->task != current;
6770 const u32 max_stack = event->attr.sample_max_stack;
Jiri Olsa99e818c2018-01-07 17:03:50 +01006771 struct perf_callchain_entry *callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006772
6773 if (!kernel && !user)
Jiri Olsa99e818c2018-01-07 17:03:50 +01006774 return &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006775
Jiri Olsa99e818c2018-01-07 17:03:50 +01006776 callchain = get_perf_callchain(regs, 0, kernel, user,
6777 max_stack, crosstask, true);
6778 return callchain ?: &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006779}
6780
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006781void perf_prepare_sample(struct perf_event_header *header,
6782 struct perf_sample_data *data,
6783 struct perf_event *event,
6784 struct pt_regs *regs)
6785{
6786 u64 sample_type = event->attr.sample_type;
6787
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006788 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006789 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006790
6791 header->misc = 0;
6792 header->misc |= perf_misc_flags(regs);
6793
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006794 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006795
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006796 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006797 data->ip = perf_instruction_pointer(regs);
6798
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006799 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
6800 int size = 1;
6801
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02006802 if (!(sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
6803 data->callchain = perf_callchain(event, regs);
6804
Jiri Olsa99e818c2018-01-07 17:03:50 +01006805 size += data->callchain->nr;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006806
6807 header->size += size * sizeof(u64);
6808 }
6809
6810 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006811 struct perf_raw_record *raw = data->raw;
6812 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006813
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006814 if (raw) {
6815 struct perf_raw_frag *frag = &raw->frag;
6816 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006817
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006818 do {
6819 sum += frag->size;
6820 if (perf_raw_frag_last(frag))
6821 break;
6822 frag = frag->next;
6823 } while (1);
6824
6825 size = round_up(sum + sizeof(u32), sizeof(u64));
6826 raw->size = size - sizeof(u32);
6827 frag->pad = raw->size - sum;
6828 } else {
6829 size = sizeof(u64);
6830 }
6831
6832 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006833 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006834
6835 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6836 int size = sizeof(u64); /* nr */
6837 if (data->br_stack) {
6838 size += data->br_stack->nr
6839 * sizeof(struct perf_branch_entry);
6840 }
6841 header->size += size;
6842 }
Jiri Olsa40189942012-08-07 15:20:37 +02006843
Peter Zijlstra25657112014-09-24 13:48:42 +02006844 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006845 perf_sample_regs_user(&data->regs_user, regs,
6846 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006847
Jiri Olsa40189942012-08-07 15:20:37 +02006848 if (sample_type & PERF_SAMPLE_REGS_USER) {
6849 /* regs dump ABI info */
6850 int size = sizeof(u64);
6851
Jiri Olsa40189942012-08-07 15:20:37 +02006852 if (data->regs_user.regs) {
6853 u64 mask = event->attr.sample_regs_user;
6854 size += hweight64(mask) * sizeof(u64);
6855 }
6856
6857 header->size += size;
6858 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006859
6860 if (sample_type & PERF_SAMPLE_STACK_USER) {
6861 /*
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03006862 * Either we need PERF_SAMPLE_STACK_USER bit to be always
Jiri Olsac5ebced2012-08-07 15:20:40 +02006863 * processed as the last one or have additional check added
6864 * in case new sample type is added, because we could eat
6865 * up the rest of the sample size.
6866 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02006867 u16 stack_size = event->attr.sample_stack_user;
6868 u16 size = sizeof(u64);
6869
Jiri Olsac5ebced2012-08-07 15:20:40 +02006870 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02006871 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006872
6873 /*
6874 * If there is something to dump, add space for the dump
6875 * itself and for the field that tells the dynamic size,
6876 * which is how many have been actually dumped.
6877 */
6878 if (stack_size)
6879 size += sizeof(u64) + stack_size;
6880
6881 data->stack_user_size = stack_size;
6882 header->size += size;
6883 }
Stephane Eranian60e23642014-09-24 13:48:37 +02006884
6885 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6886 /* regs dump ABI info */
6887 int size = sizeof(u64);
6888
6889 perf_sample_regs_intr(&data->regs_intr, regs);
6890
6891 if (data->regs_intr.regs) {
6892 u64 mask = event->attr.sample_regs_intr;
6893
6894 size += hweight64(mask) * sizeof(u64);
6895 }
6896
6897 header->size += size;
6898 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006899
6900 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
6901 data->phys_addr = perf_virt_to_phys(data->addr);
Alexander Shishkina4faf002019-10-25 17:08:33 +03006902
6903 if (sample_type & PERF_SAMPLE_AUX) {
6904 u64 size;
6905
6906 header->size += sizeof(u64); /* size */
6907
6908 /*
6909 * Given the 16bit nature of header::size, an AUX sample can
6910 * easily overflow it, what with all the preceding sample bits.
6911 * Make sure this doesn't happen by using up to U16_MAX bytes
6912 * per sample in total (rounded down to 8 byte boundary).
6913 */
6914 size = min_t(size_t, U16_MAX - header->size,
6915 event->attr.aux_sample_size);
6916 size = rounddown(size, 8);
6917 size = perf_prepare_sample_aux(event, data, size);
6918
6919 WARN_ON_ONCE(size + header->size > U16_MAX);
6920 header->size += size;
6921 }
6922 /*
6923 * If you're adding more sample types here, you likely need to do
6924 * something about the overflowing header::size, like repurpose the
6925 * lowest 3 bits of size, which should be always zero at the moment.
6926 * This raises a more important question, do we really need 512k sized
6927 * samples and why, so good argumentation is in order for whatever you
6928 * do here next.
6929 */
6930 WARN_ON_ONCE(header->size & 7);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006931}
6932
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03006933static __always_inline int
Wang Nan9ecda412016-04-05 14:11:18 +00006934__perf_event_output(struct perf_event *event,
6935 struct perf_sample_data *data,
6936 struct pt_regs *regs,
6937 int (*output_begin)(struct perf_output_handle *,
6938 struct perf_event *,
6939 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006940{
6941 struct perf_output_handle handle;
6942 struct perf_event_header header;
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03006943 int err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006944
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006945 /* protect the callchain buffers */
6946 rcu_read_lock();
6947
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006948 perf_prepare_sample(&header, data, event, regs);
6949
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03006950 err = output_begin(&handle, event, header.size);
6951 if (err)
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006952 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006953
6954 perf_output_sample(&handle, &header, data, event);
6955
6956 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006957
6958exit:
6959 rcu_read_unlock();
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03006960 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006961}
6962
Wang Nan9ecda412016-04-05 14:11:18 +00006963void
6964perf_event_output_forward(struct perf_event *event,
6965 struct perf_sample_data *data,
6966 struct pt_regs *regs)
6967{
6968 __perf_event_output(event, data, regs, perf_output_begin_forward);
6969}
6970
6971void
6972perf_event_output_backward(struct perf_event *event,
6973 struct perf_sample_data *data,
6974 struct pt_regs *regs)
6975{
6976 __perf_event_output(event, data, regs, perf_output_begin_backward);
6977}
6978
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03006979int
Wang Nan9ecda412016-04-05 14:11:18 +00006980perf_event_output(struct perf_event *event,
6981 struct perf_sample_data *data,
6982 struct pt_regs *regs)
6983{
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03006984 return __perf_event_output(event, data, regs, perf_output_begin);
Wang Nan9ecda412016-04-05 14:11:18 +00006985}
6986
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006987/*
6988 * read event_id
6989 */
6990
6991struct perf_read_event {
6992 struct perf_event_header header;
6993
6994 u32 pid;
6995 u32 tid;
6996};
6997
6998static void
6999perf_event_read_event(struct perf_event *event,
7000 struct task_struct *task)
7001{
7002 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007003 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007004 struct perf_read_event read_event = {
7005 .header = {
7006 .type = PERF_RECORD_READ,
7007 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007008 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007009 },
7010 .pid = perf_event_pid(event, task),
7011 .tid = perf_event_tid(event, task),
7012 };
7013 int ret;
7014
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007015 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007016 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007017 if (ret)
7018 return;
7019
7020 perf_output_put(&handle, read_event);
7021 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007022 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007023
7024 perf_output_end(&handle);
7025}
7026
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007027typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007028
7029static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007030perf_iterate_ctx(struct perf_event_context *ctx,
7031 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007032 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02007033{
7034 struct perf_event *event;
7035
7036 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007037 if (!all) {
7038 if (event->state < PERF_EVENT_STATE_INACTIVE)
7039 continue;
7040 if (!event_filter_match(event))
7041 continue;
7042 }
7043
Jiri Olsa67516842013-07-09 18:56:31 +02007044 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007045 }
7046}
7047
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007048static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07007049{
7050 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
7051 struct perf_event *event;
7052
7053 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02007054 /*
7055 * Skip events that are not fully formed yet; ensure that
7056 * if we observe event->ctx, both event and ctx will be
7057 * complete enough. See perf_install_in_context().
7058 */
7059 if (!smp_load_acquire(&event->ctx))
7060 continue;
7061
Kan Liangf2fb6be2016-03-23 11:24:37 -07007062 if (event->state < PERF_EVENT_STATE_INACTIVE)
7063 continue;
7064 if (!event_filter_match(event))
7065 continue;
7066 output(event, data);
7067 }
7068}
7069
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007070/*
7071 * Iterate all events that need to receive side-band events.
7072 *
7073 * For new callers; ensure that account_pmu_sb_event() includes
7074 * your event, otherwise it might not get delivered.
7075 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007076static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007077perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007078 struct perf_event_context *task_ctx)
7079{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007080 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02007081 int ctxn;
7082
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007083 rcu_read_lock();
7084 preempt_disable();
7085
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007086 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007087 * If we have task_ctx != NULL we only notify the task context itself.
7088 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007089 * context.
7090 */
7091 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007092 perf_iterate_ctx(task_ctx, output, data, false);
7093 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007094 }
7095
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007096 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07007097
7098 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02007099 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
7100 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007101 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007102 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007103done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07007104 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02007105 rcu_read_unlock();
7106}
7107
Alexander Shishkin375637b2016-04-27 18:44:46 +03007108/*
7109 * Clear all file-based filters at exec, they'll have to be
7110 * re-instated when/if these objects are mmapped again.
7111 */
7112static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
7113{
7114 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7115 struct perf_addr_filter *filter;
7116 unsigned int restart = 0, count = 0;
7117 unsigned long flags;
7118
7119 if (!has_addr_filter(event))
7120 return;
7121
7122 raw_spin_lock_irqsave(&ifh->lock, flags);
7123 list_for_each_entry(filter, &ifh->list, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07007124 if (filter->path.dentry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007125 event->addr_filter_ranges[count].start = 0;
7126 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007127 restart++;
7128 }
7129
7130 count++;
7131 }
7132
7133 if (restart)
7134 event->addr_filters_gen++;
7135 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7136
7137 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03007138 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007139}
7140
7141void perf_event_exec(void)
7142{
7143 struct perf_event_context *ctx;
7144 int ctxn;
7145
7146 rcu_read_lock();
7147 for_each_task_context_nr(ctxn) {
7148 ctx = current->perf_event_ctxp[ctxn];
7149 if (!ctx)
7150 continue;
7151
7152 perf_event_enable_on_exec(ctxn);
7153
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007154 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03007155 true);
7156 }
7157 rcu_read_unlock();
7158}
7159
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007160struct remote_output {
7161 struct ring_buffer *rb;
7162 int err;
7163};
7164
7165static void __perf_event_output_stop(struct perf_event *event, void *data)
7166{
7167 struct perf_event *parent = event->parent;
7168 struct remote_output *ro = data;
7169 struct ring_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007170 struct stop_event_data sd = {
7171 .event = event,
7172 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007173
7174 if (!has_aux(event))
7175 return;
7176
7177 if (!parent)
7178 parent = event;
7179
7180 /*
7181 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03007182 * ring-buffer, but it will be the child that's actually using it.
7183 *
7184 * We are using event::rb to determine if the event should be stopped,
7185 * however this may race with ring_buffer_attach() (through set_output),
7186 * which will make us skip the event that actually needs to be stopped.
7187 * So ring_buffer_attach() has to stop an aux event before re-assigning
7188 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007189 */
7190 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007191 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007192}
7193
7194static int __perf_pmu_output_stop(void *info)
7195{
7196 struct perf_event *event = info;
Alexander Shishkinf3a519e2019-10-22 10:39:40 +03007197 struct pmu *pmu = event->ctx->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01007198 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007199 struct remote_output ro = {
7200 .rb = event->rb,
7201 };
7202
7203 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007204 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007205 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007206 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007207 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007208 rcu_read_unlock();
7209
7210 return ro.err;
7211}
7212
7213static void perf_pmu_output_stop(struct perf_event *event)
7214{
7215 struct perf_event *iter;
7216 int err, cpu;
7217
7218restart:
7219 rcu_read_lock();
7220 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
7221 /*
7222 * For per-CPU events, we need to make sure that neither they
7223 * nor their children are running; for cpu==-1 events it's
7224 * sufficient to stop the event itself if it's active, since
7225 * it can't have children.
7226 */
7227 cpu = iter->cpu;
7228 if (cpu == -1)
7229 cpu = READ_ONCE(iter->oncpu);
7230
7231 if (cpu == -1)
7232 continue;
7233
7234 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
7235 if (err == -EAGAIN) {
7236 rcu_read_unlock();
7237 goto restart;
7238 }
7239 }
7240 rcu_read_unlock();
7241}
7242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007243/*
7244 * task tracking -- fork/exit
7245 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02007246 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007247 */
7248
7249struct perf_task_event {
7250 struct task_struct *task;
7251 struct perf_event_context *task_ctx;
7252
7253 struct {
7254 struct perf_event_header header;
7255
7256 u32 pid;
7257 u32 ppid;
7258 u32 tid;
7259 u32 ptid;
7260 u64 time;
7261 } event_id;
7262};
7263
Jiri Olsa67516842013-07-09 18:56:31 +02007264static int perf_event_task_match(struct perf_event *event)
7265{
Stephane Eranian13d7a242013-08-21 12:10:24 +02007266 return event->attr.comm || event->attr.mmap ||
7267 event->attr.mmap2 || event->attr.mmap_data ||
7268 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02007269}
7270
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007271static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007272 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007273{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007274 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007275 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007276 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007277 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007278 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01007279
Jiri Olsa67516842013-07-09 18:56:31 +02007280 if (!perf_event_task_match(event))
7281 return;
7282
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007283 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007284
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007285 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007286 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02007287 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007288 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007289
7290 task_event->event_id.pid = perf_event_pid(event, task);
7291 task_event->event_id.ppid = perf_event_pid(event, current);
7292
7293 task_event->event_id.tid = perf_event_tid(event, task);
7294 task_event->event_id.ptid = perf_event_tid(event, current);
7295
Peter Zijlstra34f43922015-02-20 14:05:38 +01007296 task_event->event_id.time = perf_event_clock(event);
7297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007298 perf_output_put(&handle, task_event->event_id);
7299
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007300 perf_event__output_id_sample(event, &handle, &sample);
7301
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007302 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007303out:
7304 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007305}
7306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007307static void perf_event_task(struct task_struct *task,
7308 struct perf_event_context *task_ctx,
7309 int new)
7310{
7311 struct perf_task_event task_event;
7312
7313 if (!atomic_read(&nr_comm_events) &&
7314 !atomic_read(&nr_mmap_events) &&
7315 !atomic_read(&nr_task_events))
7316 return;
7317
7318 task_event = (struct perf_task_event){
7319 .task = task,
7320 .task_ctx = task_ctx,
7321 .event_id = {
7322 .header = {
7323 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
7324 .misc = 0,
7325 .size = sizeof(task_event.event_id),
7326 },
7327 /* .pid */
7328 /* .ppid */
7329 /* .tid */
7330 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01007331 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007332 },
7333 };
7334
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007335 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007336 &task_event,
7337 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007338}
7339
7340void perf_event_fork(struct task_struct *task)
7341{
7342 perf_event_task(task, NULL, 1);
Hari Bathinie4222672017-03-08 02:11:36 +05307343 perf_event_namespaces(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007344}
7345
7346/*
7347 * comm tracking
7348 */
7349
7350struct perf_comm_event {
7351 struct task_struct *task;
7352 char *comm;
7353 int comm_size;
7354
7355 struct {
7356 struct perf_event_header header;
7357
7358 u32 pid;
7359 u32 tid;
7360 } event_id;
7361};
7362
Jiri Olsa67516842013-07-09 18:56:31 +02007363static int perf_event_comm_match(struct perf_event *event)
7364{
7365 return event->attr.comm;
7366}
7367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007368static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007369 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007370{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007371 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007372 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007373 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007374 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007375 int ret;
7376
Jiri Olsa67516842013-07-09 18:56:31 +02007377 if (!perf_event_comm_match(event))
7378 return;
7379
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007380 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
7381 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007382 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007383
7384 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007385 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007386
7387 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
7388 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
7389
7390 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02007391 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007392 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007393
7394 perf_event__output_id_sample(event, &handle, &sample);
7395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007396 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007397out:
7398 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007399}
7400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007401static void perf_event_comm_event(struct perf_comm_event *comm_event)
7402{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007403 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007404 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007405
7406 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01007407 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007408 size = ALIGN(strlen(comm)+1, sizeof(u64));
7409
7410 comm_event->comm = comm;
7411 comm_event->comm_size = size;
7412
7413 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007414
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007415 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007416 comm_event,
7417 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007418}
7419
Adrian Hunter82b89772014-05-28 11:45:04 +03007420void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007421{
7422 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007423
7424 if (!atomic_read(&nr_comm_events))
7425 return;
7426
7427 comm_event = (struct perf_comm_event){
7428 .task = task,
7429 /* .comm */
7430 /* .comm_size */
7431 .event_id = {
7432 .header = {
7433 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03007434 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007435 /* .size */
7436 },
7437 /* .pid */
7438 /* .tid */
7439 },
7440 };
7441
7442 perf_event_comm_event(&comm_event);
7443}
7444
7445/*
Hari Bathinie4222672017-03-08 02:11:36 +05307446 * namespaces tracking
7447 */
7448
7449struct perf_namespaces_event {
7450 struct task_struct *task;
7451
7452 struct {
7453 struct perf_event_header header;
7454
7455 u32 pid;
7456 u32 tid;
7457 u64 nr_namespaces;
7458 struct perf_ns_link_info link_info[NR_NAMESPACES];
7459 } event_id;
7460};
7461
7462static int perf_event_namespaces_match(struct perf_event *event)
7463{
7464 return event->attr.namespaces;
7465}
7466
7467static void perf_event_namespaces_output(struct perf_event *event,
7468 void *data)
7469{
7470 struct perf_namespaces_event *namespaces_event = data;
7471 struct perf_output_handle handle;
7472 struct perf_sample_data sample;
Jiri Olsa34900ec2017-08-09 18:14:06 +02007473 u16 header_size = namespaces_event->event_id.header.size;
Hari Bathinie4222672017-03-08 02:11:36 +05307474 int ret;
7475
7476 if (!perf_event_namespaces_match(event))
7477 return;
7478
7479 perf_event_header__init_id(&namespaces_event->event_id.header,
7480 &sample, event);
7481 ret = perf_output_begin(&handle, event,
7482 namespaces_event->event_id.header.size);
7483 if (ret)
Jiri Olsa34900ec2017-08-09 18:14:06 +02007484 goto out;
Hari Bathinie4222672017-03-08 02:11:36 +05307485
7486 namespaces_event->event_id.pid = perf_event_pid(event,
7487 namespaces_event->task);
7488 namespaces_event->event_id.tid = perf_event_tid(event,
7489 namespaces_event->task);
7490
7491 perf_output_put(&handle, namespaces_event->event_id);
7492
7493 perf_event__output_id_sample(event, &handle, &sample);
7494
7495 perf_output_end(&handle);
Jiri Olsa34900ec2017-08-09 18:14:06 +02007496out:
7497 namespaces_event->event_id.header.size = header_size;
Hari Bathinie4222672017-03-08 02:11:36 +05307498}
7499
7500static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info,
7501 struct task_struct *task,
7502 const struct proc_ns_operations *ns_ops)
7503{
7504 struct path ns_path;
7505 struct inode *ns_inode;
7506 void *error;
7507
7508 error = ns_get_path(&ns_path, task, ns_ops);
7509 if (!error) {
7510 ns_inode = ns_path.dentry->d_inode;
7511 ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev);
7512 ns_link_info->ino = ns_inode->i_ino;
Vasily Averin0e18dd12017-11-15 08:47:02 +03007513 path_put(&ns_path);
Hari Bathinie4222672017-03-08 02:11:36 +05307514 }
7515}
7516
7517void perf_event_namespaces(struct task_struct *task)
7518{
7519 struct perf_namespaces_event namespaces_event;
7520 struct perf_ns_link_info *ns_link_info;
7521
7522 if (!atomic_read(&nr_namespaces_events))
7523 return;
7524
7525 namespaces_event = (struct perf_namespaces_event){
7526 .task = task,
7527 .event_id = {
7528 .header = {
7529 .type = PERF_RECORD_NAMESPACES,
7530 .misc = 0,
7531 .size = sizeof(namespaces_event.event_id),
7532 },
7533 /* .pid */
7534 /* .tid */
7535 .nr_namespaces = NR_NAMESPACES,
7536 /* .link_info[NR_NAMESPACES] */
7537 },
7538 };
7539
7540 ns_link_info = namespaces_event.event_id.link_info;
7541
7542 perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX],
7543 task, &mntns_operations);
7544
7545#ifdef CONFIG_USER_NS
7546 perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX],
7547 task, &userns_operations);
7548#endif
7549#ifdef CONFIG_NET_NS
7550 perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX],
7551 task, &netns_operations);
7552#endif
7553#ifdef CONFIG_UTS_NS
7554 perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX],
7555 task, &utsns_operations);
7556#endif
7557#ifdef CONFIG_IPC_NS
7558 perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX],
7559 task, &ipcns_operations);
7560#endif
7561#ifdef CONFIG_PID_NS
7562 perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX],
7563 task, &pidns_operations);
7564#endif
7565#ifdef CONFIG_CGROUPS
7566 perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX],
7567 task, &cgroupns_operations);
7568#endif
7569
7570 perf_iterate_sb(perf_event_namespaces_output,
7571 &namespaces_event,
7572 NULL);
7573}
7574
7575/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007576 * mmap tracking
7577 */
7578
7579struct perf_mmap_event {
7580 struct vm_area_struct *vma;
7581
7582 const char *file_name;
7583 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007584 int maj, min;
7585 u64 ino;
7586 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007587 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007588
7589 struct {
7590 struct perf_event_header header;
7591
7592 u32 pid;
7593 u32 tid;
7594 u64 start;
7595 u64 len;
7596 u64 pgoff;
7597 } event_id;
7598};
7599
Jiri Olsa67516842013-07-09 18:56:31 +02007600static int perf_event_mmap_match(struct perf_event *event,
7601 void *data)
7602{
7603 struct perf_mmap_event *mmap_event = data;
7604 struct vm_area_struct *vma = mmap_event->vma;
7605 int executable = vma->vm_flags & VM_EXEC;
7606
7607 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02007608 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02007609}
7610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007611static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007612 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007613{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007614 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007615 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007616 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007617 int size = mmap_event->event_id.header.size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007618 u32 type = mmap_event->event_id.header.type;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007619 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007620
Jiri Olsa67516842013-07-09 18:56:31 +02007621 if (!perf_event_mmap_match(event, data))
7622 return;
7623
Stephane Eranian13d7a242013-08-21 12:10:24 +02007624 if (event->attr.mmap2) {
7625 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
7626 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
7627 mmap_event->event_id.header.size += sizeof(mmap_event->min);
7628 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03007629 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007630 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
7631 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007632 }
7633
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007634 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
7635 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007636 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007637 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007638 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007639
7640 mmap_event->event_id.pid = perf_event_pid(event, current);
7641 mmap_event->event_id.tid = perf_event_tid(event, current);
7642
7643 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007644
7645 if (event->attr.mmap2) {
7646 perf_output_put(&handle, mmap_event->maj);
7647 perf_output_put(&handle, mmap_event->min);
7648 perf_output_put(&handle, mmap_event->ino);
7649 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007650 perf_output_put(&handle, mmap_event->prot);
7651 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007652 }
7653
Frederic Weisbecker76369132011-05-19 19:55:04 +02007654 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007655 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007656
7657 perf_event__output_id_sample(event, &handle, &sample);
7658
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007659 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007660out:
7661 mmap_event->event_id.header.size = size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007662 mmap_event->event_id.header.type = type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007663}
7664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007665static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
7666{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007667 struct vm_area_struct *vma = mmap_event->vma;
7668 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007669 int maj = 0, min = 0;
7670 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007671 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007672 unsigned int size;
7673 char tmp[16];
7674 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02007675 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007676
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01007677 if (vma->vm_flags & VM_READ)
7678 prot |= PROT_READ;
7679 if (vma->vm_flags & VM_WRITE)
7680 prot |= PROT_WRITE;
7681 if (vma->vm_flags & VM_EXEC)
7682 prot |= PROT_EXEC;
7683
7684 if (vma->vm_flags & VM_MAYSHARE)
7685 flags = MAP_SHARED;
7686 else
7687 flags = MAP_PRIVATE;
7688
7689 if (vma->vm_flags & VM_DENYWRITE)
7690 flags |= MAP_DENYWRITE;
7691 if (vma->vm_flags & VM_MAYEXEC)
7692 flags |= MAP_EXECUTABLE;
7693 if (vma->vm_flags & VM_LOCKED)
7694 flags |= MAP_LOCKED;
7695 if (vma->vm_flags & VM_HUGETLB)
7696 flags |= MAP_HUGETLB;
7697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007698 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02007699 struct inode *inode;
7700 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02007701
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02007702 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007703 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007704 name = "//enomem";
7705 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007706 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007707 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02007708 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007709 * need to add enough zero bytes after the string to handle
7710 * the 64bit alignment we do later.
7711 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02007712 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007713 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007714 name = "//toolong";
7715 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007716 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02007717 inode = file_inode(vma->vm_file);
7718 dev = inode->i_sb->s_dev;
7719 ino = inode->i_ino;
7720 gen = inode->i_generation;
7721 maj = MAJOR(dev);
7722 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007723
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007724 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007725 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02007726 if (vma->vm_ops && vma->vm_ops->name) {
7727 name = (char *) vma->vm_ops->name(vma);
7728 if (name)
7729 goto cpy_name;
7730 }
7731
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02007732 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007733 if (name)
7734 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007735
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02007736 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007737 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007738 name = "[heap]";
7739 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02007740 }
7741 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007742 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007743 name = "[stack]";
7744 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007745 }
7746
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007747 name = "//anon";
7748 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007749 }
7750
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02007751cpy_name:
7752 strlcpy(tmp, name, sizeof(tmp));
7753 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007754got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02007755 /*
7756 * Since our buffer works in 8 byte units we need to align our string
7757 * size to a multiple of 8. However, we must guarantee the tail end is
7758 * zero'd out to avoid leaking random bits to userspace.
7759 */
7760 size = strlen(name)+1;
7761 while (!IS_ALIGNED(size, sizeof(u64)))
7762 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007763
7764 mmap_event->file_name = name;
7765 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007766 mmap_event->maj = maj;
7767 mmap_event->min = min;
7768 mmap_event->ino = ino;
7769 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007770 mmap_event->prot = prot;
7771 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007772
Stephane Eranian2fe85422013-01-24 16:10:39 +01007773 if (!(vma->vm_flags & VM_EXEC))
7774 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
7775
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007776 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
7777
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007778 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007779 mmap_event,
7780 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007781
7782 kfree(buf);
7783}
7784
Alexander Shishkin375637b2016-04-27 18:44:46 +03007785/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03007786 * Check whether inode and address range match filter criteria.
7787 */
7788static bool perf_addr_filter_match(struct perf_addr_filter *filter,
7789 struct file *file, unsigned long offset,
7790 unsigned long size)
7791{
Mathieu Poirier7f635ff2018-07-16 17:13:51 -06007792 /* d_inode(NULL) won't be equal to any mapped user-space file */
7793 if (!filter->path.dentry)
7794 return false;
7795
Song Liu9511bce2018-04-17 23:29:07 -07007796 if (d_inode(filter->path.dentry) != file_inode(file))
Alexander Shishkin375637b2016-04-27 18:44:46 +03007797 return false;
7798
7799 if (filter->offset > offset + size)
7800 return false;
7801
7802 if (filter->offset + filter->size < offset)
7803 return false;
7804
7805 return true;
7806}
7807
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007808static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter,
7809 struct vm_area_struct *vma,
7810 struct perf_addr_filter_range *fr)
7811{
7812 unsigned long vma_size = vma->vm_end - vma->vm_start;
7813 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7814 struct file *file = vma->vm_file;
7815
7816 if (!perf_addr_filter_match(filter, file, off, vma_size))
7817 return false;
7818
7819 if (filter->offset < off) {
7820 fr->start = vma->vm_start;
7821 fr->size = min(vma_size, filter->size - (off - filter->offset));
7822 } else {
7823 fr->start = vma->vm_start + filter->offset - off;
7824 fr->size = min(vma->vm_end - fr->start, filter->size);
7825 }
7826
7827 return true;
7828}
7829
Alexander Shishkin375637b2016-04-27 18:44:46 +03007830static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
7831{
7832 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7833 struct vm_area_struct *vma = data;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007834 struct perf_addr_filter *filter;
7835 unsigned int restart = 0, count = 0;
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007836 unsigned long flags;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007837
7838 if (!has_addr_filter(event))
7839 return;
7840
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007841 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007842 return;
7843
7844 raw_spin_lock_irqsave(&ifh->lock, flags);
7845 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007846 if (perf_addr_filter_vma_adjust(filter, vma,
7847 &event->addr_filter_ranges[count]))
Alexander Shishkin375637b2016-04-27 18:44:46 +03007848 restart++;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007849
7850 count++;
7851 }
7852
7853 if (restart)
7854 event->addr_filters_gen++;
7855 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7856
7857 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03007858 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007859}
7860
7861/*
7862 * Adjust all task's events' filters to the new vma
7863 */
7864static void perf_addr_filters_adjust(struct vm_area_struct *vma)
7865{
7866 struct perf_event_context *ctx;
7867 int ctxn;
7868
Mathieu Poirier12b40a22016-07-18 10:43:06 -06007869 /*
7870 * Data tracing isn't supported yet and as such there is no need
7871 * to keep track of anything that isn't related to executable code:
7872 */
7873 if (!(vma->vm_flags & VM_EXEC))
7874 return;
7875
Alexander Shishkin375637b2016-04-27 18:44:46 +03007876 rcu_read_lock();
7877 for_each_task_context_nr(ctxn) {
7878 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
7879 if (!ctx)
7880 continue;
7881
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007882 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007883 }
7884 rcu_read_unlock();
7885}
7886
Eric B Munson3af9e852010-05-18 15:30:49 +01007887void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007888{
7889 struct perf_mmap_event mmap_event;
7890
7891 if (!atomic_read(&nr_mmap_events))
7892 return;
7893
7894 mmap_event = (struct perf_mmap_event){
7895 .vma = vma,
7896 /* .file_name */
7897 /* .file_size */
7898 .event_id = {
7899 .header = {
7900 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08007901 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007902 /* .size */
7903 },
7904 /* .pid */
7905 /* .tid */
7906 .start = vma->vm_start,
7907 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01007908 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007909 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02007910 /* .maj (attr_mmap2 only) */
7911 /* .min (attr_mmap2 only) */
7912 /* .ino (attr_mmap2 only) */
7913 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007914 /* .prot (attr_mmap2 only) */
7915 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007916 };
7917
Alexander Shishkin375637b2016-04-27 18:44:46 +03007918 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007919 perf_event_mmap_event(&mmap_event);
7920}
7921
Alexander Shishkin68db7e92015-01-14 14:18:15 +02007922void perf_event_aux_event(struct perf_event *event, unsigned long head,
7923 unsigned long size, u64 flags)
7924{
7925 struct perf_output_handle handle;
7926 struct perf_sample_data sample;
7927 struct perf_aux_event {
7928 struct perf_event_header header;
7929 u64 offset;
7930 u64 size;
7931 u64 flags;
7932 } rec = {
7933 .header = {
7934 .type = PERF_RECORD_AUX,
7935 .misc = 0,
7936 .size = sizeof(rec),
7937 },
7938 .offset = head,
7939 .size = size,
7940 .flags = flags,
7941 };
7942 int ret;
7943
7944 perf_event_header__init_id(&rec.header, &sample, event);
7945 ret = perf_output_begin(&handle, event, rec.header.size);
7946
7947 if (ret)
7948 return;
7949
7950 perf_output_put(&handle, rec);
7951 perf_event__output_id_sample(event, &handle, &sample);
7952
7953 perf_output_end(&handle);
7954}
7955
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007956/*
Kan Liangf38b0db2015-05-10 15:13:14 -04007957 * Lost/dropped samples logging
7958 */
7959void perf_log_lost_samples(struct perf_event *event, u64 lost)
7960{
7961 struct perf_output_handle handle;
7962 struct perf_sample_data sample;
7963 int ret;
7964
7965 struct {
7966 struct perf_event_header header;
7967 u64 lost;
7968 } lost_samples_event = {
7969 .header = {
7970 .type = PERF_RECORD_LOST_SAMPLES,
7971 .misc = 0,
7972 .size = sizeof(lost_samples_event),
7973 },
7974 .lost = lost,
7975 };
7976
7977 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
7978
7979 ret = perf_output_begin(&handle, event,
7980 lost_samples_event.header.size);
7981 if (ret)
7982 return;
7983
7984 perf_output_put(&handle, lost_samples_event);
7985 perf_event__output_id_sample(event, &handle, &sample);
7986 perf_output_end(&handle);
7987}
7988
7989/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03007990 * context_switch tracking
7991 */
7992
7993struct perf_switch_event {
7994 struct task_struct *task;
7995 struct task_struct *next_prev;
7996
7997 struct {
7998 struct perf_event_header header;
7999 u32 next_prev_pid;
8000 u32 next_prev_tid;
8001 } event_id;
8002};
8003
8004static int perf_event_switch_match(struct perf_event *event)
8005{
8006 return event->attr.context_switch;
8007}
8008
8009static void perf_event_switch_output(struct perf_event *event, void *data)
8010{
8011 struct perf_switch_event *se = data;
8012 struct perf_output_handle handle;
8013 struct perf_sample_data sample;
8014 int ret;
8015
8016 if (!perf_event_switch_match(event))
8017 return;
8018
8019 /* Only CPU-wide events are allowed to see next/prev pid/tid */
8020 if (event->ctx->task) {
8021 se->event_id.header.type = PERF_RECORD_SWITCH;
8022 se->event_id.header.size = sizeof(se->event_id.header);
8023 } else {
8024 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
8025 se->event_id.header.size = sizeof(se->event_id);
8026 se->event_id.next_prev_pid =
8027 perf_event_pid(event, se->next_prev);
8028 se->event_id.next_prev_tid =
8029 perf_event_tid(event, se->next_prev);
8030 }
8031
8032 perf_event_header__init_id(&se->event_id.header, &sample, event);
8033
8034 ret = perf_output_begin(&handle, event, se->event_id.header.size);
8035 if (ret)
8036 return;
8037
8038 if (event->ctx->task)
8039 perf_output_put(&handle, se->event_id.header);
8040 else
8041 perf_output_put(&handle, se->event_id);
8042
8043 perf_event__output_id_sample(event, &handle, &sample);
8044
8045 perf_output_end(&handle);
8046}
8047
8048static void perf_event_switch(struct task_struct *task,
8049 struct task_struct *next_prev, bool sched_in)
8050{
8051 struct perf_switch_event switch_event;
8052
8053 /* N.B. caller checks nr_switch_events != 0 */
8054
8055 switch_event = (struct perf_switch_event){
8056 .task = task,
8057 .next_prev = next_prev,
8058 .event_id = {
8059 .header = {
8060 /* .type */
8061 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
8062 /* .size */
8063 },
8064 /* .next_prev_pid */
8065 /* .next_prev_tid */
8066 },
8067 };
8068
Alexey Budankov101592b2018-04-09 10:25:32 +03008069 if (!sched_in && task->state == TASK_RUNNING)
8070 switch_event.event_id.header.misc |=
8071 PERF_RECORD_MISC_SWITCH_OUT_PREEMPT;
8072
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008073 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03008074 &switch_event,
8075 NULL);
8076}
8077
8078/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008079 * IRQ throttle logging
8080 */
8081
8082static void perf_log_throttle(struct perf_event *event, int enable)
8083{
8084 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008085 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008086 int ret;
8087
8088 struct {
8089 struct perf_event_header header;
8090 u64 time;
8091 u64 id;
8092 u64 stream_id;
8093 } throttle_event = {
8094 .header = {
8095 .type = PERF_RECORD_THROTTLE,
8096 .misc = 0,
8097 .size = sizeof(throttle_event),
8098 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01008099 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008100 .id = primary_event_id(event),
8101 .stream_id = event->id,
8102 };
8103
8104 if (enable)
8105 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
8106
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008107 perf_event_header__init_id(&throttle_event.header, &sample, event);
8108
8109 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02008110 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008111 if (ret)
8112 return;
8113
8114 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008115 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008116 perf_output_end(&handle);
8117}
8118
Song Liu76193a92019-01-17 08:15:13 -08008119/*
8120 * ksymbol register/unregister tracking
8121 */
8122
8123struct perf_ksymbol_event {
8124 const char *name;
8125 int name_len;
8126 struct {
8127 struct perf_event_header header;
8128 u64 addr;
8129 u32 len;
8130 u16 ksym_type;
8131 u16 flags;
8132 } event_id;
8133};
8134
8135static int perf_event_ksymbol_match(struct perf_event *event)
8136{
8137 return event->attr.ksymbol;
8138}
8139
8140static void perf_event_ksymbol_output(struct perf_event *event, void *data)
8141{
8142 struct perf_ksymbol_event *ksymbol_event = data;
8143 struct perf_output_handle handle;
8144 struct perf_sample_data sample;
8145 int ret;
8146
8147 if (!perf_event_ksymbol_match(event))
8148 return;
8149
8150 perf_event_header__init_id(&ksymbol_event->event_id.header,
8151 &sample, event);
8152 ret = perf_output_begin(&handle, event,
8153 ksymbol_event->event_id.header.size);
8154 if (ret)
8155 return;
8156
8157 perf_output_put(&handle, ksymbol_event->event_id);
8158 __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len);
8159 perf_event__output_id_sample(event, &handle, &sample);
8160
8161 perf_output_end(&handle);
8162}
8163
8164void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister,
8165 const char *sym)
8166{
8167 struct perf_ksymbol_event ksymbol_event;
8168 char name[KSYM_NAME_LEN];
8169 u16 flags = 0;
8170 int name_len;
8171
8172 if (!atomic_read(&nr_ksymbol_events))
8173 return;
8174
8175 if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX ||
8176 ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN)
8177 goto err;
8178
8179 strlcpy(name, sym, KSYM_NAME_LEN);
8180 name_len = strlen(name) + 1;
8181 while (!IS_ALIGNED(name_len, sizeof(u64)))
8182 name[name_len++] = '\0';
8183 BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64));
8184
8185 if (unregister)
8186 flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER;
8187
8188 ksymbol_event = (struct perf_ksymbol_event){
8189 .name = name,
8190 .name_len = name_len,
8191 .event_id = {
8192 .header = {
8193 .type = PERF_RECORD_KSYMBOL,
8194 .size = sizeof(ksymbol_event.event_id) +
8195 name_len,
8196 },
8197 .addr = addr,
8198 .len = len,
8199 .ksym_type = ksym_type,
8200 .flags = flags,
8201 },
8202 };
8203
8204 perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL);
8205 return;
8206err:
8207 WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type);
8208}
8209
Song Liu6ee52e22019-01-17 08:15:15 -08008210/*
8211 * bpf program load/unload tracking
8212 */
8213
8214struct perf_bpf_event {
8215 struct bpf_prog *prog;
8216 struct {
8217 struct perf_event_header header;
8218 u16 type;
8219 u16 flags;
8220 u32 id;
8221 u8 tag[BPF_TAG_SIZE];
8222 } event_id;
8223};
8224
8225static int perf_event_bpf_match(struct perf_event *event)
8226{
8227 return event->attr.bpf_event;
8228}
8229
8230static void perf_event_bpf_output(struct perf_event *event, void *data)
8231{
8232 struct perf_bpf_event *bpf_event = data;
8233 struct perf_output_handle handle;
8234 struct perf_sample_data sample;
8235 int ret;
8236
8237 if (!perf_event_bpf_match(event))
8238 return;
8239
8240 perf_event_header__init_id(&bpf_event->event_id.header,
8241 &sample, event);
8242 ret = perf_output_begin(&handle, event,
8243 bpf_event->event_id.header.size);
8244 if (ret)
8245 return;
8246
8247 perf_output_put(&handle, bpf_event->event_id);
8248 perf_event__output_id_sample(event, &handle, &sample);
8249
8250 perf_output_end(&handle);
8251}
8252
8253static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
8254 enum perf_bpf_event_type type)
8255{
8256 bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
8257 char sym[KSYM_NAME_LEN];
8258 int i;
8259
8260 if (prog->aux->func_cnt == 0) {
8261 bpf_get_prog_name(prog, sym);
8262 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
8263 (u64)(unsigned long)prog->bpf_func,
8264 prog->jited_len, unregister, sym);
8265 } else {
8266 for (i = 0; i < prog->aux->func_cnt; i++) {
8267 struct bpf_prog *subprog = prog->aux->func[i];
8268
8269 bpf_get_prog_name(subprog, sym);
8270 perf_event_ksymbol(
8271 PERF_RECORD_KSYMBOL_TYPE_BPF,
8272 (u64)(unsigned long)subprog->bpf_func,
8273 subprog->jited_len, unregister, sym);
8274 }
8275 }
8276}
8277
8278void perf_event_bpf_event(struct bpf_prog *prog,
8279 enum perf_bpf_event_type type,
8280 u16 flags)
8281{
8282 struct perf_bpf_event bpf_event;
8283
8284 if (type <= PERF_BPF_EVENT_UNKNOWN ||
8285 type >= PERF_BPF_EVENT_MAX)
8286 return;
8287
8288 switch (type) {
8289 case PERF_BPF_EVENT_PROG_LOAD:
8290 case PERF_BPF_EVENT_PROG_UNLOAD:
8291 if (atomic_read(&nr_ksymbol_events))
8292 perf_event_bpf_emit_ksymbols(prog, type);
8293 break;
8294 default:
8295 break;
8296 }
8297
8298 if (!atomic_read(&nr_bpf_events))
8299 return;
8300
8301 bpf_event = (struct perf_bpf_event){
8302 .prog = prog,
8303 .event_id = {
8304 .header = {
8305 .type = PERF_RECORD_BPF_EVENT,
8306 .size = sizeof(bpf_event.event_id),
8307 },
8308 .type = type,
8309 .flags = flags,
8310 .id = prog->aux->id,
8311 },
8312 };
8313
8314 BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64));
8315
8316 memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
8317 perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
8318}
8319
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008320void perf_event_itrace_started(struct perf_event *event)
8321{
8322 event->attach_state |= PERF_ATTACH_ITRACE;
8323}
8324
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008325static void perf_log_itrace_start(struct perf_event *event)
8326{
8327 struct perf_output_handle handle;
8328 struct perf_sample_data sample;
8329 struct perf_aux_event {
8330 struct perf_event_header header;
8331 u32 pid;
8332 u32 tid;
8333 } rec;
8334 int ret;
8335
8336 if (event->parent)
8337 event = event->parent;
8338
8339 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008340 event->attach_state & PERF_ATTACH_ITRACE)
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008341 return;
8342
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008343 rec.header.type = PERF_RECORD_ITRACE_START;
8344 rec.header.misc = 0;
8345 rec.header.size = sizeof(rec);
8346 rec.pid = perf_event_pid(event, current);
8347 rec.tid = perf_event_tid(event, current);
8348
8349 perf_event_header__init_id(&rec.header, &sample, event);
8350 ret = perf_output_begin(&handle, event, rec.header.size);
8351
8352 if (ret)
8353 return;
8354
8355 perf_output_put(&handle, rec);
8356 perf_event__output_id_sample(event, &handle, &sample);
8357
8358 perf_output_end(&handle);
8359}
8360
Jiri Olsa475113d2016-12-28 14:31:03 +01008361static int
8362__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008363{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008364 struct hw_perf_event *hwc = &event->hw;
8365 int ret = 0;
Jiri Olsa475113d2016-12-28 14:31:03 +01008366 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01008367
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008368 seq = __this_cpu_read(perf_throttled_seq);
8369 if (seq != hwc->interrupts_seq) {
8370 hwc->interrupts_seq = seq;
8371 hwc->interrupts = 1;
8372 } else {
8373 hwc->interrupts++;
8374 if (unlikely(throttle
8375 && hwc->interrupts >= max_samples_per_tick)) {
8376 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008377 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01008378 hwc->interrupts = MAX_INTERRUPTS;
8379 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008380 ret = 1;
8381 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008382 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008383
8384 if (event->attr.freq) {
8385 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01008386 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008387
Peter Zijlstraabd50712010-01-26 18:50:16 +01008388 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008389
Peter Zijlstraabd50712010-01-26 18:50:16 +01008390 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01008391 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008392 }
8393
Jiri Olsa475113d2016-12-28 14:31:03 +01008394 return ret;
8395}
8396
8397int perf_event_account_interrupt(struct perf_event *event)
8398{
8399 return __perf_event_account_interrupt(event, 1);
8400}
8401
8402/*
8403 * Generic event overflow handling, sampling.
8404 */
8405
8406static int __perf_event_overflow(struct perf_event *event,
8407 int throttle, struct perf_sample_data *data,
8408 struct pt_regs *regs)
8409{
8410 int events = atomic_read(&event->event_limit);
8411 int ret = 0;
8412
8413 /*
8414 * Non-sampling counters might still use the PMI to fold short
8415 * hardware counters, ignore those.
8416 */
8417 if (unlikely(!is_sampling_event(event)))
8418 return 0;
8419
8420 ret = __perf_event_account_interrupt(event, throttle);
8421
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008422 /*
8423 * XXX event_limit might not quite work as expected on inherited
8424 * events
8425 */
8426
8427 event->pending_kill = POLL_IN;
8428 if (events && atomic_dec_and_test(&event->event_limit)) {
8429 ret = 1;
8430 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02008431
8432 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008433 }
8434
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07008435 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01008436
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02008437 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008438 event->pending_wakeup = 1;
8439 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02008440 }
8441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008442 return ret;
8443}
8444
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008445int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008446 struct perf_sample_data *data,
8447 struct pt_regs *regs)
8448{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008449 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008450}
8451
8452/*
8453 * Generic software event infrastructure
8454 */
8455
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008456struct swevent_htable {
8457 struct swevent_hlist *swevent_hlist;
8458 struct mutex hlist_mutex;
8459 int hlist_refcount;
8460
8461 /* Recursion avoidance in each contexts */
8462 int recursion[PERF_NR_CONTEXTS];
8463};
8464
8465static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
8466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008467/*
8468 * We directly increment event->count and keep a second value in
8469 * event->hw.period_left to count intervals. This period event
8470 * is kept in the range [-sample_period, 0] so that we can use the
8471 * sign as trigger.
8472 */
8473
Jiri Olsaab573842013-05-01 17:25:44 +02008474u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008475{
8476 struct hw_perf_event *hwc = &event->hw;
8477 u64 period = hwc->last_period;
8478 u64 nr, offset;
8479 s64 old, val;
8480
8481 hwc->last_period = hwc->sample_period;
8482
8483again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02008484 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008485 if (val < 0)
8486 return 0;
8487
8488 nr = div64_u64(period + val, period);
8489 offset = nr * period;
8490 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02008491 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008492 goto again;
8493
8494 return nr;
8495}
8496
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008497static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008498 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008499 struct pt_regs *regs)
8500{
8501 struct hw_perf_event *hwc = &event->hw;
8502 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008503
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008504 if (!overflow)
8505 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008506
8507 if (hwc->interrupts == MAX_INTERRUPTS)
8508 return;
8509
8510 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008511 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008512 data, regs)) {
8513 /*
8514 * We inhibit the overflow from happening when
8515 * hwc->interrupts == MAX_INTERRUPTS.
8516 */
8517 break;
8518 }
8519 throttle = 1;
8520 }
8521}
8522
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008523static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008524 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008525 struct pt_regs *regs)
8526{
8527 struct hw_perf_event *hwc = &event->hw;
8528
Peter Zijlstrae7850592010-05-21 14:43:08 +02008529 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008530
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008531 if (!regs)
8532 return;
8533
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008534 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008535 return;
8536
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03008537 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
8538 data->period = nr;
8539 return perf_swevent_overflow(event, 1, data, regs);
8540 } else
8541 data->period = event->hw.last_period;
8542
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008543 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008544 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008545
Peter Zijlstrae7850592010-05-21 14:43:08 +02008546 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008547 return;
8548
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008549 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008550}
8551
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008552static int perf_exclude_event(struct perf_event *event,
8553 struct pt_regs *regs)
8554{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008555 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01008556 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008557
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008558 if (regs) {
8559 if (event->attr.exclude_user && user_mode(regs))
8560 return 1;
8561
8562 if (event->attr.exclude_kernel && !user_mode(regs))
8563 return 1;
8564 }
8565
8566 return 0;
8567}
8568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008569static int perf_swevent_match(struct perf_event *event,
8570 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08008571 u32 event_id,
8572 struct perf_sample_data *data,
8573 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008574{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008575 if (event->attr.type != type)
8576 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008577
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008578 if (event->attr.config != event_id)
8579 return 0;
8580
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008581 if (perf_exclude_event(event, regs))
8582 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008583
8584 return 1;
8585}
8586
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008587static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008588{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008589 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008590
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008591 return hash_64(val, SWEVENT_HLIST_BITS);
8592}
8593
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008594static inline struct hlist_head *
8595__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008596{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008597 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008598
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008599 return &hlist->heads[hash];
8600}
8601
8602/* For the read side: events when they trigger */
8603static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008604find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008605{
8606 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008607
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008608 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008609 if (!hlist)
8610 return NULL;
8611
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008612 return __find_swevent_head(hlist, type, event_id);
8613}
8614
8615/* For the event head insertion and removal in the hlist */
8616static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008617find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008618{
8619 struct swevent_hlist *hlist;
8620 u32 event_id = event->attr.config;
8621 u64 type = event->attr.type;
8622
8623 /*
8624 * Event scheduling is always serialized against hlist allocation
8625 * and release. Which makes the protected version suitable here.
8626 * The context lock guarantees that.
8627 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008628 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008629 lockdep_is_held(&event->ctx->lock));
8630 if (!hlist)
8631 return NULL;
8632
8633 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008634}
8635
8636static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008637 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008638 struct perf_sample_data *data,
8639 struct pt_regs *regs)
8640{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008641 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008642 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008643 struct hlist_head *head;
8644
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008645 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008646 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008647 if (!head)
8648 goto end;
8649
Sasha Levinb67bfe02013-02-27 17:06:00 -08008650 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08008651 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008652 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008653 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008654end:
8655 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008656}
8657
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008658DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
8659
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01008660int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008661{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008662 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01008663
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008664 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008665}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01008666EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008667
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07008668void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008669{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008670 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02008671
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008672 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01008673}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008674
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008675void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008676{
Ingo Molnara4234bf2009-11-23 10:57:59 +01008677 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008678
8679 if (WARN_ON_ONCE(!regs))
8680 return;
8681
8682 perf_sample_data_init(&data, addr, 0);
8683 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
8684}
8685
8686void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
8687{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01008688 int rctx;
8689
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008690 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01008691 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008692 if (unlikely(rctx < 0))
8693 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008694
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008695 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01008696
8697 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008698fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008699 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008700}
8701
8702static void perf_swevent_read(struct perf_event *event)
8703{
8704}
8705
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008706static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008707{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008708 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008709 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008710 struct hlist_head *head;
8711
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008712 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008713 hwc->last_period = hwc->sample_period;
8714 perf_swevent_set_period(event);
8715 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008716
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008717 hwc->state = !(flags & PERF_EF_START);
8718
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008719 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01008720 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008721 return -EINVAL;
8722
8723 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08008724 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008726 return 0;
8727}
8728
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008729static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008730{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008731 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008732}
8733
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008734static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02008735{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008736 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02008737}
8738
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008739static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02008740{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008741 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02008742}
8743
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008744/* Deref the hlist from the update side */
8745static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008746swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008747{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008748 return rcu_dereference_protected(swhash->swevent_hlist,
8749 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008750}
8751
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008752static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008753{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008754 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008755
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008756 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008757 return;
8758
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03008759 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08008760 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008761}
8762
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008763static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008764{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008765 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008766
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008767 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008768
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008769 if (!--swhash->hlist_refcount)
8770 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008771
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008772 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008773}
8774
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008775static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008776{
8777 int cpu;
8778
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008779 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008780 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008781}
8782
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008783static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008784{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008785 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008786 int err = 0;
8787
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008788 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02008789 if (!swevent_hlist_deref(swhash) &&
8790 cpumask_test_cpu(cpu, perf_online_mask)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008791 struct swevent_hlist *hlist;
8792
8793 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
8794 if (!hlist) {
8795 err = -ENOMEM;
8796 goto exit;
8797 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008798 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008799 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008800 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02008801exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008802 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008803
8804 return err;
8805}
8806
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008807static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008808{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008809 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008810
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02008811 mutex_lock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008812 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008813 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008814 if (err) {
8815 failed_cpu = cpu;
8816 goto fail;
8817 }
8818 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02008819 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008820 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02008821fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008822 for_each_possible_cpu(cpu) {
8823 if (cpu == failed_cpu)
8824 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008825 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008826 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02008827 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008828 return err;
8829}
8830
Ingo Molnarc5905af2012-02-24 08:31:31 +01008831struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02008832
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008833static void sw_perf_event_destroy(struct perf_event *event)
8834{
8835 u64 event_id = event->attr.config;
8836
8837 WARN_ON(event->parent);
8838
Ingo Molnarc5905af2012-02-24 08:31:31 +01008839 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008840 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008841}
8842
8843static int perf_swevent_init(struct perf_event *event)
8844{
Tommi Rantala8176cce2013-04-13 22:49:14 +03008845 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008846
8847 if (event->attr.type != PERF_TYPE_SOFTWARE)
8848 return -ENOENT;
8849
Stephane Eranian2481c5f2012-02-09 23:20:59 +01008850 /*
8851 * no branch sampling for software events
8852 */
8853 if (has_branch_stack(event))
8854 return -EOPNOTSUPP;
8855
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008856 switch (event_id) {
8857 case PERF_COUNT_SW_CPU_CLOCK:
8858 case PERF_COUNT_SW_TASK_CLOCK:
8859 return -ENOENT;
8860
8861 default:
8862 break;
8863 }
8864
Dan Carpenterce677832010-10-24 21:50:42 +02008865 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008866 return -ENOENT;
8867
8868 if (!event->parent) {
8869 int err;
8870
Thomas Gleixner3b364d72016-02-09 20:11:40 +00008871 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008872 if (err)
8873 return err;
8874
Ingo Molnarc5905af2012-02-24 08:31:31 +01008875 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008876 event->destroy = sw_perf_event_destroy;
8877 }
8878
8879 return 0;
8880}
8881
8882static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02008883 .task_ctx_nr = perf_sw_context,
8884
Peter Zijlstra34f43922015-02-20 14:05:38 +01008885 .capabilities = PERF_PMU_CAP_NO_NMI,
8886
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008887 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008888 .add = perf_swevent_add,
8889 .del = perf_swevent_del,
8890 .start = perf_swevent_start,
8891 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008892 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008893};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02008894
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008895#ifdef CONFIG_EVENT_TRACING
8896
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008897static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02008898 struct perf_sample_data *data)
8899{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02008900 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02008901
Peter Zijlstrab71b4372015-11-02 10:50:51 +01008902 /* only top level events have filters set */
8903 if (event->parent)
8904 event = event->parent;
8905
Frederic Weisbecker95476b62010-04-14 23:42:18 +02008906 if (likely(!event->filter) || filter_match_preds(event->filter, record))
8907 return 1;
8908 return 0;
8909}
8910
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008911static int perf_tp_event_match(struct perf_event *event,
8912 struct perf_sample_data *data,
8913 struct pt_regs *regs)
8914{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01008915 if (event->hw.state & PERF_HES_STOPPED)
8916 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02008917 /*
Song Liu9fd2e482019-05-07 09:15:45 -07008918 * If exclude_kernel, only trace user-space tracepoints (uprobes)
Peter Zijlstra580d6072010-05-20 20:54:31 +02008919 */
Song Liu9fd2e482019-05-07 09:15:45 -07008920 if (event->attr.exclude_kernel && !user_mode(regs))
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008921 return 0;
8922
8923 if (!perf_tp_filter_match(event, data))
8924 return 0;
8925
8926 return 1;
8927}
8928
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07008929void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
8930 struct trace_event_call *call, u64 count,
8931 struct pt_regs *regs, struct hlist_head *head,
8932 struct task_struct *task)
8933{
Yonghong Songe87c6bc2017-10-23 23:53:08 -07008934 if (bpf_prog_array_valid(call)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07008935 *(struct pt_regs **)raw_data = regs;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07008936 if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07008937 perf_swevent_put_recursion_context(rctx);
8938 return;
8939 }
8940 }
8941 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02008942 rctx, task);
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07008943}
8944EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
8945
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07008946void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04008947 struct pt_regs *regs, struct hlist_head *head, int rctx,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02008948 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008949{
8950 struct perf_sample_data data;
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02008951 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008952
8953 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02008954 .frag = {
8955 .size = entry_size,
8956 .data = record,
8957 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008958 };
8959
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07008960 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008961 data.raw = &raw;
8962
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07008963 perf_trace_buf_update(record, event_type);
8964
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02008965 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008966 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008967 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008968 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02008969
Andrew Vagine6dab5f2012-07-11 18:14:58 +04008970 /*
8971 * If we got specified a target task, also iterate its context and
8972 * deliver this event there too.
8973 */
8974 if (task && task != current) {
8975 struct perf_event_context *ctx;
8976 struct trace_entry *entry = record;
8977
8978 rcu_read_lock();
8979 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
8980 if (!ctx)
8981 goto unlock;
8982
8983 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Jiri Olsacd6fb6772018-09-23 18:13:43 +02008984 if (event->cpu != smp_processor_id())
8985 continue;
Andrew Vagine6dab5f2012-07-11 18:14:58 +04008986 if (event->attr.type != PERF_TYPE_TRACEPOINT)
8987 continue;
8988 if (event->attr.config != entry->type)
8989 continue;
8990 if (perf_tp_event_match(event, &data, regs))
8991 perf_swevent_event(event, count, &data, regs);
8992 }
8993unlock:
8994 rcu_read_unlock();
8995 }
8996
Peter Zijlstraecc55f82010-05-21 15:11:34 +02008997 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02008998}
8999EXPORT_SYMBOL_GPL(perf_tp_event);
9000
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009001static void tp_perf_event_destroy(struct perf_event *event)
9002{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009003 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009004}
9005
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009006static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009007{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009008 int err;
9009
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009010 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9011 return -ENOENT;
9012
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009013 /*
9014 * no branch sampling for tracepoint events
9015 */
9016 if (has_branch_stack(event))
9017 return -EOPNOTSUPP;
9018
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009019 err = perf_trace_init(event);
9020 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009021 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009022
9023 event->destroy = tp_perf_event_destroy;
9024
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009025 return 0;
9026}
9027
9028static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009029 .task_ctx_nr = perf_sw_context,
9030
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009031 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009032 .add = perf_trace_add,
9033 .del = perf_trace_del,
9034 .start = perf_swevent_start,
9035 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009036 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009037};
9038
Song Liu33ea4b22017-12-06 14:45:16 -08009039#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Song Liue12f03d2017-12-06 14:45:15 -08009040/*
9041 * Flags in config, used by dynamic PMU kprobe and uprobe
9042 * The flags should match following PMU_FORMAT_ATTR().
9043 *
9044 * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe
9045 * if not set, create kprobe/uprobe
Song Liua6ca88b2018-10-01 22:36:36 -07009046 *
9047 * The following values specify a reference counter (or semaphore in the
9048 * terminology of tools like dtrace, systemtap, etc.) Userspace Statically
9049 * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset.
9050 *
9051 * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset
9052 * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left
Song Liue12f03d2017-12-06 14:45:15 -08009053 */
9054enum perf_probe_config {
9055 PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */
Song Liua6ca88b2018-10-01 22:36:36 -07009056 PERF_UPROBE_REF_CTR_OFFSET_BITS = 32,
9057 PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS,
Song Liue12f03d2017-12-06 14:45:15 -08009058};
9059
9060PMU_FORMAT_ATTR(retprobe, "config:0");
Song Liua6ca88b2018-10-01 22:36:36 -07009061#endif
Song Liue12f03d2017-12-06 14:45:15 -08009062
Song Liua6ca88b2018-10-01 22:36:36 -07009063#ifdef CONFIG_KPROBE_EVENTS
9064static struct attribute *kprobe_attrs[] = {
Song Liue12f03d2017-12-06 14:45:15 -08009065 &format_attr_retprobe.attr,
9066 NULL,
9067};
9068
Song Liua6ca88b2018-10-01 22:36:36 -07009069static struct attribute_group kprobe_format_group = {
Song Liue12f03d2017-12-06 14:45:15 -08009070 .name = "format",
Song Liua6ca88b2018-10-01 22:36:36 -07009071 .attrs = kprobe_attrs,
Song Liue12f03d2017-12-06 14:45:15 -08009072};
9073
Song Liua6ca88b2018-10-01 22:36:36 -07009074static const struct attribute_group *kprobe_attr_groups[] = {
9075 &kprobe_format_group,
Song Liue12f03d2017-12-06 14:45:15 -08009076 NULL,
9077};
9078
9079static int perf_kprobe_event_init(struct perf_event *event);
9080static struct pmu perf_kprobe = {
9081 .task_ctx_nr = perf_sw_context,
9082 .event_init = perf_kprobe_event_init,
9083 .add = perf_trace_add,
9084 .del = perf_trace_del,
9085 .start = perf_swevent_start,
9086 .stop = perf_swevent_stop,
9087 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009088 .attr_groups = kprobe_attr_groups,
Song Liue12f03d2017-12-06 14:45:15 -08009089};
9090
9091static int perf_kprobe_event_init(struct perf_event *event)
9092{
9093 int err;
9094 bool is_retprobe;
9095
9096 if (event->attr.type != perf_kprobe.type)
9097 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009098
9099 if (!capable(CAP_SYS_ADMIN))
9100 return -EACCES;
9101
Song Liue12f03d2017-12-06 14:45:15 -08009102 /*
9103 * no branch sampling for probe events
9104 */
9105 if (has_branch_stack(event))
9106 return -EOPNOTSUPP;
9107
9108 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
9109 err = perf_kprobe_init(event, is_retprobe);
9110 if (err)
9111 return err;
9112
9113 event->destroy = perf_kprobe_destroy;
9114
9115 return 0;
9116}
9117#endif /* CONFIG_KPROBE_EVENTS */
9118
Song Liu33ea4b22017-12-06 14:45:16 -08009119#ifdef CONFIG_UPROBE_EVENTS
Song Liua6ca88b2018-10-01 22:36:36 -07009120PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63");
9121
9122static struct attribute *uprobe_attrs[] = {
9123 &format_attr_retprobe.attr,
9124 &format_attr_ref_ctr_offset.attr,
9125 NULL,
9126};
9127
9128static struct attribute_group uprobe_format_group = {
9129 .name = "format",
9130 .attrs = uprobe_attrs,
9131};
9132
9133static const struct attribute_group *uprobe_attr_groups[] = {
9134 &uprobe_format_group,
9135 NULL,
9136};
9137
Song Liu33ea4b22017-12-06 14:45:16 -08009138static int perf_uprobe_event_init(struct perf_event *event);
9139static struct pmu perf_uprobe = {
9140 .task_ctx_nr = perf_sw_context,
9141 .event_init = perf_uprobe_event_init,
9142 .add = perf_trace_add,
9143 .del = perf_trace_del,
9144 .start = perf_swevent_start,
9145 .stop = perf_swevent_stop,
9146 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009147 .attr_groups = uprobe_attr_groups,
Song Liu33ea4b22017-12-06 14:45:16 -08009148};
9149
9150static int perf_uprobe_event_init(struct perf_event *event)
9151{
9152 int err;
Song Liua6ca88b2018-10-01 22:36:36 -07009153 unsigned long ref_ctr_offset;
Song Liu33ea4b22017-12-06 14:45:16 -08009154 bool is_retprobe;
9155
9156 if (event->attr.type != perf_uprobe.type)
9157 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009158
9159 if (!capable(CAP_SYS_ADMIN))
9160 return -EACCES;
9161
Song Liu33ea4b22017-12-06 14:45:16 -08009162 /*
9163 * no branch sampling for probe events
9164 */
9165 if (has_branch_stack(event))
9166 return -EOPNOTSUPP;
9167
9168 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
Song Liua6ca88b2018-10-01 22:36:36 -07009169 ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
9170 err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe);
Song Liu33ea4b22017-12-06 14:45:16 -08009171 if (err)
9172 return err;
9173
9174 event->destroy = perf_uprobe_destroy;
9175
9176 return 0;
9177}
9178#endif /* CONFIG_UPROBE_EVENTS */
9179
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009180static inline void perf_tp_register(void)
9181{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009182 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Song Liue12f03d2017-12-06 14:45:15 -08009183#ifdef CONFIG_KPROBE_EVENTS
9184 perf_pmu_register(&perf_kprobe, "kprobe", -1);
9185#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009186#ifdef CONFIG_UPROBE_EVENTS
9187 perf_pmu_register(&perf_uprobe, "uprobe", -1);
9188#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009189}
Li Zefan6fb29152009-10-15 11:21:42 +08009190
Li Zefan6fb29152009-10-15 11:21:42 +08009191static void perf_event_free_filter(struct perf_event *event)
9192{
9193 ftrace_profile_free_filter(event);
9194}
9195
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009196#ifdef CONFIG_BPF_SYSCALL
9197static void bpf_overflow_handler(struct perf_event *event,
9198 struct perf_sample_data *data,
9199 struct pt_regs *regs)
9200{
9201 struct bpf_perf_event_data_kern ctx = {
9202 .data = data,
Yonghong Song7d9285e2017-10-05 09:19:19 -07009203 .event = event,
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009204 };
9205 int ret = 0;
9206
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +01009207 ctx.regs = perf_arch_bpf_user_pt_regs(regs);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009208 preempt_disable();
9209 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
9210 goto out;
9211 rcu_read_lock();
Daniel Borkmann88575192016-11-26 01:28:04 +01009212 ret = BPF_PROG_RUN(event->prog, &ctx);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009213 rcu_read_unlock();
9214out:
9215 __this_cpu_dec(bpf_prog_active);
9216 preempt_enable();
9217 if (!ret)
9218 return;
9219
9220 event->orig_overflow_handler(event, data, regs);
9221}
9222
9223static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9224{
9225 struct bpf_prog *prog;
9226
9227 if (event->overflow_handler_context)
9228 /* hw breakpoint or kernel counter */
9229 return -EINVAL;
9230
9231 if (event->prog)
9232 return -EEXIST;
9233
9234 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
9235 if (IS_ERR(prog))
9236 return PTR_ERR(prog);
9237
9238 event->prog = prog;
9239 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
9240 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
9241 return 0;
9242}
9243
9244static void perf_event_free_bpf_handler(struct perf_event *event)
9245{
9246 struct bpf_prog *prog = event->prog;
9247
9248 if (!prog)
9249 return;
9250
9251 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
9252 event->prog = NULL;
9253 bpf_prog_put(prog);
9254}
9255#else
9256static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9257{
9258 return -EOPNOTSUPP;
9259}
9260static void perf_event_free_bpf_handler(struct perf_event *event)
9261{
9262}
9263#endif
9264
Song Liue12f03d2017-12-06 14:45:15 -08009265/*
9266 * returns true if the event is a tracepoint, or a kprobe/upprobe created
9267 * with perf_event_open()
9268 */
9269static inline bool perf_event_is_tracing(struct perf_event *event)
9270{
9271 if (event->pmu == &perf_tracepoint)
9272 return true;
9273#ifdef CONFIG_KPROBE_EVENTS
9274 if (event->pmu == &perf_kprobe)
9275 return true;
9276#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009277#ifdef CONFIG_UPROBE_EVENTS
9278 if (event->pmu == &perf_uprobe)
9279 return true;
9280#endif
Song Liue12f03d2017-12-06 14:45:15 -08009281 return false;
9282}
9283
Alexei Starovoitov25415172015-03-25 12:49:20 -07009284static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9285{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009286 bool is_kprobe, is_tracepoint, is_syscall_tp;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009287 struct bpf_prog *prog;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009288 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009289
Song Liue12f03d2017-12-06 14:45:15 -08009290 if (!perf_event_is_tracing(event))
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07009291 return perf_event_set_bpf_handler(event, prog_fd);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009292
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009293 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
9294 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009295 is_syscall_tp = is_syscall_trace_event(event->tp_event);
9296 if (!is_kprobe && !is_tracepoint && !is_syscall_tp)
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009297 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07009298 return -EINVAL;
9299
9300 prog = bpf_prog_get(prog_fd);
9301 if (IS_ERR(prog))
9302 return PTR_ERR(prog);
9303
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009304 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009305 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
9306 (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07009307 /* valid fd, but invalid bpf program type */
9308 bpf_prog_put(prog);
9309 return -EINVAL;
9310 }
9311
Josef Bacik9802d862017-12-11 11:36:48 -05009312 /* Kprobe override only works for kprobes, not uprobes. */
9313 if (prog->kprobe_override &&
9314 !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) {
9315 bpf_prog_put(prog);
9316 return -EINVAL;
9317 }
9318
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009319 if (is_tracepoint || is_syscall_tp) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07009320 int off = trace_event_get_offsets(event->tp_event);
9321
9322 if (prog->aux->max_ctx_offset > off) {
9323 bpf_prog_put(prog);
9324 return -EACCES;
9325 }
9326 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07009327
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009328 ret = perf_event_attach_bpf_prog(event, prog);
9329 if (ret)
9330 bpf_prog_put(prog);
9331 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009332}
9333
9334static void perf_event_free_bpf_prog(struct perf_event *event)
9335{
Song Liue12f03d2017-12-06 14:45:15 -08009336 if (!perf_event_is_tracing(event)) {
Yonghong Song0b4c6842017-10-23 23:53:07 -07009337 perf_event_free_bpf_handler(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009338 return;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009339 }
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009340 perf_event_detach_bpf_prog(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009341}
9342
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009343#else
Li Zefan6fb29152009-10-15 11:21:42 +08009344
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009345static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009346{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009347}
Li Zefan6fb29152009-10-15 11:21:42 +08009348
Li Zefan6fb29152009-10-15 11:21:42 +08009349static void perf_event_free_filter(struct perf_event *event)
9350{
9351}
9352
Alexei Starovoitov25415172015-03-25 12:49:20 -07009353static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9354{
9355 return -ENOENT;
9356}
9357
9358static void perf_event_free_bpf_prog(struct perf_event *event)
9359{
9360}
Li Zefan07b139c2009-12-21 14:27:35 +08009361#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009362
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009363#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009364void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009365{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009366 struct perf_sample_data sample;
9367 struct pt_regs *regs = data;
9368
Robert Richterfd0d0002012-04-02 20:19:08 +02009369 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009370
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009371 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009372 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009373}
9374#endif
9375
Alexander Shishkin375637b2016-04-27 18:44:46 +03009376/*
9377 * Allocate a new address filter
9378 */
9379static struct perf_addr_filter *
9380perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
9381{
9382 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
9383 struct perf_addr_filter *filter;
9384
9385 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
9386 if (!filter)
9387 return NULL;
9388
9389 INIT_LIST_HEAD(&filter->entry);
9390 list_add_tail(&filter->entry, filters);
9391
9392 return filter;
9393}
9394
9395static void free_filters_list(struct list_head *filters)
9396{
9397 struct perf_addr_filter *filter, *iter;
9398
9399 list_for_each_entry_safe(filter, iter, filters, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07009400 path_put(&filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009401 list_del(&filter->entry);
9402 kfree(filter);
9403 }
9404}
9405
9406/*
9407 * Free existing address filters and optionally install new ones
9408 */
9409static void perf_addr_filters_splice(struct perf_event *event,
9410 struct list_head *head)
9411{
9412 unsigned long flags;
9413 LIST_HEAD(list);
9414
9415 if (!has_addr_filter(event))
9416 return;
9417
9418 /* don't bother with children, they don't have their own filters */
9419 if (event->parent)
9420 return;
9421
9422 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
9423
9424 list_splice_init(&event->addr_filters.list, &list);
9425 if (head)
9426 list_splice(head, &event->addr_filters.list);
9427
9428 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
9429
9430 free_filters_list(&list);
9431}
9432
9433/*
9434 * Scan through mm's vmas and see if one of them matches the
9435 * @filter; if so, adjust filter's address range.
9436 * Called with mm::mmap_sem down for reading.
9437 */
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009438static void perf_addr_filter_apply(struct perf_addr_filter *filter,
9439 struct mm_struct *mm,
9440 struct perf_addr_filter_range *fr)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009441{
9442 struct vm_area_struct *vma;
9443
9444 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009445 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009446 continue;
9447
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009448 if (perf_addr_filter_vma_adjust(filter, vma, fr))
9449 return;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009450 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009451}
9452
9453/*
9454 * Update event's address range filters based on the
9455 * task's existing mappings, if any.
9456 */
9457static void perf_event_addr_filters_apply(struct perf_event *event)
9458{
9459 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
9460 struct task_struct *task = READ_ONCE(event->ctx->task);
9461 struct perf_addr_filter *filter;
9462 struct mm_struct *mm = NULL;
9463 unsigned int count = 0;
9464 unsigned long flags;
9465
9466 /*
9467 * We may observe TASK_TOMBSTONE, which means that the event tear-down
9468 * will stop on the parent's child_mutex that our caller is also holding
9469 */
9470 if (task == TASK_TOMBSTONE)
9471 return;
9472
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009473 if (ifh->nr_file_filters) {
9474 mm = get_task_mm(event->ctx->task);
9475 if (!mm)
9476 goto restart;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009477
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009478 down_read(&mm->mmap_sem);
9479 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009480
9481 raw_spin_lock_irqsave(&ifh->lock, flags);
9482 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009483 if (filter->path.dentry) {
9484 /*
9485 * Adjust base offset if the filter is associated to a
9486 * binary that needs to be mapped:
9487 */
9488 event->addr_filter_ranges[count].start = 0;
9489 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009490
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009491 perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009492 } else {
9493 event->addr_filter_ranges[count].start = filter->offset;
9494 event->addr_filter_ranges[count].size = filter->size;
9495 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009496
9497 count++;
9498 }
9499
9500 event->addr_filters_gen++;
9501 raw_spin_unlock_irqrestore(&ifh->lock, flags);
9502
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009503 if (ifh->nr_file_filters) {
9504 up_read(&mm->mmap_sem);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009505
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009506 mmput(mm);
9507 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009508
9509restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03009510 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009511}
9512
9513/*
9514 * Address range filtering: limiting the data to certain
9515 * instruction address ranges. Filters are ioctl()ed to us from
9516 * userspace as ascii strings.
9517 *
9518 * Filter string format:
9519 *
9520 * ACTION RANGE_SPEC
9521 * where ACTION is one of the
9522 * * "filter": limit the trace to this region
9523 * * "start": start tracing from this address
9524 * * "stop": stop tracing at this address/region;
9525 * RANGE_SPEC is
9526 * * for kernel addresses: <start address>[/<size>]
9527 * * for object files: <start address>[/<size>]@</path/to/object/file>
9528 *
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009529 * if <size> is not specified or is zero, the range is treated as a single
9530 * address; not valid for ACTION=="filter".
Alexander Shishkin375637b2016-04-27 18:44:46 +03009531 */
9532enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02009533 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03009534 IF_ACT_FILTER,
9535 IF_ACT_START,
9536 IF_ACT_STOP,
9537 IF_SRC_FILE,
9538 IF_SRC_KERNEL,
9539 IF_SRC_FILEADDR,
9540 IF_SRC_KERNELADDR,
9541};
9542
9543enum {
9544 IF_STATE_ACTION = 0,
9545 IF_STATE_SOURCE,
9546 IF_STATE_END,
9547};
9548
9549static const match_table_t if_tokens = {
9550 { IF_ACT_FILTER, "filter" },
9551 { IF_ACT_START, "start" },
9552 { IF_ACT_STOP, "stop" },
9553 { IF_SRC_FILE, "%u/%u@%s" },
9554 { IF_SRC_KERNEL, "%u/%u" },
9555 { IF_SRC_FILEADDR, "%u@%s" },
9556 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02009557 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03009558};
9559
9560/*
9561 * Address filter string parser
9562 */
9563static int
9564perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
9565 struct list_head *filters)
9566{
9567 struct perf_addr_filter *filter = NULL;
9568 char *start, *orig, *filename = NULL;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009569 substring_t args[MAX_OPT_ARGS];
9570 int state = IF_STATE_ACTION, token;
9571 unsigned int kernel = 0;
9572 int ret = -EINVAL;
9573
9574 orig = fstr = kstrdup(fstr, GFP_KERNEL);
9575 if (!fstr)
9576 return -ENOMEM;
9577
9578 while ((start = strsep(&fstr, " ,\n")) != NULL) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009579 static const enum perf_addr_filter_action_t actions[] = {
9580 [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER,
9581 [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START,
9582 [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP,
9583 };
Alexander Shishkin375637b2016-04-27 18:44:46 +03009584 ret = -EINVAL;
9585
9586 if (!*start)
9587 continue;
9588
9589 /* filter definition begins */
9590 if (state == IF_STATE_ACTION) {
9591 filter = perf_addr_filter_new(event, filters);
9592 if (!filter)
9593 goto fail;
9594 }
9595
9596 token = match_token(start, if_tokens, args);
9597 switch (token) {
9598 case IF_ACT_FILTER:
9599 case IF_ACT_START:
Alexander Shishkin375637b2016-04-27 18:44:46 +03009600 case IF_ACT_STOP:
9601 if (state != IF_STATE_ACTION)
9602 goto fail;
9603
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009604 filter->action = actions[token];
Alexander Shishkin375637b2016-04-27 18:44:46 +03009605 state = IF_STATE_SOURCE;
9606 break;
9607
9608 case IF_SRC_KERNELADDR:
9609 case IF_SRC_KERNEL:
9610 kernel = 1;
Gustavo A. R. Silva10c34052019-02-12 14:54:30 -06009611 /* fall through */
Alexander Shishkin375637b2016-04-27 18:44:46 +03009612
9613 case IF_SRC_FILEADDR:
9614 case IF_SRC_FILE:
9615 if (state != IF_STATE_SOURCE)
9616 goto fail;
9617
Alexander Shishkin375637b2016-04-27 18:44:46 +03009618 *args[0].to = 0;
9619 ret = kstrtoul(args[0].from, 0, &filter->offset);
9620 if (ret)
9621 goto fail;
9622
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009623 if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) {
Alexander Shishkin375637b2016-04-27 18:44:46 +03009624 *args[1].to = 0;
9625 ret = kstrtoul(args[1].from, 0, &filter->size);
9626 if (ret)
9627 goto fail;
9628 }
9629
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06009630 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009631 int fpos = token == IF_SRC_FILE ? 2 : 1;
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06009632
9633 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009634 if (!filename) {
9635 ret = -ENOMEM;
9636 goto fail;
9637 }
9638 }
9639
9640 state = IF_STATE_END;
9641 break;
9642
9643 default:
9644 goto fail;
9645 }
9646
9647 /*
9648 * Filter definition is fully parsed, validate and install it.
9649 * Make sure that it doesn't contradict itself or the event's
9650 * attribute.
9651 */
9652 if (state == IF_STATE_END) {
Alexander Shishkin9ccbfbb2017-01-26 11:40:56 +02009653 ret = -EINVAL;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009654 if (kernel && event->attr.exclude_kernel)
9655 goto fail;
9656
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009657 /*
9658 * ACTION "filter" must have a non-zero length region
9659 * specified.
9660 */
9661 if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER &&
9662 !filter->size)
9663 goto fail;
9664
Alexander Shishkin375637b2016-04-27 18:44:46 +03009665 if (!kernel) {
9666 if (!filename)
9667 goto fail;
9668
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009669 /*
9670 * For now, we only support file-based filters
9671 * in per-task events; doing so for CPU-wide
9672 * events requires additional context switching
9673 * trickery, since same object code will be
9674 * mapped at different virtual addresses in
9675 * different processes.
9676 */
9677 ret = -EOPNOTSUPP;
9678 if (!event->ctx->task)
9679 goto fail_free_name;
9680
Alexander Shishkin375637b2016-04-27 18:44:46 +03009681 /* look up the path and grab its inode */
Song Liu9511bce2018-04-17 23:29:07 -07009682 ret = kern_path(filename, LOOKUP_FOLLOW,
9683 &filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009684 if (ret)
9685 goto fail_free_name;
9686
Alexander Shishkin375637b2016-04-27 18:44:46 +03009687 kfree(filename);
9688 filename = NULL;
9689
9690 ret = -EINVAL;
Song Liu9511bce2018-04-17 23:29:07 -07009691 if (!filter->path.dentry ||
9692 !S_ISREG(d_inode(filter->path.dentry)
9693 ->i_mode))
Alexander Shishkin375637b2016-04-27 18:44:46 +03009694 goto fail;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009695
9696 event->addr_filters.nr_file_filters++;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009697 }
9698
9699 /* ready to consume more filters */
9700 state = IF_STATE_ACTION;
9701 filter = NULL;
9702 }
9703 }
9704
9705 if (state != IF_STATE_ACTION)
9706 goto fail;
9707
9708 kfree(orig);
9709
9710 return 0;
9711
9712fail_free_name:
9713 kfree(filename);
9714fail:
9715 free_filters_list(filters);
9716 kfree(orig);
9717
9718 return ret;
9719}
9720
9721static int
9722perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
9723{
9724 LIST_HEAD(filters);
9725 int ret;
9726
9727 /*
9728 * Since this is called in perf_ioctl() path, we're already holding
9729 * ctx::mutex.
9730 */
9731 lockdep_assert_held(&event->ctx->mutex);
9732
9733 if (WARN_ON_ONCE(event->parent))
9734 return -EINVAL;
9735
Alexander Shishkin375637b2016-04-27 18:44:46 +03009736 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
9737 if (ret)
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009738 goto fail_clear_files;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009739
9740 ret = event->pmu->addr_filters_validate(&filters);
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009741 if (ret)
9742 goto fail_free_filters;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009743
9744 /* remove existing filters, if any */
9745 perf_addr_filters_splice(event, &filters);
9746
9747 /* install new filters */
9748 perf_event_for_each_child(event, perf_event_addr_filters_apply);
9749
9750 return ret;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009751
9752fail_free_filters:
9753 free_filters_list(&filters);
9754
9755fail_clear_files:
9756 event->addr_filters.nr_file_filters = 0;
9757
9758 return ret;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009759}
9760
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03009761static int perf_event_set_filter(struct perf_event *event, void __user *arg)
9762{
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03009763 int ret = -EINVAL;
Song Liue12f03d2017-12-06 14:45:15 -08009764 char *filter_str;
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03009765
9766 filter_str = strndup_user(arg, PAGE_SIZE);
9767 if (IS_ERR(filter_str))
9768 return PTR_ERR(filter_str);
9769
Song Liue12f03d2017-12-06 14:45:15 -08009770#ifdef CONFIG_EVENT_TRACING
9771 if (perf_event_is_tracing(event)) {
9772 struct perf_event_context *ctx = event->ctx;
9773
9774 /*
9775 * Beware, here be dragons!!
9776 *
9777 * the tracepoint muck will deadlock against ctx->mutex, but
9778 * the tracepoint stuff does not actually need it. So
9779 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
9780 * already have a reference on ctx.
9781 *
9782 * This can result in event getting moved to a different ctx,
9783 * but that does not affect the tracepoint state.
9784 */
9785 mutex_unlock(&ctx->mutex);
9786 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
9787 mutex_lock(&ctx->mutex);
9788 } else
9789#endif
9790 if (has_addr_filter(event))
Alexander Shishkin375637b2016-04-27 18:44:46 +03009791 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +03009792
9793 kfree(filter_str);
9794 return ret;
9795}
9796
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009797/*
9798 * hrtimer based swevent callback
9799 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009800
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009801static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009802{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009803 enum hrtimer_restart ret = HRTIMER_RESTART;
9804 struct perf_sample_data data;
9805 struct pt_regs *regs;
9806 struct perf_event *event;
9807 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009808
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009809 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01009810
9811 if (event->state != PERF_EVENT_STATE_ACTIVE)
9812 return HRTIMER_NORESTART;
9813
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009814 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009815
Robert Richterfd0d0002012-04-02 20:19:08 +02009816 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009817 regs = get_irq_regs();
9818
9819 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08009820 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02009821 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009822 ret = HRTIMER_NORESTART;
9823 }
9824
9825 period = max_t(u64, 10000, event->hw.sample_period);
9826 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
9827
9828 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009829}
9830
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009831static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009832{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009833 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01009834 s64 period;
9835
9836 if (!is_sampling_event(event))
9837 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009838
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01009839 period = local64_read(&hwc->period_left);
9840 if (period) {
9841 if (period < 0)
9842 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02009843
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01009844 local64_set(&hwc->period_left, 0);
9845 } else {
9846 period = max_t(u64, 10000, hwc->sample_period);
9847 }
Thomas Gleixner3497d202015-04-14 21:09:03 +00009848 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02009849 HRTIMER_MODE_REL_PINNED_HARD);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009850}
9851
9852static void perf_swevent_cancel_hrtimer(struct perf_event *event)
9853{
9854 struct hw_perf_event *hwc = &event->hw;
9855
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01009856 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009857 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02009858 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009859
9860 hrtimer_cancel(&hwc->hrtimer);
9861 }
9862}
9863
Peter Zijlstraba3dd362011-02-15 12:41:46 +01009864static void perf_swevent_init_hrtimer(struct perf_event *event)
9865{
9866 struct hw_perf_event *hwc = &event->hw;
9867
9868 if (!is_sampling_event(event))
9869 return;
9870
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02009871 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01009872 hwc->hrtimer.function = perf_swevent_hrtimer;
9873
9874 /*
9875 * Since hrtimers have a fixed rate, we can do a static freq->period
9876 * mapping and avoid the whole period adjust feedback stuff.
9877 */
9878 if (event->attr.freq) {
9879 long freq = event->attr.sample_freq;
9880
9881 event->attr.sample_period = NSEC_PER_SEC / freq;
9882 hwc->sample_period = event->attr.sample_period;
9883 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09009884 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01009885 event->attr.freq = 0;
9886 }
9887}
9888
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009889/*
9890 * Software event: cpu wall time clock
9891 */
9892
9893static void cpu_clock_event_update(struct perf_event *event)
9894{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009895 s64 prev;
9896 u64 now;
9897
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009898 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009899 prev = local64_xchg(&event->hw.prev_count, now);
9900 local64_add(now - prev, &event->count);
9901}
9902
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009903static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009904{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009905 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009906 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009907}
9908
9909static void cpu_clock_event_stop(struct perf_event *event, int flags)
9910{
9911 perf_swevent_cancel_hrtimer(event);
9912 cpu_clock_event_update(event);
9913}
9914
9915static int cpu_clock_event_add(struct perf_event *event, int flags)
9916{
9917 if (flags & PERF_EF_START)
9918 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08009919 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009920
9921 return 0;
9922}
9923
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009924static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009925{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009926 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009927}
9928
9929static void cpu_clock_event_read(struct perf_event *event)
9930{
9931 cpu_clock_event_update(event);
9932}
9933
9934static int cpu_clock_event_init(struct perf_event *event)
9935{
9936 if (event->attr.type != PERF_TYPE_SOFTWARE)
9937 return -ENOENT;
9938
9939 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
9940 return -ENOENT;
9941
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009942 /*
9943 * no branch sampling for software events
9944 */
9945 if (has_branch_stack(event))
9946 return -EOPNOTSUPP;
9947
Peter Zijlstraba3dd362011-02-15 12:41:46 +01009948 perf_swevent_init_hrtimer(event);
9949
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009950 return 0;
9951}
9952
9953static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009954 .task_ctx_nr = perf_sw_context,
9955
Peter Zijlstra34f43922015-02-20 14:05:38 +01009956 .capabilities = PERF_PMU_CAP_NO_NMI,
9957
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009958 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009959 .add = cpu_clock_event_add,
9960 .del = cpu_clock_event_del,
9961 .start = cpu_clock_event_start,
9962 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009963 .read = cpu_clock_event_read,
9964};
9965
9966/*
9967 * Software event: task time clock
9968 */
9969
9970static void task_clock_event_update(struct perf_event *event, u64 now)
9971{
9972 u64 prev;
9973 s64 delta;
9974
9975 prev = local64_xchg(&event->hw.prev_count, now);
9976 delta = now - prev;
9977 local64_add(delta, &event->count);
9978}
9979
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009980static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009981{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009982 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009983 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009984}
9985
9986static void task_clock_event_stop(struct perf_event *event, int flags)
9987{
9988 perf_swevent_cancel_hrtimer(event);
9989 task_clock_event_update(event, event->ctx->time);
9990}
9991
9992static int task_clock_event_add(struct perf_event *event, int flags)
9993{
9994 if (flags & PERF_EF_START)
9995 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08009996 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009997
9998 return 0;
9999}
10000
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010001static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010002{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010003 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010004}
10005
10006static void task_clock_event_read(struct perf_event *event)
10007{
Peter Zijlstra768a06e2011-02-22 16:52:24 +010010008 u64 now = perf_clock();
10009 u64 delta = now - event->ctx->timestamp;
10010 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010011
10012 task_clock_event_update(event, time);
10013}
10014
10015static int task_clock_event_init(struct perf_event *event)
10016{
10017 if (event->attr.type != PERF_TYPE_SOFTWARE)
10018 return -ENOENT;
10019
10020 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
10021 return -ENOENT;
10022
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010023 /*
10024 * no branch sampling for software events
10025 */
10026 if (has_branch_stack(event))
10027 return -EOPNOTSUPP;
10028
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010029 perf_swevent_init_hrtimer(event);
10030
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010031 return 0;
10032}
10033
10034static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010035 .task_ctx_nr = perf_sw_context,
10036
Peter Zijlstra34f43922015-02-20 14:05:38 +010010037 .capabilities = PERF_PMU_CAP_NO_NMI,
10038
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010039 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010040 .add = task_clock_event_add,
10041 .del = task_clock_event_del,
10042 .start = task_clock_event_start,
10043 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010044 .read = task_clock_event_read,
10045};
10046
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010047static void perf_pmu_nop_void(struct pmu *pmu)
10048{
10049}
10050
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010051static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
10052{
10053}
10054
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010055static int perf_pmu_nop_int(struct pmu *pmu)
10056{
10057 return 0;
10058}
10059
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010060static int perf_event_nop_int(struct perf_event *event, u64 value)
10061{
10062 return 0;
10063}
10064
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010065static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010066
10067static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010068{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010069 __this_cpu_write(nop_txn_flags, flags);
10070
10071 if (flags & ~PERF_PMU_TXN_ADD)
10072 return;
10073
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010074 perf_pmu_disable(pmu);
10075}
10076
10077static int perf_pmu_commit_txn(struct pmu *pmu)
10078{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010079 unsigned int flags = __this_cpu_read(nop_txn_flags);
10080
10081 __this_cpu_write(nop_txn_flags, 0);
10082
10083 if (flags & ~PERF_PMU_TXN_ADD)
10084 return 0;
10085
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010086 perf_pmu_enable(pmu);
10087 return 0;
10088}
10089
10090static void perf_pmu_cancel_txn(struct pmu *pmu)
10091{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010092 unsigned int flags = __this_cpu_read(nop_txn_flags);
10093
10094 __this_cpu_write(nop_txn_flags, 0);
10095
10096 if (flags & ~PERF_PMU_TXN_ADD)
10097 return;
10098
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010099 perf_pmu_enable(pmu);
10100}
10101
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010102static int perf_event_idx_default(struct perf_event *event)
10103{
Peter Zijlstrac719f562014-10-21 11:10:21 +020010104 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010105}
10106
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010107/*
10108 * Ensures all contexts with the same task_ctx_nr have the same
10109 * pmu_cpu_context too.
10110 */
Mark Rutland9e317042014-02-10 17:44:18 +000010111static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010112{
10113 struct pmu *pmu;
10114
10115 if (ctxn < 0)
10116 return NULL;
10117
10118 list_for_each_entry(pmu, &pmus, entry) {
10119 if (pmu->task_ctx_nr == ctxn)
10120 return pmu->pmu_cpu_context;
10121 }
10122
10123 return NULL;
10124}
10125
Peter Zijlstra51676952010-12-07 14:18:20 +010010126static void free_pmu_context(struct pmu *pmu)
10127{
Will Deacondf0062b2017-10-03 15:20:50 +010010128 /*
10129 * Static contexts such as perf_sw_context have a global lifetime
10130 * and may be shared between different PMUs. Avoid freeing them
10131 * when a single PMU is going away.
10132 */
10133 if (pmu->task_ctx_nr > perf_invalid_context)
10134 return;
10135
Peter Zijlstra51676952010-12-07 14:18:20 +010010136 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010137}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010138
10139/*
10140 * Let userspace know that this PMU supports address range filtering:
10141 */
10142static ssize_t nr_addr_filters_show(struct device *dev,
10143 struct device_attribute *attr,
10144 char *page)
10145{
10146 struct pmu *pmu = dev_get_drvdata(dev);
10147
10148 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
10149}
10150DEVICE_ATTR_RO(nr_addr_filters);
10151
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010152static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010153
Peter Zijlstraabe43402010-11-17 23:17:37 +010010154static ssize_t
10155type_show(struct device *dev, struct device_attribute *attr, char *page)
10156{
10157 struct pmu *pmu = dev_get_drvdata(dev);
10158
10159 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
10160}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010161static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010162
Stephane Eranian62b85632013-04-03 14:21:34 +020010163static ssize_t
10164perf_event_mux_interval_ms_show(struct device *dev,
10165 struct device_attribute *attr,
10166 char *page)
10167{
10168 struct pmu *pmu = dev_get_drvdata(dev);
10169
10170 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
10171}
10172
Peter Zijlstra272325c2015-04-15 11:41:58 +020010173static DEFINE_MUTEX(mux_interval_mutex);
10174
Stephane Eranian62b85632013-04-03 14:21:34 +020010175static ssize_t
10176perf_event_mux_interval_ms_store(struct device *dev,
10177 struct device_attribute *attr,
10178 const char *buf, size_t count)
10179{
10180 struct pmu *pmu = dev_get_drvdata(dev);
10181 int timer, cpu, ret;
10182
10183 ret = kstrtoint(buf, 0, &timer);
10184 if (ret)
10185 return ret;
10186
10187 if (timer < 1)
10188 return -EINVAL;
10189
10190 /* same value, noting to do */
10191 if (timer == pmu->hrtimer_interval_ms)
10192 return count;
10193
Peter Zijlstra272325c2015-04-15 11:41:58 +020010194 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010195 pmu->hrtimer_interval_ms = timer;
10196
10197 /* update all cpuctx for this PMU */
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010198 cpus_read_lock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010199 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +020010200 struct perf_cpu_context *cpuctx;
10201 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
10202 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
10203
Peter Zijlstra272325c2015-04-15 11:41:58 +020010204 cpu_function_call(cpu,
10205 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +020010206 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010207 cpus_read_unlock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010208 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010209
10210 return count;
10211}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010212static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +020010213
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010214static struct attribute *pmu_dev_attrs[] = {
10215 &dev_attr_type.attr,
10216 &dev_attr_perf_event_mux_interval_ms.attr,
10217 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010218};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010219ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010220
10221static int pmu_bus_running;
10222static struct bus_type pmu_bus = {
10223 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010224 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010225};
10226
10227static void pmu_dev_release(struct device *dev)
10228{
10229 kfree(dev);
10230}
10231
10232static int pmu_dev_alloc(struct pmu *pmu)
10233{
10234 int ret = -ENOMEM;
10235
10236 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
10237 if (!pmu->dev)
10238 goto out;
10239
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +010010240 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +010010241 device_initialize(pmu->dev);
10242 ret = dev_set_name(pmu->dev, "%s", pmu->name);
10243 if (ret)
10244 goto free_dev;
10245
10246 dev_set_drvdata(pmu->dev, pmu);
10247 pmu->dev->bus = &pmu_bus;
10248 pmu->dev->release = pmu_dev_release;
10249 ret = device_add(pmu->dev);
10250 if (ret)
10251 goto free_dev;
10252
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010253 /* For PMUs with address filters, throw in an extra attribute: */
10254 if (pmu->nr_addr_filters)
10255 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
10256
10257 if (ret)
10258 goto del_dev;
10259
Jiri Olsaf3a3a822019-05-12 17:55:11 +020010260 if (pmu->attr_update)
10261 ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update);
10262
10263 if (ret)
10264 goto del_dev;
10265
Peter Zijlstraabe43402010-11-17 23:17:37 +010010266out:
10267 return ret;
10268
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010269del_dev:
10270 device_del(pmu->dev);
10271
Peter Zijlstraabe43402010-11-17 23:17:37 +010010272free_dev:
10273 put_device(pmu->dev);
10274 goto out;
10275}
10276
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010277static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010278static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010279
Mischa Jonker03d8e802013-06-04 11:45:48 +020010280int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010281{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010282 int cpu, ret, max = PERF_TYPE_MAX;
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010283
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010284 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010285 ret = -ENOMEM;
10286 pmu->pmu_disable_count = alloc_percpu(int);
10287 if (!pmu->pmu_disable_count)
10288 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010289
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010290 pmu->type = -1;
10291 if (!name)
10292 goto skip_type;
10293 pmu->name = name;
10294
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010295 if (type != PERF_TYPE_SOFTWARE) {
10296 if (type >= 0)
10297 max = type;
10298
10299 ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
10300 if (ret < 0)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010301 goto free_pdc;
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010302
10303 WARN_ON(type >= 0 && ret != type);
10304
10305 type = ret;
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010306 }
10307 pmu->type = type;
10308
Peter Zijlstraabe43402010-11-17 23:17:37 +010010309 if (pmu_bus_running) {
10310 ret = pmu_dev_alloc(pmu);
10311 if (ret)
10312 goto free_idr;
10313 }
10314
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010315skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +010010316 if (pmu->task_ctx_nr == perf_hw_context) {
10317 static int hw_context_taken = 0;
10318
Mark Rutland5101ef22016-04-26 11:33:46 +010010319 /*
10320 * Other than systems with heterogeneous CPUs, it never makes
10321 * sense for two PMUs to share perf_hw_context. PMUs which are
10322 * uncore must use perf_invalid_context.
10323 */
10324 if (WARN_ON_ONCE(hw_context_taken &&
10325 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +010010326 pmu->task_ctx_nr = perf_invalid_context;
10327
10328 hw_context_taken = 1;
10329 }
10330
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010331 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
10332 if (pmu->pmu_cpu_context)
10333 goto got_cpu_context;
10334
Wei Yongjunc4814202013-04-12 11:05:54 +080010335 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010336 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
10337 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +010010338 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010339
10340 for_each_possible_cpu(cpu) {
10341 struct perf_cpu_context *cpuctx;
10342
10343 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +020010344 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010345 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010346 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010347 cpuctx->ctx.pmu = pmu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010348 cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);
Stephane Eranian9e630202013-04-03 14:21:33 +020010349
Peter Zijlstra272325c2015-04-15 11:41:58 +020010350 __perf_mux_hrtimer_init(cpuctx, cpu);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010351 }
10352
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010353got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010354 if (!pmu->start_txn) {
10355 if (pmu->pmu_enable) {
10356 /*
10357 * If we have pmu_enable/pmu_disable calls, install
10358 * transaction stubs that use that to try and batch
10359 * hardware accesses.
10360 */
10361 pmu->start_txn = perf_pmu_start_txn;
10362 pmu->commit_txn = perf_pmu_commit_txn;
10363 pmu->cancel_txn = perf_pmu_cancel_txn;
10364 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010365 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010366 pmu->commit_txn = perf_pmu_nop_int;
10367 pmu->cancel_txn = perf_pmu_nop_void;
10368 }
10369 }
10370
10371 if (!pmu->pmu_enable) {
10372 pmu->pmu_enable = perf_pmu_nop_void;
10373 pmu->pmu_disable = perf_pmu_nop_void;
10374 }
10375
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010376 if (!pmu->check_period)
10377 pmu->check_period = perf_event_nop_int;
10378
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010379 if (!pmu->event_idx)
10380 pmu->event_idx = perf_event_idx_default;
10381
Liang, Kand44f8212019-10-22 11:13:09 +020010382 /*
10383 * Ensure the TYPE_SOFTWARE PMUs are at the head of the list,
10384 * since these cannot be in the IDR. This way the linear search
10385 * is fast, provided a valid software event is provided.
10386 */
10387 if (type == PERF_TYPE_SOFTWARE || !name)
10388 list_add_rcu(&pmu->entry, &pmus);
10389 else
10390 list_add_tail_rcu(&pmu->entry, &pmus);
10391
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010392 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010393 ret = 0;
10394unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010395 mutex_unlock(&pmus_lock);
10396
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010397 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010398
Peter Zijlstraabe43402010-11-17 23:17:37 +010010399free_dev:
10400 device_del(pmu->dev);
10401 put_device(pmu->dev);
10402
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010403free_idr:
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010404 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010405 idr_remove(&pmu_idr, pmu->type);
10406
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010407free_pdc:
10408 free_percpu(pmu->pmu_disable_count);
10409 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010410}
Yan, Zhengc464c762014-03-18 16:56:41 +080010411EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010412
10413void perf_pmu_unregister(struct pmu *pmu)
10414{
10415 mutex_lock(&pmus_lock);
10416 list_del_rcu(&pmu->entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010417
10418 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +020010419 * We dereference the pmu list under both SRCU and regular RCU, so
10420 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010421 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010422 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +020010423 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010424
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010425 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010426 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010427 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010428 if (pmu_bus_running) {
Jiri Olsa09338402016-10-20 13:10:11 +020010429 if (pmu->nr_addr_filters)
10430 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
10431 device_del(pmu->dev);
10432 put_device(pmu->dev);
10433 }
Peter Zijlstra51676952010-12-07 14:18:20 +010010434 free_pmu_context(pmu);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010435 mutex_unlock(&pmus_lock);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010436}
Yan, Zhengc464c762014-03-18 16:56:41 +080010437EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010438
Kan Liange321d022019-05-28 15:08:30 -070010439static inline bool has_extended_regs(struct perf_event *event)
10440{
10441 return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) ||
10442 (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK);
10443}
10444
Mark Rutlandcc34b982015-01-07 14:56:51 +000010445static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
10446{
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010447 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +000010448 int ret;
10449
10450 if (!try_module_get(pmu->module))
10451 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010452
Peter Zijlstra0c7296c2018-01-09 21:23:02 +010010453 /*
10454 * A number of pmu->event_init() methods iterate the sibling_list to,
10455 * for example, validate if the group fits on the PMU. Therefore,
10456 * if this is a sibling event, acquire the ctx->mutex to protect
10457 * the sibling_list.
10458 */
10459 if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +020010460 /*
10461 * This ctx->mutex can nest when we're called through
10462 * inheritance. See the perf_event_ctx_lock_nested() comment.
10463 */
10464 ctx = perf_event_ctx_lock_nested(event->group_leader,
10465 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010466 BUG_ON(!ctx);
10467 }
10468
Mark Rutlandcc34b982015-01-07 14:56:51 +000010469 event->pmu = pmu;
10470 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010471
10472 if (ctx)
10473 perf_event_ctx_unlock(event->group_leader, ctx);
10474
Andrew Murraycc6795a2019-01-10 13:53:25 +000010475 if (!ret) {
Kan Liange321d022019-05-28 15:08:30 -070010476 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
10477 has_extended_regs(event))
10478 ret = -EOPNOTSUPP;
10479
Andrew Murraycc6795a2019-01-10 13:53:25 +000010480 if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
Kan Liange321d022019-05-28 15:08:30 -070010481 event_has_any_exclude_flag(event))
Andrew Murraycc6795a2019-01-10 13:53:25 +000010482 ret = -EINVAL;
Kan Liange321d022019-05-28 15:08:30 -070010483
10484 if (ret && event->destroy)
10485 event->destroy(event);
Andrew Murraycc6795a2019-01-10 13:53:25 +000010486 }
10487
Mark Rutlandcc34b982015-01-07 14:56:51 +000010488 if (ret)
10489 module_put(pmu->module);
10490
10491 return ret;
10492}
10493
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010494static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010495{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010496 int idx, type, ret;
Dan Carpenter85c617a2017-05-22 12:03:49 +030010497 struct pmu *pmu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010498
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010499 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010500
Kan Liang40999312017-01-18 08:21:01 -050010501 /* Try parent's PMU first: */
10502 if (event->parent && event->parent->pmu) {
10503 pmu = event->parent->pmu;
10504 ret = perf_try_init_event(pmu, event);
10505 if (!ret)
10506 goto unlock;
10507 }
10508
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010509 /*
10510 * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
10511 * are often aliases for PERF_TYPE_RAW.
10512 */
10513 type = event->attr.type;
10514 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
10515 type = PERF_TYPE_RAW;
10516
10517again:
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010518 rcu_read_lock();
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010519 pmu = idr_find(&pmu_idr, type);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010520 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +080010521 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010522 ret = perf_try_init_event(pmu, event);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010523 if (ret == -ENOENT && event->attr.type != type) {
10524 type = event->attr.type;
10525 goto again;
10526 }
10527
Lin Ming940c5b22011-02-27 21:13:31 +080010528 if (ret)
10529 pmu = ERR_PTR(ret);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010530
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010531 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +080010532 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010533
Sebastian Andrzej Siewior9f0bff12019-11-19 13:14:29 +010010534 list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010535 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010536 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010537 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010538
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010539 if (ret != -ENOENT) {
10540 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010541 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010542 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010543 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010544 pmu = ERR_PTR(-ENOENT);
10545unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010546 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010547
10548 return pmu;
10549}
10550
Kan Liangf2fb6be2016-03-23 11:24:37 -070010551static void attach_sb_event(struct perf_event *event)
10552{
10553 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
10554
10555 raw_spin_lock(&pel->lock);
10556 list_add_rcu(&event->sb_list, &pel->list);
10557 raw_spin_unlock(&pel->lock);
10558}
10559
Peter Zijlstraaab5b712016-05-12 17:26:46 +020010560/*
10561 * We keep a list of all !task (and therefore per-cpu) events
10562 * that need to receive side-band records.
10563 *
10564 * This avoids having to scan all the various PMU per-cpu contexts
10565 * looking for them.
10566 */
Kan Liangf2fb6be2016-03-23 11:24:37 -070010567static void account_pmu_sb_event(struct perf_event *event)
10568{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -070010569 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -070010570 attach_sb_event(event);
10571}
10572
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010573static void account_event_cpu(struct perf_event *event, int cpu)
10574{
10575 if (event->parent)
10576 return;
10577
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010578 if (is_cgroup_event(event))
10579 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
10580}
10581
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020010582/* Freq events need the tick to stay alive (see perf_event_task_tick). */
10583static void account_freq_event_nohz(void)
10584{
10585#ifdef CONFIG_NO_HZ_FULL
10586 /* Lock so we don't race with concurrent unaccount */
10587 spin_lock(&nr_freq_lock);
10588 if (atomic_inc_return(&nr_freq_events) == 1)
10589 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
10590 spin_unlock(&nr_freq_lock);
10591#endif
10592}
10593
10594static void account_freq_event(void)
10595{
10596 if (tick_nohz_full_enabled())
10597 account_freq_event_nohz();
10598 else
10599 atomic_inc(&nr_freq_events);
10600}
10601
10602
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010603static void account_event(struct perf_event *event)
10604{
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010605 bool inc = false;
10606
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010607 if (event->parent)
10608 return;
10609
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010610 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010611 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010612 if (event->attr.mmap || event->attr.mmap_data)
10613 atomic_inc(&nr_mmap_events);
10614 if (event->attr.comm)
10615 atomic_inc(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +053010616 if (event->attr.namespaces)
10617 atomic_inc(&nr_namespaces_events);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010618 if (event->attr.task)
10619 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020010620 if (event->attr.freq)
10621 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +030010622 if (event->attr.context_switch) {
10623 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010624 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +030010625 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010626 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010627 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010628 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010629 inc = true;
Song Liu76193a92019-01-17 08:15:13 -080010630 if (event->attr.ksymbol)
10631 atomic_inc(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -080010632 if (event->attr.bpf_event)
10633 atomic_inc(&nr_bpf_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010634
Peter Zijlstra9107c892016-02-24 18:45:45 +010010635 if (inc) {
Alexander Shishkin5bce9db2017-08-29 17:01:03 +030010636 /*
10637 * We need the mutex here because static_branch_enable()
10638 * must complete *before* the perf_sched_count increment
10639 * becomes visible.
10640 */
Peter Zijlstra9107c892016-02-24 18:45:45 +010010641 if (atomic_inc_not_zero(&perf_sched_count))
10642 goto enabled;
10643
10644 mutex_lock(&perf_sched_mutex);
10645 if (!atomic_read(&perf_sched_count)) {
10646 static_branch_enable(&perf_sched_events);
10647 /*
10648 * Guarantee that all CPUs observe they key change and
10649 * call the perf scheduling hooks before proceeding to
10650 * install events that need them.
10651 */
Paul E. McKenney0809d9542018-11-06 19:20:05 -080010652 synchronize_rcu();
Peter Zijlstra9107c892016-02-24 18:45:45 +010010653 }
10654 /*
10655 * Now that we have waited for the sync_sched(), allow further
10656 * increments to by-pass the mutex.
10657 */
10658 atomic_inc(&perf_sched_count);
10659 mutex_unlock(&perf_sched_mutex);
10660 }
10661enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010662
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010663 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -070010664
10665 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010666}
10667
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010668/*
Tobias Tefke788faab2018-07-09 12:57:15 +020010669 * Allocate and initialize an event structure
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010670 */
10671static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010672perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010673 struct task_struct *task,
10674 struct perf_event *group_leader,
10675 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +030010676 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +000010677 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010678{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +020010679 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010680 struct perf_event *event;
10681 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010682 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010683
Oleg Nesterov66832eb2011-01-18 17:10:32 +010010684 if ((unsigned)cpu >= nr_cpu_ids) {
10685 if (!task || cpu != -1)
10686 return ERR_PTR(-EINVAL);
10687 }
10688
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020010689 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010690 if (!event)
10691 return ERR_PTR(-ENOMEM);
10692
10693 /*
10694 * Single events are their own group leaders, with an
10695 * empty sibling list:
10696 */
10697 if (!group_leader)
10698 group_leader = event;
10699
10700 mutex_init(&event->child_mutex);
10701 INIT_LIST_HEAD(&event->child_list);
10702
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010703 INIT_LIST_HEAD(&event->event_entry);
10704 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra66681282017-11-13 14:28:38 +010010705 INIT_LIST_HEAD(&event->active_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +030010706 init_event_group(event);
Peter Zijlstra10c6db12011-11-26 02:47:31 +010010707 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +010010708 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010709 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +010010710 INIT_HLIST_NODE(&event->hlist_entry);
10711
Peter Zijlstra10c6db12011-11-26 02:47:31 +010010712
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010713 init_waitqueue_head(&event->waitq);
Peter Zijlstra1d54ad92019-04-04 15:03:00 +020010714 event->pending_disable = -1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +080010715 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010716
10717 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010718 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010719
Al Viroa6fa9412012-08-20 14:59:25 +010010720 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010721 event->cpu = cpu;
10722 event->attr = *attr;
10723 event->group_leader = group_leader;
10724 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010725 event->oncpu = -1;
10726
10727 event->parent = parent_event;
10728
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080010729 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010730 event->id = atomic64_inc_return(&perf_event_id);
10731
10732 event->state = PERF_EVENT_STATE_INACTIVE;
10733
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010734 if (task) {
10735 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010736 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +010010737 * XXX pmu::event_init needs to know what task to account to
10738 * and we cannot use the ctx information because we need the
10739 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010740 */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -070010741 event->hw.target = get_task_struct(task);
Peter Zijlstrad580ff82010-10-14 17:43:23 +020010742 }
10743
Peter Zijlstra34f43922015-02-20 14:05:38 +010010744 event->clock = &local_clock;
10745 if (parent_event)
10746 event->clock = parent_event->clock;
10747
Avi Kivity4dc0da82011-06-29 18:42:35 +030010748 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +010010749 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030010750 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +020010751#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070010752 if (overflow_handler == bpf_overflow_handler) {
Andrii Nakryiko85192db2019-11-17 09:28:03 -080010753 struct bpf_prog *prog = parent_event->prog;
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070010754
Andrii Nakryiko85192db2019-11-17 09:28:03 -080010755 bpf_prog_inc(prog);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070010756 event->prog = prog;
10757 event->orig_overflow_handler =
10758 parent_event->orig_overflow_handler;
10759 }
10760#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +030010761 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +010010762
Wang Nan18794452016-03-28 06:41:30 +000010763 if (overflow_handler) {
10764 event->overflow_handler = overflow_handler;
10765 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +000010766 } else if (is_write_backward(event)){
10767 event->overflow_handler = perf_event_output_backward;
10768 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +000010769 } else {
Wang Nan9ecda412016-04-05 14:11:18 +000010770 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +000010771 event->overflow_handler_context = NULL;
10772 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +020010773
Jiri Olsa0231bb52013-02-01 11:23:45 +010010774 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010775
10776 pmu = NULL;
10777
10778 hwc = &event->hw;
10779 hwc->sample_period = attr->sample_period;
10780 if (attr->freq && attr->sample_freq)
10781 hwc->sample_period = 1;
10782 hwc->last_period = hwc->sample_period;
10783
Peter Zijlstrae7850592010-05-21 14:43:08 +020010784 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010785
10786 /*
Peter Zijlstraba5213a2017-05-30 11:45:12 +020010787 * We currently do not support PERF_SAMPLE_READ on inherited events.
10788 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010789 */
Peter Zijlstraba5213a2017-05-30 11:45:12 +020010790 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010791 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010792
Yan, Zhenga46a2302014-11-04 21:56:06 -050010793 if (!has_branch_stack(event))
10794 event->attr.branch_sample_type = 0;
10795
Matt Fleming79dff512015-01-23 18:45:42 +000010796 if (cgroup_fd != -1) {
10797 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
10798 if (err)
10799 goto err_ns;
10800 }
10801
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010802 pmu = perf_init_event(event);
Dan Carpenter85c617a2017-05-22 12:03:49 +030010803 if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010804 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010805 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010806 }
10807
Peter Zijlstra09f4e8f2019-11-06 12:51:04 +010010808 /*
10809 * Disallow uncore-cgroup events, they don't make sense as the cgroup will
10810 * be different on other CPUs in the uncore mask.
10811 */
10812 if (pmu->task_ctx_nr == perf_invalid_context && cgroup_fd != -1) {
10813 err = -EINVAL;
10814 goto err_pmu;
10815 }
10816
Alexander Shishkinab437622019-08-06 11:46:00 +030010817 if (event->attr.aux_output &&
10818 !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) {
10819 err = -EOPNOTSUPP;
10820 goto err_pmu;
10821 }
10822
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010823 err = exclusive_event_init(event);
10824 if (err)
10825 goto err_pmu;
10826
Alexander Shishkin375637b2016-04-27 18:44:46 +030010827 if (has_addr_filter(event)) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020010828 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
10829 sizeof(struct perf_addr_filter_range),
10830 GFP_KERNEL);
10831 if (!event->addr_filter_ranges) {
Dan Carpenter36cc2b92017-05-22 12:04:18 +030010832 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010833 goto err_per_task;
Dan Carpenter36cc2b92017-05-22 12:04:18 +030010834 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030010835
Alexander Shishkin18736ee2019-02-15 13:56:54 +020010836 /*
10837 * Clone the parent's vma offsets: they are valid until exec()
10838 * even if the mm is not shared with the parent.
10839 */
10840 if (event->parent) {
10841 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
10842
10843 raw_spin_lock_irq(&ifh->lock);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020010844 memcpy(event->addr_filter_ranges,
10845 event->parent->addr_filter_ranges,
10846 pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range));
Alexander Shishkin18736ee2019-02-15 13:56:54 +020010847 raw_spin_unlock_irq(&ifh->lock);
10848 }
10849
Alexander Shishkin375637b2016-04-27 18:44:46 +030010850 /* force hw sync on the address filters */
10851 event->addr_filters_gen = 1;
10852 }
10853
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010854 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +020010855 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -030010856 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010857 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +030010858 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +010010859 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010860 }
10861
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040010862 err = security_perf_event_alloc(event);
10863 if (err)
10864 goto err_callchain_buffer;
10865
Alexander Shishkin927a5572016-03-02 13:24:14 +020010866 /* symmetric to unaccount_event() in _free_event() */
10867 account_event(event);
10868
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010869 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010870
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040010871err_callchain_buffer:
10872 if (!event->parent) {
10873 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
10874 put_callchain_buffers();
10875 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030010876err_addr_filters:
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020010877 kfree(event->addr_filter_ranges);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010878
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010879err_per_task:
10880 exclusive_event_destroy(event);
10881
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010882err_pmu:
10883 if (event->destroy)
10884 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +080010885 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010886err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +000010887 if (is_cgroup_event(event))
10888 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010889 if (event->ns)
10890 put_pid_ns(event->ns);
Prashant Bhole621b6d22018-04-09 19:03:46 +090010891 if (event->hw.target)
10892 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020010893 kfree(event);
10894
10895 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010896}
10897
10898static int perf_copy_attr(struct perf_event_attr __user *uattr,
10899 struct perf_event_attr *attr)
10900{
10901 u32 size;
10902 int ret;
10903
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100010904 /* Zero the full structure, so that a short copy will be nice. */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010905 memset(attr, 0, sizeof(*attr));
10906
10907 ret = get_user(size, &uattr->size);
10908 if (ret)
10909 return ret;
10910
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100010911 /* ABI compatibility quirk: */
10912 if (!size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010913 size = PERF_ATTR_SIZE_VER0;
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100010914 if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010915 goto err_size;
10916
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100010917 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
10918 if (ret) {
10919 if (ret == -E2BIG)
10920 goto err_size;
10921 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010922 }
10923
Meng Xuf12f42a2017-08-23 17:07:50 -040010924 attr->size = size;
10925
Alexander Shishkina4faf002019-10-25 17:08:33 +030010926 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010927 return -EINVAL;
10928
10929 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
10930 return -EINVAL;
10931
10932 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
10933 return -EINVAL;
10934
Stephane Eranianbce38cd2012-02-09 23:20:51 +010010935 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
10936 u64 mask = attr->branch_sample_type;
10937
10938 /* only using defined bits */
10939 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
10940 return -EINVAL;
10941
10942 /* at least one branch bit must be set */
10943 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
10944 return -EINVAL;
10945
Stephane Eranianbce38cd2012-02-09 23:20:51 +010010946 /* propagate priv level, when not set for branch */
10947 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
10948
10949 /* exclude_kernel checked on syscall entry */
10950 if (!attr->exclude_kernel)
10951 mask |= PERF_SAMPLE_BRANCH_KERNEL;
10952
10953 if (!attr->exclude_user)
10954 mask |= PERF_SAMPLE_BRANCH_USER;
10955
10956 if (!attr->exclude_hv)
10957 mask |= PERF_SAMPLE_BRANCH_HV;
10958 /*
10959 * adjust user setting (for HW filter setup)
10960 */
10961 attr->branch_sample_type = mask;
10962 }
Stephane Eraniane7122092013-06-06 11:02:04 +020010963 /* privileged levels capture (kernel, hv): check permissions */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040010964 if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
10965 ret = perf_allow_kernel(attr);
10966 if (ret)
10967 return ret;
10968 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +010010969 }
Jiri Olsa40189942012-08-07 15:20:37 +020010970
Jiri Olsac5ebced2012-08-07 15:20:40 +020010971 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +020010972 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +020010973 if (ret)
10974 return ret;
10975 }
10976
10977 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
10978 if (!arch_perf_have_user_stack_dump())
10979 return -ENOSYS;
10980
10981 /*
10982 * We have __u32 type for the size, but so far
10983 * we can only use __u16 as maximum due to the
10984 * __u16 sample size limit.
10985 */
10986 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa78b562f2018-04-15 11:23:50 +020010987 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020010988 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa78b562f2018-04-15 11:23:50 +020010989 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020010990 }
Jiri Olsa40189942012-08-07 15:20:37 +020010991
Jiri Olsa5f970522018-03-12 14:45:46 +010010992 if (!attr->sample_max_stack)
10993 attr->sample_max_stack = sysctl_perf_event_max_stack;
10994
Stephane Eranian60e23642014-09-24 13:48:37 +020010995 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
10996 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010997out:
10998 return ret;
10999
11000err_size:
11001 put_user(sizeof(*attr), &uattr->size);
11002 ret = -E2BIG;
11003 goto out;
11004}
11005
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011006static int
11007perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011008{
Peter Zijlstrab69cf532014-03-14 10:50:33 +010011009 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011010 int ret = -EINVAL;
11011
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011012 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011013 goto set;
11014
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011015 /* don't allow circular references */
11016 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011017 goto out;
11018
Peter Zijlstra0f139302010-05-20 14:35:15 +020011019 /*
11020 * Don't allow cross-cpu buffers
11021 */
11022 if (output_event->cpu != event->cpu)
11023 goto out;
11024
11025 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +020011026 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +020011027 */
11028 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
11029 goto out;
11030
Peter Zijlstra34f43922015-02-20 14:05:38 +010011031 /*
11032 * Mixing clocks in the same buffer is trouble you don't need.
11033 */
11034 if (output_event->clock != event->clock)
11035 goto out;
11036
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011037 /*
Wang Nan9ecda412016-04-05 14:11:18 +000011038 * Either writing ring buffer from beginning or from end.
11039 * Mixing is not allowed.
11040 */
11041 if (is_write_backward(output_event) != is_write_backward(event))
11042 goto out;
11043
11044 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011045 * If both events generate aux data, they must be on the same PMU
11046 */
11047 if (has_aux(event) && has_aux(output_event) &&
11048 event->pmu != output_event->pmu)
11049 goto out;
11050
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011051set:
11052 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011053 /* Can't redirect output if we've got an active mmap() */
11054 if (atomic_read(&event->mmap_count))
11055 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011056
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011057 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +020011058 /* get the rb we want to redirect to */
11059 rb = ring_buffer_get(output_event);
11060 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011061 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011062 }
11063
Peter Zijlstrab69cf532014-03-14 10:50:33 +010011064 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +020011065
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011066 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011067unlock:
11068 mutex_unlock(&event->mmap_mutex);
11069
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011070out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011071 return ret;
11072}
11073
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011074static void mutex_lock_double(struct mutex *a, struct mutex *b)
11075{
11076 if (b < a)
11077 swap(a, b);
11078
11079 mutex_lock(a);
11080 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
11081}
11082
Peter Zijlstra34f43922015-02-20 14:05:38 +010011083static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
11084{
11085 bool nmi_safe = false;
11086
11087 switch (clk_id) {
11088 case CLOCK_MONOTONIC:
11089 event->clock = &ktime_get_mono_fast_ns;
11090 nmi_safe = true;
11091 break;
11092
11093 case CLOCK_MONOTONIC_RAW:
11094 event->clock = &ktime_get_raw_fast_ns;
11095 nmi_safe = true;
11096 break;
11097
11098 case CLOCK_REALTIME:
11099 event->clock = &ktime_get_real_ns;
11100 break;
11101
11102 case CLOCK_BOOTTIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011103 event->clock = &ktime_get_boottime_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011104 break;
11105
11106 case CLOCK_TAI:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011107 event->clock = &ktime_get_clocktai_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011108 break;
11109
11110 default:
11111 return -EINVAL;
11112 }
11113
11114 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
11115 return -EINVAL;
11116
11117 return 0;
11118}
11119
Peter Zijlstra321027c2017-01-11 21:09:50 +010011120/*
11121 * Variation on perf_event_ctx_lock_nested(), except we take two context
11122 * mutexes.
11123 */
11124static struct perf_event_context *
11125__perf_event_ctx_lock_double(struct perf_event *group_leader,
11126 struct perf_event_context *ctx)
11127{
11128 struct perf_event_context *gctx;
11129
11130again:
11131 rcu_read_lock();
11132 gctx = READ_ONCE(group_leader->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +020011133 if (!refcount_inc_not_zero(&gctx->refcount)) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011134 rcu_read_unlock();
11135 goto again;
11136 }
11137 rcu_read_unlock();
11138
11139 mutex_lock_double(&gctx->mutex, &ctx->mutex);
11140
11141 if (group_leader->ctx != gctx) {
11142 mutex_unlock(&ctx->mutex);
11143 mutex_unlock(&gctx->mutex);
11144 put_ctx(gctx);
11145 goto again;
11146 }
11147
11148 return gctx;
11149}
11150
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011151/**
11152 * sys_perf_event_open - open a performance event, associate it to a task/cpu
11153 *
11154 * @attr_uptr: event_id type attributes for monitoring/sampling
11155 * @pid: target pid
11156 * @cpu: target cpu
11157 * @group_fd: group leader event fd
11158 */
11159SYSCALL_DEFINE5(perf_event_open,
11160 struct perf_event_attr __user *, attr_uptr,
11161 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
11162{
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011163 struct perf_event *group_leader = NULL, *output_event = NULL;
11164 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011165 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011166 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011167 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -040011168 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -070011169 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011170 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -040011171 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011172 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011173 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +010011174 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +000011175 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011176
11177 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +020011178 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011179 return -EINVAL;
11180
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011181 /* Do we allow access to perf_event_open(2) ? */
11182 err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
11183 if (err)
11184 return err;
11185
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011186 err = perf_copy_attr(attr_uptr, &attr);
11187 if (err)
11188 return err;
11189
11190 if (!attr.exclude_kernel) {
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011191 err = perf_allow_kernel(&attr);
11192 if (err)
11193 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011194 }
11195
Hari Bathinie4222672017-03-08 02:11:36 +053011196 if (attr.namespaces) {
11197 if (!capable(CAP_SYS_ADMIN))
11198 return -EACCES;
11199 }
11200
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011201 if (attr.freq) {
11202 if (attr.sample_freq > sysctl_perf_event_sample_rate)
11203 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +020011204 } else {
11205 if (attr.sample_period & (1ULL << 63))
11206 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011207 }
11208
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011209 /* Only privileged users can get physical addresses */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011210 if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
11211 err = perf_allow_kernel(&attr);
11212 if (err)
11213 return err;
11214 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011215
David Howellsb0c8fdc2019-08-19 17:18:00 -070011216 err = security_locked_down(LOCKDOWN_PERF);
11217 if (err && (attr.sample_type & PERF_SAMPLE_REGS_INTR))
11218 /* REGS_INTR can leak data, lockdown must prevent this */
11219 return err;
11220
11221 err = 0;
11222
Stephane Eraniane5d13672011-02-14 11:20:01 +020011223 /*
11224 * In cgroup mode, the pid argument is used to pass the fd
11225 * opened to the cgroup directory in cgroupfs. The cpu argument
11226 * designates the cpu on which to monitor threads from that
11227 * cgroup.
11228 */
11229 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
11230 return -EINVAL;
11231
Yann Droneauda21b0b32014-01-05 21:36:33 +010011232 if (flags & PERF_FLAG_FD_CLOEXEC)
11233 f_flags |= O_CLOEXEC;
11234
11235 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011236 if (event_fd < 0)
11237 return event_fd;
11238
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011239 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -040011240 err = perf_fget_light(group_fd, &group);
11241 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011242 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -040011243 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011244 if (flags & PERF_FLAG_FD_OUTPUT)
11245 output_event = group_leader;
11246 if (flags & PERF_FLAG_FD_NO_GROUP)
11247 group_leader = NULL;
11248 }
11249
Stephane Eraniane5d13672011-02-14 11:20:01 +020011250 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011251 task = find_lively_task_by_vpid(pid);
11252 if (IS_ERR(task)) {
11253 err = PTR_ERR(task);
11254 goto err_group_fd;
11255 }
11256 }
11257
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020011258 if (task && group_leader &&
11259 group_leader->attr.inherit != attr.inherit) {
11260 err = -EINVAL;
11261 goto err_task;
11262 }
11263
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011264 if (task) {
11265 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
11266 if (err)
Alexander Levine5aeee52017-06-03 03:39:13 +000011267 goto err_task;
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011268
11269 /*
11270 * Reuse ptrace permission checks for now.
11271 *
11272 * We must hold cred_guard_mutex across this and any potential
11273 * perf_install_in_context() call for this new event to
11274 * serialize against exec() altering our credentials (and the
11275 * perf_event_exit_task() that could imply).
11276 */
11277 err = -EACCES;
11278 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
11279 goto err_cred;
11280 }
11281
Matt Fleming79dff512015-01-23 18:45:42 +000011282 if (flags & PERF_FLAG_PID_CGROUP)
11283 cgroup_fd = pid;
11284
Avi Kivity4dc0da82011-06-29 18:42:35 +030011285 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000011286 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011287 if (IS_ERR(event)) {
11288 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011289 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011290 }
11291
Vince Weaver53b25332014-05-16 17:12:12 -040011292 if (is_sampling_event(event)) {
11293 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +053011294 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -040011295 goto err_alloc;
11296 }
11297 }
11298
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011299 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011300 * Special case software events and allow them to be part of
11301 * any hardware group.
11302 */
11303 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011304
Peter Zijlstra34f43922015-02-20 14:05:38 +010011305 if (attr.use_clockid) {
11306 err = perf_event_set_clock(event, attr.clockid);
11307 if (err)
11308 goto err_alloc;
11309 }
11310
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011311 if (pmu->task_ctx_nr == perf_sw_context)
11312 event->event_caps |= PERF_EV_CAP_SOFTWARE;
11313
Song Liua1150c22018-05-03 12:47:16 -070011314 if (group_leader) {
11315 if (is_software_event(event) &&
11316 !in_software_context(group_leader)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011317 /*
Song Liua1150c22018-05-03 12:47:16 -070011318 * If the event is a sw event, but the group_leader
11319 * is on hw context.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011320 *
Song Liua1150c22018-05-03 12:47:16 -070011321 * Allow the addition of software events to hw
11322 * groups, this is safe because software events
11323 * never fail to schedule.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011324 */
Song Liua1150c22018-05-03 12:47:16 -070011325 pmu = group_leader->ctx->pmu;
11326 } else if (!is_software_event(event) &&
11327 is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011328 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011329 /*
11330 * In case the group is a pure software group, and we
11331 * try to add a hardware event, move the whole group to
11332 * the hardware context.
11333 */
11334 move_group = 1;
11335 }
11336 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011337
11338 /*
11339 * Get the target context (task or percpu):
11340 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050011341 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011342 if (IS_ERR(ctx)) {
11343 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011344 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011345 }
11346
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011347 /*
11348 * Look up the group leader (we will attach this event to it):
11349 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011350 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011351 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011352
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011353 /*
11354 * Do not allow a recursive hierarchy (this new sibling
11355 * becoming part of another group-sibling):
11356 */
11357 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011358 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011359
11360 /* All events in a group should have the same clock */
11361 if (group_leader->clock != event->clock)
11362 goto err_context;
11363
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011364 /*
Mark Rutland64aee2a2017-06-22 15:41:38 +010011365 * Make sure we're both events for the same CPU;
11366 * grouping events for different CPUs is broken; since
11367 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011368 */
Mark Rutland64aee2a2017-06-22 15:41:38 +010011369 if (group_leader->cpu != event->cpu)
11370 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +010011371
Mark Rutland64aee2a2017-06-22 15:41:38 +010011372 /*
11373 * Make sure we're both on the same task, or both
11374 * per-CPU events.
11375 */
11376 if (group_leader->ctx->task != ctx->task)
11377 goto err_context;
11378
11379 /*
11380 * Do not allow to attach to a group in a different task
11381 * or CPU context. If we're moving SW events, we'll fix
11382 * this up later, so allow that.
11383 */
11384 if (!move_group && group_leader->ctx != ctx)
11385 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011386
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011387 /*
11388 * Only a group leader can be exclusive or pinned
11389 */
11390 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011391 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011392 }
11393
11394 if (output_event) {
11395 err = perf_event_set_output(event, output_event);
11396 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011397 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011398 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011399
Yann Droneauda21b0b32014-01-05 21:36:33 +010011400 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
11401 f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011402 if (IS_ERR(event_file)) {
11403 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +020011404 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011405 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -040011406 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011407
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011408 if (move_group) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011409 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
11410
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011411 if (gctx->task == TASK_TOMBSTONE) {
11412 err = -ESRCH;
11413 goto err_locked;
11414 }
Peter Zijlstra321027c2017-01-11 21:09:50 +010011415
11416 /*
11417 * Check if we raced against another sys_perf_event_open() call
11418 * moving the software group underneath us.
11419 */
11420 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
11421 /*
11422 * If someone moved the group out from under us, check
11423 * if this new event wound up on the same ctx, if so
11424 * its the regular !move_group case, otherwise fail.
11425 */
11426 if (gctx != ctx) {
11427 err = -EINVAL;
11428 goto err_locked;
11429 } else {
11430 perf_event_ctx_unlock(group_leader, gctx);
11431 move_group = 0;
11432 }
11433 }
Alexander Shishkin8a58dda2019-07-01 14:07:55 +030011434
11435 /*
11436 * Failure to create exclusive events returns -EBUSY.
11437 */
11438 err = -EBUSY;
11439 if (!exclusive_event_installable(group_leader, ctx))
11440 goto err_locked;
11441
11442 for_each_sibling_event(sibling, group_leader) {
11443 if (!exclusive_event_installable(sibling, ctx))
11444 goto err_locked;
11445 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011446 } else {
11447 mutex_lock(&ctx->mutex);
11448 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011449
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011450 if (ctx->task == TASK_TOMBSTONE) {
11451 err = -ESRCH;
11452 goto err_locked;
11453 }
11454
Peter Zijlstraa7239682015-09-09 19:06:33 +020011455 if (!perf_event_validate_size(event)) {
11456 err = -E2BIG;
11457 goto err_locked;
11458 }
11459
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011460 if (!task) {
11461 /*
11462 * Check if the @cpu we're creating an event for is online.
11463 *
11464 * We use the perf_cpu_context::ctx::mutex to serialize against
11465 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
11466 */
11467 struct perf_cpu_context *cpuctx =
11468 container_of(ctx, struct perf_cpu_context, ctx);
11469
11470 if (!cpuctx->online) {
11471 err = -ENODEV;
11472 goto err_locked;
11473 }
11474 }
11475
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011476 if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) {
11477 err = -EINVAL;
Alexander Shishkinab437622019-08-06 11:46:00 +030011478 goto err_locked;
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011479 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011480
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011481 /*
11482 * Must be under the same ctx::mutex as perf_install_in_context(),
11483 * because we need to serialize with concurrent event creation.
11484 */
11485 if (!exclusive_event_installable(event, ctx)) {
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011486 err = -EBUSY;
11487 goto err_locked;
11488 }
11489
11490 WARN_ON_ONCE(ctx->parent_ctx);
11491
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011492 /*
11493 * This is the point on no return; we cannot fail hereafter. This is
11494 * where we start modifying current state.
11495 */
11496
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011497 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011498 /*
11499 * See perf_event_ctx_lock() for comments on the details
11500 * of swizzling perf_event::ctx.
11501 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011502 perf_remove_from_context(group_leader, 0);
Peter Zijlstra279b5162017-02-16 10:28:37 +010011503 put_ctx(gctx);
Jiri Olsa0231bb52013-02-01 11:23:45 +010011504
Peter Zijlstraedb39592018-03-15 17:36:56 +010011505 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011506 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011507 put_ctx(gctx);
11508 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011509
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011510 /*
11511 * Wait for everybody to stop referencing the events through
11512 * the old lists, before installing it on new lists.
11513 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011514 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011515
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011516 /*
11517 * Install the group siblings before the group leader.
11518 *
11519 * Because a group leader will try and install the entire group
11520 * (through the sibling list, which is still in-tact), we can
11521 * end up with siblings installed in the wrong context.
11522 *
11523 * By installing siblings first we NO-OP because they're not
11524 * reachable through the group lists.
11525 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010011526 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011527 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +010011528 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011529 get_ctx(ctx);
11530 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011531
11532 /*
11533 * Removing from the context ends up with disabled
11534 * event. What we want here is event in the initial
11535 * startup state, ready to be add into new context.
11536 */
11537 perf_event__state_init(group_leader);
11538 perf_install_in_context(ctx, group_leader, group_leader->cpu);
11539 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011540 }
11541
Peter Zijlstraf73e22a2015-09-09 20:48:22 +020011542 /*
11543 * Precalculate sample_data sizes; do while holding ctx::mutex such
11544 * that we're serialized against further additions and before
11545 * perf_install_in_context() which is the point the event is active and
11546 * can use these values.
11547 */
11548 perf_event__header_size(event);
11549 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011550
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010011551 event->owner = current;
11552
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080011553 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010011554 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011555
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011556 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010011557 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011558 mutex_unlock(&ctx->mutex);
11559
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011560 if (task) {
11561 mutex_unlock(&task->signal->cred_guard_mutex);
11562 put_task_struct(task);
11563 }
11564
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011565 mutex_lock(&current->perf_event_mutex);
11566 list_add_tail(&event->owner_entry, &current->perf_event_list);
11567 mutex_unlock(&current->perf_event_mutex);
11568
Peter Zijlstra8a495422010-05-27 15:47:49 +020011569 /*
11570 * Drop the reference on the group_event after placing the
11571 * new event on the sibling_list. This ensures destruction
11572 * of the group leader will find the pointer to itself in
11573 * perf_group_detach().
11574 */
Al Viro2903ff02012-08-28 12:52:22 -040011575 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040011576 fd_install(event_fd, event_file);
11577 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011578
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011579err_locked:
11580 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010011581 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011582 mutex_unlock(&ctx->mutex);
11583/* err_file: */
11584 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011585err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010011586 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040011587 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011588err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010011589 /*
11590 * If event_file is set, the fput() above will have called ->release()
11591 * and that will take care of freeing the event.
11592 */
11593 if (!event_file)
11594 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011595err_cred:
11596 if (task)
11597 mutex_unlock(&task->signal->cred_guard_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020011598err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020011599 if (task)
11600 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011601err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040011602 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040011603err_fd:
11604 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011605 return err;
11606}
11607
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011608/**
11609 * perf_event_create_kernel_counter
11610 *
11611 * @attr: attributes of the counter to create
11612 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070011613 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011614 */
11615struct perf_event *
11616perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070011617 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030011618 perf_overflow_handler_t overflow_handler,
11619 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011620{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011621 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011622 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011623 int err;
11624
Alexander Shishkindce5aff2019-10-30 15:47:31 +020011625 /*
11626 * Grouping is not supported for kernel events, neither is 'AUX',
11627 * make sure the caller's intentions are adjusted.
11628 */
11629 if (attr->aux_output)
11630 return ERR_PTR(-EINVAL);
11631
Avi Kivity4dc0da82011-06-29 18:42:35 +030011632 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000011633 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010011634 if (IS_ERR(event)) {
11635 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011636 goto err;
11637 }
11638
Jiri Olsaf8697762014-08-01 14:33:01 +020011639 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011640 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020011641
Alexander Shishkinf25d8ba2019-10-30 15:47:30 +020011642 /*
11643 * Get the target context (task or percpu):
11644 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050011645 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011646 if (IS_ERR(ctx)) {
11647 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011648 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010011649 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011650
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011651 WARN_ON_ONCE(ctx->parent_ctx);
11652 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011653 if (ctx->task == TASK_TOMBSTONE) {
11654 err = -ESRCH;
11655 goto err_unlock;
11656 }
11657
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011658 if (!task) {
11659 /*
11660 * Check if the @cpu we're creating an event for is online.
11661 *
11662 * We use the perf_cpu_context::ctx::mutex to serialize against
11663 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
11664 */
11665 struct perf_cpu_context *cpuctx =
11666 container_of(ctx, struct perf_cpu_context, ctx);
11667 if (!cpuctx->online) {
11668 err = -ENODEV;
11669 goto err_unlock;
11670 }
11671 }
11672
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011673 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011674 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011675 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011676 }
11677
Leonard Crestez4ce54af2019-07-24 15:53:24 +030011678 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010011679 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011680 mutex_unlock(&ctx->mutex);
11681
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011682 return event;
11683
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011684err_unlock:
11685 mutex_unlock(&ctx->mutex);
11686 perf_unpin_context(ctx);
11687 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011688err_free:
11689 free_event(event);
11690err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010011691 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011692}
11693EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
11694
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011695void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
11696{
11697 struct perf_event_context *src_ctx;
11698 struct perf_event_context *dst_ctx;
11699 struct perf_event *event, *tmp;
11700 LIST_HEAD(events);
11701
11702 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
11703 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
11704
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011705 /*
11706 * See perf_event_ctx_lock() for comments on the details
11707 * of swizzling perf_event::ctx.
11708 */
11709 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011710 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
11711 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011712 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020011713 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011714 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020011715 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011716 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011717
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011718 /*
11719 * Wait for the events to quiesce before re-instating them.
11720 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011721 synchronize_rcu();
11722
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011723 /*
11724 * Re-instate events in 2 passes.
11725 *
11726 * Skip over group leaders and only install siblings on this first
11727 * pass, siblings will not get enabled without a leader, however a
11728 * leader will enable its siblings, even if those are still on the old
11729 * context.
11730 */
11731 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
11732 if (event->group_leader == event)
11733 continue;
11734
11735 list_del(&event->migrate_entry);
11736 if (event->state >= PERF_EVENT_STATE_OFF)
11737 event->state = PERF_EVENT_STATE_INACTIVE;
11738 account_event_cpu(event, dst_cpu);
11739 perf_install_in_context(dst_ctx, event, dst_cpu);
11740 get_ctx(dst_ctx);
11741 }
11742
11743 /*
11744 * Once all the siblings are setup properly, install the group leaders
11745 * to make it go.
11746 */
Peter Zijlstra98861672013-10-03 16:02:23 +020011747 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
11748 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011749 if (event->state >= PERF_EVENT_STATE_OFF)
11750 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020011751 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011752 perf_install_in_context(dst_ctx, event, dst_cpu);
11753 get_ctx(dst_ctx);
11754 }
11755 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011756 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011757}
11758EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
11759
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011760static void sync_child_event(struct perf_event *child_event,
11761 struct task_struct *child)
11762{
11763 struct perf_event *parent_event = child_event->parent;
11764 u64 child_val;
11765
11766 if (child_event->attr.inherit_stat)
11767 perf_event_read_event(child_event, child);
11768
Peter Zijlstrab5e58792010-05-21 14:43:12 +020011769 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011770
11771 /*
11772 * Add back the child's count to the parent's count:
11773 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020011774 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011775 atomic64_add(child_event->total_time_enabled,
11776 &parent_event->child_total_time_enabled);
11777 atomic64_add(child_event->total_time_running,
11778 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011779}
11780
11781static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011782perf_event_exit_event(struct perf_event *child_event,
11783 struct perf_event_context *child_ctx,
11784 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011785{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011786 struct perf_event *parent_event = child_event->parent;
11787
Peter Zijlstra1903d502014-07-15 17:27:27 +020011788 /*
11789 * Do not destroy the 'original' grouping; because of the context
11790 * switch optimization the original events could've ended up in a
11791 * random child task.
11792 *
11793 * If we were to destroy the original group, all group related
11794 * operations would cease to function properly after this random
11795 * child dies.
11796 *
11797 * Do destroy all inherited groups, we don't care about those
11798 * and being thorough is better.
11799 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010011800 raw_spin_lock_irq(&child_ctx->lock);
11801 WARN_ON_ONCE(child_ctx->is_active);
11802
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011803 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010011804 perf_group_detach(child_event);
11805 list_del_event(child_event, child_ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +020011806 perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010011807 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011808
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011809 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011810 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011811 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011812 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040011813 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011814 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011815 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011816 /*
11817 * Child events can be cleaned up.
11818 */
11819
11820 sync_child_event(child_event, child);
11821
11822 /*
11823 * Remove this event from the parent's list
11824 */
11825 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
11826 mutex_lock(&parent_event->child_mutex);
11827 list_del_init(&child_event->child_list);
11828 mutex_unlock(&parent_event->child_mutex);
11829
11830 /*
11831 * Kick perf_poll() for is_event_hup().
11832 */
11833 perf_event_wakeup(parent_event);
11834 free_event(child_event);
11835 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011836}
11837
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011838static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011839{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020011840 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011841 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011842
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011843 WARN_ON_ONCE(child != current);
11844
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010011845 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011846 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011847 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011848
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011849 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010011850 * In order to reduce the amount of tricky in ctx tear-down, we hold
11851 * ctx::mutex over the entire thing. This serializes against almost
11852 * everything that wants to access the ctx.
11853 *
11854 * The exception is sys_perf_event_open() /
11855 * perf_event_create_kernel_count() which does find_get_context()
11856 * without ctx::mutex (it cannot because of the move_group double mutex
11857 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011858 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010011859 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011860
11861 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010011862 * In a single ctx::lock section, de-schedule the events and detach the
11863 * context from the task such that we cannot ever get it scheduled back
11864 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011865 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010011866 raw_spin_lock_irq(&child_ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +020011867 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020011868
11869 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011870 * Now that the context is inactive, destroy the task <-> ctx relation
11871 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011872 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011873 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
11874 put_ctx(child_ctx); /* cannot be last */
11875 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
11876 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011877
Peter Zijlstra211de6e2014-09-30 19:23:08 +020011878 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010011879 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011880
Peter Zijlstra211de6e2014-09-30 19:23:08 +020011881 if (clone_ctx)
11882 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020011883
11884 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011885 * Report the task dead after unscheduling the events so that we
11886 * won't get any samples after PERF_RECORD_EXIT. We can however still
11887 * get a few PERF_RECORD_READ events.
11888 */
11889 perf_event_task(child, child_ctx, 0);
11890
Peter Zijlstraebf905f2014-05-29 19:00:24 +020011891 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010011892 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010011893
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011894 mutex_unlock(&child_ctx->mutex);
11895
11896 put_ctx(child_ctx);
11897}
11898
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011899/*
11900 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011901 *
11902 * Can be called with cred_guard_mutex held when called from
11903 * install_exec_creds().
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011904 */
11905void perf_event_exit_task(struct task_struct *child)
11906{
Peter Zijlstra88821352010-11-09 19:01:43 +010011907 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011908 int ctxn;
11909
Peter Zijlstra88821352010-11-09 19:01:43 +010011910 mutex_lock(&child->perf_event_mutex);
11911 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
11912 owner_entry) {
11913 list_del_init(&event->owner_entry);
11914
11915 /*
11916 * Ensure the list deletion is visible before we clear
11917 * the owner, closes a race against perf_release() where
11918 * we need to serialize on the owner->perf_event_mutex.
11919 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010011920 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010011921 }
11922 mutex_unlock(&child->perf_event_mutex);
11923
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011924 for_each_task_context_nr(ctxn)
11925 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010011926
11927 /*
11928 * The perf_event_exit_task_context calls perf_event_task
11929 * with child's task_ctx, which generates EXIT events for
11930 * child contexts and sets child->perf_event_ctxp[] to NULL.
11931 * At this point we need to send EXIT events to cpu contexts.
11932 */
11933 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011934}
11935
Frederic Weisbecker889ff012010-01-09 20:04:47 +010011936static void perf_free_event(struct perf_event *event,
11937 struct perf_event_context *ctx)
11938{
11939 struct perf_event *parent = event->parent;
11940
11941 if (WARN_ON_ONCE(!parent))
11942 return;
11943
11944 mutex_lock(&parent->child_mutex);
11945 list_del_init(&event->child_list);
11946 mutex_unlock(&parent->child_mutex);
11947
Al Viroa6fa9412012-08-20 14:59:25 +010011948 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010011949
Peter Zijlstra652884f2015-01-23 11:20:10 +010011950 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020011951 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010011952 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010011953 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010011954 free_event(event);
11955}
11956
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011957/*
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020011958 * Free a context as created by inheritance by perf_event_init_task() below,
11959 * used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010011960 *
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020011961 * Even though the task has never lived, the context and events have been
11962 * exposed through the child_list, so we must take care tearing it all down.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011963 */
11964void perf_event_free_task(struct task_struct *task)
11965{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011966 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011967 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011968 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011969
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011970 for_each_task_context_nr(ctxn) {
11971 ctx = task->perf_event_ctxp[ctxn];
11972 if (!ctx)
11973 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011974
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011975 mutex_lock(&ctx->mutex);
Peter Zijlstrae552a832017-03-16 13:47:48 +010011976 raw_spin_lock_irq(&ctx->lock);
11977 /*
11978 * Destroy the task <-> ctx relation and mark the context dead.
11979 *
11980 * This is important because even though the task hasn't been
11981 * exposed yet the context has been (through child_list).
11982 */
11983 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
11984 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
11985 put_task_struct(task); /* cannot be last */
11986 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011987
Peter Zijlstra15121c72017-03-16 13:47:50 +010011988 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011989 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011990
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020011991 mutex_unlock(&ctx->mutex);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020011992
11993 /*
11994 * perf_event_release_kernel() could've stolen some of our
11995 * child events and still have them on its free_list. In that
11996 * case we must wait for these events to have been freed (in
11997 * particular all their references to this task must've been
11998 * dropped).
11999 *
12000 * Without this copy_process() will unconditionally free this
12001 * task (irrespective of its reference count) and
12002 * _free_event()'s put_task_struct(event->hw.target) will be a
12003 * use-after-free.
12004 *
12005 * Wait for all events to drop their context reference.
12006 */
12007 wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);
12008 put_ctx(ctx); /* must be last */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012009 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012010}
12011
Peter Zijlstra4e231c72010-09-09 21:01:59 +020012012void perf_event_delayed_put(struct task_struct *task)
12013{
12014 int ctxn;
12015
12016 for_each_task_context_nr(ctxn)
12017 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
12018}
12019
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012020struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000012021{
Al Viro02e5ad92019-06-26 20:43:53 -040012022 struct file *file = fget(fd);
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012023 if (!file)
12024 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000012025
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012026 if (file->f_op != &perf_fops) {
12027 fput(file);
12028 return ERR_PTR(-EBADF);
12029 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000012030
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012031 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000012032}
12033
Yonghong Songf8d959a2018-05-24 11:21:08 -070012034const struct perf_event *perf_get_event(struct file *file)
12035{
12036 if (file->f_op != &perf_fops)
12037 return ERR_PTR(-EINVAL);
12038
12039 return file->private_data;
12040}
12041
Kaixu Xiaffe86902015-08-06 07:02:32 +000012042const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
12043{
12044 if (!event)
12045 return ERR_PTR(-EINVAL);
12046
12047 return &event->attr;
12048}
12049
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012050/*
Tobias Tefke788faab2018-07-09 12:57:15 +020012051 * Inherit an event from parent task to child task.
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012052 *
12053 * Returns:
12054 * - valid pointer on success
12055 * - NULL for orphaned events
12056 * - IS_ERR() on error
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012057 */
12058static struct perf_event *
12059inherit_event(struct perf_event *parent_event,
12060 struct task_struct *parent,
12061 struct perf_event_context *parent_ctx,
12062 struct task_struct *child,
12063 struct perf_event *group_leader,
12064 struct perf_event_context *child_ctx)
12065{
Peter Zijlstra8ca2bd42017-09-05 14:12:35 +020012066 enum perf_event_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012067 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020012068 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012069
12070 /*
12071 * Instead of creating recursive hierarchies of events,
12072 * we link inherited events back to the original parent,
12073 * which has a filp for sure, which we use as the reference
12074 * count:
12075 */
12076 if (parent_event->parent)
12077 parent_event = parent_event->parent;
12078
12079 child_event = perf_event_alloc(&parent_event->attr,
12080 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020012081 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012082 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000012083 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012084 if (IS_ERR(child_event))
12085 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010012086
Jiri Olsa313ccb92018-01-07 17:03:47 +010012087
12088 if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) &&
12089 !child_ctx->task_ctx_data) {
12090 struct pmu *pmu = child_event->pmu;
12091
12092 child_ctx->task_ctx_data = kzalloc(pmu->task_ctx_size,
12093 GFP_KERNEL);
12094 if (!child_ctx->task_ctx_data) {
12095 free_event(child_event);
Alexander Shishkin697d8772019-11-05 09:57:02 +020012096 return ERR_PTR(-ENOMEM);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012097 }
12098 }
12099
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012100 /*
12101 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
12102 * must be under the same lock in order to serialize against
12103 * perf_event_release_kernel(), such that either we must observe
12104 * is_orphaned_event() or they will observe us on the child_list.
12105 */
12106 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020012107 if (is_orphaned_event(parent_event) ||
12108 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012109 mutex_unlock(&parent_event->child_mutex);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012110 /* task_ctx_data is freed with child_ctx */
Al Viroa6fa9412012-08-20 14:59:25 +010012111 free_event(child_event);
12112 return NULL;
12113 }
12114
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012115 get_ctx(child_ctx);
12116
12117 /*
12118 * Make the child state follow the state of the parent event,
12119 * not its attr.disabled bit. We hold the parent's mutex,
12120 * so we won't race with perf_event_{en, dis}able_family.
12121 */
Jiri Olsa1929def2014-09-12 13:18:27 +020012122 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012123 child_event->state = PERF_EVENT_STATE_INACTIVE;
12124 else
12125 child_event->state = PERF_EVENT_STATE_OFF;
12126
12127 if (parent_event->attr.freq) {
12128 u64 sample_period = parent_event->hw.sample_period;
12129 struct hw_perf_event *hwc = &child_event->hw;
12130
12131 hwc->sample_period = sample_period;
12132 hwc->last_period = sample_period;
12133
12134 local64_set(&hwc->period_left, sample_period);
12135 }
12136
12137 child_event->ctx = child_ctx;
12138 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030012139 child_event->overflow_handler_context
12140 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012141
12142 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020012143 * Precalculate sample_data sizes
12144 */
12145 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020012146 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020012147
12148 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012149 * Link it up in the child's context:
12150 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020012151 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012152 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020012153 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012154
12155 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012156 * Link this into the parent event's child list
12157 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012158 list_add_tail(&child_event->child_list, &parent_event->child_list);
12159 mutex_unlock(&parent_event->child_mutex);
12160
12161 return child_event;
12162}
12163
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012164/*
12165 * Inherits an event group.
12166 *
12167 * This will quietly suppress orphaned events; !inherit_event() is not an error.
12168 * This matches with perf_event_release_kernel() removing all child events.
12169 *
12170 * Returns:
12171 * - 0 on success
12172 * - <0 on error
12173 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012174static int inherit_group(struct perf_event *parent_event,
12175 struct task_struct *parent,
12176 struct perf_event_context *parent_ctx,
12177 struct task_struct *child,
12178 struct perf_event_context *child_ctx)
12179{
12180 struct perf_event *leader;
12181 struct perf_event *sub;
12182 struct perf_event *child_ctr;
12183
12184 leader = inherit_event(parent_event, parent, parent_ctx,
12185 child, NULL, child_ctx);
12186 if (IS_ERR(leader))
12187 return PTR_ERR(leader);
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012188 /*
12189 * @leader can be NULL here because of is_orphaned_event(). In this
12190 * case inherit_event() will create individual events, similar to what
12191 * perf_group_detach() would do anyway.
12192 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010012193 for_each_sibling_event(sub, parent_event) {
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012194 child_ctr = inherit_event(sub, parent, parent_ctx,
12195 child, leader, child_ctx);
12196 if (IS_ERR(child_ctr))
12197 return PTR_ERR(child_ctr);
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012198
Alexander Shishkin00496fe2019-11-01 17:12:48 +020012199 if (sub->aux_event == parent_event && child_ctr &&
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012200 !perf_get_aux_event(child_ctr, leader))
12201 return -EINVAL;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012202 }
12203 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012204}
12205
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012206/*
12207 * Creates the child task context and tries to inherit the event-group.
12208 *
12209 * Clears @inherited_all on !attr.inherited or error. Note that we'll leave
12210 * inherited_all set when we 'fail' to inherit an orphaned event; this is
12211 * consistent with perf_event_release_kernel() removing all child events.
12212 *
12213 * Returns:
12214 * - 0 on success
12215 * - <0 on error
12216 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012217static int
12218inherit_task_group(struct perf_event *event, struct task_struct *parent,
12219 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012220 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012221 int *inherited_all)
12222{
12223 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012224 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012225
12226 if (!event->attr.inherit) {
12227 *inherited_all = 0;
12228 return 0;
12229 }
12230
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012231 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012232 if (!child_ctx) {
12233 /*
12234 * This is executed from the parent task context, so
12235 * inherit events that have been marked for cloning.
12236 * First allocate and initialize a context for the
12237 * child.
12238 */
Jiri Olsa734df5a2013-07-09 17:44:10 +020012239 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012240 if (!child_ctx)
12241 return -ENOMEM;
12242
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012243 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012244 }
12245
12246 ret = inherit_group(event, parent, parent_ctx,
12247 child, child_ctx);
12248
12249 if (ret)
12250 *inherited_all = 0;
12251
12252 return ret;
12253}
12254
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012255/*
12256 * Initialize the perf_event context in task_struct
12257 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020012258static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012259{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012260 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012261 struct perf_event_context *cloned_ctx;
12262 struct perf_event *event;
12263 struct task_struct *parent = current;
12264 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012265 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012266 int ret = 0;
12267
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012268 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012269 return 0;
12270
12271 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012272 * If the parent's context is a clone, pin it so it won't get
12273 * swapped under us.
12274 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012275 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020012276 if (!parent_ctx)
12277 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012278
12279 /*
12280 * No need to check if parent_ctx != NULL here; since we saw
12281 * it non-NULL earlier, the only reason for it to become NULL
12282 * is if we exit, and since we're currently in the middle of
12283 * a fork we can't be exiting at the same time.
12284 */
12285
12286 /*
12287 * Lock the parent list. No need to lock the child - not PID
12288 * hashed yet and not running, so nobody can access it.
12289 */
12290 mutex_lock(&parent_ctx->mutex);
12291
12292 /*
12293 * We dont have to disable NMIs - we are only looking at
12294 * the list, not manipulating it:
12295 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012296 perf_event_groups_for_each(event, &parent_ctx->pinned_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012297 ret = inherit_task_group(event, parent, parent_ctx,
12298 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012299 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012300 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012301 }
12302
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012303 /*
12304 * We can't hold ctx->lock when iterating the ->flexible_group list due
12305 * to allocations, but we need to prevent rotation because
12306 * rotate_ctx() will change the list from interrupt context.
12307 */
12308 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12309 parent_ctx->rotate_disable = 1;
12310 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
12311
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012312 perf_event_groups_for_each(event, &parent_ctx->flexible_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012313 ret = inherit_task_group(event, parent, parent_ctx,
12314 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012315 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012316 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012317 }
12318
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012319 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12320 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012321
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012322 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012323
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010012324 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012325 /*
12326 * Mark the child context as a clone of the parent
12327 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012328 *
12329 * Note that if the parent is a clone, the holding of
12330 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012331 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012332 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012333 if (cloned_ctx) {
12334 child_ctx->parent_ctx = cloned_ctx;
12335 child_ctx->parent_gen = parent_ctx->parent_gen;
12336 } else {
12337 child_ctx->parent_ctx = parent_ctx;
12338 child_ctx->parent_gen = parent_ctx->generation;
12339 }
12340 get_ctx(child_ctx->parent_ctx);
12341 }
12342
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012343 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012344out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012345 mutex_unlock(&parent_ctx->mutex);
12346
12347 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012348 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012349
12350 return ret;
12351}
12352
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012353/*
12354 * Initialize the perf_event context in task_struct
12355 */
12356int perf_event_init_task(struct task_struct *child)
12357{
12358 int ctxn, ret;
12359
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010012360 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
12361 mutex_init(&child->perf_event_mutex);
12362 INIT_LIST_HEAD(&child->perf_event_list);
12363
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012364 for_each_task_context_nr(ctxn) {
12365 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012366 if (ret) {
12367 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012368 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012369 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012370 }
12371
12372 return 0;
12373}
12374
Paul Mackerras220b1402010-03-10 20:45:52 +110012375static void __init perf_event_init_all_cpus(void)
12376{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012377 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110012378 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110012379
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012380 zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL);
12381
Paul Mackerras220b1402010-03-10 20:45:52 +110012382 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012383 swhash = &per_cpu(swevent_htable, cpu);
12384 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000012385 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070012386
12387 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
12388 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012389
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -080012390#ifdef CONFIG_CGROUP_PERF
12391 INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu));
12392#endif
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012393 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110012394 }
12395}
12396
Valdis Kletnieksd18bf422019-03-12 04:06:37 -040012397static void perf_swevent_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012398{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012399 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012400
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012401 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000012402 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012403 struct swevent_hlist *hlist;
12404
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012405 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
12406 WARN_ON(!hlist);
12407 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012408 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012409 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012410}
12411
Dave Young2965faa2015-09-09 15:38:55 -070012412#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012413static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012414{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012415 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012416 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
12417 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012418
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012419 raw_spin_lock(&ctx->lock);
Peter Zijlstra0ee098c2017-09-05 13:24:28 +020012420 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012421 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012422 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012423 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012424}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012425
12426static void perf_event_exit_cpu_context(int cpu)
12427{
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012428 struct perf_cpu_context *cpuctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012429 struct perf_event_context *ctx;
12430 struct pmu *pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012431
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012432 mutex_lock(&pmus_lock);
12433 list_for_each_entry(pmu, &pmus, entry) {
12434 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12435 ctx = &cpuctx->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012436
12437 mutex_lock(&ctx->mutex);
12438 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012439 cpuctx->online = 0;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012440 mutex_unlock(&ctx->mutex);
12441 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012442 cpumask_clear_cpu(cpu, perf_online_mask);
12443 mutex_unlock(&pmus_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012444}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012445#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012446
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012447static void perf_event_exit_cpu_context(int cpu) { }
12448
12449#endif
12450
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012451int perf_event_init_cpu(unsigned int cpu)
12452{
12453 struct perf_cpu_context *cpuctx;
12454 struct perf_event_context *ctx;
12455 struct pmu *pmu;
12456
12457 perf_swevent_init_cpu(cpu);
12458
12459 mutex_lock(&pmus_lock);
12460 cpumask_set_cpu(cpu, perf_online_mask);
12461 list_for_each_entry(pmu, &pmus, entry) {
12462 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12463 ctx = &cpuctx->ctx;
12464
12465 mutex_lock(&ctx->mutex);
12466 cpuctx->online = 1;
12467 mutex_unlock(&ctx->mutex);
12468 }
12469 mutex_unlock(&pmus_lock);
12470
12471 return 0;
12472}
12473
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012474int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012475{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010012476 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012477 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012478}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012479
Peter Zijlstrac2774432010-12-08 15:29:02 +010012480static int
12481perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
12482{
12483 int cpu;
12484
12485 for_each_online_cpu(cpu)
12486 perf_event_exit_cpu(cpu);
12487
12488 return NOTIFY_OK;
12489}
12490
12491/*
12492 * Run the perf reboot notifier at the very last possible moment so that
12493 * the generic watchdog code runs as long as possible.
12494 */
12495static struct notifier_block perf_reboot_notifier = {
12496 .notifier_call = perf_reboot,
12497 .priority = INT_MIN,
12498};
12499
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012500void __init perf_event_init(void)
12501{
Jason Wessel3c502e72010-11-04 17:33:01 -050012502 int ret;
12503
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012504 idr_init(&pmu_idr);
12505
Paul Mackerras220b1402010-03-10 20:45:52 +110012506 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012507 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012508 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
12509 perf_pmu_register(&perf_cpu_clock, NULL, -1);
12510 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012511 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012512 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010012513 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050012514
12515 ret = init_hw_breakpoint();
12516 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020012517
Jiri Olsab01c3a02012-03-23 15:41:20 +010012518 /*
12519 * Build time assertion that we keep the data_head at the intended
12520 * location. IOW, validation we got the __reserved[] size right.
12521 */
12522 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
12523 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012524}
Peter Zijlstraabe43402010-11-17 23:17:37 +010012525
Cody P Schaferfd979c02015-01-30 13:45:57 -080012526ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
12527 char *page)
12528{
12529 struct perf_pmu_events_attr *pmu_attr =
12530 container_of(attr, struct perf_pmu_events_attr, attr);
12531
12532 if (pmu_attr->event_str)
12533 return sprintf(page, "%s\n", pmu_attr->event_str);
12534
12535 return 0;
12536}
Thomas Gleixner675965b2016-02-22 22:19:27 +000012537EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080012538
Peter Zijlstraabe43402010-11-17 23:17:37 +010012539static int __init perf_event_sysfs_init(void)
12540{
12541 struct pmu *pmu;
12542 int ret;
12543
12544 mutex_lock(&pmus_lock);
12545
12546 ret = bus_register(&pmu_bus);
12547 if (ret)
12548 goto unlock;
12549
12550 list_for_each_entry(pmu, &pmus, entry) {
12551 if (!pmu->name || pmu->type < 0)
12552 continue;
12553
12554 ret = pmu_dev_alloc(pmu);
12555 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
12556 }
12557 pmu_bus_running = 1;
12558 ret = 0;
12559
12560unlock:
12561 mutex_unlock(&pmus_lock);
12562
12563 return ret;
12564}
12565device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020012566
12567#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040012568static struct cgroup_subsys_state *
12569perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020012570{
12571 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020012572
Li Zefan1b15d052011-03-03 14:26:06 +080012573 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020012574 if (!jc)
12575 return ERR_PTR(-ENOMEM);
12576
Stephane Eraniane5d13672011-02-14 11:20:01 +020012577 jc->info = alloc_percpu(struct perf_cgroup_info);
12578 if (!jc->info) {
12579 kfree(jc);
12580 return ERR_PTR(-ENOMEM);
12581 }
12582
Stephane Eraniane5d13672011-02-14 11:20:01 +020012583 return &jc->css;
12584}
12585
Tejun Heoeb954192013-08-08 20:11:23 -040012586static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020012587{
Tejun Heoeb954192013-08-08 20:11:23 -040012588 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
12589
Stephane Eraniane5d13672011-02-14 11:20:01 +020012590 free_percpu(jc->info);
12591 kfree(jc);
12592}
12593
12594static int __perf_cgroup_move(void *info)
12595{
12596 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010012597 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020012598 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010012599 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020012600 return 0;
12601}
12602
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050012603static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020012604{
Tejun Heobb9d97b2011-12-12 18:12:21 -080012605 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050012606 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080012607
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050012608 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080012609 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020012610}
12611
Tejun Heo073219e2014-02-08 10:36:58 -050012612struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080012613 .css_alloc = perf_cgroup_css_alloc,
12614 .css_free = perf_cgroup_css_free,
Tejun Heobb9d97b2011-12-12 18:12:21 -080012615 .attach = perf_cgroup_attach,
Tejun Heo968ebff2017-01-29 14:35:20 -050012616 /*
12617 * Implicitly enable on dfl hierarchy so that perf events can
12618 * always be filtered by cgroup2 path as long as perf_event
12619 * controller is not mounted on a legacy hierarchy.
12620 */
12621 .implicit_on_dfl = true,
Tejun Heo8cfd8142017-07-21 11:14:51 -040012622 .threaded = true,
Stephane Eraniane5d13672011-02-14 11:20:01 +020012623};
12624#endif /* CONFIG_CGROUP_PERF */