blob: 3f5fec4dcf016849f01fb232dfb908f8d28b30a9 [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>
Anshuman Khandual03911132020-04-06 20:03:51 -070031#include <linux/hugetlb.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020032#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
Matt Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Steven Rostedt (Red Hat)af658dc2015-04-29 14:36:05 -040039#include <linux/trace_events.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Alexei Starovoitov25415172015-03-25 12:49:20 -070045#include <linux/bpf.h>
46#include <linux/filter.h>
Alexander Shishkin375637b2016-04-27 18:44:46 +030047#include <linux/namei.h>
48#include <linux/parser.h>
Ingo Molnare6017572017-02-01 16:36:40 +010049#include <linux/sched/clock.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010050#include <linux/sched/mm.h>
Hari Bathinie4222672017-03-08 02:11:36 +053051#include <linux/proc_ns.h>
52#include <linux/mount.h>
Ian Rogers6eef8a712020-02-13 23:51:30 -080053#include <linux/min_heap.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020054
Frederic Weisbecker76369132011-05-19 19:55:04 +020055#include "internal.h"
56
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057#include <asm/irq_regs.h>
58
Peter Zijlstra272325c2015-04-15 11:41:58 +020059typedef int (*remote_function_f)(void *);
60
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010061struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020062 struct task_struct *p;
Peter Zijlstra272325c2015-04-15 11:41:58 +020063 remote_function_f func;
Ingo Molnare7e7ee22011-05-04 08:42:29 +020064 void *info;
65 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010066};
67
68static void remote_function(void *data)
69{
70 struct remote_function_call *tfc = data;
71 struct task_struct *p = tfc->p;
72
73 if (p) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +010074 /* -EAGAIN */
75 if (task_cpu(p) != smp_processor_id())
76 return;
77
78 /*
79 * Now that we're on right CPU with IRQs disabled, we can test
80 * if we hit the right task without races.
81 */
82
83 tfc->ret = -ESRCH; /* No such (running) process */
84 if (p != current)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010085 return;
86 }
87
88 tfc->ret = tfc->func(tfc->info);
89}
90
91/**
92 * task_function_call - call a function on the cpu on which a task runs
93 * @p: the task to evaluate
94 * @func: the function to be called
95 * @info: the function call argument
96 *
97 * Calls the function @func when the task is currently running. This might
Barret Rhoden2ed6edd2020-04-14 18:29:20 -040098 * be on the current CPU, which just calls the function directly. This will
99 * retry due to any failures in smp_call_function_single(), such as if the
100 * task_cpu() goes offline concurrently.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100101 *
Barret Rhoden2ed6edd2020-04-14 18:29:20 -0400102 * returns @func return value or -ESRCH when the process isn't running
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100103 */
104static int
Peter Zijlstra272325c2015-04-15 11:41:58 +0200105task_function_call(struct task_struct *p, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100106{
107 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200108 .p = p,
109 .func = func,
110 .info = info,
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100111 .ret = -EAGAIN,
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100112 };
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100113 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100114
Barret Rhoden2ed6edd2020-04-14 18:29:20 -0400115 for (;;) {
116 ret = smp_call_function_single(task_cpu(p), remote_function,
117 &data, 1);
118 ret = !ret ? data.ret : -EAGAIN;
119
120 if (ret != -EAGAIN)
121 break;
122
123 cond_resched();
124 }
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100125
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100126 return ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100127}
128
129/**
130 * cpu_function_call - call a function on the cpu
131 * @func: the function to be called
132 * @info: the function call argument
133 *
134 * Calls the function @func on the remote cpu.
135 *
136 * returns: @func return value or -ENXIO when the cpu is offline
137 */
Peter Zijlstra272325c2015-04-15 11:41:58 +0200138static int cpu_function_call(int cpu, remote_function_f func, void *info)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100139{
140 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200141 .p = NULL,
142 .func = func,
143 .info = info,
144 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100145 };
146
147 smp_call_function_single(cpu, remote_function, &data, 1);
148
149 return data.ret;
150}
151
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100152static inline struct perf_cpu_context *
153__get_cpu_context(struct perf_event_context *ctx)
154{
155 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
156}
157
158static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
159 struct perf_event_context *ctx)
160{
161 raw_spin_lock(&cpuctx->ctx.lock);
162 if (ctx)
163 raw_spin_lock(&ctx->lock);
164}
165
166static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
167 struct perf_event_context *ctx)
168{
169 if (ctx)
170 raw_spin_unlock(&ctx->lock);
171 raw_spin_unlock(&cpuctx->ctx.lock);
172}
173
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100174#define TASK_TOMBSTONE ((void *)-1L)
175
176static bool is_kernel_event(struct perf_event *event)
177{
Peter Zijlstraf47c02c2016-01-26 12:30:14 +0100178 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100179}
180
Peter Zijlstra39a43642016-01-11 12:46:35 +0100181/*
182 * On task ctx scheduling...
183 *
184 * When !ctx->nr_events a task context will not be scheduled. This means
185 * we can disable the scheduler hooks (for performance) without leaving
186 * pending task ctx state.
187 *
188 * This however results in two special cases:
189 *
190 * - removing the last event from a task ctx; this is relatively straight
191 * forward and is done in __perf_remove_from_context.
192 *
193 * - adding the first event to a task ctx; this is tricky because we cannot
194 * rely on ctx->is_active and therefore cannot use event_function_call().
195 * See perf_install_in_context().
196 *
Peter Zijlstra39a43642016-01-11 12:46:35 +0100197 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
198 */
199
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100200typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
201 struct perf_event_context *, void *);
202
203struct event_function_struct {
204 struct perf_event *event;
205 event_f func;
206 void *data;
207};
208
209static int event_function(void *info)
210{
211 struct event_function_struct *efs = info;
212 struct perf_event *event = efs->event;
213 struct perf_event_context *ctx = event->ctx;
214 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
215 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100216 int ret = 0;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100217
Frederic Weisbecker16444642017-11-06 16:01:24 +0100218 lockdep_assert_irqs_disabled();
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100219
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100220 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100221 /*
222 * Since we do the IPI call without holding ctx->lock things can have
223 * changed, double check we hit the task we set out to hit.
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100224 */
225 if (ctx->task) {
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100226 if (ctx->task != current) {
Peter Zijlstra0da4cf32016-02-24 18:45:51 +0100227 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100228 goto unlock;
229 }
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100230
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100231 /*
232 * We only use event_function_call() on established contexts,
233 * and event_function() is only ever called when active (or
234 * rather, we'll have bailed in task_function_call() or the
235 * above ctx->task != current test), therefore we must have
236 * ctx->is_active here.
237 */
238 WARN_ON_ONCE(!ctx->is_active);
239 /*
240 * And since we have ctx->is_active, cpuctx->task_ctx must
241 * match.
242 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100243 WARN_ON_ONCE(task_ctx != ctx);
244 } else {
245 WARN_ON_ONCE(&cpuctx->ctx != ctx);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100246 }
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100247
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100248 efs->func(event, cpuctx, ctx, efs->data);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100249unlock:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100250 perf_ctx_unlock(cpuctx, task_ctx);
251
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100252 return ret;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100253}
254
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100255static void event_function_call(struct perf_event *event, event_f func, void *data)
Peter Zijlstra00179602015-11-30 16:26:35 +0100256{
257 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100258 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100259 struct event_function_struct efs = {
260 .event = event,
261 .func = func,
262 .data = data,
263 };
Peter Zijlstra00179602015-11-30 16:26:35 +0100264
Peter Zijlstrac97f4732016-01-14 10:51:03 +0100265 if (!event->parent) {
266 /*
267 * If this is a !child event, we must hold ctx::mutex to
268 * stabilize the the event->ctx relation. See
269 * perf_event_ctx_lock().
270 */
271 lockdep_assert_held(&ctx->mutex);
272 }
Peter Zijlstra00179602015-11-30 16:26:35 +0100273
274 if (!task) {
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100275 cpu_function_call(event->cpu, event_function, &efs);
Peter Zijlstra00179602015-11-30 16:26:35 +0100276 return;
277 }
278
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100279 if (task == TASK_TOMBSTONE)
280 return;
281
Peter Zijlstraa0963092016-02-24 18:45:50 +0100282again:
Peter Zijlstrafae3fde2016-01-11 15:00:50 +0100283 if (!task_function_call(task, event_function, &efs))
Peter Zijlstra00179602015-11-30 16:26:35 +0100284 return;
285
286 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra63b6da32016-01-14 16:05:37 +0100287 /*
288 * Reload the task pointer, it might have been changed by
289 * a concurrent perf_event_context_sched_out().
290 */
291 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +0100292 if (task == TASK_TOMBSTONE) {
293 raw_spin_unlock_irq(&ctx->lock);
294 return;
Peter Zijlstra00179602015-11-30 16:26:35 +0100295 }
Peter Zijlstraa0963092016-02-24 18:45:50 +0100296 if (ctx->is_active) {
297 raw_spin_unlock_irq(&ctx->lock);
298 goto again;
299 }
300 func(event, NULL, ctx, data);
Peter Zijlstra00179602015-11-30 16:26:35 +0100301 raw_spin_unlock_irq(&ctx->lock);
302}
303
Peter Zijlstracca20942016-08-16 13:33:26 +0200304/*
305 * Similar to event_function_call() + event_function(), but hard assumes IRQs
306 * are already disabled and we're on the right CPU.
307 */
308static void event_function_local(struct perf_event *event, event_f func, void *data)
309{
310 struct perf_event_context *ctx = event->ctx;
311 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
312 struct task_struct *task = READ_ONCE(ctx->task);
313 struct perf_event_context *task_ctx = NULL;
314
Frederic Weisbecker16444642017-11-06 16:01:24 +0100315 lockdep_assert_irqs_disabled();
Peter Zijlstracca20942016-08-16 13:33:26 +0200316
317 if (task) {
318 if (task == TASK_TOMBSTONE)
319 return;
320
321 task_ctx = ctx;
322 }
323
324 perf_ctx_lock(cpuctx, task_ctx);
325
326 task = ctx->task;
327 if (task == TASK_TOMBSTONE)
328 goto unlock;
329
330 if (task) {
331 /*
332 * We must be either inactive or active and the right task,
333 * otherwise we're screwed, since we cannot IPI to somewhere
334 * else.
335 */
336 if (ctx->is_active) {
337 if (WARN_ON_ONCE(task != current))
338 goto unlock;
339
340 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx))
341 goto unlock;
342 }
343 } else {
344 WARN_ON_ONCE(&cpuctx->ctx != ctx);
345 }
346
347 func(event, cpuctx, ctx, data);
348unlock:
349 perf_ctx_unlock(cpuctx, task_ctx);
350}
351
Stephane Eraniane5d13672011-02-14 11:20:01 +0200352#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
353 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100354 PERF_FLAG_PID_CGROUP |\
355 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200356
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100357/*
358 * branch priv levels that need permission checks
359 */
360#define PERF_SAMPLE_BRANCH_PERM_PLM \
361 (PERF_SAMPLE_BRANCH_KERNEL |\
362 PERF_SAMPLE_BRANCH_HV)
363
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200364enum event_type_t {
365 EVENT_FLEXIBLE = 0x1,
366 EVENT_PINNED = 0x2,
Peter Zijlstra3cbaa592016-02-24 18:45:47 +0100367 EVENT_TIME = 0x4,
Alexander Shishkin487f05e2017-01-19 18:43:30 +0200368 /* see ctx_resched() for details */
369 EVENT_CPU = 0x8,
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200370 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
371};
372
Stephane Eraniane5d13672011-02-14 11:20:01 +0200373/*
374 * perf_sched_events : >0 events exist
375 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
376 */
Peter Zijlstra9107c892016-02-24 18:45:45 +0100377
378static void perf_sched_delayed(struct work_struct *work);
379DEFINE_STATIC_KEY_FALSE(perf_sched_events);
380static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
381static DEFINE_MUTEX(perf_sched_mutex);
382static atomic_t perf_sched_count;
383
Stephane Eraniane5d13672011-02-14 11:20:01 +0200384static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500385static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Kan Liangf2fb6be2016-03-23 11:24:37 -0700386static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200387
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200388static atomic_t nr_mmap_events __read_mostly;
389static atomic_t nr_comm_events __read_mostly;
Hari Bathinie4222672017-03-08 02:11:36 +0530390static atomic_t nr_namespaces_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200391static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200392static atomic_t nr_freq_events __read_mostly;
Adrian Hunter45ac1402015-07-21 12:44:02 +0300393static atomic_t nr_switch_events __read_mostly;
Song Liu76193a92019-01-17 08:15:13 -0800394static atomic_t nr_ksymbol_events __read_mostly;
Song Liu6ee52e22019-01-17 08:15:15 -0800395static atomic_t nr_bpf_events __read_mostly;
Namhyung Kim96aaab62020-03-25 21:45:28 +0900396static atomic_t nr_cgroup_events __read_mostly;
Adrian Huntere17d43b2020-05-12 15:19:08 +0300397static atomic_t nr_text_poke_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200398
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200399static LIST_HEAD(pmus);
400static DEFINE_MUTEX(pmus_lock);
401static struct srcu_struct pmus_srcu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +0200402static cpumask_var_t perf_online_mask;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200403
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200404/*
405 * perf event paranoia level:
406 * -1 - not paranoid at all
407 * 0 - disallow raw tracepoint access for unpriv
408 * 1 - disallow cpu events for unpriv
409 * 2 - disallow kernel profiling for unpriv
410 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700411int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200412
Frederic Weisbecker20443382011-03-31 03:33:29 +0200413/* Minimum for 512 kiB + 1 user control page */
414int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200415
416/*
417 * max perf event sample rate
418 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700419#define DEFAULT_MAX_SAMPLE_RATE 100000
420#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
421#define DEFAULT_CPU_TIME_MAX_PERCENT 25
422
423int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
424
425static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
426static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
427
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200428static int perf_sample_allowed_ns __read_mostly =
429 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700430
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800431static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700432{
433 u64 tmp = perf_sample_period_ns;
434
435 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100436 tmp = div_u64(tmp, 100);
437 if (!tmp)
438 tmp = 1;
439
440 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700441}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100442
Peter Zijlstra8d5bce02018-03-09 14:56:27 +0100443static bool perf_rotate_context(struct perf_cpu_context *cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +0200444
Peter Zijlstra163ec432011-02-16 11:22:34 +0100445int perf_proc_update_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200446 void *buffer, size_t *lenp, loff_t *ppos)
Peter Zijlstra163ec432011-02-16 11:22:34 +0100447{
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800448 int ret;
449 int perf_cpu = sysctl_perf_cpu_time_max_percent;
Kan Liangab7fdef2016-05-03 00:26:06 -0700450 /*
451 * If throttling is disabled don't allow the write:
452 */
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800453 if (write && (perf_cpu == 100 || perf_cpu == 0))
Kan Liangab7fdef2016-05-03 00:26:06 -0700454 return -EINVAL;
455
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800456 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
457 if (ret || !write)
458 return ret;
459
Peter Zijlstra163ec432011-02-16 11:22:34 +0100460 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700461 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
462 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100463
464 return 0;
465}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200466
Dave Hansen14c63f12013-06-21 08:51:36 -0700467int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
468
469int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200470 void *buffer, size_t *lenp, loff_t *ppos)
Dave Hansen14c63f12013-06-21 08:51:36 -0700471{
Tan Xiaojun1572e452017-02-23 14:04:39 +0800472 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700473
474 if (ret || !write)
475 return ret;
476
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200477 if (sysctl_perf_cpu_time_max_percent == 100 ||
478 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100479 printk(KERN_WARNING
480 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
481 WRITE_ONCE(perf_sample_allowed_ns, 0);
482 } else {
483 update_perf_cpu_limits();
484 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700485
486 return 0;
487}
488
489/*
490 * perf samples are done in some very critical code paths (NMIs).
491 * If they take too much CPU time, the system can lock up and not
492 * get any real work done. This will drop the sample rate when
493 * we detect that events are taking too long.
494 */
495#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200496static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700497
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100498static u64 __report_avg;
499static u64 __report_allowed;
500
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100501static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700502{
David Ahern0d87d7e2016-08-01 13:49:29 -0700503 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100504 "perf: interrupt took too long (%lld > %lld), lowering "
505 "kernel.perf_event_max_sample_rate to %d\n",
506 __report_avg, __report_allowed,
507 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100508}
509
510static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
511
512void perf_sample_event_took(u64 sample_len_ns)
513{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100514 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
515 u64 running_len;
516 u64 avg_len;
517 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700518
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100519 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700520 return;
521
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100522 /* Decay the counter by 1 average sample. */
523 running_len = __this_cpu_read(running_sample_length);
524 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
525 running_len += sample_len_ns;
526 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700527
528 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100529 * Note: this will be biased artifically low until we have
530 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700531 * from having to maintain a count.
532 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100533 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
534 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700535 return;
536
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100537 __report_avg = avg_len;
538 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700539
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100540 /*
541 * Compute a throttle threshold 25% below the current duration.
542 */
543 avg_len += avg_len / 4;
544 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
545 if (avg_len < max)
546 max /= (u32)avg_len;
547 else
548 max = 1;
549
550 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
551 WRITE_ONCE(max_samples_per_tick, max);
552
553 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700554 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
555
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100556 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100557 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100558 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100559 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100560 sysctl_perf_event_sample_rate);
561 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700562}
563
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200564static atomic64_t perf_event_id;
565
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200566static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
567 enum event_type_t event_type);
568
569static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200570 enum event_type_t event_type,
571 struct task_struct *task);
572
573static void update_context_time(struct perf_event_context *ctx);
574static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200575
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200576void __weak perf_event_print_debug(void) { }
577
Matt Fleming84c79912010-10-03 21:41:13 +0100578extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200579{
Matt Fleming84c79912010-10-03 21:41:13 +0100580 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200581}
582
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200583static inline u64 perf_clock(void)
584{
585 return local_clock();
586}
587
Peter Zijlstra34f43922015-02-20 14:05:38 +0100588static inline u64 perf_event_clock(struct perf_event *event)
589{
590 return event->clock();
591}
592
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200593/*
594 * State based event timekeeping...
595 *
596 * The basic idea is to use event->state to determine which (if any) time
597 * fields to increment with the current delta. This means we only need to
598 * update timestamps when we change state or when they are explicitly requested
599 * (read).
600 *
601 * Event groups make things a little more complicated, but not terribly so. The
602 * rules for a group are that if the group leader is OFF the entire group is
603 * OFF, irrespecive of what the group member states are. This results in
604 * __perf_effective_state().
605 *
606 * A futher ramification is that when a group leader flips between OFF and
607 * !OFF, we need to update all group member times.
608 *
609 *
610 * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we
611 * need to make sure the relevant context time is updated before we try and
612 * update our timestamps.
613 */
614
615static __always_inline enum perf_event_state
616__perf_effective_state(struct perf_event *event)
617{
618 struct perf_event *leader = event->group_leader;
619
620 if (leader->state <= PERF_EVENT_STATE_OFF)
621 return leader->state;
622
623 return event->state;
624}
625
626static __always_inline void
627__perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running)
628{
629 enum perf_event_state state = __perf_effective_state(event);
630 u64 delta = now - event->tstamp;
631
632 *enabled = event->total_time_enabled;
633 if (state >= PERF_EVENT_STATE_INACTIVE)
634 *enabled += delta;
635
636 *running = event->total_time_running;
637 if (state >= PERF_EVENT_STATE_ACTIVE)
638 *running += delta;
639}
640
641static void perf_event_update_time(struct perf_event *event)
642{
643 u64 now = perf_event_time(event);
644
645 __perf_update_times(event, now, &event->total_time_enabled,
646 &event->total_time_running);
647 event->tstamp = now;
648}
649
650static void perf_event_update_sibling_time(struct perf_event *leader)
651{
652 struct perf_event *sibling;
653
Peter Zijlstraedb39592018-03-15 17:36:56 +0100654 for_each_sibling_event(sibling, leader)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200655 perf_event_update_time(sibling);
656}
657
658static void
659perf_event_set_state(struct perf_event *event, enum perf_event_state state)
660{
661 if (event->state == state)
662 return;
663
664 perf_event_update_time(event);
665 /*
666 * If a group leader gets enabled/disabled all its siblings
667 * are affected too.
668 */
669 if ((event->state < 0) ^ (state < 0))
670 perf_event_update_sibling_time(event);
671
672 WRITE_ONCE(event->state, state);
673}
674
Stephane Eraniane5d13672011-02-14 11:20:01 +0200675#ifdef CONFIG_CGROUP_PERF
676
Stephane Eraniane5d13672011-02-14 11:20:01 +0200677static inline bool
678perf_cgroup_match(struct perf_event *event)
679{
680 struct perf_event_context *ctx = event->ctx;
681 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
682
Tejun Heoef824fa2013-04-08 19:00:38 -0700683 /* @event doesn't care about cgroup */
684 if (!event->cgrp)
685 return true;
686
687 /* wants specific cgroup scope but @cpuctx isn't associated with any */
688 if (!cpuctx->cgrp)
689 return false;
690
691 /*
692 * Cgroup scoping is recursive. An event enabled for a cgroup is
693 * also enabled for all its descendant cgroups. If @cpuctx's
694 * cgroup is a descendant of @event's (the test covers identity
695 * case), it's a match.
696 */
697 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
698 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200699}
700
Stephane Eraniane5d13672011-02-14 11:20:01 +0200701static inline void perf_detach_cgroup(struct perf_event *event)
702{
Zefan Li4e2ba652014-09-19 16:53:14 +0800703 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200704 event->cgrp = NULL;
705}
706
707static inline int is_cgroup_event(struct perf_event *event)
708{
709 return event->cgrp != NULL;
710}
711
712static inline u64 perf_cgroup_event_time(struct perf_event *event)
713{
714 struct perf_cgroup_info *t;
715
716 t = per_cpu_ptr(event->cgrp->info, event->cpu);
717 return t->time;
718}
719
720static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
721{
722 struct perf_cgroup_info *info;
723 u64 now;
724
725 now = perf_clock();
726
727 info = this_cpu_ptr(cgrp->info);
728
729 info->time += now - info->timestamp;
730 info->timestamp = now;
731}
732
733static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
734{
Song Liuc917e0f22018-03-12 09:59:43 -0700735 struct perf_cgroup *cgrp = cpuctx->cgrp;
736 struct cgroup_subsys_state *css;
737
738 if (cgrp) {
739 for (css = &cgrp->css; css; css = css->parent) {
740 cgrp = container_of(css, struct perf_cgroup, css);
741 __update_cgrp_time(cgrp);
742 }
743 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200744}
745
746static inline void update_cgrp_time_from_event(struct perf_event *event)
747{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200748 struct perf_cgroup *cgrp;
749
Stephane Eraniane5d13672011-02-14 11:20:01 +0200750 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200751 * ensure we access cgroup data only when needed and
752 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200753 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200754 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200755 return;
756
Stephane Eranian614e4c42015-11-12 11:00:04 +0100757 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200758 /*
759 * Do not update time when cgroup is not active
760 */
Colin Ian King28fa7412018-10-29 23:32:11 +0000761 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200762 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200763}
764
765static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200766perf_cgroup_set_timestamp(struct task_struct *task,
767 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200768{
769 struct perf_cgroup *cgrp;
770 struct perf_cgroup_info *info;
Song Liuc917e0f22018-03-12 09:59:43 -0700771 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200772
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200773 /*
774 * ctx->lock held by caller
775 * ensure we do not access cgroup data
776 * unless we have the cgroup pinned (css_get)
777 */
778 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200779 return;
780
Stephane Eranian614e4c42015-11-12 11:00:04 +0100781 cgrp = perf_cgroup_from_task(task, ctx);
Song Liuc917e0f22018-03-12 09:59:43 -0700782
783 for (css = &cgrp->css; css; css = css->parent) {
784 cgrp = container_of(css, struct perf_cgroup, css);
785 info = this_cpu_ptr(cgrp->info);
786 info->timestamp = ctx->timestamp;
787 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200788}
789
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800790static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list);
791
Stephane Eraniane5d13672011-02-14 11:20:01 +0200792#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
793#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
794
795/*
796 * reschedule events based on the cgroup constraint of task.
797 *
798 * mode SWOUT : schedule out everything
799 * mode SWIN : schedule in based on cgroup for next
800 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800801static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200802{
803 struct perf_cpu_context *cpuctx;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800804 struct list_head *list;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200805 unsigned long flags;
806
807 /*
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800808 * Disable interrupts and preemption to avoid this CPU's
809 * cgrp_cpuctx_entry to change under us.
Stephane Eraniane5d13672011-02-14 11:20:01 +0200810 */
811 local_irq_save(flags);
812
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800813 list = this_cpu_ptr(&cgrp_cpuctx_list);
814 list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) {
815 WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200816
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800817 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
818 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200819
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800820 if (mode & PERF_CGROUP_SWOUT) {
821 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
822 /*
823 * must not be done before ctxswout due
824 * to event_filter_match() in event_sched_out()
825 */
826 cpuctx->cgrp = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200827 }
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800828
829 if (mode & PERF_CGROUP_SWIN) {
830 WARN_ON_ONCE(cpuctx->cgrp);
831 /*
832 * set cgrp before ctxsw in to allow
833 * event_filter_match() to not have to pass
834 * task around
835 * we pass the cpuctx->ctx to perf_cgroup_from_task()
836 * because cgorup events are only per-cpu
837 */
838 cpuctx->cgrp = perf_cgroup_from_task(task,
839 &cpuctx->ctx);
840 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
841 }
842 perf_pmu_enable(cpuctx->ctx.pmu);
843 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200844 }
845
Stephane Eraniane5d13672011-02-14 11:20:01 +0200846 local_irq_restore(flags);
847}
848
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200849static inline void perf_cgroup_sched_out(struct task_struct *task,
850 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200851{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200852 struct perf_cgroup *cgrp1;
853 struct perf_cgroup *cgrp2 = NULL;
854
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100855 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200856 /*
857 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100858 * we do not need to pass the ctx here because we know
859 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200860 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100861 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100862 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200863
864 /*
865 * only schedule out current cgroup events if we know
866 * that we are switching to a different cgroup. Otherwise,
867 * do no touch the cgroup events.
868 */
869 if (cgrp1 != cgrp2)
870 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100871
872 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200873}
874
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200875static inline void perf_cgroup_sched_in(struct task_struct *prev,
876 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200877{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200878 struct perf_cgroup *cgrp1;
879 struct perf_cgroup *cgrp2 = NULL;
880
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100881 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200882 /*
883 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100884 * we do not need to pass the ctx here because we know
885 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200886 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100887 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100888 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200889
890 /*
891 * only need to schedule in cgroup events if we are changing
892 * cgroup during ctxsw. Cgroup events were not scheduled
893 * out of ctxsw out if that was not the case.
894 */
895 if (cgrp1 != cgrp2)
896 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100897
898 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200899}
900
Ian Rogersc2283c92020-02-13 23:51:32 -0800901static int perf_cgroup_ensure_storage(struct perf_event *event,
902 struct cgroup_subsys_state *css)
903{
904 struct perf_cpu_context *cpuctx;
905 struct perf_event **storage;
906 int cpu, heap_size, ret = 0;
907
908 /*
909 * Allow storage to have sufficent space for an iterator for each
910 * possibly nested cgroup plus an iterator for events with no cgroup.
911 */
912 for (heap_size = 1; css; css = css->parent)
913 heap_size++;
914
915 for_each_possible_cpu(cpu) {
916 cpuctx = per_cpu_ptr(event->pmu->pmu_cpu_context, cpu);
917 if (heap_size <= cpuctx->heap_size)
918 continue;
919
920 storage = kmalloc_node(heap_size * sizeof(struct perf_event *),
921 GFP_KERNEL, cpu_to_node(cpu));
922 if (!storage) {
923 ret = -ENOMEM;
924 break;
925 }
926
927 raw_spin_lock_irq(&cpuctx->ctx.lock);
928 if (cpuctx->heap_size < heap_size) {
929 swap(cpuctx->heap, storage);
930 if (storage == cpuctx->heap_default)
931 storage = NULL;
932 cpuctx->heap_size = heap_size;
933 }
934 raw_spin_unlock_irq(&cpuctx->ctx.lock);
935
936 kfree(storage);
937 }
938
939 return ret;
940}
941
Stephane Eraniane5d13672011-02-14 11:20:01 +0200942static inline int perf_cgroup_connect(int fd, struct perf_event *event,
943 struct perf_event_attr *attr,
944 struct perf_event *group_leader)
945{
946 struct perf_cgroup *cgrp;
947 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400948 struct fd f = fdget(fd);
949 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200950
Al Viro2903ff02012-08-28 12:52:22 -0400951 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200952 return -EBADF;
953
Al Virob5830432014-10-31 01:22:04 -0400954 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400955 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800956 if (IS_ERR(css)) {
957 ret = PTR_ERR(css);
958 goto out;
959 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200960
Ian Rogersc2283c92020-02-13 23:51:32 -0800961 ret = perf_cgroup_ensure_storage(event, css);
962 if (ret)
963 goto out;
964
Stephane Eraniane5d13672011-02-14 11:20:01 +0200965 cgrp = container_of(css, struct perf_cgroup, css);
966 event->cgrp = cgrp;
967
968 /*
969 * all events in a group must monitor
970 * the same cgroup because a task belongs
971 * to only one perf cgroup at a time
972 */
973 if (group_leader && group_leader->cgrp != cgrp) {
974 perf_detach_cgroup(event);
975 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200976 }
Li Zefan3db272c2011-03-03 14:25:37 +0800977out:
Al Viro2903ff02012-08-28 12:52:22 -0400978 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200979 return ret;
980}
981
982static inline void
983perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
984{
985 struct perf_cgroup_info *t;
986 t = per_cpu_ptr(event->cgrp->info, event->cpu);
987 event->shadow_ctx_time = now - t->timestamp;
988}
989
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700990static inline void
Peter Zijlstra33238c52020-03-18 20:33:37 +0100991perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx)
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700992{
993 struct perf_cpu_context *cpuctx;
994
995 if (!is_cgroup_event(event))
996 return;
997
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700998 /*
999 * Because cgroup events are always per-cpu events,
Song Liu07c59722020-01-22 11:50:27 -08001000 * @ctx == &cpuctx->ctx.
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001001 */
Song Liu07c59722020-01-22 11:50:27 -08001002 cpuctx = container_of(ctx, struct perf_cpu_context, ctx);
leilei.lin33801b92018-03-06 17:36:37 +08001003
1004 /*
1005 * Since setting cpuctx->cgrp is conditional on the current @cgrp
1006 * matching the event's cgroup, we must do this for every new event,
1007 * because if the first would mismatch, the second would not try again
1008 * and we would leave cpuctx->cgrp unset.
1009 */
Peter Zijlstra33238c52020-03-18 20:33:37 +01001010 if (ctx->is_active && !cpuctx->cgrp) {
Tejun Heobe96b312017-10-28 09:49:37 -07001011 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
1012
Tejun Heobe96b312017-10-28 09:49:37 -07001013 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
1014 cpuctx->cgrp = cgrp;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -08001015 }
leilei.lin33801b92018-03-06 17:36:37 +08001016
Peter Zijlstra33238c52020-03-18 20:33:37 +01001017 if (ctx->nr_cgroups++)
leilei.lin33801b92018-03-06 17:36:37 +08001018 return;
1019
Peter Zijlstra33238c52020-03-18 20:33:37 +01001020 list_add(&cpuctx->cgrp_cpuctx_entry,
1021 per_cpu_ptr(&cgrp_cpuctx_list, event->cpu));
1022}
1023
1024static inline void
1025perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx)
1026{
1027 struct perf_cpu_context *cpuctx;
1028
1029 if (!is_cgroup_event(event))
1030 return;
1031
1032 /*
1033 * Because cgroup events are always per-cpu events,
1034 * @ctx == &cpuctx->ctx.
1035 */
1036 cpuctx = container_of(ctx, struct perf_cpu_context, ctx);
1037
1038 if (--ctx->nr_cgroups)
1039 return;
1040
1041 if (ctx->is_active && cpuctx->cgrp)
leilei.lin33801b92018-03-06 17:36:37 +08001042 cpuctx->cgrp = NULL;
1043
Peter Zijlstra33238c52020-03-18 20:33:37 +01001044 list_del(&cpuctx->cgrp_cpuctx_entry);
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001045}
1046
Stephane Eraniane5d13672011-02-14 11:20:01 +02001047#else /* !CONFIG_CGROUP_PERF */
1048
1049static inline bool
1050perf_cgroup_match(struct perf_event *event)
1051{
1052 return true;
1053}
1054
1055static inline void perf_detach_cgroup(struct perf_event *event)
1056{}
1057
1058static inline int is_cgroup_event(struct perf_event *event)
1059{
1060 return 0;
1061}
1062
Stephane Eraniane5d13672011-02-14 11:20:01 +02001063static inline void update_cgrp_time_from_event(struct perf_event *event)
1064{
1065}
1066
1067static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
1068{
1069}
1070
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001071static inline void perf_cgroup_sched_out(struct task_struct *task,
1072 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001073{
1074}
1075
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001076static inline void perf_cgroup_sched_in(struct task_struct *prev,
1077 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001078{
1079}
1080
1081static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
1082 struct perf_event_attr *attr,
1083 struct perf_event *group_leader)
1084{
1085 return -EINVAL;
1086}
1087
1088static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001089perf_cgroup_set_timestamp(struct task_struct *task,
1090 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001091{
1092}
1093
Ben Dooks (Codethink)d00dbd292019-11-06 13:25:27 +00001094static inline void
Stephane Eraniane5d13672011-02-14 11:20:01 +02001095perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
1096{
1097}
1098
1099static inline void
1100perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
1101{
1102}
1103
1104static inline u64 perf_cgroup_event_time(struct perf_event *event)
1105{
1106 return 0;
1107}
1108
1109static inline void
Peter Zijlstra33238c52020-03-18 20:33:37 +01001110perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx)
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001111{
1112}
1113
Peter Zijlstra33238c52020-03-18 20:33:37 +01001114static inline void
1115perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx)
1116{
1117}
Stephane Eraniane5d13672011-02-14 11:20:01 +02001118#endif
1119
Stephane Eranian9e630202013-04-03 14:21:33 +02001120/*
1121 * set default to be dependent on timer tick just
1122 * like original code
1123 */
1124#define PERF_CPU_HRTIMER (1000 / HZ)
1125/*
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08001126 * function must be called with interrupts disabled
Stephane Eranian9e630202013-04-03 14:21:33 +02001127 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001128static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001129{
1130 struct perf_cpu_context *cpuctx;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01001131 bool rotations;
Stephane Eranian9e630202013-04-03 14:21:33 +02001132
Frederic Weisbecker16444642017-11-06 16:01:24 +01001133 lockdep_assert_irqs_disabled();
Stephane Eranian9e630202013-04-03 14:21:33 +02001134
1135 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001136 rotations = perf_rotate_context(cpuctx);
1137
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001138 raw_spin_lock(&cpuctx->hrtimer_lock);
1139 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001140 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001141 else
1142 cpuctx->hrtimer_active = 0;
1143 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001144
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001145 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001146}
1147
Peter Zijlstra272325c2015-04-15 11:41:58 +02001148static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001149{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001150 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001151 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001152 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001153
1154 /* no multiplexing needed for SW PMU */
1155 if (pmu->task_ctx_nr == perf_sw_context)
1156 return;
1157
Stephane Eranian62b85632013-04-03 14:21:34 +02001158 /*
1159 * check default is sane, if not set then force to
1160 * default interval (1/tick)
1161 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001162 interval = pmu->hrtimer_interval_ms;
1163 if (interval < 1)
1164 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001165
Peter Zijlstra272325c2015-04-15 11:41:58 +02001166 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001167
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001168 raw_spin_lock_init(&cpuctx->hrtimer_lock);
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02001169 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001170 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001171}
1172
Peter Zijlstra272325c2015-04-15 11:41:58 +02001173static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001174{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001175 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001176 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001177 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001178
1179 /* not for SW PMU */
1180 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001181 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001182
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001183 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1184 if (!cpuctx->hrtimer_active) {
1185 cpuctx->hrtimer_active = 1;
1186 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02001187 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001188 }
1189 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001190
Peter Zijlstra272325c2015-04-15 11:41:58 +02001191 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001192}
1193
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001194void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001196 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1197 if (!(*count)++)
1198 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001199}
1200
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001201void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001202{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001203 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1204 if (!--(*count))
1205 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001206}
1207
Mark Rutland2fde4f92015-01-07 15:01:54 +00001208static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001209
1210/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001211 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1212 * perf_event_task_tick() are fully serialized because they're strictly cpu
1213 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1214 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001215 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001216static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001217{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001218 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001219
Frederic Weisbecker16444642017-11-06 16:01:24 +01001220 lockdep_assert_irqs_disabled();
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001221
Mark Rutland2fde4f92015-01-07 15:01:54 +00001222 WARN_ON(!list_empty(&ctx->active_ctx_list));
1223
1224 list_add(&ctx->active_ctx_list, head);
1225}
1226
1227static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1228{
Frederic Weisbecker16444642017-11-06 16:01:24 +01001229 lockdep_assert_irqs_disabled();
Mark Rutland2fde4f92015-01-07 15:01:54 +00001230
1231 WARN_ON(list_empty(&ctx->active_ctx_list));
1232
1233 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001234}
1235
1236static void get_ctx(struct perf_event_context *ctx)
1237{
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001238 refcount_inc(&ctx->refcount);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001239}
1240
Kan Liangff9ff922020-07-03 05:49:21 -07001241static void *alloc_task_ctx_data(struct pmu *pmu)
1242{
Kan Liang217c2a62020-07-03 05:49:22 -07001243 if (pmu->task_ctx_cache)
1244 return kmem_cache_zalloc(pmu->task_ctx_cache, GFP_KERNEL);
1245
Kan Liang5a099282020-07-03 05:49:24 -07001246 return NULL;
Kan Liangff9ff922020-07-03 05:49:21 -07001247}
1248
1249static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data)
1250{
Kan Liang217c2a62020-07-03 05:49:22 -07001251 if (pmu->task_ctx_cache && task_ctx_data)
1252 kmem_cache_free(pmu->task_ctx_cache, task_ctx_data);
Kan Liangff9ff922020-07-03 05:49:21 -07001253}
1254
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001255static void free_ctx(struct rcu_head *head)
1256{
1257 struct perf_event_context *ctx;
1258
1259 ctx = container_of(head, struct perf_event_context, rcu_head);
Kan Liangff9ff922020-07-03 05:49:21 -07001260 free_task_ctx_data(ctx->pmu, ctx->task_ctx_data);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001261 kfree(ctx);
1262}
1263
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264static void put_ctx(struct perf_event_context *ctx)
1265{
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001266 if (refcount_dec_and_test(&ctx->refcount)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267 if (ctx->parent_ctx)
1268 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001269 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270 put_task_struct(ctx->task);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001271 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001272 }
1273}
1274
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001275/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001276 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1277 * perf_pmu_migrate_context() we need some magic.
1278 *
1279 * Those places that change perf_event::ctx will hold both
1280 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1281 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001282 * Lock ordering is by mutex address. There are two other sites where
1283 * perf_event_context::mutex nests and those are:
1284 *
1285 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001286 * perf_event_exit_event()
1287 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001288 *
1289 * - perf_event_init_context() [ parent, 0 ]
1290 * inherit_task_group()
1291 * inherit_group()
1292 * inherit_event()
1293 * perf_event_alloc()
1294 * perf_init_event()
1295 * perf_try_init_event() [ child , 1 ]
1296 *
1297 * While it appears there is an obvious deadlock here -- the parent and child
1298 * nesting levels are inverted between the two. This is in fact safe because
1299 * life-time rules separate them. That is an exiting task cannot fork, and a
1300 * spawning task cannot (yet) exit.
1301 *
1302 * But remember that that these are parent<->child context relations, and
1303 * migration does not affect children, therefore these two orderings should not
1304 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001305 *
1306 * The change in perf_event::ctx does not affect children (as claimed above)
1307 * because the sys_perf_event_open() case will install a new event and break
1308 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1309 * concerned with cpuctx and that doesn't have children.
1310 *
1311 * The places that change perf_event::ctx will issue:
1312 *
1313 * perf_remove_from_context();
1314 * synchronize_rcu();
1315 * perf_install_in_context();
1316 *
1317 * to affect the change. The remove_from_context() + synchronize_rcu() should
1318 * quiesce the event, after which we can install it in the new location. This
1319 * means that only external vectors (perf_fops, prctl) can perturb the event
1320 * while in transit. Therefore all such accessors should also acquire
1321 * perf_event_context::mutex to serialize against this.
1322 *
1323 * However; because event->ctx can change while we're waiting to acquire
1324 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1325 * function.
1326 *
1327 * Lock order:
Bernd Edlinger69143032020-03-20 21:27:55 +01001328 * exec_update_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001329 * task_struct::perf_event_mutex
1330 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001331 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001332 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001333 * perf_event::mmap_mutex
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001334 * mmap_lock
Alexander Shishkin18736ee2019-02-15 13:56:54 +02001335 * perf_addr_filters_head::lock
Peter Zijlstra82d94852018-01-09 13:10:30 +01001336 *
1337 * cpu_hotplug_lock
1338 * pmus_lock
1339 * cpuctx->mutex / perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001340 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001341static struct perf_event_context *
1342perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001343{
1344 struct perf_event_context *ctx;
1345
1346again:
1347 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -07001348 ctx = READ_ONCE(event->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001349 if (!refcount_inc_not_zero(&ctx->refcount)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001350 rcu_read_unlock();
1351 goto again;
1352 }
1353 rcu_read_unlock();
1354
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001355 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001356 if (event->ctx != ctx) {
1357 mutex_unlock(&ctx->mutex);
1358 put_ctx(ctx);
1359 goto again;
1360 }
1361
1362 return ctx;
1363}
1364
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001365static inline struct perf_event_context *
1366perf_event_ctx_lock(struct perf_event *event)
1367{
1368 return perf_event_ctx_lock_nested(event, 0);
1369}
1370
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001371static void perf_event_ctx_unlock(struct perf_event *event,
1372 struct perf_event_context *ctx)
1373{
1374 mutex_unlock(&ctx->mutex);
1375 put_ctx(ctx);
1376}
1377
1378/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001379 * This must be done under the ctx->lock, such as to serialize against
1380 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1381 * calling scheduler related locks and ctx->lock nests inside those.
1382 */
1383static __must_check struct perf_event_context *
1384unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001386 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1387
1388 lockdep_assert_held(&ctx->lock);
1389
1390 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001391 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001392 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001393
1394 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395}
1396
Oleg Nesterov1d953112017-08-22 17:59:28 +02001397static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
1398 enum pid_type type)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001399{
Oleg Nesterov1d953112017-08-22 17:59:28 +02001400 u32 nr;
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001401 /*
1402 * only top level events have the pid namespace they were created in
1403 */
1404 if (event->parent)
1405 event = event->parent;
1406
Oleg Nesterov1d953112017-08-22 17:59:28 +02001407 nr = __task_pid_nr_ns(p, type, event->ns);
1408 /* avoid -1 if it is idle thread or runs in another ns */
1409 if (!nr && !pid_alive(p))
1410 nr = -1;
1411 return nr;
1412}
1413
1414static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1415{
Eric W. Biederman6883f812017-06-04 04:32:13 -05001416 return perf_event_pid_type(event, p, PIDTYPE_TGID);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001417}
1418
1419static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1420{
Oleg Nesterov1d953112017-08-22 17:59:28 +02001421 return perf_event_pid_type(event, p, PIDTYPE_PID);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001422}
1423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001424/*
1425 * If we inherit events we want to return the parent event id
1426 * to userspace.
1427 */
1428static u64 primary_event_id(struct perf_event *event)
1429{
1430 u64 id = event->id;
1431
1432 if (event->parent)
1433 id = event->parent->id;
1434
1435 return id;
1436}
1437
1438/*
1439 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001440 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001441 * This has to cope with with the fact that until it is locked,
1442 * the context could get moved to another task.
1443 */
1444static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001445perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001446{
1447 struct perf_event_context *ctx;
1448
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001449retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001450 /*
1451 * One of the few rules of preemptible RCU is that one cannot do
1452 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001453 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001454 * rcu_read_unlock_special().
1455 *
1456 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001457 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001458 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001459 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001460 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001461 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001462 if (ctx) {
1463 /*
1464 * If this context is a clone of another, it might
1465 * get swapped for another underneath us by
1466 * perf_event_task_sched_out, though the
1467 * rcu_read_lock() protects us from any context
1468 * getting freed. Lock the context and check if it
1469 * got swapped before we could get the lock, and retry
1470 * if so. If we locked the right context, then it
1471 * can't get swapped on us any more.
1472 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001473 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001474 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001475 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001476 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001477 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001478 goto retry;
1479 }
1480
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001481 if (ctx->task == TASK_TOMBSTONE ||
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001482 !refcount_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001483 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001485 } else {
1486 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487 }
1488 }
1489 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001490 if (!ctx)
1491 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 return ctx;
1493}
1494
1495/*
1496 * Get the context for a task and increment its pin_count so it
1497 * can't get swapped to another task. This also increments its
1498 * reference count so that the context can't get freed.
1499 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001500static struct perf_event_context *
1501perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502{
1503 struct perf_event_context *ctx;
1504 unsigned long flags;
1505
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001506 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001507 if (ctx) {
1508 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001509 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001510 }
1511 return ctx;
1512}
1513
1514static void perf_unpin_context(struct perf_event_context *ctx)
1515{
1516 unsigned long flags;
1517
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001518 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001520 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001521}
1522
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001523/*
1524 * Update the record of the current time in a context.
1525 */
1526static void update_context_time(struct perf_event_context *ctx)
1527{
1528 u64 now = perf_clock();
1529
1530 ctx->time += now - ctx->timestamp;
1531 ctx->timestamp = now;
1532}
1533
Stephane Eranian41587552011-01-03 18:20:01 +02001534static u64 perf_event_time(struct perf_event *event)
1535{
1536 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001537
1538 if (is_cgroup_event(event))
1539 return perf_cgroup_event_time(event);
1540
Stephane Eranian41587552011-01-03 18:20:01 +02001541 return ctx ? ctx->time : 0;
1542}
1543
Alexander Shishkin487f05e2017-01-19 18:43:30 +02001544static enum event_type_t get_event_type(struct perf_event *event)
1545{
1546 struct perf_event_context *ctx = event->ctx;
1547 enum event_type_t event_type;
1548
1549 lockdep_assert_held(&ctx->lock);
1550
Alexander Shishkin3bda69c2017-07-18 14:08:34 +03001551 /*
1552 * It's 'group type', really, because if our group leader is
1553 * pinned, so are we.
1554 */
1555 if (event->group_leader != event)
1556 event = event->group_leader;
1557
Alexander Shishkin487f05e2017-01-19 18:43:30 +02001558 event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE;
1559 if (!ctx->task)
1560 event_type |= EVENT_CPU;
1561
1562 return event_type;
1563}
1564
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001565/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001566 * Helper function to initialize event group nodes.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001567 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001568static void init_event_group(struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001569{
1570 RB_CLEAR_NODE(&event->group_node);
1571 event->group_index = 0;
1572}
1573
1574/*
1575 * Extract pinned or flexible groups from the context
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001576 * based on event attrs bits.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001577 */
1578static struct perf_event_groups *
1579get_event_groups(struct perf_event *event, struct perf_event_context *ctx)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001580{
1581 if (event->attr.pinned)
1582 return &ctx->pinned_groups;
1583 else
1584 return &ctx->flexible_groups;
1585}
1586
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001587/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001588 * Helper function to initializes perf_event_group trees.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001589 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001590static void perf_event_groups_init(struct perf_event_groups *groups)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001591{
1592 groups->tree = RB_ROOT;
1593 groups->index = 0;
1594}
1595
1596/*
1597 * Compare function for event groups;
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001598 *
1599 * Implements complex key that first sorts by CPU and then by virtual index
1600 * which provides ordering when rotating groups for the same CPU.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001601 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001602static bool
1603perf_event_groups_less(struct perf_event *left, struct perf_event *right)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001604{
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001605 if (left->cpu < right->cpu)
1606 return true;
1607 if (left->cpu > right->cpu)
1608 return false;
1609
Ian Rogers95ed6c72020-02-13 23:51:33 -08001610#ifdef CONFIG_CGROUP_PERF
1611 if (left->cgrp != right->cgrp) {
1612 if (!left->cgrp || !left->cgrp->css.cgroup) {
1613 /*
1614 * Left has no cgroup but right does, no cgroups come
1615 * first.
1616 */
1617 return true;
1618 }
Dan Carpentera6763622020-03-12 13:56:37 +03001619 if (!right->cgrp || !right->cgrp->css.cgroup) {
Ian Rogers95ed6c72020-02-13 23:51:33 -08001620 /*
1621 * Right has no cgroup but left does, no cgroups come
1622 * first.
1623 */
1624 return false;
1625 }
1626 /* Two dissimilar cgroups, order by id. */
1627 if (left->cgrp->css.cgroup->kn->id < right->cgrp->css.cgroup->kn->id)
1628 return true;
1629
1630 return false;
1631 }
1632#endif
1633
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001634 if (left->group_index < right->group_index)
1635 return true;
1636 if (left->group_index > right->group_index)
1637 return false;
1638
1639 return false;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001640}
1641
1642/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001643 * Insert @event into @groups' tree; using {@event->cpu, ++@groups->index} for
1644 * key (see perf_event_groups_less). This places it last inside the CPU
1645 * subtree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001646 */
1647static void
1648perf_event_groups_insert(struct perf_event_groups *groups,
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001649 struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001650{
1651 struct perf_event *node_event;
1652 struct rb_node *parent;
1653 struct rb_node **node;
1654
1655 event->group_index = ++groups->index;
1656
1657 node = &groups->tree.rb_node;
1658 parent = *node;
1659
1660 while (*node) {
1661 parent = *node;
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001662 node_event = container_of(*node, struct perf_event, group_node);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001663
1664 if (perf_event_groups_less(event, node_event))
1665 node = &parent->rb_left;
1666 else
1667 node = &parent->rb_right;
1668 }
1669
1670 rb_link_node(&event->group_node, parent, node);
1671 rb_insert_color(&event->group_node, &groups->tree);
1672}
1673
1674/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001675 * Helper function to insert event into the pinned or flexible groups.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001676 */
1677static void
1678add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx)
1679{
1680 struct perf_event_groups *groups;
1681
1682 groups = get_event_groups(event, ctx);
1683 perf_event_groups_insert(groups, event);
1684}
1685
1686/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001687 * Delete a group from a tree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001688 */
1689static void
1690perf_event_groups_delete(struct perf_event_groups *groups,
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001691 struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001692{
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001693 WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) ||
1694 RB_EMPTY_ROOT(&groups->tree));
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001695
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001696 rb_erase(&event->group_node, &groups->tree);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001697 init_event_group(event);
1698}
1699
1700/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001701 * Helper function to delete event from its groups.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001702 */
1703static void
1704del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx)
1705{
1706 struct perf_event_groups *groups;
1707
1708 groups = get_event_groups(event, ctx);
1709 perf_event_groups_delete(groups, event);
1710}
1711
1712/*
Ian Rogers95ed6c72020-02-13 23:51:33 -08001713 * Get the leftmost event in the cpu/cgroup subtree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001714 */
1715static struct perf_event *
Ian Rogers95ed6c72020-02-13 23:51:33 -08001716perf_event_groups_first(struct perf_event_groups *groups, int cpu,
1717 struct cgroup *cgrp)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001718{
1719 struct perf_event *node_event = NULL, *match = NULL;
1720 struct rb_node *node = groups->tree.rb_node;
Ian Rogers95ed6c72020-02-13 23:51:33 -08001721#ifdef CONFIG_CGROUP_PERF
1722 u64 node_cgrp_id, cgrp_id = 0;
1723
1724 if (cgrp)
1725 cgrp_id = cgrp->kn->id;
1726#endif
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001727
1728 while (node) {
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001729 node_event = container_of(node, struct perf_event, group_node);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001730
1731 if (cpu < node_event->cpu) {
1732 node = node->rb_left;
Ian Rogers95ed6c72020-02-13 23:51:33 -08001733 continue;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001734 }
Ian Rogers95ed6c72020-02-13 23:51:33 -08001735 if (cpu > node_event->cpu) {
1736 node = node->rb_right;
1737 continue;
1738 }
1739#ifdef CONFIG_CGROUP_PERF
1740 node_cgrp_id = 0;
1741 if (node_event->cgrp && node_event->cgrp->css.cgroup)
1742 node_cgrp_id = node_event->cgrp->css.cgroup->kn->id;
1743
1744 if (cgrp_id < node_cgrp_id) {
1745 node = node->rb_left;
1746 continue;
1747 }
1748 if (cgrp_id > node_cgrp_id) {
1749 node = node->rb_right;
1750 continue;
1751 }
1752#endif
1753 match = node_event;
1754 node = node->rb_left;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001755 }
1756
1757 return match;
1758}
1759
1760/*
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001761 * Like rb_entry_next_safe() for the @cpu subtree.
1762 */
1763static struct perf_event *
1764perf_event_groups_next(struct perf_event *event)
1765{
1766 struct perf_event *next;
Ian Rogers95ed6c72020-02-13 23:51:33 -08001767#ifdef CONFIG_CGROUP_PERF
1768 u64 curr_cgrp_id = 0;
1769 u64 next_cgrp_id = 0;
1770#endif
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001771
1772 next = rb_entry_safe(rb_next(&event->group_node), typeof(*event), group_node);
Ian Rogers95ed6c72020-02-13 23:51:33 -08001773 if (next == NULL || next->cpu != event->cpu)
1774 return NULL;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001775
Ian Rogers95ed6c72020-02-13 23:51:33 -08001776#ifdef CONFIG_CGROUP_PERF
1777 if (event->cgrp && event->cgrp->css.cgroup)
1778 curr_cgrp_id = event->cgrp->css.cgroup->kn->id;
1779
1780 if (next->cgrp && next->cgrp->css.cgroup)
1781 next_cgrp_id = next->cgrp->css.cgroup->kn->id;
1782
1783 if (curr_cgrp_id != next_cgrp_id)
1784 return NULL;
1785#endif
1786 return next;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001787}
1788
1789/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001790 * Iterate through the whole groups tree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001791 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +01001792#define perf_event_groups_for_each(event, groups) \
1793 for (event = rb_entry_safe(rb_first(&((groups)->tree)), \
1794 typeof(*event), group_node); event; \
1795 event = rb_entry_safe(rb_next(&event->group_node), \
1796 typeof(*event), group_node))
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001797
1798/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001799 * Add an event from the lists for its context.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001800 * Must be called with ctx->mutex and ctx->lock held.
1801 */
1802static void
1803list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1804{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001805 lockdep_assert_held(&ctx->lock);
1806
Peter Zijlstra8a495422010-05-27 15:47:49 +02001807 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1808 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02001810 event->tstamp = perf_event_time(event);
1811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001813 * If we're a stand alone event or group leader, we go to the context
1814 * list, group events are kept attached to the group so that
1815 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001816 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001817 if (event->group_leader == event) {
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001818 event->group_caps = event->event_caps;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001819 add_event_to_groups(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820 }
1821
1822 list_add_rcu(&event->event_entry, &ctx->event_list);
1823 ctx->nr_events++;
1824 if (event->attr.inherit_stat)
1825 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001826
Peter Zijlstra33238c52020-03-18 20:33:37 +01001827 if (event->state > PERF_EVENT_STATE_OFF)
1828 perf_cgroup_event_enable(event, ctx);
1829
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001830 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001831}
1832
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001833/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001834 * Initialize event state based on the perf_event_attr::disabled.
1835 */
1836static inline void perf_event__state_init(struct perf_event *event)
1837{
1838 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1839 PERF_EVENT_STATE_INACTIVE;
1840}
1841
Peter Zijlstraa7239682015-09-09 19:06:33 +02001842static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001843{
1844 int entry = sizeof(u64); /* value */
1845 int size = 0;
1846 int nr = 1;
1847
1848 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1849 size += sizeof(u64);
1850
1851 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1852 size += sizeof(u64);
1853
1854 if (event->attr.read_format & PERF_FORMAT_ID)
1855 entry += sizeof(u64);
1856
1857 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001858 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001859 size += sizeof(u64);
1860 }
1861
1862 size += entry * nr;
1863 event->read_size = size;
1864}
1865
Peter Zijlstraa7239682015-09-09 19:06:33 +02001866static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001867{
1868 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001869 u16 size = 0;
1870
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001871 if (sample_type & PERF_SAMPLE_IP)
1872 size += sizeof(data->ip);
1873
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001874 if (sample_type & PERF_SAMPLE_ADDR)
1875 size += sizeof(data->addr);
1876
1877 if (sample_type & PERF_SAMPLE_PERIOD)
1878 size += sizeof(data->period);
1879
Andi Kleenc3feedf2013-01-24 16:10:28 +01001880 if (sample_type & PERF_SAMPLE_WEIGHT)
1881 size += sizeof(data->weight);
1882
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001883 if (sample_type & PERF_SAMPLE_READ)
1884 size += event->read_size;
1885
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001886 if (sample_type & PERF_SAMPLE_DATA_SRC)
1887 size += sizeof(data->data_src.val);
1888
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001889 if (sample_type & PERF_SAMPLE_TRANSACTION)
1890 size += sizeof(data->txn);
1891
Kan Liangfc7ce9c2017-08-28 20:52:49 -04001892 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1893 size += sizeof(data->phys_addr);
1894
Namhyung Kim6546b192020-03-25 21:45:29 +09001895 if (sample_type & PERF_SAMPLE_CGROUP)
1896 size += sizeof(data->cgroup);
1897
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001898 event->header_size = size;
1899}
1900
Peter Zijlstraa7239682015-09-09 19:06:33 +02001901/*
1902 * Called at perf_event creation and when events are attached/detached from a
1903 * group.
1904 */
1905static void perf_event__header_size(struct perf_event *event)
1906{
1907 __perf_event_read_size(event,
1908 event->group_leader->nr_siblings);
1909 __perf_event_header_size(event, event->attr.sample_type);
1910}
1911
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001912static void perf_event__id_header_size(struct perf_event *event)
1913{
1914 struct perf_sample_data *data;
1915 u64 sample_type = event->attr.sample_type;
1916 u16 size = 0;
1917
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001918 if (sample_type & PERF_SAMPLE_TID)
1919 size += sizeof(data->tid_entry);
1920
1921 if (sample_type & PERF_SAMPLE_TIME)
1922 size += sizeof(data->time);
1923
Adrian Hunterff3d5272013-08-27 11:23:07 +03001924 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1925 size += sizeof(data->id);
1926
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001927 if (sample_type & PERF_SAMPLE_ID)
1928 size += sizeof(data->id);
1929
1930 if (sample_type & PERF_SAMPLE_STREAM_ID)
1931 size += sizeof(data->stream_id);
1932
1933 if (sample_type & PERF_SAMPLE_CPU)
1934 size += sizeof(data->cpu_entry);
1935
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001936 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001937}
1938
Peter Zijlstraa7239682015-09-09 19:06:33 +02001939static bool perf_event_validate_size(struct perf_event *event)
1940{
1941 /*
1942 * The values computed here will be over-written when we actually
1943 * attach the event.
1944 */
1945 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1946 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1947 perf_event__id_header_size(event);
1948
1949 /*
1950 * Sum the lot; should not exceed the 64k limit we have on records.
1951 * Conservative limit to allow for callchains and other variable fields.
1952 */
1953 if (event->read_size + event->header_size +
1954 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1955 return false;
1956
1957 return true;
1958}
1959
Peter Zijlstra8a495422010-05-27 15:47:49 +02001960static void perf_group_attach(struct perf_event *event)
1961{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001962 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001963
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01001964 lockdep_assert_held(&event->ctx->lock);
1965
Peter Zijlstra74c33372010-10-15 11:40:29 +02001966 /*
1967 * We can have double attach due to group movement in perf_event_open.
1968 */
1969 if (event->attach_state & PERF_ATTACH_GROUP)
1970 return;
1971
Peter Zijlstra8a495422010-05-27 15:47:49 +02001972 event->attach_state |= PERF_ATTACH_GROUP;
1973
1974 if (group_leader == event)
1975 return;
1976
Peter Zijlstra652884f2015-01-23 11:20:10 +01001977 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1978
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001979 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001980
Peter Zijlstra8343aae2017-11-13 14:28:33 +01001981 list_add_tail(&event->sibling_list, &group_leader->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001982 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001983
1984 perf_event__header_size(group_leader);
1985
Peter Zijlstraedb39592018-03-15 17:36:56 +01001986 for_each_sibling_event(pos, group_leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001987 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001988}
1989
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001991 * Remove an event from the lists for its context.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001992 * Must be called with ctx->mutex and ctx->lock held.
1993 */
1994static void
1995list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1996{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001997 WARN_ON_ONCE(event->ctx != ctx);
1998 lockdep_assert_held(&ctx->lock);
1999
Peter Zijlstra8a495422010-05-27 15:47:49 +02002000 /*
2001 * We can have double detach due to exit/hot-unplug + close.
2002 */
2003 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002005
2006 event->attach_state &= ~PERF_ATTACH_CONTEXT;
2007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008 ctx->nr_events--;
2009 if (event->attr.inherit_stat)
2010 ctx->nr_stat--;
2011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002012 list_del_rcu(&event->event_entry);
2013
Peter Zijlstra8a495422010-05-27 15:47:49 +02002014 if (event->group_leader == event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002015 del_event_from_groups(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016
Stephane Eranianb2e74a22009-11-26 09:24:30 -08002017 /*
2018 * If event was in error state, then keep it
2019 * that way, otherwise bogus counts will be
2020 * returned on read(). The only way to get out
2021 * of error state is by explicit re-enabling
2022 * of the event
2023 */
Peter Zijlstra33238c52020-03-18 20:33:37 +01002024 if (event->state > PERF_EVENT_STATE_OFF) {
2025 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002026 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002027 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002028
2029 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02002030}
2031
Alexander Shishkinab437622019-08-06 11:46:00 +03002032static int
2033perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event)
2034{
2035 if (!has_aux(aux_event))
2036 return 0;
2037
2038 if (!event->pmu->aux_output_match)
2039 return 0;
2040
2041 return event->pmu->aux_output_match(aux_event);
2042}
2043
2044static void put_event(struct perf_event *event);
2045static void event_sched_out(struct perf_event *event,
2046 struct perf_cpu_context *cpuctx,
2047 struct perf_event_context *ctx);
2048
2049static void perf_put_aux_event(struct perf_event *event)
2050{
2051 struct perf_event_context *ctx = event->ctx;
2052 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2053 struct perf_event *iter;
2054
2055 /*
2056 * If event uses aux_event tear down the link
2057 */
2058 if (event->aux_event) {
2059 iter = event->aux_event;
2060 event->aux_event = NULL;
2061 put_event(iter);
2062 return;
2063 }
2064
2065 /*
2066 * If the event is an aux_event, tear down all links to
2067 * it from other events.
2068 */
2069 for_each_sibling_event(iter, event->group_leader) {
2070 if (iter->aux_event != event)
2071 continue;
2072
2073 iter->aux_event = NULL;
2074 put_event(event);
2075
2076 /*
2077 * If it's ACTIVE, schedule it out and put it into ERROR
2078 * state so that we don't try to schedule it again. Note
2079 * that perf_event_enable() will clear the ERROR status.
2080 */
2081 event_sched_out(iter, cpuctx, ctx);
2082 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
2083 }
2084}
2085
Alexander Shishkina4faf002019-10-25 17:08:33 +03002086static bool perf_need_aux_event(struct perf_event *event)
2087{
2088 return !!event->attr.aux_output || !!event->attr.aux_sample_size;
2089}
2090
Alexander Shishkinab437622019-08-06 11:46:00 +03002091static int perf_get_aux_event(struct perf_event *event,
2092 struct perf_event *group_leader)
2093{
2094 /*
2095 * Our group leader must be an aux event if we want to be
2096 * an aux_output. This way, the aux event will precede its
2097 * aux_output events in the group, and therefore will always
2098 * schedule first.
2099 */
2100 if (!group_leader)
2101 return 0;
2102
Alexander Shishkina4faf002019-10-25 17:08:33 +03002103 /*
2104 * aux_output and aux_sample_size are mutually exclusive.
2105 */
2106 if (event->attr.aux_output && event->attr.aux_sample_size)
2107 return 0;
2108
2109 if (event->attr.aux_output &&
2110 !perf_aux_output_match(event, group_leader))
2111 return 0;
2112
2113 if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux)
Alexander Shishkinab437622019-08-06 11:46:00 +03002114 return 0;
2115
2116 if (!atomic_long_inc_not_zero(&group_leader->refcount))
2117 return 0;
2118
2119 /*
2120 * Link aux_outputs to their aux event; this is undone in
2121 * perf_group_detach() by perf_put_aux_event(). When the
2122 * group in torn down, the aux_output events loose their
2123 * link to the aux_event and can't schedule any more.
2124 */
2125 event->aux_event = group_leader;
2126
2127 return 1;
2128}
2129
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002130static inline struct list_head *get_event_list(struct perf_event *event)
2131{
2132 struct perf_event_context *ctx = event->ctx;
2133 return event->attr.pinned ? &ctx->pinned_active : &ctx->flexible_active;
2134}
2135
Kan Liang9f0c4fa2020-07-23 10:11:10 -07002136/*
2137 * Events that have PERF_EV_CAP_SIBLING require being part of a group and
2138 * cannot exist on their own, schedule them out and move them into the ERROR
2139 * state. Also see _perf_event_enable(), it will not be able to recover
2140 * this ERROR state.
2141 */
2142static inline void perf_remove_sibling_event(struct perf_event *event)
2143{
2144 struct perf_event_context *ctx = event->ctx;
2145 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2146
2147 event_sched_out(event, cpuctx, ctx);
2148 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
2149}
2150
Peter Zijlstra8a495422010-05-27 15:47:49 +02002151static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02002152{
Kan Liang9f0c4fa2020-07-23 10:11:10 -07002153 struct perf_event *leader = event->group_leader;
Peter Zijlstra050735b2010-05-11 11:51:53 +02002154 struct perf_event *sibling, *tmp;
Peter Zijlstra66681282017-11-13 14:28:38 +01002155 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002156
Peter Zijlstra66681282017-11-13 14:28:38 +01002157 lockdep_assert_held(&ctx->lock);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002158
Peter Zijlstra8a495422010-05-27 15:47:49 +02002159 /*
2160 * We can have double detach due to exit/hot-unplug + close.
2161 */
2162 if (!(event->attach_state & PERF_ATTACH_GROUP))
2163 return;
2164
2165 event->attach_state &= ~PERF_ATTACH_GROUP;
2166
Alexander Shishkinab437622019-08-06 11:46:00 +03002167 perf_put_aux_event(event);
2168
Peter Zijlstra8a495422010-05-27 15:47:49 +02002169 /*
2170 * If this is a sibling, remove it from its group.
2171 */
Kan Liang9f0c4fa2020-07-23 10:11:10 -07002172 if (leader != event) {
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002173 list_del_init(&event->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002174 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002175 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002176 }
2177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002178 /*
2179 * If this was a group event with sibling events then
2180 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02002181 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182 */
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002183 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002184
Kan Liang9f0c4fa2020-07-23 10:11:10 -07002185 if (sibling->event_caps & PERF_EV_CAP_SIBLING)
2186 perf_remove_sibling_event(sibling);
2187
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188 sibling->group_leader = sibling;
Mark Rutland24868362018-03-16 12:51:40 +00002189 list_del_init(&sibling->sibling_list);
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002190
2191 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002192 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002193
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002194 if (!RB_EMPTY_NODE(&event->group_node)) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002195 add_event_to_groups(sibling, event->ctx);
Peter Zijlstra66681282017-11-13 14:28:38 +01002196
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002197 if (sibling->state == PERF_EVENT_STATE_ACTIVE)
2198 list_add_tail(&sibling->active_list, get_event_list(sibling));
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002199 }
2200
Peter Zijlstra652884f2015-01-23 11:20:10 +01002201 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002202 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002203
2204out:
Kan Liang9f0c4fa2020-07-23 10:11:10 -07002205 for_each_sibling_event(tmp, leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002206 perf_event__header_size(tmp);
Kan Liang9f0c4fa2020-07-23 10:11:10 -07002207
2208 perf_event__header_size(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002209}
2210
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002211static bool is_orphaned_event(struct perf_event *event)
2212{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01002213 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002214}
2215
Mark Rutland2c81a642016-06-14 16:10:41 +01002216static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01002217{
2218 struct pmu *pmu = event->pmu;
2219 return pmu->filter_match ? pmu->filter_match(event) : 1;
2220}
2221
Mark Rutland2c81a642016-06-14 16:10:41 +01002222/*
2223 * Check whether we should attempt to schedule an event group based on
2224 * PMU-specific filtering. An event group can consist of HW and SW events,
2225 * potentially with a SW leader, so we must check all the filters, to
2226 * determine whether a group is schedulable:
2227 */
2228static inline int pmu_filter_match(struct perf_event *event)
2229{
Peter Zijlstraedb39592018-03-15 17:36:56 +01002230 struct perf_event *sibling;
Mark Rutland2c81a642016-06-14 16:10:41 +01002231
2232 if (!__pmu_filter_match(event))
2233 return 0;
2234
Peter Zijlstraedb39592018-03-15 17:36:56 +01002235 for_each_sibling_event(sibling, event) {
2236 if (!__pmu_filter_match(sibling))
Mark Rutland2c81a642016-06-14 16:10:41 +01002237 return 0;
2238 }
2239
2240 return 1;
2241}
2242
Stephane Eranianfa66f072010-08-26 16:40:01 +02002243static inline int
2244event_filter_match(struct perf_event *event)
2245{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002246 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
2247 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02002248}
2249
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002250static void
2251event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002252 struct perf_cpu_context *cpuctx,
2253 struct perf_event_context *ctx)
2254{
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002255 enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002256
2257 WARN_ON_ONCE(event->ctx != ctx);
2258 lockdep_assert_held(&ctx->lock);
2259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002260 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002261 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262
Peter Zijlstra66681282017-11-13 14:28:38 +01002263 /*
2264 * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but
2265 * we can schedule events _OUT_ individually through things like
2266 * __perf_remove_from_context().
2267 */
2268 list_del_init(&event->active_list);
2269
Alexander Shishkin44377272013-12-16 14:17:36 +02002270 perf_pmu_disable(event->pmu);
2271
Peter Zijlstra28a967c2016-02-24 18:45:46 +01002272 event->pmu->del(event, 0);
2273 event->oncpu = -1;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002274
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002275 if (READ_ONCE(event->pending_disable) >= 0) {
2276 WRITE_ONCE(event->pending_disable, -1);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002277 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002278 state = PERF_EVENT_STATE_OFF;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279 }
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002280 perf_event_set_state(event, state);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281
2282 if (!is_software_event(event))
2283 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002284 if (!--ctx->nr_active)
2285 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002286 if (event->attr.freq && event->attr.sample_freq)
2287 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288 if (event->attr.exclusive || !cpuctx->active_oncpu)
2289 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02002290
2291 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002292}
2293
2294static void
2295group_sched_out(struct perf_event *group_event,
2296 struct perf_cpu_context *cpuctx,
2297 struct perf_event_context *ctx)
2298{
2299 struct perf_event *event;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002300
2301 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
2302 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303
Mark Rutland3f005e72016-07-26 18:12:21 +01002304 perf_pmu_disable(ctx->pmu);
2305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306 event_sched_out(group_event, cpuctx, ctx);
2307
2308 /*
2309 * Schedule out siblings (if any):
2310 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002311 for_each_sibling_event(event, group_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312 event_sched_out(event, cpuctx, ctx);
2313
Mark Rutland3f005e72016-07-26 18:12:21 +01002314 perf_pmu_enable(ctx->pmu);
2315
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002316 if (group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002317 cpuctx->exclusive = 0;
2318}
2319
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002320#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01002321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002322/*
2323 * Cross CPU call to remove a performance event
2324 *
2325 * We disable the event on the hardware level first. After that we
2326 * remove it from the context list.
2327 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002328static void
2329__perf_remove_from_context(struct perf_event *event,
2330 struct perf_cpu_context *cpuctx,
2331 struct perf_event_context *ctx,
2332 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002334 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002336 if (ctx->is_active & EVENT_TIME) {
2337 update_context_time(ctx);
2338 update_cgrp_time_from_cpuctx(cpuctx);
2339 }
2340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002341 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002342 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02002343 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002344 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002345
Peter Zijlstra39a43642016-01-11 12:46:35 +01002346 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002347 ctx->is_active = 0;
Peter Zijlstra90c91df2020-03-05 13:38:51 +01002348 ctx->rotate_necessary = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01002349 if (ctx->task) {
2350 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2351 cpuctx->task_ctx = NULL;
2352 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002353 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354}
2355
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356/*
2357 * Remove the event from a task's (or a CPU's) list of events.
2358 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002359 * If event->ctx is a cloned context, callers must make sure that
2360 * every task struct that event->ctx->task could possibly point to
2361 * remains valid. This is OK when called from perf_release since
2362 * that only calls us on the top-level context, which can't be a clone.
2363 * When called from perf_event_exit_task, it's OK because the
2364 * context has been detached from its task.
2365 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002366static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367{
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002368 struct perf_event_context *ctx = event->ctx;
2369
2370 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002372 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002373
2374 /*
2375 * The above event_function_call() can NO-OP when it hits
2376 * TASK_TOMBSTONE. In that case we must already have been detached
2377 * from the context (by perf_event_exit_event()) but the grouping
2378 * might still be in-tact.
2379 */
2380 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
2381 if ((flags & DETACH_GROUP) &&
2382 (event->attach_state & PERF_ATTACH_GROUP)) {
2383 /*
2384 * Since in that case we cannot possibly be scheduled, simply
2385 * detach now.
2386 */
2387 raw_spin_lock_irq(&ctx->lock);
2388 perf_group_detach(event);
2389 raw_spin_unlock_irq(&ctx->lock);
2390 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391}
2392
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394 * Cross CPU call to disable a performance event
2395 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002396static void __perf_event_disable(struct perf_event *event,
2397 struct perf_cpu_context *cpuctx,
2398 struct perf_event_context *ctx,
2399 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002400{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002401 if (event->state < PERF_EVENT_STATE_INACTIVE)
2402 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002403
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002404 if (ctx->is_active & EVENT_TIME) {
2405 update_context_time(ctx);
2406 update_cgrp_time_from_event(event);
2407 }
2408
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002409 if (event == event->group_leader)
2410 group_sched_out(event, cpuctx, ctx);
2411 else
2412 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002413
2414 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002415 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002416}
2417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002418/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002419 * Disable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420 *
2421 * If event->ctx is a cloned context, callers must make sure that
2422 * every task struct that event->ctx->task could possibly point to
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03002423 * remains valid. This condition is satisfied when called through
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002424 * perf_event_for_each_child or perf_event_for_each because they
2425 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01002426 * goes to exit will block in perf_event_exit_event().
2427 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 * When called from perf_pending_event it's OK because event->ctx
2429 * is the current context on this CPU and preemption is disabled,
2430 * hence we can't get into perf_event_task_sched_out for this context.
2431 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002432static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433{
2434 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002435
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002436 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002437 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002438 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002439 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002440 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002441 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002442
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002443 event_function_call(event, __perf_event_disable, NULL);
2444}
2445
2446void perf_event_disable_local(struct perf_event *event)
2447{
2448 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002449}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002450
2451/*
2452 * Strictly speaking kernel users cannot create groups and therefore this
2453 * interface does not need the perf_event_ctx_lock() magic.
2454 */
2455void perf_event_disable(struct perf_event *event)
2456{
2457 struct perf_event_context *ctx;
2458
2459 ctx = perf_event_ctx_lock(event);
2460 _perf_event_disable(event);
2461 perf_event_ctx_unlock(event, ctx);
2462}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002463EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002464
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002465void perf_event_disable_inatomic(struct perf_event *event)
2466{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002467 WRITE_ONCE(event->pending_disable, smp_processor_id());
2468 /* can fail, see perf_pending_event_disable() */
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002469 irq_work_queue(&event->pending);
2470}
2471
Stephane Eraniane5d13672011-02-14 11:20:01 +02002472static void perf_set_shadow_time(struct perf_event *event,
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002473 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002474{
2475 /*
2476 * use the correct time source for the time snapshot
2477 *
2478 * We could get by without this by leveraging the
2479 * fact that to get to this function, the caller
2480 * has most likely already called update_context_time()
2481 * and update_cgrp_time_xx() and thus both timestamp
2482 * are identical (or very close). Given that tstamp is,
2483 * already adjusted for cgroup, we could say that:
2484 * tstamp - ctx->timestamp
2485 * is equivalent to
2486 * tstamp - cgrp->timestamp.
2487 *
2488 * Then, in perf_output_read(), the calculation would
2489 * work with no changes because:
2490 * - event is guaranteed scheduled in
2491 * - no scheduled out in between
2492 * - thus the timestamp would be the same
2493 *
2494 * But this is a bit hairy.
2495 *
2496 * So instead, we have an explicit cgroup call to remain
2497 * within the time time source all along. We believe it
2498 * is cleaner and simpler to understand.
2499 */
2500 if (is_cgroup_event(event))
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002501 perf_cgroup_set_shadow_time(event, event->tstamp);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002502 else
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002503 event->shadow_ctx_time = event->tstamp - ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002504}
2505
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002506#define MAX_INTERRUPTS (~0ULL)
2507
2508static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002509static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002511static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002512event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002513 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002514 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002515{
Alexander Shishkin44377272013-12-16 14:17:36 +02002516 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002517
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002518 WARN_ON_ONCE(event->ctx != ctx);
2519
Peter Zijlstra63342412014-05-05 11:49:16 +02002520 lockdep_assert_held(&ctx->lock);
2521
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002522 if (event->state <= PERF_EVENT_STATE_OFF)
2523 return 0;
2524
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002525 WRITE_ONCE(event->oncpu, smp_processor_id());
2526 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02002527 * Order event::oncpu write to happen before the ACTIVE state is
2528 * visible. This allows perf_event_{stop,read}() to observe the correct
2529 * ->oncpu if it sees ACTIVE.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002530 */
2531 smp_wmb();
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002532 perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002533
2534 /*
2535 * Unthrottle events, since we scheduled we might have missed several
2536 * ticks already, also for a heavily scheduling task there is little
2537 * guarantee it'll get a tick in a timely manner.
2538 */
2539 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2540 perf_log_throttle(event, 1);
2541 event->hw.interrupts = 0;
2542 }
2543
Alexander Shishkin44377272013-12-16 14:17:36 +02002544 perf_pmu_disable(event->pmu);
2545
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002546 perf_set_shadow_time(event, ctx);
Shaohua Li72f669c2015-02-05 15:55:31 -08002547
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002548 perf_log_itrace_start(event);
2549
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002550 if (event->pmu->add(event, PERF_EF_START)) {
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002551 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002552 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002553 ret = -EAGAIN;
2554 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002555 }
2556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002557 if (!is_software_event(event))
2558 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002559 if (!ctx->nr_active++)
2560 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002561 if (event->attr.freq && event->attr.sample_freq)
2562 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002563
2564 if (event->attr.exclusive)
2565 cpuctx->exclusive = 1;
2566
Alexander Shishkin44377272013-12-16 14:17:36 +02002567out:
2568 perf_pmu_enable(event->pmu);
2569
2570 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002571}
2572
2573static int
2574group_sched_in(struct perf_event *group_event,
2575 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002576 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577{
Lin Ming6bde9b62010-04-23 13:56:00 +08002578 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002579 struct pmu *pmu = ctx->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002580
2581 if (group_event->state == PERF_EVENT_STATE_OFF)
2582 return 0;
2583
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002584 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002585
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002586 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002587 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002588 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002589 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002590 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591
2592 /*
2593 * Schedule in siblings as one group (if any):
2594 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002595 for_each_sibling_event(event, group_event) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002596 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002597 partial_group = event;
2598 goto group_error;
2599 }
2600 }
2601
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002602 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002603 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002604
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002605group_error:
2606 /*
2607 * Groups can be scheduled in as one unit only, so undo any
2608 * partial group before returning:
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002609 * The events up to the failed event are scheduled out normally.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002610 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002611 for_each_sibling_event(event, group_event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002612 if (event == partial_group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002613 break;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002614
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002615 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002616 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002617 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002618
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002619 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002620
Peter Zijlstra272325c2015-04-15 11:41:58 +02002621 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002622
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002623 return -EAGAIN;
2624}
2625
2626/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002627 * Work out whether we can put this event group on the CPU now.
2628 */
2629static int group_can_go_on(struct perf_event *event,
2630 struct perf_cpu_context *cpuctx,
2631 int can_add_hw)
2632{
2633 /*
2634 * Groups consisting entirely of software events can always go on.
2635 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002636 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002637 return 1;
2638 /*
2639 * If an exclusive group is already on, no other hardware
2640 * events can go on.
2641 */
2642 if (cpuctx->exclusive)
2643 return 0;
2644 /*
2645 * If this group is exclusive and there are already
2646 * events on the CPU, it can't go on.
2647 */
2648 if (event->attr.exclusive && cpuctx->active_oncpu)
2649 return 0;
2650 /*
2651 * Otherwise, try to add it if all previous groups were able
2652 * to go on.
2653 */
2654 return can_add_hw;
2655}
2656
2657static void add_event_to_ctx(struct perf_event *event,
2658 struct perf_event_context *ctx)
2659{
2660 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002661 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002662}
2663
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002664static void ctx_sched_out(struct perf_event_context *ctx,
2665 struct perf_cpu_context *cpuctx,
2666 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002667static void
2668ctx_sched_in(struct perf_event_context *ctx,
2669 struct perf_cpu_context *cpuctx,
2670 enum event_type_t event_type,
2671 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002672
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002673static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002674 struct perf_event_context *ctx,
2675 enum event_type_t event_type)
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002676{
2677 if (!cpuctx->task_ctx)
2678 return;
2679
2680 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2681 return;
2682
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002683 ctx_sched_out(ctx, cpuctx, event_type);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002684}
2685
Peter Zijlstradce58552011-04-09 21:17:46 +02002686static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2687 struct perf_event_context *ctx,
2688 struct task_struct *task)
2689{
2690 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2691 if (ctx)
2692 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2693 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2694 if (ctx)
2695 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2696}
2697
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002698/*
2699 * We want to maintain the following priority of scheduling:
2700 * - CPU pinned (EVENT_CPU | EVENT_PINNED)
2701 * - task pinned (EVENT_PINNED)
2702 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
2703 * - task flexible (EVENT_FLEXIBLE).
2704 *
2705 * In order to avoid unscheduling and scheduling back in everything every
2706 * time an event is added, only do it for the groups of equal priority and
2707 * below.
2708 *
2709 * This can be called after a batch operation on task events, in which case
2710 * event_type is a bit mask of the types of events involved. For CPU events,
2711 * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
2712 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01002713static void ctx_resched(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002714 struct perf_event_context *task_ctx,
2715 enum event_type_t event_type)
Peter Zijlstra00179602015-11-30 16:26:35 +01002716{
Song Liubd903af2018-03-05 21:55:04 -08002717 enum event_type_t ctx_event_type;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002718 bool cpu_event = !!(event_type & EVENT_CPU);
2719
2720 /*
2721 * If pinned groups are involved, flexible groups also need to be
2722 * scheduled out.
2723 */
2724 if (event_type & EVENT_PINNED)
2725 event_type |= EVENT_FLEXIBLE;
2726
Song Liubd903af2018-03-05 21:55:04 -08002727 ctx_event_type = event_type & EVENT_ALL;
2728
Peter Zijlstra3e349502016-01-08 10:01:18 +01002729 perf_pmu_disable(cpuctx->ctx.pmu);
2730 if (task_ctx)
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002731 task_ctx_sched_out(cpuctx, task_ctx, event_type);
2732
2733 /*
2734 * Decide which cpu ctx groups to schedule out based on the types
2735 * of events that caused rescheduling:
2736 * - EVENT_CPU: schedule out corresponding groups;
2737 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups;
2738 * - otherwise, do nothing more.
2739 */
2740 if (cpu_event)
2741 cpu_ctx_sched_out(cpuctx, ctx_event_type);
2742 else if (ctx_event_type & EVENT_PINNED)
2743 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2744
Peter Zijlstra3e349502016-01-08 10:01:18 +01002745 perf_event_sched_in(cpuctx, task_ctx, current);
2746 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002747}
2748
Stephane Eranianc68d2242019-04-08 10:32:51 -07002749void perf_pmu_resched(struct pmu *pmu)
2750{
2751 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2752 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2753
2754 perf_ctx_lock(cpuctx, task_ctx);
2755 ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU);
2756 perf_ctx_unlock(cpuctx, task_ctx);
2757}
2758
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002759/*
2760 * Cross CPU call to install and enable a performance event
2761 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002762 * Very similar to remote_function() + event_function() but cannot assume that
2763 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002765static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002767 struct perf_event *event = info;
2768 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002769 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002770 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63cae122016-12-09 14:59:00 +01002771 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002772 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002773
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002774 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002775 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002776 raw_spin_lock(&ctx->lock);
2777 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002778
Peter Zijlstra63cae122016-12-09 14:59:00 +01002779 reprogram = (ctx->task == current);
2780
2781 /*
2782 * If the task is running, it must be running on this CPU,
2783 * otherwise we cannot reprogram things.
2784 *
2785 * If its not running, we don't care, ctx->lock will
2786 * serialize against it becoming runnable.
2787 */
2788 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002789 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002790 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002791 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792
Peter Zijlstra63cae122016-12-09 14:59:00 +01002793 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002794 } else if (task_ctx) {
2795 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002796 }
2797
leilei.lin33801b92018-03-06 17:36:37 +08002798#ifdef CONFIG_CGROUP_PERF
Peter Zijlstra33238c52020-03-18 20:33:37 +01002799 if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) {
leilei.lin33801b92018-03-06 17:36:37 +08002800 /*
2801 * If the current cgroup doesn't match the event's
2802 * cgroup, we should not try to schedule it.
2803 */
2804 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
2805 reprogram = cgroup_is_descendant(cgrp->css.cgroup,
2806 event->cgrp->css.cgroup);
2807 }
2808#endif
2809
Peter Zijlstra63cae122016-12-09 14:59:00 +01002810 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002811 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2812 add_event_to_ctx(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002813 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstraa0963092016-02-24 18:45:50 +01002814 } else {
2815 add_event_to_ctx(event, ctx);
2816 }
2817
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002818unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002819 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002820
Peter Zijlstraa0963092016-02-24 18:45:50 +01002821 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002822}
2823
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002824static bool exclusive_event_installable(struct perf_event *event,
2825 struct perf_event_context *ctx);
2826
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002827/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002828 * Attach a performance event to a context.
2829 *
2830 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002831 */
2832static void
2833perf_install_in_context(struct perf_event_context *ctx,
2834 struct perf_event *event,
2835 int cpu)
2836{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002837 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002838
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002839 lockdep_assert_held(&ctx->mutex);
2840
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002841 WARN_ON_ONCE(!exclusive_event_installable(event, ctx));
2842
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002843 if (event->cpu != -1)
2844 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002845
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002846 /*
2847 * Ensures that if we can observe event->ctx, both the event and ctx
2848 * will be 'complete'. See perf_iterate_sb_cpu().
2849 */
2850 smp_store_release(&event->ctx, ctx);
2851
Peter Zijlstradb0503e2019-10-21 16:02:39 +02002852 /*
2853 * perf_event_attr::disabled events will not run and can be initialized
2854 * without IPI. Except when this is the first event for the context, in
2855 * that case we need the magic of the IPI to set ctx->is_active.
2856 *
2857 * The IOC_ENABLE that is sure to follow the creation of a disabled
2858 * event will issue the IPI and reprogram the hardware.
2859 */
2860 if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && ctx->nr_events) {
2861 raw_spin_lock_irq(&ctx->lock);
2862 if (ctx->task == TASK_TOMBSTONE) {
2863 raw_spin_unlock_irq(&ctx->lock);
2864 return;
2865 }
2866 add_event_to_ctx(event, ctx);
2867 raw_spin_unlock_irq(&ctx->lock);
2868 return;
2869 }
2870
Peter Zijlstraa0963092016-02-24 18:45:50 +01002871 if (!task) {
2872 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002873 return;
2874 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002875
Peter Zijlstraa0963092016-02-24 18:45:50 +01002876 /*
2877 * Should not happen, we validate the ctx is still alive before calling.
2878 */
2879 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2880 return;
2881
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002882 /*
2883 * Installing events is tricky because we cannot rely on ctx->is_active
2884 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra63cae122016-12-09 14:59:00 +01002885 *
2886 * Instead we use task_curr(), which tells us if the task is running.
2887 * However, since we use task_curr() outside of rq::lock, we can race
2888 * against the actual state. This means the result can be wrong.
2889 *
2890 * If we get a false positive, we retry, this is harmless.
2891 *
2892 * If we get a false negative, things are complicated. If we are after
2893 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2894 * value must be correct. If we're before, it doesn't matter since
2895 * perf_event_context_sched_in() will program the counter.
2896 *
2897 * However, this hinges on the remote context switch having observed
2898 * our task->perf_event_ctxp[] store, such that it will in fact take
2899 * ctx::lock in perf_event_context_sched_in().
2900 *
2901 * We do this by task_function_call(), if the IPI fails to hit the task
2902 * we know any future context switch of task must see the
2903 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002905
Peter Zijlstraa0963092016-02-24 18:45:50 +01002906 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002907 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2908 * task_cpu() load, such that if the IPI then does not find the task
2909 * running, a future context switch of that task must observe the
2910 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002911 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002912 smp_mb();
2913again:
2914 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002915 return;
2916
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002917 raw_spin_lock_irq(&ctx->lock);
2918 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002919 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2920 /*
2921 * Cannot happen because we already checked above (which also
2922 * cannot happen), and we hold ctx->mutex, which serializes us
2923 * against perf_event_exit_task_context().
2924 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002925 raw_spin_unlock_irq(&ctx->lock);
2926 return;
2927 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002928 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002929 * If the task is not running, ctx->lock will avoid it becoming so,
2930 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002931 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002932 if (task_curr(task)) {
2933 raw_spin_unlock_irq(&ctx->lock);
2934 goto again;
2935 }
2936 add_event_to_ctx(event, ctx);
2937 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002938}
2939
2940/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002941 * Cross CPU call to enable a performance event
2942 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002943static void __perf_event_enable(struct perf_event *event,
2944 struct perf_cpu_context *cpuctx,
2945 struct perf_event_context *ctx,
2946 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002949 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002951 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2952 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002953 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002954
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002955 if (ctx->is_active)
2956 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2957
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002958 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002959 perf_cgroup_event_enable(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002960
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002961 if (!ctx->is_active)
2962 return;
2963
Stephane Eraniane5d13672011-02-14 11:20:01 +02002964 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002965 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002966 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002967 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002968
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969 /*
2970 * If the event is in a group and isn't the group leader,
2971 * then don't put it on unless the group is on.
2972 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002973 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2974 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002975 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002976 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002977
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002978 task_ctx = cpuctx->task_ctx;
2979 if (ctx->task)
2980 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002981
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002982 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstra7b648012015-12-03 18:35:21 +01002983}
2984
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002985/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002986 * Enable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002987 *
2988 * If event->ctx is a cloned context, callers must make sure that
2989 * every task struct that event->ctx->task could possibly point to
2990 * remains valid. This condition is satisfied when called through
2991 * perf_event_for_each_child or perf_event_for_each as described
2992 * for perf_event_disable.
2993 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002994static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002995{
2996 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002997
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002998 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002999 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
3000 event->state < PERF_EVENT_STATE_ERROR) {
Kan Liang9f0c4fa2020-07-23 10:11:10 -07003001out:
Peter Zijlstra7b648012015-12-03 18:35:21 +01003002 raw_spin_unlock_irq(&ctx->lock);
3003 return;
3004 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003005
3006 /*
3007 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01003008 *
3009 * That way, if we see the event in error state below, we know that it
3010 * has gone back into error state, as distinct from the task having
3011 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012 */
Kan Liang9f0c4fa2020-07-23 10:11:10 -07003013 if (event->state == PERF_EVENT_STATE_ERROR) {
3014 /*
3015 * Detached SIBLING events cannot leave ERROR state.
3016 */
3017 if (event->event_caps & PERF_EV_CAP_SIBLING &&
3018 event->group_leader == event)
3019 goto out;
3020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003021 event->state = PERF_EVENT_STATE_OFF;
Kan Liang9f0c4fa2020-07-23 10:11:10 -07003022 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003023 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003024
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01003025 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003027
3028/*
3029 * See perf_event_disable();
3030 */
3031void perf_event_enable(struct perf_event *event)
3032{
3033 struct perf_event_context *ctx;
3034
3035 ctx = perf_event_ctx_lock(event);
3036 _perf_event_enable(event);
3037 perf_event_ctx_unlock(event, ctx);
3038}
Robert Richterdcfce4a2011-10-11 17:11:08 +02003039EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003040
Alexander Shishkin375637b2016-04-27 18:44:46 +03003041struct stop_event_data {
3042 struct perf_event *event;
3043 unsigned int restart;
3044};
3045
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003046static int __perf_event_stop(void *info)
3047{
Alexander Shishkin375637b2016-04-27 18:44:46 +03003048 struct stop_event_data *sd = info;
3049 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003050
Alexander Shishkin375637b2016-04-27 18:44:46 +03003051 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003052 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
3053 return 0;
3054
3055 /* matches smp_wmb() in event_sched_in() */
3056 smp_rmb();
3057
3058 /*
3059 * There is a window with interrupts enabled before we get here,
3060 * so we need to check again lest we try to stop another CPU's event.
3061 */
3062 if (READ_ONCE(event->oncpu) != smp_processor_id())
3063 return -EAGAIN;
3064
3065 event->pmu->stop(event, PERF_EF_UPDATE);
3066
Alexander Shishkin375637b2016-04-27 18:44:46 +03003067 /*
3068 * May race with the actual stop (through perf_pmu_output_stop()),
3069 * but it is only used for events with AUX ring buffer, and such
3070 * events will refuse to restart because of rb::aux_mmap_count==0,
3071 * see comments in perf_aux_output_begin().
3072 *
Tobias Tefke788faab2018-07-09 12:57:15 +02003073 * Since this is happening on an event-local CPU, no trace is lost
Alexander Shishkin375637b2016-04-27 18:44:46 +03003074 * while restarting.
3075 */
3076 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01003077 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003078
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003079 return 0;
3080}
3081
Alexander Shishkin767ae082016-09-06 16:23:49 +03003082static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03003083{
3084 struct stop_event_data sd = {
3085 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03003086 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03003087 };
3088 int ret = 0;
3089
3090 do {
3091 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
3092 return 0;
3093
3094 /* matches smp_wmb() in event_sched_in() */
3095 smp_rmb();
3096
3097 /*
3098 * We only want to restart ACTIVE events, so if the event goes
3099 * inactive here (event->oncpu==-1), there's nothing more to do;
3100 * fall through with ret==-ENXIO.
3101 */
3102 ret = cpu_function_call(READ_ONCE(event->oncpu),
3103 __perf_event_stop, &sd);
3104 } while (ret == -EAGAIN);
3105
3106 return ret;
3107}
3108
3109/*
3110 * In order to contain the amount of racy and tricky in the address filter
3111 * configuration management, it is a two part process:
3112 *
3113 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
3114 * we update the addresses of corresponding vmas in
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02003115 * event::addr_filter_ranges array and bump the event::addr_filters_gen;
Alexander Shishkin375637b2016-04-27 18:44:46 +03003116 * (p2) when an event is scheduled in (pmu::add), it calls
3117 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
3118 * if the generation has changed since the previous call.
3119 *
3120 * If (p1) happens while the event is active, we restart it to force (p2).
3121 *
3122 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
3123 * pre-existing mappings, called once when new filters arrive via SET_FILTER
3124 * ioctl;
3125 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003126 * registered mapping, called for every new mmap(), with mm::mmap_lock down
Alexander Shishkin375637b2016-04-27 18:44:46 +03003127 * for reading;
3128 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
3129 * of exec.
3130 */
3131void perf_event_addr_filters_sync(struct perf_event *event)
3132{
3133 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
3134
3135 if (!has_addr_filter(event))
3136 return;
3137
3138 raw_spin_lock(&ifh->lock);
3139 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
3140 event->pmu->addr_filters_sync(event);
3141 event->hw.addr_filters_gen = event->addr_filters_gen;
3142 }
3143 raw_spin_unlock(&ifh->lock);
3144}
3145EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
3146
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003147static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148{
3149 /*
3150 * not supported on inherited events
3151 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01003152 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 return -EINVAL;
3154
3155 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003156 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157
3158 return 0;
3159}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003160
3161/*
3162 * See perf_event_disable()
3163 */
3164int perf_event_refresh(struct perf_event *event, int refresh)
3165{
3166 struct perf_event_context *ctx;
3167 int ret;
3168
3169 ctx = perf_event_ctx_lock(event);
3170 ret = _perf_event_refresh(event, refresh);
3171 perf_event_ctx_unlock(event, ctx);
3172
3173 return ret;
3174}
Avi Kivity26ca5c12011-06-29 18:42:37 +03003175EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003176
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003177static int perf_event_modify_breakpoint(struct perf_event *bp,
3178 struct perf_event_attr *attr)
3179{
3180 int err;
3181
3182 _perf_event_disable(bp);
3183
3184 err = modify_user_hw_breakpoint_check(bp, attr, true);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003185
Jiri Olsabf062782018-08-27 11:12:28 +02003186 if (!bp->attr.disabled)
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003187 _perf_event_enable(bp);
Jiri Olsabf062782018-08-27 11:12:28 +02003188
3189 return err;
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003190}
3191
3192static int perf_event_modify_attr(struct perf_event *event,
3193 struct perf_event_attr *attr)
3194{
3195 if (event->attr.type != attr->type)
3196 return -EINVAL;
3197
3198 switch (event->attr.type) {
3199 case PERF_TYPE_BREAKPOINT:
3200 return perf_event_modify_breakpoint(event, attr);
3201 default:
3202 /* Place holder for future additions. */
3203 return -EOPNOTSUPP;
3204 }
3205}
3206
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003207static void ctx_sched_out(struct perf_event_context *ctx,
3208 struct perf_cpu_context *cpuctx,
3209 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003210{
Peter Zijlstra66681282017-11-13 14:28:38 +01003211 struct perf_event *event, *tmp;
Peter Zijlstradb24d332011-04-09 21:17:45 +02003212 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003213
3214 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003215
Peter Zijlstra39a43642016-01-11 12:46:35 +01003216 if (likely(!ctx->nr_events)) {
3217 /*
3218 * See __perf_remove_from_context().
3219 */
3220 WARN_ON_ONCE(ctx->is_active);
3221 if (ctx->task)
3222 WARN_ON_ONCE(cpuctx->task_ctx);
3223 return;
3224 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003225
Peter Zijlstradb24d332011-04-09 21:17:45 +02003226 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003227 if (!(ctx->is_active & EVENT_ALL))
3228 ctx->is_active = 0;
3229
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003230 if (ctx->task) {
3231 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3232 if (!ctx->is_active)
3233 cpuctx->task_ctx = NULL;
3234 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003235
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003236 /*
3237 * Always update time if it was set; not only when it changes.
3238 * Otherwise we can 'forget' to update time for any but the last
3239 * context we sched out. For example:
3240 *
3241 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
3242 * ctx_sched_out(.event_type = EVENT_PINNED)
3243 *
3244 * would only update time for the pinned events.
3245 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003246 if (is_active & EVENT_TIME) {
3247 /* update (and stop) ctx time */
3248 update_context_time(ctx);
3249 update_cgrp_time_from_cpuctx(cpuctx);
3250 }
3251
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003252 is_active ^= ctx->is_active; /* changed bits */
3253
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003254 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003255 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003256
Peter Zijlstra075e0b02011-04-09 21:17:40 +02003257 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003258 if (is_active & EVENT_PINNED) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003259 list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003260 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003261 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003262
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003263 if (is_active & EVENT_FLEXIBLE) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003264 list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003265 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra90c91df2020-03-05 13:38:51 +01003266
3267 /*
3268 * Since we cleared EVENT_FLEXIBLE, also clear
3269 * rotate_necessary, is will be reset by
3270 * ctx_flexible_sched_in() when needed.
3271 */
3272 ctx->rotate_necessary = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003273 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003274 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003275}
3276
3277/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003278 * Test whether two contexts are equivalent, i.e. whether they have both been
3279 * cloned from the same version of the same context.
3280 *
3281 * Equivalence is measured using a generation number in the context that is
3282 * incremented on each modification to it; see unclone_ctx(), list_add_event()
3283 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003284 */
3285static int context_equiv(struct perf_event_context *ctx1,
3286 struct perf_event_context *ctx2)
3287{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003288 lockdep_assert_held(&ctx1->lock);
3289 lockdep_assert_held(&ctx2->lock);
3290
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003291 /* Pinning disables the swap optimization */
3292 if (ctx1->pin_count || ctx2->pin_count)
3293 return 0;
3294
3295 /* If ctx1 is the parent of ctx2 */
3296 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
3297 return 1;
3298
3299 /* If ctx2 is the parent of ctx1 */
3300 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
3301 return 1;
3302
3303 /*
3304 * If ctx1 and ctx2 have the same parent; we flatten the parent
3305 * hierarchy, see perf_event_init_context().
3306 */
3307 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
3308 ctx1->parent_gen == ctx2->parent_gen)
3309 return 1;
3310
3311 /* Unmatched */
3312 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313}
3314
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315static void __perf_event_sync_stat(struct perf_event *event,
3316 struct perf_event *next_event)
3317{
3318 u64 value;
3319
3320 if (!event->attr.inherit_stat)
3321 return;
3322
3323 /*
3324 * Update the event value, we cannot use perf_event_read()
3325 * because we're in the middle of a context switch and have IRQs
3326 * disabled, which upsets smp_call_function_single(), however
3327 * we know the event must be on the current CPU, therefore we
3328 * don't need to use it.
3329 */
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003330 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01003331 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003333 perf_event_update_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334
3335 /*
3336 * In order to keep per-task stats reliable we need to flip the event
3337 * values when we flip the contexts.
3338 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02003339 value = local64_read(&next_event->count);
3340 value = local64_xchg(&event->count, value);
3341 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342
3343 swap(event->total_time_enabled, next_event->total_time_enabled);
3344 swap(event->total_time_running, next_event->total_time_running);
3345
3346 /*
3347 * Since we swizzled the values, update the user visible data too.
3348 */
3349 perf_event_update_userpage(event);
3350 perf_event_update_userpage(next_event);
3351}
3352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353static void perf_event_sync_stat(struct perf_event_context *ctx,
3354 struct perf_event_context *next_ctx)
3355{
3356 struct perf_event *event, *next_event;
3357
3358 if (!ctx->nr_stat)
3359 return;
3360
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01003361 update_context_time(ctx);
3362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 event = list_first_entry(&ctx->event_list,
3364 struct perf_event, event_entry);
3365
3366 next_event = list_first_entry(&next_ctx->event_list,
3367 struct perf_event, event_entry);
3368
3369 while (&event->event_entry != &ctx->event_list &&
3370 &next_event->event_entry != &next_ctx->event_list) {
3371
3372 __perf_event_sync_stat(event, next_event);
3373
3374 event = list_next_entry(event, event_entry);
3375 next_event = list_next_entry(next_event, event_entry);
3376 }
3377}
3378
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003379static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
3380 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003382 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003383 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003384 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003385 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003386 int do_switch = 1;
3387
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003388 if (likely(!ctx))
3389 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003391 cpuctx = __get_cpu_context(ctx);
3392 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393 return;
3394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003396 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003397 if (!next_ctx)
3398 goto unlock;
3399
3400 parent = rcu_dereference(ctx->parent_ctx);
3401 next_parent = rcu_dereference(next_ctx->parent_ctx);
3402
3403 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02003404 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003405 goto unlock;
3406
3407 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003408 /*
3409 * Looks like the two contexts are clones, so we might be
3410 * able to optimize the context switch. We lock both
3411 * contexts and check that they are clones under the
3412 * lock (including re-checking that neither has been
3413 * uncloned in the meantime). It doesn't matter which
3414 * order we take the locks because no other cpu could
3415 * be trying to lock both of these tasks.
3416 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003417 raw_spin_lock(&ctx->lock);
3418 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003419 if (context_equiv(ctx, next_ctx)) {
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003420 struct pmu *pmu = ctx->pmu;
3421
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003422 WRITE_ONCE(ctx->task, next);
3423 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003424
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003425 /*
3426 * PMU specific parts of task perf context can require
3427 * additional synchronization. As an example of such
3428 * synchronization see implementation details of Intel
3429 * LBR call stack data profiling;
3430 */
3431 if (pmu->swap_task_ctx)
3432 pmu->swap_task_ctx(ctx, next_ctx);
3433 else
3434 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003435
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003436 /*
3437 * RCU_INIT_POINTER here is safe because we've not
3438 * modified the ctx and the above modification of
3439 * ctx->task and ctx->task_ctx_data are immaterial
3440 * since those values are always verified under
3441 * ctx->lock which we're now holding.
3442 */
3443 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
3444 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
3445
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003446 do_switch = 0;
3447
3448 perf_event_sync_stat(ctx, next_ctx);
3449 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003450 raw_spin_unlock(&next_ctx->lock);
3451 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003453unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003454 rcu_read_unlock();
3455
3456 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003457 raw_spin_lock(&ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003458 task_ctx_sched_out(cpuctx, ctx, EVENT_ALL);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003459 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003460 }
3461}
3462
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003463static DEFINE_PER_CPU(struct list_head, sched_cb_list);
3464
Yan, Zhengba532502014-11-04 21:55:58 -05003465void perf_sched_cb_dec(struct pmu *pmu)
3466{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003467 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3468
Yan, Zhengba532502014-11-04 21:55:58 -05003469 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003470
3471 if (!--cpuctx->sched_cb_usage)
3472 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05003473}
3474
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003475
Yan, Zhengba532502014-11-04 21:55:58 -05003476void perf_sched_cb_inc(struct pmu *pmu)
3477{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003478 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3479
3480 if (!cpuctx->sched_cb_usage++)
3481 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
3482
Yan, Zhengba532502014-11-04 21:55:58 -05003483 this_cpu_inc(perf_sched_cb_usages);
3484}
3485
3486/*
3487 * This function provides the context switch callback to the lower code
3488 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02003489 *
3490 * This callback is relevant even to per-cpu events; for example multi event
3491 * PEBS requires this to provide PID/TID information. This requires we flush
3492 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05003493 */
Kan Liang556ccca2020-08-21 12:57:52 -07003494static void __perf_pmu_sched_task(struct perf_cpu_context *cpuctx, bool sched_in)
3495{
3496 struct pmu *pmu;
3497
3498 pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */
3499
3500 if (WARN_ON_ONCE(!pmu->sched_task))
3501 return;
3502
3503 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
3504 perf_pmu_disable(pmu);
3505
3506 pmu->sched_task(cpuctx->task_ctx, sched_in);
3507
3508 perf_pmu_enable(pmu);
3509 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
3510}
3511
Yan, Zhengba532502014-11-04 21:55:58 -05003512static void perf_pmu_sched_task(struct task_struct *prev,
3513 struct task_struct *next,
3514 bool sched_in)
3515{
3516 struct perf_cpu_context *cpuctx;
Yan, Zhengba532502014-11-04 21:55:58 -05003517
3518 if (prev == next)
3519 return;
3520
Kan Liang556ccca2020-08-21 12:57:52 -07003521 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry)
3522 __perf_pmu_sched_task(cpuctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05003523
Yan, Zhengba532502014-11-04 21:55:58 -05003524}
3525
Adrian Hunter45ac1402015-07-21 12:44:02 +03003526static void perf_event_switch(struct task_struct *task,
3527 struct task_struct *next_prev, bool sched_in);
3528
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003529#define for_each_task_context_nr(ctxn) \
3530 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
3531
3532/*
3533 * Called from scheduler to remove the events of the current task,
3534 * with interrupts disabled.
3535 *
3536 * We stop each event and update the event value in event->count.
3537 *
3538 * This does not protect us against NMI, but disable()
3539 * sets the disabled bit in the control field of event _before_
3540 * accessing the event control register. If a NMI hits, then it will
3541 * not restart the event.
3542 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003543void __perf_event_task_sched_out(struct task_struct *task,
3544 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003545{
3546 int ctxn;
3547
Yan, Zhengba532502014-11-04 21:55:58 -05003548 if (__this_cpu_read(perf_sched_cb_usages))
3549 perf_pmu_sched_task(task, next, false);
3550
Adrian Hunter45ac1402015-07-21 12:44:02 +03003551 if (atomic_read(&nr_switch_events))
3552 perf_event_switch(task, next, false);
3553
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003554 for_each_task_context_nr(ctxn)
3555 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003556
3557 /*
3558 * if cgroup events exist on this CPU, then we need
3559 * to check if we have to switch out PMU state.
3560 * cgroup event are system-wide mode only
3561 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003562 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003563 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003564}
3565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003566/*
3567 * Called with IRQs disabled
3568 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003569static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3570 enum event_type_t event_type)
3571{
3572 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003573}
3574
Ian Rogers6eef8a712020-02-13 23:51:30 -08003575static bool perf_less_group_idx(const void *l, const void *r)
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003576{
Ian Rogers24fb6b82020-03-21 09:43:31 -07003577 const struct perf_event *le = *(const struct perf_event **)l;
3578 const struct perf_event *re = *(const struct perf_event **)r;
Ian Rogers6eef8a712020-02-13 23:51:30 -08003579
3580 return le->group_index < re->group_index;
3581}
3582
3583static void swap_ptr(void *l, void *r)
3584{
3585 void **lp = l, **rp = r;
3586
3587 swap(*lp, *rp);
3588}
3589
3590static const struct min_heap_callbacks perf_min_heap = {
3591 .elem_size = sizeof(struct perf_event *),
3592 .less = perf_less_group_idx,
3593 .swp = swap_ptr,
3594};
3595
3596static void __heap_add(struct min_heap *heap, struct perf_event *event)
3597{
3598 struct perf_event **itrs = heap->data;
3599
3600 if (event) {
3601 itrs[heap->nr] = event;
3602 heap->nr++;
3603 }
3604}
3605
Ian Rogers836196be2020-02-13 23:51:31 -08003606static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx,
3607 struct perf_event_groups *groups, int cpu,
Ian Rogers6eef8a712020-02-13 23:51:30 -08003608 int (*func)(struct perf_event *, void *),
3609 void *data)
3610{
Ian Rogers95ed6c72020-02-13 23:51:33 -08003611#ifdef CONFIG_CGROUP_PERF
3612 struct cgroup_subsys_state *css = NULL;
3613#endif
Ian Rogers6eef8a712020-02-13 23:51:30 -08003614 /* Space for per CPU and/or any CPU event iterators. */
3615 struct perf_event *itrs[2];
Ian Rogers836196be2020-02-13 23:51:31 -08003616 struct min_heap event_heap;
3617 struct perf_event **evt;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003618 int ret;
3619
Ian Rogers836196be2020-02-13 23:51:31 -08003620 if (cpuctx) {
3621 event_heap = (struct min_heap){
3622 .data = cpuctx->heap,
3623 .nr = 0,
3624 .size = cpuctx->heap_size,
3625 };
Ian Rogersc2283c92020-02-13 23:51:32 -08003626
3627 lockdep_assert_held(&cpuctx->ctx.lock);
Ian Rogers95ed6c72020-02-13 23:51:33 -08003628
3629#ifdef CONFIG_CGROUP_PERF
3630 if (cpuctx->cgrp)
3631 css = &cpuctx->cgrp->css;
3632#endif
Ian Rogers836196be2020-02-13 23:51:31 -08003633 } else {
3634 event_heap = (struct min_heap){
3635 .data = itrs,
3636 .nr = 0,
3637 .size = ARRAY_SIZE(itrs),
3638 };
3639 /* Events not within a CPU context may be on any CPU. */
Ian Rogers95ed6c72020-02-13 23:51:33 -08003640 __heap_add(&event_heap, perf_event_groups_first(groups, -1, NULL));
Ian Rogers836196be2020-02-13 23:51:31 -08003641 }
3642 evt = event_heap.data;
3643
Ian Rogers95ed6c72020-02-13 23:51:33 -08003644 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, NULL));
3645
3646#ifdef CONFIG_CGROUP_PERF
3647 for (; css; css = css->parent)
3648 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, css->cgroup));
3649#endif
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003650
Ian Rogers6eef8a712020-02-13 23:51:30 -08003651 min_heapify_all(&event_heap, &perf_min_heap);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003652
Ian Rogers6eef8a712020-02-13 23:51:30 -08003653 while (event_heap.nr) {
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003654 ret = func(*evt, data);
3655 if (ret)
3656 return ret;
3657
3658 *evt = perf_event_groups_next(*evt);
Ian Rogers6eef8a712020-02-13 23:51:30 -08003659 if (*evt)
3660 min_heapify(&event_heap, 0, &perf_min_heap);
3661 else
3662 min_heap_pop(&event_heap, &perf_min_heap);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003663 }
3664
3665 return 0;
3666}
3667
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003668static int merge_sched_in(struct perf_event *event, void *data)
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003669{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003670 struct perf_event_context *ctx = event->ctx;
3671 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
3672 int *can_add_hw = data;
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003673
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003674 if (event->state <= PERF_EVENT_STATE_OFF)
3675 return 0;
3676
3677 if (!event_filter_match(event))
3678 return 0;
3679
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003680 if (group_can_go_on(event, cpuctx, *can_add_hw)) {
3681 if (!group_sched_in(event, cpuctx, ctx))
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003682 list_add_tail(&event->active_list, get_event_list(event));
Peter Zijlstra66681282017-11-13 14:28:38 +01003683 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003684
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003685 if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra33238c52020-03-18 20:33:37 +01003686 if (event->attr.pinned) {
3687 perf_cgroup_event_disable(event, ctx);
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003688 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
Peter Zijlstra33238c52020-03-18 20:33:37 +01003689 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003690
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003691 *can_add_hw = 0;
3692 ctx->rotate_necessary = 1;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003693 }
3694
3695 return 0;
3696}
3697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003698static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003699ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003700 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003701{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003702 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003703
Ian Rogers836196be2020-02-13 23:51:31 -08003704 if (ctx != &cpuctx->ctx)
3705 cpuctx = NULL;
3706
3707 visit_groups_merge(cpuctx, &ctx->pinned_groups,
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003708 smp_processor_id(),
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003709 merge_sched_in, &can_add_hw);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003710}
3711
3712static void
3713ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003714 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003715{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003716 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717
Ian Rogers836196be2020-02-13 23:51:31 -08003718 if (ctx != &cpuctx->ctx)
3719 cpuctx = NULL;
3720
3721 visit_groups_merge(cpuctx, &ctx->flexible_groups,
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003722 smp_processor_id(),
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003723 merge_sched_in, &can_add_hw);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003724}
3725
3726static void
3727ctx_sched_in(struct perf_event_context *ctx,
3728 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003729 enum event_type_t event_type,
3730 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003731{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003732 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003733 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003734
Peter Zijlstrac994d612016-01-08 09:20:23 +01003735 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003736
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003737 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003738 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003739
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003740 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003741 if (ctx->task) {
3742 if (!is_active)
3743 cpuctx->task_ctx = ctx;
3744 else
3745 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3746 }
3747
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003748 is_active ^= ctx->is_active; /* changed bits */
3749
3750 if (is_active & EVENT_TIME) {
3751 /* start ctx time */
3752 now = perf_clock();
3753 ctx->timestamp = now;
3754 perf_cgroup_set_timestamp(task, ctx);
3755 }
3756
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003757 /*
3758 * First go through the list and put on any pinned groups
3759 * in order to give them the best chance of going on.
3760 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003761 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003762 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003763
3764 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003765 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003766 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003767}
3768
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003769static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003770 enum event_type_t event_type,
3771 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003772{
3773 struct perf_event_context *ctx = &cpuctx->ctx;
3774
Stephane Eraniane5d13672011-02-14 11:20:01 +02003775 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003776}
3777
Stephane Eraniane5d13672011-02-14 11:20:01 +02003778static void perf_event_context_sched_in(struct perf_event_context *ctx,
3779 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003780{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003781 struct perf_cpu_context *cpuctx;
Kan Liang556ccca2020-08-21 12:57:52 -07003782 struct pmu *pmu = ctx->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003783
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003784 cpuctx = __get_cpu_context(ctx);
Kan Liang556ccca2020-08-21 12:57:52 -07003785 if (cpuctx->task_ctx == ctx) {
3786 if (cpuctx->sched_cb_usage)
3787 __perf_pmu_sched_task(cpuctx, true);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003788 return;
Kan Liang556ccca2020-08-21 12:57:52 -07003789 }
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003790
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003791 perf_ctx_lock(cpuctx, ctx);
leilei.linfdccc3f2017-08-09 08:29:21 +08003792 /*
3793 * We must check ctx->nr_events while holding ctx->lock, such
3794 * that we serialize against perf_install_in_context().
3795 */
3796 if (!ctx->nr_events)
3797 goto unlock;
3798
Kan Liang556ccca2020-08-21 12:57:52 -07003799 perf_pmu_disable(pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003800 /*
3801 * We want to keep the following priority order:
3802 * cpu pinned (that don't need to move), task pinned,
3803 * cpu flexible, task flexible.
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003804 *
3805 * However, if task's ctx is not carrying any pinned
3806 * events, no need to flip the cpuctx's events around.
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003807 */
Alexey Budankov8e1a2032017-09-08 11:47:03 +03003808 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree))
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003809 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003810 perf_event_sched_in(cpuctx, ctx, task);
Kan Liang556ccca2020-08-21 12:57:52 -07003811
3812 if (cpuctx->sched_cb_usage && pmu->sched_task)
3813 pmu->sched_task(cpuctx->task_ctx, true);
3814
3815 perf_pmu_enable(pmu);
leilei.linfdccc3f2017-08-09 08:29:21 +08003816
3817unlock:
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003818 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819}
3820
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003821/*
3822 * Called from scheduler to add the events of the current task
3823 * with interrupts disabled.
3824 *
3825 * We restore the event value and then enable it.
3826 *
3827 * This does not protect us against NMI, but enable()
3828 * sets the enabled bit in the control field of event _before_
3829 * accessing the event control register. If a NMI hits, then it will
3830 * keep the event running.
3831 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003832void __perf_event_task_sched_in(struct task_struct *prev,
3833 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003834{
3835 struct perf_event_context *ctx;
3836 int ctxn;
3837
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003838 /*
3839 * If cgroup events exist on this CPU, then we need to check if we have
3840 * to switch in PMU state; cgroup event are system-wide mode only.
3841 *
3842 * Since cgroup events are CPU events, we must schedule these in before
3843 * we schedule in the task events.
3844 */
3845 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3846 perf_cgroup_sched_in(prev, task);
3847
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003848 for_each_task_context_nr(ctxn) {
3849 ctx = task->perf_event_ctxp[ctxn];
3850 if (likely(!ctx))
3851 continue;
3852
Stephane Eraniane5d13672011-02-14 11:20:01 +02003853 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003854 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003855
Adrian Hunter45ac1402015-07-21 12:44:02 +03003856 if (atomic_read(&nr_switch_events))
3857 perf_event_switch(task, prev, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003858}
3859
Peter Zijlstraabd50712010-01-26 18:50:16 +01003860static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3861{
3862 u64 frequency = event->attr.sample_freq;
3863 u64 sec = NSEC_PER_SEC;
3864 u64 divisor, dividend;
3865
3866 int count_fls, nsec_fls, frequency_fls, sec_fls;
3867
3868 count_fls = fls64(count);
3869 nsec_fls = fls64(nsec);
3870 frequency_fls = fls64(frequency);
3871 sec_fls = 30;
3872
3873 /*
3874 * We got @count in @nsec, with a target of sample_freq HZ
3875 * the target period becomes:
3876 *
3877 * @count * 10^9
3878 * period = -------------------
3879 * @nsec * sample_freq
3880 *
3881 */
3882
3883 /*
3884 * Reduce accuracy by one bit such that @a and @b converge
3885 * to a similar magnitude.
3886 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003887#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003888do { \
3889 if (a##_fls > b##_fls) { \
3890 a >>= 1; \
3891 a##_fls--; \
3892 } else { \
3893 b >>= 1; \
3894 b##_fls--; \
3895 } \
3896} while (0)
3897
3898 /*
3899 * Reduce accuracy until either term fits in a u64, then proceed with
3900 * the other, so that finally we can do a u64/u64 division.
3901 */
3902 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3903 REDUCE_FLS(nsec, frequency);
3904 REDUCE_FLS(sec, count);
3905 }
3906
3907 if (count_fls + sec_fls > 64) {
3908 divisor = nsec * frequency;
3909
3910 while (count_fls + sec_fls > 64) {
3911 REDUCE_FLS(count, sec);
3912 divisor >>= 1;
3913 }
3914
3915 dividend = count * sec;
3916 } else {
3917 dividend = count * sec;
3918
3919 while (nsec_fls + frequency_fls > 64) {
3920 REDUCE_FLS(nsec, frequency);
3921 dividend >>= 1;
3922 }
3923
3924 divisor = nsec * frequency;
3925 }
3926
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003927 if (!divisor)
3928 return dividend;
3929
Peter Zijlstraabd50712010-01-26 18:50:16 +01003930 return div64_u64(dividend, divisor);
3931}
3932
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003933static DEFINE_PER_CPU(int, perf_throttled_count);
3934static DEFINE_PER_CPU(u64, perf_throttled_seq);
3935
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003936static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003937{
3938 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003939 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003940 s64 delta;
3941
Peter Zijlstraabd50712010-01-26 18:50:16 +01003942 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003943
3944 delta = (s64)(period - hwc->sample_period);
3945 delta = (delta + 7) / 8; /* low pass filter */
3946
3947 sample_period = hwc->sample_period + delta;
3948
3949 if (!sample_period)
3950 sample_period = 1;
3951
3952 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003953
Peter Zijlstrae7850592010-05-21 14:43:08 +02003954 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003955 if (disable)
3956 event->pmu->stop(event, PERF_EF_UPDATE);
3957
Peter Zijlstrae7850592010-05-21 14:43:08 +02003958 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003959
3960 if (disable)
3961 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003962 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963}
3964
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003965/*
3966 * combine freq adjustment with unthrottling to avoid two passes over the
3967 * events. At the same time, make sure, having freq events does not change
3968 * the rate of unthrottling as that would introduce bias.
3969 */
3970static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3971 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003972{
3973 struct perf_event *event;
3974 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003975 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003976 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003978 /*
3979 * only need to iterate over all events iff:
3980 * - context have events in frequency mode (needs freq adjust)
3981 * - there are events to unthrottle on this cpu
3982 */
3983 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003984 return;
3985
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003986 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003987 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003988
Paul Mackerras03541f82009-10-14 16:58:03 +11003989 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990 if (event->state != PERF_EVENT_STATE_ACTIVE)
3991 continue;
3992
Stephane Eranian5632ab12011-01-03 18:20:01 +02003993 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003994 continue;
3995
Alexander Shishkin44377272013-12-16 14:17:36 +02003996 perf_pmu_disable(event->pmu);
3997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998 hwc = &event->hw;
3999
Jiri Olsaae23bff2013-08-24 16:45:54 +02004000 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004001 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004003 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004004 }
4005
4006 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02004007 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004008
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004009 /*
4010 * stop the event and update event->count
4011 */
4012 event->pmu->stop(event, PERF_EF_UPDATE);
4013
Peter Zijlstrae7850592010-05-21 14:43:08 +02004014 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01004015 delta = now - hwc->freq_count_stamp;
4016 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004017
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004018 /*
4019 * restart the event
4020 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004021 * we have stopped the event so tell that
4022 * to perf_adjust_period() to avoid stopping it
4023 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004024 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01004025 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004026 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004027
4028 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02004029 next:
4030 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004031 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004032
Stephane Eranianf39d47f2012-02-07 14:39:57 +01004033 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004034 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004035}
4036
4037/*
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004038 * Move @event to the tail of the @ctx's elegible events.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004039 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004040static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004041{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01004042 /*
4043 * Rotate the first entry last of non-pinned groups. Rotation might be
4044 * disabled by the inheritance code.
4045 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004046 if (ctx->rotate_disable)
4047 return;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03004048
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004049 perf_event_groups_delete(&ctx->flexible_groups, event);
4050 perf_event_groups_insert(&ctx->flexible_groups, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004051}
4052
Song Liu7fa343b72019-10-08 09:59:49 -07004053/* pick an event from the flexible_groups to rotate */
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004054static inline struct perf_event *
Song Liu7fa343b72019-10-08 09:59:49 -07004055ctx_event_to_rotate(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004056{
Song Liu7fa343b72019-10-08 09:59:49 -07004057 struct perf_event *event;
4058
4059 /* pick the first active flexible event */
4060 event = list_first_entry_or_null(&ctx->flexible_active,
4061 struct perf_event, active_list);
4062
4063 /* if no active flexible event, pick the first event */
4064 if (!event) {
4065 event = rb_entry_safe(rb_first(&ctx->flexible_groups.tree),
4066 typeof(*event), group_node);
4067 }
4068
Peter Zijlstra90c91df2020-03-05 13:38:51 +01004069 /*
4070 * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in()
4071 * finds there are unschedulable events, it will set it again.
4072 */
4073 ctx->rotate_necessary = 0;
4074
Song Liu7fa343b72019-10-08 09:59:49 -07004075 return event;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004076}
4077
4078static bool perf_rotate_context(struct perf_cpu_context *cpuctx)
4079{
4080 struct perf_event *cpu_event = NULL, *task_event = NULL;
Ian Rogersfd7d5512019-06-01 01:27:22 -07004081 struct perf_event_context *task_ctx = NULL;
4082 int cpu_rotate, task_rotate;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004083
4084 /*
4085 * Since we run this from IRQ context, nobody can install new
4086 * events, thus the event count values are stable.
4087 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088
Ian Rogersfd7d5512019-06-01 01:27:22 -07004089 cpu_rotate = cpuctx->ctx.rotate_necessary;
4090 task_ctx = cpuctx->task_ctx;
4091 task_rotate = task_ctx ? task_ctx->rotate_necessary : 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004092
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004093 if (!(cpu_rotate || task_rotate))
4094 return false;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01004095
Peter Zijlstrafacc4302011-04-09 21:17:42 +02004096 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02004097 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004099 if (task_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07004100 task_event = ctx_event_to_rotate(task_ctx);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004101 if (cpu_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07004102 cpu_event = ctx_event_to_rotate(&cpuctx->ctx);
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004103
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004104 /*
4105 * As per the order given at ctx_resched() first 'pop' task flexible
4106 * and then, if needed CPU flexible.
4107 */
Ian Rogersfd7d5512019-06-01 01:27:22 -07004108 if (task_event || (task_ctx && cpu_event))
4109 ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004110 if (cpu_event)
4111 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01004112
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004113 if (task_event)
Ian Rogersfd7d5512019-06-01 01:27:22 -07004114 rotate_ctx(task_ctx, task_event);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004115 if (cpu_event)
4116 rotate_ctx(&cpuctx->ctx, cpu_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117
Ian Rogersfd7d5512019-06-01 01:27:22 -07004118 perf_event_sched_in(cpuctx, task_ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01004119
4120 perf_pmu_enable(cpuctx->ctx.pmu);
4121 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02004122
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004123 return true;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004124}
4125
4126void perf_event_task_tick(void)
4127{
Mark Rutland2fde4f92015-01-07 15:01:54 +00004128 struct list_head *head = this_cpu_ptr(&active_ctx_list);
4129 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004130 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004131
Frederic Weisbecker16444642017-11-06 16:01:24 +01004132 lockdep_assert_irqs_disabled();
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004133
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004134 __this_cpu_inc(perf_throttled_seq);
4135 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004136 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004137
Mark Rutland2fde4f92015-01-07 15:01:54 +00004138 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004139 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004140}
4141
Frederic Weisbecker889ff012010-01-09 20:04:47 +01004142static int event_enable_on_exec(struct perf_event *event,
4143 struct perf_event_context *ctx)
4144{
4145 if (!event->attr.enable_on_exec)
4146 return 0;
4147
4148 event->attr.enable_on_exec = 0;
4149 if (event->state >= PERF_EVENT_STATE_INACTIVE)
4150 return 0;
4151
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004152 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01004153
4154 return 1;
4155}
4156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004157/*
4158 * Enable all of a task's events that have been marked enable-on-exec.
4159 * This expects task == current.
4160 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01004161static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004162{
Peter Zijlstrac1274492015-12-10 20:57:40 +01004163 struct perf_event_context *ctx, *clone_ctx = NULL;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004164 enum event_type_t event_type = 0;
Peter Zijlstra3e349502016-01-08 10:01:18 +01004165 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166 struct perf_event *event;
4167 unsigned long flags;
4168 int enabled = 0;
4169
4170 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01004171 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172 if (!ctx || !ctx->nr_events)
4173 goto out;
4174
Peter Zijlstra3e349502016-01-08 10:01:18 +01004175 cpuctx = __get_cpu_context(ctx);
4176 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01004177 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004178 list_for_each_entry(event, &ctx->event_list, event_entry) {
Peter Zijlstra3e349502016-01-08 10:01:18 +01004179 enabled |= event_enable_on_exec(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004180 event_type |= get_event_type(event);
4181 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182
4183 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01004184 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01004186 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004187 clone_ctx = unclone_ctx(ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004188 ctx_resched(cpuctx, ctx, event_type);
Peter Zijlstra7bbba0e2017-02-15 16:12:20 +01004189 } else {
4190 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstra3e349502016-01-08 10:01:18 +01004191 }
4192 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004193
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004194out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004196
4197 if (clone_ctx)
4198 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199}
4200
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004201struct perf_read_data {
4202 struct perf_event *event;
4203 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004204 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004205};
4206
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004207static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004208{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004209 u16 local_pkg, event_pkg;
4210
4211 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004212 int local_cpu = smp_processor_id();
4213
4214 event_pkg = topology_physical_package_id(event_cpu);
4215 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004216
4217 if (event_pkg == local_pkg)
4218 return local_cpu;
4219 }
4220
4221 return event_cpu;
4222}
4223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004224/*
4225 * Cross CPU call to read the hardware event
4226 */
4227static void __perf_event_read(void *info)
4228{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004229 struct perf_read_data *data = info;
4230 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004231 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004232 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004233 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004234
4235 /*
4236 * If this is a task context, we need to check whether it is
4237 * the current task context of this cpu. If not it has been
4238 * scheduled out before the smp call arrived. In that case
4239 * event->count would have been updated to a recent sample
4240 * when the event was scheduled out.
4241 */
4242 if (ctx->task && cpuctx->task_ctx != ctx)
4243 return;
4244
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004245 raw_spin_lock(&ctx->lock);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004246 if (ctx->is_active & EVENT_TIME) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01004247 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004248 update_cgrp_time_from_event(event);
4249 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004250
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004251 perf_event_update_time(event);
4252 if (data->group)
4253 perf_event_update_sibling_time(event);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004254
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004255 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004256 goto unlock;
4257
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004258 if (!data->group) {
4259 pmu->read(event);
4260 data->ret = 0;
4261 goto unlock;
4262 }
4263
4264 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
4265
4266 pmu->read(event);
4267
Peter Zijlstraedb39592018-03-15 17:36:56 +01004268 for_each_sibling_event(sub, event) {
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004269 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
4270 /*
4271 * Use sibling's PMU rather than @event's since
4272 * sibling could be on different (eg: software) PMU.
4273 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004274 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004275 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004276 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004277
4278 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004279
4280unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004281 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004282}
4283
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004284static inline u64 perf_event_count(struct perf_event *event)
4285{
Vikas Shivappac39a0e22017-07-25 14:14:20 -07004286 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004287}
4288
Kaixu Xiaffe86902015-08-06 07:02:32 +00004289/*
4290 * NMI-safe method to read a local event, that is an event that
4291 * is:
4292 * - either for the current task, or for this CPU
4293 * - does not have inherit set, for inherited task events
4294 * will not be local and we cannot read them atomically
4295 * - must not have a pmu::count method
4296 */
Yonghong Song7d9285e2017-10-05 09:19:19 -07004297int perf_event_read_local(struct perf_event *event, u64 *value,
4298 u64 *enabled, u64 *running)
Kaixu Xiaffe86902015-08-06 07:02:32 +00004299{
4300 unsigned long flags;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004301 int ret = 0;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004302
4303 /*
4304 * Disabling interrupts avoids all counter scheduling (context
4305 * switches, timer based rotation and IPIs).
4306 */
4307 local_irq_save(flags);
4308
Kaixu Xiaffe86902015-08-06 07:02:32 +00004309 /*
4310 * It must not be an event with inherit set, we cannot read
4311 * all child counters from atomic context.
4312 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004313 if (event->attr.inherit) {
4314 ret = -EOPNOTSUPP;
4315 goto out;
4316 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004317
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004318 /* If this is a per-task event, it must be for current */
4319 if ((event->attach_state & PERF_ATTACH_TASK) &&
4320 event->hw.target != current) {
4321 ret = -EINVAL;
4322 goto out;
4323 }
4324
4325 /* If this is a per-CPU event, it must be for this CPU */
4326 if (!(event->attach_state & PERF_ATTACH_TASK) &&
4327 event->cpu != smp_processor_id()) {
4328 ret = -EINVAL;
4329 goto out;
4330 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004331
Reinette Chatrebefb1b32018-09-19 10:29:06 -07004332 /* If this is a pinned event it must be running on this CPU */
4333 if (event->attr.pinned && event->oncpu != smp_processor_id()) {
4334 ret = -EBUSY;
4335 goto out;
4336 }
4337
Kaixu Xiaffe86902015-08-06 07:02:32 +00004338 /*
4339 * If the event is currently on this CPU, its either a per-task event,
4340 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
4341 * oncpu == -1).
4342 */
4343 if (event->oncpu == smp_processor_id())
4344 event->pmu->read(event);
4345
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004346 *value = local64_read(&event->count);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004347 if (enabled || running) {
4348 u64 now = event->shadow_ctx_time + perf_clock();
4349 u64 __enabled, __running;
4350
4351 __perf_update_times(event, now, &__enabled, &__running);
4352 if (enabled)
4353 *enabled = __enabled;
4354 if (running)
4355 *running = __running;
4356 }
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004357out:
Kaixu Xiaffe86902015-08-06 07:02:32 +00004358 local_irq_restore(flags);
4359
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004360 return ret;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004361}
4362
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004363static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004364{
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004365 enum perf_event_state state = READ_ONCE(event->state);
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004366 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 /*
4369 * If event is enabled and currently active on a CPU, update the
4370 * value in the event structure:
4371 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004372again:
4373 if (state == PERF_EVENT_STATE_ACTIVE) {
4374 struct perf_read_data data;
4375
4376 /*
4377 * Orders the ->state and ->oncpu loads such that if we see
4378 * ACTIVE we must also see the right ->oncpu.
4379 *
4380 * Matches the smp_wmb() from event_sched_in().
4381 */
4382 smp_rmb();
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004383
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004384 event_cpu = READ_ONCE(event->oncpu);
4385 if ((unsigned)event_cpu >= nr_cpu_ids)
4386 return 0;
4387
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004388 data = (struct perf_read_data){
4389 .event = event,
4390 .group = group,
4391 .ret = 0,
4392 };
4393
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004394 preempt_disable();
4395 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004396
Peter Zijlstra58763142016-08-30 10:15:03 +02004397 /*
4398 * Purposely ignore the smp_call_function_single() return
4399 * value.
4400 *
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004401 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02004402 * scheduled out and that will have updated the event count.
4403 *
4404 * Therefore, either way, we'll have an up-to-date event count
4405 * after this.
4406 */
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004407 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
4408 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02004409 ret = data.ret;
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004410
4411 } else if (state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01004412 struct perf_event_context *ctx = event->ctx;
4413 unsigned long flags;
4414
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004415 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004416 state = event->state;
4417 if (state != PERF_EVENT_STATE_INACTIVE) {
4418 raw_spin_unlock_irqrestore(&ctx->lock, flags);
4419 goto again;
4420 }
4421
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004422 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004423 * May read while context is not active (e.g., thread is
4424 * blocked), in that case we cannot update context time
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004425 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004426 if (ctx->is_active & EVENT_TIME) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004427 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004428 update_cgrp_time_from_event(event);
4429 }
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004430
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004431 perf_event_update_time(event);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004432 if (group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004433 perf_event_update_sibling_time(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004434 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004435 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004436
4437 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004438}
4439
4440/*
4441 * Initialize the perf_event context in a task_struct:
4442 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02004443static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004444{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004445 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00004447 INIT_LIST_HEAD(&ctx->active_ctx_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03004448 perf_event_groups_init(&ctx->pinned_groups);
4449 perf_event_groups_init(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004450 INIT_LIST_HEAD(&ctx->event_list);
Peter Zijlstra66681282017-11-13 14:28:38 +01004451 INIT_LIST_HEAD(&ctx->pinned_active);
4452 INIT_LIST_HEAD(&ctx->flexible_active);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02004453 refcount_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454}
4455
Peter Zijlstraeb184472010-09-07 15:55:13 +02004456static struct perf_event_context *
4457alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458{
4459 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004460
4461 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
4462 if (!ctx)
4463 return NULL;
4464
4465 __perf_event_init_context(ctx);
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -07004466 if (task)
4467 ctx->task = get_task_struct(task);
Peter Zijlstraeb184472010-09-07 15:55:13 +02004468 ctx->pmu = pmu;
4469
4470 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004471}
4472
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004473static struct task_struct *
4474find_lively_task_by_vpid(pid_t vpid)
4475{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004478 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004479 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004480 task = current;
4481 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004482 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004483 if (task)
4484 get_task_struct(task);
4485 rcu_read_unlock();
4486
4487 if (!task)
4488 return ERR_PTR(-ESRCH);
4489
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004490 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004491}
4492
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004493/*
4494 * Returns a matching context with refcount and pincount.
4495 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004496static struct perf_event_context *
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004497find_get_context(struct pmu *pmu, struct task_struct *task,
4498 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004499{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004500 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004502 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004503 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004504 int ctxn, err;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004505 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01004507 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508 /* Must be root to operate on a CPU event: */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004509 err = perf_allow_cpu(&event->attr);
4510 if (err)
4511 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004513 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004514 ctx = &cpuctx->ctx;
4515 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004516 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004517
4518 return ctx;
4519 }
4520
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004521 err = -EINVAL;
4522 ctxn = pmu->task_ctx_nr;
4523 if (ctxn < 0)
4524 goto errout;
4525
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004526 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
Kan Liangff9ff922020-07-03 05:49:21 -07004527 task_ctx_data = alloc_task_ctx_data(pmu);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004528 if (!task_ctx_data) {
4529 err = -ENOMEM;
4530 goto errout;
4531 }
4532 }
4533
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004534retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004535 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004537 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004538 ++ctx->pin_count;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004539
4540 if (task_ctx_data && !ctx->task_ctx_data) {
4541 ctx->task_ctx_data = task_ctx_data;
4542 task_ctx_data = NULL;
4543 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004544 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004545
4546 if (clone_ctx)
4547 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004548 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02004549 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550 err = -ENOMEM;
4551 if (!ctx)
4552 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004553
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004554 if (task_ctx_data) {
4555 ctx->task_ctx_data = task_ctx_data;
4556 task_ctx_data = NULL;
4557 }
4558
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004559 err = 0;
4560 mutex_lock(&task->perf_event_mutex);
4561 /*
4562 * If it has already passed perf_event_exit_task().
4563 * we must see PF_EXITING, it takes this mutex too.
4564 */
4565 if (task->flags & PF_EXITING)
4566 err = -ESRCH;
4567 else if (task->perf_event_ctxp[ctxn])
4568 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004569 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004570 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004571 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004572 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004573 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004574 mutex_unlock(&task->perf_event_mutex);
4575
4576 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004577 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004578
4579 if (err == -EAGAIN)
4580 goto retry;
4581 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004582 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004583 }
4584
Kan Liangff9ff922020-07-03 05:49:21 -07004585 free_task_ctx_data(pmu, task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004586 return ctx;
4587
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004588errout:
Kan Liangff9ff922020-07-03 05:49:21 -07004589 free_task_ctx_data(pmu, task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004590 return ERR_PTR(err);
4591}
4592
Li Zefan6fb29152009-10-15 11:21:42 +08004593static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004594static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08004595
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004596static void free_event_rcu(struct rcu_head *head)
4597{
4598 struct perf_event *event;
4599
4600 event = container_of(head, struct perf_event, rcu_head);
4601 if (event->ns)
4602 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08004603 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004604 kfree(event);
4605}
4606
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004607static void ring_buffer_attach(struct perf_event *event,
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05004608 struct perf_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004609
Kan Liangf2fb6be2016-03-23 11:24:37 -07004610static void detach_sb_event(struct perf_event *event)
4611{
4612 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
4613
4614 raw_spin_lock(&pel->lock);
4615 list_del_rcu(&event->sb_list);
4616 raw_spin_unlock(&pel->lock);
4617}
4618
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004619static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07004620{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004621 struct perf_event_attr *attr = &event->attr;
4622
Kan Liangf2fb6be2016-03-23 11:24:37 -07004623 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004624 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004625
4626 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004627 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004628
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004629 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
4630 attr->comm || attr->comm_exec ||
Song Liu76193a92019-01-17 08:15:13 -08004631 attr->task || attr->ksymbol ||
Adrian Huntere17d43b2020-05-12 15:19:08 +03004632 attr->context_switch || attr->text_poke ||
Song Liu21038f22019-02-25 16:20:05 -08004633 attr->bpf_event)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004634 return true;
4635 return false;
4636}
4637
4638static void unaccount_pmu_sb_event(struct perf_event *event)
4639{
4640 if (is_sb_event(event))
4641 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004642}
4643
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004644static void unaccount_event_cpu(struct perf_event *event, int cpu)
4645{
4646 if (event->parent)
4647 return;
4648
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004649 if (is_cgroup_event(event))
4650 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
4651}
4652
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004653#ifdef CONFIG_NO_HZ_FULL
4654static DEFINE_SPINLOCK(nr_freq_lock);
4655#endif
4656
4657static void unaccount_freq_event_nohz(void)
4658{
4659#ifdef CONFIG_NO_HZ_FULL
4660 spin_lock(&nr_freq_lock);
4661 if (atomic_dec_and_test(&nr_freq_events))
4662 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
4663 spin_unlock(&nr_freq_lock);
4664#endif
4665}
4666
4667static void unaccount_freq_event(void)
4668{
4669 if (tick_nohz_full_enabled())
4670 unaccount_freq_event_nohz();
4671 else
4672 atomic_dec(&nr_freq_events);
4673}
4674
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004675static void unaccount_event(struct perf_event *event)
4676{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004677 bool dec = false;
4678
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004679 if (event->parent)
4680 return;
4681
4682 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004683 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004684 if (event->attr.mmap || event->attr.mmap_data)
4685 atomic_dec(&nr_mmap_events);
4686 if (event->attr.comm)
4687 atomic_dec(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +05304688 if (event->attr.namespaces)
4689 atomic_dec(&nr_namespaces_events);
Namhyung Kim96aaab62020-03-25 21:45:28 +09004690 if (event->attr.cgroup)
4691 atomic_dec(&nr_cgroup_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004692 if (event->attr.task)
4693 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02004694 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004695 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03004696 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004697 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03004698 atomic_dec(&nr_switch_events);
4699 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004700 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004701 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004702 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004703 dec = true;
Song Liu76193a92019-01-17 08:15:13 -08004704 if (event->attr.ksymbol)
4705 atomic_dec(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -08004706 if (event->attr.bpf_event)
4707 atomic_dec(&nr_bpf_events);
Adrian Huntere17d43b2020-05-12 15:19:08 +03004708 if (event->attr.text_poke)
4709 atomic_dec(&nr_text_poke_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004710
Peter Zijlstra9107c892016-02-24 18:45:45 +01004711 if (dec) {
4712 if (!atomic_add_unless(&perf_sched_count, -1, 1))
4713 schedule_delayed_work(&perf_sched_work, HZ);
4714 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004715
4716 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004717
4718 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004719}
4720
Peter Zijlstra9107c892016-02-24 18:45:45 +01004721static void perf_sched_delayed(struct work_struct *work)
4722{
4723 mutex_lock(&perf_sched_mutex);
4724 if (atomic_dec_and_test(&perf_sched_count))
4725 static_branch_disable(&perf_sched_events);
4726 mutex_unlock(&perf_sched_mutex);
4727}
4728
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004729/*
4730 * The following implement mutual exclusion of events on "exclusive" pmus
4731 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
4732 * at a time, so we disallow creating events that might conflict, namely:
4733 *
4734 * 1) cpu-wide events in the presence of per-task events,
4735 * 2) per-task events in the presence of cpu-wide events,
4736 * 3) two matching events on the same context.
4737 *
4738 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004739 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004740 */
4741static int exclusive_event_init(struct perf_event *event)
4742{
4743 struct pmu *pmu = event->pmu;
4744
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004745 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004746 return 0;
4747
4748 /*
4749 * Prevent co-existence of per-task and cpu-wide events on the
4750 * same exclusive pmu.
4751 *
4752 * Negative pmu::exclusive_cnt means there are cpu-wide
4753 * events on this "exclusive" pmu, positive means there are
4754 * per-task events.
4755 *
4756 * Since this is called in perf_event_alloc() path, event::ctx
4757 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4758 * to mean "per-task event", because unlike other attach states it
4759 * never gets cleared.
4760 */
4761 if (event->attach_state & PERF_ATTACH_TASK) {
4762 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4763 return -EBUSY;
4764 } else {
4765 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4766 return -EBUSY;
4767 }
4768
4769 return 0;
4770}
4771
4772static void exclusive_event_destroy(struct perf_event *event)
4773{
4774 struct pmu *pmu = event->pmu;
4775
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004776 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004777 return;
4778
4779 /* see comment in exclusive_event_init() */
4780 if (event->attach_state & PERF_ATTACH_TASK)
4781 atomic_dec(&pmu->exclusive_cnt);
4782 else
4783 atomic_inc(&pmu->exclusive_cnt);
4784}
4785
4786static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4787{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004788 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004789 (e1->cpu == e2->cpu ||
4790 e1->cpu == -1 ||
4791 e2->cpu == -1))
4792 return true;
4793 return false;
4794}
4795
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004796static bool exclusive_event_installable(struct perf_event *event,
4797 struct perf_event_context *ctx)
4798{
4799 struct perf_event *iter_event;
4800 struct pmu *pmu = event->pmu;
4801
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004802 lockdep_assert_held(&ctx->mutex);
4803
4804 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004805 return true;
4806
4807 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4808 if (exclusive_event_match(iter_event, event))
4809 return false;
4810 }
4811
4812 return true;
4813}
4814
Alexander Shishkin375637b2016-04-27 18:44:46 +03004815static void perf_addr_filters_splice(struct perf_event *event,
4816 struct list_head *head);
4817
Peter Zijlstra683ede42014-05-05 12:11:24 +02004818static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004819{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004820 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004821
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004822 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004823
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004824 security_perf_event_free(event);
4825
Frederic Weisbecker76369132011-05-19 19:55:04 +02004826 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004827 /*
4828 * Can happen when we close an event with re-directed output.
4829 *
4830 * Since we have a 0 refcount, perf_mmap_close() will skip
4831 * over us; possibly making our ring_buffer_put() the last.
4832 */
4833 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004834 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004835 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004836 }
4837
Stephane Eraniane5d13672011-02-14 11:20:01 +02004838 if (is_cgroup_event(event))
4839 perf_detach_cgroup(event);
4840
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004841 if (!event->parent) {
4842 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4843 put_callchain_buffers();
4844 }
4845
4846 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004847 perf_addr_filters_splice(event, NULL);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02004848 kfree(event->addr_filter_ranges);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004849
4850 if (event->destroy)
4851 event->destroy(event);
4852
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004853 /*
4854 * Must be after ->destroy(), due to uprobe_perf_close() using
4855 * hw.target.
4856 */
Prashant Bhole621b6d22018-04-09 19:03:46 +09004857 if (event->hw.target)
4858 put_task_struct(event->hw.target);
4859
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004860 /*
4861 * perf_event_free_task() relies on put_ctx() being 'last', in particular
4862 * all task references must be cleaned up.
4863 */
4864 if (event->ctx)
4865 put_ctx(event->ctx);
4866
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004867 exclusive_event_destroy(event);
4868 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004869
4870 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871}
4872
Peter Zijlstra683ede42014-05-05 12:11:24 +02004873/*
4874 * Used to free events which have a known refcount of 1, such as in error paths
4875 * where the event isn't exposed yet and inherited events.
4876 */
4877static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004878{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004879 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4880 "unexpected event refcount: %ld; ptr=%p\n",
4881 atomic_long_read(&event->refcount), event)) {
4882 /* leak to avoid use-after-free */
4883 return;
4884 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004885
Peter Zijlstra683ede42014-05-05 12:11:24 +02004886 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004887}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004888
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004889/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004890 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004891 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004892static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004893{
Peter Zijlstra88821352010-11-09 19:01:43 +01004894 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004895
Peter Zijlstra88821352010-11-09 19:01:43 +01004896 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004897 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004898 * Matches the smp_store_release() in perf_event_exit_task(). If we
4899 * observe !owner it means the list deletion is complete and we can
4900 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004901 * owner->perf_event_mutex.
4902 */
Will Deacon506458e2017-10-24 11:22:48 +01004903 owner = READ_ONCE(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004904 if (owner) {
4905 /*
4906 * Since delayed_put_task_struct() also drops the last
4907 * task reference we can safely take a new reference
4908 * while holding the rcu_read_lock().
4909 */
4910 get_task_struct(owner);
4911 }
4912 rcu_read_unlock();
4913
4914 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004915 /*
4916 * If we're here through perf_event_exit_task() we're already
4917 * holding ctx->mutex which would be an inversion wrt. the
4918 * normal lock order.
4919 *
4920 * However we can safely take this lock because its the child
4921 * ctx->mutex.
4922 */
4923 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4924
Peter Zijlstra88821352010-11-09 19:01:43 +01004925 /*
4926 * We have to re-check the event->owner field, if it is cleared
4927 * we raced with perf_event_exit_task(), acquiring the mutex
4928 * ensured they're done, and we can proceed with freeing the
4929 * event.
4930 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004931 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004932 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004933 smp_store_release(&event->owner, NULL);
4934 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004935 mutex_unlock(&owner->perf_event_mutex);
4936 put_task_struct(owner);
4937 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004938}
4939
Jiri Olsaf8697762014-08-01 14:33:01 +02004940static void put_event(struct perf_event *event)
4941{
Jiri Olsaf8697762014-08-01 14:33:01 +02004942 if (!atomic_long_dec_and_test(&event->refcount))
4943 return;
4944
Peter Zijlstra683ede42014-05-05 12:11:24 +02004945 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004946}
4947
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004948/*
4949 * Kill an event dead; while event:refcount will preserve the event
4950 * object, it will not preserve its functionality. Once the last 'user'
4951 * gives up the object, we'll destroy the thing.
4952 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004953int perf_event_release_kernel(struct perf_event *event)
4954{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004955 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004956 struct perf_event *child, *tmp;
Peter Zijlstra82d94852018-01-09 13:10:30 +01004957 LIST_HEAD(free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004958
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004959 /*
4960 * If we got here through err_file: fput(event_file); we will not have
4961 * attached to a context yet.
4962 */
4963 if (!ctx) {
4964 WARN_ON_ONCE(event->attach_state &
4965 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4966 goto no_ctx;
4967 }
4968
Peter Zijlstra88821352010-11-09 19:01:43 +01004969 if (!is_kernel_event(event))
4970 perf_remove_from_owner(event);
4971
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004972 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004973 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004974 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004975
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004976 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004977 /*
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +01004978 * Mark this event as STATE_DEAD, there is no external reference to it
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004979 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004980 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004981 * Anybody acquiring event->child_mutex after the below loop _must_
4982 * also see this, most importantly inherit_event() which will avoid
4983 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004984 *
4985 * Thus this guarantees that we will in fact observe and kill _ALL_
4986 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004987 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004988 event->state = PERF_EVENT_STATE_DEAD;
4989 raw_spin_unlock_irq(&ctx->lock);
4990
4991 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004992
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004993again:
4994 mutex_lock(&event->child_mutex);
4995 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004996
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004997 /*
4998 * Cannot change, child events are not migrated, see the
4999 * comment with perf_event_ctx_lock_nested().
5000 */
Will Deacon506458e2017-10-24 11:22:48 +01005001 ctx = READ_ONCE(child->ctx);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02005002 /*
5003 * Since child_mutex nests inside ctx::mutex, we must jump
5004 * through hoops. We start by grabbing a reference on the ctx.
5005 *
5006 * Since the event cannot get freed while we hold the
5007 * child_mutex, the context must also exist and have a !0
5008 * reference count.
5009 */
5010 get_ctx(ctx);
5011
5012 /*
5013 * Now that we have a ctx ref, we can drop child_mutex, and
5014 * acquire ctx::mutex without fear of it going away. Then we
5015 * can re-acquire child_mutex.
5016 */
5017 mutex_unlock(&event->child_mutex);
5018 mutex_lock(&ctx->mutex);
5019 mutex_lock(&event->child_mutex);
5020
5021 /*
5022 * Now that we hold ctx::mutex and child_mutex, revalidate our
5023 * state, if child is still the first entry, it didn't get freed
5024 * and we can continue doing so.
5025 */
5026 tmp = list_first_entry_or_null(&event->child_list,
5027 struct perf_event, child_list);
5028 if (tmp == child) {
5029 perf_remove_from_context(child, DETACH_GROUP);
Peter Zijlstra82d94852018-01-09 13:10:30 +01005030 list_move(&child->child_list, &free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02005031 /*
5032 * This matches the refcount bump in inherit_event();
5033 * this can't be the last reference.
5034 */
5035 put_event(event);
5036 }
5037
5038 mutex_unlock(&event->child_mutex);
5039 mutex_unlock(&ctx->mutex);
5040 put_ctx(ctx);
5041 goto again;
5042 }
5043 mutex_unlock(&event->child_mutex);
5044
Peter Zijlstra82d94852018-01-09 13:10:30 +01005045 list_for_each_entry_safe(child, tmp, &free_list, child_list) {
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02005046 void *var = &child->ctx->refcount;
5047
Peter Zijlstra82d94852018-01-09 13:10:30 +01005048 list_del(&child->child_list);
5049 free_event(child);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02005050
5051 /*
5052 * Wake any perf_event_free_task() waiting for this event to be
5053 * freed.
5054 */
5055 smp_mb(); /* pairs with wait_var_event() */
5056 wake_up_var(var);
Peter Zijlstra82d94852018-01-09 13:10:30 +01005057 }
5058
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01005059no_ctx:
5060 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02005061 return 0;
5062}
5063EXPORT_SYMBOL_GPL(perf_event_release_kernel);
5064
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02005065/*
5066 * Called when the last reference to the file is gone.
5067 */
Al Viroa6fa9412012-08-20 14:59:25 +01005068static int perf_release(struct inode *inode, struct file *file)
5069{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02005070 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01005071 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01005072}
5073
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005074static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005075{
5076 struct perf_event *child;
5077 u64 total = 0;
5078
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005079 *enabled = 0;
5080 *running = 0;
5081
Peter Zijlstra6f105812009-11-20 22:19:56 +01005082 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005083
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005084 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005085 total += perf_event_count(event);
5086
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005087 *enabled += event->total_time_enabled +
5088 atomic64_read(&event->child_total_time_enabled);
5089 *running += event->total_time_running +
5090 atomic64_read(&event->child_total_time_running);
5091
5092 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005093 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005094 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005095 *enabled += child->total_time_enabled;
5096 *running += child->total_time_running;
5097 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01005098 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005099
5100 return total;
5101}
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005102
5103u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
5104{
5105 struct perf_event_context *ctx;
5106 u64 count;
5107
5108 ctx = perf_event_ctx_lock(event);
5109 count = __perf_event_read_value(event, enabled, running);
5110 perf_event_ctx_unlock(event, ctx);
5111
5112 return count;
5113}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005114EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005116static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005117 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005118{
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005119 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005120 struct perf_event *sub;
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005121 unsigned long flags;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005122 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005123 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01005124
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005125 ret = perf_event_read(leader, true);
5126 if (ret)
5127 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005128
Peter Zijlstraa9cd8192017-09-05 13:38:24 +02005129 raw_spin_lock_irqsave(&ctx->lock, flags);
5130
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005131 /*
5132 * Since we co-schedule groups, {enabled,running} times of siblings
5133 * will be identical to those of the leader, so we only publish one
5134 * set.
5135 */
5136 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
5137 values[n++] += leader->total_time_enabled +
5138 atomic64_read(&leader->child_total_time_enabled);
5139 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005140
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005141 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
5142 values[n++] += leader->total_time_running +
5143 atomic64_read(&leader->child_total_time_running);
5144 }
5145
5146 /*
5147 * Write {count,id} tuples for every sibling.
5148 */
5149 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005150 if (read_format & PERF_FORMAT_ID)
5151 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005152
Peter Zijlstraedb39592018-03-15 17:36:56 +01005153 for_each_sibling_event(sub, leader) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005154 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005155 if (read_format & PERF_FORMAT_ID)
5156 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005158
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005159 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005160 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005161}
5162
5163static int perf_read_group(struct perf_event *event,
5164 u64 read_format, char __user *buf)
5165{
5166 struct perf_event *leader = event->group_leader, *child;
5167 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005168 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005169 u64 *values;
5170
5171 lockdep_assert_held(&ctx->mutex);
5172
5173 values = kzalloc(event->read_size, GFP_KERNEL);
5174 if (!values)
5175 return -ENOMEM;
5176
5177 values[0] = 1 + leader->nr_siblings;
5178
5179 /*
5180 * By locking the child_mutex of the leader we effectively
5181 * lock the child list of all siblings.. XXX explain how.
5182 */
5183 mutex_lock(&leader->child_mutex);
5184
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005185 ret = __perf_read_group_add(leader, read_format, values);
5186 if (ret)
5187 goto unlock;
5188
5189 list_for_each_entry(child, &leader->child_list, child_list) {
5190 ret = __perf_read_group_add(child, read_format, values);
5191 if (ret)
5192 goto unlock;
5193 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005194
5195 mutex_unlock(&leader->child_mutex);
5196
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005197 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005198 if (copy_to_user(buf, values, event->read_size))
5199 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005200 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005201
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005202unlock:
5203 mutex_unlock(&leader->child_mutex);
5204out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005205 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005206 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005207}
5208
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005209static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005210 u64 read_format, char __user *buf)
5211{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005212 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005213 u64 values[4];
5214 int n = 0;
5215
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005216 values[n++] = __perf_event_read_value(event, &enabled, &running);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005217 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
5218 values[n++] = enabled;
5219 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
5220 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005221 if (read_format & PERF_FORMAT_ID)
5222 values[n++] = primary_event_id(event);
5223
5224 if (copy_to_user(buf, values, n * sizeof(u64)))
5225 return -EFAULT;
5226
5227 return n * sizeof(u64);
5228}
5229
Jiri Olsadc633982014-09-12 13:18:26 +02005230static bool is_event_hup(struct perf_event *event)
5231{
5232 bool no_children;
5233
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01005234 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02005235 return false;
5236
5237 mutex_lock(&event->child_mutex);
5238 no_children = list_empty(&event->child_list);
5239 mutex_unlock(&event->child_mutex);
5240 return no_children;
5241}
5242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005243/*
5244 * Read the performance event - simple non blocking version for now
5245 */
5246static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005247__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005248{
5249 u64 read_format = event->attr.read_format;
5250 int ret;
5251
5252 /*
Tobias Tefke788faab2018-07-09 12:57:15 +02005253 * Return end-of-file for a read on an event that is in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254 * error state (i.e. because it was pinned but it couldn't be
5255 * scheduled on to the CPU at some point).
5256 */
5257 if (event->state == PERF_EVENT_STATE_ERROR)
5258 return 0;
5259
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005260 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005261 return -ENOSPC;
5262
5263 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005264 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005265 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005267 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268
5269 return ret;
5270}
5271
5272static ssize_t
5273perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
5274{
5275 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005276 struct perf_event_context *ctx;
5277 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005279 ret = security_perf_event_read(event);
5280 if (ret)
5281 return ret;
5282
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005283 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005284 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005285 perf_event_ctx_unlock(event, ctx);
5286
5287 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005288}
5289
Al Viro9dd95742017-07-03 00:42:43 -04005290static __poll_t perf_poll(struct file *file, poll_table *wait)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291{
5292 struct perf_event *event = file->private_data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005293 struct perf_buffer *rb;
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005294 __poll_t events = EPOLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02005296 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04005297
Jiri Olsadc633982014-09-12 13:18:26 +02005298 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04005299 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005300
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005301 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005302 * Pin the event->rb by taking event->mmap_mutex; otherwise
5303 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005304 */
5305 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005306 rb = event->rb;
5307 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005308 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005309 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005310 return events;
5311}
5312
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005313static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005314{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005315 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02005316 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005317 perf_event_update_userpage(event);
5318}
5319
Like Xu52ba4b02019-10-27 18:52:39 +08005320/* Assume it's not an event with inherit set. */
5321u64 perf_event_pause(struct perf_event *event, bool reset)
5322{
5323 struct perf_event_context *ctx;
5324 u64 count;
5325
5326 ctx = perf_event_ctx_lock(event);
5327 WARN_ON_ONCE(event->attr.inherit);
5328 _perf_event_disable(event);
5329 count = local64_read(&event->count);
5330 if (reset)
5331 local64_set(&event->count, 0);
5332 perf_event_ctx_unlock(event, ctx);
5333
5334 return count;
5335}
5336EXPORT_SYMBOL_GPL(perf_event_pause);
5337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005338/*
5339 * Holding the top-level event's child_mutex means that any
5340 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01005341 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342 * task existence requirements of perf_event_enable/disable.
5343 */
5344static void perf_event_for_each_child(struct perf_event *event,
5345 void (*func)(struct perf_event *))
5346{
5347 struct perf_event *child;
5348
5349 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005350
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005351 mutex_lock(&event->child_mutex);
5352 func(event);
5353 list_for_each_entry(child, &event->child_list, child_list)
5354 func(child);
5355 mutex_unlock(&event->child_mutex);
5356}
5357
5358static void perf_event_for_each(struct perf_event *event,
5359 void (*func)(struct perf_event *))
5360{
5361 struct perf_event_context *ctx = event->ctx;
5362 struct perf_event *sibling;
5363
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005364 lockdep_assert_held(&ctx->mutex);
5365
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005366 event = event->group_leader;
5367
5368 perf_event_for_each_child(event, func);
Peter Zijlstraedb39592018-03-15 17:36:56 +01005369 for_each_sibling_event(sibling, event)
Michael Ellerman724b6da2012-04-11 11:54:13 +10005370 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005371}
5372
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005373static void __perf_event_period(struct perf_event *event,
5374 struct perf_cpu_context *cpuctx,
5375 struct perf_event_context *ctx,
5376 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01005377{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005378 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005379 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005380
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005382 event->attr.sample_freq = value;
5383 } else {
5384 event->attr.sample_period = value;
5385 event->hw.sample_period = value;
5386 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005387
5388 active = (event->state == PERF_EVENT_STATE_ACTIVE);
5389 if (active) {
5390 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01005391 /*
5392 * We could be throttled; unthrottle now to avoid the tick
5393 * trying to unthrottle while we already re-started the event.
5394 */
5395 if (event->hw.interrupts == MAX_INTERRUPTS) {
5396 event->hw.interrupts = 0;
5397 perf_log_throttle(event, 1);
5398 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005399 event->pmu->stop(event, PERF_EF_UPDATE);
5400 }
5401
5402 local64_set(&event->hw.period_left, 0);
5403
5404 if (active) {
5405 event->pmu->start(event, PERF_EF_RELOAD);
5406 perf_pmu_enable(ctx->pmu);
5407 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005408}
5409
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005410static int perf_event_check_period(struct perf_event *event, u64 value)
5411{
5412 return event->pmu->check_period(event, value);
5413}
5414
Like Xu3ca270f2019-10-27 18:52:38 +08005415static int _perf_event_period(struct perf_event *event, u64 value)
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005416{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005417 if (!is_sampling_event(event))
5418 return -EINVAL;
5419
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005420 if (!value)
5421 return -EINVAL;
5422
5423 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
5424 return -EINVAL;
5425
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005426 if (perf_event_check_period(event, value))
5427 return -EINVAL;
5428
Ravi Bangoria913a90b2019-06-04 09:59:53 +05305429 if (!event->attr.freq && (value & (1ULL << 63)))
5430 return -EINVAL;
5431
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005432 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005433
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005434 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005435}
5436
Like Xu3ca270f2019-10-27 18:52:38 +08005437int perf_event_period(struct perf_event *event, u64 value)
5438{
5439 struct perf_event_context *ctx;
5440 int ret;
5441
5442 ctx = perf_event_ctx_lock(event);
5443 ret = _perf_event_period(event, value);
5444 perf_event_ctx_unlock(event, ctx);
5445
5446 return ret;
5447}
5448EXPORT_SYMBOL_GPL(perf_event_period);
5449
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005450static const struct file_operations perf_fops;
5451
Al Viro2903ff02012-08-28 12:52:22 -04005452static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005453{
Al Viro2903ff02012-08-28 12:52:22 -04005454 struct fd f = fdget(fd);
5455 if (!f.file)
5456 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005457
Al Viro2903ff02012-08-28 12:52:22 -04005458 if (f.file->f_op != &perf_fops) {
5459 fdput(f);
5460 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005461 }
Al Viro2903ff02012-08-28 12:52:22 -04005462 *p = f;
5463 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005464}
5465
5466static int perf_event_set_output(struct perf_event *event,
5467 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08005468static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07005469static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005470static int perf_copy_attr(struct perf_event_attr __user *uattr,
5471 struct perf_event_attr *attr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005472
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005473static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005474{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005475 void (*func)(struct perf_event *);
5476 u32 flags = arg;
5477
5478 switch (cmd) {
5479 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005480 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481 break;
5482 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005483 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005484 break;
5485 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005486 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005487 break;
5488
5489 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005490 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005491
5492 case PERF_EVENT_IOC_PERIOD:
Like Xu3ca270f2019-10-27 18:52:38 +08005493 {
5494 u64 value;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005495
Like Xu3ca270f2019-10-27 18:52:38 +08005496 if (copy_from_user(&value, (u64 __user *)arg, sizeof(value)))
5497 return -EFAULT;
5498
5499 return _perf_event_period(event, value);
5500 }
Jiri Olsacf4957f2012-10-24 13:37:58 +02005501 case PERF_EVENT_IOC_ID:
5502 {
5503 u64 id = primary_event_id(event);
5504
5505 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
5506 return -EFAULT;
5507 return 0;
5508 }
5509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005510 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005511 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005512 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005513 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04005514 struct perf_event *output_event;
5515 struct fd output;
5516 ret = perf_fget_light(arg, &output);
5517 if (ret)
5518 return ret;
5519 output_event = output.file->private_data;
5520 ret = perf_event_set_output(event, output_event);
5521 fdput(output);
5522 } else {
5523 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005524 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005525 return ret;
5526 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005527
Li Zefan6fb29152009-10-15 11:21:42 +08005528 case PERF_EVENT_IOC_SET_FILTER:
5529 return perf_event_set_filter(event, (void __user *)arg);
5530
Alexei Starovoitov25415172015-03-25 12:49:20 -07005531 case PERF_EVENT_IOC_SET_BPF:
5532 return perf_event_set_bpf_prog(event, arg);
5533
Wang Nan86e79722016-03-28 06:41:29 +00005534 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005535 struct perf_buffer *rb;
Wang Nan86e79722016-03-28 06:41:29 +00005536
5537 rcu_read_lock();
5538 rb = rcu_dereference(event->rb);
5539 if (!rb || !rb->nr_pages) {
5540 rcu_read_unlock();
5541 return -EINVAL;
5542 }
5543 rb_toggle_paused(rb, !!arg);
5544 rcu_read_unlock();
5545 return 0;
5546 }
Yonghong Songf371b302017-12-11 11:39:02 -08005547
5548 case PERF_EVENT_IOC_QUERY_BPF:
Yonghong Songf4e22982017-12-13 10:35:37 -08005549 return perf_event_query_prog_array(event, (void __user *)arg);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005550
5551 case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
5552 struct perf_event_attr new_attr;
5553 int err = perf_copy_attr((struct perf_event_attr __user *)arg,
5554 &new_attr);
5555
5556 if (err)
5557 return err;
5558
5559 return perf_event_modify_attr(event, &new_attr);
5560 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005561 default:
5562 return -ENOTTY;
5563 }
5564
5565 if (flags & PERF_IOC_FLAG_GROUP)
5566 perf_event_for_each(event, func);
5567 else
5568 perf_event_for_each_child(event, func);
5569
5570 return 0;
5571}
5572
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005573static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5574{
5575 struct perf_event *event = file->private_data;
5576 struct perf_event_context *ctx;
5577 long ret;
5578
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005579 /* Treat ioctl like writes as it is likely a mutating operation. */
5580 ret = security_perf_event_write(event);
5581 if (ret)
5582 return ret;
5583
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005584 ctx = perf_event_ctx_lock(event);
5585 ret = _perf_ioctl(event, cmd, arg);
5586 perf_event_ctx_unlock(event, ctx);
5587
5588 return ret;
5589}
5590
Pawel Mollb3f20782014-06-13 16:03:32 +01005591#ifdef CONFIG_COMPAT
5592static long perf_compat_ioctl(struct file *file, unsigned int cmd,
5593 unsigned long arg)
5594{
5595 switch (_IOC_NR(cmd)) {
5596 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
5597 case _IOC_NR(PERF_EVENT_IOC_ID):
Eugene Syromiatnikov82489c52018-05-21 14:34:20 +02005598 case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF):
5599 case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES):
Pawel Mollb3f20782014-06-13 16:03:32 +01005600 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
5601 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
5602 cmd &= ~IOCSIZE_MASK;
5603 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
5604 }
5605 break;
5606 }
5607 return perf_ioctl(file, cmd, arg);
5608}
5609#else
5610# define perf_compat_ioctl NULL
5611#endif
5612
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005613int perf_event_task_enable(void)
5614{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005615 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005616 struct perf_event *event;
5617
5618 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005619 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5620 ctx = perf_event_ctx_lock(event);
5621 perf_event_for_each_child(event, _perf_event_enable);
5622 perf_event_ctx_unlock(event, ctx);
5623 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005624 mutex_unlock(&current->perf_event_mutex);
5625
5626 return 0;
5627}
5628
5629int perf_event_task_disable(void)
5630{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005631 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005632 struct perf_event *event;
5633
5634 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005635 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5636 ctx = perf_event_ctx_lock(event);
5637 perf_event_for_each_child(event, _perf_event_disable);
5638 perf_event_ctx_unlock(event, ctx);
5639 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005640 mutex_unlock(&current->perf_event_mutex);
5641
5642 return 0;
5643}
5644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645static int perf_event_index(struct perf_event *event)
5646{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005647 if (event->hw.state & PERF_HES_STOPPED)
5648 return 0;
5649
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005650 if (event->state != PERF_EVENT_STATE_ACTIVE)
5651 return 0;
5652
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005653 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005654}
5655
Eric B Munsonc4794292011-06-23 16:34:38 -04005656static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005657 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04005658 u64 *enabled,
5659 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04005660{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005661 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04005662
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005663 *now = perf_clock();
5664 ctx_time = event->shadow_ctx_time + *now;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02005665 __perf_update_times(event, ctx_time, enabled, running);
Eric B Munsonc4794292011-06-23 16:34:38 -04005666}
5667
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005668static void perf_event_init_userpage(struct perf_event *event)
5669{
5670 struct perf_event_mmap_page *userpg;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005671 struct perf_buffer *rb;
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005672
5673 rcu_read_lock();
5674 rb = rcu_dereference(event->rb);
5675 if (!rb)
5676 goto unlock;
5677
5678 userpg = rb->user_page;
5679
5680 /* Allow new userspace to detect that bit 0 is deprecated */
5681 userpg->cap_bit0_is_deprecated = 1;
5682 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02005683 userpg->data_offset = PAGE_SIZE;
5684 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005685
5686unlock:
5687 rcu_read_unlock();
5688}
5689
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005690void __weak arch_perf_update_userpage(
5691 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005692{
5693}
5694
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695/*
5696 * Callers need to ensure there can be no nesting of this function, otherwise
5697 * the seqlock logic goes bad. We can not serialize this because the arch
5698 * code calls this from NMI context.
5699 */
5700void perf_event_update_userpage(struct perf_event *event)
5701{
5702 struct perf_event_mmap_page *userpg;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005703 struct perf_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005704 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005705
5706 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02005707 rb = rcu_dereference(event->rb);
5708 if (!rb)
5709 goto unlock;
5710
Eric B Munson0d641202011-06-24 12:26:26 -04005711 /*
5712 * compute total_time_enabled, total_time_running
5713 * based on snapshot values taken when the event
5714 * was last scheduled in.
5715 *
5716 * we cannot simply called update_context_time()
5717 * because of locking issue as we can be called in
5718 * NMI context
5719 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005720 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005721
Frederic Weisbecker76369132011-05-19 19:55:04 +02005722 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005723 /*
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07005724 * Disable preemption to guarantee consistent time stamps are stored to
5725 * the user page.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726 */
5727 preempt_disable();
5728 ++userpg->lock;
5729 barrier();
5730 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005731 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01005732 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02005733 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005734
Eric B Munson0d641202011-06-24 12:26:26 -04005735 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005736 atomic64_read(&event->child_total_time_enabled);
5737
Eric B Munson0d641202011-06-24 12:26:26 -04005738 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005739 atomic64_read(&event->child_total_time_running);
5740
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005741 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005743 barrier();
5744 ++userpg->lock;
5745 preempt_enable();
5746unlock:
5747 rcu_read_unlock();
5748}
Suzuki K Poulose82975c42018-01-02 11:25:26 +00005749EXPORT_SYMBOL_GPL(perf_event_update_userpage);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005750
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305751static vm_fault_t perf_mmap_fault(struct vm_fault *vmf)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005752{
Dave Jiang11bac802017-02-24 14:56:41 -08005753 struct perf_event *event = vmf->vma->vm_file->private_data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005754 struct perf_buffer *rb;
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305755 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005756
5757 if (vmf->flags & FAULT_FLAG_MKWRITE) {
5758 if (vmf->pgoff == 0)
5759 ret = 0;
5760 return ret;
5761 }
5762
5763 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005764 rb = rcu_dereference(event->rb);
5765 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005766 goto unlock;
5767
5768 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
5769 goto unlock;
5770
Frederic Weisbecker76369132011-05-19 19:55:04 +02005771 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005772 if (!vmf->page)
5773 goto unlock;
5774
5775 get_page(vmf->page);
Dave Jiang11bac802017-02-24 14:56:41 -08005776 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005777 vmf->page->index = vmf->pgoff;
5778
5779 ret = 0;
5780unlock:
5781 rcu_read_unlock();
5782
5783 return ret;
5784}
5785
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005786static void ring_buffer_attach(struct perf_event *event,
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005787 struct perf_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005788{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005789 struct perf_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005790 unsigned long flags;
5791
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005792 if (event->rb) {
5793 /*
5794 * Should be impossible, we set this when removing
5795 * event->rb_entry and wait/clear when adding event->rb_entry.
5796 */
5797 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005798
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005799 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005800 spin_lock_irqsave(&old_rb->event_lock, flags);
5801 list_del_rcu(&event->rb_entry);
5802 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005803
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005804 event->rcu_batches = get_state_synchronize_rcu();
5805 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005806 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005807
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005808 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005809 if (event->rcu_pending) {
5810 cond_synchronize_rcu(event->rcu_batches);
5811 event->rcu_pending = 0;
5812 }
5813
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005814 spin_lock_irqsave(&rb->event_lock, flags);
5815 list_add_rcu(&event->rb_entry, &rb->event_list);
5816 spin_unlock_irqrestore(&rb->event_lock, flags);
5817 }
5818
Alexander Shishkin767ae082016-09-06 16:23:49 +03005819 /*
5820 * Avoid racing with perf_mmap_close(AUX): stop the event
5821 * before swizzling the event::rb pointer; if it's getting
5822 * unmapped, its aux_mmap_count will be 0 and it won't
5823 * restart. See the comment in __perf_pmu_output_stop().
5824 *
5825 * Data will inevitably be lost when set_output is done in
5826 * mid-air, but then again, whoever does it like this is
5827 * not in for the data anyway.
5828 */
5829 if (has_aux(event))
5830 perf_event_stop(event, 0);
5831
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005832 rcu_assign_pointer(event->rb, rb);
5833
5834 if (old_rb) {
5835 ring_buffer_put(old_rb);
5836 /*
5837 * Since we detached before setting the new rb, so that we
5838 * could attach the new rb, we could have missed a wakeup.
5839 * Provide it now.
5840 */
5841 wake_up_all(&event->waitq);
5842 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005843}
5844
5845static void ring_buffer_wakeup(struct perf_event *event)
5846{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005847 struct perf_buffer *rb;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005848
5849 rcu_read_lock();
5850 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005851 if (rb) {
5852 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
5853 wake_up_all(&event->waitq);
5854 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005855 rcu_read_unlock();
5856}
5857
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005858struct perf_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005859{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005860 struct perf_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005861
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005862 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005863 rb = rcu_dereference(event->rb);
5864 if (rb) {
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005865 if (!refcount_inc_not_zero(&rb->refcount))
Frederic Weisbecker76369132011-05-19 19:55:04 +02005866 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005867 }
5868 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005869
Frederic Weisbecker76369132011-05-19 19:55:04 +02005870 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005871}
5872
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005873void ring_buffer_put(struct perf_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005874{
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005875 if (!refcount_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005876 return;
5877
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005878 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005879
Frederic Weisbecker76369132011-05-19 19:55:04 +02005880 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005881}
5882
5883static void perf_mmap_open(struct vm_area_struct *vma)
5884{
5885 struct perf_event *event = vma->vm_file->private_data;
5886
5887 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005888 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005889
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005890 if (vma->vm_pgoff)
5891 atomic_inc(&event->rb->aux_mmap_count);
5892
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005893 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005894 event->pmu->event_mapped(event, vma->vm_mm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005895}
5896
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005897static void perf_pmu_output_stop(struct perf_event *event);
5898
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005899/*
5900 * A buffer can be mmap()ed multiple times; either directly through the same
5901 * event, or through other events by use of perf_event_set_output().
5902 *
5903 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5904 * the buffer here, where we still have a VM context. This means we need
5905 * to detach all events redirecting to us.
5906 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005907static void perf_mmap_close(struct vm_area_struct *vma)
5908{
5909 struct perf_event *event = vma->vm_file->private_data;
5910
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005911 struct perf_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005912 struct user_struct *mmap_user = rb->mmap_user;
5913 int mmap_locked = rb->mmap_locked;
5914 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005915
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005916 if (event->pmu->event_unmapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005917 event->pmu->event_unmapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005918
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005919 /*
5920 * rb->aux_mmap_count will always drop before rb->mmap_count and
5921 * event->mmap_count, so it is ok to use event->mmap_mutex to
5922 * serialize with perf_mmap here.
5923 */
5924 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5925 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005926 /*
5927 * Stop all AUX events that are writing to this buffer,
5928 * so that we can free its AUX pages and corresponding PMU
5929 * data. Note that after rb::aux_mmap_count dropped to zero,
5930 * they won't start any more (see perf_aux_output_begin()).
5931 */
5932 perf_pmu_output_stop(event);
5933
5934 /* now it's safe to free the pages */
Alexander Shishkin36b3db02019-11-15 18:08:18 +02005935 atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm);
5936 atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005937
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005938 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005939 rb_free_aux(rb);
Elena Reshetovaca3bb3d2019-01-28 14:27:28 +02005940 WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005941
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005942 mutex_unlock(&event->mmap_mutex);
5943 }
5944
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005945 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005946
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005947 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005948 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005949
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005950 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005951 mutex_unlock(&event->mmap_mutex);
5952
5953 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005954 if (atomic_read(&rb->mmap_count))
5955 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005956
5957 /*
5958 * No other mmap()s, detach from all other events that might redirect
5959 * into the now unreachable buffer. Somewhat complicated by the
5960 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5961 */
5962again:
5963 rcu_read_lock();
5964 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5965 if (!atomic_long_inc_not_zero(&event->refcount)) {
5966 /*
5967 * This event is en-route to free_event() which will
5968 * detach it and remove it from the list.
5969 */
5970 continue;
5971 }
5972 rcu_read_unlock();
5973
5974 mutex_lock(&event->mmap_mutex);
5975 /*
5976 * Check we didn't race with perf_event_set_output() which can
5977 * swizzle the rb from under us while we were waiting to
5978 * acquire mmap_mutex.
5979 *
5980 * If we find a different rb; ignore this event, a next
5981 * iteration will no longer find it on the list. We have to
5982 * still restart the iteration to make sure we're not now
5983 * iterating the wrong list.
5984 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005985 if (event->rb == rb)
5986 ring_buffer_attach(event, NULL);
5987
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005988 mutex_unlock(&event->mmap_mutex);
5989 put_event(event);
5990
5991 /*
5992 * Restart the iteration; either we're on the wrong list or
5993 * destroyed its integrity by doing a deletion.
5994 */
5995 goto again;
5996 }
5997 rcu_read_unlock();
5998
5999 /*
6000 * It could be there's still a few 0-ref events on the list; they'll
6001 * get cleaned up by free_event() -- they'll also still have their
6002 * ref on the rb and will free it whenever they are done with it.
6003 *
6004 * Aside from that, this buffer is 'fully' detached and unmapped,
6005 * undo the VM accounting.
6006 */
6007
Song Liud44248a2019-09-04 14:46:18 -07006008 atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
6009 &mmap_user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006010 atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006011 free_uid(mmap_user);
6012
Peter Zijlstrab69cf532014-03-14 10:50:33 +01006013out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006014 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006015}
6016
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04006017static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006018 .open = perf_mmap_open,
Ingo Molnarfca0c112018-12-03 10:52:21 +01006019 .close = perf_mmap_close, /* non mergeable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006020 .fault = perf_mmap_fault,
6021 .page_mkwrite = perf_mmap_fault,
6022};
6023
6024static int perf_mmap(struct file *file, struct vm_area_struct *vma)
6025{
6026 struct perf_event *event = file->private_data;
6027 unsigned long user_locked, user_lock_limit;
6028 struct user_struct *user = current_user();
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006029 struct perf_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006030 unsigned long locked, lock_limit;
6031 unsigned long vma_size;
6032 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006033 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006034 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006035
Peter Zijlstrac7920612010-05-18 10:33:24 +02006036 /*
6037 * Don't allow mmap() of inherited per-task counters. This would
6038 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02006039 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02006040 */
6041 if (event->cpu == -1 && event->attr.inherit)
6042 return -EINVAL;
6043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006044 if (!(vma->vm_flags & VM_SHARED))
6045 return -EINVAL;
6046
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006047 ret = security_perf_event_read(event);
6048 if (ret)
6049 return ret;
6050
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006052
6053 if (vma->vm_pgoff == 0) {
6054 nr_pages = (vma_size / PAGE_SIZE) - 1;
6055 } else {
6056 /*
6057 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
6058 * mapped, all subsequent mappings should have the same size
6059 * and offset. Must be above the normal perf buffer.
6060 */
6061 u64 aux_offset, aux_size;
6062
6063 if (!event->rb)
6064 return -EINVAL;
6065
6066 nr_pages = vma_size / PAGE_SIZE;
6067
6068 mutex_lock(&event->mmap_mutex);
6069 ret = -EINVAL;
6070
6071 rb = event->rb;
6072 if (!rb)
6073 goto aux_unlock;
6074
Mark Rutland6aa7de02017-10-23 14:07:29 -07006075 aux_offset = READ_ONCE(rb->user_page->aux_offset);
6076 aux_size = READ_ONCE(rb->user_page->aux_size);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006077
6078 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
6079 goto aux_unlock;
6080
6081 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
6082 goto aux_unlock;
6083
6084 /* already mapped with a different offset */
6085 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
6086 goto aux_unlock;
6087
6088 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
6089 goto aux_unlock;
6090
6091 /* already mapped with a different size */
6092 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
6093 goto aux_unlock;
6094
6095 if (!is_power_of_2(nr_pages))
6096 goto aux_unlock;
6097
6098 if (!atomic_inc_not_zero(&rb->mmap_count))
6099 goto aux_unlock;
6100
6101 if (rb_has_aux(rb)) {
6102 atomic_inc(&rb->aux_mmap_count);
6103 ret = 0;
6104 goto unlock;
6105 }
6106
6107 atomic_set(&rb->aux_mmap_count, 1);
6108 user_extra = nr_pages;
6109
6110 goto accounting;
6111 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006112
6113 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006114 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006115 * can do bitmasks instead of modulo.
6116 */
Kan Liang2ed11312015-03-02 02:14:26 -05006117 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118 return -EINVAL;
6119
6120 if (vma_size != PAGE_SIZE * (1 + nr_pages))
6121 return -EINVAL;
6122
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006123 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006124again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006125 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006126 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006127 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006128 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006129 goto unlock;
6130 }
6131
6132 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
6133 /*
6134 * Raced against perf_mmap_close() through
6135 * perf_event_set_output(). Try again, hope for better
6136 * luck.
6137 */
6138 mutex_unlock(&event->mmap_mutex);
6139 goto again;
6140 }
6141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006142 goto unlock;
6143 }
6144
6145 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006146
6147accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006148 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
6149
6150 /*
6151 * Increase the limit linearly with more CPUs:
6152 */
6153 user_lock_limit *= num_online_cpus();
6154
Song Liu00346152020-01-23 10:11:46 -08006155 user_locked = atomic_long_read(&user->locked_vm);
6156
6157 /*
6158 * sysctl_perf_event_mlock may have changed, so that
6159 * user->locked_vm > user_lock_limit
6160 */
6161 if (user_locked > user_lock_limit)
6162 user_locked = user_lock_limit;
6163 user_locked += user_extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006164
Alexander Shishkinc4b75472019-11-20 19:06:40 +02006165 if (user_locked > user_lock_limit) {
Song Liud44248a2019-09-04 14:46:18 -07006166 /*
6167 * charge locked_vm until it hits user_lock_limit;
6168 * charge the rest from pinned_vm
6169 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006170 extra = user_locked - user_lock_limit;
Song Liud44248a2019-09-04 14:46:18 -07006171 user_extra -= extra;
6172 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006173
Jiri Slaby78d7d402010-03-05 13:42:54 -08006174 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006175 lock_limit >>= PAGE_SHIFT;
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006176 locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006177
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006178 if ((locked > lock_limit) && perf_is_paranoid() &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006179 !capable(CAP_IPC_LOCK)) {
6180 ret = -EPERM;
6181 goto unlock;
6182 }
6183
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006184 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02006185
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006186 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02006187 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006188
Frederic Weisbecker76369132011-05-19 19:55:04 +02006189 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006190 rb = rb_alloc(nr_pages,
6191 event->attr.watermark ? event->attr.wakeup_watermark : 0,
6192 event->cpu, flags);
6193
6194 if (!rb) {
6195 ret = -ENOMEM;
6196 goto unlock;
6197 }
6198
6199 atomic_set(&rb->mmap_count, 1);
6200 rb->mmap_user = get_current_user();
6201 rb->mmap_locked = extra;
6202
6203 ring_buffer_attach(event, rb);
6204
6205 perf_event_init_userpage(event);
6206 perf_event_update_userpage(event);
6207 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02006208 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
6209 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006210 if (!ret)
6211 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006212 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02006213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006214unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006215 if (!ret) {
6216 atomic_long_add(user_extra, &user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006217 atomic64_add(extra, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006218
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006219 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006220 } else if (rb) {
6221 atomic_dec(&rb->mmap_count);
6222 }
6223aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006224 mutex_unlock(&event->mmap_mutex);
6225
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006226 /*
6227 * Since pinned accounting is per vm we cannot allow fork() to copy our
6228 * vma.
6229 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02006230 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231 vma->vm_ops = &perf_mmap_vmops;
6232
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07006233 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02006234 event->pmu->event_mapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07006235
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006236 return ret;
6237}
6238
6239static int perf_fasync(int fd, struct file *filp, int on)
6240{
Al Viro496ad9a2013-01-23 17:07:38 -05006241 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006242 struct perf_event *event = filp->private_data;
6243 int retval;
6244
Al Viro59551022016-01-22 15:40:57 -05006245 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006246 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05006247 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006248
6249 if (retval < 0)
6250 return retval;
6251
6252 return 0;
6253}
6254
6255static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01006256 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006257 .release = perf_release,
6258 .read = perf_read,
6259 .poll = perf_poll,
6260 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01006261 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006262 .mmap = perf_mmap,
6263 .fasync = perf_fasync,
6264};
6265
6266/*
6267 * Perf event wakeup
6268 *
6269 * If there's data, ensure we set the poll() state and publish everything
6270 * to user-space before waking everybody up.
6271 */
6272
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006273static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
6274{
6275 /* only the parent has fasync state */
6276 if (event->parent)
6277 event = event->parent;
6278 return &event->fasync;
6279}
6280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006281void perf_event_wakeup(struct perf_event *event)
6282{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006283 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006284
6285 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006286 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006287 event->pending_kill = 0;
6288 }
6289}
6290
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006291static void perf_pending_event_disable(struct perf_event *event)
6292{
6293 int cpu = READ_ONCE(event->pending_disable);
6294
6295 if (cpu < 0)
6296 return;
6297
6298 if (cpu == smp_processor_id()) {
6299 WRITE_ONCE(event->pending_disable, -1);
6300 perf_event_disable_local(event);
6301 return;
6302 }
6303
6304 /*
6305 * CPU-A CPU-B
6306 *
6307 * perf_event_disable_inatomic()
6308 * @pending_disable = CPU-A;
6309 * irq_work_queue();
6310 *
6311 * sched-out
6312 * @pending_disable = -1;
6313 *
6314 * sched-in
6315 * perf_event_disable_inatomic()
6316 * @pending_disable = CPU-B;
6317 * irq_work_queue(); // FAILS
6318 *
6319 * irq_work_run()
6320 * perf_pending_event()
6321 *
6322 * But the event runs on CPU-B and wants disabling there.
6323 */
6324 irq_work_queue_on(&event->pending, cpu);
6325}
6326
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006327static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006328{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006329 struct perf_event *event = container_of(entry, struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01006330 int rctx;
6331
6332 rctx = perf_swevent_get_recursion_context();
6333 /*
6334 * If we 'fail' here, that's OK, it means recursion is already disabled
6335 * and we won't recurse 'further'.
6336 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006337
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006338 perf_pending_event_disable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006339
6340 if (event->pending_wakeup) {
6341 event->pending_wakeup = 0;
6342 perf_event_wakeup(event);
6343 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01006344
6345 if (rctx >= 0)
6346 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006347}
6348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006350 * We assume there is only KVM supporting the callbacks.
6351 * Later on, we might change it to a list if there is
6352 * another virtualization implementation supporting the callbacks.
6353 */
6354struct perf_guest_info_callbacks *perf_guest_cbs;
6355
6356int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6357{
6358 perf_guest_cbs = cbs;
6359 return 0;
6360}
6361EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
6362
6363int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6364{
6365 perf_guest_cbs = NULL;
6366 return 0;
6367}
6368EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
6369
Jiri Olsa40189942012-08-07 15:20:37 +02006370static void
6371perf_output_sample_regs(struct perf_output_handle *handle,
6372 struct pt_regs *regs, u64 mask)
6373{
6374 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306375 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02006376
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306377 bitmap_from_u64(_mask, mask);
6378 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02006379 u64 val;
6380
6381 val = perf_reg_value(regs, bit);
6382 perf_output_put(handle, val);
6383 }
6384}
6385
Stephane Eranian60e23642014-09-24 13:48:37 +02006386static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006387 struct pt_regs *regs,
6388 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02006389{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006390 if (user_mode(regs)) {
6391 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02006392 regs_user->regs = regs;
Peter Zijlstra085ebfe2019-05-29 14:37:24 +02006393 } else if (!(current->flags & PF_KTHREAD)) {
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006394 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006395 } else {
6396 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
6397 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02006398 }
6399}
6400
Stephane Eranian60e23642014-09-24 13:48:37 +02006401static void perf_sample_regs_intr(struct perf_regs *regs_intr,
6402 struct pt_regs *regs)
6403{
6404 regs_intr->regs = regs;
6405 regs_intr->abi = perf_reg_abi(current);
6406}
6407
6408
Jiri Olsac5ebced2012-08-07 15:20:40 +02006409/*
6410 * Get remaining task size from user stack pointer.
6411 *
6412 * It'd be better to take stack vma map and limit this more
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03006413 * precisely, but there's no way to get it safely under interrupt,
Jiri Olsac5ebced2012-08-07 15:20:40 +02006414 * so using TASK_SIZE as limit.
6415 */
6416static u64 perf_ustack_task_size(struct pt_regs *regs)
6417{
6418 unsigned long addr = perf_user_stack_pointer(regs);
6419
6420 if (!addr || addr >= TASK_SIZE)
6421 return 0;
6422
6423 return TASK_SIZE - addr;
6424}
6425
6426static u16
6427perf_sample_ustack_size(u16 stack_size, u16 header_size,
6428 struct pt_regs *regs)
6429{
6430 u64 task_size;
6431
6432 /* No regs, no stack pointer, no dump. */
6433 if (!regs)
6434 return 0;
6435
6436 /*
6437 * Check if we fit in with the requested stack size into the:
6438 * - TASK_SIZE
6439 * If we don't, we limit the size to the TASK_SIZE.
6440 *
6441 * - remaining sample size
6442 * If we don't, we customize the stack size to
6443 * fit in to the remaining sample size.
6444 */
6445
6446 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
6447 stack_size = min(stack_size, (u16) task_size);
6448
6449 /* Current header size plus static size and dynamic size. */
6450 header_size += 2 * sizeof(u64);
6451
6452 /* Do we fit in with the current stack dump size? */
6453 if ((u16) (header_size + stack_size) < header_size) {
6454 /*
6455 * If we overflow the maximum size for the sample,
6456 * we customize the stack dump size to fit in.
6457 */
6458 stack_size = USHRT_MAX - header_size - sizeof(u64);
6459 stack_size = round_up(stack_size, sizeof(u64));
6460 }
6461
6462 return stack_size;
6463}
6464
6465static void
6466perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
6467 struct pt_regs *regs)
6468{
6469 /* Case of a kernel thread, nothing to dump */
6470 if (!regs) {
6471 u64 size = 0;
6472 perf_output_put(handle, size);
6473 } else {
6474 unsigned long sp;
6475 unsigned int rem;
6476 u64 dyn_size;
Yabin Cui02e18442018-08-23 15:59:35 -07006477 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02006478
6479 /*
6480 * We dump:
6481 * static size
6482 * - the size requested by user or the best one we can fit
6483 * in to the sample max size
6484 * data
6485 * - user stack dump data
6486 * dynamic size
6487 * - the actual dumped size
6488 */
6489
6490 /* Static size. */
6491 perf_output_put(handle, dump_size);
6492
6493 /* Data. */
6494 sp = perf_user_stack_pointer(regs);
Christoph Hellwig3d13f312020-08-11 18:33:47 -07006495 fs = force_uaccess_begin();
Jiri Olsac5ebced2012-08-07 15:20:40 +02006496 rem = __output_copy_user(handle, (void *) sp, dump_size);
Christoph Hellwig3d13f312020-08-11 18:33:47 -07006497 force_uaccess_end(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006498 dyn_size = dump_size - rem;
6499
6500 perf_output_skip(handle, rem);
6501
6502 /* Dynamic size. */
6503 perf_output_put(handle, dyn_size);
6504 }
6505}
6506
Alexander Shishkina4faf002019-10-25 17:08:33 +03006507static unsigned long perf_prepare_sample_aux(struct perf_event *event,
6508 struct perf_sample_data *data,
6509 size_t size)
6510{
6511 struct perf_event *sampler = event->aux_event;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006512 struct perf_buffer *rb;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006513
6514 data->aux_size = 0;
6515
6516 if (!sampler)
6517 goto out;
6518
6519 if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE))
6520 goto out;
6521
6522 if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id()))
6523 goto out;
6524
6525 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6526 if (!rb)
6527 goto out;
6528
6529 /*
6530 * If this is an NMI hit inside sampling code, don't take
6531 * the sample. See also perf_aux_sample_output().
6532 */
6533 if (READ_ONCE(rb->aux_in_sampling)) {
6534 data->aux_size = 0;
6535 } else {
6536 size = min_t(size_t, size, perf_aux_size(rb));
6537 data->aux_size = ALIGN(size, sizeof(u64));
6538 }
6539 ring_buffer_put(rb);
6540
6541out:
6542 return data->aux_size;
6543}
6544
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006545long perf_pmu_snapshot_aux(struct perf_buffer *rb,
Alexander Shishkina4faf002019-10-25 17:08:33 +03006546 struct perf_event *event,
6547 struct perf_output_handle *handle,
6548 unsigned long size)
6549{
6550 unsigned long flags;
6551 long ret;
6552
6553 /*
6554 * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler
6555 * paths. If we start calling them in NMI context, they may race with
6556 * the IRQ ones, that is, for example, re-starting an event that's just
6557 * been stopped, which is why we're using a separate callback that
6558 * doesn't change the event state.
6559 *
6560 * IRQs need to be disabled to prevent IPIs from racing with us.
6561 */
6562 local_irq_save(flags);
6563 /*
6564 * Guard against NMI hits inside the critical section;
6565 * see also perf_prepare_sample_aux().
6566 */
6567 WRITE_ONCE(rb->aux_in_sampling, 1);
6568 barrier();
6569
6570 ret = event->pmu->snapshot_aux(event, handle, size);
6571
6572 barrier();
6573 WRITE_ONCE(rb->aux_in_sampling, 0);
6574 local_irq_restore(flags);
6575
6576 return ret;
6577}
6578
6579static void perf_aux_sample_output(struct perf_event *event,
6580 struct perf_output_handle *handle,
6581 struct perf_sample_data *data)
6582{
6583 struct perf_event *sampler = event->aux_event;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006584 struct perf_buffer *rb;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006585 unsigned long pad;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006586 long size;
6587
6588 if (WARN_ON_ONCE(!sampler || !data->aux_size))
6589 return;
6590
6591 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6592 if (!rb)
6593 return;
6594
6595 size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size);
6596
6597 /*
6598 * An error here means that perf_output_copy() failed (returned a
6599 * non-zero surplus that it didn't copy), which in its current
6600 * enlightened implementation is not possible. If that changes, we'd
6601 * like to know.
6602 */
6603 if (WARN_ON_ONCE(size < 0))
6604 goto out_put;
6605
6606 /*
6607 * The pad comes from ALIGN()ing data->aux_size up to u64 in
6608 * perf_prepare_sample_aux(), so should not be more than that.
6609 */
6610 pad = data->aux_size - size;
6611 if (WARN_ON_ONCE(pad >= sizeof(u64)))
6612 pad = 8;
6613
6614 if (pad) {
6615 u64 zero = 0;
6616 perf_output_copy(handle, &zero, pad);
6617 }
6618
6619out_put:
6620 ring_buffer_put(rb);
6621}
6622
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006623static void __perf_event_header__init_id(struct perf_event_header *header,
6624 struct perf_sample_data *data,
6625 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006626{
6627 u64 sample_type = event->attr.sample_type;
6628
6629 data->type = sample_type;
6630 header->size += event->id_header_size;
6631
6632 if (sample_type & PERF_SAMPLE_TID) {
6633 /* namespace issues */
6634 data->tid_entry.pid = perf_event_pid(event, current);
6635 data->tid_entry.tid = perf_event_tid(event, current);
6636 }
6637
6638 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01006639 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006640
Adrian Hunterff3d5272013-08-27 11:23:07 +03006641 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006642 data->id = primary_event_id(event);
6643
6644 if (sample_type & PERF_SAMPLE_STREAM_ID)
6645 data->stream_id = event->id;
6646
6647 if (sample_type & PERF_SAMPLE_CPU) {
6648 data->cpu_entry.cpu = raw_smp_processor_id();
6649 data->cpu_entry.reserved = 0;
6650 }
6651}
6652
Frederic Weisbecker76369132011-05-19 19:55:04 +02006653void perf_event_header__init_id(struct perf_event_header *header,
6654 struct perf_sample_data *data,
6655 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006656{
6657 if (event->attr.sample_id_all)
6658 __perf_event_header__init_id(header, data, event);
6659}
6660
6661static void __perf_event__output_id_sample(struct perf_output_handle *handle,
6662 struct perf_sample_data *data)
6663{
6664 u64 sample_type = data->type;
6665
6666 if (sample_type & PERF_SAMPLE_TID)
6667 perf_output_put(handle, data->tid_entry);
6668
6669 if (sample_type & PERF_SAMPLE_TIME)
6670 perf_output_put(handle, data->time);
6671
6672 if (sample_type & PERF_SAMPLE_ID)
6673 perf_output_put(handle, data->id);
6674
6675 if (sample_type & PERF_SAMPLE_STREAM_ID)
6676 perf_output_put(handle, data->stream_id);
6677
6678 if (sample_type & PERF_SAMPLE_CPU)
6679 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03006680
6681 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6682 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006683}
6684
Frederic Weisbecker76369132011-05-19 19:55:04 +02006685void perf_event__output_id_sample(struct perf_event *event,
6686 struct perf_output_handle *handle,
6687 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006688{
6689 if (event->attr.sample_id_all)
6690 __perf_event__output_id_sample(handle, sample);
6691}
6692
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006693static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006694 struct perf_event *event,
6695 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006696{
6697 u64 read_format = event->attr.read_format;
6698 u64 values[4];
6699 int n = 0;
6700
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006701 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006702 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006703 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006704 atomic64_read(&event->child_total_time_enabled);
6705 }
6706 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006707 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006708 atomic64_read(&event->child_total_time_running);
6709 }
6710 if (read_format & PERF_FORMAT_ID)
6711 values[n++] = primary_event_id(event);
6712
Frederic Weisbecker76369132011-05-19 19:55:04 +02006713 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006714}
6715
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006716static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006717 struct perf_event *event,
6718 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006719{
6720 struct perf_event *leader = event->group_leader, *sub;
6721 u64 read_format = event->attr.read_format;
6722 u64 values[5];
6723 int n = 0;
6724
6725 values[n++] = 1 + leader->nr_siblings;
6726
6727 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02006728 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006729
6730 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02006731 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006732
Peter Zijlstra9e5b1272018-03-09 12:52:04 +01006733 if ((leader != event) &&
6734 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006735 leader->pmu->read(leader);
6736
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006737 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006738 if (read_format & PERF_FORMAT_ID)
6739 values[n++] = primary_event_id(leader);
6740
Frederic Weisbecker76369132011-05-19 19:55:04 +02006741 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006742
Peter Zijlstraedb39592018-03-15 17:36:56 +01006743 for_each_sibling_event(sub, leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006744 n = 0;
6745
Jiri Olsa6f5ab002012-10-15 20:13:45 +02006746 if ((sub != event) &&
6747 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006748 sub->pmu->read(sub);
6749
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006750 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006751 if (read_format & PERF_FORMAT_ID)
6752 values[n++] = primary_event_id(sub);
6753
Frederic Weisbecker76369132011-05-19 19:55:04 +02006754 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006755 }
6756}
6757
Stephane Eranianeed01522010-10-26 16:08:01 +02006758#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
6759 PERF_FORMAT_TOTAL_TIME_RUNNING)
6760
Peter Zijlstraba5213a2017-05-30 11:45:12 +02006761/*
6762 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
6763 *
6764 * The problem is that its both hard and excessively expensive to iterate the
6765 * child list, not to mention that its impossible to IPI the children running
6766 * on another CPU, from interrupt/NMI context.
6767 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006768static void perf_output_read(struct perf_output_handle *handle,
6769 struct perf_event *event)
6770{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006771 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02006772 u64 read_format = event->attr.read_format;
6773
6774 /*
6775 * compute total_time_enabled, total_time_running
6776 * based on snapshot values taken when the event
6777 * was last scheduled in.
6778 *
6779 * we cannot simply called update_context_time()
6780 * because of locking issue as we are called in
6781 * NMI context
6782 */
Eric B Munsonc4794292011-06-23 16:34:38 -04006783 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006784 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02006785
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006786 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02006787 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006788 else
Stephane Eranianeed01522010-10-26 16:08:01 +02006789 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006790}
6791
Kan Liangbbfd5e42020-01-27 08:53:54 -08006792static inline bool perf_sample_save_hw_index(struct perf_event *event)
6793{
6794 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
6795}
6796
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006797void perf_output_sample(struct perf_output_handle *handle,
6798 struct perf_event_header *header,
6799 struct perf_sample_data *data,
6800 struct perf_event *event)
6801{
6802 u64 sample_type = data->type;
6803
6804 perf_output_put(handle, *header);
6805
Adrian Hunterff3d5272013-08-27 11:23:07 +03006806 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6807 perf_output_put(handle, data->id);
6808
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006809 if (sample_type & PERF_SAMPLE_IP)
6810 perf_output_put(handle, data->ip);
6811
6812 if (sample_type & PERF_SAMPLE_TID)
6813 perf_output_put(handle, data->tid_entry);
6814
6815 if (sample_type & PERF_SAMPLE_TIME)
6816 perf_output_put(handle, data->time);
6817
6818 if (sample_type & PERF_SAMPLE_ADDR)
6819 perf_output_put(handle, data->addr);
6820
6821 if (sample_type & PERF_SAMPLE_ID)
6822 perf_output_put(handle, data->id);
6823
6824 if (sample_type & PERF_SAMPLE_STREAM_ID)
6825 perf_output_put(handle, data->stream_id);
6826
6827 if (sample_type & PERF_SAMPLE_CPU)
6828 perf_output_put(handle, data->cpu_entry);
6829
6830 if (sample_type & PERF_SAMPLE_PERIOD)
6831 perf_output_put(handle, data->period);
6832
6833 if (sample_type & PERF_SAMPLE_READ)
6834 perf_output_read(handle, event);
6835
6836 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Jiri Olsa99e818c2018-01-07 17:03:50 +01006837 int size = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006838
Jiri Olsa99e818c2018-01-07 17:03:50 +01006839 size += data->callchain->nr;
6840 size *= sizeof(u64);
6841 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006842 }
6843
6844 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006845 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07006846
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006847 if (raw) {
6848 struct perf_raw_frag *frag = &raw->frag;
6849
6850 perf_output_put(handle, raw->size);
6851 do {
6852 if (frag->copy) {
6853 __output_custom(handle, frag->copy,
6854 frag->data, frag->size);
6855 } else {
6856 __output_copy(handle, frag->data,
6857 frag->size);
6858 }
6859 if (perf_raw_frag_last(frag))
6860 break;
6861 frag = frag->next;
6862 } while (1);
6863 if (frag->pad)
6864 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006865 } else {
6866 struct {
6867 u32 size;
6868 u32 data;
6869 } raw = {
6870 .size = sizeof(u32),
6871 .data = 0,
6872 };
6873 perf_output_put(handle, raw);
6874 }
6875 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006876
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006877 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6878 if (data->br_stack) {
6879 size_t size;
6880
6881 size = data->br_stack->nr
6882 * sizeof(struct perf_branch_entry);
6883
6884 perf_output_put(handle, data->br_stack->nr);
Kan Liangbbfd5e42020-01-27 08:53:54 -08006885 if (perf_sample_save_hw_index(event))
6886 perf_output_put(handle, data->br_stack->hw_idx);
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006887 perf_output_copy(handle, data->br_stack->entries, size);
6888 } else {
6889 /*
6890 * we always store at least the value of nr
6891 */
6892 u64 nr = 0;
6893 perf_output_put(handle, nr);
6894 }
6895 }
Jiri Olsa40189942012-08-07 15:20:37 +02006896
6897 if (sample_type & PERF_SAMPLE_REGS_USER) {
6898 u64 abi = data->regs_user.abi;
6899
6900 /*
6901 * If there are no regs to dump, notice it through
6902 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6903 */
6904 perf_output_put(handle, abi);
6905
6906 if (abi) {
6907 u64 mask = event->attr.sample_regs_user;
6908 perf_output_sample_regs(handle,
6909 data->regs_user.regs,
6910 mask);
6911 }
6912 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006913
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006914 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02006915 perf_output_sample_ustack(handle,
6916 data->stack_user_size,
6917 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006918 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01006919
6920 if (sample_type & PERF_SAMPLE_WEIGHT)
6921 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01006922
6923 if (sample_type & PERF_SAMPLE_DATA_SRC)
6924 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006925
Andi Kleenfdfbbd02013-09-20 07:40:39 -07006926 if (sample_type & PERF_SAMPLE_TRANSACTION)
6927 perf_output_put(handle, data->txn);
6928
Stephane Eranian60e23642014-09-24 13:48:37 +02006929 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6930 u64 abi = data->regs_intr.abi;
6931 /*
6932 * If there are no regs to dump, notice it through
6933 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6934 */
6935 perf_output_put(handle, abi);
6936
6937 if (abi) {
6938 u64 mask = event->attr.sample_regs_intr;
6939
6940 perf_output_sample_regs(handle,
6941 data->regs_intr.regs,
6942 mask);
6943 }
6944 }
6945
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006946 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
6947 perf_output_put(handle, data->phys_addr);
6948
Namhyung Kim6546b192020-03-25 21:45:29 +09006949 if (sample_type & PERF_SAMPLE_CGROUP)
6950 perf_output_put(handle, data->cgroup);
6951
Alexander Shishkina4faf002019-10-25 17:08:33 +03006952 if (sample_type & PERF_SAMPLE_AUX) {
6953 perf_output_put(handle, data->aux_size);
6954
6955 if (data->aux_size)
6956 perf_aux_sample_output(event, handle, data);
6957 }
6958
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006959 if (!event->attr.watermark) {
6960 int wakeup_events = event->attr.wakeup_events;
6961
6962 if (wakeup_events) {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006963 struct perf_buffer *rb = handle->rb;
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006964 int events = local_inc_return(&rb->events);
6965
6966 if (events >= wakeup_events) {
6967 local_sub(wakeup_events, &rb->events);
6968 local_inc(&rb->wakeup);
6969 }
6970 }
6971 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006972}
6973
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006974static u64 perf_virt_to_phys(u64 virt)
6975{
6976 u64 phys_addr = 0;
6977 struct page *p = NULL;
6978
6979 if (!virt)
6980 return 0;
6981
6982 if (virt >= TASK_SIZE) {
6983 /* If it's vmalloc()d memory, leave phys_addr as 0 */
6984 if (virt_addr_valid((void *)(uintptr_t)virt) &&
6985 !(virt >= VMALLOC_START && virt < VMALLOC_END))
6986 phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt);
6987 } else {
6988 /*
6989 * Walking the pages tables for user address.
6990 * Interrupts are disabled, so it prevents any tear down
6991 * of the page tables.
Souptick Joarderdadbb612020-06-07 21:40:55 -07006992 * Try IRQ-safe get_user_page_fast_only first.
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006993 * If failed, leave phys_addr as 0.
6994 */
Jiri Olsad3296fb2020-04-07 16:14:27 +02006995 if (current->mm != NULL) {
6996 pagefault_disable();
Souptick Joarderdadbb612020-06-07 21:40:55 -07006997 if (get_user_page_fast_only(virt, 0, &p))
Jiri Olsad3296fb2020-04-07 16:14:27 +02006998 phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
6999 pagefault_enable();
7000 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04007001
7002 if (p)
7003 put_page(p);
7004 }
7005
7006 return phys_addr;
7007}
7008
Jiri Olsa99e818c2018-01-07 17:03:50 +01007009static struct perf_callchain_entry __empty_callchain = { .nr = 0, };
7010
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02007011struct perf_callchain_entry *
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01007012perf_callchain(struct perf_event *event, struct pt_regs *regs)
7013{
7014 bool kernel = !event->attr.exclude_callchain_kernel;
7015 bool user = !event->attr.exclude_callchain_user;
7016 /* Disallow cross-task user callchains. */
7017 bool crosstask = event->ctx->task && event->ctx->task != current;
7018 const u32 max_stack = event->attr.sample_max_stack;
Jiri Olsa99e818c2018-01-07 17:03:50 +01007019 struct perf_callchain_entry *callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01007020
7021 if (!kernel && !user)
Jiri Olsa99e818c2018-01-07 17:03:50 +01007022 return &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01007023
Jiri Olsa99e818c2018-01-07 17:03:50 +01007024 callchain = get_perf_callchain(regs, 0, kernel, user,
7025 max_stack, crosstask, true);
7026 return callchain ?: &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01007027}
7028
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007029void perf_prepare_sample(struct perf_event_header *header,
7030 struct perf_sample_data *data,
7031 struct perf_event *event,
7032 struct pt_regs *regs)
7033{
7034 u64 sample_type = event->attr.sample_type;
7035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007036 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007037 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007038
7039 header->misc = 0;
7040 header->misc |= perf_misc_flags(regs);
7041
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007042 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007043
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007044 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045 data->ip = perf_instruction_pointer(regs);
7046
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007047 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
7048 int size = 1;
7049
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02007050 if (!(sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
7051 data->callchain = perf_callchain(event, regs);
7052
Jiri Olsa99e818c2018-01-07 17:03:50 +01007053 size += data->callchain->nr;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007054
7055 header->size += size * sizeof(u64);
7056 }
7057
7058 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007059 struct perf_raw_record *raw = data->raw;
7060 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007061
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007062 if (raw) {
7063 struct perf_raw_frag *frag = &raw->frag;
7064 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007065
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007066 do {
7067 sum += frag->size;
7068 if (perf_raw_frag_last(frag))
7069 break;
7070 frag = frag->next;
7071 } while (1);
7072
7073 size = round_up(sum + sizeof(u32), sizeof(u64));
7074 raw->size = size - sizeof(u32);
7075 frag->pad = raw->size - sum;
7076 } else {
7077 size = sizeof(u64);
7078 }
7079
7080 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007081 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007082
7083 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
7084 int size = sizeof(u64); /* nr */
7085 if (data->br_stack) {
Kan Liangbbfd5e42020-01-27 08:53:54 -08007086 if (perf_sample_save_hw_index(event))
7087 size += sizeof(u64);
7088
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007089 size += data->br_stack->nr
7090 * sizeof(struct perf_branch_entry);
7091 }
7092 header->size += size;
7093 }
Jiri Olsa40189942012-08-07 15:20:37 +02007094
Peter Zijlstra25657112014-09-24 13:48:42 +02007095 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08007096 perf_sample_regs_user(&data->regs_user, regs,
7097 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02007098
Jiri Olsa40189942012-08-07 15:20:37 +02007099 if (sample_type & PERF_SAMPLE_REGS_USER) {
7100 /* regs dump ABI info */
7101 int size = sizeof(u64);
7102
Jiri Olsa40189942012-08-07 15:20:37 +02007103 if (data->regs_user.regs) {
7104 u64 mask = event->attr.sample_regs_user;
7105 size += hweight64(mask) * sizeof(u64);
7106 }
7107
7108 header->size += size;
7109 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02007110
7111 if (sample_type & PERF_SAMPLE_STACK_USER) {
7112 /*
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03007113 * Either we need PERF_SAMPLE_STACK_USER bit to be always
Jiri Olsac5ebced2012-08-07 15:20:40 +02007114 * processed as the last one or have additional check added
7115 * in case new sample type is added, because we could eat
7116 * up the rest of the sample size.
7117 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02007118 u16 stack_size = event->attr.sample_stack_user;
7119 u16 size = sizeof(u64);
7120
Jiri Olsac5ebced2012-08-07 15:20:40 +02007121 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02007122 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007123
7124 /*
7125 * If there is something to dump, add space for the dump
7126 * itself and for the field that tells the dynamic size,
7127 * which is how many have been actually dumped.
7128 */
7129 if (stack_size)
7130 size += sizeof(u64) + stack_size;
7131
7132 data->stack_user_size = stack_size;
7133 header->size += size;
7134 }
Stephane Eranian60e23642014-09-24 13:48:37 +02007135
7136 if (sample_type & PERF_SAMPLE_REGS_INTR) {
7137 /* regs dump ABI info */
7138 int size = sizeof(u64);
7139
7140 perf_sample_regs_intr(&data->regs_intr, regs);
7141
7142 if (data->regs_intr.regs) {
7143 u64 mask = event->attr.sample_regs_intr;
7144
7145 size += hweight64(mask) * sizeof(u64);
7146 }
7147
7148 header->size += size;
7149 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04007150
7151 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
7152 data->phys_addr = perf_virt_to_phys(data->addr);
Alexander Shishkina4faf002019-10-25 17:08:33 +03007153
Namhyung Kim6546b192020-03-25 21:45:29 +09007154#ifdef CONFIG_CGROUP_PERF
7155 if (sample_type & PERF_SAMPLE_CGROUP) {
7156 struct cgroup *cgrp;
7157
7158 /* protected by RCU */
7159 cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup;
7160 data->cgroup = cgroup_id(cgrp);
7161 }
7162#endif
7163
Alexander Shishkina4faf002019-10-25 17:08:33 +03007164 if (sample_type & PERF_SAMPLE_AUX) {
7165 u64 size;
7166
7167 header->size += sizeof(u64); /* size */
7168
7169 /*
7170 * Given the 16bit nature of header::size, an AUX sample can
7171 * easily overflow it, what with all the preceding sample bits.
7172 * Make sure this doesn't happen by using up to U16_MAX bytes
7173 * per sample in total (rounded down to 8 byte boundary).
7174 */
7175 size = min_t(size_t, U16_MAX - header->size,
7176 event->attr.aux_sample_size);
7177 size = rounddown(size, 8);
7178 size = perf_prepare_sample_aux(event, data, size);
7179
7180 WARN_ON_ONCE(size + header->size > U16_MAX);
7181 header->size += size;
7182 }
7183 /*
7184 * If you're adding more sample types here, you likely need to do
7185 * something about the overflowing header::size, like repurpose the
7186 * lowest 3 bits of size, which should be always zero at the moment.
7187 * This raises a more important question, do we really need 512k sized
7188 * samples and why, so good argumentation is in order for whatever you
7189 * do here next.
7190 */
7191 WARN_ON_ONCE(header->size & 7);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007192}
7193
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007194static __always_inline int
Wang Nan9ecda412016-04-05 14:11:18 +00007195__perf_event_output(struct perf_event *event,
7196 struct perf_sample_data *data,
7197 struct pt_regs *regs,
7198 int (*output_begin)(struct perf_output_handle *,
7199 struct perf_event *,
7200 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007201{
7202 struct perf_output_handle handle;
7203 struct perf_event_header header;
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007204 int err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007205
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007206 /* protect the callchain buffers */
7207 rcu_read_lock();
7208
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007209 perf_prepare_sample(&header, data, event, regs);
7210
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007211 err = output_begin(&handle, event, header.size);
7212 if (err)
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007213 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007214
7215 perf_output_sample(&handle, &header, data, event);
7216
7217 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007218
7219exit:
7220 rcu_read_unlock();
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007221 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007222}
7223
Wang Nan9ecda412016-04-05 14:11:18 +00007224void
7225perf_event_output_forward(struct perf_event *event,
7226 struct perf_sample_data *data,
7227 struct pt_regs *regs)
7228{
7229 __perf_event_output(event, data, regs, perf_output_begin_forward);
7230}
7231
7232void
7233perf_event_output_backward(struct perf_event *event,
7234 struct perf_sample_data *data,
7235 struct pt_regs *regs)
7236{
7237 __perf_event_output(event, data, regs, perf_output_begin_backward);
7238}
7239
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007240int
Wang Nan9ecda412016-04-05 14:11:18 +00007241perf_event_output(struct perf_event *event,
7242 struct perf_sample_data *data,
7243 struct pt_regs *regs)
7244{
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007245 return __perf_event_output(event, data, regs, perf_output_begin);
Wang Nan9ecda412016-04-05 14:11:18 +00007246}
7247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007248/*
7249 * read event_id
7250 */
7251
7252struct perf_read_event {
7253 struct perf_event_header header;
7254
7255 u32 pid;
7256 u32 tid;
7257};
7258
7259static void
7260perf_event_read_event(struct perf_event *event,
7261 struct task_struct *task)
7262{
7263 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007264 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007265 struct perf_read_event read_event = {
7266 .header = {
7267 .type = PERF_RECORD_READ,
7268 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007269 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007270 },
7271 .pid = perf_event_pid(event, task),
7272 .tid = perf_event_tid(event, task),
7273 };
7274 int ret;
7275
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007276 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007277 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007278 if (ret)
7279 return;
7280
7281 perf_output_put(&handle, read_event);
7282 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007283 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007284
7285 perf_output_end(&handle);
7286}
7287
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007288typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007289
7290static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007291perf_iterate_ctx(struct perf_event_context *ctx,
7292 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007293 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02007294{
7295 struct perf_event *event;
7296
7297 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007298 if (!all) {
7299 if (event->state < PERF_EVENT_STATE_INACTIVE)
7300 continue;
7301 if (!event_filter_match(event))
7302 continue;
7303 }
7304
Jiri Olsa67516842013-07-09 18:56:31 +02007305 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007306 }
7307}
7308
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007309static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07007310{
7311 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
7312 struct perf_event *event;
7313
7314 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02007315 /*
7316 * Skip events that are not fully formed yet; ensure that
7317 * if we observe event->ctx, both event and ctx will be
7318 * complete enough. See perf_install_in_context().
7319 */
7320 if (!smp_load_acquire(&event->ctx))
7321 continue;
7322
Kan Liangf2fb6be2016-03-23 11:24:37 -07007323 if (event->state < PERF_EVENT_STATE_INACTIVE)
7324 continue;
7325 if (!event_filter_match(event))
7326 continue;
7327 output(event, data);
7328 }
7329}
7330
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007331/*
7332 * Iterate all events that need to receive side-band events.
7333 *
7334 * For new callers; ensure that account_pmu_sb_event() includes
7335 * your event, otherwise it might not get delivered.
7336 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007337static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007338perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007339 struct perf_event_context *task_ctx)
7340{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007341 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02007342 int ctxn;
7343
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007344 rcu_read_lock();
7345 preempt_disable();
7346
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007347 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007348 * If we have task_ctx != NULL we only notify the task context itself.
7349 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007350 * context.
7351 */
7352 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007353 perf_iterate_ctx(task_ctx, output, data, false);
7354 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007355 }
7356
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007357 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07007358
7359 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02007360 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
7361 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007362 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007363 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007364done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07007365 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02007366 rcu_read_unlock();
7367}
7368
Alexander Shishkin375637b2016-04-27 18:44:46 +03007369/*
7370 * Clear all file-based filters at exec, they'll have to be
7371 * re-instated when/if these objects are mmapped again.
7372 */
7373static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
7374{
7375 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7376 struct perf_addr_filter *filter;
7377 unsigned int restart = 0, count = 0;
7378 unsigned long flags;
7379
7380 if (!has_addr_filter(event))
7381 return;
7382
7383 raw_spin_lock_irqsave(&ifh->lock, flags);
7384 list_for_each_entry(filter, &ifh->list, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07007385 if (filter->path.dentry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007386 event->addr_filter_ranges[count].start = 0;
7387 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007388 restart++;
7389 }
7390
7391 count++;
7392 }
7393
7394 if (restart)
7395 event->addr_filters_gen++;
7396 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7397
7398 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03007399 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007400}
7401
7402void perf_event_exec(void)
7403{
7404 struct perf_event_context *ctx;
7405 int ctxn;
7406
7407 rcu_read_lock();
7408 for_each_task_context_nr(ctxn) {
7409 ctx = current->perf_event_ctxp[ctxn];
7410 if (!ctx)
7411 continue;
7412
7413 perf_event_enable_on_exec(ctxn);
7414
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007415 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03007416 true);
7417 }
7418 rcu_read_unlock();
7419}
7420
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007421struct remote_output {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05007422 struct perf_buffer *rb;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007423 int err;
7424};
7425
7426static void __perf_event_output_stop(struct perf_event *event, void *data)
7427{
7428 struct perf_event *parent = event->parent;
7429 struct remote_output *ro = data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05007430 struct perf_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007431 struct stop_event_data sd = {
7432 .event = event,
7433 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007434
7435 if (!has_aux(event))
7436 return;
7437
7438 if (!parent)
7439 parent = event;
7440
7441 /*
7442 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03007443 * ring-buffer, but it will be the child that's actually using it.
7444 *
7445 * We are using event::rb to determine if the event should be stopped,
7446 * however this may race with ring_buffer_attach() (through set_output),
7447 * which will make us skip the event that actually needs to be stopped.
7448 * So ring_buffer_attach() has to stop an aux event before re-assigning
7449 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007450 */
7451 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007452 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007453}
7454
7455static int __perf_pmu_output_stop(void *info)
7456{
7457 struct perf_event *event = info;
Alexander Shishkinf3a519e2019-10-22 10:39:40 +03007458 struct pmu *pmu = event->ctx->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01007459 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007460 struct remote_output ro = {
7461 .rb = event->rb,
7462 };
7463
7464 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007465 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007466 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007467 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007468 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007469 rcu_read_unlock();
7470
7471 return ro.err;
7472}
7473
7474static void perf_pmu_output_stop(struct perf_event *event)
7475{
7476 struct perf_event *iter;
7477 int err, cpu;
7478
7479restart:
7480 rcu_read_lock();
7481 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
7482 /*
7483 * For per-CPU events, we need to make sure that neither they
7484 * nor their children are running; for cpu==-1 events it's
7485 * sufficient to stop the event itself if it's active, since
7486 * it can't have children.
7487 */
7488 cpu = iter->cpu;
7489 if (cpu == -1)
7490 cpu = READ_ONCE(iter->oncpu);
7491
7492 if (cpu == -1)
7493 continue;
7494
7495 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
7496 if (err == -EAGAIN) {
7497 rcu_read_unlock();
7498 goto restart;
7499 }
7500 }
7501 rcu_read_unlock();
7502}
7503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007504/*
7505 * task tracking -- fork/exit
7506 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02007507 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007508 */
7509
7510struct perf_task_event {
7511 struct task_struct *task;
7512 struct perf_event_context *task_ctx;
7513
7514 struct {
7515 struct perf_event_header header;
7516
7517 u32 pid;
7518 u32 ppid;
7519 u32 tid;
7520 u32 ptid;
7521 u64 time;
7522 } event_id;
7523};
7524
Jiri Olsa67516842013-07-09 18:56:31 +02007525static int perf_event_task_match(struct perf_event *event)
7526{
Stephane Eranian13d7a242013-08-21 12:10:24 +02007527 return event->attr.comm || event->attr.mmap ||
7528 event->attr.mmap2 || event->attr.mmap_data ||
7529 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02007530}
7531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007532static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007533 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007534{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007535 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007536 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007537 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007538 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007539 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01007540
Jiri Olsa67516842013-07-09 18:56:31 +02007541 if (!perf_event_task_match(event))
7542 return;
7543
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007544 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007545
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007546 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007547 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02007548 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007549 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007550
7551 task_event->event_id.pid = perf_event_pid(event, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007552 task_event->event_id.tid = perf_event_tid(event, task);
Ian Rogersf3bed552020-04-17 11:28:42 -07007553
7554 if (task_event->event_id.header.type == PERF_RECORD_EXIT) {
7555 task_event->event_id.ppid = perf_event_pid(event,
7556 task->real_parent);
7557 task_event->event_id.ptid = perf_event_pid(event,
7558 task->real_parent);
7559 } else { /* PERF_RECORD_FORK */
7560 task_event->event_id.ppid = perf_event_pid(event, current);
7561 task_event->event_id.ptid = perf_event_tid(event, current);
7562 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007563
Peter Zijlstra34f43922015-02-20 14:05:38 +01007564 task_event->event_id.time = perf_event_clock(event);
7565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007566 perf_output_put(&handle, task_event->event_id);
7567
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007568 perf_event__output_id_sample(event, &handle, &sample);
7569
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007570 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007571out:
7572 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007573}
7574
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007575static void perf_event_task(struct task_struct *task,
7576 struct perf_event_context *task_ctx,
7577 int new)
7578{
7579 struct perf_task_event task_event;
7580
7581 if (!atomic_read(&nr_comm_events) &&
7582 !atomic_read(&nr_mmap_events) &&
7583 !atomic_read(&nr_task_events))
7584 return;
7585
7586 task_event = (struct perf_task_event){
7587 .task = task,
7588 .task_ctx = task_ctx,
7589 .event_id = {
7590 .header = {
7591 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
7592 .misc = 0,
7593 .size = sizeof(task_event.event_id),
7594 },
7595 /* .pid */
7596 /* .ppid */
7597 /* .tid */
7598 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01007599 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007600 },
7601 };
7602
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007603 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007604 &task_event,
7605 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007606}
7607
7608void perf_event_fork(struct task_struct *task)
7609{
7610 perf_event_task(task, NULL, 1);
Hari Bathinie4222672017-03-08 02:11:36 +05307611 perf_event_namespaces(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007612}
7613
7614/*
7615 * comm tracking
7616 */
7617
7618struct perf_comm_event {
7619 struct task_struct *task;
7620 char *comm;
7621 int comm_size;
7622
7623 struct {
7624 struct perf_event_header header;
7625
7626 u32 pid;
7627 u32 tid;
7628 } event_id;
7629};
7630
Jiri Olsa67516842013-07-09 18:56:31 +02007631static int perf_event_comm_match(struct perf_event *event)
7632{
7633 return event->attr.comm;
7634}
7635
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007636static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007637 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007638{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007639 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007640 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007641 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007642 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007643 int ret;
7644
Jiri Olsa67516842013-07-09 18:56:31 +02007645 if (!perf_event_comm_match(event))
7646 return;
7647
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007648 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
7649 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007650 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007651
7652 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007653 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007654
7655 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
7656 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
7657
7658 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02007659 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007660 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007661
7662 perf_event__output_id_sample(event, &handle, &sample);
7663
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007664 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007665out:
7666 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007667}
7668
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007669static void perf_event_comm_event(struct perf_comm_event *comm_event)
7670{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007671 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007672 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007673
7674 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01007675 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007676 size = ALIGN(strlen(comm)+1, sizeof(u64));
7677
7678 comm_event->comm = comm;
7679 comm_event->comm_size = size;
7680
7681 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007682
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007683 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007684 comm_event,
7685 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007686}
7687
Adrian Hunter82b89772014-05-28 11:45:04 +03007688void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007689{
7690 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007691
7692 if (!atomic_read(&nr_comm_events))
7693 return;
7694
7695 comm_event = (struct perf_comm_event){
7696 .task = task,
7697 /* .comm */
7698 /* .comm_size */
7699 .event_id = {
7700 .header = {
7701 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03007702 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007703 /* .size */
7704 },
7705 /* .pid */
7706 /* .tid */
7707 },
7708 };
7709
7710 perf_event_comm_event(&comm_event);
7711}
7712
7713/*
Hari Bathinie4222672017-03-08 02:11:36 +05307714 * namespaces tracking
7715 */
7716
7717struct perf_namespaces_event {
7718 struct task_struct *task;
7719
7720 struct {
7721 struct perf_event_header header;
7722
7723 u32 pid;
7724 u32 tid;
7725 u64 nr_namespaces;
7726 struct perf_ns_link_info link_info[NR_NAMESPACES];
7727 } event_id;
7728};
7729
7730static int perf_event_namespaces_match(struct perf_event *event)
7731{
7732 return event->attr.namespaces;
7733}
7734
7735static void perf_event_namespaces_output(struct perf_event *event,
7736 void *data)
7737{
7738 struct perf_namespaces_event *namespaces_event = data;
7739 struct perf_output_handle handle;
7740 struct perf_sample_data sample;
Jiri Olsa34900ec2017-08-09 18:14:06 +02007741 u16 header_size = namespaces_event->event_id.header.size;
Hari Bathinie4222672017-03-08 02:11:36 +05307742 int ret;
7743
7744 if (!perf_event_namespaces_match(event))
7745 return;
7746
7747 perf_event_header__init_id(&namespaces_event->event_id.header,
7748 &sample, event);
7749 ret = perf_output_begin(&handle, event,
7750 namespaces_event->event_id.header.size);
7751 if (ret)
Jiri Olsa34900ec2017-08-09 18:14:06 +02007752 goto out;
Hari Bathinie4222672017-03-08 02:11:36 +05307753
7754 namespaces_event->event_id.pid = perf_event_pid(event,
7755 namespaces_event->task);
7756 namespaces_event->event_id.tid = perf_event_tid(event,
7757 namespaces_event->task);
7758
7759 perf_output_put(&handle, namespaces_event->event_id);
7760
7761 perf_event__output_id_sample(event, &handle, &sample);
7762
7763 perf_output_end(&handle);
Jiri Olsa34900ec2017-08-09 18:14:06 +02007764out:
7765 namespaces_event->event_id.header.size = header_size;
Hari Bathinie4222672017-03-08 02:11:36 +05307766}
7767
7768static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info,
7769 struct task_struct *task,
7770 const struct proc_ns_operations *ns_ops)
7771{
7772 struct path ns_path;
7773 struct inode *ns_inode;
Aleksa Saraice623f82019-12-07 01:13:27 +11007774 int error;
Hari Bathinie4222672017-03-08 02:11:36 +05307775
7776 error = ns_get_path(&ns_path, task, ns_ops);
7777 if (!error) {
7778 ns_inode = ns_path.dentry->d_inode;
7779 ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev);
7780 ns_link_info->ino = ns_inode->i_ino;
Vasily Averin0e18dd12017-11-15 08:47:02 +03007781 path_put(&ns_path);
Hari Bathinie4222672017-03-08 02:11:36 +05307782 }
7783}
7784
7785void perf_event_namespaces(struct task_struct *task)
7786{
7787 struct perf_namespaces_event namespaces_event;
7788 struct perf_ns_link_info *ns_link_info;
7789
7790 if (!atomic_read(&nr_namespaces_events))
7791 return;
7792
7793 namespaces_event = (struct perf_namespaces_event){
7794 .task = task,
7795 .event_id = {
7796 .header = {
7797 .type = PERF_RECORD_NAMESPACES,
7798 .misc = 0,
7799 .size = sizeof(namespaces_event.event_id),
7800 },
7801 /* .pid */
7802 /* .tid */
7803 .nr_namespaces = NR_NAMESPACES,
7804 /* .link_info[NR_NAMESPACES] */
7805 },
7806 };
7807
7808 ns_link_info = namespaces_event.event_id.link_info;
7809
7810 perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX],
7811 task, &mntns_operations);
7812
7813#ifdef CONFIG_USER_NS
7814 perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX],
7815 task, &userns_operations);
7816#endif
7817#ifdef CONFIG_NET_NS
7818 perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX],
7819 task, &netns_operations);
7820#endif
7821#ifdef CONFIG_UTS_NS
7822 perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX],
7823 task, &utsns_operations);
7824#endif
7825#ifdef CONFIG_IPC_NS
7826 perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX],
7827 task, &ipcns_operations);
7828#endif
7829#ifdef CONFIG_PID_NS
7830 perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX],
7831 task, &pidns_operations);
7832#endif
7833#ifdef CONFIG_CGROUPS
7834 perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX],
7835 task, &cgroupns_operations);
7836#endif
7837
7838 perf_iterate_sb(perf_event_namespaces_output,
7839 &namespaces_event,
7840 NULL);
7841}
7842
7843/*
Namhyung Kim96aaab62020-03-25 21:45:28 +09007844 * cgroup tracking
7845 */
7846#ifdef CONFIG_CGROUP_PERF
7847
7848struct perf_cgroup_event {
7849 char *path;
7850 int path_size;
7851 struct {
7852 struct perf_event_header header;
7853 u64 id;
7854 char path[];
7855 } event_id;
7856};
7857
7858static int perf_event_cgroup_match(struct perf_event *event)
7859{
7860 return event->attr.cgroup;
7861}
7862
7863static void perf_event_cgroup_output(struct perf_event *event, void *data)
7864{
7865 struct perf_cgroup_event *cgroup_event = data;
7866 struct perf_output_handle handle;
7867 struct perf_sample_data sample;
7868 u16 header_size = cgroup_event->event_id.header.size;
7869 int ret;
7870
7871 if (!perf_event_cgroup_match(event))
7872 return;
7873
7874 perf_event_header__init_id(&cgroup_event->event_id.header,
7875 &sample, event);
7876 ret = perf_output_begin(&handle, event,
7877 cgroup_event->event_id.header.size);
7878 if (ret)
7879 goto out;
7880
7881 perf_output_put(&handle, cgroup_event->event_id);
7882 __output_copy(&handle, cgroup_event->path, cgroup_event->path_size);
7883
7884 perf_event__output_id_sample(event, &handle, &sample);
7885
7886 perf_output_end(&handle);
7887out:
7888 cgroup_event->event_id.header.size = header_size;
7889}
7890
7891static void perf_event_cgroup(struct cgroup *cgrp)
7892{
7893 struct perf_cgroup_event cgroup_event;
7894 char path_enomem[16] = "//enomem";
7895 char *pathname;
7896 size_t size;
7897
7898 if (!atomic_read(&nr_cgroup_events))
7899 return;
7900
7901 cgroup_event = (struct perf_cgroup_event){
7902 .event_id = {
7903 .header = {
7904 .type = PERF_RECORD_CGROUP,
7905 .misc = 0,
7906 .size = sizeof(cgroup_event.event_id),
7907 },
7908 .id = cgroup_id(cgrp),
7909 },
7910 };
7911
7912 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
7913 if (pathname == NULL) {
7914 cgroup_event.path = path_enomem;
7915 } else {
7916 /* just to be sure to have enough space for alignment */
7917 cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64));
7918 cgroup_event.path = pathname;
7919 }
7920
7921 /*
7922 * Since our buffer works in 8 byte units we need to align our string
7923 * size to a multiple of 8. However, we must guarantee the tail end is
7924 * zero'd out to avoid leaking random bits to userspace.
7925 */
7926 size = strlen(cgroup_event.path) + 1;
7927 while (!IS_ALIGNED(size, sizeof(u64)))
7928 cgroup_event.path[size++] = '\0';
7929
7930 cgroup_event.event_id.header.size += size;
7931 cgroup_event.path_size = size;
7932
7933 perf_iterate_sb(perf_event_cgroup_output,
7934 &cgroup_event,
7935 NULL);
7936
7937 kfree(pathname);
7938}
7939
7940#endif
7941
7942/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007943 * mmap tracking
7944 */
7945
7946struct perf_mmap_event {
7947 struct vm_area_struct *vma;
7948
7949 const char *file_name;
7950 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007951 int maj, min;
7952 u64 ino;
7953 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007954 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007955
7956 struct {
7957 struct perf_event_header header;
7958
7959 u32 pid;
7960 u32 tid;
7961 u64 start;
7962 u64 len;
7963 u64 pgoff;
7964 } event_id;
7965};
7966
Jiri Olsa67516842013-07-09 18:56:31 +02007967static int perf_event_mmap_match(struct perf_event *event,
7968 void *data)
7969{
7970 struct perf_mmap_event *mmap_event = data;
7971 struct vm_area_struct *vma = mmap_event->vma;
7972 int executable = vma->vm_flags & VM_EXEC;
7973
7974 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02007975 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02007976}
7977
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007978static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007979 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007980{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007981 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007982 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007983 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007984 int size = mmap_event->event_id.header.size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007985 u32 type = mmap_event->event_id.header.type;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007986 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007987
Jiri Olsa67516842013-07-09 18:56:31 +02007988 if (!perf_event_mmap_match(event, data))
7989 return;
7990
Stephane Eranian13d7a242013-08-21 12:10:24 +02007991 if (event->attr.mmap2) {
7992 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
7993 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
7994 mmap_event->event_id.header.size += sizeof(mmap_event->min);
7995 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03007996 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007997 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
7998 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007999 }
8000
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008001 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
8002 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02008003 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008004 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008005 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008006
8007 mmap_event->event_id.pid = perf_event_pid(event, current);
8008 mmap_event->event_id.tid = perf_event_tid(event, current);
8009
8010 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02008011
8012 if (event->attr.mmap2) {
8013 perf_output_put(&handle, mmap_event->maj);
8014 perf_output_put(&handle, mmap_event->min);
8015 perf_output_put(&handle, mmap_event->ino);
8016 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008017 perf_output_put(&handle, mmap_event->prot);
8018 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02008019 }
8020
Frederic Weisbecker76369132011-05-19 19:55:04 +02008021 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008022 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008023
8024 perf_event__output_id_sample(event, &handle, &sample);
8025
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008026 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008027out:
8028 mmap_event->event_id.header.size = size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08008029 mmap_event->event_id.header.type = type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008030}
8031
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008032static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
8033{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008034 struct vm_area_struct *vma = mmap_event->vma;
8035 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02008036 int maj = 0, min = 0;
8037 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008038 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008039 unsigned int size;
8040 char tmp[16];
8041 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008042 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008043
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01008044 if (vma->vm_flags & VM_READ)
8045 prot |= PROT_READ;
8046 if (vma->vm_flags & VM_WRITE)
8047 prot |= PROT_WRITE;
8048 if (vma->vm_flags & VM_EXEC)
8049 prot |= PROT_EXEC;
8050
8051 if (vma->vm_flags & VM_MAYSHARE)
8052 flags = MAP_SHARED;
8053 else
8054 flags = MAP_PRIVATE;
8055
8056 if (vma->vm_flags & VM_DENYWRITE)
8057 flags |= MAP_DENYWRITE;
8058 if (vma->vm_flags & VM_MAYEXEC)
8059 flags |= MAP_EXECUTABLE;
8060 if (vma->vm_flags & VM_LOCKED)
8061 flags |= MAP_LOCKED;
Anshuman Khandual03911132020-04-06 20:03:51 -07008062 if (is_vm_hugetlb_page(vma))
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01008063 flags |= MAP_HUGETLB;
8064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008065 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02008066 struct inode *inode;
8067 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02008068
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008069 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008070 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008071 name = "//enomem";
8072 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008073 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008074 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02008075 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008076 * need to add enough zero bytes after the string to handle
8077 * the 64bit alignment we do later.
8078 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02008079 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008080 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008081 name = "//toolong";
8082 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008083 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02008084 inode = file_inode(vma->vm_file);
8085 dev = inode->i_sb->s_dev;
8086 ino = inode->i_ino;
8087 gen = inode->i_generation;
8088 maj = MAJOR(dev);
8089 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008090
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008091 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008092 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02008093 if (vma->vm_ops && vma->vm_ops->name) {
8094 name = (char *) vma->vm_ops->name(vma);
8095 if (name)
8096 goto cpy_name;
8097 }
8098
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008099 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008100 if (name)
8101 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008102
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02008103 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008104 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008105 name = "[heap]";
8106 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02008107 }
8108 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008109 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008110 name = "[stack]";
8111 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008112 }
8113
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008114 name = "//anon";
8115 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008116 }
8117
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008118cpy_name:
8119 strlcpy(tmp, name, sizeof(tmp));
8120 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008121got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008122 /*
8123 * Since our buffer works in 8 byte units we need to align our string
8124 * size to a multiple of 8. However, we must guarantee the tail end is
8125 * zero'd out to avoid leaking random bits to userspace.
8126 */
8127 size = strlen(name)+1;
8128 while (!IS_ALIGNED(size, sizeof(u64)))
8129 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008130
8131 mmap_event->file_name = name;
8132 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02008133 mmap_event->maj = maj;
8134 mmap_event->min = min;
8135 mmap_event->ino = ino;
8136 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008137 mmap_event->prot = prot;
8138 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008139
Stephane Eranian2fe85422013-01-24 16:10:39 +01008140 if (!(vma->vm_flags & VM_EXEC))
8141 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
8142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008143 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
8144
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008145 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02008146 mmap_event,
8147 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008148
8149 kfree(buf);
8150}
8151
Alexander Shishkin375637b2016-04-27 18:44:46 +03008152/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03008153 * Check whether inode and address range match filter criteria.
8154 */
8155static bool perf_addr_filter_match(struct perf_addr_filter *filter,
8156 struct file *file, unsigned long offset,
8157 unsigned long size)
8158{
Mathieu Poirier7f635ff2018-07-16 17:13:51 -06008159 /* d_inode(NULL) won't be equal to any mapped user-space file */
8160 if (!filter->path.dentry)
8161 return false;
8162
Song Liu9511bce2018-04-17 23:29:07 -07008163 if (d_inode(filter->path.dentry) != file_inode(file))
Alexander Shishkin375637b2016-04-27 18:44:46 +03008164 return false;
8165
8166 if (filter->offset > offset + size)
8167 return false;
8168
8169 if (filter->offset + filter->size < offset)
8170 return false;
8171
8172 return true;
8173}
8174
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008175static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter,
8176 struct vm_area_struct *vma,
8177 struct perf_addr_filter_range *fr)
8178{
8179 unsigned long vma_size = vma->vm_end - vma->vm_start;
8180 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8181 struct file *file = vma->vm_file;
8182
8183 if (!perf_addr_filter_match(filter, file, off, vma_size))
8184 return false;
8185
8186 if (filter->offset < off) {
8187 fr->start = vma->vm_start;
8188 fr->size = min(vma_size, filter->size - (off - filter->offset));
8189 } else {
8190 fr->start = vma->vm_start + filter->offset - off;
8191 fr->size = min(vma->vm_end - fr->start, filter->size);
8192 }
8193
8194 return true;
8195}
8196
Alexander Shishkin375637b2016-04-27 18:44:46 +03008197static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
8198{
8199 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8200 struct vm_area_struct *vma = data;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008201 struct perf_addr_filter *filter;
8202 unsigned int restart = 0, count = 0;
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008203 unsigned long flags;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008204
8205 if (!has_addr_filter(event))
8206 return;
8207
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008208 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008209 return;
8210
8211 raw_spin_lock_irqsave(&ifh->lock, flags);
8212 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008213 if (perf_addr_filter_vma_adjust(filter, vma,
8214 &event->addr_filter_ranges[count]))
Alexander Shishkin375637b2016-04-27 18:44:46 +03008215 restart++;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008216
8217 count++;
8218 }
8219
8220 if (restart)
8221 event->addr_filters_gen++;
8222 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8223
8224 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03008225 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008226}
8227
8228/*
8229 * Adjust all task's events' filters to the new vma
8230 */
8231static void perf_addr_filters_adjust(struct vm_area_struct *vma)
8232{
8233 struct perf_event_context *ctx;
8234 int ctxn;
8235
Mathieu Poirier12b40a22016-07-18 10:43:06 -06008236 /*
8237 * Data tracing isn't supported yet and as such there is no need
8238 * to keep track of anything that isn't related to executable code:
8239 */
8240 if (!(vma->vm_flags & VM_EXEC))
8241 return;
8242
Alexander Shishkin375637b2016-04-27 18:44:46 +03008243 rcu_read_lock();
8244 for_each_task_context_nr(ctxn) {
8245 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
8246 if (!ctx)
8247 continue;
8248
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008249 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008250 }
8251 rcu_read_unlock();
8252}
8253
Eric B Munson3af9e852010-05-18 15:30:49 +01008254void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008255{
8256 struct perf_mmap_event mmap_event;
8257
8258 if (!atomic_read(&nr_mmap_events))
8259 return;
8260
8261 mmap_event = (struct perf_mmap_event){
8262 .vma = vma,
8263 /* .file_name */
8264 /* .file_size */
8265 .event_id = {
8266 .header = {
8267 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08008268 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008269 /* .size */
8270 },
8271 /* .pid */
8272 /* .tid */
8273 .start = vma->vm_start,
8274 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01008275 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008276 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02008277 /* .maj (attr_mmap2 only) */
8278 /* .min (attr_mmap2 only) */
8279 /* .ino (attr_mmap2 only) */
8280 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008281 /* .prot (attr_mmap2 only) */
8282 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008283 };
8284
Alexander Shishkin375637b2016-04-27 18:44:46 +03008285 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008286 perf_event_mmap_event(&mmap_event);
8287}
8288
Alexander Shishkin68db7e92015-01-14 14:18:15 +02008289void perf_event_aux_event(struct perf_event *event, unsigned long head,
8290 unsigned long size, u64 flags)
8291{
8292 struct perf_output_handle handle;
8293 struct perf_sample_data sample;
8294 struct perf_aux_event {
8295 struct perf_event_header header;
8296 u64 offset;
8297 u64 size;
8298 u64 flags;
8299 } rec = {
8300 .header = {
8301 .type = PERF_RECORD_AUX,
8302 .misc = 0,
8303 .size = sizeof(rec),
8304 },
8305 .offset = head,
8306 .size = size,
8307 .flags = flags,
8308 };
8309 int ret;
8310
8311 perf_event_header__init_id(&rec.header, &sample, event);
8312 ret = perf_output_begin(&handle, event, rec.header.size);
8313
8314 if (ret)
8315 return;
8316
8317 perf_output_put(&handle, rec);
8318 perf_event__output_id_sample(event, &handle, &sample);
8319
8320 perf_output_end(&handle);
8321}
8322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008323/*
Kan Liangf38b0db2015-05-10 15:13:14 -04008324 * Lost/dropped samples logging
8325 */
8326void perf_log_lost_samples(struct perf_event *event, u64 lost)
8327{
8328 struct perf_output_handle handle;
8329 struct perf_sample_data sample;
8330 int ret;
8331
8332 struct {
8333 struct perf_event_header header;
8334 u64 lost;
8335 } lost_samples_event = {
8336 .header = {
8337 .type = PERF_RECORD_LOST_SAMPLES,
8338 .misc = 0,
8339 .size = sizeof(lost_samples_event),
8340 },
8341 .lost = lost,
8342 };
8343
8344 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
8345
8346 ret = perf_output_begin(&handle, event,
8347 lost_samples_event.header.size);
8348 if (ret)
8349 return;
8350
8351 perf_output_put(&handle, lost_samples_event);
8352 perf_event__output_id_sample(event, &handle, &sample);
8353 perf_output_end(&handle);
8354}
8355
8356/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03008357 * context_switch tracking
8358 */
8359
8360struct perf_switch_event {
8361 struct task_struct *task;
8362 struct task_struct *next_prev;
8363
8364 struct {
8365 struct perf_event_header header;
8366 u32 next_prev_pid;
8367 u32 next_prev_tid;
8368 } event_id;
8369};
8370
8371static int perf_event_switch_match(struct perf_event *event)
8372{
8373 return event->attr.context_switch;
8374}
8375
8376static void perf_event_switch_output(struct perf_event *event, void *data)
8377{
8378 struct perf_switch_event *se = data;
8379 struct perf_output_handle handle;
8380 struct perf_sample_data sample;
8381 int ret;
8382
8383 if (!perf_event_switch_match(event))
8384 return;
8385
8386 /* Only CPU-wide events are allowed to see next/prev pid/tid */
8387 if (event->ctx->task) {
8388 se->event_id.header.type = PERF_RECORD_SWITCH;
8389 se->event_id.header.size = sizeof(se->event_id.header);
8390 } else {
8391 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
8392 se->event_id.header.size = sizeof(se->event_id);
8393 se->event_id.next_prev_pid =
8394 perf_event_pid(event, se->next_prev);
8395 se->event_id.next_prev_tid =
8396 perf_event_tid(event, se->next_prev);
8397 }
8398
8399 perf_event_header__init_id(&se->event_id.header, &sample, event);
8400
8401 ret = perf_output_begin(&handle, event, se->event_id.header.size);
8402 if (ret)
8403 return;
8404
8405 if (event->ctx->task)
8406 perf_output_put(&handle, se->event_id.header);
8407 else
8408 perf_output_put(&handle, se->event_id);
8409
8410 perf_event__output_id_sample(event, &handle, &sample);
8411
8412 perf_output_end(&handle);
8413}
8414
8415static void perf_event_switch(struct task_struct *task,
8416 struct task_struct *next_prev, bool sched_in)
8417{
8418 struct perf_switch_event switch_event;
8419
8420 /* N.B. caller checks nr_switch_events != 0 */
8421
8422 switch_event = (struct perf_switch_event){
8423 .task = task,
8424 .next_prev = next_prev,
8425 .event_id = {
8426 .header = {
8427 /* .type */
8428 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
8429 /* .size */
8430 },
8431 /* .next_prev_pid */
8432 /* .next_prev_tid */
8433 },
8434 };
8435
Alexey Budankov101592b2018-04-09 10:25:32 +03008436 if (!sched_in && task->state == TASK_RUNNING)
8437 switch_event.event_id.header.misc |=
8438 PERF_RECORD_MISC_SWITCH_OUT_PREEMPT;
8439
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008440 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03008441 &switch_event,
8442 NULL);
8443}
8444
8445/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008446 * IRQ throttle logging
8447 */
8448
8449static void perf_log_throttle(struct perf_event *event, int enable)
8450{
8451 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008452 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008453 int ret;
8454
8455 struct {
8456 struct perf_event_header header;
8457 u64 time;
8458 u64 id;
8459 u64 stream_id;
8460 } throttle_event = {
8461 .header = {
8462 .type = PERF_RECORD_THROTTLE,
8463 .misc = 0,
8464 .size = sizeof(throttle_event),
8465 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01008466 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008467 .id = primary_event_id(event),
8468 .stream_id = event->id,
8469 };
8470
8471 if (enable)
8472 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
8473
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008474 perf_event_header__init_id(&throttle_event.header, &sample, event);
8475
8476 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02008477 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008478 if (ret)
8479 return;
8480
8481 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008482 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008483 perf_output_end(&handle);
8484}
8485
Song Liu76193a92019-01-17 08:15:13 -08008486/*
8487 * ksymbol register/unregister tracking
8488 */
8489
8490struct perf_ksymbol_event {
8491 const char *name;
8492 int name_len;
8493 struct {
8494 struct perf_event_header header;
8495 u64 addr;
8496 u32 len;
8497 u16 ksym_type;
8498 u16 flags;
8499 } event_id;
8500};
8501
8502static int perf_event_ksymbol_match(struct perf_event *event)
8503{
8504 return event->attr.ksymbol;
8505}
8506
8507static void perf_event_ksymbol_output(struct perf_event *event, void *data)
8508{
8509 struct perf_ksymbol_event *ksymbol_event = data;
8510 struct perf_output_handle handle;
8511 struct perf_sample_data sample;
8512 int ret;
8513
8514 if (!perf_event_ksymbol_match(event))
8515 return;
8516
8517 perf_event_header__init_id(&ksymbol_event->event_id.header,
8518 &sample, event);
8519 ret = perf_output_begin(&handle, event,
8520 ksymbol_event->event_id.header.size);
8521 if (ret)
8522 return;
8523
8524 perf_output_put(&handle, ksymbol_event->event_id);
8525 __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len);
8526 perf_event__output_id_sample(event, &handle, &sample);
8527
8528 perf_output_end(&handle);
8529}
8530
8531void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister,
8532 const char *sym)
8533{
8534 struct perf_ksymbol_event ksymbol_event;
8535 char name[KSYM_NAME_LEN];
8536 u16 flags = 0;
8537 int name_len;
8538
8539 if (!atomic_read(&nr_ksymbol_events))
8540 return;
8541
8542 if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX ||
8543 ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN)
8544 goto err;
8545
8546 strlcpy(name, sym, KSYM_NAME_LEN);
8547 name_len = strlen(name) + 1;
8548 while (!IS_ALIGNED(name_len, sizeof(u64)))
8549 name[name_len++] = '\0';
8550 BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64));
8551
8552 if (unregister)
8553 flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER;
8554
8555 ksymbol_event = (struct perf_ksymbol_event){
8556 .name = name,
8557 .name_len = name_len,
8558 .event_id = {
8559 .header = {
8560 .type = PERF_RECORD_KSYMBOL,
8561 .size = sizeof(ksymbol_event.event_id) +
8562 name_len,
8563 },
8564 .addr = addr,
8565 .len = len,
8566 .ksym_type = ksym_type,
8567 .flags = flags,
8568 },
8569 };
8570
8571 perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL);
8572 return;
8573err:
8574 WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type);
8575}
8576
Song Liu6ee52e22019-01-17 08:15:15 -08008577/*
8578 * bpf program load/unload tracking
8579 */
8580
8581struct perf_bpf_event {
8582 struct bpf_prog *prog;
8583 struct {
8584 struct perf_event_header header;
8585 u16 type;
8586 u16 flags;
8587 u32 id;
8588 u8 tag[BPF_TAG_SIZE];
8589 } event_id;
8590};
8591
8592static int perf_event_bpf_match(struct perf_event *event)
8593{
8594 return event->attr.bpf_event;
8595}
8596
8597static void perf_event_bpf_output(struct perf_event *event, void *data)
8598{
8599 struct perf_bpf_event *bpf_event = data;
8600 struct perf_output_handle handle;
8601 struct perf_sample_data sample;
8602 int ret;
8603
8604 if (!perf_event_bpf_match(event))
8605 return;
8606
8607 perf_event_header__init_id(&bpf_event->event_id.header,
8608 &sample, event);
8609 ret = perf_output_begin(&handle, event,
8610 bpf_event->event_id.header.size);
8611 if (ret)
8612 return;
8613
8614 perf_output_put(&handle, bpf_event->event_id);
8615 perf_event__output_id_sample(event, &handle, &sample);
8616
8617 perf_output_end(&handle);
8618}
8619
8620static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
8621 enum perf_bpf_event_type type)
8622{
8623 bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
Song Liu6ee52e22019-01-17 08:15:15 -08008624 int i;
8625
8626 if (prog->aux->func_cnt == 0) {
Song Liu6ee52e22019-01-17 08:15:15 -08008627 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
8628 (u64)(unsigned long)prog->bpf_func,
Jiri Olsabfea9a82020-03-12 20:55:59 +01008629 prog->jited_len, unregister,
8630 prog->aux->ksym.name);
Song Liu6ee52e22019-01-17 08:15:15 -08008631 } else {
8632 for (i = 0; i < prog->aux->func_cnt; i++) {
8633 struct bpf_prog *subprog = prog->aux->func[i];
8634
Song Liu6ee52e22019-01-17 08:15:15 -08008635 perf_event_ksymbol(
8636 PERF_RECORD_KSYMBOL_TYPE_BPF,
8637 (u64)(unsigned long)subprog->bpf_func,
Jiri Olsabfea9a82020-03-12 20:55:59 +01008638 subprog->jited_len, unregister,
8639 prog->aux->ksym.name);
Song Liu6ee52e22019-01-17 08:15:15 -08008640 }
8641 }
8642}
8643
8644void perf_event_bpf_event(struct bpf_prog *prog,
8645 enum perf_bpf_event_type type,
8646 u16 flags)
8647{
8648 struct perf_bpf_event bpf_event;
8649
8650 if (type <= PERF_BPF_EVENT_UNKNOWN ||
8651 type >= PERF_BPF_EVENT_MAX)
8652 return;
8653
8654 switch (type) {
8655 case PERF_BPF_EVENT_PROG_LOAD:
8656 case PERF_BPF_EVENT_PROG_UNLOAD:
8657 if (atomic_read(&nr_ksymbol_events))
8658 perf_event_bpf_emit_ksymbols(prog, type);
8659 break;
8660 default:
8661 break;
8662 }
8663
8664 if (!atomic_read(&nr_bpf_events))
8665 return;
8666
8667 bpf_event = (struct perf_bpf_event){
8668 .prog = prog,
8669 .event_id = {
8670 .header = {
8671 .type = PERF_RECORD_BPF_EVENT,
8672 .size = sizeof(bpf_event.event_id),
8673 },
8674 .type = type,
8675 .flags = flags,
8676 .id = prog->aux->id,
8677 },
8678 };
8679
8680 BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64));
8681
8682 memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
8683 perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
8684}
8685
Adrian Huntere17d43b2020-05-12 15:19:08 +03008686struct perf_text_poke_event {
8687 const void *old_bytes;
8688 const void *new_bytes;
8689 size_t pad;
8690 u16 old_len;
8691 u16 new_len;
8692
8693 struct {
8694 struct perf_event_header header;
8695
8696 u64 addr;
8697 } event_id;
8698};
8699
8700static int perf_event_text_poke_match(struct perf_event *event)
8701{
8702 return event->attr.text_poke;
8703}
8704
8705static void perf_event_text_poke_output(struct perf_event *event, void *data)
8706{
8707 struct perf_text_poke_event *text_poke_event = data;
8708 struct perf_output_handle handle;
8709 struct perf_sample_data sample;
8710 u64 padding = 0;
8711 int ret;
8712
8713 if (!perf_event_text_poke_match(event))
8714 return;
8715
8716 perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event);
8717
8718 ret = perf_output_begin(&handle, event, text_poke_event->event_id.header.size);
8719 if (ret)
8720 return;
8721
8722 perf_output_put(&handle, text_poke_event->event_id);
8723 perf_output_put(&handle, text_poke_event->old_len);
8724 perf_output_put(&handle, text_poke_event->new_len);
8725
8726 __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len);
8727 __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len);
8728
8729 if (text_poke_event->pad)
8730 __output_copy(&handle, &padding, text_poke_event->pad);
8731
8732 perf_event__output_id_sample(event, &handle, &sample);
8733
8734 perf_output_end(&handle);
8735}
8736
8737void perf_event_text_poke(const void *addr, const void *old_bytes,
8738 size_t old_len, const void *new_bytes, size_t new_len)
8739{
8740 struct perf_text_poke_event text_poke_event;
8741 size_t tot, pad;
8742
8743 if (!atomic_read(&nr_text_poke_events))
8744 return;
8745
8746 tot = sizeof(text_poke_event.old_len) + old_len;
8747 tot += sizeof(text_poke_event.new_len) + new_len;
8748 pad = ALIGN(tot, sizeof(u64)) - tot;
8749
8750 text_poke_event = (struct perf_text_poke_event){
8751 .old_bytes = old_bytes,
8752 .new_bytes = new_bytes,
8753 .pad = pad,
8754 .old_len = old_len,
8755 .new_len = new_len,
8756 .event_id = {
8757 .header = {
8758 .type = PERF_RECORD_TEXT_POKE,
8759 .misc = PERF_RECORD_MISC_KERNEL,
8760 .size = sizeof(text_poke_event.event_id) + tot + pad,
8761 },
8762 .addr = (unsigned long)addr,
8763 },
8764 };
8765
8766 perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL);
8767}
8768
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008769void perf_event_itrace_started(struct perf_event *event)
8770{
8771 event->attach_state |= PERF_ATTACH_ITRACE;
8772}
8773
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008774static void perf_log_itrace_start(struct perf_event *event)
8775{
8776 struct perf_output_handle handle;
8777 struct perf_sample_data sample;
8778 struct perf_aux_event {
8779 struct perf_event_header header;
8780 u32 pid;
8781 u32 tid;
8782 } rec;
8783 int ret;
8784
8785 if (event->parent)
8786 event = event->parent;
8787
8788 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008789 event->attach_state & PERF_ATTACH_ITRACE)
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008790 return;
8791
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008792 rec.header.type = PERF_RECORD_ITRACE_START;
8793 rec.header.misc = 0;
8794 rec.header.size = sizeof(rec);
8795 rec.pid = perf_event_pid(event, current);
8796 rec.tid = perf_event_tid(event, current);
8797
8798 perf_event_header__init_id(&rec.header, &sample, event);
8799 ret = perf_output_begin(&handle, event, rec.header.size);
8800
8801 if (ret)
8802 return;
8803
8804 perf_output_put(&handle, rec);
8805 perf_event__output_id_sample(event, &handle, &sample);
8806
8807 perf_output_end(&handle);
8808}
8809
Jiri Olsa475113d2016-12-28 14:31:03 +01008810static int
8811__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008812{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008813 struct hw_perf_event *hwc = &event->hw;
8814 int ret = 0;
Jiri Olsa475113d2016-12-28 14:31:03 +01008815 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01008816
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008817 seq = __this_cpu_read(perf_throttled_seq);
8818 if (seq != hwc->interrupts_seq) {
8819 hwc->interrupts_seq = seq;
8820 hwc->interrupts = 1;
8821 } else {
8822 hwc->interrupts++;
8823 if (unlikely(throttle
8824 && hwc->interrupts >= max_samples_per_tick)) {
8825 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008826 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01008827 hwc->interrupts = MAX_INTERRUPTS;
8828 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008829 ret = 1;
8830 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008831 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008832
8833 if (event->attr.freq) {
8834 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01008835 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008836
Peter Zijlstraabd50712010-01-26 18:50:16 +01008837 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008838
Peter Zijlstraabd50712010-01-26 18:50:16 +01008839 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01008840 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008841 }
8842
Jiri Olsa475113d2016-12-28 14:31:03 +01008843 return ret;
8844}
8845
8846int perf_event_account_interrupt(struct perf_event *event)
8847{
8848 return __perf_event_account_interrupt(event, 1);
8849}
8850
8851/*
8852 * Generic event overflow handling, sampling.
8853 */
8854
8855static int __perf_event_overflow(struct perf_event *event,
8856 int throttle, struct perf_sample_data *data,
8857 struct pt_regs *regs)
8858{
8859 int events = atomic_read(&event->event_limit);
8860 int ret = 0;
8861
8862 /*
8863 * Non-sampling counters might still use the PMI to fold short
8864 * hardware counters, ignore those.
8865 */
8866 if (unlikely(!is_sampling_event(event)))
8867 return 0;
8868
8869 ret = __perf_event_account_interrupt(event, throttle);
8870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008871 /*
8872 * XXX event_limit might not quite work as expected on inherited
8873 * events
8874 */
8875
8876 event->pending_kill = POLL_IN;
8877 if (events && atomic_dec_and_test(&event->event_limit)) {
8878 ret = 1;
8879 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02008880
8881 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008882 }
8883
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07008884 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01008885
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02008886 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008887 event->pending_wakeup = 1;
8888 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02008889 }
8890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008891 return ret;
8892}
8893
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008894int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008895 struct perf_sample_data *data,
8896 struct pt_regs *regs)
8897{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008898 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008899}
8900
8901/*
8902 * Generic software event infrastructure
8903 */
8904
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008905struct swevent_htable {
8906 struct swevent_hlist *swevent_hlist;
8907 struct mutex hlist_mutex;
8908 int hlist_refcount;
8909
8910 /* Recursion avoidance in each contexts */
8911 int recursion[PERF_NR_CONTEXTS];
8912};
8913
8914static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
8915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008916/*
8917 * We directly increment event->count and keep a second value in
8918 * event->hw.period_left to count intervals. This period event
8919 * is kept in the range [-sample_period, 0] so that we can use the
8920 * sign as trigger.
8921 */
8922
Jiri Olsaab573842013-05-01 17:25:44 +02008923u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008924{
8925 struct hw_perf_event *hwc = &event->hw;
8926 u64 period = hwc->last_period;
8927 u64 nr, offset;
8928 s64 old, val;
8929
8930 hwc->last_period = hwc->sample_period;
8931
8932again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02008933 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008934 if (val < 0)
8935 return 0;
8936
8937 nr = div64_u64(period + val, period);
8938 offset = nr * period;
8939 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02008940 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008941 goto again;
8942
8943 return nr;
8944}
8945
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008946static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008947 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008948 struct pt_regs *regs)
8949{
8950 struct hw_perf_event *hwc = &event->hw;
8951 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008952
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008953 if (!overflow)
8954 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008955
8956 if (hwc->interrupts == MAX_INTERRUPTS)
8957 return;
8958
8959 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008960 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008961 data, regs)) {
8962 /*
8963 * We inhibit the overflow from happening when
8964 * hwc->interrupts == MAX_INTERRUPTS.
8965 */
8966 break;
8967 }
8968 throttle = 1;
8969 }
8970}
8971
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008972static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008973 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008974 struct pt_regs *regs)
8975{
8976 struct hw_perf_event *hwc = &event->hw;
8977
Peter Zijlstrae7850592010-05-21 14:43:08 +02008978 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008980 if (!regs)
8981 return;
8982
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008983 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008984 return;
8985
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03008986 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
8987 data->period = nr;
8988 return perf_swevent_overflow(event, 1, data, regs);
8989 } else
8990 data->period = event->hw.last_period;
8991
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008992 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008993 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008994
Peter Zijlstrae7850592010-05-21 14:43:08 +02008995 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008996 return;
8997
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008998 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008999}
9000
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009001static int perf_exclude_event(struct perf_event *event,
9002 struct pt_regs *regs)
9003{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009004 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01009005 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009006
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009007 if (regs) {
9008 if (event->attr.exclude_user && user_mode(regs))
9009 return 1;
9010
9011 if (event->attr.exclude_kernel && !user_mode(regs))
9012 return 1;
9013 }
9014
9015 return 0;
9016}
9017
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009018static int perf_swevent_match(struct perf_event *event,
9019 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08009020 u32 event_id,
9021 struct perf_sample_data *data,
9022 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009023{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009024 if (event->attr.type != type)
9025 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009026
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009027 if (event->attr.config != event_id)
9028 return 0;
9029
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009030 if (perf_exclude_event(event, regs))
9031 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009032
9033 return 1;
9034}
9035
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009036static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009037{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009038 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009039
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009040 return hash_64(val, SWEVENT_HLIST_BITS);
9041}
9042
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009043static inline struct hlist_head *
9044__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009045{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009046 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009047
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009048 return &hlist->heads[hash];
9049}
9050
9051/* For the read side: events when they trigger */
9052static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009053find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009054{
9055 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009056
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009057 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009058 if (!hlist)
9059 return NULL;
9060
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009061 return __find_swevent_head(hlist, type, event_id);
9062}
9063
9064/* For the event head insertion and removal in the hlist */
9065static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009066find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009067{
9068 struct swevent_hlist *hlist;
9069 u32 event_id = event->attr.config;
9070 u64 type = event->attr.type;
9071
9072 /*
9073 * Event scheduling is always serialized against hlist allocation
9074 * and release. Which makes the protected version suitable here.
9075 * The context lock guarantees that.
9076 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009077 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009078 lockdep_is_held(&event->ctx->lock));
9079 if (!hlist)
9080 return NULL;
9081
9082 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009083}
9084
9085static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009086 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009087 struct perf_sample_data *data,
9088 struct pt_regs *regs)
9089{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009090 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009091 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009092 struct hlist_head *head;
9093
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009094 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009095 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009096 if (!head)
9097 goto end;
9098
Sasha Levinb67bfe02013-02-27 17:06:00 -08009099 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08009100 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009101 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009102 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009103end:
9104 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009105}
9106
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009107DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
9108
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009109int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009110{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009111 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01009112
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009113 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009114}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01009115EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009116
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009117void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009118{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009119 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009120
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009121 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01009122}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009123
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009124void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009125{
Ingo Molnara4234bf2009-11-23 10:57:59 +01009126 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009127
9128 if (WARN_ON_ONCE(!regs))
9129 return;
9130
9131 perf_sample_data_init(&data, addr, 0);
9132 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
9133}
9134
9135void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
9136{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009137 int rctx;
9138
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009139 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009140 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009141 if (unlikely(rctx < 0))
9142 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009143
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009144 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009145
9146 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009147fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009148 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009149}
9150
9151static void perf_swevent_read(struct perf_event *event)
9152{
9153}
9154
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009155static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009156{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009157 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009158 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009159 struct hlist_head *head;
9160
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01009161 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009162 hwc->last_period = hwc->sample_period;
9163 perf_swevent_set_period(event);
9164 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009165
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009166 hwc->state = !(flags & PERF_EF_START);
9167
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009168 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01009169 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009170 return -EINVAL;
9171
9172 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08009173 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009175 return 0;
9176}
9177
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009178static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009179{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009180 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009181}
9182
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009183static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009184{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009185 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009186}
9187
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009188static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009189{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009190 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009191}
9192
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009193/* Deref the hlist from the update side */
9194static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009195swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009196{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009197 return rcu_dereference_protected(swhash->swevent_hlist,
9198 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009199}
9200
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009201static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009202{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009203 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009204
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009205 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009206 return;
9207
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03009208 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08009209 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009210}
9211
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009212static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009213{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009214 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009215
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009216 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009217
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009218 if (!--swhash->hlist_refcount)
9219 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009220
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009221 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009222}
9223
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009224static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009225{
9226 int cpu;
9227
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009228 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009229 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009230}
9231
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009232static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009233{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009234 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009235 int err = 0;
9236
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009237 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009238 if (!swevent_hlist_deref(swhash) &&
9239 cpumask_test_cpu(cpu, perf_online_mask)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009240 struct swevent_hlist *hlist;
9241
9242 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
9243 if (!hlist) {
9244 err = -ENOMEM;
9245 goto exit;
9246 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009247 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009248 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009249 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02009250exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009251 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009252
9253 return err;
9254}
9255
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009256static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009257{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009258 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009259
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009260 mutex_lock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009261 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009262 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009263 if (err) {
9264 failed_cpu = cpu;
9265 goto fail;
9266 }
9267 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009268 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009269 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02009270fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009271 for_each_possible_cpu(cpu) {
9272 if (cpu == failed_cpu)
9273 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009274 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009275 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009276 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009277 return err;
9278}
9279
Ingo Molnarc5905af2012-02-24 08:31:31 +01009280struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009281
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009282static void sw_perf_event_destroy(struct perf_event *event)
9283{
9284 u64 event_id = event->attr.config;
9285
9286 WARN_ON(event->parent);
9287
Ingo Molnarc5905af2012-02-24 08:31:31 +01009288 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009289 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009290}
9291
9292static int perf_swevent_init(struct perf_event *event)
9293{
Tommi Rantala8176cce2013-04-13 22:49:14 +03009294 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009295
9296 if (event->attr.type != PERF_TYPE_SOFTWARE)
9297 return -ENOENT;
9298
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009299 /*
9300 * no branch sampling for software events
9301 */
9302 if (has_branch_stack(event))
9303 return -EOPNOTSUPP;
9304
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009305 switch (event_id) {
9306 case PERF_COUNT_SW_CPU_CLOCK:
9307 case PERF_COUNT_SW_TASK_CLOCK:
9308 return -ENOENT;
9309
9310 default:
9311 break;
9312 }
9313
Dan Carpenterce677832010-10-24 21:50:42 +02009314 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009315 return -ENOENT;
9316
9317 if (!event->parent) {
9318 int err;
9319
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009320 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009321 if (err)
9322 return err;
9323
Ingo Molnarc5905af2012-02-24 08:31:31 +01009324 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009325 event->destroy = sw_perf_event_destroy;
9326 }
9327
9328 return 0;
9329}
9330
9331static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009332 .task_ctx_nr = perf_sw_context,
9333
Peter Zijlstra34f43922015-02-20 14:05:38 +01009334 .capabilities = PERF_PMU_CAP_NO_NMI,
9335
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009336 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009337 .add = perf_swevent_add,
9338 .del = perf_swevent_del,
9339 .start = perf_swevent_start,
9340 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009341 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009342};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009343
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009344#ifdef CONFIG_EVENT_TRACING
9345
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009346static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009347 struct perf_sample_data *data)
9348{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02009349 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009350
Peter Zijlstrab71b4372015-11-02 10:50:51 +01009351 /* only top level events have filters set */
9352 if (event->parent)
9353 event = event->parent;
9354
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009355 if (likely(!event->filter) || filter_match_preds(event->filter, record))
9356 return 1;
9357 return 0;
9358}
9359
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009360static int perf_tp_event_match(struct perf_event *event,
9361 struct perf_sample_data *data,
9362 struct pt_regs *regs)
9363{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01009364 if (event->hw.state & PERF_HES_STOPPED)
9365 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02009366 /*
Song Liu9fd2e482019-05-07 09:15:45 -07009367 * If exclude_kernel, only trace user-space tracepoints (uprobes)
Peter Zijlstra580d6072010-05-20 20:54:31 +02009368 */
Song Liu9fd2e482019-05-07 09:15:45 -07009369 if (event->attr.exclude_kernel && !user_mode(regs))
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009370 return 0;
9371
9372 if (!perf_tp_filter_match(event, data))
9373 return 0;
9374
9375 return 1;
9376}
9377
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009378void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
9379 struct trace_event_call *call, u64 count,
9380 struct pt_regs *regs, struct hlist_head *head,
9381 struct task_struct *task)
9382{
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009383 if (bpf_prog_array_valid(call)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009384 *(struct pt_regs **)raw_data = regs;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009385 if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009386 perf_swevent_put_recursion_context(rctx);
9387 return;
9388 }
9389 }
9390 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009391 rctx, task);
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009392}
9393EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
9394
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009395void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009396 struct pt_regs *regs, struct hlist_head *head, int rctx,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009397 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009398{
9399 struct perf_sample_data data;
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009400 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009401
9402 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02009403 .frag = {
9404 .size = entry_size,
9405 .data = record,
9406 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009407 };
9408
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009409 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009410 data.raw = &raw;
9411
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009412 perf_trace_buf_update(record, event_type);
9413
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009414 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009415 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009416 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009417 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02009418
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009419 /*
9420 * If we got specified a target task, also iterate its context and
9421 * deliver this event there too.
9422 */
9423 if (task && task != current) {
9424 struct perf_event_context *ctx;
9425 struct trace_entry *entry = record;
9426
9427 rcu_read_lock();
9428 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
9429 if (!ctx)
9430 goto unlock;
9431
9432 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Jiri Olsacd6fb6772018-09-23 18:13:43 +02009433 if (event->cpu != smp_processor_id())
9434 continue;
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009435 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9436 continue;
9437 if (event->attr.config != entry->type)
9438 continue;
9439 if (perf_tp_event_match(event, &data, regs))
9440 perf_swevent_event(event, count, &data, regs);
9441 }
9442unlock:
9443 rcu_read_unlock();
9444 }
9445
Peter Zijlstraecc55f82010-05-21 15:11:34 +02009446 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009447}
9448EXPORT_SYMBOL_GPL(perf_tp_event);
9449
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009450static void tp_perf_event_destroy(struct perf_event *event)
9451{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009452 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009453}
9454
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009455static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009456{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009457 int err;
9458
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009459 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9460 return -ENOENT;
9461
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009462 /*
9463 * no branch sampling for tracepoint events
9464 */
9465 if (has_branch_stack(event))
9466 return -EOPNOTSUPP;
9467
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009468 err = perf_trace_init(event);
9469 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009470 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009471
9472 event->destroy = tp_perf_event_destroy;
9473
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009474 return 0;
9475}
9476
9477static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009478 .task_ctx_nr = perf_sw_context,
9479
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009480 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009481 .add = perf_trace_add,
9482 .del = perf_trace_del,
9483 .start = perf_swevent_start,
9484 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009485 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009486};
9487
Song Liu33ea4b22017-12-06 14:45:16 -08009488#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Song Liue12f03d2017-12-06 14:45:15 -08009489/*
9490 * Flags in config, used by dynamic PMU kprobe and uprobe
9491 * The flags should match following PMU_FORMAT_ATTR().
9492 *
9493 * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe
9494 * if not set, create kprobe/uprobe
Song Liua6ca88b2018-10-01 22:36:36 -07009495 *
9496 * The following values specify a reference counter (or semaphore in the
9497 * terminology of tools like dtrace, systemtap, etc.) Userspace Statically
9498 * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset.
9499 *
9500 * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset
9501 * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left
Song Liue12f03d2017-12-06 14:45:15 -08009502 */
9503enum perf_probe_config {
9504 PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */
Song Liua6ca88b2018-10-01 22:36:36 -07009505 PERF_UPROBE_REF_CTR_OFFSET_BITS = 32,
9506 PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS,
Song Liue12f03d2017-12-06 14:45:15 -08009507};
9508
9509PMU_FORMAT_ATTR(retprobe, "config:0");
Song Liua6ca88b2018-10-01 22:36:36 -07009510#endif
Song Liue12f03d2017-12-06 14:45:15 -08009511
Song Liua6ca88b2018-10-01 22:36:36 -07009512#ifdef CONFIG_KPROBE_EVENTS
9513static struct attribute *kprobe_attrs[] = {
Song Liue12f03d2017-12-06 14:45:15 -08009514 &format_attr_retprobe.attr,
9515 NULL,
9516};
9517
Song Liua6ca88b2018-10-01 22:36:36 -07009518static struct attribute_group kprobe_format_group = {
Song Liue12f03d2017-12-06 14:45:15 -08009519 .name = "format",
Song Liua6ca88b2018-10-01 22:36:36 -07009520 .attrs = kprobe_attrs,
Song Liue12f03d2017-12-06 14:45:15 -08009521};
9522
Song Liua6ca88b2018-10-01 22:36:36 -07009523static const struct attribute_group *kprobe_attr_groups[] = {
9524 &kprobe_format_group,
Song Liue12f03d2017-12-06 14:45:15 -08009525 NULL,
9526};
9527
9528static int perf_kprobe_event_init(struct perf_event *event);
9529static struct pmu perf_kprobe = {
9530 .task_ctx_nr = perf_sw_context,
9531 .event_init = perf_kprobe_event_init,
9532 .add = perf_trace_add,
9533 .del = perf_trace_del,
9534 .start = perf_swevent_start,
9535 .stop = perf_swevent_stop,
9536 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009537 .attr_groups = kprobe_attr_groups,
Song Liue12f03d2017-12-06 14:45:15 -08009538};
9539
9540static int perf_kprobe_event_init(struct perf_event *event)
9541{
9542 int err;
9543 bool is_retprobe;
9544
9545 if (event->attr.type != perf_kprobe.type)
9546 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009547
Alexey Budankovc9e09242020-04-02 11:47:01 +03009548 if (!perfmon_capable())
Song Liu32e6e962018-04-11 18:02:37 +00009549 return -EACCES;
9550
Song Liue12f03d2017-12-06 14:45:15 -08009551 /*
9552 * no branch sampling for probe events
9553 */
9554 if (has_branch_stack(event))
9555 return -EOPNOTSUPP;
9556
9557 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
9558 err = perf_kprobe_init(event, is_retprobe);
9559 if (err)
9560 return err;
9561
9562 event->destroy = perf_kprobe_destroy;
9563
9564 return 0;
9565}
9566#endif /* CONFIG_KPROBE_EVENTS */
9567
Song Liu33ea4b22017-12-06 14:45:16 -08009568#ifdef CONFIG_UPROBE_EVENTS
Song Liua6ca88b2018-10-01 22:36:36 -07009569PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63");
9570
9571static struct attribute *uprobe_attrs[] = {
9572 &format_attr_retprobe.attr,
9573 &format_attr_ref_ctr_offset.attr,
9574 NULL,
9575};
9576
9577static struct attribute_group uprobe_format_group = {
9578 .name = "format",
9579 .attrs = uprobe_attrs,
9580};
9581
9582static const struct attribute_group *uprobe_attr_groups[] = {
9583 &uprobe_format_group,
9584 NULL,
9585};
9586
Song Liu33ea4b22017-12-06 14:45:16 -08009587static int perf_uprobe_event_init(struct perf_event *event);
9588static struct pmu perf_uprobe = {
9589 .task_ctx_nr = perf_sw_context,
9590 .event_init = perf_uprobe_event_init,
9591 .add = perf_trace_add,
9592 .del = perf_trace_del,
9593 .start = perf_swevent_start,
9594 .stop = perf_swevent_stop,
9595 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009596 .attr_groups = uprobe_attr_groups,
Song Liu33ea4b22017-12-06 14:45:16 -08009597};
9598
9599static int perf_uprobe_event_init(struct perf_event *event)
9600{
9601 int err;
Song Liua6ca88b2018-10-01 22:36:36 -07009602 unsigned long ref_ctr_offset;
Song Liu33ea4b22017-12-06 14:45:16 -08009603 bool is_retprobe;
9604
9605 if (event->attr.type != perf_uprobe.type)
9606 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009607
Alexey Budankovc9e09242020-04-02 11:47:01 +03009608 if (!perfmon_capable())
Song Liu32e6e962018-04-11 18:02:37 +00009609 return -EACCES;
9610
Song Liu33ea4b22017-12-06 14:45:16 -08009611 /*
9612 * no branch sampling for probe events
9613 */
9614 if (has_branch_stack(event))
9615 return -EOPNOTSUPP;
9616
9617 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
Song Liua6ca88b2018-10-01 22:36:36 -07009618 ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
9619 err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe);
Song Liu33ea4b22017-12-06 14:45:16 -08009620 if (err)
9621 return err;
9622
9623 event->destroy = perf_uprobe_destroy;
9624
9625 return 0;
9626}
9627#endif /* CONFIG_UPROBE_EVENTS */
9628
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009629static inline void perf_tp_register(void)
9630{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009631 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Song Liue12f03d2017-12-06 14:45:15 -08009632#ifdef CONFIG_KPROBE_EVENTS
9633 perf_pmu_register(&perf_kprobe, "kprobe", -1);
9634#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009635#ifdef CONFIG_UPROBE_EVENTS
9636 perf_pmu_register(&perf_uprobe, "uprobe", -1);
9637#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009638}
Li Zefan6fb29152009-10-15 11:21:42 +08009639
Li Zefan6fb29152009-10-15 11:21:42 +08009640static void perf_event_free_filter(struct perf_event *event)
9641{
9642 ftrace_profile_free_filter(event);
9643}
9644
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009645#ifdef CONFIG_BPF_SYSCALL
9646static void bpf_overflow_handler(struct perf_event *event,
9647 struct perf_sample_data *data,
9648 struct pt_regs *regs)
9649{
9650 struct bpf_perf_event_data_kern ctx = {
9651 .data = data,
Yonghong Song7d9285e2017-10-05 09:19:19 -07009652 .event = event,
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009653 };
9654 int ret = 0;
9655
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +01009656 ctx.regs = perf_arch_bpf_user_pt_regs(regs);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009657 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
9658 goto out;
9659 rcu_read_lock();
Daniel Borkmann88575192016-11-26 01:28:04 +01009660 ret = BPF_PROG_RUN(event->prog, &ctx);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009661 rcu_read_unlock();
9662out:
9663 __this_cpu_dec(bpf_prog_active);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009664 if (!ret)
9665 return;
9666
9667 event->orig_overflow_handler(event, data, regs);
9668}
9669
9670static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9671{
9672 struct bpf_prog *prog;
9673
9674 if (event->overflow_handler_context)
9675 /* hw breakpoint or kernel counter */
9676 return -EINVAL;
9677
9678 if (event->prog)
9679 return -EEXIST;
9680
9681 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
9682 if (IS_ERR(prog))
9683 return PTR_ERR(prog);
9684
Song Liu5d99cb2c2020-07-23 11:06:45 -07009685 if (event->attr.precise_ip &&
9686 prog->call_get_stack &&
9687 (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY) ||
9688 event->attr.exclude_callchain_kernel ||
9689 event->attr.exclude_callchain_user)) {
9690 /*
9691 * On perf_event with precise_ip, calling bpf_get_stack()
9692 * may trigger unwinder warnings and occasional crashes.
9693 * bpf_get_[stack|stackid] works around this issue by using
9694 * callchain attached to perf_sample_data. If the
9695 * perf_event does not full (kernel and user) callchain
9696 * attached to perf_sample_data, do not allow attaching BPF
9697 * program that calls bpf_get_[stack|stackid].
9698 */
9699 bpf_prog_put(prog);
9700 return -EPROTO;
9701 }
9702
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009703 event->prog = prog;
9704 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
9705 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
9706 return 0;
9707}
9708
9709static void perf_event_free_bpf_handler(struct perf_event *event)
9710{
9711 struct bpf_prog *prog = event->prog;
9712
9713 if (!prog)
9714 return;
9715
9716 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
9717 event->prog = NULL;
9718 bpf_prog_put(prog);
9719}
9720#else
9721static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9722{
9723 return -EOPNOTSUPP;
9724}
9725static void perf_event_free_bpf_handler(struct perf_event *event)
9726{
9727}
9728#endif
9729
Song Liue12f03d2017-12-06 14:45:15 -08009730/*
9731 * returns true if the event is a tracepoint, or a kprobe/upprobe created
9732 * with perf_event_open()
9733 */
9734static inline bool perf_event_is_tracing(struct perf_event *event)
9735{
9736 if (event->pmu == &perf_tracepoint)
9737 return true;
9738#ifdef CONFIG_KPROBE_EVENTS
9739 if (event->pmu == &perf_kprobe)
9740 return true;
9741#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009742#ifdef CONFIG_UPROBE_EVENTS
9743 if (event->pmu == &perf_uprobe)
9744 return true;
9745#endif
Song Liue12f03d2017-12-06 14:45:15 -08009746 return false;
9747}
9748
Alexei Starovoitov25415172015-03-25 12:49:20 -07009749static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9750{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009751 bool is_kprobe, is_tracepoint, is_syscall_tp;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009752 struct bpf_prog *prog;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009753 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009754
Song Liue12f03d2017-12-06 14:45:15 -08009755 if (!perf_event_is_tracing(event))
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07009756 return perf_event_set_bpf_handler(event, prog_fd);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009757
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009758 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
9759 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009760 is_syscall_tp = is_syscall_trace_event(event->tp_event);
9761 if (!is_kprobe && !is_tracepoint && !is_syscall_tp)
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009762 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07009763 return -EINVAL;
9764
9765 prog = bpf_prog_get(prog_fd);
9766 if (IS_ERR(prog))
9767 return PTR_ERR(prog);
9768
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009769 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009770 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
9771 (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07009772 /* valid fd, but invalid bpf program type */
9773 bpf_prog_put(prog);
9774 return -EINVAL;
9775 }
9776
Josef Bacik9802d862017-12-11 11:36:48 -05009777 /* Kprobe override only works for kprobes, not uprobes. */
9778 if (prog->kprobe_override &&
9779 !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) {
9780 bpf_prog_put(prog);
9781 return -EINVAL;
9782 }
9783
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009784 if (is_tracepoint || is_syscall_tp) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07009785 int off = trace_event_get_offsets(event->tp_event);
9786
9787 if (prog->aux->max_ctx_offset > off) {
9788 bpf_prog_put(prog);
9789 return -EACCES;
9790 }
9791 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07009792
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009793 ret = perf_event_attach_bpf_prog(event, prog);
9794 if (ret)
9795 bpf_prog_put(prog);
9796 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009797}
9798
9799static void perf_event_free_bpf_prog(struct perf_event *event)
9800{
Song Liue12f03d2017-12-06 14:45:15 -08009801 if (!perf_event_is_tracing(event)) {
Yonghong Song0b4c6842017-10-23 23:53:07 -07009802 perf_event_free_bpf_handler(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009803 return;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009804 }
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009805 perf_event_detach_bpf_prog(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009806}
9807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009808#else
Li Zefan6fb29152009-10-15 11:21:42 +08009809
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009810static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009811{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009812}
Li Zefan6fb29152009-10-15 11:21:42 +08009813
Li Zefan6fb29152009-10-15 11:21:42 +08009814static void perf_event_free_filter(struct perf_event *event)
9815{
9816}
9817
Alexei Starovoitov25415172015-03-25 12:49:20 -07009818static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9819{
9820 return -ENOENT;
9821}
9822
9823static void perf_event_free_bpf_prog(struct perf_event *event)
9824{
9825}
Li Zefan07b139c2009-12-21 14:27:35 +08009826#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009827
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009828#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009829void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009830{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009831 struct perf_sample_data sample;
9832 struct pt_regs *regs = data;
9833
Robert Richterfd0d0002012-04-02 20:19:08 +02009834 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009835
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009836 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009837 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009838}
9839#endif
9840
Alexander Shishkin375637b2016-04-27 18:44:46 +03009841/*
9842 * Allocate a new address filter
9843 */
9844static struct perf_addr_filter *
9845perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
9846{
9847 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
9848 struct perf_addr_filter *filter;
9849
9850 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
9851 if (!filter)
9852 return NULL;
9853
9854 INIT_LIST_HEAD(&filter->entry);
9855 list_add_tail(&filter->entry, filters);
9856
9857 return filter;
9858}
9859
9860static void free_filters_list(struct list_head *filters)
9861{
9862 struct perf_addr_filter *filter, *iter;
9863
9864 list_for_each_entry_safe(filter, iter, filters, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07009865 path_put(&filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009866 list_del(&filter->entry);
9867 kfree(filter);
9868 }
9869}
9870
9871/*
9872 * Free existing address filters and optionally install new ones
9873 */
9874static void perf_addr_filters_splice(struct perf_event *event,
9875 struct list_head *head)
9876{
9877 unsigned long flags;
9878 LIST_HEAD(list);
9879
9880 if (!has_addr_filter(event))
9881 return;
9882
9883 /* don't bother with children, they don't have their own filters */
9884 if (event->parent)
9885 return;
9886
9887 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
9888
9889 list_splice_init(&event->addr_filters.list, &list);
9890 if (head)
9891 list_splice(head, &event->addr_filters.list);
9892
9893 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
9894
9895 free_filters_list(&list);
9896}
9897
9898/*
9899 * Scan through mm's vmas and see if one of them matches the
9900 * @filter; if so, adjust filter's address range.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07009901 * Called with mm::mmap_lock down for reading.
Alexander Shishkin375637b2016-04-27 18:44:46 +03009902 */
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009903static void perf_addr_filter_apply(struct perf_addr_filter *filter,
9904 struct mm_struct *mm,
9905 struct perf_addr_filter_range *fr)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009906{
9907 struct vm_area_struct *vma;
9908
9909 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009910 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009911 continue;
9912
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009913 if (perf_addr_filter_vma_adjust(filter, vma, fr))
9914 return;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009915 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009916}
9917
9918/*
9919 * Update event's address range filters based on the
9920 * task's existing mappings, if any.
9921 */
9922static void perf_event_addr_filters_apply(struct perf_event *event)
9923{
9924 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
9925 struct task_struct *task = READ_ONCE(event->ctx->task);
9926 struct perf_addr_filter *filter;
9927 struct mm_struct *mm = NULL;
9928 unsigned int count = 0;
9929 unsigned long flags;
9930
9931 /*
9932 * We may observe TASK_TOMBSTONE, which means that the event tear-down
9933 * will stop on the parent's child_mutex that our caller is also holding
9934 */
9935 if (task == TASK_TOMBSTONE)
9936 return;
9937
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009938 if (ifh->nr_file_filters) {
9939 mm = get_task_mm(event->ctx->task);
9940 if (!mm)
9941 goto restart;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009942
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07009943 mmap_read_lock(mm);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009944 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009945
9946 raw_spin_lock_irqsave(&ifh->lock, flags);
9947 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009948 if (filter->path.dentry) {
9949 /*
9950 * Adjust base offset if the filter is associated to a
9951 * binary that needs to be mapped:
9952 */
9953 event->addr_filter_ranges[count].start = 0;
9954 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009955
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009956 perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009957 } else {
9958 event->addr_filter_ranges[count].start = filter->offset;
9959 event->addr_filter_ranges[count].size = filter->size;
9960 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009961
9962 count++;
9963 }
9964
9965 event->addr_filters_gen++;
9966 raw_spin_unlock_irqrestore(&ifh->lock, flags);
9967
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009968 if (ifh->nr_file_filters) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07009969 mmap_read_unlock(mm);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009970
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009971 mmput(mm);
9972 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009973
9974restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03009975 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009976}
9977
9978/*
9979 * Address range filtering: limiting the data to certain
9980 * instruction address ranges. Filters are ioctl()ed to us from
9981 * userspace as ascii strings.
9982 *
9983 * Filter string format:
9984 *
9985 * ACTION RANGE_SPEC
9986 * where ACTION is one of the
9987 * * "filter": limit the trace to this region
9988 * * "start": start tracing from this address
9989 * * "stop": stop tracing at this address/region;
9990 * RANGE_SPEC is
9991 * * for kernel addresses: <start address>[/<size>]
9992 * * for object files: <start address>[/<size>]@</path/to/object/file>
9993 *
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009994 * if <size> is not specified or is zero, the range is treated as a single
9995 * address; not valid for ACTION=="filter".
Alexander Shishkin375637b2016-04-27 18:44:46 +03009996 */
9997enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02009998 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03009999 IF_ACT_FILTER,
10000 IF_ACT_START,
10001 IF_ACT_STOP,
10002 IF_SRC_FILE,
10003 IF_SRC_KERNEL,
10004 IF_SRC_FILEADDR,
10005 IF_SRC_KERNELADDR,
10006};
10007
10008enum {
10009 IF_STATE_ACTION = 0,
10010 IF_STATE_SOURCE,
10011 IF_STATE_END,
10012};
10013
10014static const match_table_t if_tokens = {
10015 { IF_ACT_FILTER, "filter" },
10016 { IF_ACT_START, "start" },
10017 { IF_ACT_STOP, "stop" },
10018 { IF_SRC_FILE, "%u/%u@%s" },
10019 { IF_SRC_KERNEL, "%u/%u" },
10020 { IF_SRC_FILEADDR, "%u@%s" },
10021 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +020010022 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +030010023};
10024
10025/*
10026 * Address filter string parser
10027 */
10028static int
10029perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
10030 struct list_head *filters)
10031{
10032 struct perf_addr_filter *filter = NULL;
10033 char *start, *orig, *filename = NULL;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010034 substring_t args[MAX_OPT_ARGS];
10035 int state = IF_STATE_ACTION, token;
10036 unsigned int kernel = 0;
10037 int ret = -EINVAL;
10038
10039 orig = fstr = kstrdup(fstr, GFP_KERNEL);
10040 if (!fstr)
10041 return -ENOMEM;
10042
10043 while ((start = strsep(&fstr, " ,\n")) != NULL) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010044 static const enum perf_addr_filter_action_t actions[] = {
10045 [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER,
10046 [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START,
10047 [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP,
10048 };
Alexander Shishkin375637b2016-04-27 18:44:46 +030010049 ret = -EINVAL;
10050
10051 if (!*start)
10052 continue;
10053
10054 /* filter definition begins */
10055 if (state == IF_STATE_ACTION) {
10056 filter = perf_addr_filter_new(event, filters);
10057 if (!filter)
10058 goto fail;
10059 }
10060
10061 token = match_token(start, if_tokens, args);
10062 switch (token) {
10063 case IF_ACT_FILTER:
10064 case IF_ACT_START:
Alexander Shishkin375637b2016-04-27 18:44:46 +030010065 case IF_ACT_STOP:
10066 if (state != IF_STATE_ACTION)
10067 goto fail;
10068
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010069 filter->action = actions[token];
Alexander Shishkin375637b2016-04-27 18:44:46 +030010070 state = IF_STATE_SOURCE;
10071 break;
10072
10073 case IF_SRC_KERNELADDR:
10074 case IF_SRC_KERNEL:
10075 kernel = 1;
Gustavo A. R. Silva10c34052019-02-12 14:54:30 -060010076 /* fall through */
Alexander Shishkin375637b2016-04-27 18:44:46 +030010077
10078 case IF_SRC_FILEADDR:
10079 case IF_SRC_FILE:
10080 if (state != IF_STATE_SOURCE)
10081 goto fail;
10082
Alexander Shishkin375637b2016-04-27 18:44:46 +030010083 *args[0].to = 0;
10084 ret = kstrtoul(args[0].from, 0, &filter->offset);
10085 if (ret)
10086 goto fail;
10087
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010088 if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) {
Alexander Shishkin375637b2016-04-27 18:44:46 +030010089 *args[1].to = 0;
10090 ret = kstrtoul(args[1].from, 0, &filter->size);
10091 if (ret)
10092 goto fail;
10093 }
10094
Mathieu Poirier4059ffd2016-07-18 10:43:05 -060010095 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010096 int fpos = token == IF_SRC_FILE ? 2 : 1;
Mathieu Poirier4059ffd2016-07-18 10:43:05 -060010097
10098 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010099 if (!filename) {
10100 ret = -ENOMEM;
10101 goto fail;
10102 }
10103 }
10104
10105 state = IF_STATE_END;
10106 break;
10107
10108 default:
10109 goto fail;
10110 }
10111
10112 /*
10113 * Filter definition is fully parsed, validate and install it.
10114 * Make sure that it doesn't contradict itself or the event's
10115 * attribute.
10116 */
10117 if (state == IF_STATE_END) {
Alexander Shishkin9ccbfbb2017-01-26 11:40:56 +020010118 ret = -EINVAL;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010119 if (kernel && event->attr.exclude_kernel)
10120 goto fail;
10121
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010122 /*
10123 * ACTION "filter" must have a non-zero length region
10124 * specified.
10125 */
10126 if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER &&
10127 !filter->size)
10128 goto fail;
10129
Alexander Shishkin375637b2016-04-27 18:44:46 +030010130 if (!kernel) {
10131 if (!filename)
10132 goto fail;
10133
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010134 /*
10135 * For now, we only support file-based filters
10136 * in per-task events; doing so for CPU-wide
10137 * events requires additional context switching
10138 * trickery, since same object code will be
10139 * mapped at different virtual addresses in
10140 * different processes.
10141 */
10142 ret = -EOPNOTSUPP;
10143 if (!event->ctx->task)
10144 goto fail_free_name;
10145
Alexander Shishkin375637b2016-04-27 18:44:46 +030010146 /* look up the path and grab its inode */
Song Liu9511bce2018-04-17 23:29:07 -070010147 ret = kern_path(filename, LOOKUP_FOLLOW,
10148 &filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010149 if (ret)
10150 goto fail_free_name;
10151
Alexander Shishkin375637b2016-04-27 18:44:46 +030010152 kfree(filename);
10153 filename = NULL;
10154
10155 ret = -EINVAL;
Song Liu9511bce2018-04-17 23:29:07 -070010156 if (!filter->path.dentry ||
10157 !S_ISREG(d_inode(filter->path.dentry)
10158 ->i_mode))
Alexander Shishkin375637b2016-04-27 18:44:46 +030010159 goto fail;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010160
10161 event->addr_filters.nr_file_filters++;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010162 }
10163
10164 /* ready to consume more filters */
10165 state = IF_STATE_ACTION;
10166 filter = NULL;
10167 }
10168 }
10169
10170 if (state != IF_STATE_ACTION)
10171 goto fail;
10172
10173 kfree(orig);
10174
10175 return 0;
10176
10177fail_free_name:
10178 kfree(filename);
10179fail:
10180 free_filters_list(filters);
10181 kfree(orig);
10182
10183 return ret;
10184}
10185
10186static int
10187perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
10188{
10189 LIST_HEAD(filters);
10190 int ret;
10191
10192 /*
10193 * Since this is called in perf_ioctl() path, we're already holding
10194 * ctx::mutex.
10195 */
10196 lockdep_assert_held(&event->ctx->mutex);
10197
10198 if (WARN_ON_ONCE(event->parent))
10199 return -EINVAL;
10200
Alexander Shishkin375637b2016-04-27 18:44:46 +030010201 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
10202 if (ret)
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010203 goto fail_clear_files;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010204
10205 ret = event->pmu->addr_filters_validate(&filters);
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010206 if (ret)
10207 goto fail_free_filters;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010208
10209 /* remove existing filters, if any */
10210 perf_addr_filters_splice(event, &filters);
10211
10212 /* install new filters */
10213 perf_event_for_each_child(event, perf_event_addr_filters_apply);
10214
10215 return ret;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010216
10217fail_free_filters:
10218 free_filters_list(&filters);
10219
10220fail_clear_files:
10221 event->addr_filters.nr_file_filters = 0;
10222
10223 return ret;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010224}
10225
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010226static int perf_event_set_filter(struct perf_event *event, void __user *arg)
10227{
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010228 int ret = -EINVAL;
Song Liue12f03d2017-12-06 14:45:15 -080010229 char *filter_str;
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010230
10231 filter_str = strndup_user(arg, PAGE_SIZE);
10232 if (IS_ERR(filter_str))
10233 return PTR_ERR(filter_str);
10234
Song Liue12f03d2017-12-06 14:45:15 -080010235#ifdef CONFIG_EVENT_TRACING
10236 if (perf_event_is_tracing(event)) {
10237 struct perf_event_context *ctx = event->ctx;
10238
10239 /*
10240 * Beware, here be dragons!!
10241 *
10242 * the tracepoint muck will deadlock against ctx->mutex, but
10243 * the tracepoint stuff does not actually need it. So
10244 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
10245 * already have a reference on ctx.
10246 *
10247 * This can result in event getting moved to a different ctx,
10248 * but that does not affect the tracepoint state.
10249 */
10250 mutex_unlock(&ctx->mutex);
10251 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
10252 mutex_lock(&ctx->mutex);
10253 } else
10254#endif
10255 if (has_addr_filter(event))
Alexander Shishkin375637b2016-04-27 18:44:46 +030010256 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010257
10258 kfree(filter_str);
10259 return ret;
10260}
10261
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010262/*
10263 * hrtimer based swevent callback
10264 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010265
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010266static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010267{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010268 enum hrtimer_restart ret = HRTIMER_RESTART;
10269 struct perf_sample_data data;
10270 struct pt_regs *regs;
10271 struct perf_event *event;
10272 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010273
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010274 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010275
10276 if (event->state != PERF_EVENT_STATE_ACTIVE)
10277 return HRTIMER_NORESTART;
10278
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010279 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010280
Robert Richterfd0d0002012-04-02 20:19:08 +020010281 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010282 regs = get_irq_regs();
10283
10284 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -080010285 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +020010286 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010287 ret = HRTIMER_NORESTART;
10288 }
10289
10290 period = max_t(u64, 10000, event->hw.sample_period);
10291 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
10292
10293 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010294}
10295
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010296static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010297{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010298 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010299 s64 period;
10300
10301 if (!is_sampling_event(event))
10302 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010303
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010304 period = local64_read(&hwc->period_left);
10305 if (period) {
10306 if (period < 0)
10307 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +020010308
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010309 local64_set(&hwc->period_left, 0);
10310 } else {
10311 period = max_t(u64, 10000, hwc->sample_period);
10312 }
Thomas Gleixner3497d202015-04-14 21:09:03 +000010313 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +020010314 HRTIMER_MODE_REL_PINNED_HARD);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010315}
10316
10317static void perf_swevent_cancel_hrtimer(struct perf_event *event)
10318{
10319 struct hw_perf_event *hwc = &event->hw;
10320
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +010010321 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010322 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +020010323 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010324
10325 hrtimer_cancel(&hwc->hrtimer);
10326 }
10327}
10328
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010329static void perf_swevent_init_hrtimer(struct perf_event *event)
10330{
10331 struct hw_perf_event *hwc = &event->hw;
10332
10333 if (!is_sampling_event(event))
10334 return;
10335
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +020010336 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010337 hwc->hrtimer.function = perf_swevent_hrtimer;
10338
10339 /*
10340 * Since hrtimers have a fixed rate, we can do a static freq->period
10341 * mapping and avoid the whole period adjust feedback stuff.
10342 */
10343 if (event->attr.freq) {
10344 long freq = event->attr.sample_freq;
10345
10346 event->attr.sample_period = NSEC_PER_SEC / freq;
10347 hwc->sample_period = event->attr.sample_period;
10348 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +090010349 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010350 event->attr.freq = 0;
10351 }
10352}
10353
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010354/*
10355 * Software event: cpu wall time clock
10356 */
10357
10358static void cpu_clock_event_update(struct perf_event *event)
10359{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010360 s64 prev;
10361 u64 now;
10362
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010363 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010364 prev = local64_xchg(&event->hw.prev_count, now);
10365 local64_add(now - prev, &event->count);
10366}
10367
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010368static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010369{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010370 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010371 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010372}
10373
10374static void cpu_clock_event_stop(struct perf_event *event, int flags)
10375{
10376 perf_swevent_cancel_hrtimer(event);
10377 cpu_clock_event_update(event);
10378}
10379
10380static int cpu_clock_event_add(struct perf_event *event, int flags)
10381{
10382 if (flags & PERF_EF_START)
10383 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -080010384 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010385
10386 return 0;
10387}
10388
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010389static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010390{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010391 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010392}
10393
10394static void cpu_clock_event_read(struct perf_event *event)
10395{
10396 cpu_clock_event_update(event);
10397}
10398
10399static int cpu_clock_event_init(struct perf_event *event)
10400{
10401 if (event->attr.type != PERF_TYPE_SOFTWARE)
10402 return -ENOENT;
10403
10404 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
10405 return -ENOENT;
10406
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010407 /*
10408 * no branch sampling for software events
10409 */
10410 if (has_branch_stack(event))
10411 return -EOPNOTSUPP;
10412
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010413 perf_swevent_init_hrtimer(event);
10414
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010415 return 0;
10416}
10417
10418static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010419 .task_ctx_nr = perf_sw_context,
10420
Peter Zijlstra34f43922015-02-20 14:05:38 +010010421 .capabilities = PERF_PMU_CAP_NO_NMI,
10422
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010423 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010424 .add = cpu_clock_event_add,
10425 .del = cpu_clock_event_del,
10426 .start = cpu_clock_event_start,
10427 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010428 .read = cpu_clock_event_read,
10429};
10430
10431/*
10432 * Software event: task time clock
10433 */
10434
10435static void task_clock_event_update(struct perf_event *event, u64 now)
10436{
10437 u64 prev;
10438 s64 delta;
10439
10440 prev = local64_xchg(&event->hw.prev_count, now);
10441 delta = now - prev;
10442 local64_add(delta, &event->count);
10443}
10444
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010445static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010446{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010447 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010448 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010449}
10450
10451static void task_clock_event_stop(struct perf_event *event, int flags)
10452{
10453 perf_swevent_cancel_hrtimer(event);
10454 task_clock_event_update(event, event->ctx->time);
10455}
10456
10457static int task_clock_event_add(struct perf_event *event, int flags)
10458{
10459 if (flags & PERF_EF_START)
10460 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -080010461 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010462
10463 return 0;
10464}
10465
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010466static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010467{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010468 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010469}
10470
10471static void task_clock_event_read(struct perf_event *event)
10472{
Peter Zijlstra768a06e2011-02-22 16:52:24 +010010473 u64 now = perf_clock();
10474 u64 delta = now - event->ctx->timestamp;
10475 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010476
10477 task_clock_event_update(event, time);
10478}
10479
10480static int task_clock_event_init(struct perf_event *event)
10481{
10482 if (event->attr.type != PERF_TYPE_SOFTWARE)
10483 return -ENOENT;
10484
10485 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
10486 return -ENOENT;
10487
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010488 /*
10489 * no branch sampling for software events
10490 */
10491 if (has_branch_stack(event))
10492 return -EOPNOTSUPP;
10493
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010494 perf_swevent_init_hrtimer(event);
10495
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010496 return 0;
10497}
10498
10499static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010500 .task_ctx_nr = perf_sw_context,
10501
Peter Zijlstra34f43922015-02-20 14:05:38 +010010502 .capabilities = PERF_PMU_CAP_NO_NMI,
10503
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010504 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010505 .add = task_clock_event_add,
10506 .del = task_clock_event_del,
10507 .start = task_clock_event_start,
10508 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010509 .read = task_clock_event_read,
10510};
10511
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010512static void perf_pmu_nop_void(struct pmu *pmu)
10513{
10514}
10515
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010516static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
10517{
10518}
10519
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010520static int perf_pmu_nop_int(struct pmu *pmu)
10521{
10522 return 0;
10523}
10524
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010525static int perf_event_nop_int(struct perf_event *event, u64 value)
10526{
10527 return 0;
10528}
10529
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010530static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010531
10532static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010533{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010534 __this_cpu_write(nop_txn_flags, flags);
10535
10536 if (flags & ~PERF_PMU_TXN_ADD)
10537 return;
10538
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010539 perf_pmu_disable(pmu);
10540}
10541
10542static int perf_pmu_commit_txn(struct pmu *pmu)
10543{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010544 unsigned int flags = __this_cpu_read(nop_txn_flags);
10545
10546 __this_cpu_write(nop_txn_flags, 0);
10547
10548 if (flags & ~PERF_PMU_TXN_ADD)
10549 return 0;
10550
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010551 perf_pmu_enable(pmu);
10552 return 0;
10553}
10554
10555static void perf_pmu_cancel_txn(struct pmu *pmu)
10556{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010557 unsigned int flags = __this_cpu_read(nop_txn_flags);
10558
10559 __this_cpu_write(nop_txn_flags, 0);
10560
10561 if (flags & ~PERF_PMU_TXN_ADD)
10562 return;
10563
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010564 perf_pmu_enable(pmu);
10565}
10566
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010567static int perf_event_idx_default(struct perf_event *event)
10568{
Peter Zijlstrac719f562014-10-21 11:10:21 +020010569 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010570}
10571
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010572/*
10573 * Ensures all contexts with the same task_ctx_nr have the same
10574 * pmu_cpu_context too.
10575 */
Mark Rutland9e317042014-02-10 17:44:18 +000010576static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010577{
10578 struct pmu *pmu;
10579
10580 if (ctxn < 0)
10581 return NULL;
10582
10583 list_for_each_entry(pmu, &pmus, entry) {
10584 if (pmu->task_ctx_nr == ctxn)
10585 return pmu->pmu_cpu_context;
10586 }
10587
10588 return NULL;
10589}
10590
Peter Zijlstra51676952010-12-07 14:18:20 +010010591static void free_pmu_context(struct pmu *pmu)
10592{
Will Deacondf0062b2017-10-03 15:20:50 +010010593 /*
10594 * Static contexts such as perf_sw_context have a global lifetime
10595 * and may be shared between different PMUs. Avoid freeing them
10596 * when a single PMU is going away.
10597 */
10598 if (pmu->task_ctx_nr > perf_invalid_context)
10599 return;
10600
Peter Zijlstra51676952010-12-07 14:18:20 +010010601 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010602}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010603
10604/*
10605 * Let userspace know that this PMU supports address range filtering:
10606 */
10607static ssize_t nr_addr_filters_show(struct device *dev,
10608 struct device_attribute *attr,
10609 char *page)
10610{
10611 struct pmu *pmu = dev_get_drvdata(dev);
10612
10613 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
10614}
10615DEVICE_ATTR_RO(nr_addr_filters);
10616
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010617static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010618
Peter Zijlstraabe43402010-11-17 23:17:37 +010010619static ssize_t
10620type_show(struct device *dev, struct device_attribute *attr, char *page)
10621{
10622 struct pmu *pmu = dev_get_drvdata(dev);
10623
10624 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
10625}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010626static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010627
Stephane Eranian62b85632013-04-03 14:21:34 +020010628static ssize_t
10629perf_event_mux_interval_ms_show(struct device *dev,
10630 struct device_attribute *attr,
10631 char *page)
10632{
10633 struct pmu *pmu = dev_get_drvdata(dev);
10634
10635 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
10636}
10637
Peter Zijlstra272325c2015-04-15 11:41:58 +020010638static DEFINE_MUTEX(mux_interval_mutex);
10639
Stephane Eranian62b85632013-04-03 14:21:34 +020010640static ssize_t
10641perf_event_mux_interval_ms_store(struct device *dev,
10642 struct device_attribute *attr,
10643 const char *buf, size_t count)
10644{
10645 struct pmu *pmu = dev_get_drvdata(dev);
10646 int timer, cpu, ret;
10647
10648 ret = kstrtoint(buf, 0, &timer);
10649 if (ret)
10650 return ret;
10651
10652 if (timer < 1)
10653 return -EINVAL;
10654
10655 /* same value, noting to do */
10656 if (timer == pmu->hrtimer_interval_ms)
10657 return count;
10658
Peter Zijlstra272325c2015-04-15 11:41:58 +020010659 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010660 pmu->hrtimer_interval_ms = timer;
10661
10662 /* update all cpuctx for this PMU */
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010663 cpus_read_lock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010664 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +020010665 struct perf_cpu_context *cpuctx;
10666 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
10667 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
10668
Peter Zijlstra272325c2015-04-15 11:41:58 +020010669 cpu_function_call(cpu,
10670 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +020010671 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010672 cpus_read_unlock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010673 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010674
10675 return count;
10676}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010677static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +020010678
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010679static struct attribute *pmu_dev_attrs[] = {
10680 &dev_attr_type.attr,
10681 &dev_attr_perf_event_mux_interval_ms.attr,
10682 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010683};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010684ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010685
10686static int pmu_bus_running;
10687static struct bus_type pmu_bus = {
10688 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010689 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010690};
10691
10692static void pmu_dev_release(struct device *dev)
10693{
10694 kfree(dev);
10695}
10696
10697static int pmu_dev_alloc(struct pmu *pmu)
10698{
10699 int ret = -ENOMEM;
10700
10701 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
10702 if (!pmu->dev)
10703 goto out;
10704
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +010010705 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +010010706 device_initialize(pmu->dev);
10707 ret = dev_set_name(pmu->dev, "%s", pmu->name);
10708 if (ret)
10709 goto free_dev;
10710
10711 dev_set_drvdata(pmu->dev, pmu);
10712 pmu->dev->bus = &pmu_bus;
10713 pmu->dev->release = pmu_dev_release;
10714 ret = device_add(pmu->dev);
10715 if (ret)
10716 goto free_dev;
10717
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010718 /* For PMUs with address filters, throw in an extra attribute: */
10719 if (pmu->nr_addr_filters)
10720 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
10721
10722 if (ret)
10723 goto del_dev;
10724
Jiri Olsaf3a3a822019-05-12 17:55:11 +020010725 if (pmu->attr_update)
10726 ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update);
10727
10728 if (ret)
10729 goto del_dev;
10730
Peter Zijlstraabe43402010-11-17 23:17:37 +010010731out:
10732 return ret;
10733
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010734del_dev:
10735 device_del(pmu->dev);
10736
Peter Zijlstraabe43402010-11-17 23:17:37 +010010737free_dev:
10738 put_device(pmu->dev);
10739 goto out;
10740}
10741
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010742static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010743static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010744
Mischa Jonker03d8e802013-06-04 11:45:48 +020010745int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010746{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010747 int cpu, ret, max = PERF_TYPE_MAX;
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010748
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010749 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010750 ret = -ENOMEM;
10751 pmu->pmu_disable_count = alloc_percpu(int);
10752 if (!pmu->pmu_disable_count)
10753 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010754
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010755 pmu->type = -1;
10756 if (!name)
10757 goto skip_type;
10758 pmu->name = name;
10759
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010760 if (type != PERF_TYPE_SOFTWARE) {
10761 if (type >= 0)
10762 max = type;
10763
10764 ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
10765 if (ret < 0)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010766 goto free_pdc;
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010767
10768 WARN_ON(type >= 0 && ret != type);
10769
10770 type = ret;
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010771 }
10772 pmu->type = type;
10773
Peter Zijlstraabe43402010-11-17 23:17:37 +010010774 if (pmu_bus_running) {
10775 ret = pmu_dev_alloc(pmu);
10776 if (ret)
10777 goto free_idr;
10778 }
10779
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010780skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +010010781 if (pmu->task_ctx_nr == perf_hw_context) {
10782 static int hw_context_taken = 0;
10783
Mark Rutland5101ef22016-04-26 11:33:46 +010010784 /*
10785 * Other than systems with heterogeneous CPUs, it never makes
10786 * sense for two PMUs to share perf_hw_context. PMUs which are
10787 * uncore must use perf_invalid_context.
10788 */
10789 if (WARN_ON_ONCE(hw_context_taken &&
10790 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +010010791 pmu->task_ctx_nr = perf_invalid_context;
10792
10793 hw_context_taken = 1;
10794 }
10795
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010796 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
10797 if (pmu->pmu_cpu_context)
10798 goto got_cpu_context;
10799
Wei Yongjunc4814202013-04-12 11:05:54 +080010800 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010801 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
10802 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +010010803 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010804
10805 for_each_possible_cpu(cpu) {
10806 struct perf_cpu_context *cpuctx;
10807
10808 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +020010809 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010810 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010811 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010812 cpuctx->ctx.pmu = pmu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010813 cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);
Stephane Eranian9e630202013-04-03 14:21:33 +020010814
Peter Zijlstra272325c2015-04-15 11:41:58 +020010815 __perf_mux_hrtimer_init(cpuctx, cpu);
Ian Rogers836196be2020-02-13 23:51:31 -080010816
10817 cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default);
10818 cpuctx->heap = cpuctx->heap_default;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010819 }
10820
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010821got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010822 if (!pmu->start_txn) {
10823 if (pmu->pmu_enable) {
10824 /*
10825 * If we have pmu_enable/pmu_disable calls, install
10826 * transaction stubs that use that to try and batch
10827 * hardware accesses.
10828 */
10829 pmu->start_txn = perf_pmu_start_txn;
10830 pmu->commit_txn = perf_pmu_commit_txn;
10831 pmu->cancel_txn = perf_pmu_cancel_txn;
10832 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010833 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010834 pmu->commit_txn = perf_pmu_nop_int;
10835 pmu->cancel_txn = perf_pmu_nop_void;
10836 }
10837 }
10838
10839 if (!pmu->pmu_enable) {
10840 pmu->pmu_enable = perf_pmu_nop_void;
10841 pmu->pmu_disable = perf_pmu_nop_void;
10842 }
10843
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010844 if (!pmu->check_period)
10845 pmu->check_period = perf_event_nop_int;
10846
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010847 if (!pmu->event_idx)
10848 pmu->event_idx = perf_event_idx_default;
10849
Liang, Kand44f8212019-10-22 11:13:09 +020010850 /*
10851 * Ensure the TYPE_SOFTWARE PMUs are at the head of the list,
10852 * since these cannot be in the IDR. This way the linear search
10853 * is fast, provided a valid software event is provided.
10854 */
10855 if (type == PERF_TYPE_SOFTWARE || !name)
10856 list_add_rcu(&pmu->entry, &pmus);
10857 else
10858 list_add_tail_rcu(&pmu->entry, &pmus);
10859
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010860 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010861 ret = 0;
10862unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010863 mutex_unlock(&pmus_lock);
10864
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010865 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010866
Peter Zijlstraabe43402010-11-17 23:17:37 +010010867free_dev:
10868 device_del(pmu->dev);
10869 put_device(pmu->dev);
10870
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010871free_idr:
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010872 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010873 idr_remove(&pmu_idr, pmu->type);
10874
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010875free_pdc:
10876 free_percpu(pmu->pmu_disable_count);
10877 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010878}
Yan, Zhengc464c762014-03-18 16:56:41 +080010879EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010880
10881void perf_pmu_unregister(struct pmu *pmu)
10882{
10883 mutex_lock(&pmus_lock);
10884 list_del_rcu(&pmu->entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010885
10886 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +020010887 * We dereference the pmu list under both SRCU and regular RCU, so
10888 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010889 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010890 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +020010891 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010892
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010893 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010894 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010895 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010896 if (pmu_bus_running) {
Jiri Olsa09338402016-10-20 13:10:11 +020010897 if (pmu->nr_addr_filters)
10898 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
10899 device_del(pmu->dev);
10900 put_device(pmu->dev);
10901 }
Peter Zijlstra51676952010-12-07 14:18:20 +010010902 free_pmu_context(pmu);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010903 mutex_unlock(&pmus_lock);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010904}
Yan, Zhengc464c762014-03-18 16:56:41 +080010905EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010906
Kan Liange321d022019-05-28 15:08:30 -070010907static inline bool has_extended_regs(struct perf_event *event)
10908{
10909 return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) ||
10910 (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK);
10911}
10912
Mark Rutlandcc34b982015-01-07 14:56:51 +000010913static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
10914{
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010915 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +000010916 int ret;
10917
10918 if (!try_module_get(pmu->module))
10919 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010920
Peter Zijlstra0c7296c2018-01-09 21:23:02 +010010921 /*
10922 * A number of pmu->event_init() methods iterate the sibling_list to,
10923 * for example, validate if the group fits on the PMU. Therefore,
10924 * if this is a sibling event, acquire the ctx->mutex to protect
10925 * the sibling_list.
10926 */
10927 if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +020010928 /*
10929 * This ctx->mutex can nest when we're called through
10930 * inheritance. See the perf_event_ctx_lock_nested() comment.
10931 */
10932 ctx = perf_event_ctx_lock_nested(event->group_leader,
10933 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010934 BUG_ON(!ctx);
10935 }
10936
Mark Rutlandcc34b982015-01-07 14:56:51 +000010937 event->pmu = pmu;
10938 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010939
10940 if (ctx)
10941 perf_event_ctx_unlock(event->group_leader, ctx);
10942
Andrew Murraycc6795a2019-01-10 13:53:25 +000010943 if (!ret) {
Kan Liange321d022019-05-28 15:08:30 -070010944 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
10945 has_extended_regs(event))
10946 ret = -EOPNOTSUPP;
10947
Andrew Murraycc6795a2019-01-10 13:53:25 +000010948 if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
Kan Liange321d022019-05-28 15:08:30 -070010949 event_has_any_exclude_flag(event))
Andrew Murraycc6795a2019-01-10 13:53:25 +000010950 ret = -EINVAL;
Kan Liange321d022019-05-28 15:08:30 -070010951
10952 if (ret && event->destroy)
10953 event->destroy(event);
Andrew Murraycc6795a2019-01-10 13:53:25 +000010954 }
10955
Mark Rutlandcc34b982015-01-07 14:56:51 +000010956 if (ret)
10957 module_put(pmu->module);
10958
10959 return ret;
10960}
10961
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010962static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010963{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010964 int idx, type, ret;
Dan Carpenter85c617a2017-05-22 12:03:49 +030010965 struct pmu *pmu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010966
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010967 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010968
Kan Liang40999312017-01-18 08:21:01 -050010969 /* Try parent's PMU first: */
10970 if (event->parent && event->parent->pmu) {
10971 pmu = event->parent->pmu;
10972 ret = perf_try_init_event(pmu, event);
10973 if (!ret)
10974 goto unlock;
10975 }
10976
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010977 /*
10978 * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
10979 * are often aliases for PERF_TYPE_RAW.
10980 */
10981 type = event->attr.type;
10982 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
10983 type = PERF_TYPE_RAW;
10984
10985again:
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010986 rcu_read_lock();
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010987 pmu = idr_find(&pmu_idr, type);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010988 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +080010989 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010990 ret = perf_try_init_event(pmu, event);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010991 if (ret == -ENOENT && event->attr.type != type) {
10992 type = event->attr.type;
10993 goto again;
10994 }
10995
Lin Ming940c5b22011-02-27 21:13:31 +080010996 if (ret)
10997 pmu = ERR_PTR(ret);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010998
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010999 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +080011000 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +010011001
Sebastian Andrzej Siewior9f0bff12019-11-19 13:14:29 +010011002 list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000011003 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020011004 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020011005 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020011006
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020011007 if (ret != -ENOENT) {
11008 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020011009 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011010 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011011 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020011012 pmu = ERR_PTR(-ENOENT);
11013unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020011014 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011015
11016 return pmu;
11017}
11018
Kan Liangf2fb6be2016-03-23 11:24:37 -070011019static void attach_sb_event(struct perf_event *event)
11020{
11021 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
11022
11023 raw_spin_lock(&pel->lock);
11024 list_add_rcu(&event->sb_list, &pel->list);
11025 raw_spin_unlock(&pel->lock);
11026}
11027
Peter Zijlstraaab5b712016-05-12 17:26:46 +020011028/*
11029 * We keep a list of all !task (and therefore per-cpu) events
11030 * that need to receive side-band records.
11031 *
11032 * This avoids having to scan all the various PMU per-cpu contexts
11033 * looking for them.
11034 */
Kan Liangf2fb6be2016-03-23 11:24:37 -070011035static void account_pmu_sb_event(struct perf_event *event)
11036{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -070011037 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -070011038 attach_sb_event(event);
11039}
11040
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011041static void account_event_cpu(struct perf_event *event, int cpu)
11042{
11043 if (event->parent)
11044 return;
11045
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011046 if (is_cgroup_event(event))
11047 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
11048}
11049
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020011050/* Freq events need the tick to stay alive (see perf_event_task_tick). */
11051static void account_freq_event_nohz(void)
11052{
11053#ifdef CONFIG_NO_HZ_FULL
11054 /* Lock so we don't race with concurrent unaccount */
11055 spin_lock(&nr_freq_lock);
11056 if (atomic_inc_return(&nr_freq_events) == 1)
11057 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
11058 spin_unlock(&nr_freq_lock);
11059#endif
11060}
11061
11062static void account_freq_event(void)
11063{
11064 if (tick_nohz_full_enabled())
11065 account_freq_event_nohz();
11066 else
11067 atomic_inc(&nr_freq_events);
11068}
11069
11070
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011071static void account_event(struct perf_event *event)
11072{
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011073 bool inc = false;
11074
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011075 if (event->parent)
11076 return;
11077
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011078 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011079 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011080 if (event->attr.mmap || event->attr.mmap_data)
11081 atomic_inc(&nr_mmap_events);
11082 if (event->attr.comm)
11083 atomic_inc(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +053011084 if (event->attr.namespaces)
11085 atomic_inc(&nr_namespaces_events);
Namhyung Kim96aaab62020-03-25 21:45:28 +090011086 if (event->attr.cgroup)
11087 atomic_inc(&nr_cgroup_events);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011088 if (event->attr.task)
11089 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020011090 if (event->attr.freq)
11091 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +030011092 if (event->attr.context_switch) {
11093 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011094 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +030011095 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011096 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011097 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011098 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011099 inc = true;
Song Liu76193a92019-01-17 08:15:13 -080011100 if (event->attr.ksymbol)
11101 atomic_inc(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -080011102 if (event->attr.bpf_event)
11103 atomic_inc(&nr_bpf_events);
Adrian Huntere17d43b2020-05-12 15:19:08 +030011104 if (event->attr.text_poke)
11105 atomic_inc(&nr_text_poke_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011106
Peter Zijlstra9107c892016-02-24 18:45:45 +010011107 if (inc) {
Alexander Shishkin5bce9db2017-08-29 17:01:03 +030011108 /*
11109 * We need the mutex here because static_branch_enable()
11110 * must complete *before* the perf_sched_count increment
11111 * becomes visible.
11112 */
Peter Zijlstra9107c892016-02-24 18:45:45 +010011113 if (atomic_inc_not_zero(&perf_sched_count))
11114 goto enabled;
11115
11116 mutex_lock(&perf_sched_mutex);
11117 if (!atomic_read(&perf_sched_count)) {
11118 static_branch_enable(&perf_sched_events);
11119 /*
11120 * Guarantee that all CPUs observe they key change and
11121 * call the perf scheduling hooks before proceeding to
11122 * install events that need them.
11123 */
Paul E. McKenney0809d9542018-11-06 19:20:05 -080011124 synchronize_rcu();
Peter Zijlstra9107c892016-02-24 18:45:45 +010011125 }
11126 /*
11127 * Now that we have waited for the sync_sched(), allow further
11128 * increments to by-pass the mutex.
11129 */
11130 atomic_inc(&perf_sched_count);
11131 mutex_unlock(&perf_sched_mutex);
11132 }
11133enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011134
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011135 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -070011136
11137 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011138}
11139
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011140/*
Tobias Tefke788faab2018-07-09 12:57:15 +020011141 * Allocate and initialize an event structure
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011142 */
11143static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011144perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011145 struct task_struct *task,
11146 struct perf_event *group_leader,
11147 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +030011148 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +000011149 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011150{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +020011151 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011152 struct perf_event *event;
11153 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011154 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011155
Oleg Nesterov66832eb2011-01-18 17:10:32 +010011156 if ((unsigned)cpu >= nr_cpu_ids) {
11157 if (!task || cpu != -1)
11158 return ERR_PTR(-EINVAL);
11159 }
11160
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011161 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011162 if (!event)
11163 return ERR_PTR(-ENOMEM);
11164
11165 /*
11166 * Single events are their own group leaders, with an
11167 * empty sibling list:
11168 */
11169 if (!group_leader)
11170 group_leader = event;
11171
11172 mutex_init(&event->child_mutex);
11173 INIT_LIST_HEAD(&event->child_list);
11174
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011175 INIT_LIST_HEAD(&event->event_entry);
11176 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra66681282017-11-13 14:28:38 +010011177 INIT_LIST_HEAD(&event->active_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +030011178 init_event_group(event);
Peter Zijlstra10c6db12011-11-26 02:47:31 +010011179 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +010011180 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011181 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +010011182 INIT_HLIST_NODE(&event->hlist_entry);
11183
Peter Zijlstra10c6db12011-11-26 02:47:31 +010011184
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011185 init_waitqueue_head(&event->waitq);
Peter Zijlstra1d54ad92019-04-04 15:03:00 +020011186 event->pending_disable = -1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +080011187 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011188
11189 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011190 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011191
Al Viroa6fa9412012-08-20 14:59:25 +010011192 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011193 event->cpu = cpu;
11194 event->attr = *attr;
11195 event->group_leader = group_leader;
11196 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011197 event->oncpu = -1;
11198
11199 event->parent = parent_event;
11200
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080011201 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011202 event->id = atomic64_inc_return(&perf_event_id);
11203
11204 event->state = PERF_EVENT_STATE_INACTIVE;
11205
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011206 if (task) {
11207 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011208 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +010011209 * XXX pmu::event_init needs to know what task to account to
11210 * and we cannot use the ctx information because we need the
11211 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011212 */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -070011213 event->hw.target = get_task_struct(task);
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011214 }
11215
Peter Zijlstra34f43922015-02-20 14:05:38 +010011216 event->clock = &local_clock;
11217 if (parent_event)
11218 event->clock = parent_event->clock;
11219
Avi Kivity4dc0da82011-06-29 18:42:35 +030011220 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +010011221 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030011222 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +020011223#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011224 if (overflow_handler == bpf_overflow_handler) {
Andrii Nakryiko85192db2019-11-17 09:28:03 -080011225 struct bpf_prog *prog = parent_event->prog;
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011226
Andrii Nakryiko85192db2019-11-17 09:28:03 -080011227 bpf_prog_inc(prog);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011228 event->prog = prog;
11229 event->orig_overflow_handler =
11230 parent_event->orig_overflow_handler;
11231 }
11232#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +030011233 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +010011234
Wang Nan18794452016-03-28 06:41:30 +000011235 if (overflow_handler) {
11236 event->overflow_handler = overflow_handler;
11237 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +000011238 } else if (is_write_backward(event)){
11239 event->overflow_handler = perf_event_output_backward;
11240 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +000011241 } else {
Wang Nan9ecda412016-04-05 14:11:18 +000011242 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +000011243 event->overflow_handler_context = NULL;
11244 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +020011245
Jiri Olsa0231bb52013-02-01 11:23:45 +010011246 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011247
11248 pmu = NULL;
11249
11250 hwc = &event->hw;
11251 hwc->sample_period = attr->sample_period;
11252 if (attr->freq && attr->sample_freq)
11253 hwc->sample_period = 1;
11254 hwc->last_period = hwc->sample_period;
11255
Peter Zijlstrae7850592010-05-21 14:43:08 +020011256 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011257
11258 /*
Peter Zijlstraba5213a2017-05-30 11:45:12 +020011259 * We currently do not support PERF_SAMPLE_READ on inherited events.
11260 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011261 */
Peter Zijlstraba5213a2017-05-30 11:45:12 +020011262 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011263 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011264
Yan, Zhenga46a2302014-11-04 21:56:06 -050011265 if (!has_branch_stack(event))
11266 event->attr.branch_sample_type = 0;
11267
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020011268 pmu = perf_init_event(event);
Dan Carpenter85c617a2017-05-22 12:03:49 +030011269 if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011270 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011271 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011272 }
11273
Peter Zijlstra09f4e8f2019-11-06 12:51:04 +010011274 /*
11275 * Disallow uncore-cgroup events, they don't make sense as the cgroup will
11276 * be different on other CPUs in the uncore mask.
11277 */
11278 if (pmu->task_ctx_nr == perf_invalid_context && cgroup_fd != -1) {
11279 err = -EINVAL;
11280 goto err_pmu;
11281 }
11282
Alexander Shishkinab437622019-08-06 11:46:00 +030011283 if (event->attr.aux_output &&
11284 !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) {
11285 err = -EOPNOTSUPP;
11286 goto err_pmu;
11287 }
11288
Peter Zijlstra98add2a2020-02-13 23:51:28 -080011289 if (cgroup_fd != -1) {
11290 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
11291 if (err)
11292 goto err_pmu;
11293 }
11294
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011295 err = exclusive_event_init(event);
11296 if (err)
11297 goto err_pmu;
11298
Alexander Shishkin375637b2016-04-27 18:44:46 +030011299 if (has_addr_filter(event)) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011300 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
11301 sizeof(struct perf_addr_filter_range),
11302 GFP_KERNEL);
11303 if (!event->addr_filter_ranges) {
Dan Carpenter36cc2b92017-05-22 12:04:18 +030011304 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +030011305 goto err_per_task;
Dan Carpenter36cc2b92017-05-22 12:04:18 +030011306 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030011307
Alexander Shishkin18736ee2019-02-15 13:56:54 +020011308 /*
11309 * Clone the parent's vma offsets: they are valid until exec()
11310 * even if the mm is not shared with the parent.
11311 */
11312 if (event->parent) {
11313 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
11314
11315 raw_spin_lock_irq(&ifh->lock);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011316 memcpy(event->addr_filter_ranges,
11317 event->parent->addr_filter_ranges,
11318 pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range));
Alexander Shishkin18736ee2019-02-15 13:56:54 +020011319 raw_spin_unlock_irq(&ifh->lock);
11320 }
11321
Alexander Shishkin375637b2016-04-27 18:44:46 +030011322 /* force hw sync on the address filters */
11323 event->addr_filters_gen = 1;
11324 }
11325
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011326 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +020011327 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -030011328 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011329 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +030011330 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +010011331 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011332 }
11333
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011334 err = security_perf_event_alloc(event);
11335 if (err)
11336 goto err_callchain_buffer;
11337
Alexander Shishkin927a5572016-03-02 13:24:14 +020011338 /* symmetric to unaccount_event() in _free_event() */
11339 account_event(event);
11340
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011341 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011342
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011343err_callchain_buffer:
11344 if (!event->parent) {
11345 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
11346 put_callchain_buffers();
11347 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030011348err_addr_filters:
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011349 kfree(event->addr_filter_ranges);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011350
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011351err_per_task:
11352 exclusive_event_destroy(event);
11353
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011354err_pmu:
Peter Zijlstra98add2a2020-02-13 23:51:28 -080011355 if (is_cgroup_event(event))
11356 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011357 if (event->destroy)
11358 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +080011359 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011360err_ns:
11361 if (event->ns)
11362 put_pid_ns(event->ns);
Prashant Bhole621b6d22018-04-09 19:03:46 +090011363 if (event->hw.target)
11364 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011365 kfree(event);
11366
11367 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011368}
11369
11370static int perf_copy_attr(struct perf_event_attr __user *uattr,
11371 struct perf_event_attr *attr)
11372{
11373 u32 size;
11374 int ret;
11375
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011376 /* Zero the full structure, so that a short copy will be nice. */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011377 memset(attr, 0, sizeof(*attr));
11378
11379 ret = get_user(size, &uattr->size);
11380 if (ret)
11381 return ret;
11382
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011383 /* ABI compatibility quirk: */
11384 if (!size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011385 size = PERF_ATTR_SIZE_VER0;
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011386 if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011387 goto err_size;
11388
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011389 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
11390 if (ret) {
11391 if (ret == -E2BIG)
11392 goto err_size;
11393 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011394 }
11395
Meng Xuf12f42a2017-08-23 17:07:50 -040011396 attr->size = size;
11397
Alexander Shishkina4faf002019-10-25 17:08:33 +030011398 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011399 return -EINVAL;
11400
11401 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
11402 return -EINVAL;
11403
11404 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
11405 return -EINVAL;
11406
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011407 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
11408 u64 mask = attr->branch_sample_type;
11409
11410 /* only using defined bits */
11411 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
11412 return -EINVAL;
11413
11414 /* at least one branch bit must be set */
11415 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
11416 return -EINVAL;
11417
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011418 /* propagate priv level, when not set for branch */
11419 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
11420
11421 /* exclude_kernel checked on syscall entry */
11422 if (!attr->exclude_kernel)
11423 mask |= PERF_SAMPLE_BRANCH_KERNEL;
11424
11425 if (!attr->exclude_user)
11426 mask |= PERF_SAMPLE_BRANCH_USER;
11427
11428 if (!attr->exclude_hv)
11429 mask |= PERF_SAMPLE_BRANCH_HV;
11430 /*
11431 * adjust user setting (for HW filter setup)
11432 */
11433 attr->branch_sample_type = mask;
11434 }
Stephane Eraniane7122092013-06-06 11:02:04 +020011435 /* privileged levels capture (kernel, hv): check permissions */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011436 if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
11437 ret = perf_allow_kernel(attr);
11438 if (ret)
11439 return ret;
11440 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011441 }
Jiri Olsa40189942012-08-07 15:20:37 +020011442
Jiri Olsac5ebced2012-08-07 15:20:40 +020011443 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +020011444 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +020011445 if (ret)
11446 return ret;
11447 }
11448
11449 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
11450 if (!arch_perf_have_user_stack_dump())
11451 return -ENOSYS;
11452
11453 /*
11454 * We have __u32 type for the size, but so far
11455 * we can only use __u16 as maximum due to the
11456 * __u16 sample size limit.
11457 */
11458 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa78b562f2018-04-15 11:23:50 +020011459 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020011460 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa78b562f2018-04-15 11:23:50 +020011461 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020011462 }
Jiri Olsa40189942012-08-07 15:20:37 +020011463
Jiri Olsa5f970522018-03-12 14:45:46 +010011464 if (!attr->sample_max_stack)
11465 attr->sample_max_stack = sysctl_perf_event_max_stack;
11466
Stephane Eranian60e23642014-09-24 13:48:37 +020011467 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
11468 ret = perf_reg_validate(attr->sample_regs_intr);
Namhyung Kim6546b192020-03-25 21:45:29 +090011469
11470#ifndef CONFIG_CGROUP_PERF
11471 if (attr->sample_type & PERF_SAMPLE_CGROUP)
11472 return -EINVAL;
11473#endif
11474
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011475out:
11476 return ret;
11477
11478err_size:
11479 put_user(sizeof(*attr), &uattr->size);
11480 ret = -E2BIG;
11481 goto out;
11482}
11483
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011484static int
11485perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011486{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -050011487 struct perf_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011488 int ret = -EINVAL;
11489
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011490 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011491 goto set;
11492
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011493 /* don't allow circular references */
11494 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011495 goto out;
11496
Peter Zijlstra0f139302010-05-20 14:35:15 +020011497 /*
11498 * Don't allow cross-cpu buffers
11499 */
11500 if (output_event->cpu != event->cpu)
11501 goto out;
11502
11503 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +020011504 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +020011505 */
11506 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
11507 goto out;
11508
Peter Zijlstra34f43922015-02-20 14:05:38 +010011509 /*
11510 * Mixing clocks in the same buffer is trouble you don't need.
11511 */
11512 if (output_event->clock != event->clock)
11513 goto out;
11514
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011515 /*
Wang Nan9ecda412016-04-05 14:11:18 +000011516 * Either writing ring buffer from beginning or from end.
11517 * Mixing is not allowed.
11518 */
11519 if (is_write_backward(output_event) != is_write_backward(event))
11520 goto out;
11521
11522 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011523 * If both events generate aux data, they must be on the same PMU
11524 */
11525 if (has_aux(event) && has_aux(output_event) &&
11526 event->pmu != output_event->pmu)
11527 goto out;
11528
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011529set:
11530 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011531 /* Can't redirect output if we've got an active mmap() */
11532 if (atomic_read(&event->mmap_count))
11533 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011534
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011535 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +020011536 /* get the rb we want to redirect to */
11537 rb = ring_buffer_get(output_event);
11538 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011539 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011540 }
11541
Peter Zijlstrab69cf532014-03-14 10:50:33 +010011542 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +020011543
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011544 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011545unlock:
11546 mutex_unlock(&event->mmap_mutex);
11547
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011548out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011549 return ret;
11550}
11551
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011552static void mutex_lock_double(struct mutex *a, struct mutex *b)
11553{
11554 if (b < a)
11555 swap(a, b);
11556
11557 mutex_lock(a);
11558 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
11559}
11560
Peter Zijlstra34f43922015-02-20 14:05:38 +010011561static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
11562{
11563 bool nmi_safe = false;
11564
11565 switch (clk_id) {
11566 case CLOCK_MONOTONIC:
11567 event->clock = &ktime_get_mono_fast_ns;
11568 nmi_safe = true;
11569 break;
11570
11571 case CLOCK_MONOTONIC_RAW:
11572 event->clock = &ktime_get_raw_fast_ns;
11573 nmi_safe = true;
11574 break;
11575
11576 case CLOCK_REALTIME:
11577 event->clock = &ktime_get_real_ns;
11578 break;
11579
11580 case CLOCK_BOOTTIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011581 event->clock = &ktime_get_boottime_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011582 break;
11583
11584 case CLOCK_TAI:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011585 event->clock = &ktime_get_clocktai_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011586 break;
11587
11588 default:
11589 return -EINVAL;
11590 }
11591
11592 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
11593 return -EINVAL;
11594
11595 return 0;
11596}
11597
Peter Zijlstra321027c2017-01-11 21:09:50 +010011598/*
11599 * Variation on perf_event_ctx_lock_nested(), except we take two context
11600 * mutexes.
11601 */
11602static struct perf_event_context *
11603__perf_event_ctx_lock_double(struct perf_event *group_leader,
11604 struct perf_event_context *ctx)
11605{
11606 struct perf_event_context *gctx;
11607
11608again:
11609 rcu_read_lock();
11610 gctx = READ_ONCE(group_leader->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +020011611 if (!refcount_inc_not_zero(&gctx->refcount)) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011612 rcu_read_unlock();
11613 goto again;
11614 }
11615 rcu_read_unlock();
11616
11617 mutex_lock_double(&gctx->mutex, &ctx->mutex);
11618
11619 if (group_leader->ctx != gctx) {
11620 mutex_unlock(&ctx->mutex);
11621 mutex_unlock(&gctx->mutex);
11622 put_ctx(gctx);
11623 goto again;
11624 }
11625
11626 return gctx;
11627}
11628
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011629/**
11630 * sys_perf_event_open - open a performance event, associate it to a task/cpu
11631 *
11632 * @attr_uptr: event_id type attributes for monitoring/sampling
11633 * @pid: target pid
11634 * @cpu: target cpu
11635 * @group_fd: group leader event fd
11636 */
11637SYSCALL_DEFINE5(perf_event_open,
11638 struct perf_event_attr __user *, attr_uptr,
11639 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
11640{
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011641 struct perf_event *group_leader = NULL, *output_event = NULL;
11642 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011643 struct perf_event_attr attr;
Kees Cook3f649ab2020-06-03 13:09:38 -070011644 struct perf_event_context *ctx, *gctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011645 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -040011646 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -070011647 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011648 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -040011649 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011650 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011651 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +010011652 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +000011653 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011654
11655 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +020011656 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011657 return -EINVAL;
11658
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011659 /* Do we allow access to perf_event_open(2) ? */
11660 err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
11661 if (err)
11662 return err;
11663
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011664 err = perf_copy_attr(attr_uptr, &attr);
11665 if (err)
11666 return err;
11667
11668 if (!attr.exclude_kernel) {
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011669 err = perf_allow_kernel(&attr);
11670 if (err)
11671 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011672 }
11673
Hari Bathinie4222672017-03-08 02:11:36 +053011674 if (attr.namespaces) {
Alexey Budankov18aa1852020-04-02 11:46:24 +030011675 if (!perfmon_capable())
Hari Bathinie4222672017-03-08 02:11:36 +053011676 return -EACCES;
11677 }
11678
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011679 if (attr.freq) {
11680 if (attr.sample_freq > sysctl_perf_event_sample_rate)
11681 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +020011682 } else {
11683 if (attr.sample_period & (1ULL << 63))
11684 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011685 }
11686
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011687 /* Only privileged users can get physical addresses */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011688 if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
11689 err = perf_allow_kernel(&attr);
11690 if (err)
11691 return err;
11692 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011693
David Howellsb0c8fdc2019-08-19 17:18:00 -070011694 err = security_locked_down(LOCKDOWN_PERF);
11695 if (err && (attr.sample_type & PERF_SAMPLE_REGS_INTR))
11696 /* REGS_INTR can leak data, lockdown must prevent this */
11697 return err;
11698
11699 err = 0;
11700
Stephane Eraniane5d13672011-02-14 11:20:01 +020011701 /*
11702 * In cgroup mode, the pid argument is used to pass the fd
11703 * opened to the cgroup directory in cgroupfs. The cpu argument
11704 * designates the cpu on which to monitor threads from that
11705 * cgroup.
11706 */
11707 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
11708 return -EINVAL;
11709
Yann Droneauda21b0b32014-01-05 21:36:33 +010011710 if (flags & PERF_FLAG_FD_CLOEXEC)
11711 f_flags |= O_CLOEXEC;
11712
11713 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011714 if (event_fd < 0)
11715 return event_fd;
11716
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011717 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -040011718 err = perf_fget_light(group_fd, &group);
11719 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011720 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -040011721 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011722 if (flags & PERF_FLAG_FD_OUTPUT)
11723 output_event = group_leader;
11724 if (flags & PERF_FLAG_FD_NO_GROUP)
11725 group_leader = NULL;
11726 }
11727
Stephane Eraniane5d13672011-02-14 11:20:01 +020011728 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011729 task = find_lively_task_by_vpid(pid);
11730 if (IS_ERR(task)) {
11731 err = PTR_ERR(task);
11732 goto err_group_fd;
11733 }
11734 }
11735
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020011736 if (task && group_leader &&
11737 group_leader->attr.inherit != attr.inherit) {
11738 err = -EINVAL;
11739 goto err_task;
11740 }
11741
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011742 if (task) {
Bernd Edlinger69143032020-03-20 21:27:55 +010011743 err = mutex_lock_interruptible(&task->signal->exec_update_mutex);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011744 if (err)
Alexander Levine5aeee52017-06-03 03:39:13 +000011745 goto err_task;
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011746
11747 /*
Alexey Budankov45fd22d2020-08-05 10:56:56 +030011748 * Preserve ptrace permission check for backwards compatibility.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011749 *
Bernd Edlinger69143032020-03-20 21:27:55 +010011750 * We must hold exec_update_mutex across this and any potential
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011751 * perf_install_in_context() call for this new event to
11752 * serialize against exec() altering our credentials (and the
11753 * perf_event_exit_task() that could imply).
11754 */
11755 err = -EACCES;
Alexey Budankov45fd22d2020-08-05 10:56:56 +030011756 if (!perfmon_capable() && !ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011757 goto err_cred;
11758 }
11759
Matt Fleming79dff512015-01-23 18:45:42 +000011760 if (flags & PERF_FLAG_PID_CGROUP)
11761 cgroup_fd = pid;
11762
Avi Kivity4dc0da82011-06-29 18:42:35 +030011763 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000011764 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011765 if (IS_ERR(event)) {
11766 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011767 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011768 }
11769
Vince Weaver53b25332014-05-16 17:12:12 -040011770 if (is_sampling_event(event)) {
11771 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +053011772 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -040011773 goto err_alloc;
11774 }
11775 }
11776
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011777 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011778 * Special case software events and allow them to be part of
11779 * any hardware group.
11780 */
11781 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011782
Peter Zijlstra34f43922015-02-20 14:05:38 +010011783 if (attr.use_clockid) {
11784 err = perf_event_set_clock(event, attr.clockid);
11785 if (err)
11786 goto err_alloc;
11787 }
11788
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011789 if (pmu->task_ctx_nr == perf_sw_context)
11790 event->event_caps |= PERF_EV_CAP_SOFTWARE;
11791
Song Liua1150c22018-05-03 12:47:16 -070011792 if (group_leader) {
11793 if (is_software_event(event) &&
11794 !in_software_context(group_leader)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011795 /*
Song Liua1150c22018-05-03 12:47:16 -070011796 * If the event is a sw event, but the group_leader
11797 * is on hw context.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011798 *
Song Liua1150c22018-05-03 12:47:16 -070011799 * Allow the addition of software events to hw
11800 * groups, this is safe because software events
11801 * never fail to schedule.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011802 */
Song Liua1150c22018-05-03 12:47:16 -070011803 pmu = group_leader->ctx->pmu;
11804 } else if (!is_software_event(event) &&
11805 is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011806 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011807 /*
11808 * In case the group is a pure software group, and we
11809 * try to add a hardware event, move the whole group to
11810 * the hardware context.
11811 */
11812 move_group = 1;
11813 }
11814 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011815
11816 /*
11817 * Get the target context (task or percpu):
11818 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050011819 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011820 if (IS_ERR(ctx)) {
11821 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011822 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011823 }
11824
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011825 /*
11826 * Look up the group leader (we will attach this event to it):
11827 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011828 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011829 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011830
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011831 /*
11832 * Do not allow a recursive hierarchy (this new sibling
11833 * becoming part of another group-sibling):
11834 */
11835 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011836 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011837
11838 /* All events in a group should have the same clock */
11839 if (group_leader->clock != event->clock)
11840 goto err_context;
11841
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011842 /*
Mark Rutland64aee2a2017-06-22 15:41:38 +010011843 * Make sure we're both events for the same CPU;
11844 * grouping events for different CPUs is broken; since
11845 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011846 */
Mark Rutland64aee2a2017-06-22 15:41:38 +010011847 if (group_leader->cpu != event->cpu)
11848 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +010011849
Mark Rutland64aee2a2017-06-22 15:41:38 +010011850 /*
11851 * Make sure we're both on the same task, or both
11852 * per-CPU events.
11853 */
11854 if (group_leader->ctx->task != ctx->task)
11855 goto err_context;
11856
11857 /*
11858 * Do not allow to attach to a group in a different task
11859 * or CPU context. If we're moving SW events, we'll fix
11860 * this up later, so allow that.
11861 */
11862 if (!move_group && group_leader->ctx != ctx)
11863 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011864
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011865 /*
11866 * Only a group leader can be exclusive or pinned
11867 */
11868 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011869 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011870 }
11871
11872 if (output_event) {
11873 err = perf_event_set_output(event, output_event);
11874 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011875 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011876 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011877
Yann Droneauda21b0b32014-01-05 21:36:33 +010011878 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
11879 f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011880 if (IS_ERR(event_file)) {
11881 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +020011882 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011883 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -040011884 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011885
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011886 if (move_group) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011887 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
11888
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011889 if (gctx->task == TASK_TOMBSTONE) {
11890 err = -ESRCH;
11891 goto err_locked;
11892 }
Peter Zijlstra321027c2017-01-11 21:09:50 +010011893
11894 /*
11895 * Check if we raced against another sys_perf_event_open() call
11896 * moving the software group underneath us.
11897 */
11898 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
11899 /*
11900 * If someone moved the group out from under us, check
11901 * if this new event wound up on the same ctx, if so
11902 * its the regular !move_group case, otherwise fail.
11903 */
11904 if (gctx != ctx) {
11905 err = -EINVAL;
11906 goto err_locked;
11907 } else {
11908 perf_event_ctx_unlock(group_leader, gctx);
11909 move_group = 0;
11910 }
11911 }
Alexander Shishkin8a58dda2019-07-01 14:07:55 +030011912
11913 /*
11914 * Failure to create exclusive events returns -EBUSY.
11915 */
11916 err = -EBUSY;
11917 if (!exclusive_event_installable(group_leader, ctx))
11918 goto err_locked;
11919
11920 for_each_sibling_event(sibling, group_leader) {
11921 if (!exclusive_event_installable(sibling, ctx))
11922 goto err_locked;
11923 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011924 } else {
11925 mutex_lock(&ctx->mutex);
11926 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011927
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011928 if (ctx->task == TASK_TOMBSTONE) {
11929 err = -ESRCH;
11930 goto err_locked;
11931 }
11932
Peter Zijlstraa7239682015-09-09 19:06:33 +020011933 if (!perf_event_validate_size(event)) {
11934 err = -E2BIG;
11935 goto err_locked;
11936 }
11937
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011938 if (!task) {
11939 /*
11940 * Check if the @cpu we're creating an event for is online.
11941 *
11942 * We use the perf_cpu_context::ctx::mutex to serialize against
11943 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
11944 */
11945 struct perf_cpu_context *cpuctx =
11946 container_of(ctx, struct perf_cpu_context, ctx);
11947
11948 if (!cpuctx->online) {
11949 err = -ENODEV;
11950 goto err_locked;
11951 }
11952 }
11953
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011954 if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) {
11955 err = -EINVAL;
Alexander Shishkinab437622019-08-06 11:46:00 +030011956 goto err_locked;
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011957 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011958
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011959 /*
11960 * Must be under the same ctx::mutex as perf_install_in_context(),
11961 * because we need to serialize with concurrent event creation.
11962 */
11963 if (!exclusive_event_installable(event, ctx)) {
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011964 err = -EBUSY;
11965 goto err_locked;
11966 }
11967
11968 WARN_ON_ONCE(ctx->parent_ctx);
11969
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011970 /*
11971 * This is the point on no return; we cannot fail hereafter. This is
11972 * where we start modifying current state.
11973 */
11974
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011975 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011976 /*
11977 * See perf_event_ctx_lock() for comments on the details
11978 * of swizzling perf_event::ctx.
11979 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011980 perf_remove_from_context(group_leader, 0);
Peter Zijlstra279b5162017-02-16 10:28:37 +010011981 put_ctx(gctx);
Jiri Olsa0231bb52013-02-01 11:23:45 +010011982
Peter Zijlstraedb39592018-03-15 17:36:56 +010011983 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011984 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011985 put_ctx(gctx);
11986 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011987
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011988 /*
11989 * Wait for everybody to stop referencing the events through
11990 * the old lists, before installing it on new lists.
11991 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011992 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011993
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011994 /*
11995 * Install the group siblings before the group leader.
11996 *
11997 * Because a group leader will try and install the entire group
11998 * (through the sibling list, which is still in-tact), we can
11999 * end up with siblings installed in the wrong context.
12000 *
12001 * By installing siblings first we NO-OP because they're not
12002 * reachable through the group lists.
12003 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010012004 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012005 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +010012006 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020012007 get_ctx(ctx);
12008 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012009
12010 /*
12011 * Removing from the context ends up with disabled
12012 * event. What we want here is event in the initial
12013 * startup state, ready to be add into new context.
12014 */
12015 perf_event__state_init(group_leader);
12016 perf_install_in_context(ctx, group_leader, group_leader->cpu);
12017 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020012018 }
12019
Peter Zijlstraf73e22a2015-09-09 20:48:22 +020012020 /*
12021 * Precalculate sample_data sizes; do while holding ctx::mutex such
12022 * that we're serialized against further additions and before
12023 * perf_install_in_context() which is the point the event is active and
12024 * can use these values.
12025 */
12026 perf_event__header_size(event);
12027 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012028
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010012029 event->owner = current;
12030
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080012031 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012032 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012033
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020012034 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010012035 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012036 mutex_unlock(&ctx->mutex);
12037
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012038 if (task) {
Bernd Edlinger69143032020-03-20 21:27:55 +010012039 mutex_unlock(&task->signal->exec_update_mutex);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012040 put_task_struct(task);
12041 }
12042
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012043 mutex_lock(&current->perf_event_mutex);
12044 list_add_tail(&event->owner_entry, &current->perf_event_list);
12045 mutex_unlock(&current->perf_event_mutex);
12046
Peter Zijlstra8a495422010-05-27 15:47:49 +020012047 /*
12048 * Drop the reference on the group_event after placing the
12049 * new event on the sibling_list. This ensures destruction
12050 * of the group leader will find the pointer to itself in
12051 * perf_group_detach().
12052 */
Al Viro2903ff02012-08-28 12:52:22 -040012053 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040012054 fd_install(event_fd, event_file);
12055 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012056
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020012057err_locked:
12058 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010012059 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020012060 mutex_unlock(&ctx->mutex);
12061/* err_file: */
12062 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012063err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012064 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040012065 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020012066err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010012067 /*
12068 * If event_file is set, the fput() above will have called ->release()
12069 * and that will take care of freeing the event.
12070 */
12071 if (!event_file)
12072 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012073err_cred:
12074 if (task)
Bernd Edlinger69143032020-03-20 21:27:55 +010012075 mutex_unlock(&task->signal->exec_update_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020012076err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020012077 if (task)
12078 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020012079err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040012080 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040012081err_fd:
12082 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012083 return err;
12084}
12085
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012086/**
12087 * perf_event_create_kernel_counter
12088 *
12089 * @attr: attributes of the counter to create
12090 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070012091 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012092 */
12093struct perf_event *
12094perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070012095 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030012096 perf_overflow_handler_t overflow_handler,
12097 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012098{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012099 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012100 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012101 int err;
12102
Alexander Shishkindce5aff2019-10-30 15:47:31 +020012103 /*
12104 * Grouping is not supported for kernel events, neither is 'AUX',
12105 * make sure the caller's intentions are adjusted.
12106 */
12107 if (attr->aux_output)
12108 return ERR_PTR(-EINVAL);
12109
Avi Kivity4dc0da82011-06-29 18:42:35 +030012110 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000012111 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012112 if (IS_ERR(event)) {
12113 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012114 goto err;
12115 }
12116
Jiri Olsaf8697762014-08-01 14:33:01 +020012117 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012118 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020012119
Alexander Shishkinf25d8ba2019-10-30 15:47:30 +020012120 /*
12121 * Get the target context (task or percpu):
12122 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050012123 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012124 if (IS_ERR(ctx)) {
12125 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012126 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012127 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012128
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012129 WARN_ON_ONCE(ctx->parent_ctx);
12130 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012131 if (ctx->task == TASK_TOMBSTONE) {
12132 err = -ESRCH;
12133 goto err_unlock;
12134 }
12135
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012136 if (!task) {
12137 /*
12138 * Check if the @cpu we're creating an event for is online.
12139 *
12140 * We use the perf_cpu_context::ctx::mutex to serialize against
12141 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
12142 */
12143 struct perf_cpu_context *cpuctx =
12144 container_of(ctx, struct perf_cpu_context, ctx);
12145 if (!cpuctx->online) {
12146 err = -ENODEV;
12147 goto err_unlock;
12148 }
12149 }
12150
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012151 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012152 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012153 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012154 }
12155
Leonard Crestez4ce54af2019-07-24 15:53:24 +030012156 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012157 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012158 mutex_unlock(&ctx->mutex);
12159
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012160 return event;
12161
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012162err_unlock:
12163 mutex_unlock(&ctx->mutex);
12164 perf_unpin_context(ctx);
12165 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012166err_free:
12167 free_event(event);
12168err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012169 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012170}
12171EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
12172
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012173void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
12174{
12175 struct perf_event_context *src_ctx;
12176 struct perf_event_context *dst_ctx;
12177 struct perf_event *event, *tmp;
12178 LIST_HEAD(events);
12179
12180 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
12181 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
12182
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012183 /*
12184 * See perf_event_ctx_lock() for comments on the details
12185 * of swizzling perf_event::ctx.
12186 */
12187 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012188 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
12189 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012190 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020012191 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012192 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020012193 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012194 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012195
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012196 /*
12197 * Wait for the events to quiesce before re-instating them.
12198 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012199 synchronize_rcu();
12200
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012201 /*
12202 * Re-instate events in 2 passes.
12203 *
12204 * Skip over group leaders and only install siblings on this first
12205 * pass, siblings will not get enabled without a leader, however a
12206 * leader will enable its siblings, even if those are still on the old
12207 * context.
12208 */
12209 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
12210 if (event->group_leader == event)
12211 continue;
12212
12213 list_del(&event->migrate_entry);
12214 if (event->state >= PERF_EVENT_STATE_OFF)
12215 event->state = PERF_EVENT_STATE_INACTIVE;
12216 account_event_cpu(event, dst_cpu);
12217 perf_install_in_context(dst_ctx, event, dst_cpu);
12218 get_ctx(dst_ctx);
12219 }
12220
12221 /*
12222 * Once all the siblings are setup properly, install the group leaders
12223 * to make it go.
12224 */
Peter Zijlstra98861672013-10-03 16:02:23 +020012225 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
12226 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012227 if (event->state >= PERF_EVENT_STATE_OFF)
12228 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020012229 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012230 perf_install_in_context(dst_ctx, event, dst_cpu);
12231 get_ctx(dst_ctx);
12232 }
12233 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012234 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012235}
12236EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
12237
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012238static void sync_child_event(struct perf_event *child_event,
12239 struct task_struct *child)
12240{
12241 struct perf_event *parent_event = child_event->parent;
12242 u64 child_val;
12243
12244 if (child_event->attr.inherit_stat)
12245 perf_event_read_event(child_event, child);
12246
Peter Zijlstrab5e58792010-05-21 14:43:12 +020012247 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012248
12249 /*
12250 * Add back the child's count to the parent's count:
12251 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020012252 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012253 atomic64_add(child_event->total_time_enabled,
12254 &parent_event->child_total_time_enabled);
12255 atomic64_add(child_event->total_time_running,
12256 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012257}
12258
12259static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012260perf_event_exit_event(struct perf_event *child_event,
12261 struct perf_event_context *child_ctx,
12262 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012263{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012264 struct perf_event *parent_event = child_event->parent;
12265
Peter Zijlstra1903d502014-07-15 17:27:27 +020012266 /*
12267 * Do not destroy the 'original' grouping; because of the context
12268 * switch optimization the original events could've ended up in a
12269 * random child task.
12270 *
12271 * If we were to destroy the original group, all group related
12272 * operations would cease to function properly after this random
12273 * child dies.
12274 *
12275 * Do destroy all inherited groups, we don't care about those
12276 * and being thorough is better.
12277 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010012278 raw_spin_lock_irq(&child_ctx->lock);
12279 WARN_ON_ONCE(child_ctx->is_active);
12280
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012281 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010012282 perf_group_detach(child_event);
12283 list_del_event(child_event, child_ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +020012284 perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010012285 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012286
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012287 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012288 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012289 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012290 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040012291 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012292 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012293 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012294 /*
12295 * Child events can be cleaned up.
12296 */
12297
12298 sync_child_event(child_event, child);
12299
12300 /*
12301 * Remove this event from the parent's list
12302 */
12303 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
12304 mutex_lock(&parent_event->child_mutex);
12305 list_del_init(&child_event->child_list);
12306 mutex_unlock(&parent_event->child_mutex);
12307
12308 /*
12309 * Kick perf_poll() for is_event_hup().
12310 */
12311 perf_event_wakeup(parent_event);
12312 free_event(child_event);
12313 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012314}
12315
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012316static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012317{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012318 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012319 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012320
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012321 WARN_ON_ONCE(child != current);
12322
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012323 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012324 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012325 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012326
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012327 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012328 * In order to reduce the amount of tricky in ctx tear-down, we hold
12329 * ctx::mutex over the entire thing. This serializes against almost
12330 * everything that wants to access the ctx.
12331 *
12332 * The exception is sys_perf_event_open() /
12333 * perf_event_create_kernel_count() which does find_get_context()
12334 * without ctx::mutex (it cannot because of the move_group double mutex
12335 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012336 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012337 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012338
12339 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012340 * In a single ctx::lock section, de-schedule the events and detach the
12341 * context from the task such that we cannot ever get it scheduled back
12342 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012343 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012344 raw_spin_lock_irq(&child_ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +020012345 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020012346
12347 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012348 * Now that the context is inactive, destroy the task <-> ctx relation
12349 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012350 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012351 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
12352 put_ctx(child_ctx); /* cannot be last */
12353 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
12354 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012355
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012356 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012357 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012358
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012359 if (clone_ctx)
12360 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020012361
12362 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012363 * Report the task dead after unscheduling the events so that we
12364 * won't get any samples after PERF_RECORD_EXIT. We can however still
12365 * get a few PERF_RECORD_READ events.
12366 */
12367 perf_event_task(child, child_ctx, 0);
12368
Peter Zijlstraebf905f2014-05-29 19:00:24 +020012369 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012370 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012371
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012372 mutex_unlock(&child_ctx->mutex);
12373
12374 put_ctx(child_ctx);
12375}
12376
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012377/*
12378 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012379 *
Bernd Edlinger69143032020-03-20 21:27:55 +010012380 * Can be called with exec_update_mutex held when called from
Eric W. Biederman96ecee22020-05-03 06:48:17 -050012381 * setup_new_exec().
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012382 */
12383void perf_event_exit_task(struct task_struct *child)
12384{
Peter Zijlstra88821352010-11-09 19:01:43 +010012385 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012386 int ctxn;
12387
Peter Zijlstra88821352010-11-09 19:01:43 +010012388 mutex_lock(&child->perf_event_mutex);
12389 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
12390 owner_entry) {
12391 list_del_init(&event->owner_entry);
12392
12393 /*
12394 * Ensure the list deletion is visible before we clear
12395 * the owner, closes a race against perf_release() where
12396 * we need to serialize on the owner->perf_event_mutex.
12397 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010012398 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010012399 }
12400 mutex_unlock(&child->perf_event_mutex);
12401
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012402 for_each_task_context_nr(ctxn)
12403 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010012404
12405 /*
12406 * The perf_event_exit_task_context calls perf_event_task
12407 * with child's task_ctx, which generates EXIT events for
12408 * child contexts and sets child->perf_event_ctxp[] to NULL.
12409 * At this point we need to send EXIT events to cpu contexts.
12410 */
12411 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012412}
12413
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012414static void perf_free_event(struct perf_event *event,
12415 struct perf_event_context *ctx)
12416{
12417 struct perf_event *parent = event->parent;
12418
12419 if (WARN_ON_ONCE(!parent))
12420 return;
12421
12422 mutex_lock(&parent->child_mutex);
12423 list_del_init(&event->child_list);
12424 mutex_unlock(&parent->child_mutex);
12425
Al Viroa6fa9412012-08-20 14:59:25 +010012426 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012427
Peter Zijlstra652884f2015-01-23 11:20:10 +010012428 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020012429 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012430 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010012431 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012432 free_event(event);
12433}
12434
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012435/*
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012436 * Free a context as created by inheritance by perf_event_init_task() below,
12437 * used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010012438 *
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012439 * Even though the task has never lived, the context and events have been
12440 * exposed through the child_list, so we must take care tearing it all down.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012441 */
12442void perf_event_free_task(struct task_struct *task)
12443{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012444 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012445 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012446 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012447
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012448 for_each_task_context_nr(ctxn) {
12449 ctx = task->perf_event_ctxp[ctxn];
12450 if (!ctx)
12451 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012452
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012453 mutex_lock(&ctx->mutex);
Peter Zijlstrae552a832017-03-16 13:47:48 +010012454 raw_spin_lock_irq(&ctx->lock);
12455 /*
12456 * Destroy the task <-> ctx relation and mark the context dead.
12457 *
12458 * This is important because even though the task hasn't been
12459 * exposed yet the context has been (through child_list).
12460 */
12461 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
12462 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
12463 put_task_struct(task); /* cannot be last */
12464 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012465
Peter Zijlstra15121c72017-03-16 13:47:50 +010012466 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012467 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012468
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012469 mutex_unlock(&ctx->mutex);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012470
12471 /*
12472 * perf_event_release_kernel() could've stolen some of our
12473 * child events and still have them on its free_list. In that
12474 * case we must wait for these events to have been freed (in
12475 * particular all their references to this task must've been
12476 * dropped).
12477 *
12478 * Without this copy_process() will unconditionally free this
12479 * task (irrespective of its reference count) and
12480 * _free_event()'s put_task_struct(event->hw.target) will be a
12481 * use-after-free.
12482 *
12483 * Wait for all events to drop their context reference.
12484 */
12485 wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);
12486 put_ctx(ctx); /* must be last */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012487 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012488}
12489
Peter Zijlstra4e231c72010-09-09 21:01:59 +020012490void perf_event_delayed_put(struct task_struct *task)
12491{
12492 int ctxn;
12493
12494 for_each_task_context_nr(ctxn)
12495 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
12496}
12497
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012498struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000012499{
Al Viro02e5ad92019-06-26 20:43:53 -040012500 struct file *file = fget(fd);
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012501 if (!file)
12502 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000012503
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012504 if (file->f_op != &perf_fops) {
12505 fput(file);
12506 return ERR_PTR(-EBADF);
12507 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000012508
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012509 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000012510}
12511
Yonghong Songf8d959a2018-05-24 11:21:08 -070012512const struct perf_event *perf_get_event(struct file *file)
12513{
12514 if (file->f_op != &perf_fops)
12515 return ERR_PTR(-EINVAL);
12516
12517 return file->private_data;
12518}
12519
Kaixu Xiaffe86902015-08-06 07:02:32 +000012520const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
12521{
12522 if (!event)
12523 return ERR_PTR(-EINVAL);
12524
12525 return &event->attr;
12526}
12527
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012528/*
Tobias Tefke788faab2018-07-09 12:57:15 +020012529 * Inherit an event from parent task to child task.
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012530 *
12531 * Returns:
12532 * - valid pointer on success
12533 * - NULL for orphaned events
12534 * - IS_ERR() on error
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012535 */
12536static struct perf_event *
12537inherit_event(struct perf_event *parent_event,
12538 struct task_struct *parent,
12539 struct perf_event_context *parent_ctx,
12540 struct task_struct *child,
12541 struct perf_event *group_leader,
12542 struct perf_event_context *child_ctx)
12543{
Peter Zijlstra8ca2bd42017-09-05 14:12:35 +020012544 enum perf_event_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012545 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020012546 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012547
12548 /*
12549 * Instead of creating recursive hierarchies of events,
12550 * we link inherited events back to the original parent,
12551 * which has a filp for sure, which we use as the reference
12552 * count:
12553 */
12554 if (parent_event->parent)
12555 parent_event = parent_event->parent;
12556
12557 child_event = perf_event_alloc(&parent_event->attr,
12558 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020012559 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012560 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000012561 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012562 if (IS_ERR(child_event))
12563 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010012564
Jiri Olsa313ccb92018-01-07 17:03:47 +010012565
12566 if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) &&
12567 !child_ctx->task_ctx_data) {
12568 struct pmu *pmu = child_event->pmu;
12569
Kan Liangff9ff922020-07-03 05:49:21 -070012570 child_ctx->task_ctx_data = alloc_task_ctx_data(pmu);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012571 if (!child_ctx->task_ctx_data) {
12572 free_event(child_event);
Alexander Shishkin697d8772019-11-05 09:57:02 +020012573 return ERR_PTR(-ENOMEM);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012574 }
12575 }
12576
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012577 /*
12578 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
12579 * must be under the same lock in order to serialize against
12580 * perf_event_release_kernel(), such that either we must observe
12581 * is_orphaned_event() or they will observe us on the child_list.
12582 */
12583 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020012584 if (is_orphaned_event(parent_event) ||
12585 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012586 mutex_unlock(&parent_event->child_mutex);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012587 /* task_ctx_data is freed with child_ctx */
Al Viroa6fa9412012-08-20 14:59:25 +010012588 free_event(child_event);
12589 return NULL;
12590 }
12591
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012592 get_ctx(child_ctx);
12593
12594 /*
12595 * Make the child state follow the state of the parent event,
12596 * not its attr.disabled bit. We hold the parent's mutex,
12597 * so we won't race with perf_event_{en, dis}able_family.
12598 */
Jiri Olsa1929def2014-09-12 13:18:27 +020012599 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012600 child_event->state = PERF_EVENT_STATE_INACTIVE;
12601 else
12602 child_event->state = PERF_EVENT_STATE_OFF;
12603
12604 if (parent_event->attr.freq) {
12605 u64 sample_period = parent_event->hw.sample_period;
12606 struct hw_perf_event *hwc = &child_event->hw;
12607
12608 hwc->sample_period = sample_period;
12609 hwc->last_period = sample_period;
12610
12611 local64_set(&hwc->period_left, sample_period);
12612 }
12613
12614 child_event->ctx = child_ctx;
12615 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030012616 child_event->overflow_handler_context
12617 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012618
12619 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020012620 * Precalculate sample_data sizes
12621 */
12622 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020012623 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020012624
12625 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012626 * Link it up in the child's context:
12627 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020012628 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012629 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020012630 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012631
12632 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012633 * Link this into the parent event's child list
12634 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012635 list_add_tail(&child_event->child_list, &parent_event->child_list);
12636 mutex_unlock(&parent_event->child_mutex);
12637
12638 return child_event;
12639}
12640
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012641/*
12642 * Inherits an event group.
12643 *
12644 * This will quietly suppress orphaned events; !inherit_event() is not an error.
12645 * This matches with perf_event_release_kernel() removing all child events.
12646 *
12647 * Returns:
12648 * - 0 on success
12649 * - <0 on error
12650 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012651static int inherit_group(struct perf_event *parent_event,
12652 struct task_struct *parent,
12653 struct perf_event_context *parent_ctx,
12654 struct task_struct *child,
12655 struct perf_event_context *child_ctx)
12656{
12657 struct perf_event *leader;
12658 struct perf_event *sub;
12659 struct perf_event *child_ctr;
12660
12661 leader = inherit_event(parent_event, parent, parent_ctx,
12662 child, NULL, child_ctx);
12663 if (IS_ERR(leader))
12664 return PTR_ERR(leader);
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012665 /*
12666 * @leader can be NULL here because of is_orphaned_event(). In this
12667 * case inherit_event() will create individual events, similar to what
12668 * perf_group_detach() would do anyway.
12669 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010012670 for_each_sibling_event(sub, parent_event) {
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012671 child_ctr = inherit_event(sub, parent, parent_ctx,
12672 child, leader, child_ctx);
12673 if (IS_ERR(child_ctr))
12674 return PTR_ERR(child_ctr);
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012675
Alexander Shishkin00496fe2019-11-01 17:12:48 +020012676 if (sub->aux_event == parent_event && child_ctr &&
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012677 !perf_get_aux_event(child_ctr, leader))
12678 return -EINVAL;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012679 }
12680 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012681}
12682
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012683/*
12684 * Creates the child task context and tries to inherit the event-group.
12685 *
12686 * Clears @inherited_all on !attr.inherited or error. Note that we'll leave
12687 * inherited_all set when we 'fail' to inherit an orphaned event; this is
12688 * consistent with perf_event_release_kernel() removing all child events.
12689 *
12690 * Returns:
12691 * - 0 on success
12692 * - <0 on error
12693 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012694static int
12695inherit_task_group(struct perf_event *event, struct task_struct *parent,
12696 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012697 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012698 int *inherited_all)
12699{
12700 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012701 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012702
12703 if (!event->attr.inherit) {
12704 *inherited_all = 0;
12705 return 0;
12706 }
12707
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012708 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012709 if (!child_ctx) {
12710 /*
12711 * This is executed from the parent task context, so
12712 * inherit events that have been marked for cloning.
12713 * First allocate and initialize a context for the
12714 * child.
12715 */
Jiri Olsa734df5a2013-07-09 17:44:10 +020012716 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012717 if (!child_ctx)
12718 return -ENOMEM;
12719
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012720 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012721 }
12722
12723 ret = inherit_group(event, parent, parent_ctx,
12724 child, child_ctx);
12725
12726 if (ret)
12727 *inherited_all = 0;
12728
12729 return ret;
12730}
12731
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012732/*
12733 * Initialize the perf_event context in task_struct
12734 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020012735static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012736{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012737 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012738 struct perf_event_context *cloned_ctx;
12739 struct perf_event *event;
12740 struct task_struct *parent = current;
12741 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012742 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012743 int ret = 0;
12744
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012745 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012746 return 0;
12747
12748 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012749 * If the parent's context is a clone, pin it so it won't get
12750 * swapped under us.
12751 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012752 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020012753 if (!parent_ctx)
12754 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012755
12756 /*
12757 * No need to check if parent_ctx != NULL here; since we saw
12758 * it non-NULL earlier, the only reason for it to become NULL
12759 * is if we exit, and since we're currently in the middle of
12760 * a fork we can't be exiting at the same time.
12761 */
12762
12763 /*
12764 * Lock the parent list. No need to lock the child - not PID
12765 * hashed yet and not running, so nobody can access it.
12766 */
12767 mutex_lock(&parent_ctx->mutex);
12768
12769 /*
12770 * We dont have to disable NMIs - we are only looking at
12771 * the list, not manipulating it:
12772 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012773 perf_event_groups_for_each(event, &parent_ctx->pinned_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012774 ret = inherit_task_group(event, parent, parent_ctx,
12775 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012776 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012777 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012778 }
12779
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012780 /*
12781 * We can't hold ctx->lock when iterating the ->flexible_group list due
12782 * to allocations, but we need to prevent rotation because
12783 * rotate_ctx() will change the list from interrupt context.
12784 */
12785 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12786 parent_ctx->rotate_disable = 1;
12787 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
12788
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012789 perf_event_groups_for_each(event, &parent_ctx->flexible_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012790 ret = inherit_task_group(event, parent, parent_ctx,
12791 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012792 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012793 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012794 }
12795
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012796 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12797 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012798
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012799 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012800
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010012801 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012802 /*
12803 * Mark the child context as a clone of the parent
12804 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012805 *
12806 * Note that if the parent is a clone, the holding of
12807 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012808 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012809 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012810 if (cloned_ctx) {
12811 child_ctx->parent_ctx = cloned_ctx;
12812 child_ctx->parent_gen = parent_ctx->parent_gen;
12813 } else {
12814 child_ctx->parent_ctx = parent_ctx;
12815 child_ctx->parent_gen = parent_ctx->generation;
12816 }
12817 get_ctx(child_ctx->parent_ctx);
12818 }
12819
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012820 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012821out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012822 mutex_unlock(&parent_ctx->mutex);
12823
12824 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012825 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012826
12827 return ret;
12828}
12829
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012830/*
12831 * Initialize the perf_event context in task_struct
12832 */
12833int perf_event_init_task(struct task_struct *child)
12834{
12835 int ctxn, ret;
12836
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010012837 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
12838 mutex_init(&child->perf_event_mutex);
12839 INIT_LIST_HEAD(&child->perf_event_list);
12840
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012841 for_each_task_context_nr(ctxn) {
12842 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012843 if (ret) {
12844 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012845 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012846 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012847 }
12848
12849 return 0;
12850}
12851
Paul Mackerras220b1402010-03-10 20:45:52 +110012852static void __init perf_event_init_all_cpus(void)
12853{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012854 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110012855 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110012856
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012857 zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL);
12858
Paul Mackerras220b1402010-03-10 20:45:52 +110012859 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012860 swhash = &per_cpu(swevent_htable, cpu);
12861 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000012862 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070012863
12864 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
12865 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012866
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -080012867#ifdef CONFIG_CGROUP_PERF
12868 INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu));
12869#endif
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012870 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110012871 }
12872}
12873
Valdis Kletnieksd18bf422019-03-12 04:06:37 -040012874static void perf_swevent_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012875{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012876 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012877
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012878 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000012879 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012880 struct swevent_hlist *hlist;
12881
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012882 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
12883 WARN_ON(!hlist);
12884 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012885 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012886 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012887}
12888
Dave Young2965faa2015-09-09 15:38:55 -070012889#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012890static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012891{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012892 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012893 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
12894 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012895
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012896 raw_spin_lock(&ctx->lock);
Peter Zijlstra0ee098c2017-09-05 13:24:28 +020012897 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012898 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012899 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012900 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012901}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012902
12903static void perf_event_exit_cpu_context(int cpu)
12904{
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012905 struct perf_cpu_context *cpuctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012906 struct perf_event_context *ctx;
12907 struct pmu *pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012908
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012909 mutex_lock(&pmus_lock);
12910 list_for_each_entry(pmu, &pmus, entry) {
12911 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12912 ctx = &cpuctx->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012913
12914 mutex_lock(&ctx->mutex);
12915 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012916 cpuctx->online = 0;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012917 mutex_unlock(&ctx->mutex);
12918 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012919 cpumask_clear_cpu(cpu, perf_online_mask);
12920 mutex_unlock(&pmus_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012921}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012922#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012923
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012924static void perf_event_exit_cpu_context(int cpu) { }
12925
12926#endif
12927
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012928int perf_event_init_cpu(unsigned int cpu)
12929{
12930 struct perf_cpu_context *cpuctx;
12931 struct perf_event_context *ctx;
12932 struct pmu *pmu;
12933
12934 perf_swevent_init_cpu(cpu);
12935
12936 mutex_lock(&pmus_lock);
12937 cpumask_set_cpu(cpu, perf_online_mask);
12938 list_for_each_entry(pmu, &pmus, entry) {
12939 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12940 ctx = &cpuctx->ctx;
12941
12942 mutex_lock(&ctx->mutex);
12943 cpuctx->online = 1;
12944 mutex_unlock(&ctx->mutex);
12945 }
12946 mutex_unlock(&pmus_lock);
12947
12948 return 0;
12949}
12950
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012951int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012952{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010012953 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012954 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012955}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012956
Peter Zijlstrac2774432010-12-08 15:29:02 +010012957static int
12958perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
12959{
12960 int cpu;
12961
12962 for_each_online_cpu(cpu)
12963 perf_event_exit_cpu(cpu);
12964
12965 return NOTIFY_OK;
12966}
12967
12968/*
12969 * Run the perf reboot notifier at the very last possible moment so that
12970 * the generic watchdog code runs as long as possible.
12971 */
12972static struct notifier_block perf_reboot_notifier = {
12973 .notifier_call = perf_reboot,
12974 .priority = INT_MIN,
12975};
12976
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012977void __init perf_event_init(void)
12978{
Jason Wessel3c502e72010-11-04 17:33:01 -050012979 int ret;
12980
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012981 idr_init(&pmu_idr);
12982
Paul Mackerras220b1402010-03-10 20:45:52 +110012983 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012984 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012985 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
12986 perf_pmu_register(&perf_cpu_clock, NULL, -1);
12987 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012988 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012989 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010012990 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050012991
12992 ret = init_hw_breakpoint();
12993 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020012994
Jiri Olsab01c3a02012-03-23 15:41:20 +010012995 /*
12996 * Build time assertion that we keep the data_head at the intended
12997 * location. IOW, validation we got the __reserved[] size right.
12998 */
12999 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
13000 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020013001}
Peter Zijlstraabe43402010-11-17 23:17:37 +010013002
Cody P Schaferfd979c02015-01-30 13:45:57 -080013003ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
13004 char *page)
13005{
13006 struct perf_pmu_events_attr *pmu_attr =
13007 container_of(attr, struct perf_pmu_events_attr, attr);
13008
13009 if (pmu_attr->event_str)
13010 return sprintf(page, "%s\n", pmu_attr->event_str);
13011
13012 return 0;
13013}
Thomas Gleixner675965b2016-02-22 22:19:27 +000013014EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080013015
Peter Zijlstraabe43402010-11-17 23:17:37 +010013016static int __init perf_event_sysfs_init(void)
13017{
13018 struct pmu *pmu;
13019 int ret;
13020
13021 mutex_lock(&pmus_lock);
13022
13023 ret = bus_register(&pmu_bus);
13024 if (ret)
13025 goto unlock;
13026
13027 list_for_each_entry(pmu, &pmus, entry) {
13028 if (!pmu->name || pmu->type < 0)
13029 continue;
13030
13031 ret = pmu_dev_alloc(pmu);
13032 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
13033 }
13034 pmu_bus_running = 1;
13035 ret = 0;
13036
13037unlock:
13038 mutex_unlock(&pmus_lock);
13039
13040 return ret;
13041}
13042device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020013043
13044#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040013045static struct cgroup_subsys_state *
13046perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020013047{
13048 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020013049
Li Zefan1b15d052011-03-03 14:26:06 +080013050 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020013051 if (!jc)
13052 return ERR_PTR(-ENOMEM);
13053
Stephane Eraniane5d13672011-02-14 11:20:01 +020013054 jc->info = alloc_percpu(struct perf_cgroup_info);
13055 if (!jc->info) {
13056 kfree(jc);
13057 return ERR_PTR(-ENOMEM);
13058 }
13059
Stephane Eraniane5d13672011-02-14 11:20:01 +020013060 return &jc->css;
13061}
13062
Tejun Heoeb954192013-08-08 20:11:23 -040013063static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020013064{
Tejun Heoeb954192013-08-08 20:11:23 -040013065 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
13066
Stephane Eraniane5d13672011-02-14 11:20:01 +020013067 free_percpu(jc->info);
13068 kfree(jc);
13069}
13070
Namhyung Kim96aaab62020-03-25 21:45:28 +090013071static int perf_cgroup_css_online(struct cgroup_subsys_state *css)
13072{
13073 perf_event_cgroup(css->cgroup);
13074 return 0;
13075}
13076
Stephane Eraniane5d13672011-02-14 11:20:01 +020013077static int __perf_cgroup_move(void *info)
13078{
13079 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010013080 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020013081 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010013082 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020013083 return 0;
13084}
13085
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050013086static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020013087{
Tejun Heobb9d97b2011-12-12 18:12:21 -080013088 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050013089 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080013090
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050013091 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080013092 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020013093}
13094
Tejun Heo073219e2014-02-08 10:36:58 -050013095struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080013096 .css_alloc = perf_cgroup_css_alloc,
13097 .css_free = perf_cgroup_css_free,
Namhyung Kim96aaab62020-03-25 21:45:28 +090013098 .css_online = perf_cgroup_css_online,
Tejun Heobb9d97b2011-12-12 18:12:21 -080013099 .attach = perf_cgroup_attach,
Tejun Heo968ebff2017-01-29 14:35:20 -050013100 /*
13101 * Implicitly enable on dfl hierarchy so that perf events can
13102 * always be filtered by cgroup2 path as long as perf_event
13103 * controller is not mounted on a legacy hierarchy.
13104 */
13105 .implicit_on_dfl = true,
Tejun Heo8cfd8142017-07-21 11:14:51 -040013106 .threaded = true,
Stephane Eraniane5d13672011-02-14 11:20:01 +020013107};
13108#endif /* CONFIG_CGROUP_PERF */