blob: d1f0a7e5b182abba3254c3cb8fe17113e29027b0 [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
Peter Zijlstra8a495422010-05-27 15:47:49 +02002136static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02002137{
2138 struct perf_event *sibling, *tmp;
Peter Zijlstra66681282017-11-13 14:28:38 +01002139 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002140
Peter Zijlstra66681282017-11-13 14:28:38 +01002141 lockdep_assert_held(&ctx->lock);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002142
Peter Zijlstra8a495422010-05-27 15:47:49 +02002143 /*
2144 * We can have double detach due to exit/hot-unplug + close.
2145 */
2146 if (!(event->attach_state & PERF_ATTACH_GROUP))
2147 return;
2148
2149 event->attach_state &= ~PERF_ATTACH_GROUP;
2150
Alexander Shishkinab437622019-08-06 11:46:00 +03002151 perf_put_aux_event(event);
2152
Peter Zijlstra8a495422010-05-27 15:47:49 +02002153 /*
2154 * If this is a sibling, remove it from its group.
2155 */
2156 if (event->group_leader != event) {
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002157 list_del_init(&event->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002158 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002159 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002160 }
2161
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002162 /*
2163 * If this was a group event with sibling events then
2164 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02002165 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166 */
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002167 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169 sibling->group_leader = sibling;
Mark Rutland24868362018-03-16 12:51:40 +00002170 list_del_init(&sibling->sibling_list);
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002171
2172 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002173 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002174
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002175 if (!RB_EMPTY_NODE(&event->group_node)) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002176 add_event_to_groups(sibling, event->ctx);
Peter Zijlstra66681282017-11-13 14:28:38 +01002177
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002178 if (sibling->state == PERF_EVENT_STATE_ACTIVE)
2179 list_add_tail(&sibling->active_list, get_event_list(sibling));
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002180 }
2181
Peter Zijlstra652884f2015-01-23 11:20:10 +01002182 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002183 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002184
2185out:
2186 perf_event__header_size(event->group_leader);
2187
Peter Zijlstraedb39592018-03-15 17:36:56 +01002188 for_each_sibling_event(tmp, event->group_leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002189 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190}
2191
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002192static bool is_orphaned_event(struct perf_event *event)
2193{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01002194 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002195}
2196
Mark Rutland2c81a642016-06-14 16:10:41 +01002197static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01002198{
2199 struct pmu *pmu = event->pmu;
2200 return pmu->filter_match ? pmu->filter_match(event) : 1;
2201}
2202
Mark Rutland2c81a642016-06-14 16:10:41 +01002203/*
2204 * Check whether we should attempt to schedule an event group based on
2205 * PMU-specific filtering. An event group can consist of HW and SW events,
2206 * potentially with a SW leader, so we must check all the filters, to
2207 * determine whether a group is schedulable:
2208 */
2209static inline int pmu_filter_match(struct perf_event *event)
2210{
Peter Zijlstraedb39592018-03-15 17:36:56 +01002211 struct perf_event *sibling;
Mark Rutland2c81a642016-06-14 16:10:41 +01002212
2213 if (!__pmu_filter_match(event))
2214 return 0;
2215
Peter Zijlstraedb39592018-03-15 17:36:56 +01002216 for_each_sibling_event(sibling, event) {
2217 if (!__pmu_filter_match(sibling))
Mark Rutland2c81a642016-06-14 16:10:41 +01002218 return 0;
2219 }
2220
2221 return 1;
2222}
2223
Stephane Eranianfa66f072010-08-26 16:40:01 +02002224static inline int
2225event_filter_match(struct perf_event *event)
2226{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002227 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
2228 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02002229}
2230
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002231static void
2232event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233 struct perf_cpu_context *cpuctx,
2234 struct perf_event_context *ctx)
2235{
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002236 enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002237
2238 WARN_ON_ONCE(event->ctx != ctx);
2239 lockdep_assert_held(&ctx->lock);
2240
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002241 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002242 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002243
Peter Zijlstra66681282017-11-13 14:28:38 +01002244 /*
2245 * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but
2246 * we can schedule events _OUT_ individually through things like
2247 * __perf_remove_from_context().
2248 */
2249 list_del_init(&event->active_list);
2250
Alexander Shishkin44377272013-12-16 14:17:36 +02002251 perf_pmu_disable(event->pmu);
2252
Peter Zijlstra28a967c2016-02-24 18:45:46 +01002253 event->pmu->del(event, 0);
2254 event->oncpu = -1;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002255
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002256 if (READ_ONCE(event->pending_disable) >= 0) {
2257 WRITE_ONCE(event->pending_disable, -1);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002258 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002259 state = PERF_EVENT_STATE_OFF;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002260 }
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002261 perf_event_set_state(event, state);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262
2263 if (!is_software_event(event))
2264 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002265 if (!--ctx->nr_active)
2266 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002267 if (event->attr.freq && event->attr.sample_freq)
2268 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269 if (event->attr.exclusive || !cpuctx->active_oncpu)
2270 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02002271
2272 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002273}
2274
2275static void
2276group_sched_out(struct perf_event *group_event,
2277 struct perf_cpu_context *cpuctx,
2278 struct perf_event_context *ctx)
2279{
2280 struct perf_event *event;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002281
2282 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
2283 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284
Mark Rutland3f005e72016-07-26 18:12:21 +01002285 perf_pmu_disable(ctx->pmu);
2286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002287 event_sched_out(group_event, cpuctx, ctx);
2288
2289 /*
2290 * Schedule out siblings (if any):
2291 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002292 for_each_sibling_event(event, group_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293 event_sched_out(event, cpuctx, ctx);
2294
Mark Rutland3f005e72016-07-26 18:12:21 +01002295 perf_pmu_enable(ctx->pmu);
2296
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002297 if (group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002298 cpuctx->exclusive = 0;
2299}
2300
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002301#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01002302
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303/*
2304 * Cross CPU call to remove a performance event
2305 *
2306 * We disable the event on the hardware level first. After that we
2307 * remove it from the context list.
2308 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002309static void
2310__perf_remove_from_context(struct perf_event *event,
2311 struct perf_cpu_context *cpuctx,
2312 struct perf_event_context *ctx,
2313 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002315 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002317 if (ctx->is_active & EVENT_TIME) {
2318 update_context_time(ctx);
2319 update_cgrp_time_from_cpuctx(cpuctx);
2320 }
2321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002322 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002323 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02002324 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002326
Peter Zijlstra39a43642016-01-11 12:46:35 +01002327 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002328 ctx->is_active = 0;
Peter Zijlstra90c91df2020-03-05 13:38:51 +01002329 ctx->rotate_necessary = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01002330 if (ctx->task) {
2331 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2332 cpuctx->task_ctx = NULL;
2333 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335}
2336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337/*
2338 * Remove the event from a task's (or a CPU's) list of events.
2339 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340 * If event->ctx is a cloned context, callers must make sure that
2341 * every task struct that event->ctx->task could possibly point to
2342 * remains valid. This is OK when called from perf_release since
2343 * that only calls us on the top-level context, which can't be a clone.
2344 * When called from perf_event_exit_task, it's OK because the
2345 * context has been detached from its task.
2346 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002347static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002348{
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002349 struct perf_event_context *ctx = event->ctx;
2350
2351 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002353 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002354
2355 /*
2356 * The above event_function_call() can NO-OP when it hits
2357 * TASK_TOMBSTONE. In that case we must already have been detached
2358 * from the context (by perf_event_exit_event()) but the grouping
2359 * might still be in-tact.
2360 */
2361 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
2362 if ((flags & DETACH_GROUP) &&
2363 (event->attach_state & PERF_ATTACH_GROUP)) {
2364 /*
2365 * Since in that case we cannot possibly be scheduled, simply
2366 * detach now.
2367 */
2368 raw_spin_lock_irq(&ctx->lock);
2369 perf_group_detach(event);
2370 raw_spin_unlock_irq(&ctx->lock);
2371 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372}
2373
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375 * Cross CPU call to disable a performance event
2376 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002377static void __perf_event_disable(struct perf_event *event,
2378 struct perf_cpu_context *cpuctx,
2379 struct perf_event_context *ctx,
2380 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002381{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002382 if (event->state < PERF_EVENT_STATE_INACTIVE)
2383 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002385 if (ctx->is_active & EVENT_TIME) {
2386 update_context_time(ctx);
2387 update_cgrp_time_from_event(event);
2388 }
2389
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002390 if (event == event->group_leader)
2391 group_sched_out(event, cpuctx, ctx);
2392 else
2393 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002394
2395 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002396 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002397}
2398
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002399/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002400 * Disable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401 *
2402 * If event->ctx is a cloned context, callers must make sure that
2403 * every task struct that event->ctx->task could possibly point to
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03002404 * remains valid. This condition is satisfied when called through
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002405 * perf_event_for_each_child or perf_event_for_each because they
2406 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01002407 * goes to exit will block in perf_event_exit_event().
2408 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002409 * When called from perf_pending_event it's OK because event->ctx
2410 * is the current context on this CPU and preemption is disabled,
2411 * hence we can't get into perf_event_task_sched_out for this context.
2412 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002413static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414{
2415 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002416
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002417 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002418 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002419 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002420 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002422 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002423
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002424 event_function_call(event, __perf_event_disable, NULL);
2425}
2426
2427void perf_event_disable_local(struct perf_event *event)
2428{
2429 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002430}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002431
2432/*
2433 * Strictly speaking kernel users cannot create groups and therefore this
2434 * interface does not need the perf_event_ctx_lock() magic.
2435 */
2436void perf_event_disable(struct perf_event *event)
2437{
2438 struct perf_event_context *ctx;
2439
2440 ctx = perf_event_ctx_lock(event);
2441 _perf_event_disable(event);
2442 perf_event_ctx_unlock(event, ctx);
2443}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002444EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002445
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002446void perf_event_disable_inatomic(struct perf_event *event)
2447{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002448 WRITE_ONCE(event->pending_disable, smp_processor_id());
2449 /* can fail, see perf_pending_event_disable() */
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002450 irq_work_queue(&event->pending);
2451}
2452
Stephane Eraniane5d13672011-02-14 11:20:01 +02002453static void perf_set_shadow_time(struct perf_event *event,
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002454 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002455{
2456 /*
2457 * use the correct time source for the time snapshot
2458 *
2459 * We could get by without this by leveraging the
2460 * fact that to get to this function, the caller
2461 * has most likely already called update_context_time()
2462 * and update_cgrp_time_xx() and thus both timestamp
2463 * are identical (or very close). Given that tstamp is,
2464 * already adjusted for cgroup, we could say that:
2465 * tstamp - ctx->timestamp
2466 * is equivalent to
2467 * tstamp - cgrp->timestamp.
2468 *
2469 * Then, in perf_output_read(), the calculation would
2470 * work with no changes because:
2471 * - event is guaranteed scheduled in
2472 * - no scheduled out in between
2473 * - thus the timestamp would be the same
2474 *
2475 * But this is a bit hairy.
2476 *
2477 * So instead, we have an explicit cgroup call to remain
2478 * within the time time source all along. We believe it
2479 * is cleaner and simpler to understand.
2480 */
2481 if (is_cgroup_event(event))
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002482 perf_cgroup_set_shadow_time(event, event->tstamp);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002483 else
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002484 event->shadow_ctx_time = event->tstamp - ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002485}
2486
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002487#define MAX_INTERRUPTS (~0ULL)
2488
2489static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002490static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002491
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002492static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002493event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002494 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002495 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002496{
Alexander Shishkin44377272013-12-16 14:17:36 +02002497 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002498
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002499 WARN_ON_ONCE(event->ctx != ctx);
2500
Peter Zijlstra63342412014-05-05 11:49:16 +02002501 lockdep_assert_held(&ctx->lock);
2502
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002503 if (event->state <= PERF_EVENT_STATE_OFF)
2504 return 0;
2505
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002506 WRITE_ONCE(event->oncpu, smp_processor_id());
2507 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02002508 * Order event::oncpu write to happen before the ACTIVE state is
2509 * visible. This allows perf_event_{stop,read}() to observe the correct
2510 * ->oncpu if it sees ACTIVE.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002511 */
2512 smp_wmb();
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002513 perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002514
2515 /*
2516 * Unthrottle events, since we scheduled we might have missed several
2517 * ticks already, also for a heavily scheduling task there is little
2518 * guarantee it'll get a tick in a timely manner.
2519 */
2520 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2521 perf_log_throttle(event, 1);
2522 event->hw.interrupts = 0;
2523 }
2524
Alexander Shishkin44377272013-12-16 14:17:36 +02002525 perf_pmu_disable(event->pmu);
2526
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002527 perf_set_shadow_time(event, ctx);
Shaohua Li72f669c2015-02-05 15:55:31 -08002528
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002529 perf_log_itrace_start(event);
2530
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002531 if (event->pmu->add(event, PERF_EF_START)) {
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002532 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002533 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002534 ret = -EAGAIN;
2535 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002536 }
2537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002538 if (!is_software_event(event))
2539 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002540 if (!ctx->nr_active++)
2541 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002542 if (event->attr.freq && event->attr.sample_freq)
2543 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002544
2545 if (event->attr.exclusive)
2546 cpuctx->exclusive = 1;
2547
Alexander Shishkin44377272013-12-16 14:17:36 +02002548out:
2549 perf_pmu_enable(event->pmu);
2550
2551 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002552}
2553
2554static int
2555group_sched_in(struct perf_event *group_event,
2556 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002557 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002558{
Lin Ming6bde9b62010-04-23 13:56:00 +08002559 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002560 struct pmu *pmu = ctx->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002561
2562 if (group_event->state == PERF_EVENT_STATE_OFF)
2563 return 0;
2564
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002565 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002566
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002567 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002568 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002569 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002570 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002571 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002572
2573 /*
2574 * Schedule in siblings as one group (if any):
2575 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002576 for_each_sibling_event(event, group_event) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002577 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002578 partial_group = event;
2579 goto group_error;
2580 }
2581 }
2582
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002583 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002584 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002585
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002586group_error:
2587 /*
2588 * Groups can be scheduled in as one unit only, so undo any
2589 * partial group before returning:
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002590 * The events up to the failed event are scheduled out normally.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002592 for_each_sibling_event(event, group_event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002593 if (event == partial_group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002594 break;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002595
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002596 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002597 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002598 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002599
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002600 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002601
Peter Zijlstra272325c2015-04-15 11:41:58 +02002602 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002603
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002604 return -EAGAIN;
2605}
2606
2607/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002608 * Work out whether we can put this event group on the CPU now.
2609 */
2610static int group_can_go_on(struct perf_event *event,
2611 struct perf_cpu_context *cpuctx,
2612 int can_add_hw)
2613{
2614 /*
2615 * Groups consisting entirely of software events can always go on.
2616 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002617 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002618 return 1;
2619 /*
2620 * If an exclusive group is already on, no other hardware
2621 * events can go on.
2622 */
2623 if (cpuctx->exclusive)
2624 return 0;
2625 /*
2626 * If this group is exclusive and there are already
2627 * events on the CPU, it can't go on.
2628 */
2629 if (event->attr.exclusive && cpuctx->active_oncpu)
2630 return 0;
2631 /*
2632 * Otherwise, try to add it if all previous groups were able
2633 * to go on.
2634 */
2635 return can_add_hw;
2636}
2637
2638static void add_event_to_ctx(struct perf_event *event,
2639 struct perf_event_context *ctx)
2640{
2641 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002642 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002643}
2644
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002645static void ctx_sched_out(struct perf_event_context *ctx,
2646 struct perf_cpu_context *cpuctx,
2647 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002648static void
2649ctx_sched_in(struct perf_event_context *ctx,
2650 struct perf_cpu_context *cpuctx,
2651 enum event_type_t event_type,
2652 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002653
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002654static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002655 struct perf_event_context *ctx,
2656 enum event_type_t event_type)
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002657{
2658 if (!cpuctx->task_ctx)
2659 return;
2660
2661 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2662 return;
2663
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002664 ctx_sched_out(ctx, cpuctx, event_type);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002665}
2666
Peter Zijlstradce58552011-04-09 21:17:46 +02002667static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2668 struct perf_event_context *ctx,
2669 struct task_struct *task)
2670{
2671 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2672 if (ctx)
2673 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2674 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2675 if (ctx)
2676 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2677}
2678
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002679/*
2680 * We want to maintain the following priority of scheduling:
2681 * - CPU pinned (EVENT_CPU | EVENT_PINNED)
2682 * - task pinned (EVENT_PINNED)
2683 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
2684 * - task flexible (EVENT_FLEXIBLE).
2685 *
2686 * In order to avoid unscheduling and scheduling back in everything every
2687 * time an event is added, only do it for the groups of equal priority and
2688 * below.
2689 *
2690 * This can be called after a batch operation on task events, in which case
2691 * event_type is a bit mask of the types of events involved. For CPU events,
2692 * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
2693 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01002694static void ctx_resched(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002695 struct perf_event_context *task_ctx,
2696 enum event_type_t event_type)
Peter Zijlstra00179602015-11-30 16:26:35 +01002697{
Song Liubd903af2018-03-05 21:55:04 -08002698 enum event_type_t ctx_event_type;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002699 bool cpu_event = !!(event_type & EVENT_CPU);
2700
2701 /*
2702 * If pinned groups are involved, flexible groups also need to be
2703 * scheduled out.
2704 */
2705 if (event_type & EVENT_PINNED)
2706 event_type |= EVENT_FLEXIBLE;
2707
Song Liubd903af2018-03-05 21:55:04 -08002708 ctx_event_type = event_type & EVENT_ALL;
2709
Peter Zijlstra3e349502016-01-08 10:01:18 +01002710 perf_pmu_disable(cpuctx->ctx.pmu);
2711 if (task_ctx)
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002712 task_ctx_sched_out(cpuctx, task_ctx, event_type);
2713
2714 /*
2715 * Decide which cpu ctx groups to schedule out based on the types
2716 * of events that caused rescheduling:
2717 * - EVENT_CPU: schedule out corresponding groups;
2718 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups;
2719 * - otherwise, do nothing more.
2720 */
2721 if (cpu_event)
2722 cpu_ctx_sched_out(cpuctx, ctx_event_type);
2723 else if (ctx_event_type & EVENT_PINNED)
2724 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2725
Peter Zijlstra3e349502016-01-08 10:01:18 +01002726 perf_event_sched_in(cpuctx, task_ctx, current);
2727 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002728}
2729
Stephane Eranianc68d2242019-04-08 10:32:51 -07002730void perf_pmu_resched(struct pmu *pmu)
2731{
2732 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2733 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2734
2735 perf_ctx_lock(cpuctx, task_ctx);
2736 ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU);
2737 perf_ctx_unlock(cpuctx, task_ctx);
2738}
2739
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740/*
2741 * Cross CPU call to install and enable a performance event
2742 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002743 * Very similar to remote_function() + event_function() but cannot assume that
2744 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002746static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002748 struct perf_event *event = info;
2749 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002750 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002751 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63cae122016-12-09 14:59:00 +01002752 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002753 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002754
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002755 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002756 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002757 raw_spin_lock(&ctx->lock);
2758 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002759
Peter Zijlstra63cae122016-12-09 14:59:00 +01002760 reprogram = (ctx->task == current);
2761
2762 /*
2763 * If the task is running, it must be running on this CPU,
2764 * otherwise we cannot reprogram things.
2765 *
2766 * If its not running, we don't care, ctx->lock will
2767 * serialize against it becoming runnable.
2768 */
2769 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002770 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002771 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002772 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773
Peter Zijlstra63cae122016-12-09 14:59:00 +01002774 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002775 } else if (task_ctx) {
2776 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002777 }
2778
leilei.lin33801b92018-03-06 17:36:37 +08002779#ifdef CONFIG_CGROUP_PERF
Peter Zijlstra33238c52020-03-18 20:33:37 +01002780 if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) {
leilei.lin33801b92018-03-06 17:36:37 +08002781 /*
2782 * If the current cgroup doesn't match the event's
2783 * cgroup, we should not try to schedule it.
2784 */
2785 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
2786 reprogram = cgroup_is_descendant(cgrp->css.cgroup,
2787 event->cgrp->css.cgroup);
2788 }
2789#endif
2790
Peter Zijlstra63cae122016-12-09 14:59:00 +01002791 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002792 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2793 add_event_to_ctx(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002794 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstraa0963092016-02-24 18:45:50 +01002795 } else {
2796 add_event_to_ctx(event, ctx);
2797 }
2798
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002799unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002800 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002801
Peter Zijlstraa0963092016-02-24 18:45:50 +01002802 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002803}
2804
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002805static bool exclusive_event_installable(struct perf_event *event,
2806 struct perf_event_context *ctx);
2807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002809 * Attach a performance event to a context.
2810 *
2811 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002812 */
2813static void
2814perf_install_in_context(struct perf_event_context *ctx,
2815 struct perf_event *event,
2816 int cpu)
2817{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002818 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002819
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002820 lockdep_assert_held(&ctx->mutex);
2821
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002822 WARN_ON_ONCE(!exclusive_event_installable(event, ctx));
2823
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002824 if (event->cpu != -1)
2825 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002826
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002827 /*
2828 * Ensures that if we can observe event->ctx, both the event and ctx
2829 * will be 'complete'. See perf_iterate_sb_cpu().
2830 */
2831 smp_store_release(&event->ctx, ctx);
2832
Peter Zijlstradb0503e2019-10-21 16:02:39 +02002833 /*
2834 * perf_event_attr::disabled events will not run and can be initialized
2835 * without IPI. Except when this is the first event for the context, in
2836 * that case we need the magic of the IPI to set ctx->is_active.
2837 *
2838 * The IOC_ENABLE that is sure to follow the creation of a disabled
2839 * event will issue the IPI and reprogram the hardware.
2840 */
2841 if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && ctx->nr_events) {
2842 raw_spin_lock_irq(&ctx->lock);
2843 if (ctx->task == TASK_TOMBSTONE) {
2844 raw_spin_unlock_irq(&ctx->lock);
2845 return;
2846 }
2847 add_event_to_ctx(event, ctx);
2848 raw_spin_unlock_irq(&ctx->lock);
2849 return;
2850 }
2851
Peter Zijlstraa0963092016-02-24 18:45:50 +01002852 if (!task) {
2853 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002854 return;
2855 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002856
Peter Zijlstraa0963092016-02-24 18:45:50 +01002857 /*
2858 * Should not happen, we validate the ctx is still alive before calling.
2859 */
2860 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2861 return;
2862
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002863 /*
2864 * Installing events is tricky because we cannot rely on ctx->is_active
2865 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra63cae122016-12-09 14:59:00 +01002866 *
2867 * Instead we use task_curr(), which tells us if the task is running.
2868 * However, since we use task_curr() outside of rq::lock, we can race
2869 * against the actual state. This means the result can be wrong.
2870 *
2871 * If we get a false positive, we retry, this is harmless.
2872 *
2873 * If we get a false negative, things are complicated. If we are after
2874 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2875 * value must be correct. If we're before, it doesn't matter since
2876 * perf_event_context_sched_in() will program the counter.
2877 *
2878 * However, this hinges on the remote context switch having observed
2879 * our task->perf_event_ctxp[] store, such that it will in fact take
2880 * ctx::lock in perf_event_context_sched_in().
2881 *
2882 * We do this by task_function_call(), if the IPI fails to hit the task
2883 * we know any future context switch of task must see the
2884 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002885 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002886
Peter Zijlstraa0963092016-02-24 18:45:50 +01002887 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002888 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2889 * task_cpu() load, such that if the IPI then does not find the task
2890 * running, a future context switch of that task must observe the
2891 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002892 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002893 smp_mb();
2894again:
2895 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002896 return;
2897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898 raw_spin_lock_irq(&ctx->lock);
2899 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002900 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2901 /*
2902 * Cannot happen because we already checked above (which also
2903 * cannot happen), and we hold ctx->mutex, which serializes us
2904 * against perf_event_exit_task_context().
2905 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002906 raw_spin_unlock_irq(&ctx->lock);
2907 return;
2908 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002909 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002910 * If the task is not running, ctx->lock will avoid it becoming so,
2911 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002912 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002913 if (task_curr(task)) {
2914 raw_spin_unlock_irq(&ctx->lock);
2915 goto again;
2916 }
2917 add_event_to_ctx(event, ctx);
2918 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919}
2920
2921/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002922 * Cross CPU call to enable a performance event
2923 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002924static void __perf_event_enable(struct perf_event *event,
2925 struct perf_cpu_context *cpuctx,
2926 struct perf_event_context *ctx,
2927 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002928{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002929 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002930 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002931
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002932 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2933 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002934 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002936 if (ctx->is_active)
2937 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2938
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002939 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002940 perf_cgroup_event_enable(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002941
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002942 if (!ctx->is_active)
2943 return;
2944
Stephane Eraniane5d13672011-02-14 11:20:01 +02002945 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002946 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002947 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002948 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002949
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950 /*
2951 * If the event is in a group and isn't the group leader,
2952 * then don't put it on unless the group is on.
2953 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002954 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2955 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002956 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002957 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002958
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002959 task_ctx = cpuctx->task_ctx;
2960 if (ctx->task)
2961 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002962
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002963 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstra7b648012015-12-03 18:35:21 +01002964}
2965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002967 * Enable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968 *
2969 * If event->ctx is a cloned context, callers must make sure that
2970 * every task struct that event->ctx->task could possibly point to
2971 * remains valid. This condition is satisfied when called through
2972 * perf_event_for_each_child or perf_event_for_each as described
2973 * for perf_event_disable.
2974 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002975static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976{
2977 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002978
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002979 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002980 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2981 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002982 raw_spin_unlock_irq(&ctx->lock);
2983 return;
2984 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002985
2986 /*
2987 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002988 *
2989 * That way, if we see the event in error state below, we know that it
2990 * has gone back into error state, as distinct from the task having
2991 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992 */
2993 if (event->state == PERF_EVENT_STATE_ERROR)
2994 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002995 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002996
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002997 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002999
3000/*
3001 * See perf_event_disable();
3002 */
3003void perf_event_enable(struct perf_event *event)
3004{
3005 struct perf_event_context *ctx;
3006
3007 ctx = perf_event_ctx_lock(event);
3008 _perf_event_enable(event);
3009 perf_event_ctx_unlock(event, ctx);
3010}
Robert Richterdcfce4a2011-10-11 17:11:08 +02003011EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012
Alexander Shishkin375637b2016-04-27 18:44:46 +03003013struct stop_event_data {
3014 struct perf_event *event;
3015 unsigned int restart;
3016};
3017
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003018static int __perf_event_stop(void *info)
3019{
Alexander Shishkin375637b2016-04-27 18:44:46 +03003020 struct stop_event_data *sd = info;
3021 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003022
Alexander Shishkin375637b2016-04-27 18:44:46 +03003023 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003024 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
3025 return 0;
3026
3027 /* matches smp_wmb() in event_sched_in() */
3028 smp_rmb();
3029
3030 /*
3031 * There is a window with interrupts enabled before we get here,
3032 * so we need to check again lest we try to stop another CPU's event.
3033 */
3034 if (READ_ONCE(event->oncpu) != smp_processor_id())
3035 return -EAGAIN;
3036
3037 event->pmu->stop(event, PERF_EF_UPDATE);
3038
Alexander Shishkin375637b2016-04-27 18:44:46 +03003039 /*
3040 * May race with the actual stop (through perf_pmu_output_stop()),
3041 * but it is only used for events with AUX ring buffer, and such
3042 * events will refuse to restart because of rb::aux_mmap_count==0,
3043 * see comments in perf_aux_output_begin().
3044 *
Tobias Tefke788faab2018-07-09 12:57:15 +02003045 * Since this is happening on an event-local CPU, no trace is lost
Alexander Shishkin375637b2016-04-27 18:44:46 +03003046 * while restarting.
3047 */
3048 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01003049 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003050
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003051 return 0;
3052}
3053
Alexander Shishkin767ae082016-09-06 16:23:49 +03003054static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03003055{
3056 struct stop_event_data sd = {
3057 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03003058 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03003059 };
3060 int ret = 0;
3061
3062 do {
3063 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
3064 return 0;
3065
3066 /* matches smp_wmb() in event_sched_in() */
3067 smp_rmb();
3068
3069 /*
3070 * We only want to restart ACTIVE events, so if the event goes
3071 * inactive here (event->oncpu==-1), there's nothing more to do;
3072 * fall through with ret==-ENXIO.
3073 */
3074 ret = cpu_function_call(READ_ONCE(event->oncpu),
3075 __perf_event_stop, &sd);
3076 } while (ret == -EAGAIN);
3077
3078 return ret;
3079}
3080
3081/*
3082 * In order to contain the amount of racy and tricky in the address filter
3083 * configuration management, it is a two part process:
3084 *
3085 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
3086 * we update the addresses of corresponding vmas in
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02003087 * event::addr_filter_ranges array and bump the event::addr_filters_gen;
Alexander Shishkin375637b2016-04-27 18:44:46 +03003088 * (p2) when an event is scheduled in (pmu::add), it calls
3089 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
3090 * if the generation has changed since the previous call.
3091 *
3092 * If (p1) happens while the event is active, we restart it to force (p2).
3093 *
3094 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
3095 * pre-existing mappings, called once when new filters arrive via SET_FILTER
3096 * ioctl;
3097 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003098 * registered mapping, called for every new mmap(), with mm::mmap_lock down
Alexander Shishkin375637b2016-04-27 18:44:46 +03003099 * for reading;
3100 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
3101 * of exec.
3102 */
3103void perf_event_addr_filters_sync(struct perf_event *event)
3104{
3105 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
3106
3107 if (!has_addr_filter(event))
3108 return;
3109
3110 raw_spin_lock(&ifh->lock);
3111 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
3112 event->pmu->addr_filters_sync(event);
3113 event->hw.addr_filters_gen = event->addr_filters_gen;
3114 }
3115 raw_spin_unlock(&ifh->lock);
3116}
3117EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
3118
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003119static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120{
3121 /*
3122 * not supported on inherited events
3123 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01003124 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003125 return -EINVAL;
3126
3127 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003128 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003129
3130 return 0;
3131}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003132
3133/*
3134 * See perf_event_disable()
3135 */
3136int perf_event_refresh(struct perf_event *event, int refresh)
3137{
3138 struct perf_event_context *ctx;
3139 int ret;
3140
3141 ctx = perf_event_ctx_lock(event);
3142 ret = _perf_event_refresh(event, refresh);
3143 perf_event_ctx_unlock(event, ctx);
3144
3145 return ret;
3146}
Avi Kivity26ca5c12011-06-29 18:42:37 +03003147EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003149static int perf_event_modify_breakpoint(struct perf_event *bp,
3150 struct perf_event_attr *attr)
3151{
3152 int err;
3153
3154 _perf_event_disable(bp);
3155
3156 err = modify_user_hw_breakpoint_check(bp, attr, true);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003157
Jiri Olsabf062782018-08-27 11:12:28 +02003158 if (!bp->attr.disabled)
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003159 _perf_event_enable(bp);
Jiri Olsabf062782018-08-27 11:12:28 +02003160
3161 return err;
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003162}
3163
3164static int perf_event_modify_attr(struct perf_event *event,
3165 struct perf_event_attr *attr)
3166{
3167 if (event->attr.type != attr->type)
3168 return -EINVAL;
3169
3170 switch (event->attr.type) {
3171 case PERF_TYPE_BREAKPOINT:
3172 return perf_event_modify_breakpoint(event, attr);
3173 default:
3174 /* Place holder for future additions. */
3175 return -EOPNOTSUPP;
3176 }
3177}
3178
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003179static void ctx_sched_out(struct perf_event_context *ctx,
3180 struct perf_cpu_context *cpuctx,
3181 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003182{
Peter Zijlstra66681282017-11-13 14:28:38 +01003183 struct perf_event *event, *tmp;
Peter Zijlstradb24d332011-04-09 21:17:45 +02003184 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003185
3186 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187
Peter Zijlstra39a43642016-01-11 12:46:35 +01003188 if (likely(!ctx->nr_events)) {
3189 /*
3190 * See __perf_remove_from_context().
3191 */
3192 WARN_ON_ONCE(ctx->is_active);
3193 if (ctx->task)
3194 WARN_ON_ONCE(cpuctx->task_ctx);
3195 return;
3196 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197
Peter Zijlstradb24d332011-04-09 21:17:45 +02003198 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003199 if (!(ctx->is_active & EVENT_ALL))
3200 ctx->is_active = 0;
3201
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003202 if (ctx->task) {
3203 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3204 if (!ctx->is_active)
3205 cpuctx->task_ctx = NULL;
3206 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003207
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003208 /*
3209 * Always update time if it was set; not only when it changes.
3210 * Otherwise we can 'forget' to update time for any but the last
3211 * context we sched out. For example:
3212 *
3213 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
3214 * ctx_sched_out(.event_type = EVENT_PINNED)
3215 *
3216 * would only update time for the pinned events.
3217 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003218 if (is_active & EVENT_TIME) {
3219 /* update (and stop) ctx time */
3220 update_context_time(ctx);
3221 update_cgrp_time_from_cpuctx(cpuctx);
3222 }
3223
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003224 is_active ^= ctx->is_active; /* changed bits */
3225
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003226 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003227 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003228
Peter Zijlstra075e0b02011-04-09 21:17:40 +02003229 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003230 if (is_active & EVENT_PINNED) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003231 list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003232 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003233 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003234
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003235 if (is_active & EVENT_FLEXIBLE) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003236 list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003237 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra90c91df2020-03-05 13:38:51 +01003238
3239 /*
3240 * Since we cleared EVENT_FLEXIBLE, also clear
3241 * rotate_necessary, is will be reset by
3242 * ctx_flexible_sched_in() when needed.
3243 */
3244 ctx->rotate_necessary = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003245 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003246 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247}
3248
3249/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003250 * Test whether two contexts are equivalent, i.e. whether they have both been
3251 * cloned from the same version of the same context.
3252 *
3253 * Equivalence is measured using a generation number in the context that is
3254 * incremented on each modification to it; see unclone_ctx(), list_add_event()
3255 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003256 */
3257static int context_equiv(struct perf_event_context *ctx1,
3258 struct perf_event_context *ctx2)
3259{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003260 lockdep_assert_held(&ctx1->lock);
3261 lockdep_assert_held(&ctx2->lock);
3262
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003263 /* Pinning disables the swap optimization */
3264 if (ctx1->pin_count || ctx2->pin_count)
3265 return 0;
3266
3267 /* If ctx1 is the parent of ctx2 */
3268 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
3269 return 1;
3270
3271 /* If ctx2 is the parent of ctx1 */
3272 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
3273 return 1;
3274
3275 /*
3276 * If ctx1 and ctx2 have the same parent; we flatten the parent
3277 * hierarchy, see perf_event_init_context().
3278 */
3279 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
3280 ctx1->parent_gen == ctx2->parent_gen)
3281 return 1;
3282
3283 /* Unmatched */
3284 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003285}
3286
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003287static void __perf_event_sync_stat(struct perf_event *event,
3288 struct perf_event *next_event)
3289{
3290 u64 value;
3291
3292 if (!event->attr.inherit_stat)
3293 return;
3294
3295 /*
3296 * Update the event value, we cannot use perf_event_read()
3297 * because we're in the middle of a context switch and have IRQs
3298 * disabled, which upsets smp_call_function_single(), however
3299 * we know the event must be on the current CPU, therefore we
3300 * don't need to use it.
3301 */
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003302 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01003303 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003305 perf_event_update_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003306
3307 /*
3308 * In order to keep per-task stats reliable we need to flip the event
3309 * values when we flip the contexts.
3310 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02003311 value = local64_read(&next_event->count);
3312 value = local64_xchg(&event->count, value);
3313 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003314
3315 swap(event->total_time_enabled, next_event->total_time_enabled);
3316 swap(event->total_time_running, next_event->total_time_running);
3317
3318 /*
3319 * Since we swizzled the values, update the user visible data too.
3320 */
3321 perf_event_update_userpage(event);
3322 perf_event_update_userpage(next_event);
3323}
3324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325static void perf_event_sync_stat(struct perf_event_context *ctx,
3326 struct perf_event_context *next_ctx)
3327{
3328 struct perf_event *event, *next_event;
3329
3330 if (!ctx->nr_stat)
3331 return;
3332
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01003333 update_context_time(ctx);
3334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335 event = list_first_entry(&ctx->event_list,
3336 struct perf_event, event_entry);
3337
3338 next_event = list_first_entry(&next_ctx->event_list,
3339 struct perf_event, event_entry);
3340
3341 while (&event->event_entry != &ctx->event_list &&
3342 &next_event->event_entry != &next_ctx->event_list) {
3343
3344 __perf_event_sync_stat(event, next_event);
3345
3346 event = list_next_entry(event, event_entry);
3347 next_event = list_next_entry(next_event, event_entry);
3348 }
3349}
3350
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003351static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
3352 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003354 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003355 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003356 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003357 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358 int do_switch = 1;
3359
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003360 if (likely(!ctx))
3361 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003362
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003363 cpuctx = __get_cpu_context(ctx);
3364 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365 return;
3366
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003368 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003369 if (!next_ctx)
3370 goto unlock;
3371
3372 parent = rcu_dereference(ctx->parent_ctx);
3373 next_parent = rcu_dereference(next_ctx->parent_ctx);
3374
3375 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02003376 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003377 goto unlock;
3378
3379 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003380 /*
3381 * Looks like the two contexts are clones, so we might be
3382 * able to optimize the context switch. We lock both
3383 * contexts and check that they are clones under the
3384 * lock (including re-checking that neither has been
3385 * uncloned in the meantime). It doesn't matter which
3386 * order we take the locks because no other cpu could
3387 * be trying to lock both of these tasks.
3388 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003389 raw_spin_lock(&ctx->lock);
3390 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391 if (context_equiv(ctx, next_ctx)) {
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003392 struct pmu *pmu = ctx->pmu;
3393
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003394 WRITE_ONCE(ctx->task, next);
3395 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003396
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003397 /*
3398 * PMU specific parts of task perf context can require
3399 * additional synchronization. As an example of such
3400 * synchronization see implementation details of Intel
3401 * LBR call stack data profiling;
3402 */
3403 if (pmu->swap_task_ctx)
3404 pmu->swap_task_ctx(ctx, next_ctx);
3405 else
3406 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003407
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003408 /*
3409 * RCU_INIT_POINTER here is safe because we've not
3410 * modified the ctx and the above modification of
3411 * ctx->task and ctx->task_ctx_data are immaterial
3412 * since those values are always verified under
3413 * ctx->lock which we're now holding.
3414 */
3415 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
3416 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
3417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418 do_switch = 0;
3419
3420 perf_event_sync_stat(ctx, next_ctx);
3421 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003422 raw_spin_unlock(&next_ctx->lock);
3423 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003425unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003426 rcu_read_unlock();
3427
3428 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003429 raw_spin_lock(&ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003430 task_ctx_sched_out(cpuctx, ctx, EVENT_ALL);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003431 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003432 }
3433}
3434
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003435static DEFINE_PER_CPU(struct list_head, sched_cb_list);
3436
Yan, Zhengba532502014-11-04 21:55:58 -05003437void perf_sched_cb_dec(struct pmu *pmu)
3438{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003439 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3440
Yan, Zhengba532502014-11-04 21:55:58 -05003441 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003442
3443 if (!--cpuctx->sched_cb_usage)
3444 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05003445}
3446
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003447
Yan, Zhengba532502014-11-04 21:55:58 -05003448void perf_sched_cb_inc(struct pmu *pmu)
3449{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003450 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3451
3452 if (!cpuctx->sched_cb_usage++)
3453 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
3454
Yan, Zhengba532502014-11-04 21:55:58 -05003455 this_cpu_inc(perf_sched_cb_usages);
3456}
3457
3458/*
3459 * This function provides the context switch callback to the lower code
3460 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02003461 *
3462 * This callback is relevant even to per-cpu events; for example multi event
3463 * PEBS requires this to provide PID/TID information. This requires we flush
3464 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05003465 */
3466static void perf_pmu_sched_task(struct task_struct *prev,
3467 struct task_struct *next,
3468 bool sched_in)
3469{
3470 struct perf_cpu_context *cpuctx;
3471 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05003472
3473 if (prev == next)
3474 return;
3475
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003476 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
David Carrillo-Cisneros1fd7e412017-01-18 11:24:54 -08003477 pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05003478
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003479 if (WARN_ON_ONCE(!pmu->sched_task))
3480 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05003481
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003482 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
3483 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05003484
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003485 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05003486
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003487 perf_pmu_enable(pmu);
3488 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05003489 }
Yan, Zhengba532502014-11-04 21:55:58 -05003490}
3491
Adrian Hunter45ac1402015-07-21 12:44:02 +03003492static void perf_event_switch(struct task_struct *task,
3493 struct task_struct *next_prev, bool sched_in);
3494
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003495#define for_each_task_context_nr(ctxn) \
3496 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
3497
3498/*
3499 * Called from scheduler to remove the events of the current task,
3500 * with interrupts disabled.
3501 *
3502 * We stop each event and update the event value in event->count.
3503 *
3504 * This does not protect us against NMI, but disable()
3505 * sets the disabled bit in the control field of event _before_
3506 * accessing the event control register. If a NMI hits, then it will
3507 * not restart the event.
3508 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003509void __perf_event_task_sched_out(struct task_struct *task,
3510 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003511{
3512 int ctxn;
3513
Yan, Zhengba532502014-11-04 21:55:58 -05003514 if (__this_cpu_read(perf_sched_cb_usages))
3515 perf_pmu_sched_task(task, next, false);
3516
Adrian Hunter45ac1402015-07-21 12:44:02 +03003517 if (atomic_read(&nr_switch_events))
3518 perf_event_switch(task, next, false);
3519
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003520 for_each_task_context_nr(ctxn)
3521 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003522
3523 /*
3524 * if cgroup events exist on this CPU, then we need
3525 * to check if we have to switch out PMU state.
3526 * cgroup event are system-wide mode only
3527 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003528 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003529 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003530}
3531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003532/*
3533 * Called with IRQs disabled
3534 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003535static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3536 enum event_type_t event_type)
3537{
3538 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003539}
3540
Ian Rogers6eef8a712020-02-13 23:51:30 -08003541static bool perf_less_group_idx(const void *l, const void *r)
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003542{
Ian Rogers24fb6b82020-03-21 09:43:31 -07003543 const struct perf_event *le = *(const struct perf_event **)l;
3544 const struct perf_event *re = *(const struct perf_event **)r;
Ian Rogers6eef8a712020-02-13 23:51:30 -08003545
3546 return le->group_index < re->group_index;
3547}
3548
3549static void swap_ptr(void *l, void *r)
3550{
3551 void **lp = l, **rp = r;
3552
3553 swap(*lp, *rp);
3554}
3555
3556static const struct min_heap_callbacks perf_min_heap = {
3557 .elem_size = sizeof(struct perf_event *),
3558 .less = perf_less_group_idx,
3559 .swp = swap_ptr,
3560};
3561
3562static void __heap_add(struct min_heap *heap, struct perf_event *event)
3563{
3564 struct perf_event **itrs = heap->data;
3565
3566 if (event) {
3567 itrs[heap->nr] = event;
3568 heap->nr++;
3569 }
3570}
3571
Ian Rogers836196be2020-02-13 23:51:31 -08003572static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx,
3573 struct perf_event_groups *groups, int cpu,
Ian Rogers6eef8a712020-02-13 23:51:30 -08003574 int (*func)(struct perf_event *, void *),
3575 void *data)
3576{
Ian Rogers95ed6c72020-02-13 23:51:33 -08003577#ifdef CONFIG_CGROUP_PERF
3578 struct cgroup_subsys_state *css = NULL;
3579#endif
Ian Rogers6eef8a712020-02-13 23:51:30 -08003580 /* Space for per CPU and/or any CPU event iterators. */
3581 struct perf_event *itrs[2];
Ian Rogers836196be2020-02-13 23:51:31 -08003582 struct min_heap event_heap;
3583 struct perf_event **evt;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003584 int ret;
3585
Ian Rogers836196be2020-02-13 23:51:31 -08003586 if (cpuctx) {
3587 event_heap = (struct min_heap){
3588 .data = cpuctx->heap,
3589 .nr = 0,
3590 .size = cpuctx->heap_size,
3591 };
Ian Rogersc2283c92020-02-13 23:51:32 -08003592
3593 lockdep_assert_held(&cpuctx->ctx.lock);
Ian Rogers95ed6c72020-02-13 23:51:33 -08003594
3595#ifdef CONFIG_CGROUP_PERF
3596 if (cpuctx->cgrp)
3597 css = &cpuctx->cgrp->css;
3598#endif
Ian Rogers836196be2020-02-13 23:51:31 -08003599 } else {
3600 event_heap = (struct min_heap){
3601 .data = itrs,
3602 .nr = 0,
3603 .size = ARRAY_SIZE(itrs),
3604 };
3605 /* Events not within a CPU context may be on any CPU. */
Ian Rogers95ed6c72020-02-13 23:51:33 -08003606 __heap_add(&event_heap, perf_event_groups_first(groups, -1, NULL));
Ian Rogers836196be2020-02-13 23:51:31 -08003607 }
3608 evt = event_heap.data;
3609
Ian Rogers95ed6c72020-02-13 23:51:33 -08003610 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, NULL));
3611
3612#ifdef CONFIG_CGROUP_PERF
3613 for (; css; css = css->parent)
3614 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, css->cgroup));
3615#endif
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003616
Ian Rogers6eef8a712020-02-13 23:51:30 -08003617 min_heapify_all(&event_heap, &perf_min_heap);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003618
Ian Rogers6eef8a712020-02-13 23:51:30 -08003619 while (event_heap.nr) {
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003620 ret = func(*evt, data);
3621 if (ret)
3622 return ret;
3623
3624 *evt = perf_event_groups_next(*evt);
Ian Rogers6eef8a712020-02-13 23:51:30 -08003625 if (*evt)
3626 min_heapify(&event_heap, 0, &perf_min_heap);
3627 else
3628 min_heap_pop(&event_heap, &perf_min_heap);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003629 }
3630
3631 return 0;
3632}
3633
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003634static int merge_sched_in(struct perf_event *event, void *data)
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003635{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003636 struct perf_event_context *ctx = event->ctx;
3637 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
3638 int *can_add_hw = data;
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003639
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003640 if (event->state <= PERF_EVENT_STATE_OFF)
3641 return 0;
3642
3643 if (!event_filter_match(event))
3644 return 0;
3645
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003646 if (group_can_go_on(event, cpuctx, *can_add_hw)) {
3647 if (!group_sched_in(event, cpuctx, ctx))
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003648 list_add_tail(&event->active_list, get_event_list(event));
Peter Zijlstra66681282017-11-13 14:28:38 +01003649 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003650
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003651 if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra33238c52020-03-18 20:33:37 +01003652 if (event->attr.pinned) {
3653 perf_cgroup_event_disable(event, ctx);
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003654 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
Peter Zijlstra33238c52020-03-18 20:33:37 +01003655 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003656
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003657 *can_add_hw = 0;
3658 ctx->rotate_necessary = 1;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003659 }
3660
3661 return 0;
3662}
3663
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003664static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003665ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003666 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003668 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003669
Ian Rogers836196be2020-02-13 23:51:31 -08003670 if (ctx != &cpuctx->ctx)
3671 cpuctx = NULL;
3672
3673 visit_groups_merge(cpuctx, &ctx->pinned_groups,
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003674 smp_processor_id(),
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003675 merge_sched_in, &can_add_hw);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003676}
3677
3678static void
3679ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003680 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003681{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003682 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003683
Ian Rogers836196be2020-02-13 23:51:31 -08003684 if (ctx != &cpuctx->ctx)
3685 cpuctx = NULL;
3686
3687 visit_groups_merge(cpuctx, &ctx->flexible_groups,
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003688 smp_processor_id(),
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003689 merge_sched_in, &can_add_hw);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003690}
3691
3692static void
3693ctx_sched_in(struct perf_event_context *ctx,
3694 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003695 enum event_type_t event_type,
3696 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003697{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003698 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003699 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003700
Peter Zijlstrac994d612016-01-08 09:20:23 +01003701 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003702
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003703 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003704 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003705
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003706 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003707 if (ctx->task) {
3708 if (!is_active)
3709 cpuctx->task_ctx = ctx;
3710 else
3711 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3712 }
3713
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003714 is_active ^= ctx->is_active; /* changed bits */
3715
3716 if (is_active & EVENT_TIME) {
3717 /* start ctx time */
3718 now = perf_clock();
3719 ctx->timestamp = now;
3720 perf_cgroup_set_timestamp(task, ctx);
3721 }
3722
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003723 /*
3724 * First go through the list and put on any pinned groups
3725 * in order to give them the best chance of going on.
3726 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003727 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003728 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003729
3730 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003731 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003732 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733}
3734
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003735static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003736 enum event_type_t event_type,
3737 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003738{
3739 struct perf_event_context *ctx = &cpuctx->ctx;
3740
Stephane Eraniane5d13672011-02-14 11:20:01 +02003741 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003742}
3743
Stephane Eraniane5d13672011-02-14 11:20:01 +02003744static void perf_event_context_sched_in(struct perf_event_context *ctx,
3745 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003747 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003748
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003749 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003750 if (cpuctx->task_ctx == ctx)
3751 return;
3752
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003753 perf_ctx_lock(cpuctx, ctx);
leilei.linfdccc3f2017-08-09 08:29:21 +08003754 /*
3755 * We must check ctx->nr_events while holding ctx->lock, such
3756 * that we serialize against perf_install_in_context().
3757 */
3758 if (!ctx->nr_events)
3759 goto unlock;
3760
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003761 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003762 /*
3763 * We want to keep the following priority order:
3764 * cpu pinned (that don't need to move), task pinned,
3765 * cpu flexible, task flexible.
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003766 *
3767 * However, if task's ctx is not carrying any pinned
3768 * events, no need to flip the cpuctx's events around.
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003769 */
Alexey Budankov8e1a2032017-09-08 11:47:03 +03003770 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree))
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003771 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003772 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003773 perf_pmu_enable(ctx->pmu);
leilei.linfdccc3f2017-08-09 08:29:21 +08003774
3775unlock:
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003776 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777}
3778
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003779/*
3780 * Called from scheduler to add the events of the current task
3781 * with interrupts disabled.
3782 *
3783 * We restore the event value and then enable it.
3784 *
3785 * This does not protect us against NMI, but enable()
3786 * sets the enabled bit in the control field of event _before_
3787 * accessing the event control register. If a NMI hits, then it will
3788 * keep the event running.
3789 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003790void __perf_event_task_sched_in(struct task_struct *prev,
3791 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003792{
3793 struct perf_event_context *ctx;
3794 int ctxn;
3795
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003796 /*
3797 * If cgroup events exist on this CPU, then we need to check if we have
3798 * to switch in PMU state; cgroup event are system-wide mode only.
3799 *
3800 * Since cgroup events are CPU events, we must schedule these in before
3801 * we schedule in the task events.
3802 */
3803 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3804 perf_cgroup_sched_in(prev, task);
3805
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003806 for_each_task_context_nr(ctxn) {
3807 ctx = task->perf_event_ctxp[ctxn];
3808 if (likely(!ctx))
3809 continue;
3810
Stephane Eraniane5d13672011-02-14 11:20:01 +02003811 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003812 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003813
Adrian Hunter45ac1402015-07-21 12:44:02 +03003814 if (atomic_read(&nr_switch_events))
3815 perf_event_switch(task, prev, true);
3816
Yan, Zhengba532502014-11-04 21:55:58 -05003817 if (__this_cpu_read(perf_sched_cb_usages))
3818 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819}
3820
Peter Zijlstraabd50712010-01-26 18:50:16 +01003821static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3822{
3823 u64 frequency = event->attr.sample_freq;
3824 u64 sec = NSEC_PER_SEC;
3825 u64 divisor, dividend;
3826
3827 int count_fls, nsec_fls, frequency_fls, sec_fls;
3828
3829 count_fls = fls64(count);
3830 nsec_fls = fls64(nsec);
3831 frequency_fls = fls64(frequency);
3832 sec_fls = 30;
3833
3834 /*
3835 * We got @count in @nsec, with a target of sample_freq HZ
3836 * the target period becomes:
3837 *
3838 * @count * 10^9
3839 * period = -------------------
3840 * @nsec * sample_freq
3841 *
3842 */
3843
3844 /*
3845 * Reduce accuracy by one bit such that @a and @b converge
3846 * to a similar magnitude.
3847 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003848#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003849do { \
3850 if (a##_fls > b##_fls) { \
3851 a >>= 1; \
3852 a##_fls--; \
3853 } else { \
3854 b >>= 1; \
3855 b##_fls--; \
3856 } \
3857} while (0)
3858
3859 /*
3860 * Reduce accuracy until either term fits in a u64, then proceed with
3861 * the other, so that finally we can do a u64/u64 division.
3862 */
3863 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3864 REDUCE_FLS(nsec, frequency);
3865 REDUCE_FLS(sec, count);
3866 }
3867
3868 if (count_fls + sec_fls > 64) {
3869 divisor = nsec * frequency;
3870
3871 while (count_fls + sec_fls > 64) {
3872 REDUCE_FLS(count, sec);
3873 divisor >>= 1;
3874 }
3875
3876 dividend = count * sec;
3877 } else {
3878 dividend = count * sec;
3879
3880 while (nsec_fls + frequency_fls > 64) {
3881 REDUCE_FLS(nsec, frequency);
3882 dividend >>= 1;
3883 }
3884
3885 divisor = nsec * frequency;
3886 }
3887
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003888 if (!divisor)
3889 return dividend;
3890
Peter Zijlstraabd50712010-01-26 18:50:16 +01003891 return div64_u64(dividend, divisor);
3892}
3893
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003894static DEFINE_PER_CPU(int, perf_throttled_count);
3895static DEFINE_PER_CPU(u64, perf_throttled_seq);
3896
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003897static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003898{
3899 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003900 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003901 s64 delta;
3902
Peter Zijlstraabd50712010-01-26 18:50:16 +01003903 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003904
3905 delta = (s64)(period - hwc->sample_period);
3906 delta = (delta + 7) / 8; /* low pass filter */
3907
3908 sample_period = hwc->sample_period + delta;
3909
3910 if (!sample_period)
3911 sample_period = 1;
3912
3913 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003914
Peter Zijlstrae7850592010-05-21 14:43:08 +02003915 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003916 if (disable)
3917 event->pmu->stop(event, PERF_EF_UPDATE);
3918
Peter Zijlstrae7850592010-05-21 14:43:08 +02003919 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003920
3921 if (disable)
3922 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003923 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924}
3925
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003926/*
3927 * combine freq adjustment with unthrottling to avoid two passes over the
3928 * events. At the same time, make sure, having freq events does not change
3929 * the rate of unthrottling as that would introduce bias.
3930 */
3931static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3932 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003933{
3934 struct perf_event *event;
3935 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003936 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003937 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003938
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003939 /*
3940 * only need to iterate over all events iff:
3941 * - context have events in frequency mode (needs freq adjust)
3942 * - there are events to unthrottle on this cpu
3943 */
3944 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003945 return;
3946
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003947 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003948 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003949
Paul Mackerras03541f82009-10-14 16:58:03 +11003950 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003951 if (event->state != PERF_EVENT_STATE_ACTIVE)
3952 continue;
3953
Stephane Eranian5632ab12011-01-03 18:20:01 +02003954 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003955 continue;
3956
Alexander Shishkin44377272013-12-16 14:17:36 +02003957 perf_pmu_disable(event->pmu);
3958
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959 hwc = &event->hw;
3960
Jiri Olsaae23bff2013-08-24 16:45:54 +02003961 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003962 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003964 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003965 }
3966
3967 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003968 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003969
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003970 /*
3971 * stop the event and update event->count
3972 */
3973 event->pmu->stop(event, PERF_EF_UPDATE);
3974
Peter Zijlstrae7850592010-05-21 14:43:08 +02003975 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003976 delta = now - hwc->freq_count_stamp;
3977 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003978
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003979 /*
3980 * restart the event
3981 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003982 * we have stopped the event so tell that
3983 * to perf_adjust_period() to avoid stopping it
3984 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003985 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003986 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003987 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003988
3989 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003990 next:
3991 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003992 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003993
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003994 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003995 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003996}
3997
3998/*
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003999 * Move @event to the tail of the @ctx's elegible events.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004000 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004001static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01004003 /*
4004 * Rotate the first entry last of non-pinned groups. Rotation might be
4005 * disabled by the inheritance code.
4006 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004007 if (ctx->rotate_disable)
4008 return;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03004009
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004010 perf_event_groups_delete(&ctx->flexible_groups, event);
4011 perf_event_groups_insert(&ctx->flexible_groups, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012}
4013
Song Liu7fa343b72019-10-08 09:59:49 -07004014/* pick an event from the flexible_groups to rotate */
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004015static inline struct perf_event *
Song Liu7fa343b72019-10-08 09:59:49 -07004016ctx_event_to_rotate(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004017{
Song Liu7fa343b72019-10-08 09:59:49 -07004018 struct perf_event *event;
4019
4020 /* pick the first active flexible event */
4021 event = list_first_entry_or_null(&ctx->flexible_active,
4022 struct perf_event, active_list);
4023
4024 /* if no active flexible event, pick the first event */
4025 if (!event) {
4026 event = rb_entry_safe(rb_first(&ctx->flexible_groups.tree),
4027 typeof(*event), group_node);
4028 }
4029
Peter Zijlstra90c91df2020-03-05 13:38:51 +01004030 /*
4031 * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in()
4032 * finds there are unschedulable events, it will set it again.
4033 */
4034 ctx->rotate_necessary = 0;
4035
Song Liu7fa343b72019-10-08 09:59:49 -07004036 return event;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004037}
4038
4039static bool perf_rotate_context(struct perf_cpu_context *cpuctx)
4040{
4041 struct perf_event *cpu_event = NULL, *task_event = NULL;
Ian Rogersfd7d5512019-06-01 01:27:22 -07004042 struct perf_event_context *task_ctx = NULL;
4043 int cpu_rotate, task_rotate;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004044
4045 /*
4046 * Since we run this from IRQ context, nobody can install new
4047 * events, thus the event count values are stable.
4048 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049
Ian Rogersfd7d5512019-06-01 01:27:22 -07004050 cpu_rotate = cpuctx->ctx.rotate_necessary;
4051 task_ctx = cpuctx->task_ctx;
4052 task_rotate = task_ctx ? task_ctx->rotate_necessary : 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004053
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004054 if (!(cpu_rotate || task_rotate))
4055 return false;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01004056
Peter Zijlstrafacc4302011-04-09 21:17:42 +02004057 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02004058 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004059
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004060 if (task_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07004061 task_event = ctx_event_to_rotate(task_ctx);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004062 if (cpu_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07004063 cpu_event = ctx_event_to_rotate(&cpuctx->ctx);
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004064
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004065 /*
4066 * As per the order given at ctx_resched() first 'pop' task flexible
4067 * and then, if needed CPU flexible.
4068 */
Ian Rogersfd7d5512019-06-01 01:27:22 -07004069 if (task_event || (task_ctx && cpu_event))
4070 ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004071 if (cpu_event)
4072 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01004073
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004074 if (task_event)
Ian Rogersfd7d5512019-06-01 01:27:22 -07004075 rotate_ctx(task_ctx, task_event);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004076 if (cpu_event)
4077 rotate_ctx(&cpuctx->ctx, cpu_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004078
Ian Rogersfd7d5512019-06-01 01:27:22 -07004079 perf_event_sched_in(cpuctx, task_ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01004080
4081 perf_pmu_enable(cpuctx->ctx.pmu);
4082 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02004083
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004084 return true;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004085}
4086
4087void perf_event_task_tick(void)
4088{
Mark Rutland2fde4f92015-01-07 15:01:54 +00004089 struct list_head *head = this_cpu_ptr(&active_ctx_list);
4090 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004091 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004092
Frederic Weisbecker16444642017-11-06 16:01:24 +01004093 lockdep_assert_irqs_disabled();
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004094
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004095 __this_cpu_inc(perf_throttled_seq);
4096 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004097 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004098
Mark Rutland2fde4f92015-01-07 15:01:54 +00004099 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004100 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004101}
4102
Frederic Weisbecker889ff012010-01-09 20:04:47 +01004103static int event_enable_on_exec(struct perf_event *event,
4104 struct perf_event_context *ctx)
4105{
4106 if (!event->attr.enable_on_exec)
4107 return 0;
4108
4109 event->attr.enable_on_exec = 0;
4110 if (event->state >= PERF_EVENT_STATE_INACTIVE)
4111 return 0;
4112
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004113 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01004114
4115 return 1;
4116}
4117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118/*
4119 * Enable all of a task's events that have been marked enable-on-exec.
4120 * This expects task == current.
4121 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01004122static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123{
Peter Zijlstrac1274492015-12-10 20:57:40 +01004124 struct perf_event_context *ctx, *clone_ctx = NULL;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004125 enum event_type_t event_type = 0;
Peter Zijlstra3e349502016-01-08 10:01:18 +01004126 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127 struct perf_event *event;
4128 unsigned long flags;
4129 int enabled = 0;
4130
4131 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01004132 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133 if (!ctx || !ctx->nr_events)
4134 goto out;
4135
Peter Zijlstra3e349502016-01-08 10:01:18 +01004136 cpuctx = __get_cpu_context(ctx);
4137 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01004138 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004139 list_for_each_entry(event, &ctx->event_list, event_entry) {
Peter Zijlstra3e349502016-01-08 10:01:18 +01004140 enabled |= event_enable_on_exec(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004141 event_type |= get_event_type(event);
4142 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004143
4144 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01004145 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004146 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01004147 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004148 clone_ctx = unclone_ctx(ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004149 ctx_resched(cpuctx, ctx, event_type);
Peter Zijlstra7bbba0e2017-02-15 16:12:20 +01004150 } else {
4151 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstra3e349502016-01-08 10:01:18 +01004152 }
4153 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004155out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004156 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004157
4158 if (clone_ctx)
4159 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160}
4161
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004162struct perf_read_data {
4163 struct perf_event *event;
4164 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004165 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004166};
4167
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004168static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004169{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004170 u16 local_pkg, event_pkg;
4171
4172 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004173 int local_cpu = smp_processor_id();
4174
4175 event_pkg = topology_physical_package_id(event_cpu);
4176 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004177
4178 if (event_pkg == local_pkg)
4179 return local_cpu;
4180 }
4181
4182 return event_cpu;
4183}
4184
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185/*
4186 * Cross CPU call to read the hardware event
4187 */
4188static void __perf_event_read(void *info)
4189{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004190 struct perf_read_data *data = info;
4191 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004193 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004194 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195
4196 /*
4197 * If this is a task context, we need to check whether it is
4198 * the current task context of this cpu. If not it has been
4199 * scheduled out before the smp call arrived. In that case
4200 * event->count would have been updated to a recent sample
4201 * when the event was scheduled out.
4202 */
4203 if (ctx->task && cpuctx->task_ctx != ctx)
4204 return;
4205
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004206 raw_spin_lock(&ctx->lock);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004207 if (ctx->is_active & EVENT_TIME) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01004208 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004209 update_cgrp_time_from_event(event);
4210 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004211
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004212 perf_event_update_time(event);
4213 if (data->group)
4214 perf_event_update_sibling_time(event);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004215
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004216 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004217 goto unlock;
4218
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004219 if (!data->group) {
4220 pmu->read(event);
4221 data->ret = 0;
4222 goto unlock;
4223 }
4224
4225 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
4226
4227 pmu->read(event);
4228
Peter Zijlstraedb39592018-03-15 17:36:56 +01004229 for_each_sibling_event(sub, event) {
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004230 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
4231 /*
4232 * Use sibling's PMU rather than @event's since
4233 * sibling could be on different (eg: software) PMU.
4234 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004235 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004236 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004237 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004238
4239 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004240
4241unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004242 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004243}
4244
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004245static inline u64 perf_event_count(struct perf_event *event)
4246{
Vikas Shivappac39a0e22017-07-25 14:14:20 -07004247 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004248}
4249
Kaixu Xiaffe86902015-08-06 07:02:32 +00004250/*
4251 * NMI-safe method to read a local event, that is an event that
4252 * is:
4253 * - either for the current task, or for this CPU
4254 * - does not have inherit set, for inherited task events
4255 * will not be local and we cannot read them atomically
4256 * - must not have a pmu::count method
4257 */
Yonghong Song7d9285e2017-10-05 09:19:19 -07004258int perf_event_read_local(struct perf_event *event, u64 *value,
4259 u64 *enabled, u64 *running)
Kaixu Xiaffe86902015-08-06 07:02:32 +00004260{
4261 unsigned long flags;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004262 int ret = 0;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004263
4264 /*
4265 * Disabling interrupts avoids all counter scheduling (context
4266 * switches, timer based rotation and IPIs).
4267 */
4268 local_irq_save(flags);
4269
Kaixu Xiaffe86902015-08-06 07:02:32 +00004270 /*
4271 * It must not be an event with inherit set, we cannot read
4272 * all child counters from atomic context.
4273 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004274 if (event->attr.inherit) {
4275 ret = -EOPNOTSUPP;
4276 goto out;
4277 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004278
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004279 /* If this is a per-task event, it must be for current */
4280 if ((event->attach_state & PERF_ATTACH_TASK) &&
4281 event->hw.target != current) {
4282 ret = -EINVAL;
4283 goto out;
4284 }
4285
4286 /* If this is a per-CPU event, it must be for this CPU */
4287 if (!(event->attach_state & PERF_ATTACH_TASK) &&
4288 event->cpu != smp_processor_id()) {
4289 ret = -EINVAL;
4290 goto out;
4291 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004292
Reinette Chatrebefb1b32018-09-19 10:29:06 -07004293 /* If this is a pinned event it must be running on this CPU */
4294 if (event->attr.pinned && event->oncpu != smp_processor_id()) {
4295 ret = -EBUSY;
4296 goto out;
4297 }
4298
Kaixu Xiaffe86902015-08-06 07:02:32 +00004299 /*
4300 * If the event is currently on this CPU, its either a per-task event,
4301 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
4302 * oncpu == -1).
4303 */
4304 if (event->oncpu == smp_processor_id())
4305 event->pmu->read(event);
4306
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004307 *value = local64_read(&event->count);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004308 if (enabled || running) {
4309 u64 now = event->shadow_ctx_time + perf_clock();
4310 u64 __enabled, __running;
4311
4312 __perf_update_times(event, now, &__enabled, &__running);
4313 if (enabled)
4314 *enabled = __enabled;
4315 if (running)
4316 *running = __running;
4317 }
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004318out:
Kaixu Xiaffe86902015-08-06 07:02:32 +00004319 local_irq_restore(flags);
4320
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004321 return ret;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004322}
4323
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004324static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004325{
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004326 enum perf_event_state state = READ_ONCE(event->state);
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004327 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004328
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004329 /*
4330 * If event is enabled and currently active on a CPU, update the
4331 * value in the event structure:
4332 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004333again:
4334 if (state == PERF_EVENT_STATE_ACTIVE) {
4335 struct perf_read_data data;
4336
4337 /*
4338 * Orders the ->state and ->oncpu loads such that if we see
4339 * ACTIVE we must also see the right ->oncpu.
4340 *
4341 * Matches the smp_wmb() from event_sched_in().
4342 */
4343 smp_rmb();
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004344
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004345 event_cpu = READ_ONCE(event->oncpu);
4346 if ((unsigned)event_cpu >= nr_cpu_ids)
4347 return 0;
4348
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004349 data = (struct perf_read_data){
4350 .event = event,
4351 .group = group,
4352 .ret = 0,
4353 };
4354
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004355 preempt_disable();
4356 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004357
Peter Zijlstra58763142016-08-30 10:15:03 +02004358 /*
4359 * Purposely ignore the smp_call_function_single() return
4360 * value.
4361 *
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004362 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02004363 * scheduled out and that will have updated the event count.
4364 *
4365 * Therefore, either way, we'll have an up-to-date event count
4366 * after this.
4367 */
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004368 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
4369 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02004370 ret = data.ret;
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004371
4372 } else if (state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01004373 struct perf_event_context *ctx = event->ctx;
4374 unsigned long flags;
4375
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004376 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004377 state = event->state;
4378 if (state != PERF_EVENT_STATE_INACTIVE) {
4379 raw_spin_unlock_irqrestore(&ctx->lock, flags);
4380 goto again;
4381 }
4382
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004383 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004384 * May read while context is not active (e.g., thread is
4385 * blocked), in that case we cannot update context time
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004386 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004387 if (ctx->is_active & EVENT_TIME) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004388 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004389 update_cgrp_time_from_event(event);
4390 }
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004391
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004392 perf_event_update_time(event);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004393 if (group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004394 perf_event_update_sibling_time(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004395 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004396 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004397
4398 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004399}
4400
4401/*
4402 * Initialize the perf_event context in a task_struct:
4403 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02004404static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004405{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004406 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004407 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00004408 INIT_LIST_HEAD(&ctx->active_ctx_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03004409 perf_event_groups_init(&ctx->pinned_groups);
4410 perf_event_groups_init(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411 INIT_LIST_HEAD(&ctx->event_list);
Peter Zijlstra66681282017-11-13 14:28:38 +01004412 INIT_LIST_HEAD(&ctx->pinned_active);
4413 INIT_LIST_HEAD(&ctx->flexible_active);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02004414 refcount_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415}
4416
Peter Zijlstraeb184472010-09-07 15:55:13 +02004417static struct perf_event_context *
4418alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004419{
4420 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004421
4422 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
4423 if (!ctx)
4424 return NULL;
4425
4426 __perf_event_init_context(ctx);
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -07004427 if (task)
4428 ctx->task = get_task_struct(task);
Peter Zijlstraeb184472010-09-07 15:55:13 +02004429 ctx->pmu = pmu;
4430
4431 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432}
4433
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004434static struct task_struct *
4435find_lively_task_by_vpid(pid_t vpid)
4436{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004438
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004439 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004440 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004441 task = current;
4442 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004443 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004444 if (task)
4445 get_task_struct(task);
4446 rcu_read_unlock();
4447
4448 if (!task)
4449 return ERR_PTR(-ESRCH);
4450
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004451 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004452}
4453
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004454/*
4455 * Returns a matching context with refcount and pincount.
4456 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004457static struct perf_event_context *
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004458find_get_context(struct pmu *pmu, struct task_struct *task,
4459 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004460{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004461 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004462 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004463 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004465 int ctxn, err;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004466 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004467
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01004468 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004469 /* Must be root to operate on a CPU event: */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004470 err = perf_allow_cpu(&event->attr);
4471 if (err)
4472 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004473
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004474 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475 ctx = &cpuctx->ctx;
4476 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004477 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004478
4479 return ctx;
4480 }
4481
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004482 err = -EINVAL;
4483 ctxn = pmu->task_ctx_nr;
4484 if (ctxn < 0)
4485 goto errout;
4486
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004487 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
Kan Liangff9ff922020-07-03 05:49:21 -07004488 task_ctx_data = alloc_task_ctx_data(pmu);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004489 if (!task_ctx_data) {
4490 err = -ENOMEM;
4491 goto errout;
4492 }
4493 }
4494
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004495retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004496 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004498 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004499 ++ctx->pin_count;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004500
4501 if (task_ctx_data && !ctx->task_ctx_data) {
4502 ctx->task_ctx_data = task_ctx_data;
4503 task_ctx_data = NULL;
4504 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004505 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004506
4507 if (clone_ctx)
4508 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004509 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02004510 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511 err = -ENOMEM;
4512 if (!ctx)
4513 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004514
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004515 if (task_ctx_data) {
4516 ctx->task_ctx_data = task_ctx_data;
4517 task_ctx_data = NULL;
4518 }
4519
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004520 err = 0;
4521 mutex_lock(&task->perf_event_mutex);
4522 /*
4523 * If it has already passed perf_event_exit_task().
4524 * we must see PF_EXITING, it takes this mutex too.
4525 */
4526 if (task->flags & PF_EXITING)
4527 err = -ESRCH;
4528 else if (task->perf_event_ctxp[ctxn])
4529 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004530 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004531 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004532 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004533 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004534 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004535 mutex_unlock(&task->perf_event_mutex);
4536
4537 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004538 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004539
4540 if (err == -EAGAIN)
4541 goto retry;
4542 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004543 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004544 }
4545
Kan Liangff9ff922020-07-03 05:49:21 -07004546 free_task_ctx_data(pmu, task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004547 return ctx;
4548
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004549errout:
Kan Liangff9ff922020-07-03 05:49:21 -07004550 free_task_ctx_data(pmu, task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551 return ERR_PTR(err);
4552}
4553
Li Zefan6fb29152009-10-15 11:21:42 +08004554static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004555static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08004556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004557static void free_event_rcu(struct rcu_head *head)
4558{
4559 struct perf_event *event;
4560
4561 event = container_of(head, struct perf_event, rcu_head);
4562 if (event->ns)
4563 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08004564 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004565 kfree(event);
4566}
4567
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004568static void ring_buffer_attach(struct perf_event *event,
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05004569 struct perf_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004570
Kan Liangf2fb6be2016-03-23 11:24:37 -07004571static void detach_sb_event(struct perf_event *event)
4572{
4573 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
4574
4575 raw_spin_lock(&pel->lock);
4576 list_del_rcu(&event->sb_list);
4577 raw_spin_unlock(&pel->lock);
4578}
4579
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004580static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07004581{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004582 struct perf_event_attr *attr = &event->attr;
4583
Kan Liangf2fb6be2016-03-23 11:24:37 -07004584 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004585 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004586
4587 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004588 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004589
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004590 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
4591 attr->comm || attr->comm_exec ||
Song Liu76193a92019-01-17 08:15:13 -08004592 attr->task || attr->ksymbol ||
Adrian Huntere17d43b2020-05-12 15:19:08 +03004593 attr->context_switch || attr->text_poke ||
Song Liu21038f22019-02-25 16:20:05 -08004594 attr->bpf_event)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004595 return true;
4596 return false;
4597}
4598
4599static void unaccount_pmu_sb_event(struct perf_event *event)
4600{
4601 if (is_sb_event(event))
4602 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004603}
4604
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004605static void unaccount_event_cpu(struct perf_event *event, int cpu)
4606{
4607 if (event->parent)
4608 return;
4609
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004610 if (is_cgroup_event(event))
4611 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
4612}
4613
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004614#ifdef CONFIG_NO_HZ_FULL
4615static DEFINE_SPINLOCK(nr_freq_lock);
4616#endif
4617
4618static void unaccount_freq_event_nohz(void)
4619{
4620#ifdef CONFIG_NO_HZ_FULL
4621 spin_lock(&nr_freq_lock);
4622 if (atomic_dec_and_test(&nr_freq_events))
4623 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
4624 spin_unlock(&nr_freq_lock);
4625#endif
4626}
4627
4628static void unaccount_freq_event(void)
4629{
4630 if (tick_nohz_full_enabled())
4631 unaccount_freq_event_nohz();
4632 else
4633 atomic_dec(&nr_freq_events);
4634}
4635
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004636static void unaccount_event(struct perf_event *event)
4637{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004638 bool dec = false;
4639
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004640 if (event->parent)
4641 return;
4642
4643 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004644 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004645 if (event->attr.mmap || event->attr.mmap_data)
4646 atomic_dec(&nr_mmap_events);
4647 if (event->attr.comm)
4648 atomic_dec(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +05304649 if (event->attr.namespaces)
4650 atomic_dec(&nr_namespaces_events);
Namhyung Kim96aaab62020-03-25 21:45:28 +09004651 if (event->attr.cgroup)
4652 atomic_dec(&nr_cgroup_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004653 if (event->attr.task)
4654 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02004655 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004656 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03004657 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004658 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03004659 atomic_dec(&nr_switch_events);
4660 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004661 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004662 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004663 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004664 dec = true;
Song Liu76193a92019-01-17 08:15:13 -08004665 if (event->attr.ksymbol)
4666 atomic_dec(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -08004667 if (event->attr.bpf_event)
4668 atomic_dec(&nr_bpf_events);
Adrian Huntere17d43b2020-05-12 15:19:08 +03004669 if (event->attr.text_poke)
4670 atomic_dec(&nr_text_poke_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004671
Peter Zijlstra9107c892016-02-24 18:45:45 +01004672 if (dec) {
4673 if (!atomic_add_unless(&perf_sched_count, -1, 1))
4674 schedule_delayed_work(&perf_sched_work, HZ);
4675 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004676
4677 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004678
4679 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004680}
4681
Peter Zijlstra9107c892016-02-24 18:45:45 +01004682static void perf_sched_delayed(struct work_struct *work)
4683{
4684 mutex_lock(&perf_sched_mutex);
4685 if (atomic_dec_and_test(&perf_sched_count))
4686 static_branch_disable(&perf_sched_events);
4687 mutex_unlock(&perf_sched_mutex);
4688}
4689
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004690/*
4691 * The following implement mutual exclusion of events on "exclusive" pmus
4692 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
4693 * at a time, so we disallow creating events that might conflict, namely:
4694 *
4695 * 1) cpu-wide events in the presence of per-task events,
4696 * 2) per-task events in the presence of cpu-wide events,
4697 * 3) two matching events on the same context.
4698 *
4699 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004700 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004701 */
4702static int exclusive_event_init(struct perf_event *event)
4703{
4704 struct pmu *pmu = event->pmu;
4705
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004706 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004707 return 0;
4708
4709 /*
4710 * Prevent co-existence of per-task and cpu-wide events on the
4711 * same exclusive pmu.
4712 *
4713 * Negative pmu::exclusive_cnt means there are cpu-wide
4714 * events on this "exclusive" pmu, positive means there are
4715 * per-task events.
4716 *
4717 * Since this is called in perf_event_alloc() path, event::ctx
4718 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4719 * to mean "per-task event", because unlike other attach states it
4720 * never gets cleared.
4721 */
4722 if (event->attach_state & PERF_ATTACH_TASK) {
4723 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4724 return -EBUSY;
4725 } else {
4726 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4727 return -EBUSY;
4728 }
4729
4730 return 0;
4731}
4732
4733static void exclusive_event_destroy(struct perf_event *event)
4734{
4735 struct pmu *pmu = event->pmu;
4736
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004737 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004738 return;
4739
4740 /* see comment in exclusive_event_init() */
4741 if (event->attach_state & PERF_ATTACH_TASK)
4742 atomic_dec(&pmu->exclusive_cnt);
4743 else
4744 atomic_inc(&pmu->exclusive_cnt);
4745}
4746
4747static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4748{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004749 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004750 (e1->cpu == e2->cpu ||
4751 e1->cpu == -1 ||
4752 e2->cpu == -1))
4753 return true;
4754 return false;
4755}
4756
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004757static bool exclusive_event_installable(struct perf_event *event,
4758 struct perf_event_context *ctx)
4759{
4760 struct perf_event *iter_event;
4761 struct pmu *pmu = event->pmu;
4762
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004763 lockdep_assert_held(&ctx->mutex);
4764
4765 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004766 return true;
4767
4768 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4769 if (exclusive_event_match(iter_event, event))
4770 return false;
4771 }
4772
4773 return true;
4774}
4775
Alexander Shishkin375637b2016-04-27 18:44:46 +03004776static void perf_addr_filters_splice(struct perf_event *event,
4777 struct list_head *head);
4778
Peter Zijlstra683ede42014-05-05 12:11:24 +02004779static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004781 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004782
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004783 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004784
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004785 security_perf_event_free(event);
4786
Frederic Weisbecker76369132011-05-19 19:55:04 +02004787 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004788 /*
4789 * Can happen when we close an event with re-directed output.
4790 *
4791 * Since we have a 0 refcount, perf_mmap_close() will skip
4792 * over us; possibly making our ring_buffer_put() the last.
4793 */
4794 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004795 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004796 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004797 }
4798
Stephane Eraniane5d13672011-02-14 11:20:01 +02004799 if (is_cgroup_event(event))
4800 perf_detach_cgroup(event);
4801
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004802 if (!event->parent) {
4803 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4804 put_callchain_buffers();
4805 }
4806
4807 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004808 perf_addr_filters_splice(event, NULL);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02004809 kfree(event->addr_filter_ranges);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004810
4811 if (event->destroy)
4812 event->destroy(event);
4813
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004814 /*
4815 * Must be after ->destroy(), due to uprobe_perf_close() using
4816 * hw.target.
4817 */
Prashant Bhole621b6d22018-04-09 19:03:46 +09004818 if (event->hw.target)
4819 put_task_struct(event->hw.target);
4820
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004821 /*
4822 * perf_event_free_task() relies on put_ctx() being 'last', in particular
4823 * all task references must be cleaned up.
4824 */
4825 if (event->ctx)
4826 put_ctx(event->ctx);
4827
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004828 exclusive_event_destroy(event);
4829 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004830
4831 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004832}
4833
Peter Zijlstra683ede42014-05-05 12:11:24 +02004834/*
4835 * Used to free events which have a known refcount of 1, such as in error paths
4836 * where the event isn't exposed yet and inherited events.
4837 */
4838static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004839{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004840 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4841 "unexpected event refcount: %ld; ptr=%p\n",
4842 atomic_long_read(&event->refcount), event)) {
4843 /* leak to avoid use-after-free */
4844 return;
4845 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004846
Peter Zijlstra683ede42014-05-05 12:11:24 +02004847 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004848}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004849
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004850/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004851 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004852 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004853static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004854{
Peter Zijlstra88821352010-11-09 19:01:43 +01004855 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004856
Peter Zijlstra88821352010-11-09 19:01:43 +01004857 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004858 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004859 * Matches the smp_store_release() in perf_event_exit_task(). If we
4860 * observe !owner it means the list deletion is complete and we can
4861 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004862 * owner->perf_event_mutex.
4863 */
Will Deacon506458e2017-10-24 11:22:48 +01004864 owner = READ_ONCE(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004865 if (owner) {
4866 /*
4867 * Since delayed_put_task_struct() also drops the last
4868 * task reference we can safely take a new reference
4869 * while holding the rcu_read_lock().
4870 */
4871 get_task_struct(owner);
4872 }
4873 rcu_read_unlock();
4874
4875 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004876 /*
4877 * If we're here through perf_event_exit_task() we're already
4878 * holding ctx->mutex which would be an inversion wrt. the
4879 * normal lock order.
4880 *
4881 * However we can safely take this lock because its the child
4882 * ctx->mutex.
4883 */
4884 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4885
Peter Zijlstra88821352010-11-09 19:01:43 +01004886 /*
4887 * We have to re-check the event->owner field, if it is cleared
4888 * we raced with perf_event_exit_task(), acquiring the mutex
4889 * ensured they're done, and we can proceed with freeing the
4890 * event.
4891 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004892 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004893 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004894 smp_store_release(&event->owner, NULL);
4895 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004896 mutex_unlock(&owner->perf_event_mutex);
4897 put_task_struct(owner);
4898 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004899}
4900
Jiri Olsaf8697762014-08-01 14:33:01 +02004901static void put_event(struct perf_event *event)
4902{
Jiri Olsaf8697762014-08-01 14:33:01 +02004903 if (!atomic_long_dec_and_test(&event->refcount))
4904 return;
4905
Peter Zijlstra683ede42014-05-05 12:11:24 +02004906 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004907}
4908
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004909/*
4910 * Kill an event dead; while event:refcount will preserve the event
4911 * object, it will not preserve its functionality. Once the last 'user'
4912 * gives up the object, we'll destroy the thing.
4913 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004914int perf_event_release_kernel(struct perf_event *event)
4915{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004916 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004917 struct perf_event *child, *tmp;
Peter Zijlstra82d94852018-01-09 13:10:30 +01004918 LIST_HEAD(free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004919
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004920 /*
4921 * If we got here through err_file: fput(event_file); we will not have
4922 * attached to a context yet.
4923 */
4924 if (!ctx) {
4925 WARN_ON_ONCE(event->attach_state &
4926 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4927 goto no_ctx;
4928 }
4929
Peter Zijlstra88821352010-11-09 19:01:43 +01004930 if (!is_kernel_event(event))
4931 perf_remove_from_owner(event);
4932
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004933 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004934 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004935 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004936
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004937 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004938 /*
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +01004939 * Mark this event as STATE_DEAD, there is no external reference to it
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004940 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004941 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004942 * Anybody acquiring event->child_mutex after the below loop _must_
4943 * also see this, most importantly inherit_event() which will avoid
4944 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004945 *
4946 * Thus this guarantees that we will in fact observe and kill _ALL_
4947 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004948 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004949 event->state = PERF_EVENT_STATE_DEAD;
4950 raw_spin_unlock_irq(&ctx->lock);
4951
4952 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004953
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004954again:
4955 mutex_lock(&event->child_mutex);
4956 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004957
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004958 /*
4959 * Cannot change, child events are not migrated, see the
4960 * comment with perf_event_ctx_lock_nested().
4961 */
Will Deacon506458e2017-10-24 11:22:48 +01004962 ctx = READ_ONCE(child->ctx);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004963 /*
4964 * Since child_mutex nests inside ctx::mutex, we must jump
4965 * through hoops. We start by grabbing a reference on the ctx.
4966 *
4967 * Since the event cannot get freed while we hold the
4968 * child_mutex, the context must also exist and have a !0
4969 * reference count.
4970 */
4971 get_ctx(ctx);
4972
4973 /*
4974 * Now that we have a ctx ref, we can drop child_mutex, and
4975 * acquire ctx::mutex without fear of it going away. Then we
4976 * can re-acquire child_mutex.
4977 */
4978 mutex_unlock(&event->child_mutex);
4979 mutex_lock(&ctx->mutex);
4980 mutex_lock(&event->child_mutex);
4981
4982 /*
4983 * Now that we hold ctx::mutex and child_mutex, revalidate our
4984 * state, if child is still the first entry, it didn't get freed
4985 * and we can continue doing so.
4986 */
4987 tmp = list_first_entry_or_null(&event->child_list,
4988 struct perf_event, child_list);
4989 if (tmp == child) {
4990 perf_remove_from_context(child, DETACH_GROUP);
Peter Zijlstra82d94852018-01-09 13:10:30 +01004991 list_move(&child->child_list, &free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004992 /*
4993 * This matches the refcount bump in inherit_event();
4994 * this can't be the last reference.
4995 */
4996 put_event(event);
4997 }
4998
4999 mutex_unlock(&event->child_mutex);
5000 mutex_unlock(&ctx->mutex);
5001 put_ctx(ctx);
5002 goto again;
5003 }
5004 mutex_unlock(&event->child_mutex);
5005
Peter Zijlstra82d94852018-01-09 13:10:30 +01005006 list_for_each_entry_safe(child, tmp, &free_list, child_list) {
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02005007 void *var = &child->ctx->refcount;
5008
Peter Zijlstra82d94852018-01-09 13:10:30 +01005009 list_del(&child->child_list);
5010 free_event(child);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02005011
5012 /*
5013 * Wake any perf_event_free_task() waiting for this event to be
5014 * freed.
5015 */
5016 smp_mb(); /* pairs with wait_var_event() */
5017 wake_up_var(var);
Peter Zijlstra82d94852018-01-09 13:10:30 +01005018 }
5019
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01005020no_ctx:
5021 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02005022 return 0;
5023}
5024EXPORT_SYMBOL_GPL(perf_event_release_kernel);
5025
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02005026/*
5027 * Called when the last reference to the file is gone.
5028 */
Al Viroa6fa9412012-08-20 14:59:25 +01005029static int perf_release(struct inode *inode, struct file *file)
5030{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02005031 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01005032 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01005033}
5034
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005035static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005036{
5037 struct perf_event *child;
5038 u64 total = 0;
5039
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005040 *enabled = 0;
5041 *running = 0;
5042
Peter Zijlstra6f105812009-11-20 22:19:56 +01005043 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005044
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005045 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005046 total += perf_event_count(event);
5047
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005048 *enabled += event->total_time_enabled +
5049 atomic64_read(&event->child_total_time_enabled);
5050 *running += event->total_time_running +
5051 atomic64_read(&event->child_total_time_running);
5052
5053 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005054 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005055 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005056 *enabled += child->total_time_enabled;
5057 *running += child->total_time_running;
5058 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01005059 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060
5061 return total;
5062}
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005063
5064u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
5065{
5066 struct perf_event_context *ctx;
5067 u64 count;
5068
5069 ctx = perf_event_ctx_lock(event);
5070 count = __perf_event_read_value(event, enabled, running);
5071 perf_event_ctx_unlock(event, ctx);
5072
5073 return count;
5074}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005075EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005077static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005078 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005079{
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005080 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005081 struct perf_event *sub;
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005082 unsigned long flags;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005083 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005084 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01005085
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005086 ret = perf_event_read(leader, true);
5087 if (ret)
5088 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005089
Peter Zijlstraa9cd8192017-09-05 13:38:24 +02005090 raw_spin_lock_irqsave(&ctx->lock, flags);
5091
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005092 /*
5093 * Since we co-schedule groups, {enabled,running} times of siblings
5094 * will be identical to those of the leader, so we only publish one
5095 * set.
5096 */
5097 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
5098 values[n++] += leader->total_time_enabled +
5099 atomic64_read(&leader->child_total_time_enabled);
5100 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005101
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005102 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
5103 values[n++] += leader->total_time_running +
5104 atomic64_read(&leader->child_total_time_running);
5105 }
5106
5107 /*
5108 * Write {count,id} tuples for every sibling.
5109 */
5110 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005111 if (read_format & PERF_FORMAT_ID)
5112 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113
Peter Zijlstraedb39592018-03-15 17:36:56 +01005114 for_each_sibling_event(sub, leader) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005115 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005116 if (read_format & PERF_FORMAT_ID)
5117 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005118 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005119
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005120 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005121 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005122}
5123
5124static int perf_read_group(struct perf_event *event,
5125 u64 read_format, char __user *buf)
5126{
5127 struct perf_event *leader = event->group_leader, *child;
5128 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005129 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005130 u64 *values;
5131
5132 lockdep_assert_held(&ctx->mutex);
5133
5134 values = kzalloc(event->read_size, GFP_KERNEL);
5135 if (!values)
5136 return -ENOMEM;
5137
5138 values[0] = 1 + leader->nr_siblings;
5139
5140 /*
5141 * By locking the child_mutex of the leader we effectively
5142 * lock the child list of all siblings.. XXX explain how.
5143 */
5144 mutex_lock(&leader->child_mutex);
5145
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005146 ret = __perf_read_group_add(leader, read_format, values);
5147 if (ret)
5148 goto unlock;
5149
5150 list_for_each_entry(child, &leader->child_list, child_list) {
5151 ret = __perf_read_group_add(child, read_format, values);
5152 if (ret)
5153 goto unlock;
5154 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005155
5156 mutex_unlock(&leader->child_mutex);
5157
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005158 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005159 if (copy_to_user(buf, values, event->read_size))
5160 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005161 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005162
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005163unlock:
5164 mutex_unlock(&leader->child_mutex);
5165out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005166 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005167 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005168}
5169
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005170static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005171 u64 read_format, char __user *buf)
5172{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005173 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005174 u64 values[4];
5175 int n = 0;
5176
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005177 values[n++] = __perf_event_read_value(event, &enabled, &running);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005178 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
5179 values[n++] = enabled;
5180 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
5181 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182 if (read_format & PERF_FORMAT_ID)
5183 values[n++] = primary_event_id(event);
5184
5185 if (copy_to_user(buf, values, n * sizeof(u64)))
5186 return -EFAULT;
5187
5188 return n * sizeof(u64);
5189}
5190
Jiri Olsadc633982014-09-12 13:18:26 +02005191static bool is_event_hup(struct perf_event *event)
5192{
5193 bool no_children;
5194
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01005195 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02005196 return false;
5197
5198 mutex_lock(&event->child_mutex);
5199 no_children = list_empty(&event->child_list);
5200 mutex_unlock(&event->child_mutex);
5201 return no_children;
5202}
5203
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005204/*
5205 * Read the performance event - simple non blocking version for now
5206 */
5207static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005208__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209{
5210 u64 read_format = event->attr.read_format;
5211 int ret;
5212
5213 /*
Tobias Tefke788faab2018-07-09 12:57:15 +02005214 * Return end-of-file for a read on an event that is in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005215 * error state (i.e. because it was pinned but it couldn't be
5216 * scheduled on to the CPU at some point).
5217 */
5218 if (event->state == PERF_EVENT_STATE_ERROR)
5219 return 0;
5220
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005221 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222 return -ENOSPC;
5223
5224 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005225 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005226 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005227 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005228 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229
5230 return ret;
5231}
5232
5233static ssize_t
5234perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
5235{
5236 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005237 struct perf_event_context *ctx;
5238 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005240 ret = security_perf_event_read(event);
5241 if (ret)
5242 return ret;
5243
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005244 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005245 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005246 perf_event_ctx_unlock(event, ctx);
5247
5248 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005249}
5250
Al Viro9dd95742017-07-03 00:42:43 -04005251static __poll_t perf_poll(struct file *file, poll_table *wait)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005252{
5253 struct perf_event *event = file->private_data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005254 struct perf_buffer *rb;
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005255 __poll_t events = EPOLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005256
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02005257 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04005258
Jiri Olsadc633982014-09-12 13:18:26 +02005259 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04005260 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005261
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005262 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005263 * Pin the event->rb by taking event->mmap_mutex; otherwise
5264 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005265 */
5266 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005267 rb = event->rb;
5268 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005269 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005270 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271 return events;
5272}
5273
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005274static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005275{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005276 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02005277 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 perf_event_update_userpage(event);
5279}
5280
Like Xu52ba4b02019-10-27 18:52:39 +08005281/* Assume it's not an event with inherit set. */
5282u64 perf_event_pause(struct perf_event *event, bool reset)
5283{
5284 struct perf_event_context *ctx;
5285 u64 count;
5286
5287 ctx = perf_event_ctx_lock(event);
5288 WARN_ON_ONCE(event->attr.inherit);
5289 _perf_event_disable(event);
5290 count = local64_read(&event->count);
5291 if (reset)
5292 local64_set(&event->count, 0);
5293 perf_event_ctx_unlock(event, ctx);
5294
5295 return count;
5296}
5297EXPORT_SYMBOL_GPL(perf_event_pause);
5298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299/*
5300 * Holding the top-level event's child_mutex means that any
5301 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01005302 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303 * task existence requirements of perf_event_enable/disable.
5304 */
5305static void perf_event_for_each_child(struct perf_event *event,
5306 void (*func)(struct perf_event *))
5307{
5308 struct perf_event *child;
5309
5310 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005312 mutex_lock(&event->child_mutex);
5313 func(event);
5314 list_for_each_entry(child, &event->child_list, child_list)
5315 func(child);
5316 mutex_unlock(&event->child_mutex);
5317}
5318
5319static void perf_event_for_each(struct perf_event *event,
5320 void (*func)(struct perf_event *))
5321{
5322 struct perf_event_context *ctx = event->ctx;
5323 struct perf_event *sibling;
5324
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005325 lockdep_assert_held(&ctx->mutex);
5326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005327 event = event->group_leader;
5328
5329 perf_event_for_each_child(event, func);
Peter Zijlstraedb39592018-03-15 17:36:56 +01005330 for_each_sibling_event(sibling, event)
Michael Ellerman724b6da2012-04-11 11:54:13 +10005331 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005332}
5333
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005334static void __perf_event_period(struct perf_event *event,
5335 struct perf_cpu_context *cpuctx,
5336 struct perf_event_context *ctx,
5337 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01005338{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005339 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005340 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005343 event->attr.sample_freq = value;
5344 } else {
5345 event->attr.sample_period = value;
5346 event->hw.sample_period = value;
5347 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005348
5349 active = (event->state == PERF_EVENT_STATE_ACTIVE);
5350 if (active) {
5351 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01005352 /*
5353 * We could be throttled; unthrottle now to avoid the tick
5354 * trying to unthrottle while we already re-started the event.
5355 */
5356 if (event->hw.interrupts == MAX_INTERRUPTS) {
5357 event->hw.interrupts = 0;
5358 perf_log_throttle(event, 1);
5359 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005360 event->pmu->stop(event, PERF_EF_UPDATE);
5361 }
5362
5363 local64_set(&event->hw.period_left, 0);
5364
5365 if (active) {
5366 event->pmu->start(event, PERF_EF_RELOAD);
5367 perf_pmu_enable(ctx->pmu);
5368 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005369}
5370
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005371static int perf_event_check_period(struct perf_event *event, u64 value)
5372{
5373 return event->pmu->check_period(event, value);
5374}
5375
Like Xu3ca270f2019-10-27 18:52:38 +08005376static int _perf_event_period(struct perf_event *event, u64 value)
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005377{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005378 if (!is_sampling_event(event))
5379 return -EINVAL;
5380
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005381 if (!value)
5382 return -EINVAL;
5383
5384 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
5385 return -EINVAL;
5386
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005387 if (perf_event_check_period(event, value))
5388 return -EINVAL;
5389
Ravi Bangoria913a90b2019-06-04 09:59:53 +05305390 if (!event->attr.freq && (value & (1ULL << 63)))
5391 return -EINVAL;
5392
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005393 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005394
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005395 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396}
5397
Like Xu3ca270f2019-10-27 18:52:38 +08005398int perf_event_period(struct perf_event *event, u64 value)
5399{
5400 struct perf_event_context *ctx;
5401 int ret;
5402
5403 ctx = perf_event_ctx_lock(event);
5404 ret = _perf_event_period(event, value);
5405 perf_event_ctx_unlock(event, ctx);
5406
5407 return ret;
5408}
5409EXPORT_SYMBOL_GPL(perf_event_period);
5410
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005411static const struct file_operations perf_fops;
5412
Al Viro2903ff02012-08-28 12:52:22 -04005413static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005414{
Al Viro2903ff02012-08-28 12:52:22 -04005415 struct fd f = fdget(fd);
5416 if (!f.file)
5417 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005418
Al Viro2903ff02012-08-28 12:52:22 -04005419 if (f.file->f_op != &perf_fops) {
5420 fdput(f);
5421 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005422 }
Al Viro2903ff02012-08-28 12:52:22 -04005423 *p = f;
5424 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005425}
5426
5427static int perf_event_set_output(struct perf_event *event,
5428 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08005429static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07005430static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005431static int perf_copy_attr(struct perf_event_attr __user *uattr,
5432 struct perf_event_attr *attr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005433
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005434static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005435{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436 void (*func)(struct perf_event *);
5437 u32 flags = arg;
5438
5439 switch (cmd) {
5440 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005441 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442 break;
5443 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005444 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 break;
5446 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005447 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005448 break;
5449
5450 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005451 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452
5453 case PERF_EVENT_IOC_PERIOD:
Like Xu3ca270f2019-10-27 18:52:38 +08005454 {
5455 u64 value;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456
Like Xu3ca270f2019-10-27 18:52:38 +08005457 if (copy_from_user(&value, (u64 __user *)arg, sizeof(value)))
5458 return -EFAULT;
5459
5460 return _perf_event_period(event, value);
5461 }
Jiri Olsacf4957f2012-10-24 13:37:58 +02005462 case PERF_EVENT_IOC_ID:
5463 {
5464 u64 id = primary_event_id(event);
5465
5466 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
5467 return -EFAULT;
5468 return 0;
5469 }
5470
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005471 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005472 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005473 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005474 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04005475 struct perf_event *output_event;
5476 struct fd output;
5477 ret = perf_fget_light(arg, &output);
5478 if (ret)
5479 return ret;
5480 output_event = output.file->private_data;
5481 ret = perf_event_set_output(event, output_event);
5482 fdput(output);
5483 } else {
5484 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005485 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005486 return ret;
5487 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005488
Li Zefan6fb29152009-10-15 11:21:42 +08005489 case PERF_EVENT_IOC_SET_FILTER:
5490 return perf_event_set_filter(event, (void __user *)arg);
5491
Alexei Starovoitov25415172015-03-25 12:49:20 -07005492 case PERF_EVENT_IOC_SET_BPF:
5493 return perf_event_set_bpf_prog(event, arg);
5494
Wang Nan86e79722016-03-28 06:41:29 +00005495 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005496 struct perf_buffer *rb;
Wang Nan86e79722016-03-28 06:41:29 +00005497
5498 rcu_read_lock();
5499 rb = rcu_dereference(event->rb);
5500 if (!rb || !rb->nr_pages) {
5501 rcu_read_unlock();
5502 return -EINVAL;
5503 }
5504 rb_toggle_paused(rb, !!arg);
5505 rcu_read_unlock();
5506 return 0;
5507 }
Yonghong Songf371b302017-12-11 11:39:02 -08005508
5509 case PERF_EVENT_IOC_QUERY_BPF:
Yonghong Songf4e22982017-12-13 10:35:37 -08005510 return perf_event_query_prog_array(event, (void __user *)arg);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005511
5512 case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
5513 struct perf_event_attr new_attr;
5514 int err = perf_copy_attr((struct perf_event_attr __user *)arg,
5515 &new_attr);
5516
5517 if (err)
5518 return err;
5519
5520 return perf_event_modify_attr(event, &new_attr);
5521 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005522 default:
5523 return -ENOTTY;
5524 }
5525
5526 if (flags & PERF_IOC_FLAG_GROUP)
5527 perf_event_for_each(event, func);
5528 else
5529 perf_event_for_each_child(event, func);
5530
5531 return 0;
5532}
5533
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005534static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5535{
5536 struct perf_event *event = file->private_data;
5537 struct perf_event_context *ctx;
5538 long ret;
5539
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005540 /* Treat ioctl like writes as it is likely a mutating operation. */
5541 ret = security_perf_event_write(event);
5542 if (ret)
5543 return ret;
5544
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005545 ctx = perf_event_ctx_lock(event);
5546 ret = _perf_ioctl(event, cmd, arg);
5547 perf_event_ctx_unlock(event, ctx);
5548
5549 return ret;
5550}
5551
Pawel Mollb3f20782014-06-13 16:03:32 +01005552#ifdef CONFIG_COMPAT
5553static long perf_compat_ioctl(struct file *file, unsigned int cmd,
5554 unsigned long arg)
5555{
5556 switch (_IOC_NR(cmd)) {
5557 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
5558 case _IOC_NR(PERF_EVENT_IOC_ID):
Eugene Syromiatnikov82489c52018-05-21 14:34:20 +02005559 case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF):
5560 case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES):
Pawel Mollb3f20782014-06-13 16:03:32 +01005561 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
5562 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
5563 cmd &= ~IOCSIZE_MASK;
5564 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
5565 }
5566 break;
5567 }
5568 return perf_ioctl(file, cmd, arg);
5569}
5570#else
5571# define perf_compat_ioctl NULL
5572#endif
5573
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005574int perf_event_task_enable(void)
5575{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005576 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005577 struct perf_event *event;
5578
5579 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005580 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5581 ctx = perf_event_ctx_lock(event);
5582 perf_event_for_each_child(event, _perf_event_enable);
5583 perf_event_ctx_unlock(event, ctx);
5584 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005585 mutex_unlock(&current->perf_event_mutex);
5586
5587 return 0;
5588}
5589
5590int perf_event_task_disable(void)
5591{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005592 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593 struct perf_event *event;
5594
5595 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005596 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5597 ctx = perf_event_ctx_lock(event);
5598 perf_event_for_each_child(event, _perf_event_disable);
5599 perf_event_ctx_unlock(event, ctx);
5600 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601 mutex_unlock(&current->perf_event_mutex);
5602
5603 return 0;
5604}
5605
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005606static int perf_event_index(struct perf_event *event)
5607{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005608 if (event->hw.state & PERF_HES_STOPPED)
5609 return 0;
5610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005611 if (event->state != PERF_EVENT_STATE_ACTIVE)
5612 return 0;
5613
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005614 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005615}
5616
Eric B Munsonc4794292011-06-23 16:34:38 -04005617static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005618 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04005619 u64 *enabled,
5620 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04005621{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005622 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04005623
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005624 *now = perf_clock();
5625 ctx_time = event->shadow_ctx_time + *now;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02005626 __perf_update_times(event, ctx_time, enabled, running);
Eric B Munsonc4794292011-06-23 16:34:38 -04005627}
5628
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005629static void perf_event_init_userpage(struct perf_event *event)
5630{
5631 struct perf_event_mmap_page *userpg;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005632 struct perf_buffer *rb;
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005633
5634 rcu_read_lock();
5635 rb = rcu_dereference(event->rb);
5636 if (!rb)
5637 goto unlock;
5638
5639 userpg = rb->user_page;
5640
5641 /* Allow new userspace to detect that bit 0 is deprecated */
5642 userpg->cap_bit0_is_deprecated = 1;
5643 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02005644 userpg->data_offset = PAGE_SIZE;
5645 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005646
5647unlock:
5648 rcu_read_unlock();
5649}
5650
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005651void __weak arch_perf_update_userpage(
5652 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005653{
5654}
5655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005656/*
5657 * Callers need to ensure there can be no nesting of this function, otherwise
5658 * the seqlock logic goes bad. We can not serialize this because the arch
5659 * code calls this from NMI context.
5660 */
5661void perf_event_update_userpage(struct perf_event *event)
5662{
5663 struct perf_event_mmap_page *userpg;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005664 struct perf_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005665 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005666
5667 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02005668 rb = rcu_dereference(event->rb);
5669 if (!rb)
5670 goto unlock;
5671
Eric B Munson0d641202011-06-24 12:26:26 -04005672 /*
5673 * compute total_time_enabled, total_time_running
5674 * based on snapshot values taken when the event
5675 * was last scheduled in.
5676 *
5677 * we cannot simply called update_context_time()
5678 * because of locking issue as we can be called in
5679 * NMI context
5680 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005681 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682
Frederic Weisbecker76369132011-05-19 19:55:04 +02005683 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005684 /*
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07005685 * Disable preemption to guarantee consistent time stamps are stored to
5686 * the user page.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005687 */
5688 preempt_disable();
5689 ++userpg->lock;
5690 barrier();
5691 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005692 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01005693 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02005694 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695
Eric B Munson0d641202011-06-24 12:26:26 -04005696 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005697 atomic64_read(&event->child_total_time_enabled);
5698
Eric B Munson0d641202011-06-24 12:26:26 -04005699 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700 atomic64_read(&event->child_total_time_running);
5701
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005702 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005704 barrier();
5705 ++userpg->lock;
5706 preempt_enable();
5707unlock:
5708 rcu_read_unlock();
5709}
Suzuki K Poulose82975c42018-01-02 11:25:26 +00005710EXPORT_SYMBOL_GPL(perf_event_update_userpage);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005711
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305712static vm_fault_t perf_mmap_fault(struct vm_fault *vmf)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005713{
Dave Jiang11bac802017-02-24 14:56:41 -08005714 struct perf_event *event = vmf->vma->vm_file->private_data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005715 struct perf_buffer *rb;
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305716 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005717
5718 if (vmf->flags & FAULT_FLAG_MKWRITE) {
5719 if (vmf->pgoff == 0)
5720 ret = 0;
5721 return ret;
5722 }
5723
5724 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005725 rb = rcu_dereference(event->rb);
5726 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005727 goto unlock;
5728
5729 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
5730 goto unlock;
5731
Frederic Weisbecker76369132011-05-19 19:55:04 +02005732 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005733 if (!vmf->page)
5734 goto unlock;
5735
5736 get_page(vmf->page);
Dave Jiang11bac802017-02-24 14:56:41 -08005737 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005738 vmf->page->index = vmf->pgoff;
5739
5740 ret = 0;
5741unlock:
5742 rcu_read_unlock();
5743
5744 return ret;
5745}
5746
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005747static void ring_buffer_attach(struct perf_event *event,
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005748 struct perf_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005749{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005750 struct perf_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005751 unsigned long flags;
5752
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005753 if (event->rb) {
5754 /*
5755 * Should be impossible, we set this when removing
5756 * event->rb_entry and wait/clear when adding event->rb_entry.
5757 */
5758 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005759
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005760 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005761 spin_lock_irqsave(&old_rb->event_lock, flags);
5762 list_del_rcu(&event->rb_entry);
5763 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005764
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005765 event->rcu_batches = get_state_synchronize_rcu();
5766 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005767 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005768
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005769 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005770 if (event->rcu_pending) {
5771 cond_synchronize_rcu(event->rcu_batches);
5772 event->rcu_pending = 0;
5773 }
5774
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005775 spin_lock_irqsave(&rb->event_lock, flags);
5776 list_add_rcu(&event->rb_entry, &rb->event_list);
5777 spin_unlock_irqrestore(&rb->event_lock, flags);
5778 }
5779
Alexander Shishkin767ae082016-09-06 16:23:49 +03005780 /*
5781 * Avoid racing with perf_mmap_close(AUX): stop the event
5782 * before swizzling the event::rb pointer; if it's getting
5783 * unmapped, its aux_mmap_count will be 0 and it won't
5784 * restart. See the comment in __perf_pmu_output_stop().
5785 *
5786 * Data will inevitably be lost when set_output is done in
5787 * mid-air, but then again, whoever does it like this is
5788 * not in for the data anyway.
5789 */
5790 if (has_aux(event))
5791 perf_event_stop(event, 0);
5792
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005793 rcu_assign_pointer(event->rb, rb);
5794
5795 if (old_rb) {
5796 ring_buffer_put(old_rb);
5797 /*
5798 * Since we detached before setting the new rb, so that we
5799 * could attach the new rb, we could have missed a wakeup.
5800 * Provide it now.
5801 */
5802 wake_up_all(&event->waitq);
5803 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005804}
5805
5806static void ring_buffer_wakeup(struct perf_event *event)
5807{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005808 struct perf_buffer *rb;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005809
5810 rcu_read_lock();
5811 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005812 if (rb) {
5813 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
5814 wake_up_all(&event->waitq);
5815 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005816 rcu_read_unlock();
5817}
5818
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005819struct perf_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005821 struct perf_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005823 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005824 rb = rcu_dereference(event->rb);
5825 if (rb) {
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005826 if (!refcount_inc_not_zero(&rb->refcount))
Frederic Weisbecker76369132011-05-19 19:55:04 +02005827 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005828 }
5829 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005830
Frederic Weisbecker76369132011-05-19 19:55:04 +02005831 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005832}
5833
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005834void ring_buffer_put(struct perf_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005835{
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005836 if (!refcount_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005837 return;
5838
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005839 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005840
Frederic Weisbecker76369132011-05-19 19:55:04 +02005841 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005842}
5843
5844static void perf_mmap_open(struct vm_area_struct *vma)
5845{
5846 struct perf_event *event = vma->vm_file->private_data;
5847
5848 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005849 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005850
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005851 if (vma->vm_pgoff)
5852 atomic_inc(&event->rb->aux_mmap_count);
5853
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005854 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005855 event->pmu->event_mapped(event, vma->vm_mm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005856}
5857
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005858static void perf_pmu_output_stop(struct perf_event *event);
5859
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005860/*
5861 * A buffer can be mmap()ed multiple times; either directly through the same
5862 * event, or through other events by use of perf_event_set_output().
5863 *
5864 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5865 * the buffer here, where we still have a VM context. This means we need
5866 * to detach all events redirecting to us.
5867 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005868static void perf_mmap_close(struct vm_area_struct *vma)
5869{
5870 struct perf_event *event = vma->vm_file->private_data;
5871
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005872 struct perf_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005873 struct user_struct *mmap_user = rb->mmap_user;
5874 int mmap_locked = rb->mmap_locked;
5875 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005876
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005877 if (event->pmu->event_unmapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005878 event->pmu->event_unmapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005879
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005880 /*
5881 * rb->aux_mmap_count will always drop before rb->mmap_count and
5882 * event->mmap_count, so it is ok to use event->mmap_mutex to
5883 * serialize with perf_mmap here.
5884 */
5885 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5886 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005887 /*
5888 * Stop all AUX events that are writing to this buffer,
5889 * so that we can free its AUX pages and corresponding PMU
5890 * data. Note that after rb::aux_mmap_count dropped to zero,
5891 * they won't start any more (see perf_aux_output_begin()).
5892 */
5893 perf_pmu_output_stop(event);
5894
5895 /* now it's safe to free the pages */
Alexander Shishkin36b3db02019-11-15 18:08:18 +02005896 atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm);
5897 atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005898
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005899 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005900 rb_free_aux(rb);
Elena Reshetovaca3bb3d2019-01-28 14:27:28 +02005901 WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005902
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005903 mutex_unlock(&event->mmap_mutex);
5904 }
5905
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005906 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005907
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005908 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005909 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005910
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005911 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005912 mutex_unlock(&event->mmap_mutex);
5913
5914 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005915 if (atomic_read(&rb->mmap_count))
5916 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005917
5918 /*
5919 * No other mmap()s, detach from all other events that might redirect
5920 * into the now unreachable buffer. Somewhat complicated by the
5921 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5922 */
5923again:
5924 rcu_read_lock();
5925 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5926 if (!atomic_long_inc_not_zero(&event->refcount)) {
5927 /*
5928 * This event is en-route to free_event() which will
5929 * detach it and remove it from the list.
5930 */
5931 continue;
5932 }
5933 rcu_read_unlock();
5934
5935 mutex_lock(&event->mmap_mutex);
5936 /*
5937 * Check we didn't race with perf_event_set_output() which can
5938 * swizzle the rb from under us while we were waiting to
5939 * acquire mmap_mutex.
5940 *
5941 * If we find a different rb; ignore this event, a next
5942 * iteration will no longer find it on the list. We have to
5943 * still restart the iteration to make sure we're not now
5944 * iterating the wrong list.
5945 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005946 if (event->rb == rb)
5947 ring_buffer_attach(event, NULL);
5948
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005949 mutex_unlock(&event->mmap_mutex);
5950 put_event(event);
5951
5952 /*
5953 * Restart the iteration; either we're on the wrong list or
5954 * destroyed its integrity by doing a deletion.
5955 */
5956 goto again;
5957 }
5958 rcu_read_unlock();
5959
5960 /*
5961 * It could be there's still a few 0-ref events on the list; they'll
5962 * get cleaned up by free_event() -- they'll also still have their
5963 * ref on the rb and will free it whenever they are done with it.
5964 *
5965 * Aside from that, this buffer is 'fully' detached and unmapped,
5966 * undo the VM accounting.
5967 */
5968
Song Liud44248a2019-09-04 14:46:18 -07005969 atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
5970 &mmap_user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08005971 atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005972 free_uid(mmap_user);
5973
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005974out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005975 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005976}
5977
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04005978static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005979 .open = perf_mmap_open,
Ingo Molnarfca0c112018-12-03 10:52:21 +01005980 .close = perf_mmap_close, /* non mergeable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005981 .fault = perf_mmap_fault,
5982 .page_mkwrite = perf_mmap_fault,
5983};
5984
5985static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5986{
5987 struct perf_event *event = file->private_data;
5988 unsigned long user_locked, user_lock_limit;
5989 struct user_struct *user = current_user();
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005990 struct perf_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005991 unsigned long locked, lock_limit;
5992 unsigned long vma_size;
5993 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005994 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005995 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005996
Peter Zijlstrac7920612010-05-18 10:33:24 +02005997 /*
5998 * Don't allow mmap() of inherited per-task counters. This would
5999 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02006000 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02006001 */
6002 if (event->cpu == -1 && event->attr.inherit)
6003 return -EINVAL;
6004
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006005 if (!(vma->vm_flags & VM_SHARED))
6006 return -EINVAL;
6007
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006008 ret = security_perf_event_read(event);
6009 if (ret)
6010 return ret;
6011
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006012 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006013
6014 if (vma->vm_pgoff == 0) {
6015 nr_pages = (vma_size / PAGE_SIZE) - 1;
6016 } else {
6017 /*
6018 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
6019 * mapped, all subsequent mappings should have the same size
6020 * and offset. Must be above the normal perf buffer.
6021 */
6022 u64 aux_offset, aux_size;
6023
6024 if (!event->rb)
6025 return -EINVAL;
6026
6027 nr_pages = vma_size / PAGE_SIZE;
6028
6029 mutex_lock(&event->mmap_mutex);
6030 ret = -EINVAL;
6031
6032 rb = event->rb;
6033 if (!rb)
6034 goto aux_unlock;
6035
Mark Rutland6aa7de02017-10-23 14:07:29 -07006036 aux_offset = READ_ONCE(rb->user_page->aux_offset);
6037 aux_size = READ_ONCE(rb->user_page->aux_size);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006038
6039 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
6040 goto aux_unlock;
6041
6042 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
6043 goto aux_unlock;
6044
6045 /* already mapped with a different offset */
6046 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
6047 goto aux_unlock;
6048
6049 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
6050 goto aux_unlock;
6051
6052 /* already mapped with a different size */
6053 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
6054 goto aux_unlock;
6055
6056 if (!is_power_of_2(nr_pages))
6057 goto aux_unlock;
6058
6059 if (!atomic_inc_not_zero(&rb->mmap_count))
6060 goto aux_unlock;
6061
6062 if (rb_has_aux(rb)) {
6063 atomic_inc(&rb->aux_mmap_count);
6064 ret = 0;
6065 goto unlock;
6066 }
6067
6068 atomic_set(&rb->aux_mmap_count, 1);
6069 user_extra = nr_pages;
6070
6071 goto accounting;
6072 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073
6074 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006075 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006076 * can do bitmasks instead of modulo.
6077 */
Kan Liang2ed11312015-03-02 02:14:26 -05006078 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006079 return -EINVAL;
6080
6081 if (vma_size != PAGE_SIZE * (1 + nr_pages))
6082 return -EINVAL;
6083
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006084 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006085again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006086 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006087 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006088 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006090 goto unlock;
6091 }
6092
6093 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
6094 /*
6095 * Raced against perf_mmap_close() through
6096 * perf_event_set_output(). Try again, hope for better
6097 * luck.
6098 */
6099 mutex_unlock(&event->mmap_mutex);
6100 goto again;
6101 }
6102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103 goto unlock;
6104 }
6105
6106 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006107
6108accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006109 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
6110
6111 /*
6112 * Increase the limit linearly with more CPUs:
6113 */
6114 user_lock_limit *= num_online_cpus();
6115
Song Liu00346152020-01-23 10:11:46 -08006116 user_locked = atomic_long_read(&user->locked_vm);
6117
6118 /*
6119 * sysctl_perf_event_mlock may have changed, so that
6120 * user->locked_vm > user_lock_limit
6121 */
6122 if (user_locked > user_lock_limit)
6123 user_locked = user_lock_limit;
6124 user_locked += user_extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006125
Alexander Shishkinc4b75472019-11-20 19:06:40 +02006126 if (user_locked > user_lock_limit) {
Song Liud44248a2019-09-04 14:46:18 -07006127 /*
6128 * charge locked_vm until it hits user_lock_limit;
6129 * charge the rest from pinned_vm
6130 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006131 extra = user_locked - user_lock_limit;
Song Liud44248a2019-09-04 14:46:18 -07006132 user_extra -= extra;
6133 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006134
Jiri Slaby78d7d402010-03-05 13:42:54 -08006135 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006136 lock_limit >>= PAGE_SHIFT;
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006137 locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006138
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006139 if ((locked > lock_limit) && perf_is_paranoid() &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006140 !capable(CAP_IPC_LOCK)) {
6141 ret = -EPERM;
6142 goto unlock;
6143 }
6144
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006145 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02006146
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006147 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02006148 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006149
Frederic Weisbecker76369132011-05-19 19:55:04 +02006150 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006151 rb = rb_alloc(nr_pages,
6152 event->attr.watermark ? event->attr.wakeup_watermark : 0,
6153 event->cpu, flags);
6154
6155 if (!rb) {
6156 ret = -ENOMEM;
6157 goto unlock;
6158 }
6159
6160 atomic_set(&rb->mmap_count, 1);
6161 rb->mmap_user = get_current_user();
6162 rb->mmap_locked = extra;
6163
6164 ring_buffer_attach(event, rb);
6165
6166 perf_event_init_userpage(event);
6167 perf_event_update_userpage(event);
6168 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02006169 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
6170 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006171 if (!ret)
6172 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006173 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02006174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006175unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006176 if (!ret) {
6177 atomic_long_add(user_extra, &user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006178 atomic64_add(extra, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006179
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006180 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006181 } else if (rb) {
6182 atomic_dec(&rb->mmap_count);
6183 }
6184aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006185 mutex_unlock(&event->mmap_mutex);
6186
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006187 /*
6188 * Since pinned accounting is per vm we cannot allow fork() to copy our
6189 * vma.
6190 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02006191 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006192 vma->vm_ops = &perf_mmap_vmops;
6193
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07006194 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02006195 event->pmu->event_mapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07006196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006197 return ret;
6198}
6199
6200static int perf_fasync(int fd, struct file *filp, int on)
6201{
Al Viro496ad9a2013-01-23 17:07:38 -05006202 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006203 struct perf_event *event = filp->private_data;
6204 int retval;
6205
Al Viro59551022016-01-22 15:40:57 -05006206 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006207 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05006208 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006209
6210 if (retval < 0)
6211 return retval;
6212
6213 return 0;
6214}
6215
6216static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01006217 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006218 .release = perf_release,
6219 .read = perf_read,
6220 .poll = perf_poll,
6221 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01006222 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006223 .mmap = perf_mmap,
6224 .fasync = perf_fasync,
6225};
6226
6227/*
6228 * Perf event wakeup
6229 *
6230 * If there's data, ensure we set the poll() state and publish everything
6231 * to user-space before waking everybody up.
6232 */
6233
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006234static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
6235{
6236 /* only the parent has fasync state */
6237 if (event->parent)
6238 event = event->parent;
6239 return &event->fasync;
6240}
6241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006242void perf_event_wakeup(struct perf_event *event)
6243{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006244 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006245
6246 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006247 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006248 event->pending_kill = 0;
6249 }
6250}
6251
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006252static void perf_pending_event_disable(struct perf_event *event)
6253{
6254 int cpu = READ_ONCE(event->pending_disable);
6255
6256 if (cpu < 0)
6257 return;
6258
6259 if (cpu == smp_processor_id()) {
6260 WRITE_ONCE(event->pending_disable, -1);
6261 perf_event_disable_local(event);
6262 return;
6263 }
6264
6265 /*
6266 * CPU-A CPU-B
6267 *
6268 * perf_event_disable_inatomic()
6269 * @pending_disable = CPU-A;
6270 * irq_work_queue();
6271 *
6272 * sched-out
6273 * @pending_disable = -1;
6274 *
6275 * sched-in
6276 * perf_event_disable_inatomic()
6277 * @pending_disable = CPU-B;
6278 * irq_work_queue(); // FAILS
6279 *
6280 * irq_work_run()
6281 * perf_pending_event()
6282 *
6283 * But the event runs on CPU-B and wants disabling there.
6284 */
6285 irq_work_queue_on(&event->pending, cpu);
6286}
6287
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006288static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006289{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006290 struct perf_event *event = container_of(entry, struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01006291 int rctx;
6292
6293 rctx = perf_swevent_get_recursion_context();
6294 /*
6295 * If we 'fail' here, that's OK, it means recursion is already disabled
6296 * and we won't recurse 'further'.
6297 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006298
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006299 perf_pending_event_disable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006300
6301 if (event->pending_wakeup) {
6302 event->pending_wakeup = 0;
6303 perf_event_wakeup(event);
6304 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01006305
6306 if (rctx >= 0)
6307 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006308}
6309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006310/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006311 * We assume there is only KVM supporting the callbacks.
6312 * Later on, we might change it to a list if there is
6313 * another virtualization implementation supporting the callbacks.
6314 */
6315struct perf_guest_info_callbacks *perf_guest_cbs;
6316
6317int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6318{
6319 perf_guest_cbs = cbs;
6320 return 0;
6321}
6322EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
6323
6324int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6325{
6326 perf_guest_cbs = NULL;
6327 return 0;
6328}
6329EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
6330
Jiri Olsa40189942012-08-07 15:20:37 +02006331static void
6332perf_output_sample_regs(struct perf_output_handle *handle,
6333 struct pt_regs *regs, u64 mask)
6334{
6335 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306336 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02006337
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306338 bitmap_from_u64(_mask, mask);
6339 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02006340 u64 val;
6341
6342 val = perf_reg_value(regs, bit);
6343 perf_output_put(handle, val);
6344 }
6345}
6346
Stephane Eranian60e23642014-09-24 13:48:37 +02006347static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006348 struct pt_regs *regs,
6349 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02006350{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006351 if (user_mode(regs)) {
6352 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02006353 regs_user->regs = regs;
Peter Zijlstra085ebfe2019-05-29 14:37:24 +02006354 } else if (!(current->flags & PF_KTHREAD)) {
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006355 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006356 } else {
6357 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
6358 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02006359 }
6360}
6361
Stephane Eranian60e23642014-09-24 13:48:37 +02006362static void perf_sample_regs_intr(struct perf_regs *regs_intr,
6363 struct pt_regs *regs)
6364{
6365 regs_intr->regs = regs;
6366 regs_intr->abi = perf_reg_abi(current);
6367}
6368
6369
Jiri Olsac5ebced2012-08-07 15:20:40 +02006370/*
6371 * Get remaining task size from user stack pointer.
6372 *
6373 * It'd be better to take stack vma map and limit this more
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03006374 * precisely, but there's no way to get it safely under interrupt,
Jiri Olsac5ebced2012-08-07 15:20:40 +02006375 * so using TASK_SIZE as limit.
6376 */
6377static u64 perf_ustack_task_size(struct pt_regs *regs)
6378{
6379 unsigned long addr = perf_user_stack_pointer(regs);
6380
6381 if (!addr || addr >= TASK_SIZE)
6382 return 0;
6383
6384 return TASK_SIZE - addr;
6385}
6386
6387static u16
6388perf_sample_ustack_size(u16 stack_size, u16 header_size,
6389 struct pt_regs *regs)
6390{
6391 u64 task_size;
6392
6393 /* No regs, no stack pointer, no dump. */
6394 if (!regs)
6395 return 0;
6396
6397 /*
6398 * Check if we fit in with the requested stack size into the:
6399 * - TASK_SIZE
6400 * If we don't, we limit the size to the TASK_SIZE.
6401 *
6402 * - remaining sample size
6403 * If we don't, we customize the stack size to
6404 * fit in to the remaining sample size.
6405 */
6406
6407 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
6408 stack_size = min(stack_size, (u16) task_size);
6409
6410 /* Current header size plus static size and dynamic size. */
6411 header_size += 2 * sizeof(u64);
6412
6413 /* Do we fit in with the current stack dump size? */
6414 if ((u16) (header_size + stack_size) < header_size) {
6415 /*
6416 * If we overflow the maximum size for the sample,
6417 * we customize the stack dump size to fit in.
6418 */
6419 stack_size = USHRT_MAX - header_size - sizeof(u64);
6420 stack_size = round_up(stack_size, sizeof(u64));
6421 }
6422
6423 return stack_size;
6424}
6425
6426static void
6427perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
6428 struct pt_regs *regs)
6429{
6430 /* Case of a kernel thread, nothing to dump */
6431 if (!regs) {
6432 u64 size = 0;
6433 perf_output_put(handle, size);
6434 } else {
6435 unsigned long sp;
6436 unsigned int rem;
6437 u64 dyn_size;
Yabin Cui02e18442018-08-23 15:59:35 -07006438 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02006439
6440 /*
6441 * We dump:
6442 * static size
6443 * - the size requested by user or the best one we can fit
6444 * in to the sample max size
6445 * data
6446 * - user stack dump data
6447 * dynamic size
6448 * - the actual dumped size
6449 */
6450
6451 /* Static size. */
6452 perf_output_put(handle, dump_size);
6453
6454 /* Data. */
6455 sp = perf_user_stack_pointer(regs);
Yabin Cui02e18442018-08-23 15:59:35 -07006456 fs = get_fs();
6457 set_fs(USER_DS);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006458 rem = __output_copy_user(handle, (void *) sp, dump_size);
Yabin Cui02e18442018-08-23 15:59:35 -07006459 set_fs(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006460 dyn_size = dump_size - rem;
6461
6462 perf_output_skip(handle, rem);
6463
6464 /* Dynamic size. */
6465 perf_output_put(handle, dyn_size);
6466 }
6467}
6468
Alexander Shishkina4faf002019-10-25 17:08:33 +03006469static unsigned long perf_prepare_sample_aux(struct perf_event *event,
6470 struct perf_sample_data *data,
6471 size_t size)
6472{
6473 struct perf_event *sampler = event->aux_event;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006474 struct perf_buffer *rb;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006475
6476 data->aux_size = 0;
6477
6478 if (!sampler)
6479 goto out;
6480
6481 if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE))
6482 goto out;
6483
6484 if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id()))
6485 goto out;
6486
6487 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6488 if (!rb)
6489 goto out;
6490
6491 /*
6492 * If this is an NMI hit inside sampling code, don't take
6493 * the sample. See also perf_aux_sample_output().
6494 */
6495 if (READ_ONCE(rb->aux_in_sampling)) {
6496 data->aux_size = 0;
6497 } else {
6498 size = min_t(size_t, size, perf_aux_size(rb));
6499 data->aux_size = ALIGN(size, sizeof(u64));
6500 }
6501 ring_buffer_put(rb);
6502
6503out:
6504 return data->aux_size;
6505}
6506
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006507long perf_pmu_snapshot_aux(struct perf_buffer *rb,
Alexander Shishkina4faf002019-10-25 17:08:33 +03006508 struct perf_event *event,
6509 struct perf_output_handle *handle,
6510 unsigned long size)
6511{
6512 unsigned long flags;
6513 long ret;
6514
6515 /*
6516 * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler
6517 * paths. If we start calling them in NMI context, they may race with
6518 * the IRQ ones, that is, for example, re-starting an event that's just
6519 * been stopped, which is why we're using a separate callback that
6520 * doesn't change the event state.
6521 *
6522 * IRQs need to be disabled to prevent IPIs from racing with us.
6523 */
6524 local_irq_save(flags);
6525 /*
6526 * Guard against NMI hits inside the critical section;
6527 * see also perf_prepare_sample_aux().
6528 */
6529 WRITE_ONCE(rb->aux_in_sampling, 1);
6530 barrier();
6531
6532 ret = event->pmu->snapshot_aux(event, handle, size);
6533
6534 barrier();
6535 WRITE_ONCE(rb->aux_in_sampling, 0);
6536 local_irq_restore(flags);
6537
6538 return ret;
6539}
6540
6541static void perf_aux_sample_output(struct perf_event *event,
6542 struct perf_output_handle *handle,
6543 struct perf_sample_data *data)
6544{
6545 struct perf_event *sampler = event->aux_event;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006546 struct perf_buffer *rb;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006547 unsigned long pad;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006548 long size;
6549
6550 if (WARN_ON_ONCE(!sampler || !data->aux_size))
6551 return;
6552
6553 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6554 if (!rb)
6555 return;
6556
6557 size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size);
6558
6559 /*
6560 * An error here means that perf_output_copy() failed (returned a
6561 * non-zero surplus that it didn't copy), which in its current
6562 * enlightened implementation is not possible. If that changes, we'd
6563 * like to know.
6564 */
6565 if (WARN_ON_ONCE(size < 0))
6566 goto out_put;
6567
6568 /*
6569 * The pad comes from ALIGN()ing data->aux_size up to u64 in
6570 * perf_prepare_sample_aux(), so should not be more than that.
6571 */
6572 pad = data->aux_size - size;
6573 if (WARN_ON_ONCE(pad >= sizeof(u64)))
6574 pad = 8;
6575
6576 if (pad) {
6577 u64 zero = 0;
6578 perf_output_copy(handle, &zero, pad);
6579 }
6580
6581out_put:
6582 ring_buffer_put(rb);
6583}
6584
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006585static void __perf_event_header__init_id(struct perf_event_header *header,
6586 struct perf_sample_data *data,
6587 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006588{
6589 u64 sample_type = event->attr.sample_type;
6590
6591 data->type = sample_type;
6592 header->size += event->id_header_size;
6593
6594 if (sample_type & PERF_SAMPLE_TID) {
6595 /* namespace issues */
6596 data->tid_entry.pid = perf_event_pid(event, current);
6597 data->tid_entry.tid = perf_event_tid(event, current);
6598 }
6599
6600 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01006601 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006602
Adrian Hunterff3d5272013-08-27 11:23:07 +03006603 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006604 data->id = primary_event_id(event);
6605
6606 if (sample_type & PERF_SAMPLE_STREAM_ID)
6607 data->stream_id = event->id;
6608
6609 if (sample_type & PERF_SAMPLE_CPU) {
6610 data->cpu_entry.cpu = raw_smp_processor_id();
6611 data->cpu_entry.reserved = 0;
6612 }
6613}
6614
Frederic Weisbecker76369132011-05-19 19:55:04 +02006615void perf_event_header__init_id(struct perf_event_header *header,
6616 struct perf_sample_data *data,
6617 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006618{
6619 if (event->attr.sample_id_all)
6620 __perf_event_header__init_id(header, data, event);
6621}
6622
6623static void __perf_event__output_id_sample(struct perf_output_handle *handle,
6624 struct perf_sample_data *data)
6625{
6626 u64 sample_type = data->type;
6627
6628 if (sample_type & PERF_SAMPLE_TID)
6629 perf_output_put(handle, data->tid_entry);
6630
6631 if (sample_type & PERF_SAMPLE_TIME)
6632 perf_output_put(handle, data->time);
6633
6634 if (sample_type & PERF_SAMPLE_ID)
6635 perf_output_put(handle, data->id);
6636
6637 if (sample_type & PERF_SAMPLE_STREAM_ID)
6638 perf_output_put(handle, data->stream_id);
6639
6640 if (sample_type & PERF_SAMPLE_CPU)
6641 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03006642
6643 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6644 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006645}
6646
Frederic Weisbecker76369132011-05-19 19:55:04 +02006647void perf_event__output_id_sample(struct perf_event *event,
6648 struct perf_output_handle *handle,
6649 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006650{
6651 if (event->attr.sample_id_all)
6652 __perf_event__output_id_sample(handle, sample);
6653}
6654
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006655static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006656 struct perf_event *event,
6657 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006658{
6659 u64 read_format = event->attr.read_format;
6660 u64 values[4];
6661 int n = 0;
6662
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006663 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006664 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006665 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006666 atomic64_read(&event->child_total_time_enabled);
6667 }
6668 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006669 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006670 atomic64_read(&event->child_total_time_running);
6671 }
6672 if (read_format & PERF_FORMAT_ID)
6673 values[n++] = primary_event_id(event);
6674
Frederic Weisbecker76369132011-05-19 19:55:04 +02006675 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006676}
6677
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006678static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006679 struct perf_event *event,
6680 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006681{
6682 struct perf_event *leader = event->group_leader, *sub;
6683 u64 read_format = event->attr.read_format;
6684 u64 values[5];
6685 int n = 0;
6686
6687 values[n++] = 1 + leader->nr_siblings;
6688
6689 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02006690 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006691
6692 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02006693 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006694
Peter Zijlstra9e5b1272018-03-09 12:52:04 +01006695 if ((leader != event) &&
6696 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006697 leader->pmu->read(leader);
6698
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006699 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006700 if (read_format & PERF_FORMAT_ID)
6701 values[n++] = primary_event_id(leader);
6702
Frederic Weisbecker76369132011-05-19 19:55:04 +02006703 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006704
Peter Zijlstraedb39592018-03-15 17:36:56 +01006705 for_each_sibling_event(sub, leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006706 n = 0;
6707
Jiri Olsa6f5ab002012-10-15 20:13:45 +02006708 if ((sub != event) &&
6709 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006710 sub->pmu->read(sub);
6711
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006712 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006713 if (read_format & PERF_FORMAT_ID)
6714 values[n++] = primary_event_id(sub);
6715
Frederic Weisbecker76369132011-05-19 19:55:04 +02006716 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006717 }
6718}
6719
Stephane Eranianeed01522010-10-26 16:08:01 +02006720#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
6721 PERF_FORMAT_TOTAL_TIME_RUNNING)
6722
Peter Zijlstraba5213a2017-05-30 11:45:12 +02006723/*
6724 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
6725 *
6726 * The problem is that its both hard and excessively expensive to iterate the
6727 * child list, not to mention that its impossible to IPI the children running
6728 * on another CPU, from interrupt/NMI context.
6729 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006730static void perf_output_read(struct perf_output_handle *handle,
6731 struct perf_event *event)
6732{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006733 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02006734 u64 read_format = event->attr.read_format;
6735
6736 /*
6737 * compute total_time_enabled, total_time_running
6738 * based on snapshot values taken when the event
6739 * was last scheduled in.
6740 *
6741 * we cannot simply called update_context_time()
6742 * because of locking issue as we are called in
6743 * NMI context
6744 */
Eric B Munsonc4794292011-06-23 16:34:38 -04006745 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006746 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02006747
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006748 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02006749 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006750 else
Stephane Eranianeed01522010-10-26 16:08:01 +02006751 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006752}
6753
Kan Liangbbfd5e42020-01-27 08:53:54 -08006754static inline bool perf_sample_save_hw_index(struct perf_event *event)
6755{
6756 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
6757}
6758
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006759void perf_output_sample(struct perf_output_handle *handle,
6760 struct perf_event_header *header,
6761 struct perf_sample_data *data,
6762 struct perf_event *event)
6763{
6764 u64 sample_type = data->type;
6765
6766 perf_output_put(handle, *header);
6767
Adrian Hunterff3d5272013-08-27 11:23:07 +03006768 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6769 perf_output_put(handle, data->id);
6770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006771 if (sample_type & PERF_SAMPLE_IP)
6772 perf_output_put(handle, data->ip);
6773
6774 if (sample_type & PERF_SAMPLE_TID)
6775 perf_output_put(handle, data->tid_entry);
6776
6777 if (sample_type & PERF_SAMPLE_TIME)
6778 perf_output_put(handle, data->time);
6779
6780 if (sample_type & PERF_SAMPLE_ADDR)
6781 perf_output_put(handle, data->addr);
6782
6783 if (sample_type & PERF_SAMPLE_ID)
6784 perf_output_put(handle, data->id);
6785
6786 if (sample_type & PERF_SAMPLE_STREAM_ID)
6787 perf_output_put(handle, data->stream_id);
6788
6789 if (sample_type & PERF_SAMPLE_CPU)
6790 perf_output_put(handle, data->cpu_entry);
6791
6792 if (sample_type & PERF_SAMPLE_PERIOD)
6793 perf_output_put(handle, data->period);
6794
6795 if (sample_type & PERF_SAMPLE_READ)
6796 perf_output_read(handle, event);
6797
6798 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Jiri Olsa99e818c2018-01-07 17:03:50 +01006799 int size = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006800
Jiri Olsa99e818c2018-01-07 17:03:50 +01006801 size += data->callchain->nr;
6802 size *= sizeof(u64);
6803 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006804 }
6805
6806 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006807 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07006808
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006809 if (raw) {
6810 struct perf_raw_frag *frag = &raw->frag;
6811
6812 perf_output_put(handle, raw->size);
6813 do {
6814 if (frag->copy) {
6815 __output_custom(handle, frag->copy,
6816 frag->data, frag->size);
6817 } else {
6818 __output_copy(handle, frag->data,
6819 frag->size);
6820 }
6821 if (perf_raw_frag_last(frag))
6822 break;
6823 frag = frag->next;
6824 } while (1);
6825 if (frag->pad)
6826 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006827 } else {
6828 struct {
6829 u32 size;
6830 u32 data;
6831 } raw = {
6832 .size = sizeof(u32),
6833 .data = 0,
6834 };
6835 perf_output_put(handle, raw);
6836 }
6837 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006838
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006839 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6840 if (data->br_stack) {
6841 size_t size;
6842
6843 size = data->br_stack->nr
6844 * sizeof(struct perf_branch_entry);
6845
6846 perf_output_put(handle, data->br_stack->nr);
Kan Liangbbfd5e42020-01-27 08:53:54 -08006847 if (perf_sample_save_hw_index(event))
6848 perf_output_put(handle, data->br_stack->hw_idx);
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006849 perf_output_copy(handle, data->br_stack->entries, size);
6850 } else {
6851 /*
6852 * we always store at least the value of nr
6853 */
6854 u64 nr = 0;
6855 perf_output_put(handle, nr);
6856 }
6857 }
Jiri Olsa40189942012-08-07 15:20:37 +02006858
6859 if (sample_type & PERF_SAMPLE_REGS_USER) {
6860 u64 abi = data->regs_user.abi;
6861
6862 /*
6863 * If there are no regs to dump, notice it through
6864 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6865 */
6866 perf_output_put(handle, abi);
6867
6868 if (abi) {
6869 u64 mask = event->attr.sample_regs_user;
6870 perf_output_sample_regs(handle,
6871 data->regs_user.regs,
6872 mask);
6873 }
6874 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006875
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006876 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02006877 perf_output_sample_ustack(handle,
6878 data->stack_user_size,
6879 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006880 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01006881
6882 if (sample_type & PERF_SAMPLE_WEIGHT)
6883 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01006884
6885 if (sample_type & PERF_SAMPLE_DATA_SRC)
6886 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006887
Andi Kleenfdfbbd02013-09-20 07:40:39 -07006888 if (sample_type & PERF_SAMPLE_TRANSACTION)
6889 perf_output_put(handle, data->txn);
6890
Stephane Eranian60e23642014-09-24 13:48:37 +02006891 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6892 u64 abi = data->regs_intr.abi;
6893 /*
6894 * If there are no regs to dump, notice it through
6895 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6896 */
6897 perf_output_put(handle, abi);
6898
6899 if (abi) {
6900 u64 mask = event->attr.sample_regs_intr;
6901
6902 perf_output_sample_regs(handle,
6903 data->regs_intr.regs,
6904 mask);
6905 }
6906 }
6907
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006908 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
6909 perf_output_put(handle, data->phys_addr);
6910
Namhyung Kim6546b192020-03-25 21:45:29 +09006911 if (sample_type & PERF_SAMPLE_CGROUP)
6912 perf_output_put(handle, data->cgroup);
6913
Alexander Shishkina4faf002019-10-25 17:08:33 +03006914 if (sample_type & PERF_SAMPLE_AUX) {
6915 perf_output_put(handle, data->aux_size);
6916
6917 if (data->aux_size)
6918 perf_aux_sample_output(event, handle, data);
6919 }
6920
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006921 if (!event->attr.watermark) {
6922 int wakeup_events = event->attr.wakeup_events;
6923
6924 if (wakeup_events) {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006925 struct perf_buffer *rb = handle->rb;
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006926 int events = local_inc_return(&rb->events);
6927
6928 if (events >= wakeup_events) {
6929 local_sub(wakeup_events, &rb->events);
6930 local_inc(&rb->wakeup);
6931 }
6932 }
6933 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006934}
6935
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006936static u64 perf_virt_to_phys(u64 virt)
6937{
6938 u64 phys_addr = 0;
6939 struct page *p = NULL;
6940
6941 if (!virt)
6942 return 0;
6943
6944 if (virt >= TASK_SIZE) {
6945 /* If it's vmalloc()d memory, leave phys_addr as 0 */
6946 if (virt_addr_valid((void *)(uintptr_t)virt) &&
6947 !(virt >= VMALLOC_START && virt < VMALLOC_END))
6948 phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt);
6949 } else {
6950 /*
6951 * Walking the pages tables for user address.
6952 * Interrupts are disabled, so it prevents any tear down
6953 * of the page tables.
Souptick Joarderdadbb612020-06-07 21:40:55 -07006954 * Try IRQ-safe get_user_page_fast_only first.
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006955 * If failed, leave phys_addr as 0.
6956 */
Jiri Olsad3296fb2020-04-07 16:14:27 +02006957 if (current->mm != NULL) {
6958 pagefault_disable();
Souptick Joarderdadbb612020-06-07 21:40:55 -07006959 if (get_user_page_fast_only(virt, 0, &p))
Jiri Olsad3296fb2020-04-07 16:14:27 +02006960 phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
6961 pagefault_enable();
6962 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006963
6964 if (p)
6965 put_page(p);
6966 }
6967
6968 return phys_addr;
6969}
6970
Jiri Olsa99e818c2018-01-07 17:03:50 +01006971static struct perf_callchain_entry __empty_callchain = { .nr = 0, };
6972
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02006973struct perf_callchain_entry *
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006974perf_callchain(struct perf_event *event, struct pt_regs *regs)
6975{
6976 bool kernel = !event->attr.exclude_callchain_kernel;
6977 bool user = !event->attr.exclude_callchain_user;
6978 /* Disallow cross-task user callchains. */
6979 bool crosstask = event->ctx->task && event->ctx->task != current;
6980 const u32 max_stack = event->attr.sample_max_stack;
Jiri Olsa99e818c2018-01-07 17:03:50 +01006981 struct perf_callchain_entry *callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006982
6983 if (!kernel && !user)
Jiri Olsa99e818c2018-01-07 17:03:50 +01006984 return &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006985
Jiri Olsa99e818c2018-01-07 17:03:50 +01006986 callchain = get_perf_callchain(regs, 0, kernel, user,
6987 max_stack, crosstask, true);
6988 return callchain ?: &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006989}
6990
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006991void perf_prepare_sample(struct perf_event_header *header,
6992 struct perf_sample_data *data,
6993 struct perf_event *event,
6994 struct pt_regs *regs)
6995{
6996 u64 sample_type = event->attr.sample_type;
6997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006998 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006999 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007000
7001 header->misc = 0;
7002 header->misc |= perf_misc_flags(regs);
7003
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007004 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007005
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007006 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007007 data->ip = perf_instruction_pointer(regs);
7008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007009 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
7010 int size = 1;
7011
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02007012 if (!(sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
7013 data->callchain = perf_callchain(event, regs);
7014
Jiri Olsa99e818c2018-01-07 17:03:50 +01007015 size += data->callchain->nr;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007016
7017 header->size += size * sizeof(u64);
7018 }
7019
7020 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007021 struct perf_raw_record *raw = data->raw;
7022 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007023
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007024 if (raw) {
7025 struct perf_raw_frag *frag = &raw->frag;
7026 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007027
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007028 do {
7029 sum += frag->size;
7030 if (perf_raw_frag_last(frag))
7031 break;
7032 frag = frag->next;
7033 } while (1);
7034
7035 size = round_up(sum + sizeof(u32), sizeof(u64));
7036 raw->size = size - sizeof(u32);
7037 frag->pad = raw->size - sum;
7038 } else {
7039 size = sizeof(u64);
7040 }
7041
7042 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007043 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007044
7045 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
7046 int size = sizeof(u64); /* nr */
7047 if (data->br_stack) {
Kan Liangbbfd5e42020-01-27 08:53:54 -08007048 if (perf_sample_save_hw_index(event))
7049 size += sizeof(u64);
7050
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007051 size += data->br_stack->nr
7052 * sizeof(struct perf_branch_entry);
7053 }
7054 header->size += size;
7055 }
Jiri Olsa40189942012-08-07 15:20:37 +02007056
Peter Zijlstra25657112014-09-24 13:48:42 +02007057 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08007058 perf_sample_regs_user(&data->regs_user, regs,
7059 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02007060
Jiri Olsa40189942012-08-07 15:20:37 +02007061 if (sample_type & PERF_SAMPLE_REGS_USER) {
7062 /* regs dump ABI info */
7063 int size = sizeof(u64);
7064
Jiri Olsa40189942012-08-07 15:20:37 +02007065 if (data->regs_user.regs) {
7066 u64 mask = event->attr.sample_regs_user;
7067 size += hweight64(mask) * sizeof(u64);
7068 }
7069
7070 header->size += size;
7071 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02007072
7073 if (sample_type & PERF_SAMPLE_STACK_USER) {
7074 /*
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03007075 * Either we need PERF_SAMPLE_STACK_USER bit to be always
Jiri Olsac5ebced2012-08-07 15:20:40 +02007076 * processed as the last one or have additional check added
7077 * in case new sample type is added, because we could eat
7078 * up the rest of the sample size.
7079 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02007080 u16 stack_size = event->attr.sample_stack_user;
7081 u16 size = sizeof(u64);
7082
Jiri Olsac5ebced2012-08-07 15:20:40 +02007083 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02007084 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007085
7086 /*
7087 * If there is something to dump, add space for the dump
7088 * itself and for the field that tells the dynamic size,
7089 * which is how many have been actually dumped.
7090 */
7091 if (stack_size)
7092 size += sizeof(u64) + stack_size;
7093
7094 data->stack_user_size = stack_size;
7095 header->size += size;
7096 }
Stephane Eranian60e23642014-09-24 13:48:37 +02007097
7098 if (sample_type & PERF_SAMPLE_REGS_INTR) {
7099 /* regs dump ABI info */
7100 int size = sizeof(u64);
7101
7102 perf_sample_regs_intr(&data->regs_intr, regs);
7103
7104 if (data->regs_intr.regs) {
7105 u64 mask = event->attr.sample_regs_intr;
7106
7107 size += hweight64(mask) * sizeof(u64);
7108 }
7109
7110 header->size += size;
7111 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04007112
7113 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
7114 data->phys_addr = perf_virt_to_phys(data->addr);
Alexander Shishkina4faf002019-10-25 17:08:33 +03007115
Namhyung Kim6546b192020-03-25 21:45:29 +09007116#ifdef CONFIG_CGROUP_PERF
7117 if (sample_type & PERF_SAMPLE_CGROUP) {
7118 struct cgroup *cgrp;
7119
7120 /* protected by RCU */
7121 cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup;
7122 data->cgroup = cgroup_id(cgrp);
7123 }
7124#endif
7125
Alexander Shishkina4faf002019-10-25 17:08:33 +03007126 if (sample_type & PERF_SAMPLE_AUX) {
7127 u64 size;
7128
7129 header->size += sizeof(u64); /* size */
7130
7131 /*
7132 * Given the 16bit nature of header::size, an AUX sample can
7133 * easily overflow it, what with all the preceding sample bits.
7134 * Make sure this doesn't happen by using up to U16_MAX bytes
7135 * per sample in total (rounded down to 8 byte boundary).
7136 */
7137 size = min_t(size_t, U16_MAX - header->size,
7138 event->attr.aux_sample_size);
7139 size = rounddown(size, 8);
7140 size = perf_prepare_sample_aux(event, data, size);
7141
7142 WARN_ON_ONCE(size + header->size > U16_MAX);
7143 header->size += size;
7144 }
7145 /*
7146 * If you're adding more sample types here, you likely need to do
7147 * something about the overflowing header::size, like repurpose the
7148 * lowest 3 bits of size, which should be always zero at the moment.
7149 * This raises a more important question, do we really need 512k sized
7150 * samples and why, so good argumentation is in order for whatever you
7151 * do here next.
7152 */
7153 WARN_ON_ONCE(header->size & 7);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154}
7155
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007156static __always_inline int
Wang Nan9ecda412016-04-05 14:11:18 +00007157__perf_event_output(struct perf_event *event,
7158 struct perf_sample_data *data,
7159 struct pt_regs *regs,
7160 int (*output_begin)(struct perf_output_handle *,
7161 struct perf_event *,
7162 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007163{
7164 struct perf_output_handle handle;
7165 struct perf_event_header header;
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007166 int err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007167
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007168 /* protect the callchain buffers */
7169 rcu_read_lock();
7170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007171 perf_prepare_sample(&header, data, event, regs);
7172
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007173 err = output_begin(&handle, event, header.size);
7174 if (err)
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007175 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007176
7177 perf_output_sample(&handle, &header, data, event);
7178
7179 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007180
7181exit:
7182 rcu_read_unlock();
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007183 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007184}
7185
Wang Nan9ecda412016-04-05 14:11:18 +00007186void
7187perf_event_output_forward(struct perf_event *event,
7188 struct perf_sample_data *data,
7189 struct pt_regs *regs)
7190{
7191 __perf_event_output(event, data, regs, perf_output_begin_forward);
7192}
7193
7194void
7195perf_event_output_backward(struct perf_event *event,
7196 struct perf_sample_data *data,
7197 struct pt_regs *regs)
7198{
7199 __perf_event_output(event, data, regs, perf_output_begin_backward);
7200}
7201
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007202int
Wang Nan9ecda412016-04-05 14:11:18 +00007203perf_event_output(struct perf_event *event,
7204 struct perf_sample_data *data,
7205 struct pt_regs *regs)
7206{
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007207 return __perf_event_output(event, data, regs, perf_output_begin);
Wang Nan9ecda412016-04-05 14:11:18 +00007208}
7209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007210/*
7211 * read event_id
7212 */
7213
7214struct perf_read_event {
7215 struct perf_event_header header;
7216
7217 u32 pid;
7218 u32 tid;
7219};
7220
7221static void
7222perf_event_read_event(struct perf_event *event,
7223 struct task_struct *task)
7224{
7225 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007226 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007227 struct perf_read_event read_event = {
7228 .header = {
7229 .type = PERF_RECORD_READ,
7230 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007231 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007232 },
7233 .pid = perf_event_pid(event, task),
7234 .tid = perf_event_tid(event, task),
7235 };
7236 int ret;
7237
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007238 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007239 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007240 if (ret)
7241 return;
7242
7243 perf_output_put(&handle, read_event);
7244 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007245 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007246
7247 perf_output_end(&handle);
7248}
7249
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007250typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007251
7252static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007253perf_iterate_ctx(struct perf_event_context *ctx,
7254 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007255 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02007256{
7257 struct perf_event *event;
7258
7259 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007260 if (!all) {
7261 if (event->state < PERF_EVENT_STATE_INACTIVE)
7262 continue;
7263 if (!event_filter_match(event))
7264 continue;
7265 }
7266
Jiri Olsa67516842013-07-09 18:56:31 +02007267 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007268 }
7269}
7270
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007271static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07007272{
7273 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
7274 struct perf_event *event;
7275
7276 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02007277 /*
7278 * Skip events that are not fully formed yet; ensure that
7279 * if we observe event->ctx, both event and ctx will be
7280 * complete enough. See perf_install_in_context().
7281 */
7282 if (!smp_load_acquire(&event->ctx))
7283 continue;
7284
Kan Liangf2fb6be2016-03-23 11:24:37 -07007285 if (event->state < PERF_EVENT_STATE_INACTIVE)
7286 continue;
7287 if (!event_filter_match(event))
7288 continue;
7289 output(event, data);
7290 }
7291}
7292
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007293/*
7294 * Iterate all events that need to receive side-band events.
7295 *
7296 * For new callers; ensure that account_pmu_sb_event() includes
7297 * your event, otherwise it might not get delivered.
7298 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007299static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007300perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007301 struct perf_event_context *task_ctx)
7302{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007303 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02007304 int ctxn;
7305
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007306 rcu_read_lock();
7307 preempt_disable();
7308
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007309 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007310 * If we have task_ctx != NULL we only notify the task context itself.
7311 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007312 * context.
7313 */
7314 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007315 perf_iterate_ctx(task_ctx, output, data, false);
7316 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007317 }
7318
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007319 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07007320
7321 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02007322 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
7323 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007324 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007325 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007326done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07007327 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02007328 rcu_read_unlock();
7329}
7330
Alexander Shishkin375637b2016-04-27 18:44:46 +03007331/*
7332 * Clear all file-based filters at exec, they'll have to be
7333 * re-instated when/if these objects are mmapped again.
7334 */
7335static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
7336{
7337 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7338 struct perf_addr_filter *filter;
7339 unsigned int restart = 0, count = 0;
7340 unsigned long flags;
7341
7342 if (!has_addr_filter(event))
7343 return;
7344
7345 raw_spin_lock_irqsave(&ifh->lock, flags);
7346 list_for_each_entry(filter, &ifh->list, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07007347 if (filter->path.dentry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007348 event->addr_filter_ranges[count].start = 0;
7349 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007350 restart++;
7351 }
7352
7353 count++;
7354 }
7355
7356 if (restart)
7357 event->addr_filters_gen++;
7358 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7359
7360 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03007361 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007362}
7363
7364void perf_event_exec(void)
7365{
7366 struct perf_event_context *ctx;
7367 int ctxn;
7368
7369 rcu_read_lock();
7370 for_each_task_context_nr(ctxn) {
7371 ctx = current->perf_event_ctxp[ctxn];
7372 if (!ctx)
7373 continue;
7374
7375 perf_event_enable_on_exec(ctxn);
7376
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007377 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03007378 true);
7379 }
7380 rcu_read_unlock();
7381}
7382
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007383struct remote_output {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05007384 struct perf_buffer *rb;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007385 int err;
7386};
7387
7388static void __perf_event_output_stop(struct perf_event *event, void *data)
7389{
7390 struct perf_event *parent = event->parent;
7391 struct remote_output *ro = data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05007392 struct perf_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007393 struct stop_event_data sd = {
7394 .event = event,
7395 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007396
7397 if (!has_aux(event))
7398 return;
7399
7400 if (!parent)
7401 parent = event;
7402
7403 /*
7404 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03007405 * ring-buffer, but it will be the child that's actually using it.
7406 *
7407 * We are using event::rb to determine if the event should be stopped,
7408 * however this may race with ring_buffer_attach() (through set_output),
7409 * which will make us skip the event that actually needs to be stopped.
7410 * So ring_buffer_attach() has to stop an aux event before re-assigning
7411 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007412 */
7413 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007414 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007415}
7416
7417static int __perf_pmu_output_stop(void *info)
7418{
7419 struct perf_event *event = info;
Alexander Shishkinf3a519e2019-10-22 10:39:40 +03007420 struct pmu *pmu = event->ctx->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01007421 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007422 struct remote_output ro = {
7423 .rb = event->rb,
7424 };
7425
7426 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007427 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007428 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007429 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007430 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007431 rcu_read_unlock();
7432
7433 return ro.err;
7434}
7435
7436static void perf_pmu_output_stop(struct perf_event *event)
7437{
7438 struct perf_event *iter;
7439 int err, cpu;
7440
7441restart:
7442 rcu_read_lock();
7443 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
7444 /*
7445 * For per-CPU events, we need to make sure that neither they
7446 * nor their children are running; for cpu==-1 events it's
7447 * sufficient to stop the event itself if it's active, since
7448 * it can't have children.
7449 */
7450 cpu = iter->cpu;
7451 if (cpu == -1)
7452 cpu = READ_ONCE(iter->oncpu);
7453
7454 if (cpu == -1)
7455 continue;
7456
7457 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
7458 if (err == -EAGAIN) {
7459 rcu_read_unlock();
7460 goto restart;
7461 }
7462 }
7463 rcu_read_unlock();
7464}
7465
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007466/*
7467 * task tracking -- fork/exit
7468 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02007469 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007470 */
7471
7472struct perf_task_event {
7473 struct task_struct *task;
7474 struct perf_event_context *task_ctx;
7475
7476 struct {
7477 struct perf_event_header header;
7478
7479 u32 pid;
7480 u32 ppid;
7481 u32 tid;
7482 u32 ptid;
7483 u64 time;
7484 } event_id;
7485};
7486
Jiri Olsa67516842013-07-09 18:56:31 +02007487static int perf_event_task_match(struct perf_event *event)
7488{
Stephane Eranian13d7a242013-08-21 12:10:24 +02007489 return event->attr.comm || event->attr.mmap ||
7490 event->attr.mmap2 || event->attr.mmap_data ||
7491 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02007492}
7493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007494static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007495 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007496{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007497 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007498 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007499 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007500 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007501 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01007502
Jiri Olsa67516842013-07-09 18:56:31 +02007503 if (!perf_event_task_match(event))
7504 return;
7505
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007506 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007507
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007508 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007509 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02007510 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007511 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007512
7513 task_event->event_id.pid = perf_event_pid(event, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007514 task_event->event_id.tid = perf_event_tid(event, task);
Ian Rogersf3bed552020-04-17 11:28:42 -07007515
7516 if (task_event->event_id.header.type == PERF_RECORD_EXIT) {
7517 task_event->event_id.ppid = perf_event_pid(event,
7518 task->real_parent);
7519 task_event->event_id.ptid = perf_event_pid(event,
7520 task->real_parent);
7521 } else { /* PERF_RECORD_FORK */
7522 task_event->event_id.ppid = perf_event_pid(event, current);
7523 task_event->event_id.ptid = perf_event_tid(event, current);
7524 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007525
Peter Zijlstra34f43922015-02-20 14:05:38 +01007526 task_event->event_id.time = perf_event_clock(event);
7527
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007528 perf_output_put(&handle, task_event->event_id);
7529
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007530 perf_event__output_id_sample(event, &handle, &sample);
7531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007532 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007533out:
7534 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007535}
7536
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007537static void perf_event_task(struct task_struct *task,
7538 struct perf_event_context *task_ctx,
7539 int new)
7540{
7541 struct perf_task_event task_event;
7542
7543 if (!atomic_read(&nr_comm_events) &&
7544 !atomic_read(&nr_mmap_events) &&
7545 !atomic_read(&nr_task_events))
7546 return;
7547
7548 task_event = (struct perf_task_event){
7549 .task = task,
7550 .task_ctx = task_ctx,
7551 .event_id = {
7552 .header = {
7553 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
7554 .misc = 0,
7555 .size = sizeof(task_event.event_id),
7556 },
7557 /* .pid */
7558 /* .ppid */
7559 /* .tid */
7560 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01007561 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007562 },
7563 };
7564
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007565 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007566 &task_event,
7567 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007568}
7569
7570void perf_event_fork(struct task_struct *task)
7571{
7572 perf_event_task(task, NULL, 1);
Hari Bathinie4222672017-03-08 02:11:36 +05307573 perf_event_namespaces(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007574}
7575
7576/*
7577 * comm tracking
7578 */
7579
7580struct perf_comm_event {
7581 struct task_struct *task;
7582 char *comm;
7583 int comm_size;
7584
7585 struct {
7586 struct perf_event_header header;
7587
7588 u32 pid;
7589 u32 tid;
7590 } event_id;
7591};
7592
Jiri Olsa67516842013-07-09 18:56:31 +02007593static int perf_event_comm_match(struct perf_event *event)
7594{
7595 return event->attr.comm;
7596}
7597
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007598static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007599 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007600{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007601 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007602 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007603 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007604 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007605 int ret;
7606
Jiri Olsa67516842013-07-09 18:56:31 +02007607 if (!perf_event_comm_match(event))
7608 return;
7609
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007610 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
7611 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007612 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007613
7614 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007615 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007616
7617 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
7618 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
7619
7620 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02007621 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007622 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007623
7624 perf_event__output_id_sample(event, &handle, &sample);
7625
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007626 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007627out:
7628 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007629}
7630
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007631static void perf_event_comm_event(struct perf_comm_event *comm_event)
7632{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007633 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007634 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007635
7636 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01007637 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007638 size = ALIGN(strlen(comm)+1, sizeof(u64));
7639
7640 comm_event->comm = comm;
7641 comm_event->comm_size = size;
7642
7643 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007644
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007645 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007646 comm_event,
7647 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007648}
7649
Adrian Hunter82b89772014-05-28 11:45:04 +03007650void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007651{
7652 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007653
7654 if (!atomic_read(&nr_comm_events))
7655 return;
7656
7657 comm_event = (struct perf_comm_event){
7658 .task = task,
7659 /* .comm */
7660 /* .comm_size */
7661 .event_id = {
7662 .header = {
7663 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03007664 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007665 /* .size */
7666 },
7667 /* .pid */
7668 /* .tid */
7669 },
7670 };
7671
7672 perf_event_comm_event(&comm_event);
7673}
7674
7675/*
Hari Bathinie4222672017-03-08 02:11:36 +05307676 * namespaces tracking
7677 */
7678
7679struct perf_namespaces_event {
7680 struct task_struct *task;
7681
7682 struct {
7683 struct perf_event_header header;
7684
7685 u32 pid;
7686 u32 tid;
7687 u64 nr_namespaces;
7688 struct perf_ns_link_info link_info[NR_NAMESPACES];
7689 } event_id;
7690};
7691
7692static int perf_event_namespaces_match(struct perf_event *event)
7693{
7694 return event->attr.namespaces;
7695}
7696
7697static void perf_event_namespaces_output(struct perf_event *event,
7698 void *data)
7699{
7700 struct perf_namespaces_event *namespaces_event = data;
7701 struct perf_output_handle handle;
7702 struct perf_sample_data sample;
Jiri Olsa34900ec2017-08-09 18:14:06 +02007703 u16 header_size = namespaces_event->event_id.header.size;
Hari Bathinie4222672017-03-08 02:11:36 +05307704 int ret;
7705
7706 if (!perf_event_namespaces_match(event))
7707 return;
7708
7709 perf_event_header__init_id(&namespaces_event->event_id.header,
7710 &sample, event);
7711 ret = perf_output_begin(&handle, event,
7712 namespaces_event->event_id.header.size);
7713 if (ret)
Jiri Olsa34900ec2017-08-09 18:14:06 +02007714 goto out;
Hari Bathinie4222672017-03-08 02:11:36 +05307715
7716 namespaces_event->event_id.pid = perf_event_pid(event,
7717 namespaces_event->task);
7718 namespaces_event->event_id.tid = perf_event_tid(event,
7719 namespaces_event->task);
7720
7721 perf_output_put(&handle, namespaces_event->event_id);
7722
7723 perf_event__output_id_sample(event, &handle, &sample);
7724
7725 perf_output_end(&handle);
Jiri Olsa34900ec2017-08-09 18:14:06 +02007726out:
7727 namespaces_event->event_id.header.size = header_size;
Hari Bathinie4222672017-03-08 02:11:36 +05307728}
7729
7730static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info,
7731 struct task_struct *task,
7732 const struct proc_ns_operations *ns_ops)
7733{
7734 struct path ns_path;
7735 struct inode *ns_inode;
Aleksa Saraice623f82019-12-07 01:13:27 +11007736 int error;
Hari Bathinie4222672017-03-08 02:11:36 +05307737
7738 error = ns_get_path(&ns_path, task, ns_ops);
7739 if (!error) {
7740 ns_inode = ns_path.dentry->d_inode;
7741 ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev);
7742 ns_link_info->ino = ns_inode->i_ino;
Vasily Averin0e18dd12017-11-15 08:47:02 +03007743 path_put(&ns_path);
Hari Bathinie4222672017-03-08 02:11:36 +05307744 }
7745}
7746
7747void perf_event_namespaces(struct task_struct *task)
7748{
7749 struct perf_namespaces_event namespaces_event;
7750 struct perf_ns_link_info *ns_link_info;
7751
7752 if (!atomic_read(&nr_namespaces_events))
7753 return;
7754
7755 namespaces_event = (struct perf_namespaces_event){
7756 .task = task,
7757 .event_id = {
7758 .header = {
7759 .type = PERF_RECORD_NAMESPACES,
7760 .misc = 0,
7761 .size = sizeof(namespaces_event.event_id),
7762 },
7763 /* .pid */
7764 /* .tid */
7765 .nr_namespaces = NR_NAMESPACES,
7766 /* .link_info[NR_NAMESPACES] */
7767 },
7768 };
7769
7770 ns_link_info = namespaces_event.event_id.link_info;
7771
7772 perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX],
7773 task, &mntns_operations);
7774
7775#ifdef CONFIG_USER_NS
7776 perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX],
7777 task, &userns_operations);
7778#endif
7779#ifdef CONFIG_NET_NS
7780 perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX],
7781 task, &netns_operations);
7782#endif
7783#ifdef CONFIG_UTS_NS
7784 perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX],
7785 task, &utsns_operations);
7786#endif
7787#ifdef CONFIG_IPC_NS
7788 perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX],
7789 task, &ipcns_operations);
7790#endif
7791#ifdef CONFIG_PID_NS
7792 perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX],
7793 task, &pidns_operations);
7794#endif
7795#ifdef CONFIG_CGROUPS
7796 perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX],
7797 task, &cgroupns_operations);
7798#endif
7799
7800 perf_iterate_sb(perf_event_namespaces_output,
7801 &namespaces_event,
7802 NULL);
7803}
7804
7805/*
Namhyung Kim96aaab62020-03-25 21:45:28 +09007806 * cgroup tracking
7807 */
7808#ifdef CONFIG_CGROUP_PERF
7809
7810struct perf_cgroup_event {
7811 char *path;
7812 int path_size;
7813 struct {
7814 struct perf_event_header header;
7815 u64 id;
7816 char path[];
7817 } event_id;
7818};
7819
7820static int perf_event_cgroup_match(struct perf_event *event)
7821{
7822 return event->attr.cgroup;
7823}
7824
7825static void perf_event_cgroup_output(struct perf_event *event, void *data)
7826{
7827 struct perf_cgroup_event *cgroup_event = data;
7828 struct perf_output_handle handle;
7829 struct perf_sample_data sample;
7830 u16 header_size = cgroup_event->event_id.header.size;
7831 int ret;
7832
7833 if (!perf_event_cgroup_match(event))
7834 return;
7835
7836 perf_event_header__init_id(&cgroup_event->event_id.header,
7837 &sample, event);
7838 ret = perf_output_begin(&handle, event,
7839 cgroup_event->event_id.header.size);
7840 if (ret)
7841 goto out;
7842
7843 perf_output_put(&handle, cgroup_event->event_id);
7844 __output_copy(&handle, cgroup_event->path, cgroup_event->path_size);
7845
7846 perf_event__output_id_sample(event, &handle, &sample);
7847
7848 perf_output_end(&handle);
7849out:
7850 cgroup_event->event_id.header.size = header_size;
7851}
7852
7853static void perf_event_cgroup(struct cgroup *cgrp)
7854{
7855 struct perf_cgroup_event cgroup_event;
7856 char path_enomem[16] = "//enomem";
7857 char *pathname;
7858 size_t size;
7859
7860 if (!atomic_read(&nr_cgroup_events))
7861 return;
7862
7863 cgroup_event = (struct perf_cgroup_event){
7864 .event_id = {
7865 .header = {
7866 .type = PERF_RECORD_CGROUP,
7867 .misc = 0,
7868 .size = sizeof(cgroup_event.event_id),
7869 },
7870 .id = cgroup_id(cgrp),
7871 },
7872 };
7873
7874 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
7875 if (pathname == NULL) {
7876 cgroup_event.path = path_enomem;
7877 } else {
7878 /* just to be sure to have enough space for alignment */
7879 cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64));
7880 cgroup_event.path = pathname;
7881 }
7882
7883 /*
7884 * Since our buffer works in 8 byte units we need to align our string
7885 * size to a multiple of 8. However, we must guarantee the tail end is
7886 * zero'd out to avoid leaking random bits to userspace.
7887 */
7888 size = strlen(cgroup_event.path) + 1;
7889 while (!IS_ALIGNED(size, sizeof(u64)))
7890 cgroup_event.path[size++] = '\0';
7891
7892 cgroup_event.event_id.header.size += size;
7893 cgroup_event.path_size = size;
7894
7895 perf_iterate_sb(perf_event_cgroup_output,
7896 &cgroup_event,
7897 NULL);
7898
7899 kfree(pathname);
7900}
7901
7902#endif
7903
7904/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007905 * mmap tracking
7906 */
7907
7908struct perf_mmap_event {
7909 struct vm_area_struct *vma;
7910
7911 const char *file_name;
7912 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007913 int maj, min;
7914 u64 ino;
7915 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007916 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007917
7918 struct {
7919 struct perf_event_header header;
7920
7921 u32 pid;
7922 u32 tid;
7923 u64 start;
7924 u64 len;
7925 u64 pgoff;
7926 } event_id;
7927};
7928
Jiri Olsa67516842013-07-09 18:56:31 +02007929static int perf_event_mmap_match(struct perf_event *event,
7930 void *data)
7931{
7932 struct perf_mmap_event *mmap_event = data;
7933 struct vm_area_struct *vma = mmap_event->vma;
7934 int executable = vma->vm_flags & VM_EXEC;
7935
7936 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02007937 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02007938}
7939
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007940static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007941 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007942{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007943 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007944 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007945 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007946 int size = mmap_event->event_id.header.size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007947 u32 type = mmap_event->event_id.header.type;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007948 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007949
Jiri Olsa67516842013-07-09 18:56:31 +02007950 if (!perf_event_mmap_match(event, data))
7951 return;
7952
Stephane Eranian13d7a242013-08-21 12:10:24 +02007953 if (event->attr.mmap2) {
7954 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
7955 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
7956 mmap_event->event_id.header.size += sizeof(mmap_event->min);
7957 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03007958 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007959 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
7960 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007961 }
7962
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007963 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
7964 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007965 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007966 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007967 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007968
7969 mmap_event->event_id.pid = perf_event_pid(event, current);
7970 mmap_event->event_id.tid = perf_event_tid(event, current);
7971
7972 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007973
7974 if (event->attr.mmap2) {
7975 perf_output_put(&handle, mmap_event->maj);
7976 perf_output_put(&handle, mmap_event->min);
7977 perf_output_put(&handle, mmap_event->ino);
7978 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007979 perf_output_put(&handle, mmap_event->prot);
7980 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007981 }
7982
Frederic Weisbecker76369132011-05-19 19:55:04 +02007983 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007984 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007985
7986 perf_event__output_id_sample(event, &handle, &sample);
7987
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007988 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007989out:
7990 mmap_event->event_id.header.size = size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007991 mmap_event->event_id.header.type = type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007992}
7993
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007994static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
7995{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007996 struct vm_area_struct *vma = mmap_event->vma;
7997 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007998 int maj = 0, min = 0;
7999 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008000 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008001 unsigned int size;
8002 char tmp[16];
8003 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008004 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008005
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01008006 if (vma->vm_flags & VM_READ)
8007 prot |= PROT_READ;
8008 if (vma->vm_flags & VM_WRITE)
8009 prot |= PROT_WRITE;
8010 if (vma->vm_flags & VM_EXEC)
8011 prot |= PROT_EXEC;
8012
8013 if (vma->vm_flags & VM_MAYSHARE)
8014 flags = MAP_SHARED;
8015 else
8016 flags = MAP_PRIVATE;
8017
8018 if (vma->vm_flags & VM_DENYWRITE)
8019 flags |= MAP_DENYWRITE;
8020 if (vma->vm_flags & VM_MAYEXEC)
8021 flags |= MAP_EXECUTABLE;
8022 if (vma->vm_flags & VM_LOCKED)
8023 flags |= MAP_LOCKED;
Anshuman Khandual03911132020-04-06 20:03:51 -07008024 if (is_vm_hugetlb_page(vma))
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01008025 flags |= MAP_HUGETLB;
8026
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008027 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02008028 struct inode *inode;
8029 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02008030
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008031 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008032 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008033 name = "//enomem";
8034 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008035 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008036 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02008037 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008038 * need to add enough zero bytes after the string to handle
8039 * the 64bit alignment we do later.
8040 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02008041 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008042 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008043 name = "//toolong";
8044 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008045 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02008046 inode = file_inode(vma->vm_file);
8047 dev = inode->i_sb->s_dev;
8048 ino = inode->i_ino;
8049 gen = inode->i_generation;
8050 maj = MAJOR(dev);
8051 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008053 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008054 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02008055 if (vma->vm_ops && vma->vm_ops->name) {
8056 name = (char *) vma->vm_ops->name(vma);
8057 if (name)
8058 goto cpy_name;
8059 }
8060
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008061 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008062 if (name)
8063 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008064
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02008065 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008066 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008067 name = "[heap]";
8068 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02008069 }
8070 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008071 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008072 name = "[stack]";
8073 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008074 }
8075
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008076 name = "//anon";
8077 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008078 }
8079
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008080cpy_name:
8081 strlcpy(tmp, name, sizeof(tmp));
8082 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008083got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008084 /*
8085 * Since our buffer works in 8 byte units we need to align our string
8086 * size to a multiple of 8. However, we must guarantee the tail end is
8087 * zero'd out to avoid leaking random bits to userspace.
8088 */
8089 size = strlen(name)+1;
8090 while (!IS_ALIGNED(size, sizeof(u64)))
8091 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008092
8093 mmap_event->file_name = name;
8094 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02008095 mmap_event->maj = maj;
8096 mmap_event->min = min;
8097 mmap_event->ino = ino;
8098 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008099 mmap_event->prot = prot;
8100 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008101
Stephane Eranian2fe85422013-01-24 16:10:39 +01008102 if (!(vma->vm_flags & VM_EXEC))
8103 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
8104
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008105 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
8106
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008107 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02008108 mmap_event,
8109 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008110
8111 kfree(buf);
8112}
8113
Alexander Shishkin375637b2016-04-27 18:44:46 +03008114/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03008115 * Check whether inode and address range match filter criteria.
8116 */
8117static bool perf_addr_filter_match(struct perf_addr_filter *filter,
8118 struct file *file, unsigned long offset,
8119 unsigned long size)
8120{
Mathieu Poirier7f635ff2018-07-16 17:13:51 -06008121 /* d_inode(NULL) won't be equal to any mapped user-space file */
8122 if (!filter->path.dentry)
8123 return false;
8124
Song Liu9511bce2018-04-17 23:29:07 -07008125 if (d_inode(filter->path.dentry) != file_inode(file))
Alexander Shishkin375637b2016-04-27 18:44:46 +03008126 return false;
8127
8128 if (filter->offset > offset + size)
8129 return false;
8130
8131 if (filter->offset + filter->size < offset)
8132 return false;
8133
8134 return true;
8135}
8136
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008137static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter,
8138 struct vm_area_struct *vma,
8139 struct perf_addr_filter_range *fr)
8140{
8141 unsigned long vma_size = vma->vm_end - vma->vm_start;
8142 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8143 struct file *file = vma->vm_file;
8144
8145 if (!perf_addr_filter_match(filter, file, off, vma_size))
8146 return false;
8147
8148 if (filter->offset < off) {
8149 fr->start = vma->vm_start;
8150 fr->size = min(vma_size, filter->size - (off - filter->offset));
8151 } else {
8152 fr->start = vma->vm_start + filter->offset - off;
8153 fr->size = min(vma->vm_end - fr->start, filter->size);
8154 }
8155
8156 return true;
8157}
8158
Alexander Shishkin375637b2016-04-27 18:44:46 +03008159static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
8160{
8161 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8162 struct vm_area_struct *vma = data;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008163 struct perf_addr_filter *filter;
8164 unsigned int restart = 0, count = 0;
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008165 unsigned long flags;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008166
8167 if (!has_addr_filter(event))
8168 return;
8169
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008170 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008171 return;
8172
8173 raw_spin_lock_irqsave(&ifh->lock, flags);
8174 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008175 if (perf_addr_filter_vma_adjust(filter, vma,
8176 &event->addr_filter_ranges[count]))
Alexander Shishkin375637b2016-04-27 18:44:46 +03008177 restart++;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008178
8179 count++;
8180 }
8181
8182 if (restart)
8183 event->addr_filters_gen++;
8184 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8185
8186 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03008187 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008188}
8189
8190/*
8191 * Adjust all task's events' filters to the new vma
8192 */
8193static void perf_addr_filters_adjust(struct vm_area_struct *vma)
8194{
8195 struct perf_event_context *ctx;
8196 int ctxn;
8197
Mathieu Poirier12b40a22016-07-18 10:43:06 -06008198 /*
8199 * Data tracing isn't supported yet and as such there is no need
8200 * to keep track of anything that isn't related to executable code:
8201 */
8202 if (!(vma->vm_flags & VM_EXEC))
8203 return;
8204
Alexander Shishkin375637b2016-04-27 18:44:46 +03008205 rcu_read_lock();
8206 for_each_task_context_nr(ctxn) {
8207 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
8208 if (!ctx)
8209 continue;
8210
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008211 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008212 }
8213 rcu_read_unlock();
8214}
8215
Eric B Munson3af9e852010-05-18 15:30:49 +01008216void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008217{
8218 struct perf_mmap_event mmap_event;
8219
8220 if (!atomic_read(&nr_mmap_events))
8221 return;
8222
8223 mmap_event = (struct perf_mmap_event){
8224 .vma = vma,
8225 /* .file_name */
8226 /* .file_size */
8227 .event_id = {
8228 .header = {
8229 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08008230 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008231 /* .size */
8232 },
8233 /* .pid */
8234 /* .tid */
8235 .start = vma->vm_start,
8236 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01008237 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008238 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02008239 /* .maj (attr_mmap2 only) */
8240 /* .min (attr_mmap2 only) */
8241 /* .ino (attr_mmap2 only) */
8242 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008243 /* .prot (attr_mmap2 only) */
8244 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008245 };
8246
Alexander Shishkin375637b2016-04-27 18:44:46 +03008247 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008248 perf_event_mmap_event(&mmap_event);
8249}
8250
Alexander Shishkin68db7e92015-01-14 14:18:15 +02008251void perf_event_aux_event(struct perf_event *event, unsigned long head,
8252 unsigned long size, u64 flags)
8253{
8254 struct perf_output_handle handle;
8255 struct perf_sample_data sample;
8256 struct perf_aux_event {
8257 struct perf_event_header header;
8258 u64 offset;
8259 u64 size;
8260 u64 flags;
8261 } rec = {
8262 .header = {
8263 .type = PERF_RECORD_AUX,
8264 .misc = 0,
8265 .size = sizeof(rec),
8266 },
8267 .offset = head,
8268 .size = size,
8269 .flags = flags,
8270 };
8271 int ret;
8272
8273 perf_event_header__init_id(&rec.header, &sample, event);
8274 ret = perf_output_begin(&handle, event, rec.header.size);
8275
8276 if (ret)
8277 return;
8278
8279 perf_output_put(&handle, rec);
8280 perf_event__output_id_sample(event, &handle, &sample);
8281
8282 perf_output_end(&handle);
8283}
8284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008285/*
Kan Liangf38b0db2015-05-10 15:13:14 -04008286 * Lost/dropped samples logging
8287 */
8288void perf_log_lost_samples(struct perf_event *event, u64 lost)
8289{
8290 struct perf_output_handle handle;
8291 struct perf_sample_data sample;
8292 int ret;
8293
8294 struct {
8295 struct perf_event_header header;
8296 u64 lost;
8297 } lost_samples_event = {
8298 .header = {
8299 .type = PERF_RECORD_LOST_SAMPLES,
8300 .misc = 0,
8301 .size = sizeof(lost_samples_event),
8302 },
8303 .lost = lost,
8304 };
8305
8306 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
8307
8308 ret = perf_output_begin(&handle, event,
8309 lost_samples_event.header.size);
8310 if (ret)
8311 return;
8312
8313 perf_output_put(&handle, lost_samples_event);
8314 perf_event__output_id_sample(event, &handle, &sample);
8315 perf_output_end(&handle);
8316}
8317
8318/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03008319 * context_switch tracking
8320 */
8321
8322struct perf_switch_event {
8323 struct task_struct *task;
8324 struct task_struct *next_prev;
8325
8326 struct {
8327 struct perf_event_header header;
8328 u32 next_prev_pid;
8329 u32 next_prev_tid;
8330 } event_id;
8331};
8332
8333static int perf_event_switch_match(struct perf_event *event)
8334{
8335 return event->attr.context_switch;
8336}
8337
8338static void perf_event_switch_output(struct perf_event *event, void *data)
8339{
8340 struct perf_switch_event *se = data;
8341 struct perf_output_handle handle;
8342 struct perf_sample_data sample;
8343 int ret;
8344
8345 if (!perf_event_switch_match(event))
8346 return;
8347
8348 /* Only CPU-wide events are allowed to see next/prev pid/tid */
8349 if (event->ctx->task) {
8350 se->event_id.header.type = PERF_RECORD_SWITCH;
8351 se->event_id.header.size = sizeof(se->event_id.header);
8352 } else {
8353 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
8354 se->event_id.header.size = sizeof(se->event_id);
8355 se->event_id.next_prev_pid =
8356 perf_event_pid(event, se->next_prev);
8357 se->event_id.next_prev_tid =
8358 perf_event_tid(event, se->next_prev);
8359 }
8360
8361 perf_event_header__init_id(&se->event_id.header, &sample, event);
8362
8363 ret = perf_output_begin(&handle, event, se->event_id.header.size);
8364 if (ret)
8365 return;
8366
8367 if (event->ctx->task)
8368 perf_output_put(&handle, se->event_id.header);
8369 else
8370 perf_output_put(&handle, se->event_id);
8371
8372 perf_event__output_id_sample(event, &handle, &sample);
8373
8374 perf_output_end(&handle);
8375}
8376
8377static void perf_event_switch(struct task_struct *task,
8378 struct task_struct *next_prev, bool sched_in)
8379{
8380 struct perf_switch_event switch_event;
8381
8382 /* N.B. caller checks nr_switch_events != 0 */
8383
8384 switch_event = (struct perf_switch_event){
8385 .task = task,
8386 .next_prev = next_prev,
8387 .event_id = {
8388 .header = {
8389 /* .type */
8390 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
8391 /* .size */
8392 },
8393 /* .next_prev_pid */
8394 /* .next_prev_tid */
8395 },
8396 };
8397
Alexey Budankov101592b2018-04-09 10:25:32 +03008398 if (!sched_in && task->state == TASK_RUNNING)
8399 switch_event.event_id.header.misc |=
8400 PERF_RECORD_MISC_SWITCH_OUT_PREEMPT;
8401
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008402 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03008403 &switch_event,
8404 NULL);
8405}
8406
8407/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008408 * IRQ throttle logging
8409 */
8410
8411static void perf_log_throttle(struct perf_event *event, int enable)
8412{
8413 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008414 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008415 int ret;
8416
8417 struct {
8418 struct perf_event_header header;
8419 u64 time;
8420 u64 id;
8421 u64 stream_id;
8422 } throttle_event = {
8423 .header = {
8424 .type = PERF_RECORD_THROTTLE,
8425 .misc = 0,
8426 .size = sizeof(throttle_event),
8427 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01008428 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008429 .id = primary_event_id(event),
8430 .stream_id = event->id,
8431 };
8432
8433 if (enable)
8434 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
8435
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008436 perf_event_header__init_id(&throttle_event.header, &sample, event);
8437
8438 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02008439 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008440 if (ret)
8441 return;
8442
8443 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008444 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008445 perf_output_end(&handle);
8446}
8447
Song Liu76193a92019-01-17 08:15:13 -08008448/*
8449 * ksymbol register/unregister tracking
8450 */
8451
8452struct perf_ksymbol_event {
8453 const char *name;
8454 int name_len;
8455 struct {
8456 struct perf_event_header header;
8457 u64 addr;
8458 u32 len;
8459 u16 ksym_type;
8460 u16 flags;
8461 } event_id;
8462};
8463
8464static int perf_event_ksymbol_match(struct perf_event *event)
8465{
8466 return event->attr.ksymbol;
8467}
8468
8469static void perf_event_ksymbol_output(struct perf_event *event, void *data)
8470{
8471 struct perf_ksymbol_event *ksymbol_event = data;
8472 struct perf_output_handle handle;
8473 struct perf_sample_data sample;
8474 int ret;
8475
8476 if (!perf_event_ksymbol_match(event))
8477 return;
8478
8479 perf_event_header__init_id(&ksymbol_event->event_id.header,
8480 &sample, event);
8481 ret = perf_output_begin(&handle, event,
8482 ksymbol_event->event_id.header.size);
8483 if (ret)
8484 return;
8485
8486 perf_output_put(&handle, ksymbol_event->event_id);
8487 __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len);
8488 perf_event__output_id_sample(event, &handle, &sample);
8489
8490 perf_output_end(&handle);
8491}
8492
8493void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister,
8494 const char *sym)
8495{
8496 struct perf_ksymbol_event ksymbol_event;
8497 char name[KSYM_NAME_LEN];
8498 u16 flags = 0;
8499 int name_len;
8500
8501 if (!atomic_read(&nr_ksymbol_events))
8502 return;
8503
8504 if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX ||
8505 ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN)
8506 goto err;
8507
8508 strlcpy(name, sym, KSYM_NAME_LEN);
8509 name_len = strlen(name) + 1;
8510 while (!IS_ALIGNED(name_len, sizeof(u64)))
8511 name[name_len++] = '\0';
8512 BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64));
8513
8514 if (unregister)
8515 flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER;
8516
8517 ksymbol_event = (struct perf_ksymbol_event){
8518 .name = name,
8519 .name_len = name_len,
8520 .event_id = {
8521 .header = {
8522 .type = PERF_RECORD_KSYMBOL,
8523 .size = sizeof(ksymbol_event.event_id) +
8524 name_len,
8525 },
8526 .addr = addr,
8527 .len = len,
8528 .ksym_type = ksym_type,
8529 .flags = flags,
8530 },
8531 };
8532
8533 perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL);
8534 return;
8535err:
8536 WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type);
8537}
8538
Song Liu6ee52e22019-01-17 08:15:15 -08008539/*
8540 * bpf program load/unload tracking
8541 */
8542
8543struct perf_bpf_event {
8544 struct bpf_prog *prog;
8545 struct {
8546 struct perf_event_header header;
8547 u16 type;
8548 u16 flags;
8549 u32 id;
8550 u8 tag[BPF_TAG_SIZE];
8551 } event_id;
8552};
8553
8554static int perf_event_bpf_match(struct perf_event *event)
8555{
8556 return event->attr.bpf_event;
8557}
8558
8559static void perf_event_bpf_output(struct perf_event *event, void *data)
8560{
8561 struct perf_bpf_event *bpf_event = data;
8562 struct perf_output_handle handle;
8563 struct perf_sample_data sample;
8564 int ret;
8565
8566 if (!perf_event_bpf_match(event))
8567 return;
8568
8569 perf_event_header__init_id(&bpf_event->event_id.header,
8570 &sample, event);
8571 ret = perf_output_begin(&handle, event,
8572 bpf_event->event_id.header.size);
8573 if (ret)
8574 return;
8575
8576 perf_output_put(&handle, bpf_event->event_id);
8577 perf_event__output_id_sample(event, &handle, &sample);
8578
8579 perf_output_end(&handle);
8580}
8581
8582static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
8583 enum perf_bpf_event_type type)
8584{
8585 bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
Song Liu6ee52e22019-01-17 08:15:15 -08008586 int i;
8587
8588 if (prog->aux->func_cnt == 0) {
Song Liu6ee52e22019-01-17 08:15:15 -08008589 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
8590 (u64)(unsigned long)prog->bpf_func,
Jiri Olsabfea9a82020-03-12 20:55:59 +01008591 prog->jited_len, unregister,
8592 prog->aux->ksym.name);
Song Liu6ee52e22019-01-17 08:15:15 -08008593 } else {
8594 for (i = 0; i < prog->aux->func_cnt; i++) {
8595 struct bpf_prog *subprog = prog->aux->func[i];
8596
Song Liu6ee52e22019-01-17 08:15:15 -08008597 perf_event_ksymbol(
8598 PERF_RECORD_KSYMBOL_TYPE_BPF,
8599 (u64)(unsigned long)subprog->bpf_func,
Jiri Olsabfea9a82020-03-12 20:55:59 +01008600 subprog->jited_len, unregister,
8601 prog->aux->ksym.name);
Song Liu6ee52e22019-01-17 08:15:15 -08008602 }
8603 }
8604}
8605
8606void perf_event_bpf_event(struct bpf_prog *prog,
8607 enum perf_bpf_event_type type,
8608 u16 flags)
8609{
8610 struct perf_bpf_event bpf_event;
8611
8612 if (type <= PERF_BPF_EVENT_UNKNOWN ||
8613 type >= PERF_BPF_EVENT_MAX)
8614 return;
8615
8616 switch (type) {
8617 case PERF_BPF_EVENT_PROG_LOAD:
8618 case PERF_BPF_EVENT_PROG_UNLOAD:
8619 if (atomic_read(&nr_ksymbol_events))
8620 perf_event_bpf_emit_ksymbols(prog, type);
8621 break;
8622 default:
8623 break;
8624 }
8625
8626 if (!atomic_read(&nr_bpf_events))
8627 return;
8628
8629 bpf_event = (struct perf_bpf_event){
8630 .prog = prog,
8631 .event_id = {
8632 .header = {
8633 .type = PERF_RECORD_BPF_EVENT,
8634 .size = sizeof(bpf_event.event_id),
8635 },
8636 .type = type,
8637 .flags = flags,
8638 .id = prog->aux->id,
8639 },
8640 };
8641
8642 BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64));
8643
8644 memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
8645 perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
8646}
8647
Adrian Huntere17d43b2020-05-12 15:19:08 +03008648struct perf_text_poke_event {
8649 const void *old_bytes;
8650 const void *new_bytes;
8651 size_t pad;
8652 u16 old_len;
8653 u16 new_len;
8654
8655 struct {
8656 struct perf_event_header header;
8657
8658 u64 addr;
8659 } event_id;
8660};
8661
8662static int perf_event_text_poke_match(struct perf_event *event)
8663{
8664 return event->attr.text_poke;
8665}
8666
8667static void perf_event_text_poke_output(struct perf_event *event, void *data)
8668{
8669 struct perf_text_poke_event *text_poke_event = data;
8670 struct perf_output_handle handle;
8671 struct perf_sample_data sample;
8672 u64 padding = 0;
8673 int ret;
8674
8675 if (!perf_event_text_poke_match(event))
8676 return;
8677
8678 perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event);
8679
8680 ret = perf_output_begin(&handle, event, text_poke_event->event_id.header.size);
8681 if (ret)
8682 return;
8683
8684 perf_output_put(&handle, text_poke_event->event_id);
8685 perf_output_put(&handle, text_poke_event->old_len);
8686 perf_output_put(&handle, text_poke_event->new_len);
8687
8688 __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len);
8689 __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len);
8690
8691 if (text_poke_event->pad)
8692 __output_copy(&handle, &padding, text_poke_event->pad);
8693
8694 perf_event__output_id_sample(event, &handle, &sample);
8695
8696 perf_output_end(&handle);
8697}
8698
8699void perf_event_text_poke(const void *addr, const void *old_bytes,
8700 size_t old_len, const void *new_bytes, size_t new_len)
8701{
8702 struct perf_text_poke_event text_poke_event;
8703 size_t tot, pad;
8704
8705 if (!atomic_read(&nr_text_poke_events))
8706 return;
8707
8708 tot = sizeof(text_poke_event.old_len) + old_len;
8709 tot += sizeof(text_poke_event.new_len) + new_len;
8710 pad = ALIGN(tot, sizeof(u64)) - tot;
8711
8712 text_poke_event = (struct perf_text_poke_event){
8713 .old_bytes = old_bytes,
8714 .new_bytes = new_bytes,
8715 .pad = pad,
8716 .old_len = old_len,
8717 .new_len = new_len,
8718 .event_id = {
8719 .header = {
8720 .type = PERF_RECORD_TEXT_POKE,
8721 .misc = PERF_RECORD_MISC_KERNEL,
8722 .size = sizeof(text_poke_event.event_id) + tot + pad,
8723 },
8724 .addr = (unsigned long)addr,
8725 },
8726 };
8727
8728 perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL);
8729}
8730
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008731void perf_event_itrace_started(struct perf_event *event)
8732{
8733 event->attach_state |= PERF_ATTACH_ITRACE;
8734}
8735
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008736static void perf_log_itrace_start(struct perf_event *event)
8737{
8738 struct perf_output_handle handle;
8739 struct perf_sample_data sample;
8740 struct perf_aux_event {
8741 struct perf_event_header header;
8742 u32 pid;
8743 u32 tid;
8744 } rec;
8745 int ret;
8746
8747 if (event->parent)
8748 event = event->parent;
8749
8750 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008751 event->attach_state & PERF_ATTACH_ITRACE)
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008752 return;
8753
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008754 rec.header.type = PERF_RECORD_ITRACE_START;
8755 rec.header.misc = 0;
8756 rec.header.size = sizeof(rec);
8757 rec.pid = perf_event_pid(event, current);
8758 rec.tid = perf_event_tid(event, current);
8759
8760 perf_event_header__init_id(&rec.header, &sample, event);
8761 ret = perf_output_begin(&handle, event, rec.header.size);
8762
8763 if (ret)
8764 return;
8765
8766 perf_output_put(&handle, rec);
8767 perf_event__output_id_sample(event, &handle, &sample);
8768
8769 perf_output_end(&handle);
8770}
8771
Jiri Olsa475113d2016-12-28 14:31:03 +01008772static int
8773__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008774{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008775 struct hw_perf_event *hwc = &event->hw;
8776 int ret = 0;
Jiri Olsa475113d2016-12-28 14:31:03 +01008777 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01008778
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008779 seq = __this_cpu_read(perf_throttled_seq);
8780 if (seq != hwc->interrupts_seq) {
8781 hwc->interrupts_seq = seq;
8782 hwc->interrupts = 1;
8783 } else {
8784 hwc->interrupts++;
8785 if (unlikely(throttle
8786 && hwc->interrupts >= max_samples_per_tick)) {
8787 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008788 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01008789 hwc->interrupts = MAX_INTERRUPTS;
8790 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008791 ret = 1;
8792 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008793 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008794
8795 if (event->attr.freq) {
8796 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01008797 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008798
Peter Zijlstraabd50712010-01-26 18:50:16 +01008799 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008800
Peter Zijlstraabd50712010-01-26 18:50:16 +01008801 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01008802 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008803 }
8804
Jiri Olsa475113d2016-12-28 14:31:03 +01008805 return ret;
8806}
8807
8808int perf_event_account_interrupt(struct perf_event *event)
8809{
8810 return __perf_event_account_interrupt(event, 1);
8811}
8812
8813/*
8814 * Generic event overflow handling, sampling.
8815 */
8816
8817static int __perf_event_overflow(struct perf_event *event,
8818 int throttle, struct perf_sample_data *data,
8819 struct pt_regs *regs)
8820{
8821 int events = atomic_read(&event->event_limit);
8822 int ret = 0;
8823
8824 /*
8825 * Non-sampling counters might still use the PMI to fold short
8826 * hardware counters, ignore those.
8827 */
8828 if (unlikely(!is_sampling_event(event)))
8829 return 0;
8830
8831 ret = __perf_event_account_interrupt(event, throttle);
8832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008833 /*
8834 * XXX event_limit might not quite work as expected on inherited
8835 * events
8836 */
8837
8838 event->pending_kill = POLL_IN;
8839 if (events && atomic_dec_and_test(&event->event_limit)) {
8840 ret = 1;
8841 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02008842
8843 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008844 }
8845
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07008846 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01008847
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02008848 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008849 event->pending_wakeup = 1;
8850 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02008851 }
8852
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008853 return ret;
8854}
8855
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008856int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008857 struct perf_sample_data *data,
8858 struct pt_regs *regs)
8859{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008860 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008861}
8862
8863/*
8864 * Generic software event infrastructure
8865 */
8866
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008867struct swevent_htable {
8868 struct swevent_hlist *swevent_hlist;
8869 struct mutex hlist_mutex;
8870 int hlist_refcount;
8871
8872 /* Recursion avoidance in each contexts */
8873 int recursion[PERF_NR_CONTEXTS];
8874};
8875
8876static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
8877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008878/*
8879 * We directly increment event->count and keep a second value in
8880 * event->hw.period_left to count intervals. This period event
8881 * is kept in the range [-sample_period, 0] so that we can use the
8882 * sign as trigger.
8883 */
8884
Jiri Olsaab573842013-05-01 17:25:44 +02008885u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008886{
8887 struct hw_perf_event *hwc = &event->hw;
8888 u64 period = hwc->last_period;
8889 u64 nr, offset;
8890 s64 old, val;
8891
8892 hwc->last_period = hwc->sample_period;
8893
8894again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02008895 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008896 if (val < 0)
8897 return 0;
8898
8899 nr = div64_u64(period + val, period);
8900 offset = nr * period;
8901 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02008902 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008903 goto again;
8904
8905 return nr;
8906}
8907
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008908static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008909 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008910 struct pt_regs *regs)
8911{
8912 struct hw_perf_event *hwc = &event->hw;
8913 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008914
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008915 if (!overflow)
8916 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008917
8918 if (hwc->interrupts == MAX_INTERRUPTS)
8919 return;
8920
8921 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008922 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008923 data, regs)) {
8924 /*
8925 * We inhibit the overflow from happening when
8926 * hwc->interrupts == MAX_INTERRUPTS.
8927 */
8928 break;
8929 }
8930 throttle = 1;
8931 }
8932}
8933
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008934static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008935 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008936 struct pt_regs *regs)
8937{
8938 struct hw_perf_event *hwc = &event->hw;
8939
Peter Zijlstrae7850592010-05-21 14:43:08 +02008940 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008941
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008942 if (!regs)
8943 return;
8944
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008945 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008946 return;
8947
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03008948 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
8949 data->period = nr;
8950 return perf_swevent_overflow(event, 1, data, regs);
8951 } else
8952 data->period = event->hw.last_period;
8953
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008954 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008955 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008956
Peter Zijlstrae7850592010-05-21 14:43:08 +02008957 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008958 return;
8959
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008960 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008961}
8962
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008963static int perf_exclude_event(struct perf_event *event,
8964 struct pt_regs *regs)
8965{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008966 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01008967 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008968
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008969 if (regs) {
8970 if (event->attr.exclude_user && user_mode(regs))
8971 return 1;
8972
8973 if (event->attr.exclude_kernel && !user_mode(regs))
8974 return 1;
8975 }
8976
8977 return 0;
8978}
8979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008980static int perf_swevent_match(struct perf_event *event,
8981 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08008982 u32 event_id,
8983 struct perf_sample_data *data,
8984 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008985{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008986 if (event->attr.type != type)
8987 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008988
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008989 if (event->attr.config != event_id)
8990 return 0;
8991
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008992 if (perf_exclude_event(event, regs))
8993 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008994
8995 return 1;
8996}
8997
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008998static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008999{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009000 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009001
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009002 return hash_64(val, SWEVENT_HLIST_BITS);
9003}
9004
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009005static inline struct hlist_head *
9006__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009007{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009008 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009009
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009010 return &hlist->heads[hash];
9011}
9012
9013/* For the read side: events when they trigger */
9014static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009015find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009016{
9017 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009018
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009019 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009020 if (!hlist)
9021 return NULL;
9022
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009023 return __find_swevent_head(hlist, type, event_id);
9024}
9025
9026/* For the event head insertion and removal in the hlist */
9027static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009028find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009029{
9030 struct swevent_hlist *hlist;
9031 u32 event_id = event->attr.config;
9032 u64 type = event->attr.type;
9033
9034 /*
9035 * Event scheduling is always serialized against hlist allocation
9036 * and release. Which makes the protected version suitable here.
9037 * The context lock guarantees that.
9038 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009039 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009040 lockdep_is_held(&event->ctx->lock));
9041 if (!hlist)
9042 return NULL;
9043
9044 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009045}
9046
9047static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009048 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009049 struct perf_sample_data *data,
9050 struct pt_regs *regs)
9051{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009052 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009053 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009054 struct hlist_head *head;
9055
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009056 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009057 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009058 if (!head)
9059 goto end;
9060
Sasha Levinb67bfe02013-02-27 17:06:00 -08009061 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08009062 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009063 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009064 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009065end:
9066 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009067}
9068
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009069DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
9070
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009071int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009072{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009073 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01009074
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009075 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009076}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01009077EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009078
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009079void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009080{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009081 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02009082
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009083 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01009084}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009085
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009086void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009087{
Ingo Molnara4234bf2009-11-23 10:57:59 +01009088 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009089
9090 if (WARN_ON_ONCE(!regs))
9091 return;
9092
9093 perf_sample_data_init(&data, addr, 0);
9094 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
9095}
9096
9097void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
9098{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009099 int rctx;
9100
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009101 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009102 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009103 if (unlikely(rctx < 0))
9104 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009105
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009106 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009107
9108 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009109fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009110 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009111}
9112
9113static void perf_swevent_read(struct perf_event *event)
9114{
9115}
9116
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009117static int perf_swevent_add(struct perf_event *event, int flags)
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);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009120 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009121 struct hlist_head *head;
9122
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01009123 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009124 hwc->last_period = hwc->sample_period;
9125 perf_swevent_set_period(event);
9126 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009127
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009128 hwc->state = !(flags & PERF_EF_START);
9129
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009130 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01009131 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009132 return -EINVAL;
9133
9134 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08009135 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009136
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009137 return 0;
9138}
9139
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009140static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009141{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009142 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009143}
9144
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009145static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009146{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009147 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009148}
9149
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009150static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009151{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009152 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009153}
9154
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009155/* Deref the hlist from the update side */
9156static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009157swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009158{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009159 return rcu_dereference_protected(swhash->swevent_hlist,
9160 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009161}
9162
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009163static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009164{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009165 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009166
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009167 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009168 return;
9169
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03009170 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08009171 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009172}
9173
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009174static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009175{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009176 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009177
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009178 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009179
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009180 if (!--swhash->hlist_refcount)
9181 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009182
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009183 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009184}
9185
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009186static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009187{
9188 int cpu;
9189
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009190 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009191 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009192}
9193
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009194static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009195{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009196 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009197 int err = 0;
9198
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009199 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009200 if (!swevent_hlist_deref(swhash) &&
9201 cpumask_test_cpu(cpu, perf_online_mask)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009202 struct swevent_hlist *hlist;
9203
9204 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
9205 if (!hlist) {
9206 err = -ENOMEM;
9207 goto exit;
9208 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009209 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009210 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009211 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02009212exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009213 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009214
9215 return err;
9216}
9217
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009218static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009219{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009220 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009221
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009222 mutex_lock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009223 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009224 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009225 if (err) {
9226 failed_cpu = cpu;
9227 goto fail;
9228 }
9229 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009230 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009231 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02009232fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009233 for_each_possible_cpu(cpu) {
9234 if (cpu == failed_cpu)
9235 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009236 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009237 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009238 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009239 return err;
9240}
9241
Ingo Molnarc5905af2012-02-24 08:31:31 +01009242struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009243
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009244static void sw_perf_event_destroy(struct perf_event *event)
9245{
9246 u64 event_id = event->attr.config;
9247
9248 WARN_ON(event->parent);
9249
Ingo Molnarc5905af2012-02-24 08:31:31 +01009250 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009251 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009252}
9253
9254static int perf_swevent_init(struct perf_event *event)
9255{
Tommi Rantala8176cce2013-04-13 22:49:14 +03009256 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009257
9258 if (event->attr.type != PERF_TYPE_SOFTWARE)
9259 return -ENOENT;
9260
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009261 /*
9262 * no branch sampling for software events
9263 */
9264 if (has_branch_stack(event))
9265 return -EOPNOTSUPP;
9266
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009267 switch (event_id) {
9268 case PERF_COUNT_SW_CPU_CLOCK:
9269 case PERF_COUNT_SW_TASK_CLOCK:
9270 return -ENOENT;
9271
9272 default:
9273 break;
9274 }
9275
Dan Carpenterce677832010-10-24 21:50:42 +02009276 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009277 return -ENOENT;
9278
9279 if (!event->parent) {
9280 int err;
9281
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009282 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009283 if (err)
9284 return err;
9285
Ingo Molnarc5905af2012-02-24 08:31:31 +01009286 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009287 event->destroy = sw_perf_event_destroy;
9288 }
9289
9290 return 0;
9291}
9292
9293static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009294 .task_ctx_nr = perf_sw_context,
9295
Peter Zijlstra34f43922015-02-20 14:05:38 +01009296 .capabilities = PERF_PMU_CAP_NO_NMI,
9297
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009298 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009299 .add = perf_swevent_add,
9300 .del = perf_swevent_del,
9301 .start = perf_swevent_start,
9302 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009303 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009304};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009305
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009306#ifdef CONFIG_EVENT_TRACING
9307
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009308static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009309 struct perf_sample_data *data)
9310{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02009311 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009312
Peter Zijlstrab71b4372015-11-02 10:50:51 +01009313 /* only top level events have filters set */
9314 if (event->parent)
9315 event = event->parent;
9316
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009317 if (likely(!event->filter) || filter_match_preds(event->filter, record))
9318 return 1;
9319 return 0;
9320}
9321
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009322static int perf_tp_event_match(struct perf_event *event,
9323 struct perf_sample_data *data,
9324 struct pt_regs *regs)
9325{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01009326 if (event->hw.state & PERF_HES_STOPPED)
9327 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02009328 /*
Song Liu9fd2e482019-05-07 09:15:45 -07009329 * If exclude_kernel, only trace user-space tracepoints (uprobes)
Peter Zijlstra580d6072010-05-20 20:54:31 +02009330 */
Song Liu9fd2e482019-05-07 09:15:45 -07009331 if (event->attr.exclude_kernel && !user_mode(regs))
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009332 return 0;
9333
9334 if (!perf_tp_filter_match(event, data))
9335 return 0;
9336
9337 return 1;
9338}
9339
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009340void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
9341 struct trace_event_call *call, u64 count,
9342 struct pt_regs *regs, struct hlist_head *head,
9343 struct task_struct *task)
9344{
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009345 if (bpf_prog_array_valid(call)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009346 *(struct pt_regs **)raw_data = regs;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009347 if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009348 perf_swevent_put_recursion_context(rctx);
9349 return;
9350 }
9351 }
9352 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009353 rctx, task);
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009354}
9355EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
9356
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009357void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009358 struct pt_regs *regs, struct hlist_head *head, int rctx,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009359 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009360{
9361 struct perf_sample_data data;
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009362 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009363
9364 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02009365 .frag = {
9366 .size = entry_size,
9367 .data = record,
9368 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009369 };
9370
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009371 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009372 data.raw = &raw;
9373
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009374 perf_trace_buf_update(record, event_type);
9375
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009376 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009377 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009378 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009379 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02009380
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009381 /*
9382 * If we got specified a target task, also iterate its context and
9383 * deliver this event there too.
9384 */
9385 if (task && task != current) {
9386 struct perf_event_context *ctx;
9387 struct trace_entry *entry = record;
9388
9389 rcu_read_lock();
9390 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
9391 if (!ctx)
9392 goto unlock;
9393
9394 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Jiri Olsacd6fb6772018-09-23 18:13:43 +02009395 if (event->cpu != smp_processor_id())
9396 continue;
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009397 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9398 continue;
9399 if (event->attr.config != entry->type)
9400 continue;
9401 if (perf_tp_event_match(event, &data, regs))
9402 perf_swevent_event(event, count, &data, regs);
9403 }
9404unlock:
9405 rcu_read_unlock();
9406 }
9407
Peter Zijlstraecc55f82010-05-21 15:11:34 +02009408 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009409}
9410EXPORT_SYMBOL_GPL(perf_tp_event);
9411
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009412static void tp_perf_event_destroy(struct perf_event *event)
9413{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009414 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009415}
9416
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009417static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009418{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009419 int err;
9420
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009421 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9422 return -ENOENT;
9423
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009424 /*
9425 * no branch sampling for tracepoint events
9426 */
9427 if (has_branch_stack(event))
9428 return -EOPNOTSUPP;
9429
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009430 err = perf_trace_init(event);
9431 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009432 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009433
9434 event->destroy = tp_perf_event_destroy;
9435
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009436 return 0;
9437}
9438
9439static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009440 .task_ctx_nr = perf_sw_context,
9441
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009442 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009443 .add = perf_trace_add,
9444 .del = perf_trace_del,
9445 .start = perf_swevent_start,
9446 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009447 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009448};
9449
Song Liu33ea4b22017-12-06 14:45:16 -08009450#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Song Liue12f03d2017-12-06 14:45:15 -08009451/*
9452 * Flags in config, used by dynamic PMU kprobe and uprobe
9453 * The flags should match following PMU_FORMAT_ATTR().
9454 *
9455 * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe
9456 * if not set, create kprobe/uprobe
Song Liua6ca88b2018-10-01 22:36:36 -07009457 *
9458 * The following values specify a reference counter (or semaphore in the
9459 * terminology of tools like dtrace, systemtap, etc.) Userspace Statically
9460 * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset.
9461 *
9462 * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset
9463 * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left
Song Liue12f03d2017-12-06 14:45:15 -08009464 */
9465enum perf_probe_config {
9466 PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */
Song Liua6ca88b2018-10-01 22:36:36 -07009467 PERF_UPROBE_REF_CTR_OFFSET_BITS = 32,
9468 PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS,
Song Liue12f03d2017-12-06 14:45:15 -08009469};
9470
9471PMU_FORMAT_ATTR(retprobe, "config:0");
Song Liua6ca88b2018-10-01 22:36:36 -07009472#endif
Song Liue12f03d2017-12-06 14:45:15 -08009473
Song Liua6ca88b2018-10-01 22:36:36 -07009474#ifdef CONFIG_KPROBE_EVENTS
9475static struct attribute *kprobe_attrs[] = {
Song Liue12f03d2017-12-06 14:45:15 -08009476 &format_attr_retprobe.attr,
9477 NULL,
9478};
9479
Song Liua6ca88b2018-10-01 22:36:36 -07009480static struct attribute_group kprobe_format_group = {
Song Liue12f03d2017-12-06 14:45:15 -08009481 .name = "format",
Song Liua6ca88b2018-10-01 22:36:36 -07009482 .attrs = kprobe_attrs,
Song Liue12f03d2017-12-06 14:45:15 -08009483};
9484
Song Liua6ca88b2018-10-01 22:36:36 -07009485static const struct attribute_group *kprobe_attr_groups[] = {
9486 &kprobe_format_group,
Song Liue12f03d2017-12-06 14:45:15 -08009487 NULL,
9488};
9489
9490static int perf_kprobe_event_init(struct perf_event *event);
9491static struct pmu perf_kprobe = {
9492 .task_ctx_nr = perf_sw_context,
9493 .event_init = perf_kprobe_event_init,
9494 .add = perf_trace_add,
9495 .del = perf_trace_del,
9496 .start = perf_swevent_start,
9497 .stop = perf_swevent_stop,
9498 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009499 .attr_groups = kprobe_attr_groups,
Song Liue12f03d2017-12-06 14:45:15 -08009500};
9501
9502static int perf_kprobe_event_init(struct perf_event *event)
9503{
9504 int err;
9505 bool is_retprobe;
9506
9507 if (event->attr.type != perf_kprobe.type)
9508 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009509
Alexey Budankovc9e09242020-04-02 11:47:01 +03009510 if (!perfmon_capable())
Song Liu32e6e962018-04-11 18:02:37 +00009511 return -EACCES;
9512
Song Liue12f03d2017-12-06 14:45:15 -08009513 /*
9514 * no branch sampling for probe events
9515 */
9516 if (has_branch_stack(event))
9517 return -EOPNOTSUPP;
9518
9519 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
9520 err = perf_kprobe_init(event, is_retprobe);
9521 if (err)
9522 return err;
9523
9524 event->destroy = perf_kprobe_destroy;
9525
9526 return 0;
9527}
9528#endif /* CONFIG_KPROBE_EVENTS */
9529
Song Liu33ea4b22017-12-06 14:45:16 -08009530#ifdef CONFIG_UPROBE_EVENTS
Song Liua6ca88b2018-10-01 22:36:36 -07009531PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63");
9532
9533static struct attribute *uprobe_attrs[] = {
9534 &format_attr_retprobe.attr,
9535 &format_attr_ref_ctr_offset.attr,
9536 NULL,
9537};
9538
9539static struct attribute_group uprobe_format_group = {
9540 .name = "format",
9541 .attrs = uprobe_attrs,
9542};
9543
9544static const struct attribute_group *uprobe_attr_groups[] = {
9545 &uprobe_format_group,
9546 NULL,
9547};
9548
Song Liu33ea4b22017-12-06 14:45:16 -08009549static int perf_uprobe_event_init(struct perf_event *event);
9550static struct pmu perf_uprobe = {
9551 .task_ctx_nr = perf_sw_context,
9552 .event_init = perf_uprobe_event_init,
9553 .add = perf_trace_add,
9554 .del = perf_trace_del,
9555 .start = perf_swevent_start,
9556 .stop = perf_swevent_stop,
9557 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009558 .attr_groups = uprobe_attr_groups,
Song Liu33ea4b22017-12-06 14:45:16 -08009559};
9560
9561static int perf_uprobe_event_init(struct perf_event *event)
9562{
9563 int err;
Song Liua6ca88b2018-10-01 22:36:36 -07009564 unsigned long ref_ctr_offset;
Song Liu33ea4b22017-12-06 14:45:16 -08009565 bool is_retprobe;
9566
9567 if (event->attr.type != perf_uprobe.type)
9568 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009569
Alexey Budankovc9e09242020-04-02 11:47:01 +03009570 if (!perfmon_capable())
Song Liu32e6e962018-04-11 18:02:37 +00009571 return -EACCES;
9572
Song Liu33ea4b22017-12-06 14:45:16 -08009573 /*
9574 * no branch sampling for probe events
9575 */
9576 if (has_branch_stack(event))
9577 return -EOPNOTSUPP;
9578
9579 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
Song Liua6ca88b2018-10-01 22:36:36 -07009580 ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
9581 err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe);
Song Liu33ea4b22017-12-06 14:45:16 -08009582 if (err)
9583 return err;
9584
9585 event->destroy = perf_uprobe_destroy;
9586
9587 return 0;
9588}
9589#endif /* CONFIG_UPROBE_EVENTS */
9590
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009591static inline void perf_tp_register(void)
9592{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009593 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Song Liue12f03d2017-12-06 14:45:15 -08009594#ifdef CONFIG_KPROBE_EVENTS
9595 perf_pmu_register(&perf_kprobe, "kprobe", -1);
9596#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009597#ifdef CONFIG_UPROBE_EVENTS
9598 perf_pmu_register(&perf_uprobe, "uprobe", -1);
9599#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009600}
Li Zefan6fb29152009-10-15 11:21:42 +08009601
Li Zefan6fb29152009-10-15 11:21:42 +08009602static void perf_event_free_filter(struct perf_event *event)
9603{
9604 ftrace_profile_free_filter(event);
9605}
9606
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009607#ifdef CONFIG_BPF_SYSCALL
9608static void bpf_overflow_handler(struct perf_event *event,
9609 struct perf_sample_data *data,
9610 struct pt_regs *regs)
9611{
9612 struct bpf_perf_event_data_kern ctx = {
9613 .data = data,
Yonghong Song7d9285e2017-10-05 09:19:19 -07009614 .event = event,
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009615 };
9616 int ret = 0;
9617
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +01009618 ctx.regs = perf_arch_bpf_user_pt_regs(regs);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009619 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
9620 goto out;
9621 rcu_read_lock();
Daniel Borkmann88575192016-11-26 01:28:04 +01009622 ret = BPF_PROG_RUN(event->prog, &ctx);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009623 rcu_read_unlock();
9624out:
9625 __this_cpu_dec(bpf_prog_active);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009626 if (!ret)
9627 return;
9628
9629 event->orig_overflow_handler(event, data, regs);
9630}
9631
9632static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9633{
9634 struct bpf_prog *prog;
9635
9636 if (event->overflow_handler_context)
9637 /* hw breakpoint or kernel counter */
9638 return -EINVAL;
9639
9640 if (event->prog)
9641 return -EEXIST;
9642
9643 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
9644 if (IS_ERR(prog))
9645 return PTR_ERR(prog);
9646
Song Liu5d99cb2c2020-07-23 11:06:45 -07009647 if (event->attr.precise_ip &&
9648 prog->call_get_stack &&
9649 (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY) ||
9650 event->attr.exclude_callchain_kernel ||
9651 event->attr.exclude_callchain_user)) {
9652 /*
9653 * On perf_event with precise_ip, calling bpf_get_stack()
9654 * may trigger unwinder warnings and occasional crashes.
9655 * bpf_get_[stack|stackid] works around this issue by using
9656 * callchain attached to perf_sample_data. If the
9657 * perf_event does not full (kernel and user) callchain
9658 * attached to perf_sample_data, do not allow attaching BPF
9659 * program that calls bpf_get_[stack|stackid].
9660 */
9661 bpf_prog_put(prog);
9662 return -EPROTO;
9663 }
9664
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009665 event->prog = prog;
9666 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
9667 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
9668 return 0;
9669}
9670
9671static void perf_event_free_bpf_handler(struct perf_event *event)
9672{
9673 struct bpf_prog *prog = event->prog;
9674
9675 if (!prog)
9676 return;
9677
9678 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
9679 event->prog = NULL;
9680 bpf_prog_put(prog);
9681}
9682#else
9683static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9684{
9685 return -EOPNOTSUPP;
9686}
9687static void perf_event_free_bpf_handler(struct perf_event *event)
9688{
9689}
9690#endif
9691
Song Liue12f03d2017-12-06 14:45:15 -08009692/*
9693 * returns true if the event is a tracepoint, or a kprobe/upprobe created
9694 * with perf_event_open()
9695 */
9696static inline bool perf_event_is_tracing(struct perf_event *event)
9697{
9698 if (event->pmu == &perf_tracepoint)
9699 return true;
9700#ifdef CONFIG_KPROBE_EVENTS
9701 if (event->pmu == &perf_kprobe)
9702 return true;
9703#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009704#ifdef CONFIG_UPROBE_EVENTS
9705 if (event->pmu == &perf_uprobe)
9706 return true;
9707#endif
Song Liue12f03d2017-12-06 14:45:15 -08009708 return false;
9709}
9710
Alexei Starovoitov25415172015-03-25 12:49:20 -07009711static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9712{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009713 bool is_kprobe, is_tracepoint, is_syscall_tp;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009714 struct bpf_prog *prog;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009715 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009716
Song Liue12f03d2017-12-06 14:45:15 -08009717 if (!perf_event_is_tracing(event))
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07009718 return perf_event_set_bpf_handler(event, prog_fd);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009719
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009720 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
9721 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009722 is_syscall_tp = is_syscall_trace_event(event->tp_event);
9723 if (!is_kprobe && !is_tracepoint && !is_syscall_tp)
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009724 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07009725 return -EINVAL;
9726
9727 prog = bpf_prog_get(prog_fd);
9728 if (IS_ERR(prog))
9729 return PTR_ERR(prog);
9730
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009731 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009732 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
9733 (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07009734 /* valid fd, but invalid bpf program type */
9735 bpf_prog_put(prog);
9736 return -EINVAL;
9737 }
9738
Josef Bacik9802d862017-12-11 11:36:48 -05009739 /* Kprobe override only works for kprobes, not uprobes. */
9740 if (prog->kprobe_override &&
9741 !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) {
9742 bpf_prog_put(prog);
9743 return -EINVAL;
9744 }
9745
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009746 if (is_tracepoint || is_syscall_tp) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07009747 int off = trace_event_get_offsets(event->tp_event);
9748
9749 if (prog->aux->max_ctx_offset > off) {
9750 bpf_prog_put(prog);
9751 return -EACCES;
9752 }
9753 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07009754
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009755 ret = perf_event_attach_bpf_prog(event, prog);
9756 if (ret)
9757 bpf_prog_put(prog);
9758 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009759}
9760
9761static void perf_event_free_bpf_prog(struct perf_event *event)
9762{
Song Liue12f03d2017-12-06 14:45:15 -08009763 if (!perf_event_is_tracing(event)) {
Yonghong Song0b4c6842017-10-23 23:53:07 -07009764 perf_event_free_bpf_handler(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009765 return;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009766 }
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009767 perf_event_detach_bpf_prog(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009768}
9769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009770#else
Li Zefan6fb29152009-10-15 11:21:42 +08009771
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009772static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009773{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009774}
Li Zefan6fb29152009-10-15 11:21:42 +08009775
Li Zefan6fb29152009-10-15 11:21:42 +08009776static void perf_event_free_filter(struct perf_event *event)
9777{
9778}
9779
Alexei Starovoitov25415172015-03-25 12:49:20 -07009780static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9781{
9782 return -ENOENT;
9783}
9784
9785static void perf_event_free_bpf_prog(struct perf_event *event)
9786{
9787}
Li Zefan07b139c2009-12-21 14:27:35 +08009788#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009789
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009790#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009791void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009792{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009793 struct perf_sample_data sample;
9794 struct pt_regs *regs = data;
9795
Robert Richterfd0d0002012-04-02 20:19:08 +02009796 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009797
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009798 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009799 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009800}
9801#endif
9802
Alexander Shishkin375637b2016-04-27 18:44:46 +03009803/*
9804 * Allocate a new address filter
9805 */
9806static struct perf_addr_filter *
9807perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
9808{
9809 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
9810 struct perf_addr_filter *filter;
9811
9812 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
9813 if (!filter)
9814 return NULL;
9815
9816 INIT_LIST_HEAD(&filter->entry);
9817 list_add_tail(&filter->entry, filters);
9818
9819 return filter;
9820}
9821
9822static void free_filters_list(struct list_head *filters)
9823{
9824 struct perf_addr_filter *filter, *iter;
9825
9826 list_for_each_entry_safe(filter, iter, filters, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07009827 path_put(&filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009828 list_del(&filter->entry);
9829 kfree(filter);
9830 }
9831}
9832
9833/*
9834 * Free existing address filters and optionally install new ones
9835 */
9836static void perf_addr_filters_splice(struct perf_event *event,
9837 struct list_head *head)
9838{
9839 unsigned long flags;
9840 LIST_HEAD(list);
9841
9842 if (!has_addr_filter(event))
9843 return;
9844
9845 /* don't bother with children, they don't have their own filters */
9846 if (event->parent)
9847 return;
9848
9849 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
9850
9851 list_splice_init(&event->addr_filters.list, &list);
9852 if (head)
9853 list_splice(head, &event->addr_filters.list);
9854
9855 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
9856
9857 free_filters_list(&list);
9858}
9859
9860/*
9861 * Scan through mm's vmas and see if one of them matches the
9862 * @filter; if so, adjust filter's address range.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07009863 * Called with mm::mmap_lock down for reading.
Alexander Shishkin375637b2016-04-27 18:44:46 +03009864 */
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009865static void perf_addr_filter_apply(struct perf_addr_filter *filter,
9866 struct mm_struct *mm,
9867 struct perf_addr_filter_range *fr)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009868{
9869 struct vm_area_struct *vma;
9870
9871 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009872 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009873 continue;
9874
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009875 if (perf_addr_filter_vma_adjust(filter, vma, fr))
9876 return;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009877 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009878}
9879
9880/*
9881 * Update event's address range filters based on the
9882 * task's existing mappings, if any.
9883 */
9884static void perf_event_addr_filters_apply(struct perf_event *event)
9885{
9886 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
9887 struct task_struct *task = READ_ONCE(event->ctx->task);
9888 struct perf_addr_filter *filter;
9889 struct mm_struct *mm = NULL;
9890 unsigned int count = 0;
9891 unsigned long flags;
9892
9893 /*
9894 * We may observe TASK_TOMBSTONE, which means that the event tear-down
9895 * will stop on the parent's child_mutex that our caller is also holding
9896 */
9897 if (task == TASK_TOMBSTONE)
9898 return;
9899
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009900 if (ifh->nr_file_filters) {
9901 mm = get_task_mm(event->ctx->task);
9902 if (!mm)
9903 goto restart;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009904
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07009905 mmap_read_lock(mm);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009906 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009907
9908 raw_spin_lock_irqsave(&ifh->lock, flags);
9909 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009910 if (filter->path.dentry) {
9911 /*
9912 * Adjust base offset if the filter is associated to a
9913 * binary that needs to be mapped:
9914 */
9915 event->addr_filter_ranges[count].start = 0;
9916 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009917
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009918 perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009919 } else {
9920 event->addr_filter_ranges[count].start = filter->offset;
9921 event->addr_filter_ranges[count].size = filter->size;
9922 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009923
9924 count++;
9925 }
9926
9927 event->addr_filters_gen++;
9928 raw_spin_unlock_irqrestore(&ifh->lock, flags);
9929
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009930 if (ifh->nr_file_filters) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07009931 mmap_read_unlock(mm);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009932
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009933 mmput(mm);
9934 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009935
9936restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03009937 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009938}
9939
9940/*
9941 * Address range filtering: limiting the data to certain
9942 * instruction address ranges. Filters are ioctl()ed to us from
9943 * userspace as ascii strings.
9944 *
9945 * Filter string format:
9946 *
9947 * ACTION RANGE_SPEC
9948 * where ACTION is one of the
9949 * * "filter": limit the trace to this region
9950 * * "start": start tracing from this address
9951 * * "stop": stop tracing at this address/region;
9952 * RANGE_SPEC is
9953 * * for kernel addresses: <start address>[/<size>]
9954 * * for object files: <start address>[/<size>]@</path/to/object/file>
9955 *
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009956 * if <size> is not specified or is zero, the range is treated as a single
9957 * address; not valid for ACTION=="filter".
Alexander Shishkin375637b2016-04-27 18:44:46 +03009958 */
9959enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02009960 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03009961 IF_ACT_FILTER,
9962 IF_ACT_START,
9963 IF_ACT_STOP,
9964 IF_SRC_FILE,
9965 IF_SRC_KERNEL,
9966 IF_SRC_FILEADDR,
9967 IF_SRC_KERNELADDR,
9968};
9969
9970enum {
9971 IF_STATE_ACTION = 0,
9972 IF_STATE_SOURCE,
9973 IF_STATE_END,
9974};
9975
9976static const match_table_t if_tokens = {
9977 { IF_ACT_FILTER, "filter" },
9978 { IF_ACT_START, "start" },
9979 { IF_ACT_STOP, "stop" },
9980 { IF_SRC_FILE, "%u/%u@%s" },
9981 { IF_SRC_KERNEL, "%u/%u" },
9982 { IF_SRC_FILEADDR, "%u@%s" },
9983 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02009984 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03009985};
9986
9987/*
9988 * Address filter string parser
9989 */
9990static int
9991perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
9992 struct list_head *filters)
9993{
9994 struct perf_addr_filter *filter = NULL;
9995 char *start, *orig, *filename = NULL;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009996 substring_t args[MAX_OPT_ARGS];
9997 int state = IF_STATE_ACTION, token;
9998 unsigned int kernel = 0;
9999 int ret = -EINVAL;
10000
10001 orig = fstr = kstrdup(fstr, GFP_KERNEL);
10002 if (!fstr)
10003 return -ENOMEM;
10004
10005 while ((start = strsep(&fstr, " ,\n")) != NULL) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010006 static const enum perf_addr_filter_action_t actions[] = {
10007 [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER,
10008 [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START,
10009 [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP,
10010 };
Alexander Shishkin375637b2016-04-27 18:44:46 +030010011 ret = -EINVAL;
10012
10013 if (!*start)
10014 continue;
10015
10016 /* filter definition begins */
10017 if (state == IF_STATE_ACTION) {
10018 filter = perf_addr_filter_new(event, filters);
10019 if (!filter)
10020 goto fail;
10021 }
10022
10023 token = match_token(start, if_tokens, args);
10024 switch (token) {
10025 case IF_ACT_FILTER:
10026 case IF_ACT_START:
Alexander Shishkin375637b2016-04-27 18:44:46 +030010027 case IF_ACT_STOP:
10028 if (state != IF_STATE_ACTION)
10029 goto fail;
10030
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010031 filter->action = actions[token];
Alexander Shishkin375637b2016-04-27 18:44:46 +030010032 state = IF_STATE_SOURCE;
10033 break;
10034
10035 case IF_SRC_KERNELADDR:
10036 case IF_SRC_KERNEL:
10037 kernel = 1;
Gustavo A. R. Silva10c34052019-02-12 14:54:30 -060010038 /* fall through */
Alexander Shishkin375637b2016-04-27 18:44:46 +030010039
10040 case IF_SRC_FILEADDR:
10041 case IF_SRC_FILE:
10042 if (state != IF_STATE_SOURCE)
10043 goto fail;
10044
Alexander Shishkin375637b2016-04-27 18:44:46 +030010045 *args[0].to = 0;
10046 ret = kstrtoul(args[0].from, 0, &filter->offset);
10047 if (ret)
10048 goto fail;
10049
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010050 if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) {
Alexander Shishkin375637b2016-04-27 18:44:46 +030010051 *args[1].to = 0;
10052 ret = kstrtoul(args[1].from, 0, &filter->size);
10053 if (ret)
10054 goto fail;
10055 }
10056
Mathieu Poirier4059ffd2016-07-18 10:43:05 -060010057 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010058 int fpos = token == IF_SRC_FILE ? 2 : 1;
Mathieu Poirier4059ffd2016-07-18 10:43:05 -060010059
10060 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010061 if (!filename) {
10062 ret = -ENOMEM;
10063 goto fail;
10064 }
10065 }
10066
10067 state = IF_STATE_END;
10068 break;
10069
10070 default:
10071 goto fail;
10072 }
10073
10074 /*
10075 * Filter definition is fully parsed, validate and install it.
10076 * Make sure that it doesn't contradict itself or the event's
10077 * attribute.
10078 */
10079 if (state == IF_STATE_END) {
Alexander Shishkin9ccbfbb2017-01-26 11:40:56 +020010080 ret = -EINVAL;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010081 if (kernel && event->attr.exclude_kernel)
10082 goto fail;
10083
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +030010084 /*
10085 * ACTION "filter" must have a non-zero length region
10086 * specified.
10087 */
10088 if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER &&
10089 !filter->size)
10090 goto fail;
10091
Alexander Shishkin375637b2016-04-27 18:44:46 +030010092 if (!kernel) {
10093 if (!filename)
10094 goto fail;
10095
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010096 /*
10097 * For now, we only support file-based filters
10098 * in per-task events; doing so for CPU-wide
10099 * events requires additional context switching
10100 * trickery, since same object code will be
10101 * mapped at different virtual addresses in
10102 * different processes.
10103 */
10104 ret = -EOPNOTSUPP;
10105 if (!event->ctx->task)
10106 goto fail_free_name;
10107
Alexander Shishkin375637b2016-04-27 18:44:46 +030010108 /* look up the path and grab its inode */
Song Liu9511bce2018-04-17 23:29:07 -070010109 ret = kern_path(filename, LOOKUP_FOLLOW,
10110 &filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010111 if (ret)
10112 goto fail_free_name;
10113
Alexander Shishkin375637b2016-04-27 18:44:46 +030010114 kfree(filename);
10115 filename = NULL;
10116
10117 ret = -EINVAL;
Song Liu9511bce2018-04-17 23:29:07 -070010118 if (!filter->path.dentry ||
10119 !S_ISREG(d_inode(filter->path.dentry)
10120 ->i_mode))
Alexander Shishkin375637b2016-04-27 18:44:46 +030010121 goto fail;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010122
10123 event->addr_filters.nr_file_filters++;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010124 }
10125
10126 /* ready to consume more filters */
10127 state = IF_STATE_ACTION;
10128 filter = NULL;
10129 }
10130 }
10131
10132 if (state != IF_STATE_ACTION)
10133 goto fail;
10134
10135 kfree(orig);
10136
10137 return 0;
10138
10139fail_free_name:
10140 kfree(filename);
10141fail:
10142 free_filters_list(filters);
10143 kfree(orig);
10144
10145 return ret;
10146}
10147
10148static int
10149perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
10150{
10151 LIST_HEAD(filters);
10152 int ret;
10153
10154 /*
10155 * Since this is called in perf_ioctl() path, we're already holding
10156 * ctx::mutex.
10157 */
10158 lockdep_assert_held(&event->ctx->mutex);
10159
10160 if (WARN_ON_ONCE(event->parent))
10161 return -EINVAL;
10162
Alexander Shishkin375637b2016-04-27 18:44:46 +030010163 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
10164 if (ret)
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010165 goto fail_clear_files;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010166
10167 ret = event->pmu->addr_filters_validate(&filters);
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010168 if (ret)
10169 goto fail_free_filters;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010170
10171 /* remove existing filters, if any */
10172 perf_addr_filters_splice(event, &filters);
10173
10174 /* install new filters */
10175 perf_event_for_each_child(event, perf_event_addr_filters_apply);
10176
10177 return ret;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010178
10179fail_free_filters:
10180 free_filters_list(&filters);
10181
10182fail_clear_files:
10183 event->addr_filters.nr_file_filters = 0;
10184
10185 return ret;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010186}
10187
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010188static int perf_event_set_filter(struct perf_event *event, void __user *arg)
10189{
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010190 int ret = -EINVAL;
Song Liue12f03d2017-12-06 14:45:15 -080010191 char *filter_str;
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010192
10193 filter_str = strndup_user(arg, PAGE_SIZE);
10194 if (IS_ERR(filter_str))
10195 return PTR_ERR(filter_str);
10196
Song Liue12f03d2017-12-06 14:45:15 -080010197#ifdef CONFIG_EVENT_TRACING
10198 if (perf_event_is_tracing(event)) {
10199 struct perf_event_context *ctx = event->ctx;
10200
10201 /*
10202 * Beware, here be dragons!!
10203 *
10204 * the tracepoint muck will deadlock against ctx->mutex, but
10205 * the tracepoint stuff does not actually need it. So
10206 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
10207 * already have a reference on ctx.
10208 *
10209 * This can result in event getting moved to a different ctx,
10210 * but that does not affect the tracepoint state.
10211 */
10212 mutex_unlock(&ctx->mutex);
10213 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
10214 mutex_lock(&ctx->mutex);
10215 } else
10216#endif
10217 if (has_addr_filter(event))
Alexander Shishkin375637b2016-04-27 18:44:46 +030010218 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010219
10220 kfree(filter_str);
10221 return ret;
10222}
10223
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010224/*
10225 * hrtimer based swevent callback
10226 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010227
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010228static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010229{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010230 enum hrtimer_restart ret = HRTIMER_RESTART;
10231 struct perf_sample_data data;
10232 struct pt_regs *regs;
10233 struct perf_event *event;
10234 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010235
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010236 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010237
10238 if (event->state != PERF_EVENT_STATE_ACTIVE)
10239 return HRTIMER_NORESTART;
10240
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010241 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010242
Robert Richterfd0d0002012-04-02 20:19:08 +020010243 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010244 regs = get_irq_regs();
10245
10246 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -080010247 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +020010248 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010249 ret = HRTIMER_NORESTART;
10250 }
10251
10252 period = max_t(u64, 10000, event->hw.sample_period);
10253 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
10254
10255 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010256}
10257
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010258static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010259{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010260 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010261 s64 period;
10262
10263 if (!is_sampling_event(event))
10264 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010265
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010266 period = local64_read(&hwc->period_left);
10267 if (period) {
10268 if (period < 0)
10269 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +020010270
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010271 local64_set(&hwc->period_left, 0);
10272 } else {
10273 period = max_t(u64, 10000, hwc->sample_period);
10274 }
Thomas Gleixner3497d202015-04-14 21:09:03 +000010275 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +020010276 HRTIMER_MODE_REL_PINNED_HARD);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010277}
10278
10279static void perf_swevent_cancel_hrtimer(struct perf_event *event)
10280{
10281 struct hw_perf_event *hwc = &event->hw;
10282
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +010010283 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010284 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +020010285 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010286
10287 hrtimer_cancel(&hwc->hrtimer);
10288 }
10289}
10290
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010291static void perf_swevent_init_hrtimer(struct perf_event *event)
10292{
10293 struct hw_perf_event *hwc = &event->hw;
10294
10295 if (!is_sampling_event(event))
10296 return;
10297
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +020010298 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010299 hwc->hrtimer.function = perf_swevent_hrtimer;
10300
10301 /*
10302 * Since hrtimers have a fixed rate, we can do a static freq->period
10303 * mapping and avoid the whole period adjust feedback stuff.
10304 */
10305 if (event->attr.freq) {
10306 long freq = event->attr.sample_freq;
10307
10308 event->attr.sample_period = NSEC_PER_SEC / freq;
10309 hwc->sample_period = event->attr.sample_period;
10310 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +090010311 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010312 event->attr.freq = 0;
10313 }
10314}
10315
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010316/*
10317 * Software event: cpu wall time clock
10318 */
10319
10320static void cpu_clock_event_update(struct perf_event *event)
10321{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010322 s64 prev;
10323 u64 now;
10324
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010325 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010326 prev = local64_xchg(&event->hw.prev_count, now);
10327 local64_add(now - prev, &event->count);
10328}
10329
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010330static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010331{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010332 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010333 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010334}
10335
10336static void cpu_clock_event_stop(struct perf_event *event, int flags)
10337{
10338 perf_swevent_cancel_hrtimer(event);
10339 cpu_clock_event_update(event);
10340}
10341
10342static int cpu_clock_event_add(struct perf_event *event, int flags)
10343{
10344 if (flags & PERF_EF_START)
10345 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -080010346 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010347
10348 return 0;
10349}
10350
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010351static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010352{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010353 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010354}
10355
10356static void cpu_clock_event_read(struct perf_event *event)
10357{
10358 cpu_clock_event_update(event);
10359}
10360
10361static int cpu_clock_event_init(struct perf_event *event)
10362{
10363 if (event->attr.type != PERF_TYPE_SOFTWARE)
10364 return -ENOENT;
10365
10366 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
10367 return -ENOENT;
10368
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010369 /*
10370 * no branch sampling for software events
10371 */
10372 if (has_branch_stack(event))
10373 return -EOPNOTSUPP;
10374
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010375 perf_swevent_init_hrtimer(event);
10376
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010377 return 0;
10378}
10379
10380static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010381 .task_ctx_nr = perf_sw_context,
10382
Peter Zijlstra34f43922015-02-20 14:05:38 +010010383 .capabilities = PERF_PMU_CAP_NO_NMI,
10384
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010385 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010386 .add = cpu_clock_event_add,
10387 .del = cpu_clock_event_del,
10388 .start = cpu_clock_event_start,
10389 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010390 .read = cpu_clock_event_read,
10391};
10392
10393/*
10394 * Software event: task time clock
10395 */
10396
10397static void task_clock_event_update(struct perf_event *event, u64 now)
10398{
10399 u64 prev;
10400 s64 delta;
10401
10402 prev = local64_xchg(&event->hw.prev_count, now);
10403 delta = now - prev;
10404 local64_add(delta, &event->count);
10405}
10406
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010407static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010408{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010409 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010410 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010411}
10412
10413static void task_clock_event_stop(struct perf_event *event, int flags)
10414{
10415 perf_swevent_cancel_hrtimer(event);
10416 task_clock_event_update(event, event->ctx->time);
10417}
10418
10419static int task_clock_event_add(struct perf_event *event, int flags)
10420{
10421 if (flags & PERF_EF_START)
10422 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -080010423 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010424
10425 return 0;
10426}
10427
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010428static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010429{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010430 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010431}
10432
10433static void task_clock_event_read(struct perf_event *event)
10434{
Peter Zijlstra768a06e2011-02-22 16:52:24 +010010435 u64 now = perf_clock();
10436 u64 delta = now - event->ctx->timestamp;
10437 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010438
10439 task_clock_event_update(event, time);
10440}
10441
10442static int task_clock_event_init(struct perf_event *event)
10443{
10444 if (event->attr.type != PERF_TYPE_SOFTWARE)
10445 return -ENOENT;
10446
10447 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
10448 return -ENOENT;
10449
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010450 /*
10451 * no branch sampling for software events
10452 */
10453 if (has_branch_stack(event))
10454 return -EOPNOTSUPP;
10455
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010456 perf_swevent_init_hrtimer(event);
10457
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010458 return 0;
10459}
10460
10461static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010462 .task_ctx_nr = perf_sw_context,
10463
Peter Zijlstra34f43922015-02-20 14:05:38 +010010464 .capabilities = PERF_PMU_CAP_NO_NMI,
10465
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010466 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010467 .add = task_clock_event_add,
10468 .del = task_clock_event_del,
10469 .start = task_clock_event_start,
10470 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010471 .read = task_clock_event_read,
10472};
10473
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010474static void perf_pmu_nop_void(struct pmu *pmu)
10475{
10476}
10477
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010478static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
10479{
10480}
10481
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010482static int perf_pmu_nop_int(struct pmu *pmu)
10483{
10484 return 0;
10485}
10486
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010487static int perf_event_nop_int(struct perf_event *event, u64 value)
10488{
10489 return 0;
10490}
10491
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010492static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010493
10494static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010495{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010496 __this_cpu_write(nop_txn_flags, flags);
10497
10498 if (flags & ~PERF_PMU_TXN_ADD)
10499 return;
10500
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010501 perf_pmu_disable(pmu);
10502}
10503
10504static int perf_pmu_commit_txn(struct pmu *pmu)
10505{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010506 unsigned int flags = __this_cpu_read(nop_txn_flags);
10507
10508 __this_cpu_write(nop_txn_flags, 0);
10509
10510 if (flags & ~PERF_PMU_TXN_ADD)
10511 return 0;
10512
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010513 perf_pmu_enable(pmu);
10514 return 0;
10515}
10516
10517static void perf_pmu_cancel_txn(struct pmu *pmu)
10518{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010519 unsigned int flags = __this_cpu_read(nop_txn_flags);
10520
10521 __this_cpu_write(nop_txn_flags, 0);
10522
10523 if (flags & ~PERF_PMU_TXN_ADD)
10524 return;
10525
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010526 perf_pmu_enable(pmu);
10527}
10528
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010529static int perf_event_idx_default(struct perf_event *event)
10530{
Peter Zijlstrac719f562014-10-21 11:10:21 +020010531 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010532}
10533
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010534/*
10535 * Ensures all contexts with the same task_ctx_nr have the same
10536 * pmu_cpu_context too.
10537 */
Mark Rutland9e317042014-02-10 17:44:18 +000010538static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010539{
10540 struct pmu *pmu;
10541
10542 if (ctxn < 0)
10543 return NULL;
10544
10545 list_for_each_entry(pmu, &pmus, entry) {
10546 if (pmu->task_ctx_nr == ctxn)
10547 return pmu->pmu_cpu_context;
10548 }
10549
10550 return NULL;
10551}
10552
Peter Zijlstra51676952010-12-07 14:18:20 +010010553static void free_pmu_context(struct pmu *pmu)
10554{
Will Deacondf0062b2017-10-03 15:20:50 +010010555 /*
10556 * Static contexts such as perf_sw_context have a global lifetime
10557 * and may be shared between different PMUs. Avoid freeing them
10558 * when a single PMU is going away.
10559 */
10560 if (pmu->task_ctx_nr > perf_invalid_context)
10561 return;
10562
Peter Zijlstra51676952010-12-07 14:18:20 +010010563 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010564}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010565
10566/*
10567 * Let userspace know that this PMU supports address range filtering:
10568 */
10569static ssize_t nr_addr_filters_show(struct device *dev,
10570 struct device_attribute *attr,
10571 char *page)
10572{
10573 struct pmu *pmu = dev_get_drvdata(dev);
10574
10575 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
10576}
10577DEVICE_ATTR_RO(nr_addr_filters);
10578
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010579static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010580
Peter Zijlstraabe43402010-11-17 23:17:37 +010010581static ssize_t
10582type_show(struct device *dev, struct device_attribute *attr, char *page)
10583{
10584 struct pmu *pmu = dev_get_drvdata(dev);
10585
10586 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
10587}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010588static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010589
Stephane Eranian62b85632013-04-03 14:21:34 +020010590static ssize_t
10591perf_event_mux_interval_ms_show(struct device *dev,
10592 struct device_attribute *attr,
10593 char *page)
10594{
10595 struct pmu *pmu = dev_get_drvdata(dev);
10596
10597 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
10598}
10599
Peter Zijlstra272325c2015-04-15 11:41:58 +020010600static DEFINE_MUTEX(mux_interval_mutex);
10601
Stephane Eranian62b85632013-04-03 14:21:34 +020010602static ssize_t
10603perf_event_mux_interval_ms_store(struct device *dev,
10604 struct device_attribute *attr,
10605 const char *buf, size_t count)
10606{
10607 struct pmu *pmu = dev_get_drvdata(dev);
10608 int timer, cpu, ret;
10609
10610 ret = kstrtoint(buf, 0, &timer);
10611 if (ret)
10612 return ret;
10613
10614 if (timer < 1)
10615 return -EINVAL;
10616
10617 /* same value, noting to do */
10618 if (timer == pmu->hrtimer_interval_ms)
10619 return count;
10620
Peter Zijlstra272325c2015-04-15 11:41:58 +020010621 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010622 pmu->hrtimer_interval_ms = timer;
10623
10624 /* update all cpuctx for this PMU */
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010625 cpus_read_lock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010626 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +020010627 struct perf_cpu_context *cpuctx;
10628 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
10629 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
10630
Peter Zijlstra272325c2015-04-15 11:41:58 +020010631 cpu_function_call(cpu,
10632 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +020010633 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010634 cpus_read_unlock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010635 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010636
10637 return count;
10638}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010639static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +020010640
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010641static struct attribute *pmu_dev_attrs[] = {
10642 &dev_attr_type.attr,
10643 &dev_attr_perf_event_mux_interval_ms.attr,
10644 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010645};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010646ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010647
10648static int pmu_bus_running;
10649static struct bus_type pmu_bus = {
10650 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010651 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010652};
10653
10654static void pmu_dev_release(struct device *dev)
10655{
10656 kfree(dev);
10657}
10658
10659static int pmu_dev_alloc(struct pmu *pmu)
10660{
10661 int ret = -ENOMEM;
10662
10663 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
10664 if (!pmu->dev)
10665 goto out;
10666
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +010010667 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +010010668 device_initialize(pmu->dev);
10669 ret = dev_set_name(pmu->dev, "%s", pmu->name);
10670 if (ret)
10671 goto free_dev;
10672
10673 dev_set_drvdata(pmu->dev, pmu);
10674 pmu->dev->bus = &pmu_bus;
10675 pmu->dev->release = pmu_dev_release;
10676 ret = device_add(pmu->dev);
10677 if (ret)
10678 goto free_dev;
10679
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010680 /* For PMUs with address filters, throw in an extra attribute: */
10681 if (pmu->nr_addr_filters)
10682 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
10683
10684 if (ret)
10685 goto del_dev;
10686
Jiri Olsaf3a3a822019-05-12 17:55:11 +020010687 if (pmu->attr_update)
10688 ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update);
10689
10690 if (ret)
10691 goto del_dev;
10692
Peter Zijlstraabe43402010-11-17 23:17:37 +010010693out:
10694 return ret;
10695
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010696del_dev:
10697 device_del(pmu->dev);
10698
Peter Zijlstraabe43402010-11-17 23:17:37 +010010699free_dev:
10700 put_device(pmu->dev);
10701 goto out;
10702}
10703
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010704static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010705static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010706
Mischa Jonker03d8e802013-06-04 11:45:48 +020010707int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010708{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010709 int cpu, ret, max = PERF_TYPE_MAX;
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010710
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010711 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010712 ret = -ENOMEM;
10713 pmu->pmu_disable_count = alloc_percpu(int);
10714 if (!pmu->pmu_disable_count)
10715 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010716
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010717 pmu->type = -1;
10718 if (!name)
10719 goto skip_type;
10720 pmu->name = name;
10721
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010722 if (type != PERF_TYPE_SOFTWARE) {
10723 if (type >= 0)
10724 max = type;
10725
10726 ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
10727 if (ret < 0)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010728 goto free_pdc;
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010729
10730 WARN_ON(type >= 0 && ret != type);
10731
10732 type = ret;
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010733 }
10734 pmu->type = type;
10735
Peter Zijlstraabe43402010-11-17 23:17:37 +010010736 if (pmu_bus_running) {
10737 ret = pmu_dev_alloc(pmu);
10738 if (ret)
10739 goto free_idr;
10740 }
10741
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010742skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +010010743 if (pmu->task_ctx_nr == perf_hw_context) {
10744 static int hw_context_taken = 0;
10745
Mark Rutland5101ef22016-04-26 11:33:46 +010010746 /*
10747 * Other than systems with heterogeneous CPUs, it never makes
10748 * sense for two PMUs to share perf_hw_context. PMUs which are
10749 * uncore must use perf_invalid_context.
10750 */
10751 if (WARN_ON_ONCE(hw_context_taken &&
10752 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +010010753 pmu->task_ctx_nr = perf_invalid_context;
10754
10755 hw_context_taken = 1;
10756 }
10757
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010758 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
10759 if (pmu->pmu_cpu_context)
10760 goto got_cpu_context;
10761
Wei Yongjunc4814202013-04-12 11:05:54 +080010762 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010763 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
10764 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +010010765 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010766
10767 for_each_possible_cpu(cpu) {
10768 struct perf_cpu_context *cpuctx;
10769
10770 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +020010771 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010772 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010773 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010774 cpuctx->ctx.pmu = pmu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010775 cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);
Stephane Eranian9e630202013-04-03 14:21:33 +020010776
Peter Zijlstra272325c2015-04-15 11:41:58 +020010777 __perf_mux_hrtimer_init(cpuctx, cpu);
Ian Rogers836196be2020-02-13 23:51:31 -080010778
10779 cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default);
10780 cpuctx->heap = cpuctx->heap_default;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010781 }
10782
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010783got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010784 if (!pmu->start_txn) {
10785 if (pmu->pmu_enable) {
10786 /*
10787 * If we have pmu_enable/pmu_disable calls, install
10788 * transaction stubs that use that to try and batch
10789 * hardware accesses.
10790 */
10791 pmu->start_txn = perf_pmu_start_txn;
10792 pmu->commit_txn = perf_pmu_commit_txn;
10793 pmu->cancel_txn = perf_pmu_cancel_txn;
10794 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010795 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010796 pmu->commit_txn = perf_pmu_nop_int;
10797 pmu->cancel_txn = perf_pmu_nop_void;
10798 }
10799 }
10800
10801 if (!pmu->pmu_enable) {
10802 pmu->pmu_enable = perf_pmu_nop_void;
10803 pmu->pmu_disable = perf_pmu_nop_void;
10804 }
10805
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010806 if (!pmu->check_period)
10807 pmu->check_period = perf_event_nop_int;
10808
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010809 if (!pmu->event_idx)
10810 pmu->event_idx = perf_event_idx_default;
10811
Liang, Kand44f8212019-10-22 11:13:09 +020010812 /*
10813 * Ensure the TYPE_SOFTWARE PMUs are at the head of the list,
10814 * since these cannot be in the IDR. This way the linear search
10815 * is fast, provided a valid software event is provided.
10816 */
10817 if (type == PERF_TYPE_SOFTWARE || !name)
10818 list_add_rcu(&pmu->entry, &pmus);
10819 else
10820 list_add_tail_rcu(&pmu->entry, &pmus);
10821
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010822 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010823 ret = 0;
10824unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010825 mutex_unlock(&pmus_lock);
10826
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010827 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010828
Peter Zijlstraabe43402010-11-17 23:17:37 +010010829free_dev:
10830 device_del(pmu->dev);
10831 put_device(pmu->dev);
10832
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010833free_idr:
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010834 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010835 idr_remove(&pmu_idr, pmu->type);
10836
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010837free_pdc:
10838 free_percpu(pmu->pmu_disable_count);
10839 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010840}
Yan, Zhengc464c762014-03-18 16:56:41 +080010841EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010842
10843void perf_pmu_unregister(struct pmu *pmu)
10844{
10845 mutex_lock(&pmus_lock);
10846 list_del_rcu(&pmu->entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010847
10848 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +020010849 * We dereference the pmu list under both SRCU and regular RCU, so
10850 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010851 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010852 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +020010853 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010854
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010855 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010856 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010857 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010858 if (pmu_bus_running) {
Jiri Olsa09338402016-10-20 13:10:11 +020010859 if (pmu->nr_addr_filters)
10860 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
10861 device_del(pmu->dev);
10862 put_device(pmu->dev);
10863 }
Peter Zijlstra51676952010-12-07 14:18:20 +010010864 free_pmu_context(pmu);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010865 mutex_unlock(&pmus_lock);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010866}
Yan, Zhengc464c762014-03-18 16:56:41 +080010867EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010868
Kan Liange321d022019-05-28 15:08:30 -070010869static inline bool has_extended_regs(struct perf_event *event)
10870{
10871 return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) ||
10872 (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK);
10873}
10874
Mark Rutlandcc34b982015-01-07 14:56:51 +000010875static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
10876{
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010877 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +000010878 int ret;
10879
10880 if (!try_module_get(pmu->module))
10881 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010882
Peter Zijlstra0c7296c2018-01-09 21:23:02 +010010883 /*
10884 * A number of pmu->event_init() methods iterate the sibling_list to,
10885 * for example, validate if the group fits on the PMU. Therefore,
10886 * if this is a sibling event, acquire the ctx->mutex to protect
10887 * the sibling_list.
10888 */
10889 if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +020010890 /*
10891 * This ctx->mutex can nest when we're called through
10892 * inheritance. See the perf_event_ctx_lock_nested() comment.
10893 */
10894 ctx = perf_event_ctx_lock_nested(event->group_leader,
10895 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010896 BUG_ON(!ctx);
10897 }
10898
Mark Rutlandcc34b982015-01-07 14:56:51 +000010899 event->pmu = pmu;
10900 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010901
10902 if (ctx)
10903 perf_event_ctx_unlock(event->group_leader, ctx);
10904
Andrew Murraycc6795a2019-01-10 13:53:25 +000010905 if (!ret) {
Kan Liange321d022019-05-28 15:08:30 -070010906 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
10907 has_extended_regs(event))
10908 ret = -EOPNOTSUPP;
10909
Andrew Murraycc6795a2019-01-10 13:53:25 +000010910 if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
Kan Liange321d022019-05-28 15:08:30 -070010911 event_has_any_exclude_flag(event))
Andrew Murraycc6795a2019-01-10 13:53:25 +000010912 ret = -EINVAL;
Kan Liange321d022019-05-28 15:08:30 -070010913
10914 if (ret && event->destroy)
10915 event->destroy(event);
Andrew Murraycc6795a2019-01-10 13:53:25 +000010916 }
10917
Mark Rutlandcc34b982015-01-07 14:56:51 +000010918 if (ret)
10919 module_put(pmu->module);
10920
10921 return ret;
10922}
10923
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010924static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010925{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010926 int idx, type, ret;
Dan Carpenter85c617a2017-05-22 12:03:49 +030010927 struct pmu *pmu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010928
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010929 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010930
Kan Liang40999312017-01-18 08:21:01 -050010931 /* Try parent's PMU first: */
10932 if (event->parent && event->parent->pmu) {
10933 pmu = event->parent->pmu;
10934 ret = perf_try_init_event(pmu, event);
10935 if (!ret)
10936 goto unlock;
10937 }
10938
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010939 /*
10940 * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
10941 * are often aliases for PERF_TYPE_RAW.
10942 */
10943 type = event->attr.type;
10944 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
10945 type = PERF_TYPE_RAW;
10946
10947again:
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010948 rcu_read_lock();
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010949 pmu = idr_find(&pmu_idr, type);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010950 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +080010951 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010952 ret = perf_try_init_event(pmu, event);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010953 if (ret == -ENOENT && event->attr.type != type) {
10954 type = event->attr.type;
10955 goto again;
10956 }
10957
Lin Ming940c5b22011-02-27 21:13:31 +080010958 if (ret)
10959 pmu = ERR_PTR(ret);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010960
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010961 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +080010962 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010963
Sebastian Andrzej Siewior9f0bff12019-11-19 13:14:29 +010010964 list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010965 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010966 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010967 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010968
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010969 if (ret != -ENOENT) {
10970 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010971 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010972 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010973 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010974 pmu = ERR_PTR(-ENOENT);
10975unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010976 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010977
10978 return pmu;
10979}
10980
Kan Liangf2fb6be2016-03-23 11:24:37 -070010981static void attach_sb_event(struct perf_event *event)
10982{
10983 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
10984
10985 raw_spin_lock(&pel->lock);
10986 list_add_rcu(&event->sb_list, &pel->list);
10987 raw_spin_unlock(&pel->lock);
10988}
10989
Peter Zijlstraaab5b712016-05-12 17:26:46 +020010990/*
10991 * We keep a list of all !task (and therefore per-cpu) events
10992 * that need to receive side-band records.
10993 *
10994 * This avoids having to scan all the various PMU per-cpu contexts
10995 * looking for them.
10996 */
Kan Liangf2fb6be2016-03-23 11:24:37 -070010997static void account_pmu_sb_event(struct perf_event *event)
10998{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -070010999 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -070011000 attach_sb_event(event);
11001}
11002
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011003static void account_event_cpu(struct perf_event *event, int cpu)
11004{
11005 if (event->parent)
11006 return;
11007
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011008 if (is_cgroup_event(event))
11009 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
11010}
11011
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020011012/* Freq events need the tick to stay alive (see perf_event_task_tick). */
11013static void account_freq_event_nohz(void)
11014{
11015#ifdef CONFIG_NO_HZ_FULL
11016 /* Lock so we don't race with concurrent unaccount */
11017 spin_lock(&nr_freq_lock);
11018 if (atomic_inc_return(&nr_freq_events) == 1)
11019 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
11020 spin_unlock(&nr_freq_lock);
11021#endif
11022}
11023
11024static void account_freq_event(void)
11025{
11026 if (tick_nohz_full_enabled())
11027 account_freq_event_nohz();
11028 else
11029 atomic_inc(&nr_freq_events);
11030}
11031
11032
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011033static void account_event(struct perf_event *event)
11034{
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011035 bool inc = false;
11036
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011037 if (event->parent)
11038 return;
11039
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011040 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011041 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011042 if (event->attr.mmap || event->attr.mmap_data)
11043 atomic_inc(&nr_mmap_events);
11044 if (event->attr.comm)
11045 atomic_inc(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +053011046 if (event->attr.namespaces)
11047 atomic_inc(&nr_namespaces_events);
Namhyung Kim96aaab62020-03-25 21:45:28 +090011048 if (event->attr.cgroup)
11049 atomic_inc(&nr_cgroup_events);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011050 if (event->attr.task)
11051 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020011052 if (event->attr.freq)
11053 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +030011054 if (event->attr.context_switch) {
11055 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011056 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +030011057 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011058 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011059 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011060 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011061 inc = true;
Song Liu76193a92019-01-17 08:15:13 -080011062 if (event->attr.ksymbol)
11063 atomic_inc(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -080011064 if (event->attr.bpf_event)
11065 atomic_inc(&nr_bpf_events);
Adrian Huntere17d43b2020-05-12 15:19:08 +030011066 if (event->attr.text_poke)
11067 atomic_inc(&nr_text_poke_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010011068
Peter Zijlstra9107c892016-02-24 18:45:45 +010011069 if (inc) {
Alexander Shishkin5bce9db2017-08-29 17:01:03 +030011070 /*
11071 * We need the mutex here because static_branch_enable()
11072 * must complete *before* the perf_sched_count increment
11073 * becomes visible.
11074 */
Peter Zijlstra9107c892016-02-24 18:45:45 +010011075 if (atomic_inc_not_zero(&perf_sched_count))
11076 goto enabled;
11077
11078 mutex_lock(&perf_sched_mutex);
11079 if (!atomic_read(&perf_sched_count)) {
11080 static_branch_enable(&perf_sched_events);
11081 /*
11082 * Guarantee that all CPUs observe they key change and
11083 * call the perf scheduling hooks before proceeding to
11084 * install events that need them.
11085 */
Paul E. McKenney0809d9542018-11-06 19:20:05 -080011086 synchronize_rcu();
Peter Zijlstra9107c892016-02-24 18:45:45 +010011087 }
11088 /*
11089 * Now that we have waited for the sync_sched(), allow further
11090 * increments to by-pass the mutex.
11091 */
11092 atomic_inc(&perf_sched_count);
11093 mutex_unlock(&perf_sched_mutex);
11094 }
11095enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011096
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020011097 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -070011098
11099 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020011100}
11101
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011102/*
Tobias Tefke788faab2018-07-09 12:57:15 +020011103 * Allocate and initialize an event structure
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011104 */
11105static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011106perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011107 struct task_struct *task,
11108 struct perf_event *group_leader,
11109 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +030011110 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +000011111 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011112{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +020011113 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011114 struct perf_event *event;
11115 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011116 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011117
Oleg Nesterov66832eb2011-01-18 17:10:32 +010011118 if ((unsigned)cpu >= nr_cpu_ids) {
11119 if (!task || cpu != -1)
11120 return ERR_PTR(-EINVAL);
11121 }
11122
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011123 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011124 if (!event)
11125 return ERR_PTR(-ENOMEM);
11126
11127 /*
11128 * Single events are their own group leaders, with an
11129 * empty sibling list:
11130 */
11131 if (!group_leader)
11132 group_leader = event;
11133
11134 mutex_init(&event->child_mutex);
11135 INIT_LIST_HEAD(&event->child_list);
11136
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011137 INIT_LIST_HEAD(&event->event_entry);
11138 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra66681282017-11-13 14:28:38 +010011139 INIT_LIST_HEAD(&event->active_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +030011140 init_event_group(event);
Peter Zijlstra10c6db12011-11-26 02:47:31 +010011141 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +010011142 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011143 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +010011144 INIT_HLIST_NODE(&event->hlist_entry);
11145
Peter Zijlstra10c6db12011-11-26 02:47:31 +010011146
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011147 init_waitqueue_head(&event->waitq);
Peter Zijlstra1d54ad92019-04-04 15:03:00 +020011148 event->pending_disable = -1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +080011149 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011150
11151 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011152 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011153
Al Viroa6fa9412012-08-20 14:59:25 +010011154 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011155 event->cpu = cpu;
11156 event->attr = *attr;
11157 event->group_leader = group_leader;
11158 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011159 event->oncpu = -1;
11160
11161 event->parent = parent_event;
11162
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080011163 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011164 event->id = atomic64_inc_return(&perf_event_id);
11165
11166 event->state = PERF_EVENT_STATE_INACTIVE;
11167
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011168 if (task) {
11169 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011170 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +010011171 * XXX pmu::event_init needs to know what task to account to
11172 * and we cannot use the ctx information because we need the
11173 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011174 */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -070011175 event->hw.target = get_task_struct(task);
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011176 }
11177
Peter Zijlstra34f43922015-02-20 14:05:38 +010011178 event->clock = &local_clock;
11179 if (parent_event)
11180 event->clock = parent_event->clock;
11181
Avi Kivity4dc0da82011-06-29 18:42:35 +030011182 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +010011183 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030011184 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +020011185#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011186 if (overflow_handler == bpf_overflow_handler) {
Andrii Nakryiko85192db2019-11-17 09:28:03 -080011187 struct bpf_prog *prog = parent_event->prog;
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011188
Andrii Nakryiko85192db2019-11-17 09:28:03 -080011189 bpf_prog_inc(prog);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011190 event->prog = prog;
11191 event->orig_overflow_handler =
11192 parent_event->orig_overflow_handler;
11193 }
11194#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +030011195 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +010011196
Wang Nan18794452016-03-28 06:41:30 +000011197 if (overflow_handler) {
11198 event->overflow_handler = overflow_handler;
11199 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +000011200 } else if (is_write_backward(event)){
11201 event->overflow_handler = perf_event_output_backward;
11202 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +000011203 } else {
Wang Nan9ecda412016-04-05 14:11:18 +000011204 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +000011205 event->overflow_handler_context = NULL;
11206 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +020011207
Jiri Olsa0231bb52013-02-01 11:23:45 +010011208 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011209
11210 pmu = NULL;
11211
11212 hwc = &event->hw;
11213 hwc->sample_period = attr->sample_period;
11214 if (attr->freq && attr->sample_freq)
11215 hwc->sample_period = 1;
11216 hwc->last_period = hwc->sample_period;
11217
Peter Zijlstrae7850592010-05-21 14:43:08 +020011218 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011219
11220 /*
Peter Zijlstraba5213a2017-05-30 11:45:12 +020011221 * We currently do not support PERF_SAMPLE_READ on inherited events.
11222 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011223 */
Peter Zijlstraba5213a2017-05-30 11:45:12 +020011224 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011225 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011226
Yan, Zhenga46a2302014-11-04 21:56:06 -050011227 if (!has_branch_stack(event))
11228 event->attr.branch_sample_type = 0;
11229
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020011230 pmu = perf_init_event(event);
Dan Carpenter85c617a2017-05-22 12:03:49 +030011231 if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011232 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011233 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011234 }
11235
Peter Zijlstra09f4e8f2019-11-06 12:51:04 +010011236 /*
11237 * Disallow uncore-cgroup events, they don't make sense as the cgroup will
11238 * be different on other CPUs in the uncore mask.
11239 */
11240 if (pmu->task_ctx_nr == perf_invalid_context && cgroup_fd != -1) {
11241 err = -EINVAL;
11242 goto err_pmu;
11243 }
11244
Alexander Shishkinab437622019-08-06 11:46:00 +030011245 if (event->attr.aux_output &&
11246 !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) {
11247 err = -EOPNOTSUPP;
11248 goto err_pmu;
11249 }
11250
Peter Zijlstra98add2a2020-02-13 23:51:28 -080011251 if (cgroup_fd != -1) {
11252 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
11253 if (err)
11254 goto err_pmu;
11255 }
11256
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011257 err = exclusive_event_init(event);
11258 if (err)
11259 goto err_pmu;
11260
Alexander Shishkin375637b2016-04-27 18:44:46 +030011261 if (has_addr_filter(event)) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011262 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
11263 sizeof(struct perf_addr_filter_range),
11264 GFP_KERNEL);
11265 if (!event->addr_filter_ranges) {
Dan Carpenter36cc2b92017-05-22 12:04:18 +030011266 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +030011267 goto err_per_task;
Dan Carpenter36cc2b92017-05-22 12:04:18 +030011268 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030011269
Alexander Shishkin18736ee2019-02-15 13:56:54 +020011270 /*
11271 * Clone the parent's vma offsets: they are valid until exec()
11272 * even if the mm is not shared with the parent.
11273 */
11274 if (event->parent) {
11275 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
11276
11277 raw_spin_lock_irq(&ifh->lock);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011278 memcpy(event->addr_filter_ranges,
11279 event->parent->addr_filter_ranges,
11280 pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range));
Alexander Shishkin18736ee2019-02-15 13:56:54 +020011281 raw_spin_unlock_irq(&ifh->lock);
11282 }
11283
Alexander Shishkin375637b2016-04-27 18:44:46 +030011284 /* force hw sync on the address filters */
11285 event->addr_filters_gen = 1;
11286 }
11287
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011288 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +020011289 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -030011290 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011291 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +030011292 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +010011293 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011294 }
11295
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011296 err = security_perf_event_alloc(event);
11297 if (err)
11298 goto err_callchain_buffer;
11299
Alexander Shishkin927a5572016-03-02 13:24:14 +020011300 /* symmetric to unaccount_event() in _free_event() */
11301 account_event(event);
11302
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011303 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011304
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011305err_callchain_buffer:
11306 if (!event->parent) {
11307 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
11308 put_callchain_buffers();
11309 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030011310err_addr_filters:
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011311 kfree(event->addr_filter_ranges);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011312
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011313err_per_task:
11314 exclusive_event_destroy(event);
11315
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011316err_pmu:
Peter Zijlstra98add2a2020-02-13 23:51:28 -080011317 if (is_cgroup_event(event))
11318 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011319 if (event->destroy)
11320 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +080011321 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011322err_ns:
11323 if (event->ns)
11324 put_pid_ns(event->ns);
Prashant Bhole621b6d22018-04-09 19:03:46 +090011325 if (event->hw.target)
11326 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011327 kfree(event);
11328
11329 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011330}
11331
11332static int perf_copy_attr(struct perf_event_attr __user *uattr,
11333 struct perf_event_attr *attr)
11334{
11335 u32 size;
11336 int ret;
11337
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011338 /* Zero the full structure, so that a short copy will be nice. */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011339 memset(attr, 0, sizeof(*attr));
11340
11341 ret = get_user(size, &uattr->size);
11342 if (ret)
11343 return ret;
11344
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011345 /* ABI compatibility quirk: */
11346 if (!size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011347 size = PERF_ATTR_SIZE_VER0;
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011348 if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011349 goto err_size;
11350
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011351 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
11352 if (ret) {
11353 if (ret == -E2BIG)
11354 goto err_size;
11355 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011356 }
11357
Meng Xuf12f42a2017-08-23 17:07:50 -040011358 attr->size = size;
11359
Alexander Shishkina4faf002019-10-25 17:08:33 +030011360 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011361 return -EINVAL;
11362
11363 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
11364 return -EINVAL;
11365
11366 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
11367 return -EINVAL;
11368
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011369 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
11370 u64 mask = attr->branch_sample_type;
11371
11372 /* only using defined bits */
11373 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
11374 return -EINVAL;
11375
11376 /* at least one branch bit must be set */
11377 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
11378 return -EINVAL;
11379
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011380 /* propagate priv level, when not set for branch */
11381 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
11382
11383 /* exclude_kernel checked on syscall entry */
11384 if (!attr->exclude_kernel)
11385 mask |= PERF_SAMPLE_BRANCH_KERNEL;
11386
11387 if (!attr->exclude_user)
11388 mask |= PERF_SAMPLE_BRANCH_USER;
11389
11390 if (!attr->exclude_hv)
11391 mask |= PERF_SAMPLE_BRANCH_HV;
11392 /*
11393 * adjust user setting (for HW filter setup)
11394 */
11395 attr->branch_sample_type = mask;
11396 }
Stephane Eraniane7122092013-06-06 11:02:04 +020011397 /* privileged levels capture (kernel, hv): check permissions */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011398 if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
11399 ret = perf_allow_kernel(attr);
11400 if (ret)
11401 return ret;
11402 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011403 }
Jiri Olsa40189942012-08-07 15:20:37 +020011404
Jiri Olsac5ebced2012-08-07 15:20:40 +020011405 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +020011406 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +020011407 if (ret)
11408 return ret;
11409 }
11410
11411 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
11412 if (!arch_perf_have_user_stack_dump())
11413 return -ENOSYS;
11414
11415 /*
11416 * We have __u32 type for the size, but so far
11417 * we can only use __u16 as maximum due to the
11418 * __u16 sample size limit.
11419 */
11420 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa78b562f2018-04-15 11:23:50 +020011421 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020011422 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa78b562f2018-04-15 11:23:50 +020011423 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020011424 }
Jiri Olsa40189942012-08-07 15:20:37 +020011425
Jiri Olsa5f970522018-03-12 14:45:46 +010011426 if (!attr->sample_max_stack)
11427 attr->sample_max_stack = sysctl_perf_event_max_stack;
11428
Stephane Eranian60e23642014-09-24 13:48:37 +020011429 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
11430 ret = perf_reg_validate(attr->sample_regs_intr);
Namhyung Kim6546b192020-03-25 21:45:29 +090011431
11432#ifndef CONFIG_CGROUP_PERF
11433 if (attr->sample_type & PERF_SAMPLE_CGROUP)
11434 return -EINVAL;
11435#endif
11436
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011437out:
11438 return ret;
11439
11440err_size:
11441 put_user(sizeof(*attr), &uattr->size);
11442 ret = -E2BIG;
11443 goto out;
11444}
11445
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011446static int
11447perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011448{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -050011449 struct perf_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011450 int ret = -EINVAL;
11451
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011452 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011453 goto set;
11454
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011455 /* don't allow circular references */
11456 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011457 goto out;
11458
Peter Zijlstra0f139302010-05-20 14:35:15 +020011459 /*
11460 * Don't allow cross-cpu buffers
11461 */
11462 if (output_event->cpu != event->cpu)
11463 goto out;
11464
11465 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +020011466 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +020011467 */
11468 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
11469 goto out;
11470
Peter Zijlstra34f43922015-02-20 14:05:38 +010011471 /*
11472 * Mixing clocks in the same buffer is trouble you don't need.
11473 */
11474 if (output_event->clock != event->clock)
11475 goto out;
11476
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011477 /*
Wang Nan9ecda412016-04-05 14:11:18 +000011478 * Either writing ring buffer from beginning or from end.
11479 * Mixing is not allowed.
11480 */
11481 if (is_write_backward(output_event) != is_write_backward(event))
11482 goto out;
11483
11484 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011485 * If both events generate aux data, they must be on the same PMU
11486 */
11487 if (has_aux(event) && has_aux(output_event) &&
11488 event->pmu != output_event->pmu)
11489 goto out;
11490
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011491set:
11492 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011493 /* Can't redirect output if we've got an active mmap() */
11494 if (atomic_read(&event->mmap_count))
11495 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011496
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011497 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +020011498 /* get the rb we want to redirect to */
11499 rb = ring_buffer_get(output_event);
11500 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011501 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011502 }
11503
Peter Zijlstrab69cf532014-03-14 10:50:33 +010011504 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +020011505
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011506 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011507unlock:
11508 mutex_unlock(&event->mmap_mutex);
11509
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011510out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011511 return ret;
11512}
11513
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011514static void mutex_lock_double(struct mutex *a, struct mutex *b)
11515{
11516 if (b < a)
11517 swap(a, b);
11518
11519 mutex_lock(a);
11520 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
11521}
11522
Peter Zijlstra34f43922015-02-20 14:05:38 +010011523static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
11524{
11525 bool nmi_safe = false;
11526
11527 switch (clk_id) {
11528 case CLOCK_MONOTONIC:
11529 event->clock = &ktime_get_mono_fast_ns;
11530 nmi_safe = true;
11531 break;
11532
11533 case CLOCK_MONOTONIC_RAW:
11534 event->clock = &ktime_get_raw_fast_ns;
11535 nmi_safe = true;
11536 break;
11537
11538 case CLOCK_REALTIME:
11539 event->clock = &ktime_get_real_ns;
11540 break;
11541
11542 case CLOCK_BOOTTIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011543 event->clock = &ktime_get_boottime_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011544 break;
11545
11546 case CLOCK_TAI:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011547 event->clock = &ktime_get_clocktai_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011548 break;
11549
11550 default:
11551 return -EINVAL;
11552 }
11553
11554 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
11555 return -EINVAL;
11556
11557 return 0;
11558}
11559
Peter Zijlstra321027c2017-01-11 21:09:50 +010011560/*
11561 * Variation on perf_event_ctx_lock_nested(), except we take two context
11562 * mutexes.
11563 */
11564static struct perf_event_context *
11565__perf_event_ctx_lock_double(struct perf_event *group_leader,
11566 struct perf_event_context *ctx)
11567{
11568 struct perf_event_context *gctx;
11569
11570again:
11571 rcu_read_lock();
11572 gctx = READ_ONCE(group_leader->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +020011573 if (!refcount_inc_not_zero(&gctx->refcount)) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011574 rcu_read_unlock();
11575 goto again;
11576 }
11577 rcu_read_unlock();
11578
11579 mutex_lock_double(&gctx->mutex, &ctx->mutex);
11580
11581 if (group_leader->ctx != gctx) {
11582 mutex_unlock(&ctx->mutex);
11583 mutex_unlock(&gctx->mutex);
11584 put_ctx(gctx);
11585 goto again;
11586 }
11587
11588 return gctx;
11589}
11590
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011591/**
11592 * sys_perf_event_open - open a performance event, associate it to a task/cpu
11593 *
11594 * @attr_uptr: event_id type attributes for monitoring/sampling
11595 * @pid: target pid
11596 * @cpu: target cpu
11597 * @group_fd: group leader event fd
11598 */
11599SYSCALL_DEFINE5(perf_event_open,
11600 struct perf_event_attr __user *, attr_uptr,
11601 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
11602{
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011603 struct perf_event *group_leader = NULL, *output_event = NULL;
11604 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011605 struct perf_event_attr attr;
Kees Cook3f649ab2020-06-03 13:09:38 -070011606 struct perf_event_context *ctx, *gctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011607 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -040011608 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -070011609 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011610 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -040011611 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011612 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011613 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +010011614 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +000011615 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011616
11617 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +020011618 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011619 return -EINVAL;
11620
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011621 /* Do we allow access to perf_event_open(2) ? */
11622 err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
11623 if (err)
11624 return err;
11625
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011626 err = perf_copy_attr(attr_uptr, &attr);
11627 if (err)
11628 return err;
11629
11630 if (!attr.exclude_kernel) {
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011631 err = perf_allow_kernel(&attr);
11632 if (err)
11633 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011634 }
11635
Hari Bathinie4222672017-03-08 02:11:36 +053011636 if (attr.namespaces) {
Alexey Budankov18aa1852020-04-02 11:46:24 +030011637 if (!perfmon_capable())
Hari Bathinie4222672017-03-08 02:11:36 +053011638 return -EACCES;
11639 }
11640
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011641 if (attr.freq) {
11642 if (attr.sample_freq > sysctl_perf_event_sample_rate)
11643 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +020011644 } else {
11645 if (attr.sample_period & (1ULL << 63))
11646 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011647 }
11648
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011649 /* Only privileged users can get physical addresses */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011650 if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
11651 err = perf_allow_kernel(&attr);
11652 if (err)
11653 return err;
11654 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011655
David Howellsb0c8fdc2019-08-19 17:18:00 -070011656 err = security_locked_down(LOCKDOWN_PERF);
11657 if (err && (attr.sample_type & PERF_SAMPLE_REGS_INTR))
11658 /* REGS_INTR can leak data, lockdown must prevent this */
11659 return err;
11660
11661 err = 0;
11662
Stephane Eraniane5d13672011-02-14 11:20:01 +020011663 /*
11664 * In cgroup mode, the pid argument is used to pass the fd
11665 * opened to the cgroup directory in cgroupfs. The cpu argument
11666 * designates the cpu on which to monitor threads from that
11667 * cgroup.
11668 */
11669 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
11670 return -EINVAL;
11671
Yann Droneauda21b0b32014-01-05 21:36:33 +010011672 if (flags & PERF_FLAG_FD_CLOEXEC)
11673 f_flags |= O_CLOEXEC;
11674
11675 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011676 if (event_fd < 0)
11677 return event_fd;
11678
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011679 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -040011680 err = perf_fget_light(group_fd, &group);
11681 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011682 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -040011683 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011684 if (flags & PERF_FLAG_FD_OUTPUT)
11685 output_event = group_leader;
11686 if (flags & PERF_FLAG_FD_NO_GROUP)
11687 group_leader = NULL;
11688 }
11689
Stephane Eraniane5d13672011-02-14 11:20:01 +020011690 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011691 task = find_lively_task_by_vpid(pid);
11692 if (IS_ERR(task)) {
11693 err = PTR_ERR(task);
11694 goto err_group_fd;
11695 }
11696 }
11697
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020011698 if (task && group_leader &&
11699 group_leader->attr.inherit != attr.inherit) {
11700 err = -EINVAL;
11701 goto err_task;
11702 }
11703
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011704 if (task) {
Bernd Edlinger69143032020-03-20 21:27:55 +010011705 err = mutex_lock_interruptible(&task->signal->exec_update_mutex);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011706 if (err)
Alexander Levine5aeee52017-06-03 03:39:13 +000011707 goto err_task;
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011708
11709 /*
11710 * Reuse ptrace permission checks for now.
11711 *
Bernd Edlinger69143032020-03-20 21:27:55 +010011712 * We must hold exec_update_mutex across this and any potential
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011713 * perf_install_in_context() call for this new event to
11714 * serialize against exec() altering our credentials (and the
11715 * perf_event_exit_task() that could imply).
11716 */
11717 err = -EACCES;
11718 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
11719 goto err_cred;
11720 }
11721
Matt Fleming79dff512015-01-23 18:45:42 +000011722 if (flags & PERF_FLAG_PID_CGROUP)
11723 cgroup_fd = pid;
11724
Avi Kivity4dc0da82011-06-29 18:42:35 +030011725 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000011726 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011727 if (IS_ERR(event)) {
11728 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011729 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011730 }
11731
Vince Weaver53b25332014-05-16 17:12:12 -040011732 if (is_sampling_event(event)) {
11733 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +053011734 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -040011735 goto err_alloc;
11736 }
11737 }
11738
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011739 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011740 * Special case software events and allow them to be part of
11741 * any hardware group.
11742 */
11743 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011744
Peter Zijlstra34f43922015-02-20 14:05:38 +010011745 if (attr.use_clockid) {
11746 err = perf_event_set_clock(event, attr.clockid);
11747 if (err)
11748 goto err_alloc;
11749 }
11750
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011751 if (pmu->task_ctx_nr == perf_sw_context)
11752 event->event_caps |= PERF_EV_CAP_SOFTWARE;
11753
Song Liua1150c22018-05-03 12:47:16 -070011754 if (group_leader) {
11755 if (is_software_event(event) &&
11756 !in_software_context(group_leader)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011757 /*
Song Liua1150c22018-05-03 12:47:16 -070011758 * If the event is a sw event, but the group_leader
11759 * is on hw context.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011760 *
Song Liua1150c22018-05-03 12:47:16 -070011761 * Allow the addition of software events to hw
11762 * groups, this is safe because software events
11763 * never fail to schedule.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011764 */
Song Liua1150c22018-05-03 12:47:16 -070011765 pmu = group_leader->ctx->pmu;
11766 } else if (!is_software_event(event) &&
11767 is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011768 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011769 /*
11770 * In case the group is a pure software group, and we
11771 * try to add a hardware event, move the whole group to
11772 * the hardware context.
11773 */
11774 move_group = 1;
11775 }
11776 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011777
11778 /*
11779 * Get the target context (task or percpu):
11780 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050011781 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011782 if (IS_ERR(ctx)) {
11783 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011784 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011785 }
11786
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011787 /*
11788 * Look up the group leader (we will attach this event to it):
11789 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011790 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011791 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011792
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011793 /*
11794 * Do not allow a recursive hierarchy (this new sibling
11795 * becoming part of another group-sibling):
11796 */
11797 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011798 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011799
11800 /* All events in a group should have the same clock */
11801 if (group_leader->clock != event->clock)
11802 goto err_context;
11803
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011804 /*
Mark Rutland64aee2a2017-06-22 15:41:38 +010011805 * Make sure we're both events for the same CPU;
11806 * grouping events for different CPUs is broken; since
11807 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011808 */
Mark Rutland64aee2a2017-06-22 15:41:38 +010011809 if (group_leader->cpu != event->cpu)
11810 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +010011811
Mark Rutland64aee2a2017-06-22 15:41:38 +010011812 /*
11813 * Make sure we're both on the same task, or both
11814 * per-CPU events.
11815 */
11816 if (group_leader->ctx->task != ctx->task)
11817 goto err_context;
11818
11819 /*
11820 * Do not allow to attach to a group in a different task
11821 * or CPU context. If we're moving SW events, we'll fix
11822 * this up later, so allow that.
11823 */
11824 if (!move_group && group_leader->ctx != ctx)
11825 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011826
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011827 /*
11828 * Only a group leader can be exclusive or pinned
11829 */
11830 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011831 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011832 }
11833
11834 if (output_event) {
11835 err = perf_event_set_output(event, output_event);
11836 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011837 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011838 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011839
Yann Droneauda21b0b32014-01-05 21:36:33 +010011840 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
11841 f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011842 if (IS_ERR(event_file)) {
11843 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +020011844 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011845 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -040011846 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011847
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011848 if (move_group) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011849 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
11850
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011851 if (gctx->task == TASK_TOMBSTONE) {
11852 err = -ESRCH;
11853 goto err_locked;
11854 }
Peter Zijlstra321027c2017-01-11 21:09:50 +010011855
11856 /*
11857 * Check if we raced against another sys_perf_event_open() call
11858 * moving the software group underneath us.
11859 */
11860 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
11861 /*
11862 * If someone moved the group out from under us, check
11863 * if this new event wound up on the same ctx, if so
11864 * its the regular !move_group case, otherwise fail.
11865 */
11866 if (gctx != ctx) {
11867 err = -EINVAL;
11868 goto err_locked;
11869 } else {
11870 perf_event_ctx_unlock(group_leader, gctx);
11871 move_group = 0;
11872 }
11873 }
Alexander Shishkin8a58dda2019-07-01 14:07:55 +030011874
11875 /*
11876 * Failure to create exclusive events returns -EBUSY.
11877 */
11878 err = -EBUSY;
11879 if (!exclusive_event_installable(group_leader, ctx))
11880 goto err_locked;
11881
11882 for_each_sibling_event(sibling, group_leader) {
11883 if (!exclusive_event_installable(sibling, ctx))
11884 goto err_locked;
11885 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011886 } else {
11887 mutex_lock(&ctx->mutex);
11888 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011889
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011890 if (ctx->task == TASK_TOMBSTONE) {
11891 err = -ESRCH;
11892 goto err_locked;
11893 }
11894
Peter Zijlstraa7239682015-09-09 19:06:33 +020011895 if (!perf_event_validate_size(event)) {
11896 err = -E2BIG;
11897 goto err_locked;
11898 }
11899
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011900 if (!task) {
11901 /*
11902 * Check if the @cpu we're creating an event for is online.
11903 *
11904 * We use the perf_cpu_context::ctx::mutex to serialize against
11905 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
11906 */
11907 struct perf_cpu_context *cpuctx =
11908 container_of(ctx, struct perf_cpu_context, ctx);
11909
11910 if (!cpuctx->online) {
11911 err = -ENODEV;
11912 goto err_locked;
11913 }
11914 }
11915
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011916 if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) {
11917 err = -EINVAL;
Alexander Shishkinab437622019-08-06 11:46:00 +030011918 goto err_locked;
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011919 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011920
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011921 /*
11922 * Must be under the same ctx::mutex as perf_install_in_context(),
11923 * because we need to serialize with concurrent event creation.
11924 */
11925 if (!exclusive_event_installable(event, ctx)) {
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011926 err = -EBUSY;
11927 goto err_locked;
11928 }
11929
11930 WARN_ON_ONCE(ctx->parent_ctx);
11931
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011932 /*
11933 * This is the point on no return; we cannot fail hereafter. This is
11934 * where we start modifying current state.
11935 */
11936
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011937 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011938 /*
11939 * See perf_event_ctx_lock() for comments on the details
11940 * of swizzling perf_event::ctx.
11941 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011942 perf_remove_from_context(group_leader, 0);
Peter Zijlstra279b5162017-02-16 10:28:37 +010011943 put_ctx(gctx);
Jiri Olsa0231bb52013-02-01 11:23:45 +010011944
Peter Zijlstraedb39592018-03-15 17:36:56 +010011945 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011946 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011947 put_ctx(gctx);
11948 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011949
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011950 /*
11951 * Wait for everybody to stop referencing the events through
11952 * the old lists, before installing it on new lists.
11953 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011954 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011955
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011956 /*
11957 * Install the group siblings before the group leader.
11958 *
11959 * Because a group leader will try and install the entire group
11960 * (through the sibling list, which is still in-tact), we can
11961 * end up with siblings installed in the wrong context.
11962 *
11963 * By installing siblings first we NO-OP because they're not
11964 * reachable through the group lists.
11965 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010011966 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011967 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +010011968 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011969 get_ctx(ctx);
11970 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011971
11972 /*
11973 * Removing from the context ends up with disabled
11974 * event. What we want here is event in the initial
11975 * startup state, ready to be add into new context.
11976 */
11977 perf_event__state_init(group_leader);
11978 perf_install_in_context(ctx, group_leader, group_leader->cpu);
11979 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011980 }
11981
Peter Zijlstraf73e22a2015-09-09 20:48:22 +020011982 /*
11983 * Precalculate sample_data sizes; do while holding ctx::mutex such
11984 * that we're serialized against further additions and before
11985 * perf_install_in_context() which is the point the event is active and
11986 * can use these values.
11987 */
11988 perf_event__header_size(event);
11989 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011990
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010011991 event->owner = current;
11992
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080011993 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010011994 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011995
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011996 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010011997 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011998 mutex_unlock(&ctx->mutex);
11999
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012000 if (task) {
Bernd Edlinger69143032020-03-20 21:27:55 +010012001 mutex_unlock(&task->signal->exec_update_mutex);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012002 put_task_struct(task);
12003 }
12004
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012005 mutex_lock(&current->perf_event_mutex);
12006 list_add_tail(&event->owner_entry, &current->perf_event_list);
12007 mutex_unlock(&current->perf_event_mutex);
12008
Peter Zijlstra8a495422010-05-27 15:47:49 +020012009 /*
12010 * Drop the reference on the group_event after placing the
12011 * new event on the sibling_list. This ensures destruction
12012 * of the group leader will find the pointer to itself in
12013 * perf_group_detach().
12014 */
Al Viro2903ff02012-08-28 12:52:22 -040012015 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040012016 fd_install(event_fd, event_file);
12017 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012018
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020012019err_locked:
12020 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010012021 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020012022 mutex_unlock(&ctx->mutex);
12023/* err_file: */
12024 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012025err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012026 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040012027 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020012028err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010012029 /*
12030 * If event_file is set, the fput() above will have called ->release()
12031 * and that will take care of freeing the event.
12032 */
12033 if (!event_file)
12034 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012035err_cred:
12036 if (task)
Bernd Edlinger69143032020-03-20 21:27:55 +010012037 mutex_unlock(&task->signal->exec_update_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020012038err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020012039 if (task)
12040 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020012041err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040012042 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040012043err_fd:
12044 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012045 return err;
12046}
12047
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012048/**
12049 * perf_event_create_kernel_counter
12050 *
12051 * @attr: attributes of the counter to create
12052 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070012053 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012054 */
12055struct perf_event *
12056perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070012057 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030012058 perf_overflow_handler_t overflow_handler,
12059 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012060{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012061 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012062 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012063 int err;
12064
Alexander Shishkindce5aff2019-10-30 15:47:31 +020012065 /*
12066 * Grouping is not supported for kernel events, neither is 'AUX',
12067 * make sure the caller's intentions are adjusted.
12068 */
12069 if (attr->aux_output)
12070 return ERR_PTR(-EINVAL);
12071
Avi Kivity4dc0da82011-06-29 18:42:35 +030012072 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000012073 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012074 if (IS_ERR(event)) {
12075 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012076 goto err;
12077 }
12078
Jiri Olsaf8697762014-08-01 14:33:01 +020012079 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012080 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020012081
Alexander Shishkinf25d8ba2019-10-30 15:47:30 +020012082 /*
12083 * Get the target context (task or percpu):
12084 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050012085 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012086 if (IS_ERR(ctx)) {
12087 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012088 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012089 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012090
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012091 WARN_ON_ONCE(ctx->parent_ctx);
12092 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012093 if (ctx->task == TASK_TOMBSTONE) {
12094 err = -ESRCH;
12095 goto err_unlock;
12096 }
12097
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012098 if (!task) {
12099 /*
12100 * Check if the @cpu we're creating an event for is online.
12101 *
12102 * We use the perf_cpu_context::ctx::mutex to serialize against
12103 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
12104 */
12105 struct perf_cpu_context *cpuctx =
12106 container_of(ctx, struct perf_cpu_context, ctx);
12107 if (!cpuctx->online) {
12108 err = -ENODEV;
12109 goto err_unlock;
12110 }
12111 }
12112
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012113 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012114 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012115 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012116 }
12117
Leonard Crestez4ce54af2019-07-24 15:53:24 +030012118 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012119 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012120 mutex_unlock(&ctx->mutex);
12121
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012122 return event;
12123
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012124err_unlock:
12125 mutex_unlock(&ctx->mutex);
12126 perf_unpin_context(ctx);
12127 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012128err_free:
12129 free_event(event);
12130err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012131 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012132}
12133EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
12134
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012135void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
12136{
12137 struct perf_event_context *src_ctx;
12138 struct perf_event_context *dst_ctx;
12139 struct perf_event *event, *tmp;
12140 LIST_HEAD(events);
12141
12142 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
12143 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
12144
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012145 /*
12146 * See perf_event_ctx_lock() for comments on the details
12147 * of swizzling perf_event::ctx.
12148 */
12149 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012150 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
12151 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012152 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020012153 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012154 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020012155 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012156 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012157
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012158 /*
12159 * Wait for the events to quiesce before re-instating them.
12160 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012161 synchronize_rcu();
12162
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012163 /*
12164 * Re-instate events in 2 passes.
12165 *
12166 * Skip over group leaders and only install siblings on this first
12167 * pass, siblings will not get enabled without a leader, however a
12168 * leader will enable its siblings, even if those are still on the old
12169 * context.
12170 */
12171 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
12172 if (event->group_leader == event)
12173 continue;
12174
12175 list_del(&event->migrate_entry);
12176 if (event->state >= PERF_EVENT_STATE_OFF)
12177 event->state = PERF_EVENT_STATE_INACTIVE;
12178 account_event_cpu(event, dst_cpu);
12179 perf_install_in_context(dst_ctx, event, dst_cpu);
12180 get_ctx(dst_ctx);
12181 }
12182
12183 /*
12184 * Once all the siblings are setup properly, install the group leaders
12185 * to make it go.
12186 */
Peter Zijlstra98861672013-10-03 16:02:23 +020012187 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
12188 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012189 if (event->state >= PERF_EVENT_STATE_OFF)
12190 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020012191 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012192 perf_install_in_context(dst_ctx, event, dst_cpu);
12193 get_ctx(dst_ctx);
12194 }
12195 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012196 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012197}
12198EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
12199
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012200static void sync_child_event(struct perf_event *child_event,
12201 struct task_struct *child)
12202{
12203 struct perf_event *parent_event = child_event->parent;
12204 u64 child_val;
12205
12206 if (child_event->attr.inherit_stat)
12207 perf_event_read_event(child_event, child);
12208
Peter Zijlstrab5e58792010-05-21 14:43:12 +020012209 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012210
12211 /*
12212 * Add back the child's count to the parent's count:
12213 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020012214 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012215 atomic64_add(child_event->total_time_enabled,
12216 &parent_event->child_total_time_enabled);
12217 atomic64_add(child_event->total_time_running,
12218 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012219}
12220
12221static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012222perf_event_exit_event(struct perf_event *child_event,
12223 struct perf_event_context *child_ctx,
12224 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012225{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012226 struct perf_event *parent_event = child_event->parent;
12227
Peter Zijlstra1903d502014-07-15 17:27:27 +020012228 /*
12229 * Do not destroy the 'original' grouping; because of the context
12230 * switch optimization the original events could've ended up in a
12231 * random child task.
12232 *
12233 * If we were to destroy the original group, all group related
12234 * operations would cease to function properly after this random
12235 * child dies.
12236 *
12237 * Do destroy all inherited groups, we don't care about those
12238 * and being thorough is better.
12239 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010012240 raw_spin_lock_irq(&child_ctx->lock);
12241 WARN_ON_ONCE(child_ctx->is_active);
12242
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012243 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010012244 perf_group_detach(child_event);
12245 list_del_event(child_event, child_ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +020012246 perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010012247 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012248
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012249 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012250 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012251 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012252 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040012253 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012254 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012255 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012256 /*
12257 * Child events can be cleaned up.
12258 */
12259
12260 sync_child_event(child_event, child);
12261
12262 /*
12263 * Remove this event from the parent's list
12264 */
12265 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
12266 mutex_lock(&parent_event->child_mutex);
12267 list_del_init(&child_event->child_list);
12268 mutex_unlock(&parent_event->child_mutex);
12269
12270 /*
12271 * Kick perf_poll() for is_event_hup().
12272 */
12273 perf_event_wakeup(parent_event);
12274 free_event(child_event);
12275 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012276}
12277
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012278static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012279{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012280 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012281 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012282
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012283 WARN_ON_ONCE(child != current);
12284
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012285 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012286 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012287 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012288
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012289 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012290 * In order to reduce the amount of tricky in ctx tear-down, we hold
12291 * ctx::mutex over the entire thing. This serializes against almost
12292 * everything that wants to access the ctx.
12293 *
12294 * The exception is sys_perf_event_open() /
12295 * perf_event_create_kernel_count() which does find_get_context()
12296 * without ctx::mutex (it cannot because of the move_group double mutex
12297 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012298 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012299 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012300
12301 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012302 * In a single ctx::lock section, de-schedule the events and detach the
12303 * context from the task such that we cannot ever get it scheduled back
12304 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012305 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012306 raw_spin_lock_irq(&child_ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +020012307 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020012308
12309 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012310 * Now that the context is inactive, destroy the task <-> ctx relation
12311 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012312 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012313 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
12314 put_ctx(child_ctx); /* cannot be last */
12315 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
12316 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012317
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012318 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012319 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012320
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012321 if (clone_ctx)
12322 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020012323
12324 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012325 * Report the task dead after unscheduling the events so that we
12326 * won't get any samples after PERF_RECORD_EXIT. We can however still
12327 * get a few PERF_RECORD_READ events.
12328 */
12329 perf_event_task(child, child_ctx, 0);
12330
Peter Zijlstraebf905f2014-05-29 19:00:24 +020012331 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012332 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012333
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012334 mutex_unlock(&child_ctx->mutex);
12335
12336 put_ctx(child_ctx);
12337}
12338
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012339/*
12340 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012341 *
Bernd Edlinger69143032020-03-20 21:27:55 +010012342 * Can be called with exec_update_mutex held when called from
Eric W. Biederman96ecee22020-05-03 06:48:17 -050012343 * setup_new_exec().
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012344 */
12345void perf_event_exit_task(struct task_struct *child)
12346{
Peter Zijlstra88821352010-11-09 19:01:43 +010012347 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012348 int ctxn;
12349
Peter Zijlstra88821352010-11-09 19:01:43 +010012350 mutex_lock(&child->perf_event_mutex);
12351 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
12352 owner_entry) {
12353 list_del_init(&event->owner_entry);
12354
12355 /*
12356 * Ensure the list deletion is visible before we clear
12357 * the owner, closes a race against perf_release() where
12358 * we need to serialize on the owner->perf_event_mutex.
12359 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010012360 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010012361 }
12362 mutex_unlock(&child->perf_event_mutex);
12363
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012364 for_each_task_context_nr(ctxn)
12365 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010012366
12367 /*
12368 * The perf_event_exit_task_context calls perf_event_task
12369 * with child's task_ctx, which generates EXIT events for
12370 * child contexts and sets child->perf_event_ctxp[] to NULL.
12371 * At this point we need to send EXIT events to cpu contexts.
12372 */
12373 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012374}
12375
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012376static void perf_free_event(struct perf_event *event,
12377 struct perf_event_context *ctx)
12378{
12379 struct perf_event *parent = event->parent;
12380
12381 if (WARN_ON_ONCE(!parent))
12382 return;
12383
12384 mutex_lock(&parent->child_mutex);
12385 list_del_init(&event->child_list);
12386 mutex_unlock(&parent->child_mutex);
12387
Al Viroa6fa9412012-08-20 14:59:25 +010012388 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012389
Peter Zijlstra652884f2015-01-23 11:20:10 +010012390 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020012391 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012392 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010012393 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012394 free_event(event);
12395}
12396
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012397/*
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012398 * Free a context as created by inheritance by perf_event_init_task() below,
12399 * used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010012400 *
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012401 * Even though the task has never lived, the context and events have been
12402 * exposed through the child_list, so we must take care tearing it all down.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012403 */
12404void perf_event_free_task(struct task_struct *task)
12405{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012406 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012407 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012408 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012409
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012410 for_each_task_context_nr(ctxn) {
12411 ctx = task->perf_event_ctxp[ctxn];
12412 if (!ctx)
12413 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012414
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012415 mutex_lock(&ctx->mutex);
Peter Zijlstrae552a832017-03-16 13:47:48 +010012416 raw_spin_lock_irq(&ctx->lock);
12417 /*
12418 * Destroy the task <-> ctx relation and mark the context dead.
12419 *
12420 * This is important because even though the task hasn't been
12421 * exposed yet the context has been (through child_list).
12422 */
12423 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
12424 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
12425 put_task_struct(task); /* cannot be last */
12426 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012427
Peter Zijlstra15121c72017-03-16 13:47:50 +010012428 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012429 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012430
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012431 mutex_unlock(&ctx->mutex);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012432
12433 /*
12434 * perf_event_release_kernel() could've stolen some of our
12435 * child events and still have them on its free_list. In that
12436 * case we must wait for these events to have been freed (in
12437 * particular all their references to this task must've been
12438 * dropped).
12439 *
12440 * Without this copy_process() will unconditionally free this
12441 * task (irrespective of its reference count) and
12442 * _free_event()'s put_task_struct(event->hw.target) will be a
12443 * use-after-free.
12444 *
12445 * Wait for all events to drop their context reference.
12446 */
12447 wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);
12448 put_ctx(ctx); /* must be last */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012449 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012450}
12451
Peter Zijlstra4e231c72010-09-09 21:01:59 +020012452void perf_event_delayed_put(struct task_struct *task)
12453{
12454 int ctxn;
12455
12456 for_each_task_context_nr(ctxn)
12457 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
12458}
12459
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012460struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000012461{
Al Viro02e5ad92019-06-26 20:43:53 -040012462 struct file *file = fget(fd);
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012463 if (!file)
12464 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000012465
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012466 if (file->f_op != &perf_fops) {
12467 fput(file);
12468 return ERR_PTR(-EBADF);
12469 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000012470
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012471 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000012472}
12473
Yonghong Songf8d959a2018-05-24 11:21:08 -070012474const struct perf_event *perf_get_event(struct file *file)
12475{
12476 if (file->f_op != &perf_fops)
12477 return ERR_PTR(-EINVAL);
12478
12479 return file->private_data;
12480}
12481
Kaixu Xiaffe86902015-08-06 07:02:32 +000012482const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
12483{
12484 if (!event)
12485 return ERR_PTR(-EINVAL);
12486
12487 return &event->attr;
12488}
12489
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012490/*
Tobias Tefke788faab2018-07-09 12:57:15 +020012491 * Inherit an event from parent task to child task.
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012492 *
12493 * Returns:
12494 * - valid pointer on success
12495 * - NULL for orphaned events
12496 * - IS_ERR() on error
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012497 */
12498static struct perf_event *
12499inherit_event(struct perf_event *parent_event,
12500 struct task_struct *parent,
12501 struct perf_event_context *parent_ctx,
12502 struct task_struct *child,
12503 struct perf_event *group_leader,
12504 struct perf_event_context *child_ctx)
12505{
Peter Zijlstra8ca2bd42017-09-05 14:12:35 +020012506 enum perf_event_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012507 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020012508 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012509
12510 /*
12511 * Instead of creating recursive hierarchies of events,
12512 * we link inherited events back to the original parent,
12513 * which has a filp for sure, which we use as the reference
12514 * count:
12515 */
12516 if (parent_event->parent)
12517 parent_event = parent_event->parent;
12518
12519 child_event = perf_event_alloc(&parent_event->attr,
12520 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020012521 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012522 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000012523 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012524 if (IS_ERR(child_event))
12525 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010012526
Jiri Olsa313ccb92018-01-07 17:03:47 +010012527
12528 if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) &&
12529 !child_ctx->task_ctx_data) {
12530 struct pmu *pmu = child_event->pmu;
12531
Kan Liangff9ff922020-07-03 05:49:21 -070012532 child_ctx->task_ctx_data = alloc_task_ctx_data(pmu);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012533 if (!child_ctx->task_ctx_data) {
12534 free_event(child_event);
Alexander Shishkin697d8772019-11-05 09:57:02 +020012535 return ERR_PTR(-ENOMEM);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012536 }
12537 }
12538
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012539 /*
12540 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
12541 * must be under the same lock in order to serialize against
12542 * perf_event_release_kernel(), such that either we must observe
12543 * is_orphaned_event() or they will observe us on the child_list.
12544 */
12545 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020012546 if (is_orphaned_event(parent_event) ||
12547 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012548 mutex_unlock(&parent_event->child_mutex);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012549 /* task_ctx_data is freed with child_ctx */
Al Viroa6fa9412012-08-20 14:59:25 +010012550 free_event(child_event);
12551 return NULL;
12552 }
12553
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012554 get_ctx(child_ctx);
12555
12556 /*
12557 * Make the child state follow the state of the parent event,
12558 * not its attr.disabled bit. We hold the parent's mutex,
12559 * so we won't race with perf_event_{en, dis}able_family.
12560 */
Jiri Olsa1929def2014-09-12 13:18:27 +020012561 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012562 child_event->state = PERF_EVENT_STATE_INACTIVE;
12563 else
12564 child_event->state = PERF_EVENT_STATE_OFF;
12565
12566 if (parent_event->attr.freq) {
12567 u64 sample_period = parent_event->hw.sample_period;
12568 struct hw_perf_event *hwc = &child_event->hw;
12569
12570 hwc->sample_period = sample_period;
12571 hwc->last_period = sample_period;
12572
12573 local64_set(&hwc->period_left, sample_period);
12574 }
12575
12576 child_event->ctx = child_ctx;
12577 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030012578 child_event->overflow_handler_context
12579 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012580
12581 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020012582 * Precalculate sample_data sizes
12583 */
12584 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020012585 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020012586
12587 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012588 * Link it up in the child's context:
12589 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020012590 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012591 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020012592 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012593
12594 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012595 * Link this into the parent event's child list
12596 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012597 list_add_tail(&child_event->child_list, &parent_event->child_list);
12598 mutex_unlock(&parent_event->child_mutex);
12599
12600 return child_event;
12601}
12602
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012603/*
12604 * Inherits an event group.
12605 *
12606 * This will quietly suppress orphaned events; !inherit_event() is not an error.
12607 * This matches with perf_event_release_kernel() removing all child events.
12608 *
12609 * Returns:
12610 * - 0 on success
12611 * - <0 on error
12612 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012613static int inherit_group(struct perf_event *parent_event,
12614 struct task_struct *parent,
12615 struct perf_event_context *parent_ctx,
12616 struct task_struct *child,
12617 struct perf_event_context *child_ctx)
12618{
12619 struct perf_event *leader;
12620 struct perf_event *sub;
12621 struct perf_event *child_ctr;
12622
12623 leader = inherit_event(parent_event, parent, parent_ctx,
12624 child, NULL, child_ctx);
12625 if (IS_ERR(leader))
12626 return PTR_ERR(leader);
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012627 /*
12628 * @leader can be NULL here because of is_orphaned_event(). In this
12629 * case inherit_event() will create individual events, similar to what
12630 * perf_group_detach() would do anyway.
12631 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010012632 for_each_sibling_event(sub, parent_event) {
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012633 child_ctr = inherit_event(sub, parent, parent_ctx,
12634 child, leader, child_ctx);
12635 if (IS_ERR(child_ctr))
12636 return PTR_ERR(child_ctr);
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012637
Alexander Shishkin00496fe2019-11-01 17:12:48 +020012638 if (sub->aux_event == parent_event && child_ctr &&
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012639 !perf_get_aux_event(child_ctr, leader))
12640 return -EINVAL;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012641 }
12642 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012643}
12644
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012645/*
12646 * Creates the child task context and tries to inherit the event-group.
12647 *
12648 * Clears @inherited_all on !attr.inherited or error. Note that we'll leave
12649 * inherited_all set when we 'fail' to inherit an orphaned event; this is
12650 * consistent with perf_event_release_kernel() removing all child events.
12651 *
12652 * Returns:
12653 * - 0 on success
12654 * - <0 on error
12655 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012656static int
12657inherit_task_group(struct perf_event *event, struct task_struct *parent,
12658 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012659 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012660 int *inherited_all)
12661{
12662 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012663 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012664
12665 if (!event->attr.inherit) {
12666 *inherited_all = 0;
12667 return 0;
12668 }
12669
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012670 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012671 if (!child_ctx) {
12672 /*
12673 * This is executed from the parent task context, so
12674 * inherit events that have been marked for cloning.
12675 * First allocate and initialize a context for the
12676 * child.
12677 */
Jiri Olsa734df5a2013-07-09 17:44:10 +020012678 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012679 if (!child_ctx)
12680 return -ENOMEM;
12681
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012682 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012683 }
12684
12685 ret = inherit_group(event, parent, parent_ctx,
12686 child, child_ctx);
12687
12688 if (ret)
12689 *inherited_all = 0;
12690
12691 return ret;
12692}
12693
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012694/*
12695 * Initialize the perf_event context in task_struct
12696 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020012697static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012698{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012699 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012700 struct perf_event_context *cloned_ctx;
12701 struct perf_event *event;
12702 struct task_struct *parent = current;
12703 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012704 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012705 int ret = 0;
12706
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012707 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012708 return 0;
12709
12710 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012711 * If the parent's context is a clone, pin it so it won't get
12712 * swapped under us.
12713 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012714 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020012715 if (!parent_ctx)
12716 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012717
12718 /*
12719 * No need to check if parent_ctx != NULL here; since we saw
12720 * it non-NULL earlier, the only reason for it to become NULL
12721 * is if we exit, and since we're currently in the middle of
12722 * a fork we can't be exiting at the same time.
12723 */
12724
12725 /*
12726 * Lock the parent list. No need to lock the child - not PID
12727 * hashed yet and not running, so nobody can access it.
12728 */
12729 mutex_lock(&parent_ctx->mutex);
12730
12731 /*
12732 * We dont have to disable NMIs - we are only looking at
12733 * the list, not manipulating it:
12734 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012735 perf_event_groups_for_each(event, &parent_ctx->pinned_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012736 ret = inherit_task_group(event, parent, parent_ctx,
12737 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012738 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012739 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012740 }
12741
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012742 /*
12743 * We can't hold ctx->lock when iterating the ->flexible_group list due
12744 * to allocations, but we need to prevent rotation because
12745 * rotate_ctx() will change the list from interrupt context.
12746 */
12747 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12748 parent_ctx->rotate_disable = 1;
12749 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
12750
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012751 perf_event_groups_for_each(event, &parent_ctx->flexible_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012752 ret = inherit_task_group(event, parent, parent_ctx,
12753 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012754 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012755 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012756 }
12757
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012758 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12759 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012760
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012761 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012762
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010012763 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012764 /*
12765 * Mark the child context as a clone of the parent
12766 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012767 *
12768 * Note that if the parent is a clone, the holding of
12769 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012770 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012771 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012772 if (cloned_ctx) {
12773 child_ctx->parent_ctx = cloned_ctx;
12774 child_ctx->parent_gen = parent_ctx->parent_gen;
12775 } else {
12776 child_ctx->parent_ctx = parent_ctx;
12777 child_ctx->parent_gen = parent_ctx->generation;
12778 }
12779 get_ctx(child_ctx->parent_ctx);
12780 }
12781
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012782 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012783out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012784 mutex_unlock(&parent_ctx->mutex);
12785
12786 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012787 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012788
12789 return ret;
12790}
12791
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012792/*
12793 * Initialize the perf_event context in task_struct
12794 */
12795int perf_event_init_task(struct task_struct *child)
12796{
12797 int ctxn, ret;
12798
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010012799 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
12800 mutex_init(&child->perf_event_mutex);
12801 INIT_LIST_HEAD(&child->perf_event_list);
12802
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012803 for_each_task_context_nr(ctxn) {
12804 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012805 if (ret) {
12806 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012807 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012808 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012809 }
12810
12811 return 0;
12812}
12813
Paul Mackerras220b1402010-03-10 20:45:52 +110012814static void __init perf_event_init_all_cpus(void)
12815{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012816 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110012817 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110012818
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012819 zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL);
12820
Paul Mackerras220b1402010-03-10 20:45:52 +110012821 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012822 swhash = &per_cpu(swevent_htable, cpu);
12823 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000012824 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070012825
12826 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
12827 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012828
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -080012829#ifdef CONFIG_CGROUP_PERF
12830 INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu));
12831#endif
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012832 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110012833 }
12834}
12835
Valdis Kletnieksd18bf422019-03-12 04:06:37 -040012836static void perf_swevent_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012837{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012838 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012839
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012840 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000012841 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012842 struct swevent_hlist *hlist;
12843
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012844 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
12845 WARN_ON(!hlist);
12846 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012847 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012848 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012849}
12850
Dave Young2965faa2015-09-09 15:38:55 -070012851#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012852static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012853{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012854 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012855 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
12856 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012857
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012858 raw_spin_lock(&ctx->lock);
Peter Zijlstra0ee098c2017-09-05 13:24:28 +020012859 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012860 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012861 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012862 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012863}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012864
12865static void perf_event_exit_cpu_context(int cpu)
12866{
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012867 struct perf_cpu_context *cpuctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012868 struct perf_event_context *ctx;
12869 struct pmu *pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012870
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012871 mutex_lock(&pmus_lock);
12872 list_for_each_entry(pmu, &pmus, entry) {
12873 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12874 ctx = &cpuctx->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012875
12876 mutex_lock(&ctx->mutex);
12877 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012878 cpuctx->online = 0;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012879 mutex_unlock(&ctx->mutex);
12880 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012881 cpumask_clear_cpu(cpu, perf_online_mask);
12882 mutex_unlock(&pmus_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012883}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012884#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012885
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012886static void perf_event_exit_cpu_context(int cpu) { }
12887
12888#endif
12889
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012890int perf_event_init_cpu(unsigned int cpu)
12891{
12892 struct perf_cpu_context *cpuctx;
12893 struct perf_event_context *ctx;
12894 struct pmu *pmu;
12895
12896 perf_swevent_init_cpu(cpu);
12897
12898 mutex_lock(&pmus_lock);
12899 cpumask_set_cpu(cpu, perf_online_mask);
12900 list_for_each_entry(pmu, &pmus, entry) {
12901 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12902 ctx = &cpuctx->ctx;
12903
12904 mutex_lock(&ctx->mutex);
12905 cpuctx->online = 1;
12906 mutex_unlock(&ctx->mutex);
12907 }
12908 mutex_unlock(&pmus_lock);
12909
12910 return 0;
12911}
12912
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012913int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012914{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010012915 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012916 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012917}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012918
Peter Zijlstrac2774432010-12-08 15:29:02 +010012919static int
12920perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
12921{
12922 int cpu;
12923
12924 for_each_online_cpu(cpu)
12925 perf_event_exit_cpu(cpu);
12926
12927 return NOTIFY_OK;
12928}
12929
12930/*
12931 * Run the perf reboot notifier at the very last possible moment so that
12932 * the generic watchdog code runs as long as possible.
12933 */
12934static struct notifier_block perf_reboot_notifier = {
12935 .notifier_call = perf_reboot,
12936 .priority = INT_MIN,
12937};
12938
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012939void __init perf_event_init(void)
12940{
Jason Wessel3c502e72010-11-04 17:33:01 -050012941 int ret;
12942
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012943 idr_init(&pmu_idr);
12944
Paul Mackerras220b1402010-03-10 20:45:52 +110012945 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012946 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012947 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
12948 perf_pmu_register(&perf_cpu_clock, NULL, -1);
12949 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012950 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012951 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010012952 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050012953
12954 ret = init_hw_breakpoint();
12955 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020012956
Jiri Olsab01c3a02012-03-23 15:41:20 +010012957 /*
12958 * Build time assertion that we keep the data_head at the intended
12959 * location. IOW, validation we got the __reserved[] size right.
12960 */
12961 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
12962 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012963}
Peter Zijlstraabe43402010-11-17 23:17:37 +010012964
Cody P Schaferfd979c02015-01-30 13:45:57 -080012965ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
12966 char *page)
12967{
12968 struct perf_pmu_events_attr *pmu_attr =
12969 container_of(attr, struct perf_pmu_events_attr, attr);
12970
12971 if (pmu_attr->event_str)
12972 return sprintf(page, "%s\n", pmu_attr->event_str);
12973
12974 return 0;
12975}
Thomas Gleixner675965b2016-02-22 22:19:27 +000012976EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080012977
Peter Zijlstraabe43402010-11-17 23:17:37 +010012978static int __init perf_event_sysfs_init(void)
12979{
12980 struct pmu *pmu;
12981 int ret;
12982
12983 mutex_lock(&pmus_lock);
12984
12985 ret = bus_register(&pmu_bus);
12986 if (ret)
12987 goto unlock;
12988
12989 list_for_each_entry(pmu, &pmus, entry) {
12990 if (!pmu->name || pmu->type < 0)
12991 continue;
12992
12993 ret = pmu_dev_alloc(pmu);
12994 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
12995 }
12996 pmu_bus_running = 1;
12997 ret = 0;
12998
12999unlock:
13000 mutex_unlock(&pmus_lock);
13001
13002 return ret;
13003}
13004device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020013005
13006#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040013007static struct cgroup_subsys_state *
13008perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020013009{
13010 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020013011
Li Zefan1b15d052011-03-03 14:26:06 +080013012 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020013013 if (!jc)
13014 return ERR_PTR(-ENOMEM);
13015
Stephane Eraniane5d13672011-02-14 11:20:01 +020013016 jc->info = alloc_percpu(struct perf_cgroup_info);
13017 if (!jc->info) {
13018 kfree(jc);
13019 return ERR_PTR(-ENOMEM);
13020 }
13021
Stephane Eraniane5d13672011-02-14 11:20:01 +020013022 return &jc->css;
13023}
13024
Tejun Heoeb954192013-08-08 20:11:23 -040013025static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020013026{
Tejun Heoeb954192013-08-08 20:11:23 -040013027 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
13028
Stephane Eraniane5d13672011-02-14 11:20:01 +020013029 free_percpu(jc->info);
13030 kfree(jc);
13031}
13032
Namhyung Kim96aaab62020-03-25 21:45:28 +090013033static int perf_cgroup_css_online(struct cgroup_subsys_state *css)
13034{
13035 perf_event_cgroup(css->cgroup);
13036 return 0;
13037}
13038
Stephane Eraniane5d13672011-02-14 11:20:01 +020013039static int __perf_cgroup_move(void *info)
13040{
13041 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010013042 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020013043 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010013044 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020013045 return 0;
13046}
13047
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050013048static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020013049{
Tejun Heobb9d97b2011-12-12 18:12:21 -080013050 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050013051 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080013052
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050013053 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080013054 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020013055}
13056
Tejun Heo073219e2014-02-08 10:36:58 -050013057struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080013058 .css_alloc = perf_cgroup_css_alloc,
13059 .css_free = perf_cgroup_css_free,
Namhyung Kim96aaab62020-03-25 21:45:28 +090013060 .css_online = perf_cgroup_css_online,
Tejun Heobb9d97b2011-12-12 18:12:21 -080013061 .attach = perf_cgroup_attach,
Tejun Heo968ebff2017-01-29 14:35:20 -050013062 /*
13063 * Implicitly enable on dfl hierarchy so that perf events can
13064 * always be filtered by cgroup2 path as long as perf_event
13065 * controller is not mounted on a legacy hierarchy.
13066 */
13067 .implicit_on_dfl = true,
Tejun Heo8cfd8142017-07-21 11:14:51 -040013068 .threaded = true,
Stephane Eraniane5d13672011-02-14 11:20:01 +020013069};
13070#endif /* CONFIG_CGROUP_PERF */