blob: ddcfd2fb5cc5a997649a6e3cccde032f1368339b [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;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200397
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200398static LIST_HEAD(pmus);
399static DEFINE_MUTEX(pmus_lock);
400static struct srcu_struct pmus_srcu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +0200401static cpumask_var_t perf_online_mask;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200402
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200403/*
404 * perf event paranoia level:
405 * -1 - not paranoid at all
406 * 0 - disallow raw tracepoint access for unpriv
407 * 1 - disallow cpu events for unpriv
408 * 2 - disallow kernel profiling for unpriv
409 */
Andy Lutomirski01610282016-05-09 15:48:51 -0700410int sysctl_perf_event_paranoid __read_mostly = 2;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200411
Frederic Weisbecker20443382011-03-31 03:33:29 +0200412/* Minimum for 512 kiB + 1 user control page */
413int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200414
415/*
416 * max perf event sample rate
417 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700418#define DEFAULT_MAX_SAMPLE_RATE 100000
419#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
420#define DEFAULT_CPU_TIME_MAX_PERCENT 25
421
422int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
423
424static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
425static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
426
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200427static int perf_sample_allowed_ns __read_mostly =
428 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700429
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800430static void update_perf_cpu_limits(void)
Dave Hansen14c63f12013-06-21 08:51:36 -0700431{
432 u64 tmp = perf_sample_period_ns;
433
434 tmp *= sysctl_perf_cpu_time_max_percent;
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100435 tmp = div_u64(tmp, 100);
436 if (!tmp)
437 tmp = 1;
438
439 WRITE_ONCE(perf_sample_allowed_ns, tmp);
Dave Hansen14c63f12013-06-21 08:51:36 -0700440}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100441
Peter Zijlstra8d5bce02018-03-09 14:56:27 +0100442static bool perf_rotate_context(struct perf_cpu_context *cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +0200443
Peter Zijlstra163ec432011-02-16 11:22:34 +0100444int perf_proc_update_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200445 void *buffer, size_t *lenp, loff_t *ppos)
Peter Zijlstra163ec432011-02-16 11:22:34 +0100446{
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800447 int ret;
448 int perf_cpu = sysctl_perf_cpu_time_max_percent;
Kan Liangab7fdef2016-05-03 00:26:06 -0700449 /*
450 * If throttling is disabled don't allow the write:
451 */
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800452 if (write && (perf_cpu == 100 || perf_cpu == 0))
Kan Liangab7fdef2016-05-03 00:26:06 -0700453 return -EINVAL;
454
Stephane Eranian1a51c5d2019-01-10 17:17:16 -0800455 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
456 if (ret || !write)
457 return ret;
458
Peter Zijlstra163ec432011-02-16 11:22:34 +0100459 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700460 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
461 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100462
463 return 0;
464}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200465
Dave Hansen14c63f12013-06-21 08:51:36 -0700466int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
467
468int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200469 void *buffer, size_t *lenp, loff_t *ppos)
Dave Hansen14c63f12013-06-21 08:51:36 -0700470{
Tan Xiaojun1572e452017-02-23 14:04:39 +0800471 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Dave Hansen14c63f12013-06-21 08:51:36 -0700472
473 if (ret || !write)
474 return ret;
475
Peter Zijlstrab303e7c2016-04-04 09:57:40 +0200476 if (sysctl_perf_cpu_time_max_percent == 100 ||
477 sysctl_perf_cpu_time_max_percent == 0) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100478 printk(KERN_WARNING
479 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
480 WRITE_ONCE(perf_sample_allowed_ns, 0);
481 } else {
482 update_perf_cpu_limits();
483 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700484
485 return 0;
486}
487
488/*
489 * perf samples are done in some very critical code paths (NMIs).
490 * If they take too much CPU time, the system can lock up and not
491 * get any real work done. This will drop the sample rate when
492 * we detect that events are taking too long.
493 */
494#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200495static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700496
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100497static u64 __report_avg;
498static u64 __report_allowed;
499
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100500static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700501{
David Ahern0d87d7e2016-08-01 13:49:29 -0700502 printk_ratelimited(KERN_INFO
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100503 "perf: interrupt took too long (%lld > %lld), lowering "
504 "kernel.perf_event_max_sample_rate to %d\n",
505 __report_avg, __report_allowed,
506 sysctl_perf_event_sample_rate);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100507}
508
509static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
510
511void perf_sample_event_took(u64 sample_len_ns)
512{
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100513 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
514 u64 running_len;
515 u64 avg_len;
516 u32 max;
Dave Hansen14c63f12013-06-21 08:51:36 -0700517
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100518 if (max_len == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700519 return;
520
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100521 /* Decay the counter by 1 average sample. */
522 running_len = __this_cpu_read(running_sample_length);
523 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
524 running_len += sample_len_ns;
525 __this_cpu_write(running_sample_length, running_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700526
527 /*
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100528 * Note: this will be biased artifically low until we have
529 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
Dave Hansen14c63f12013-06-21 08:51:36 -0700530 * from having to maintain a count.
531 */
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100532 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
533 if (avg_len <= max_len)
Dave Hansen14c63f12013-06-21 08:51:36 -0700534 return;
535
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100536 __report_avg = avg_len;
537 __report_allowed = max_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700538
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100539 /*
540 * Compute a throttle threshold 25% below the current duration.
541 */
542 avg_len += avg_len / 4;
543 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
544 if (avg_len < max)
545 max /= (u32)avg_len;
546 else
547 max = 1;
548
549 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
550 WRITE_ONCE(max_samples_per_tick, max);
551
552 sysctl_perf_event_sample_rate = max * HZ;
Dave Hansen14c63f12013-06-21 08:51:36 -0700553 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
554
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100555 if (!irq_work_queue(&perf_duration_work)) {
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100556 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100557 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstra91a612e2016-03-17 15:17:35 +0100558 __report_avg, __report_allowed,
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100559 sysctl_perf_event_sample_rate);
560 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700561}
562
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200563static atomic64_t perf_event_id;
564
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200565static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
566 enum event_type_t event_type);
567
568static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200569 enum event_type_t event_type,
570 struct task_struct *task);
571
572static void update_context_time(struct perf_event_context *ctx);
573static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200574
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575void __weak perf_event_print_debug(void) { }
576
Matt Fleming84c79912010-10-03 21:41:13 +0100577extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200578{
Matt Fleming84c79912010-10-03 21:41:13 +0100579 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200580}
581
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200582static inline u64 perf_clock(void)
583{
584 return local_clock();
585}
586
Peter Zijlstra34f43922015-02-20 14:05:38 +0100587static inline u64 perf_event_clock(struct perf_event *event)
588{
589 return event->clock();
590}
591
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200592/*
593 * State based event timekeeping...
594 *
595 * The basic idea is to use event->state to determine which (if any) time
596 * fields to increment with the current delta. This means we only need to
597 * update timestamps when we change state or when they are explicitly requested
598 * (read).
599 *
600 * Event groups make things a little more complicated, but not terribly so. The
601 * rules for a group are that if the group leader is OFF the entire group is
602 * OFF, irrespecive of what the group member states are. This results in
603 * __perf_effective_state().
604 *
605 * A futher ramification is that when a group leader flips between OFF and
606 * !OFF, we need to update all group member times.
607 *
608 *
609 * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we
610 * need to make sure the relevant context time is updated before we try and
611 * update our timestamps.
612 */
613
614static __always_inline enum perf_event_state
615__perf_effective_state(struct perf_event *event)
616{
617 struct perf_event *leader = event->group_leader;
618
619 if (leader->state <= PERF_EVENT_STATE_OFF)
620 return leader->state;
621
622 return event->state;
623}
624
625static __always_inline void
626__perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running)
627{
628 enum perf_event_state state = __perf_effective_state(event);
629 u64 delta = now - event->tstamp;
630
631 *enabled = event->total_time_enabled;
632 if (state >= PERF_EVENT_STATE_INACTIVE)
633 *enabled += delta;
634
635 *running = event->total_time_running;
636 if (state >= PERF_EVENT_STATE_ACTIVE)
637 *running += delta;
638}
639
640static void perf_event_update_time(struct perf_event *event)
641{
642 u64 now = perf_event_time(event);
643
644 __perf_update_times(event, now, &event->total_time_enabled,
645 &event->total_time_running);
646 event->tstamp = now;
647}
648
649static void perf_event_update_sibling_time(struct perf_event *leader)
650{
651 struct perf_event *sibling;
652
Peter Zijlstraedb39592018-03-15 17:36:56 +0100653 for_each_sibling_event(sibling, leader)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +0200654 perf_event_update_time(sibling);
655}
656
657static void
658perf_event_set_state(struct perf_event *event, enum perf_event_state state)
659{
660 if (event->state == state)
661 return;
662
663 perf_event_update_time(event);
664 /*
665 * If a group leader gets enabled/disabled all its siblings
666 * are affected too.
667 */
668 if ((event->state < 0) ^ (state < 0))
669 perf_event_update_sibling_time(event);
670
671 WRITE_ONCE(event->state, state);
672}
673
Stephane Eraniane5d13672011-02-14 11:20:01 +0200674#ifdef CONFIG_CGROUP_PERF
675
Stephane Eraniane5d13672011-02-14 11:20:01 +0200676static inline bool
677perf_cgroup_match(struct perf_event *event)
678{
679 struct perf_event_context *ctx = event->ctx;
680 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
681
Tejun Heoef824fa2013-04-08 19:00:38 -0700682 /* @event doesn't care about cgroup */
683 if (!event->cgrp)
684 return true;
685
686 /* wants specific cgroup scope but @cpuctx isn't associated with any */
687 if (!cpuctx->cgrp)
688 return false;
689
690 /*
691 * Cgroup scoping is recursive. An event enabled for a cgroup is
692 * also enabled for all its descendant cgroups. If @cpuctx's
693 * cgroup is a descendant of @event's (the test covers identity
694 * case), it's a match.
695 */
696 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
697 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200698}
699
Stephane Eraniane5d13672011-02-14 11:20:01 +0200700static inline void perf_detach_cgroup(struct perf_event *event)
701{
Zefan Li4e2ba652014-09-19 16:53:14 +0800702 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200703 event->cgrp = NULL;
704}
705
706static inline int is_cgroup_event(struct perf_event *event)
707{
708 return event->cgrp != NULL;
709}
710
711static inline u64 perf_cgroup_event_time(struct perf_event *event)
712{
713 struct perf_cgroup_info *t;
714
715 t = per_cpu_ptr(event->cgrp->info, event->cpu);
716 return t->time;
717}
718
719static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
720{
721 struct perf_cgroup_info *info;
722 u64 now;
723
724 now = perf_clock();
725
726 info = this_cpu_ptr(cgrp->info);
727
728 info->time += now - info->timestamp;
729 info->timestamp = now;
730}
731
732static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
733{
Song Liuc917e0f22018-03-12 09:59:43 -0700734 struct perf_cgroup *cgrp = cpuctx->cgrp;
735 struct cgroup_subsys_state *css;
736
737 if (cgrp) {
738 for (css = &cgrp->css; css; css = css->parent) {
739 cgrp = container_of(css, struct perf_cgroup, css);
740 __update_cgrp_time(cgrp);
741 }
742 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200743}
744
745static inline void update_cgrp_time_from_event(struct perf_event *event)
746{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200747 struct perf_cgroup *cgrp;
748
Stephane Eraniane5d13672011-02-14 11:20:01 +0200749 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200750 * ensure we access cgroup data only when needed and
751 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200752 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200753 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200754 return;
755
Stephane Eranian614e4c42015-11-12 11:00:04 +0100756 cgrp = perf_cgroup_from_task(current, event->ctx);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200757 /*
758 * Do not update time when cgroup is not active
759 */
Colin Ian King28fa7412018-10-29 23:32:11 +0000760 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200761 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200762}
763
764static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200765perf_cgroup_set_timestamp(struct task_struct *task,
766 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200767{
768 struct perf_cgroup *cgrp;
769 struct perf_cgroup_info *info;
Song Liuc917e0f22018-03-12 09:59:43 -0700770 struct cgroup_subsys_state *css;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200771
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200772 /*
773 * ctx->lock held by caller
774 * ensure we do not access cgroup data
775 * unless we have the cgroup pinned (css_get)
776 */
777 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200778 return;
779
Stephane Eranian614e4c42015-11-12 11:00:04 +0100780 cgrp = perf_cgroup_from_task(task, ctx);
Song Liuc917e0f22018-03-12 09:59:43 -0700781
782 for (css = &cgrp->css; css; css = css->parent) {
783 cgrp = container_of(css, struct perf_cgroup, css);
784 info = this_cpu_ptr(cgrp->info);
785 info->timestamp = ctx->timestamp;
786 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200787}
788
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800789static DEFINE_PER_CPU(struct list_head, cgrp_cpuctx_list);
790
Stephane Eraniane5d13672011-02-14 11:20:01 +0200791#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
792#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
793
794/*
795 * reschedule events based on the cgroup constraint of task.
796 *
797 * mode SWOUT : schedule out everything
798 * mode SWIN : schedule in based on cgroup for next
799 */
Geliang Tang18ab2cd2015-09-27 23:25:50 +0800800static void perf_cgroup_switch(struct task_struct *task, int mode)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200801{
802 struct perf_cpu_context *cpuctx;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800803 struct list_head *list;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200804 unsigned long flags;
805
806 /*
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800807 * Disable interrupts and preemption to avoid this CPU's
808 * cgrp_cpuctx_entry to change under us.
Stephane Eraniane5d13672011-02-14 11:20:01 +0200809 */
810 local_irq_save(flags);
811
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800812 list = this_cpu_ptr(&cgrp_cpuctx_list);
813 list_for_each_entry(cpuctx, list, cgrp_cpuctx_entry) {
814 WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200815
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800816 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
817 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200818
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800819 if (mode & PERF_CGROUP_SWOUT) {
820 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
821 /*
822 * must not be done before ctxswout due
823 * to event_filter_match() in event_sched_out()
824 */
825 cpuctx->cgrp = NULL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200826 }
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -0800827
828 if (mode & PERF_CGROUP_SWIN) {
829 WARN_ON_ONCE(cpuctx->cgrp);
830 /*
831 * set cgrp before ctxsw in to allow
832 * event_filter_match() to not have to pass
833 * task around
834 * we pass the cpuctx->ctx to perf_cgroup_from_task()
835 * because cgorup events are only per-cpu
836 */
837 cpuctx->cgrp = perf_cgroup_from_task(task,
838 &cpuctx->ctx);
839 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
840 }
841 perf_pmu_enable(cpuctx->ctx.pmu);
842 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200843 }
844
Stephane Eraniane5d13672011-02-14 11:20:01 +0200845 local_irq_restore(flags);
846}
847
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200848static inline void perf_cgroup_sched_out(struct task_struct *task,
849 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200850{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200851 struct perf_cgroup *cgrp1;
852 struct perf_cgroup *cgrp2 = NULL;
853
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100854 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200855 /*
856 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100857 * we do not need to pass the ctx here because we know
858 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200859 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100860 cgrp1 = perf_cgroup_from_task(task, NULL);
Peter Zijlstra70a01652016-01-08 09:29:16 +0100861 cgrp2 = perf_cgroup_from_task(next, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200862
863 /*
864 * only schedule out current cgroup events if we know
865 * that we are switching to a different cgroup. Otherwise,
866 * do no touch the cgroup events.
867 */
868 if (cgrp1 != cgrp2)
869 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100870
871 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200872}
873
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200874static inline void perf_cgroup_sched_in(struct task_struct *prev,
875 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200876{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200877 struct perf_cgroup *cgrp1;
878 struct perf_cgroup *cgrp2 = NULL;
879
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100880 rcu_read_lock();
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200881 /*
882 * we come here when we know perf_cgroup_events > 0
Stephane Eranian614e4c42015-11-12 11:00:04 +0100883 * we do not need to pass the ctx here because we know
884 * we are holding the rcu lock
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200885 */
Stephane Eranian614e4c42015-11-12 11:00:04 +0100886 cgrp1 = perf_cgroup_from_task(task, NULL);
Stephane Eranian614e4c42015-11-12 11:00:04 +0100887 cgrp2 = perf_cgroup_from_task(prev, NULL);
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200888
889 /*
890 * only need to schedule in cgroup events if we are changing
891 * cgroup during ctxsw. Cgroup events were not scheduled
892 * out of ctxsw out if that was not the case.
893 */
894 if (cgrp1 != cgrp2)
895 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +0100896
897 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +0200898}
899
Ian Rogersc2283c92020-02-13 23:51:32 -0800900static int perf_cgroup_ensure_storage(struct perf_event *event,
901 struct cgroup_subsys_state *css)
902{
903 struct perf_cpu_context *cpuctx;
904 struct perf_event **storage;
905 int cpu, heap_size, ret = 0;
906
907 /*
908 * Allow storage to have sufficent space for an iterator for each
909 * possibly nested cgroup plus an iterator for events with no cgroup.
910 */
911 for (heap_size = 1; css; css = css->parent)
912 heap_size++;
913
914 for_each_possible_cpu(cpu) {
915 cpuctx = per_cpu_ptr(event->pmu->pmu_cpu_context, cpu);
916 if (heap_size <= cpuctx->heap_size)
917 continue;
918
919 storage = kmalloc_node(heap_size * sizeof(struct perf_event *),
920 GFP_KERNEL, cpu_to_node(cpu));
921 if (!storage) {
922 ret = -ENOMEM;
923 break;
924 }
925
926 raw_spin_lock_irq(&cpuctx->ctx.lock);
927 if (cpuctx->heap_size < heap_size) {
928 swap(cpuctx->heap, storage);
929 if (storage == cpuctx->heap_default)
930 storage = NULL;
931 cpuctx->heap_size = heap_size;
932 }
933 raw_spin_unlock_irq(&cpuctx->ctx.lock);
934
935 kfree(storage);
936 }
937
938 return ret;
939}
940
Stephane Eraniane5d13672011-02-14 11:20:01 +0200941static inline int perf_cgroup_connect(int fd, struct perf_event *event,
942 struct perf_event_attr *attr,
943 struct perf_event *group_leader)
944{
945 struct perf_cgroup *cgrp;
946 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400947 struct fd f = fdget(fd);
948 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200949
Al Viro2903ff02012-08-28 12:52:22 -0400950 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200951 return -EBADF;
952
Al Virob5830432014-10-31 01:22:04 -0400953 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400954 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800955 if (IS_ERR(css)) {
956 ret = PTR_ERR(css);
957 goto out;
958 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200959
Ian Rogersc2283c92020-02-13 23:51:32 -0800960 ret = perf_cgroup_ensure_storage(event, css);
961 if (ret)
962 goto out;
963
Stephane Eraniane5d13672011-02-14 11:20:01 +0200964 cgrp = container_of(css, struct perf_cgroup, css);
965 event->cgrp = cgrp;
966
967 /*
968 * all events in a group must monitor
969 * the same cgroup because a task belongs
970 * to only one perf cgroup at a time
971 */
972 if (group_leader && group_leader->cgrp != cgrp) {
973 perf_detach_cgroup(event);
974 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200975 }
Li Zefan3db272c2011-03-03 14:25:37 +0800976out:
Al Viro2903ff02012-08-28 12:52:22 -0400977 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200978 return ret;
979}
980
981static inline void
982perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
983{
984 struct perf_cgroup_info *t;
985 t = per_cpu_ptr(event->cgrp->info, event->cpu);
986 event->shadow_ctx_time = now - t->timestamp;
987}
988
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700989static inline void
Peter Zijlstra33238c52020-03-18 20:33:37 +0100990perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx)
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700991{
992 struct perf_cpu_context *cpuctx;
993
994 if (!is_cgroup_event(event))
995 return;
996
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -0700997 /*
998 * Because cgroup events are always per-cpu events,
Song Liu07c59722020-01-22 11:50:27 -0800999 * @ctx == &cpuctx->ctx.
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001000 */
Song Liu07c59722020-01-22 11:50:27 -08001001 cpuctx = container_of(ctx, struct perf_cpu_context, ctx);
leilei.lin33801b92018-03-06 17:36:37 +08001002
1003 /*
1004 * Since setting cpuctx->cgrp is conditional on the current @cgrp
1005 * matching the event's cgroup, we must do this for every new event,
1006 * because if the first would mismatch, the second would not try again
1007 * and we would leave cpuctx->cgrp unset.
1008 */
Peter Zijlstra33238c52020-03-18 20:33:37 +01001009 if (ctx->is_active && !cpuctx->cgrp) {
Tejun Heobe96b312017-10-28 09:49:37 -07001010 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
1011
Tejun Heobe96b312017-10-28 09:49:37 -07001012 if (cgroup_is_descendant(cgrp->css.cgroup, event->cgrp->css.cgroup))
1013 cpuctx->cgrp = cgrp;
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -08001014 }
leilei.lin33801b92018-03-06 17:36:37 +08001015
Peter Zijlstra33238c52020-03-18 20:33:37 +01001016 if (ctx->nr_cgroups++)
leilei.lin33801b92018-03-06 17:36:37 +08001017 return;
1018
Peter Zijlstra33238c52020-03-18 20:33:37 +01001019 list_add(&cpuctx->cgrp_cpuctx_entry,
1020 per_cpu_ptr(&cgrp_cpuctx_list, event->cpu));
1021}
1022
1023static inline void
1024perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx)
1025{
1026 struct perf_cpu_context *cpuctx;
1027
1028 if (!is_cgroup_event(event))
1029 return;
1030
1031 /*
1032 * Because cgroup events are always per-cpu events,
1033 * @ctx == &cpuctx->ctx.
1034 */
1035 cpuctx = container_of(ctx, struct perf_cpu_context, ctx);
1036
1037 if (--ctx->nr_cgroups)
1038 return;
1039
1040 if (ctx->is_active && cpuctx->cgrp)
leilei.lin33801b92018-03-06 17:36:37 +08001041 cpuctx->cgrp = NULL;
1042
Peter Zijlstra33238c52020-03-18 20:33:37 +01001043 list_del(&cpuctx->cgrp_cpuctx_entry);
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001044}
1045
Stephane Eraniane5d13672011-02-14 11:20:01 +02001046#else /* !CONFIG_CGROUP_PERF */
1047
1048static inline bool
1049perf_cgroup_match(struct perf_event *event)
1050{
1051 return true;
1052}
1053
1054static inline void perf_detach_cgroup(struct perf_event *event)
1055{}
1056
1057static inline int is_cgroup_event(struct perf_event *event)
1058{
1059 return 0;
1060}
1061
Stephane Eraniane5d13672011-02-14 11:20:01 +02001062static inline void update_cgrp_time_from_event(struct perf_event *event)
1063{
1064}
1065
1066static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
1067{
1068}
1069
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001070static inline void perf_cgroup_sched_out(struct task_struct *task,
1071 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001072{
1073}
1074
Stephane Eraniana8d757e2011-08-25 15:58:03 +02001075static inline void perf_cgroup_sched_in(struct task_struct *prev,
1076 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001077{
1078}
1079
1080static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
1081 struct perf_event_attr *attr,
1082 struct perf_event *group_leader)
1083{
1084 return -EINVAL;
1085}
1086
1087static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001088perf_cgroup_set_timestamp(struct task_struct *task,
1089 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001090{
1091}
1092
Ben Dooks (Codethink)d00dbd292019-11-06 13:25:27 +00001093static inline void
Stephane Eraniane5d13672011-02-14 11:20:01 +02001094perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
1095{
1096}
1097
1098static inline void
1099perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
1100{
1101}
1102
1103static inline u64 perf_cgroup_event_time(struct perf_event *event)
1104{
1105 return 0;
1106}
1107
1108static inline void
Peter Zijlstra33238c52020-03-18 20:33:37 +01001109perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx)
David Carrillo-Cisnerosdb4a8352016-08-02 00:48:12 -07001110{
1111}
1112
Peter Zijlstra33238c52020-03-18 20:33:37 +01001113static inline void
1114perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx)
1115{
1116}
Stephane Eraniane5d13672011-02-14 11:20:01 +02001117#endif
1118
Stephane Eranian9e630202013-04-03 14:21:33 +02001119/*
1120 * set default to be dependent on timer tick just
1121 * like original code
1122 */
1123#define PERF_CPU_HRTIMER (1000 / HZ)
1124/*
Masahiro Yamada8a1115f2017-03-09 16:16:31 -08001125 * function must be called with interrupts disabled
Stephane Eranian9e630202013-04-03 14:21:33 +02001126 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001127static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
Stephane Eranian9e630202013-04-03 14:21:33 +02001128{
1129 struct perf_cpu_context *cpuctx;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01001130 bool rotations;
Stephane Eranian9e630202013-04-03 14:21:33 +02001131
Frederic Weisbecker16444642017-11-06 16:01:24 +01001132 lockdep_assert_irqs_disabled();
Stephane Eranian9e630202013-04-03 14:21:33 +02001133
1134 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
Stephane Eranian9e630202013-04-03 14:21:33 +02001135 rotations = perf_rotate_context(cpuctx);
1136
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001137 raw_spin_lock(&cpuctx->hrtimer_lock);
1138 if (rotations)
Stephane Eranian9e630202013-04-03 14:21:33 +02001139 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001140 else
1141 cpuctx->hrtimer_active = 0;
1142 raw_spin_unlock(&cpuctx->hrtimer_lock);
Stephane Eranian9e630202013-04-03 14:21:33 +02001143
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001144 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
Stephane Eranian9e630202013-04-03 14:21:33 +02001145}
1146
Peter Zijlstra272325c2015-04-15 11:41:58 +02001147static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
Stephane Eranian9e630202013-04-03 14:21:33 +02001148{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001149 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001150 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra272325c2015-04-15 11:41:58 +02001151 u64 interval;
Stephane Eranian9e630202013-04-03 14:21:33 +02001152
1153 /* no multiplexing needed for SW PMU */
1154 if (pmu->task_ctx_nr == perf_sw_context)
1155 return;
1156
Stephane Eranian62b85632013-04-03 14:21:34 +02001157 /*
1158 * check default is sane, if not set then force to
1159 * default interval (1/tick)
1160 */
Peter Zijlstra272325c2015-04-15 11:41:58 +02001161 interval = pmu->hrtimer_interval_ms;
1162 if (interval < 1)
1163 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
Stephane Eranian62b85632013-04-03 14:21:34 +02001164
Peter Zijlstra272325c2015-04-15 11:41:58 +02001165 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
Stephane Eranian9e630202013-04-03 14:21:33 +02001166
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001167 raw_spin_lock_init(&cpuctx->hrtimer_lock);
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02001168 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra272325c2015-04-15 11:41:58 +02001169 timer->function = perf_mux_hrtimer_handler;
Stephane Eranian9e630202013-04-03 14:21:33 +02001170}
1171
Peter Zijlstra272325c2015-04-15 11:41:58 +02001172static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
Stephane Eranian9e630202013-04-03 14:21:33 +02001173{
Peter Zijlstra272325c2015-04-15 11:41:58 +02001174 struct hrtimer *timer = &cpuctx->hrtimer;
Stephane Eranian9e630202013-04-03 14:21:33 +02001175 struct pmu *pmu = cpuctx->ctx.pmu;
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001176 unsigned long flags;
Stephane Eranian9e630202013-04-03 14:21:33 +02001177
1178 /* not for SW PMU */
1179 if (pmu->task_ctx_nr == perf_sw_context)
Peter Zijlstra272325c2015-04-15 11:41:58 +02001180 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001181
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001182 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
1183 if (!cpuctx->hrtimer_active) {
1184 cpuctx->hrtimer_active = 1;
1185 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +02001186 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD);
Peter Zijlstra4cfafd32015-05-14 12:23:11 +02001187 }
1188 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
Stephane Eranian9e630202013-04-03 14:21:33 +02001189
Peter Zijlstra272325c2015-04-15 11:41:58 +02001190 return 0;
Stephane Eranian9e630202013-04-03 14:21:33 +02001191}
1192
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001193void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001194{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001195 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1196 if (!(*count)++)
1197 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198}
1199
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001200void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001201{
Peter Zijlstra33696fc2010-06-14 08:49:00 +02001202 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1203 if (!--(*count))
1204 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205}
1206
Mark Rutland2fde4f92015-01-07 15:01:54 +00001207static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001208
1209/*
Mark Rutland2fde4f92015-01-07 15:01:54 +00001210 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1211 * perf_event_task_tick() are fully serialized because they're strictly cpu
1212 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1213 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001214 */
Mark Rutland2fde4f92015-01-07 15:01:54 +00001215static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001216{
Mark Rutland2fde4f92015-01-07 15:01:54 +00001217 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001218
Frederic Weisbecker16444642017-11-06 16:01:24 +01001219 lockdep_assert_irqs_disabled();
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001220
Mark Rutland2fde4f92015-01-07 15:01:54 +00001221 WARN_ON(!list_empty(&ctx->active_ctx_list));
1222
1223 list_add(&ctx->active_ctx_list, head);
1224}
1225
1226static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1227{
Frederic Weisbecker16444642017-11-06 16:01:24 +01001228 lockdep_assert_irqs_disabled();
Mark Rutland2fde4f92015-01-07 15:01:54 +00001229
1230 WARN_ON(list_empty(&ctx->active_ctx_list));
1231
1232 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233}
1234
1235static void get_ctx(struct perf_event_context *ctx)
1236{
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001237 refcount_inc(&ctx->refcount);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238}
1239
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001240static void free_ctx(struct rcu_head *head)
1241{
1242 struct perf_event_context *ctx;
1243
1244 ctx = container_of(head, struct perf_event_context, rcu_head);
1245 kfree(ctx->task_ctx_data);
1246 kfree(ctx);
1247}
1248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001249static void put_ctx(struct perf_event_context *ctx)
1250{
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001251 if (refcount_dec_and_test(&ctx->refcount)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252 if (ctx->parent_ctx)
1253 put_ctx(ctx->parent_ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001254 if (ctx->task && ctx->task != TASK_TOMBSTONE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255 put_task_struct(ctx->task);
Yan, Zheng4af57ef2014-11-04 21:56:01 -05001256 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001257 }
1258}
1259
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001260/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001261 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1262 * perf_pmu_migrate_context() we need some magic.
1263 *
1264 * Those places that change perf_event::ctx will hold both
1265 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1266 *
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001267 * Lock ordering is by mutex address. There are two other sites where
1268 * perf_event_context::mutex nests and those are:
1269 *
1270 * - perf_event_exit_task_context() [ child , 0 ]
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01001271 * perf_event_exit_event()
1272 * put_event() [ parent, 1 ]
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02001273 *
1274 * - perf_event_init_context() [ parent, 0 ]
1275 * inherit_task_group()
1276 * inherit_group()
1277 * inherit_event()
1278 * perf_event_alloc()
1279 * perf_init_event()
1280 * perf_try_init_event() [ child , 1 ]
1281 *
1282 * While it appears there is an obvious deadlock here -- the parent and child
1283 * nesting levels are inverted between the two. This is in fact safe because
1284 * life-time rules separate them. That is an exiting task cannot fork, and a
1285 * spawning task cannot (yet) exit.
1286 *
1287 * But remember that that these are parent<->child context relations, and
1288 * migration does not affect children, therefore these two orderings should not
1289 * interact.
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001290 *
1291 * The change in perf_event::ctx does not affect children (as claimed above)
1292 * because the sys_perf_event_open() case will install a new event and break
1293 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1294 * concerned with cpuctx and that doesn't have children.
1295 *
1296 * The places that change perf_event::ctx will issue:
1297 *
1298 * perf_remove_from_context();
1299 * synchronize_rcu();
1300 * perf_install_in_context();
1301 *
1302 * to affect the change. The remove_from_context() + synchronize_rcu() should
1303 * quiesce the event, after which we can install it in the new location. This
1304 * means that only external vectors (perf_fops, prctl) can perturb the event
1305 * while in transit. Therefore all such accessors should also acquire
1306 * perf_event_context::mutex to serialize against this.
1307 *
1308 * However; because event->ctx can change while we're waiting to acquire
1309 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1310 * function.
1311 *
1312 * Lock order:
Bernd Edlinger69143032020-03-20 21:27:55 +01001313 * exec_update_mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001314 * task_struct::perf_event_mutex
1315 * perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001316 * perf_event::child_mutex;
Peter Zijlstra07c4a772016-01-26 12:15:37 +01001317 * perf_event_context::lock
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001318 * perf_event::mmap_mutex
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001319 * mmap_lock
Alexander Shishkin18736ee2019-02-15 13:56:54 +02001320 * perf_addr_filters_head::lock
Peter Zijlstra82d94852018-01-09 13:10:30 +01001321 *
1322 * cpu_hotplug_lock
1323 * pmus_lock
1324 * cpuctx->mutex / perf_event_context::mutex
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001325 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001326static struct perf_event_context *
1327perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001328{
1329 struct perf_event_context *ctx;
1330
1331again:
1332 rcu_read_lock();
Mark Rutland6aa7de02017-10-23 14:07:29 -07001333 ctx = READ_ONCE(event->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001334 if (!refcount_inc_not_zero(&ctx->refcount)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001335 rcu_read_unlock();
1336 goto again;
1337 }
1338 rcu_read_unlock();
1339
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001340 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001341 if (event->ctx != ctx) {
1342 mutex_unlock(&ctx->mutex);
1343 put_ctx(ctx);
1344 goto again;
1345 }
1346
1347 return ctx;
1348}
1349
Peter Zijlstraa83fe282015-01-29 14:44:34 +01001350static inline struct perf_event_context *
1351perf_event_ctx_lock(struct perf_event *event)
1352{
1353 return perf_event_ctx_lock_nested(event, 0);
1354}
1355
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001356static void perf_event_ctx_unlock(struct perf_event *event,
1357 struct perf_event_context *ctx)
1358{
1359 mutex_unlock(&ctx->mutex);
1360 put_ctx(ctx);
1361}
1362
1363/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001364 * This must be done under the ctx->lock, such as to serialize against
1365 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1366 * calling scheduler related locks and ctx->lock nests inside those.
1367 */
1368static __must_check struct perf_event_context *
1369unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001370{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001371 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1372
1373 lockdep_assert_held(&ctx->lock);
1374
1375 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001377 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001378
1379 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380}
1381
Oleg Nesterov1d953112017-08-22 17:59:28 +02001382static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p,
1383 enum pid_type type)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001384{
Oleg Nesterov1d953112017-08-22 17:59:28 +02001385 u32 nr;
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001386 /*
1387 * only top level events have the pid namespace they were created in
1388 */
1389 if (event->parent)
1390 event = event->parent;
1391
Oleg Nesterov1d953112017-08-22 17:59:28 +02001392 nr = __task_pid_nr_ns(p, type, event->ns);
1393 /* avoid -1 if it is idle thread or runs in another ns */
1394 if (!nr && !pid_alive(p))
1395 nr = -1;
1396 return nr;
1397}
1398
1399static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1400{
Eric W. Biederman6883f812017-06-04 04:32:13 -05001401 return perf_event_pid_type(event, p, PIDTYPE_TGID);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001402}
1403
1404static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1405{
Oleg Nesterov1d953112017-08-22 17:59:28 +02001406 return perf_event_pid_type(event, p, PIDTYPE_PID);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001407}
1408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409/*
1410 * If we inherit events we want to return the parent event id
1411 * to userspace.
1412 */
1413static u64 primary_event_id(struct perf_event *event)
1414{
1415 u64 id = event->id;
1416
1417 if (event->parent)
1418 id = event->parent->id;
1419
1420 return id;
1421}
1422
1423/*
1424 * Get the perf_event_context for a task and lock it.
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001425 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426 * This has to cope with with the fact that until it is locked,
1427 * the context could get moved to another task.
1428 */
1429static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001430perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001431{
1432 struct perf_event_context *ctx;
1433
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001434retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001435 /*
1436 * One of the few rules of preemptible RCU is that one cannot do
1437 * rcu_read_unlock() while holding a scheduler (or nested) lock when
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001438 * part of the read side critical section was irqs-enabled -- see
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001439 * rcu_read_unlock_special().
1440 *
1441 * Since ctx->lock nests under rq->lock we must ensure the entire read
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001442 * side critical section has interrupts disabled.
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001443 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001444 local_irq_save(*flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001445 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001446 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447 if (ctx) {
1448 /*
1449 * If this context is a clone of another, it might
1450 * get swapped for another underneath us by
1451 * perf_event_task_sched_out, though the
1452 * rcu_read_lock() protects us from any context
1453 * getting freed. Lock the context and check if it
1454 * got swapped before we could get the lock, and retry
1455 * if so. If we locked the right context, then it
1456 * can't get swapped on us any more.
1457 */
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001458 raw_spin_lock(&ctx->lock);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001459 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001460 raw_spin_unlock(&ctx->lock);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001461 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001462 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 goto retry;
1464 }
1465
Peter Zijlstra63b6da32016-01-14 16:05:37 +01001466 if (ctx->task == TASK_TOMBSTONE ||
Elena Reshetova8c94abb2019-01-28 14:27:26 +02001467 !refcount_inc_not_zero(&ctx->refcount)) {
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001468 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469 ctx = NULL;
Peter Zijlstra828b6f02016-01-27 21:59:04 +01001470 } else {
1471 WARN_ON_ONCE(ctx->task != task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 }
1473 }
1474 rcu_read_unlock();
Paul E. McKenney2fd59072015-11-04 05:48:38 -08001475 if (!ctx)
1476 local_irq_restore(*flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001477 return ctx;
1478}
1479
1480/*
1481 * Get the context for a task and increment its pin_count so it
1482 * can't get swapped to another task. This also increments its
1483 * reference count so that the context can't get freed.
1484 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001485static struct perf_event_context *
1486perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487{
1488 struct perf_event_context *ctx;
1489 unsigned long flags;
1490
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001491 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 if (ctx) {
1493 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001494 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 }
1496 return ctx;
1497}
1498
1499static void perf_unpin_context(struct perf_event_context *ctx)
1500{
1501 unsigned long flags;
1502
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001503 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001505 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001506}
1507
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001508/*
1509 * Update the record of the current time in a context.
1510 */
1511static void update_context_time(struct perf_event_context *ctx)
1512{
1513 u64 now = perf_clock();
1514
1515 ctx->time += now - ctx->timestamp;
1516 ctx->timestamp = now;
1517}
1518
Stephane Eranian41587552011-01-03 18:20:01 +02001519static u64 perf_event_time(struct perf_event *event)
1520{
1521 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001522
1523 if (is_cgroup_event(event))
1524 return perf_cgroup_event_time(event);
1525
Stephane Eranian41587552011-01-03 18:20:01 +02001526 return ctx ? ctx->time : 0;
1527}
1528
Alexander Shishkin487f05e2017-01-19 18:43:30 +02001529static enum event_type_t get_event_type(struct perf_event *event)
1530{
1531 struct perf_event_context *ctx = event->ctx;
1532 enum event_type_t event_type;
1533
1534 lockdep_assert_held(&ctx->lock);
1535
Alexander Shishkin3bda69c2017-07-18 14:08:34 +03001536 /*
1537 * It's 'group type', really, because if our group leader is
1538 * pinned, so are we.
1539 */
1540 if (event->group_leader != event)
1541 event = event->group_leader;
1542
Alexander Shishkin487f05e2017-01-19 18:43:30 +02001543 event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE;
1544 if (!ctx->task)
1545 event_type |= EVENT_CPU;
1546
1547 return event_type;
1548}
1549
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001550/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001551 * Helper function to initialize event group nodes.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001552 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001553static void init_event_group(struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001554{
1555 RB_CLEAR_NODE(&event->group_node);
1556 event->group_index = 0;
1557}
1558
1559/*
1560 * Extract pinned or flexible groups from the context
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001561 * based on event attrs bits.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001562 */
1563static struct perf_event_groups *
1564get_event_groups(struct perf_event *event, struct perf_event_context *ctx)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001565{
1566 if (event->attr.pinned)
1567 return &ctx->pinned_groups;
1568 else
1569 return &ctx->flexible_groups;
1570}
1571
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001573 * Helper function to initializes perf_event_group trees.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001574 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001575static void perf_event_groups_init(struct perf_event_groups *groups)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001576{
1577 groups->tree = RB_ROOT;
1578 groups->index = 0;
1579}
1580
1581/*
1582 * Compare function for event groups;
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001583 *
1584 * Implements complex key that first sorts by CPU and then by virtual index
1585 * which provides ordering when rotating groups for the same CPU.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001586 */
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001587static bool
1588perf_event_groups_less(struct perf_event *left, struct perf_event *right)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001589{
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001590 if (left->cpu < right->cpu)
1591 return true;
1592 if (left->cpu > right->cpu)
1593 return false;
1594
Ian Rogers95ed6c72020-02-13 23:51:33 -08001595#ifdef CONFIG_CGROUP_PERF
1596 if (left->cgrp != right->cgrp) {
1597 if (!left->cgrp || !left->cgrp->css.cgroup) {
1598 /*
1599 * Left has no cgroup but right does, no cgroups come
1600 * first.
1601 */
1602 return true;
1603 }
Dan Carpentera6763622020-03-12 13:56:37 +03001604 if (!right->cgrp || !right->cgrp->css.cgroup) {
Ian Rogers95ed6c72020-02-13 23:51:33 -08001605 /*
1606 * Right has no cgroup but left does, no cgroups come
1607 * first.
1608 */
1609 return false;
1610 }
1611 /* Two dissimilar cgroups, order by id. */
1612 if (left->cgrp->css.cgroup->kn->id < right->cgrp->css.cgroup->kn->id)
1613 return true;
1614
1615 return false;
1616 }
1617#endif
1618
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001619 if (left->group_index < right->group_index)
1620 return true;
1621 if (left->group_index > right->group_index)
1622 return false;
1623
1624 return false;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001625}
1626
1627/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001628 * Insert @event into @groups' tree; using {@event->cpu, ++@groups->index} for
1629 * key (see perf_event_groups_less). This places it last inside the CPU
1630 * subtree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001631 */
1632static void
1633perf_event_groups_insert(struct perf_event_groups *groups,
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001634 struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001635{
1636 struct perf_event *node_event;
1637 struct rb_node *parent;
1638 struct rb_node **node;
1639
1640 event->group_index = ++groups->index;
1641
1642 node = &groups->tree.rb_node;
1643 parent = *node;
1644
1645 while (*node) {
1646 parent = *node;
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001647 node_event = container_of(*node, struct perf_event, group_node);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001648
1649 if (perf_event_groups_less(event, node_event))
1650 node = &parent->rb_left;
1651 else
1652 node = &parent->rb_right;
1653 }
1654
1655 rb_link_node(&event->group_node, parent, node);
1656 rb_insert_color(&event->group_node, &groups->tree);
1657}
1658
1659/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001660 * Helper function to insert event into the pinned or flexible groups.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001661 */
1662static void
1663add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx)
1664{
1665 struct perf_event_groups *groups;
1666
1667 groups = get_event_groups(event, ctx);
1668 perf_event_groups_insert(groups, event);
1669}
1670
1671/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001672 * Delete a group from a tree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001673 */
1674static void
1675perf_event_groups_delete(struct perf_event_groups *groups,
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001676 struct perf_event *event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001677{
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001678 WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) ||
1679 RB_EMPTY_ROOT(&groups->tree));
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001680
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001681 rb_erase(&event->group_node, &groups->tree);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001682 init_event_group(event);
1683}
1684
1685/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001686 * Helper function to delete event from its groups.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001687 */
1688static void
1689del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx)
1690{
1691 struct perf_event_groups *groups;
1692
1693 groups = get_event_groups(event, ctx);
1694 perf_event_groups_delete(groups, event);
1695}
1696
1697/*
Ian Rogers95ed6c72020-02-13 23:51:33 -08001698 * Get the leftmost event in the cpu/cgroup subtree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001699 */
1700static struct perf_event *
Ian Rogers95ed6c72020-02-13 23:51:33 -08001701perf_event_groups_first(struct perf_event_groups *groups, int cpu,
1702 struct cgroup *cgrp)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001703{
1704 struct perf_event *node_event = NULL, *match = NULL;
1705 struct rb_node *node = groups->tree.rb_node;
Ian Rogers95ed6c72020-02-13 23:51:33 -08001706#ifdef CONFIG_CGROUP_PERF
1707 u64 node_cgrp_id, cgrp_id = 0;
1708
1709 if (cgrp)
1710 cgrp_id = cgrp->kn->id;
1711#endif
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001712
1713 while (node) {
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001714 node_event = container_of(node, struct perf_event, group_node);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001715
1716 if (cpu < node_event->cpu) {
1717 node = node->rb_left;
Ian Rogers95ed6c72020-02-13 23:51:33 -08001718 continue;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001719 }
Ian Rogers95ed6c72020-02-13 23:51:33 -08001720 if (cpu > node_event->cpu) {
1721 node = node->rb_right;
1722 continue;
1723 }
1724#ifdef CONFIG_CGROUP_PERF
1725 node_cgrp_id = 0;
1726 if (node_event->cgrp && node_event->cgrp->css.cgroup)
1727 node_cgrp_id = node_event->cgrp->css.cgroup->kn->id;
1728
1729 if (cgrp_id < node_cgrp_id) {
1730 node = node->rb_left;
1731 continue;
1732 }
1733 if (cgrp_id > node_cgrp_id) {
1734 node = node->rb_right;
1735 continue;
1736 }
1737#endif
1738 match = node_event;
1739 node = node->rb_left;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001740 }
1741
1742 return match;
1743}
1744
1745/*
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001746 * Like rb_entry_next_safe() for the @cpu subtree.
1747 */
1748static struct perf_event *
1749perf_event_groups_next(struct perf_event *event)
1750{
1751 struct perf_event *next;
Ian Rogers95ed6c72020-02-13 23:51:33 -08001752#ifdef CONFIG_CGROUP_PERF
1753 u64 curr_cgrp_id = 0;
1754 u64 next_cgrp_id = 0;
1755#endif
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001756
1757 next = rb_entry_safe(rb_next(&event->group_node), typeof(*event), group_node);
Ian Rogers95ed6c72020-02-13 23:51:33 -08001758 if (next == NULL || next->cpu != event->cpu)
1759 return NULL;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001760
Ian Rogers95ed6c72020-02-13 23:51:33 -08001761#ifdef CONFIG_CGROUP_PERF
1762 if (event->cgrp && event->cgrp->css.cgroup)
1763 curr_cgrp_id = event->cgrp->css.cgroup->kn->id;
1764
1765 if (next->cgrp && next->cgrp->css.cgroup)
1766 next_cgrp_id = next->cgrp->css.cgroup->kn->id;
1767
1768 if (curr_cgrp_id != next_cgrp_id)
1769 return NULL;
1770#endif
1771 return next;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01001772}
1773
1774/*
Peter Zijlstra161c85f2017-11-13 14:28:27 +01001775 * Iterate through the whole groups tree.
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001776 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +01001777#define perf_event_groups_for_each(event, groups) \
1778 for (event = rb_entry_safe(rb_first(&((groups)->tree)), \
1779 typeof(*event), group_node); event; \
1780 event = rb_entry_safe(rb_next(&event->group_node), \
1781 typeof(*event), group_node))
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001782
1783/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001784 * Add an event from the lists for its context.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785 * Must be called with ctx->mutex and ctx->lock held.
1786 */
1787static void
1788list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1789{
Peter Zijlstrac994d612016-01-08 09:20:23 +01001790 lockdep_assert_held(&ctx->lock);
1791
Peter Zijlstra8a495422010-05-27 15:47:49 +02001792 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1793 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02001795 event->tstamp = perf_event_time(event);
1796
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001798 * If we're a stand alone event or group leader, we go to the context
1799 * list, group events are kept attached to the group so that
1800 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001801 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001802 if (event->group_leader == event) {
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001803 event->group_caps = event->event_caps;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03001804 add_event_to_groups(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805 }
1806
1807 list_add_rcu(&event->event_entry, &ctx->event_list);
1808 ctx->nr_events++;
1809 if (event->attr.inherit_stat)
1810 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001811
Peter Zijlstra33238c52020-03-18 20:33:37 +01001812 if (event->state > PERF_EVENT_STATE_OFF)
1813 perf_cgroup_event_enable(event, ctx);
1814
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001815 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001816}
1817
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001818/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001819 * Initialize event state based on the perf_event_attr::disabled.
1820 */
1821static inline void perf_event__state_init(struct perf_event *event)
1822{
1823 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1824 PERF_EVENT_STATE_INACTIVE;
1825}
1826
Peter Zijlstraa7239682015-09-09 19:06:33 +02001827static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001828{
1829 int entry = sizeof(u64); /* value */
1830 int size = 0;
1831 int nr = 1;
1832
1833 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1834 size += sizeof(u64);
1835
1836 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1837 size += sizeof(u64);
1838
1839 if (event->attr.read_format & PERF_FORMAT_ID)
1840 entry += sizeof(u64);
1841
1842 if (event->attr.read_format & PERF_FORMAT_GROUP) {
Peter Zijlstraa7239682015-09-09 19:06:33 +02001843 nr += nr_siblings;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001844 size += sizeof(u64);
1845 }
1846
1847 size += entry * nr;
1848 event->read_size = size;
1849}
1850
Peter Zijlstraa7239682015-09-09 19:06:33 +02001851static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001852{
1853 struct perf_sample_data *data;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001854 u16 size = 0;
1855
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001856 if (sample_type & PERF_SAMPLE_IP)
1857 size += sizeof(data->ip);
1858
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001859 if (sample_type & PERF_SAMPLE_ADDR)
1860 size += sizeof(data->addr);
1861
1862 if (sample_type & PERF_SAMPLE_PERIOD)
1863 size += sizeof(data->period);
1864
Andi Kleenc3feedf2013-01-24 16:10:28 +01001865 if (sample_type & PERF_SAMPLE_WEIGHT)
1866 size += sizeof(data->weight);
1867
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001868 if (sample_type & PERF_SAMPLE_READ)
1869 size += event->read_size;
1870
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001871 if (sample_type & PERF_SAMPLE_DATA_SRC)
1872 size += sizeof(data->data_src.val);
1873
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001874 if (sample_type & PERF_SAMPLE_TRANSACTION)
1875 size += sizeof(data->txn);
1876
Kan Liangfc7ce9c2017-08-28 20:52:49 -04001877 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1878 size += sizeof(data->phys_addr);
1879
Namhyung Kim6546b192020-03-25 21:45:29 +09001880 if (sample_type & PERF_SAMPLE_CGROUP)
1881 size += sizeof(data->cgroup);
1882
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001883 event->header_size = size;
1884}
1885
Peter Zijlstraa7239682015-09-09 19:06:33 +02001886/*
1887 * Called at perf_event creation and when events are attached/detached from a
1888 * group.
1889 */
1890static void perf_event__header_size(struct perf_event *event)
1891{
1892 __perf_event_read_size(event,
1893 event->group_leader->nr_siblings);
1894 __perf_event_header_size(event, event->attr.sample_type);
1895}
1896
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001897static void perf_event__id_header_size(struct perf_event *event)
1898{
1899 struct perf_sample_data *data;
1900 u64 sample_type = event->attr.sample_type;
1901 u16 size = 0;
1902
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001903 if (sample_type & PERF_SAMPLE_TID)
1904 size += sizeof(data->tid_entry);
1905
1906 if (sample_type & PERF_SAMPLE_TIME)
1907 size += sizeof(data->time);
1908
Adrian Hunterff3d5272013-08-27 11:23:07 +03001909 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1910 size += sizeof(data->id);
1911
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001912 if (sample_type & PERF_SAMPLE_ID)
1913 size += sizeof(data->id);
1914
1915 if (sample_type & PERF_SAMPLE_STREAM_ID)
1916 size += sizeof(data->stream_id);
1917
1918 if (sample_type & PERF_SAMPLE_CPU)
1919 size += sizeof(data->cpu_entry);
1920
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001921 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001922}
1923
Peter Zijlstraa7239682015-09-09 19:06:33 +02001924static bool perf_event_validate_size(struct perf_event *event)
1925{
1926 /*
1927 * The values computed here will be over-written when we actually
1928 * attach the event.
1929 */
1930 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1931 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1932 perf_event__id_header_size(event);
1933
1934 /*
1935 * Sum the lot; should not exceed the 64k limit we have on records.
1936 * Conservative limit to allow for callchains and other variable fields.
1937 */
1938 if (event->read_size + event->header_size +
1939 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1940 return false;
1941
1942 return true;
1943}
1944
Peter Zijlstra8a495422010-05-27 15:47:49 +02001945static void perf_group_attach(struct perf_event *event)
1946{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001947 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001948
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01001949 lockdep_assert_held(&event->ctx->lock);
1950
Peter Zijlstra74c33372010-10-15 11:40:29 +02001951 /*
1952 * We can have double attach due to group movement in perf_event_open.
1953 */
1954 if (event->attach_state & PERF_ATTACH_GROUP)
1955 return;
1956
Peter Zijlstra8a495422010-05-27 15:47:49 +02001957 event->attach_state |= PERF_ATTACH_GROUP;
1958
1959 if (group_leader == event)
1960 return;
1961
Peter Zijlstra652884f2015-01-23 11:20:10 +01001962 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1963
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07001964 group_leader->group_caps &= event->event_caps;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001965
Peter Zijlstra8343aae2017-11-13 14:28:33 +01001966 list_add_tail(&event->sibling_list, &group_leader->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001967 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001968
1969 perf_event__header_size(group_leader);
1970
Peter Zijlstraedb39592018-03-15 17:36:56 +01001971 for_each_sibling_event(pos, group_leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001972 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001973}
1974
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975/*
Tobias Tefke788faab2018-07-09 12:57:15 +02001976 * Remove an event from the lists for its context.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977 * Must be called with ctx->mutex and ctx->lock held.
1978 */
1979static void
1980list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1981{
Peter Zijlstra652884f2015-01-23 11:20:10 +01001982 WARN_ON_ONCE(event->ctx != ctx);
1983 lockdep_assert_held(&ctx->lock);
1984
Peter Zijlstra8a495422010-05-27 15:47:49 +02001985 /*
1986 * We can have double detach due to exit/hot-unplug + close.
1987 */
1988 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001990
1991 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1992
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001993 ctx->nr_events--;
1994 if (event->attr.inherit_stat)
1995 ctx->nr_stat--;
1996
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001997 list_del_rcu(&event->event_entry);
1998
Peter Zijlstra8a495422010-05-27 15:47:49 +02001999 if (event->group_leader == event)
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002000 del_event_from_groups(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001
Stephane Eranianb2e74a22009-11-26 09:24:30 -08002002 /*
2003 * If event was in error state, then keep it
2004 * that way, otherwise bogus counts will be
2005 * returned on read(). The only way to get out
2006 * of error state is by explicit re-enabling
2007 * of the event
2008 */
Peter Zijlstra33238c52020-03-18 20:33:37 +01002009 if (event->state > PERF_EVENT_STATE_OFF) {
2010 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002011 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002012 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002013
2014 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02002015}
2016
Alexander Shishkinab437622019-08-06 11:46:00 +03002017static int
2018perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event)
2019{
2020 if (!has_aux(aux_event))
2021 return 0;
2022
2023 if (!event->pmu->aux_output_match)
2024 return 0;
2025
2026 return event->pmu->aux_output_match(aux_event);
2027}
2028
2029static void put_event(struct perf_event *event);
2030static void event_sched_out(struct perf_event *event,
2031 struct perf_cpu_context *cpuctx,
2032 struct perf_event_context *ctx);
2033
2034static void perf_put_aux_event(struct perf_event *event)
2035{
2036 struct perf_event_context *ctx = event->ctx;
2037 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2038 struct perf_event *iter;
2039
2040 /*
2041 * If event uses aux_event tear down the link
2042 */
2043 if (event->aux_event) {
2044 iter = event->aux_event;
2045 event->aux_event = NULL;
2046 put_event(iter);
2047 return;
2048 }
2049
2050 /*
2051 * If the event is an aux_event, tear down all links to
2052 * it from other events.
2053 */
2054 for_each_sibling_event(iter, event->group_leader) {
2055 if (iter->aux_event != event)
2056 continue;
2057
2058 iter->aux_event = NULL;
2059 put_event(event);
2060
2061 /*
2062 * If it's ACTIVE, schedule it out and put it into ERROR
2063 * state so that we don't try to schedule it again. Note
2064 * that perf_event_enable() will clear the ERROR status.
2065 */
2066 event_sched_out(iter, cpuctx, ctx);
2067 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
2068 }
2069}
2070
Alexander Shishkina4faf002019-10-25 17:08:33 +03002071static bool perf_need_aux_event(struct perf_event *event)
2072{
2073 return !!event->attr.aux_output || !!event->attr.aux_sample_size;
2074}
2075
Alexander Shishkinab437622019-08-06 11:46:00 +03002076static int perf_get_aux_event(struct perf_event *event,
2077 struct perf_event *group_leader)
2078{
2079 /*
2080 * Our group leader must be an aux event if we want to be
2081 * an aux_output. This way, the aux event will precede its
2082 * aux_output events in the group, and therefore will always
2083 * schedule first.
2084 */
2085 if (!group_leader)
2086 return 0;
2087
Alexander Shishkina4faf002019-10-25 17:08:33 +03002088 /*
2089 * aux_output and aux_sample_size are mutually exclusive.
2090 */
2091 if (event->attr.aux_output && event->attr.aux_sample_size)
2092 return 0;
2093
2094 if (event->attr.aux_output &&
2095 !perf_aux_output_match(event, group_leader))
2096 return 0;
2097
2098 if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux)
Alexander Shishkinab437622019-08-06 11:46:00 +03002099 return 0;
2100
2101 if (!atomic_long_inc_not_zero(&group_leader->refcount))
2102 return 0;
2103
2104 /*
2105 * Link aux_outputs to their aux event; this is undone in
2106 * perf_group_detach() by perf_put_aux_event(). When the
2107 * group in torn down, the aux_output events loose their
2108 * link to the aux_event and can't schedule any more.
2109 */
2110 event->aux_event = group_leader;
2111
2112 return 1;
2113}
2114
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002115static inline struct list_head *get_event_list(struct perf_event *event)
2116{
2117 struct perf_event_context *ctx = event->ctx;
2118 return event->attr.pinned ? &ctx->pinned_active : &ctx->flexible_active;
2119}
2120
Peter Zijlstra8a495422010-05-27 15:47:49 +02002121static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02002122{
2123 struct perf_event *sibling, *tmp;
Peter Zijlstra66681282017-11-13 14:28:38 +01002124 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002125
Peter Zijlstra66681282017-11-13 14:28:38 +01002126 lockdep_assert_held(&ctx->lock);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002127
Peter Zijlstra8a495422010-05-27 15:47:49 +02002128 /*
2129 * We can have double detach due to exit/hot-unplug + close.
2130 */
2131 if (!(event->attach_state & PERF_ATTACH_GROUP))
2132 return;
2133
2134 event->attach_state &= ~PERF_ATTACH_GROUP;
2135
Alexander Shishkinab437622019-08-06 11:46:00 +03002136 perf_put_aux_event(event);
2137
Peter Zijlstra8a495422010-05-27 15:47:49 +02002138 /*
2139 * If this is a sibling, remove it from its group.
2140 */
2141 if (event->group_leader != event) {
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002142 list_del_init(&event->sibling_list);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002143 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002144 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02002145 }
2146
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147 /*
2148 * If this was a group event with sibling events then
2149 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02002150 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151 */
Peter Zijlstra8343aae2017-11-13 14:28:33 +01002152 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002153
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002154 sibling->group_leader = sibling;
Mark Rutland24868362018-03-16 12:51:40 +00002155 list_del_init(&sibling->sibling_list);
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01002156
2157 /* Inherit group flags from the previous leader */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002158 sibling->group_caps = event->group_caps;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002159
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002160 if (!RB_EMPTY_NODE(&event->group_node)) {
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002161 add_event_to_groups(sibling, event->ctx);
Peter Zijlstra66681282017-11-13 14:28:38 +01002162
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002163 if (sibling->state == PERF_EVENT_STATE_ACTIVE)
2164 list_add_tail(&sibling->active_list, get_event_list(sibling));
Alexey Budankov8e1a2032017-09-08 11:47:03 +03002165 }
2166
Peter Zijlstra652884f2015-01-23 11:20:10 +01002167 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002168 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002169
2170out:
2171 perf_event__header_size(event->group_leader);
2172
Peter Zijlstraedb39592018-03-15 17:36:56 +01002173 for_each_sibling_event(tmp, event->group_leader)
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002174 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175}
2176
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002177static bool is_orphaned_event(struct perf_event *event)
2178{
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01002179 return event->state == PERF_EVENT_STATE_DEAD;
Jiri Olsafadfe7b2014-08-01 14:33:02 +02002180}
2181
Mark Rutland2c81a642016-06-14 16:10:41 +01002182static inline int __pmu_filter_match(struct perf_event *event)
Mark Rutland66eb5792015-05-13 17:12:23 +01002183{
2184 struct pmu *pmu = event->pmu;
2185 return pmu->filter_match ? pmu->filter_match(event) : 1;
2186}
2187
Mark Rutland2c81a642016-06-14 16:10:41 +01002188/*
2189 * Check whether we should attempt to schedule an event group based on
2190 * PMU-specific filtering. An event group can consist of HW and SW events,
2191 * potentially with a SW leader, so we must check all the filters, to
2192 * determine whether a group is schedulable:
2193 */
2194static inline int pmu_filter_match(struct perf_event *event)
2195{
Peter Zijlstraedb39592018-03-15 17:36:56 +01002196 struct perf_event *sibling;
Mark Rutland2c81a642016-06-14 16:10:41 +01002197
2198 if (!__pmu_filter_match(event))
2199 return 0;
2200
Peter Zijlstraedb39592018-03-15 17:36:56 +01002201 for_each_sibling_event(sibling, event) {
2202 if (!__pmu_filter_match(sibling))
Mark Rutland2c81a642016-06-14 16:10:41 +01002203 return 0;
2204 }
2205
2206 return 1;
2207}
2208
Stephane Eranianfa66f072010-08-26 16:40:01 +02002209static inline int
2210event_filter_match(struct perf_event *event)
2211{
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002212 return (event->cpu == -1 || event->cpu == smp_processor_id()) &&
2213 perf_cgroup_match(event) && pmu_filter_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02002214}
2215
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002216static void
2217event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002218 struct perf_cpu_context *cpuctx,
2219 struct perf_event_context *ctx)
2220{
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002221 enum perf_event_state state = PERF_EVENT_STATE_INACTIVE;
Peter Zijlstra652884f2015-01-23 11:20:10 +01002222
2223 WARN_ON_ONCE(event->ctx != ctx);
2224 lockdep_assert_held(&ctx->lock);
2225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002226 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002227 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228
Peter Zijlstra66681282017-11-13 14:28:38 +01002229 /*
2230 * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but
2231 * we can schedule events _OUT_ individually through things like
2232 * __perf_remove_from_context().
2233 */
2234 list_del_init(&event->active_list);
2235
Alexander Shishkin44377272013-12-16 14:17:36 +02002236 perf_pmu_disable(event->pmu);
2237
Peter Zijlstra28a967c2016-02-24 18:45:46 +01002238 event->pmu->del(event, 0);
2239 event->oncpu = -1;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002240
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002241 if (READ_ONCE(event->pending_disable) >= 0) {
2242 WRITE_ONCE(event->pending_disable, -1);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002243 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002244 state = PERF_EVENT_STATE_OFF;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245 }
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002246 perf_event_set_state(event, state);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002247
2248 if (!is_software_event(event))
2249 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002250 if (!--ctx->nr_active)
2251 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002252 if (event->attr.freq && event->attr.sample_freq)
2253 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254 if (event->attr.exclusive || !cpuctx->active_oncpu)
2255 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02002256
2257 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002258}
2259
2260static void
2261group_sched_out(struct perf_event *group_event,
2262 struct perf_cpu_context *cpuctx,
2263 struct perf_event_context *ctx)
2264{
2265 struct perf_event *event;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002266
2267 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
2268 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269
Mark Rutland3f005e72016-07-26 18:12:21 +01002270 perf_pmu_disable(ctx->pmu);
2271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272 event_sched_out(group_event, cpuctx, ctx);
2273
2274 /*
2275 * Schedule out siblings (if any):
2276 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002277 for_each_sibling_event(event, group_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278 event_sched_out(event, cpuctx, ctx);
2279
Mark Rutland3f005e72016-07-26 18:12:21 +01002280 perf_pmu_enable(ctx->pmu);
2281
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002282 if (group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283 cpuctx->exclusive = 0;
2284}
2285
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002286#define DETACH_GROUP 0x01UL
Peter Zijlstra00179602015-11-30 16:26:35 +01002287
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288/*
2289 * Cross CPU call to remove a performance event
2290 *
2291 * We disable the event on the hardware level first. After that we
2292 * remove it from the context list.
2293 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002294static void
2295__perf_remove_from_context(struct perf_event *event,
2296 struct perf_cpu_context *cpuctx,
2297 struct perf_event_context *ctx,
2298 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299{
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002300 unsigned long flags = (unsigned long)info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002301
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002302 if (ctx->is_active & EVENT_TIME) {
2303 update_context_time(ctx);
2304 update_cgrp_time_from_cpuctx(cpuctx);
2305 }
2306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002308 if (flags & DETACH_GROUP)
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02002309 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002310 list_del_event(event, ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002311
Peter Zijlstra39a43642016-01-11 12:46:35 +01002312 if (!ctx->nr_events && ctx->is_active) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002313 ctx->is_active = 0;
Peter Zijlstra90c91df2020-03-05 13:38:51 +01002314 ctx->rotate_necessary = 0;
Peter Zijlstra39a43642016-01-11 12:46:35 +01002315 if (ctx->task) {
2316 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2317 cpuctx->task_ctx = NULL;
2318 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002320}
2321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002322/*
2323 * Remove the event from a task's (or a CPU's) list of events.
2324 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 * If event->ctx is a cloned context, callers must make sure that
2326 * every task struct that event->ctx->task could possibly point to
2327 * remains valid. This is OK when called from perf_release since
2328 * that only calls us on the top-level context, which can't be a clone.
2329 * When called from perf_event_exit_task, it's OK because the
2330 * context has been detached from its task.
2331 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002332static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333{
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002334 struct perf_event_context *ctx = event->ctx;
2335
2336 lockdep_assert_held(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337
Peter Zijlstra45a0e072016-01-26 13:09:48 +01002338 event_function_call(event, __perf_remove_from_context, (void *)flags);
Peter Zijlstraa76a82a2017-01-26 16:39:55 +01002339
2340 /*
2341 * The above event_function_call() can NO-OP when it hits
2342 * TASK_TOMBSTONE. In that case we must already have been detached
2343 * from the context (by perf_event_exit_event()) but the grouping
2344 * might still be in-tact.
2345 */
2346 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
2347 if ((flags & DETACH_GROUP) &&
2348 (event->attach_state & PERF_ATTACH_GROUP)) {
2349 /*
2350 * Since in that case we cannot possibly be scheduled, simply
2351 * detach now.
2352 */
2353 raw_spin_lock_irq(&ctx->lock);
2354 perf_group_detach(event);
2355 raw_spin_unlock_irq(&ctx->lock);
2356 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002357}
2358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002359/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002360 * Cross CPU call to disable a performance event
2361 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002362static void __perf_event_disable(struct perf_event *event,
2363 struct perf_cpu_context *cpuctx,
2364 struct perf_event_context *ctx,
2365 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002367 if (event->state < PERF_EVENT_STATE_INACTIVE)
2368 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369
Peter Zijlstra3c5c8712017-09-05 13:44:51 +02002370 if (ctx->is_active & EVENT_TIME) {
2371 update_context_time(ctx);
2372 update_cgrp_time_from_event(event);
2373 }
2374
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002375 if (event == event->group_leader)
2376 group_sched_out(event, cpuctx, ctx);
2377 else
2378 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002379
2380 perf_event_set_state(event, PERF_EVENT_STATE_OFF);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002381 perf_cgroup_event_disable(event, ctx);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002382}
2383
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002385 * Disable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002386 *
2387 * If event->ctx is a cloned context, callers must make sure that
2388 * every task struct that event->ctx->task could possibly point to
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03002389 * remains valid. This condition is satisfied when called through
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390 * perf_event_for_each_child or perf_event_for_each because they
2391 * hold the top-level event's child_mutex, so any descendant that
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01002392 * goes to exit will block in perf_event_exit_event().
2393 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394 * When called from perf_pending_event it's OK because event->ctx
2395 * is the current context on this CPU and preemption is disabled,
2396 * hence we can't get into perf_event_task_sched_out for this context.
2397 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002398static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002399{
2400 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002402 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002403 if (event->state <= PERF_EVENT_STATE_OFF) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002404 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002405 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002406 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002407 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstra7b648012015-12-03 18:35:21 +01002408
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002409 event_function_call(event, __perf_event_disable, NULL);
2410}
2411
2412void perf_event_disable_local(struct perf_event *event)
2413{
2414 event_function_local(event, __perf_event_disable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002416
2417/*
2418 * Strictly speaking kernel users cannot create groups and therefore this
2419 * interface does not need the perf_event_ctx_lock() magic.
2420 */
2421void perf_event_disable(struct perf_event *event)
2422{
2423 struct perf_event_context *ctx;
2424
2425 ctx = perf_event_ctx_lock(event);
2426 _perf_event_disable(event);
2427 perf_event_ctx_unlock(event, ctx);
2428}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002429EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002430
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002431void perf_event_disable_inatomic(struct perf_event *event)
2432{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02002433 WRITE_ONCE(event->pending_disable, smp_processor_id());
2434 /* can fail, see perf_pending_event_disable() */
Jiri Olsa5aab90c2016-10-26 11:48:24 +02002435 irq_work_queue(&event->pending);
2436}
2437
Stephane Eraniane5d13672011-02-14 11:20:01 +02002438static void perf_set_shadow_time(struct perf_event *event,
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002439 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002440{
2441 /*
2442 * use the correct time source for the time snapshot
2443 *
2444 * We could get by without this by leveraging the
2445 * fact that to get to this function, the caller
2446 * has most likely already called update_context_time()
2447 * and update_cgrp_time_xx() and thus both timestamp
2448 * are identical (or very close). Given that tstamp is,
2449 * already adjusted for cgroup, we could say that:
2450 * tstamp - ctx->timestamp
2451 * is equivalent to
2452 * tstamp - cgrp->timestamp.
2453 *
2454 * Then, in perf_output_read(), the calculation would
2455 * work with no changes because:
2456 * - event is guaranteed scheduled in
2457 * - no scheduled out in between
2458 * - thus the timestamp would be the same
2459 *
2460 * But this is a bit hairy.
2461 *
2462 * So instead, we have an explicit cgroup call to remain
2463 * within the time time source all along. We believe it
2464 * is cleaner and simpler to understand.
2465 */
2466 if (is_cgroup_event(event))
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002467 perf_cgroup_set_shadow_time(event, event->tstamp);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002468 else
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002469 event->shadow_ctx_time = event->tstamp - ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002470}
2471
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002472#define MAX_INTERRUPTS (~0ULL)
2473
2474static void perf_log_throttle(struct perf_event *event, int enable);
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002475static void perf_log_itrace_start(struct perf_event *event);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002477static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002478event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002480 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002481{
Alexander Shishkin44377272013-12-16 14:17:36 +02002482 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02002483
Peter Zijlstraab6f8242019-08-07 11:17:00 +02002484 WARN_ON_ONCE(event->ctx != ctx);
2485
Peter Zijlstra63342412014-05-05 11:49:16 +02002486 lockdep_assert_held(&ctx->lock);
2487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488 if (event->state <= PERF_EVENT_STATE_OFF)
2489 return 0;
2490
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002491 WRITE_ONCE(event->oncpu, smp_processor_id());
2492 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02002493 * Order event::oncpu write to happen before the ACTIVE state is
2494 * visible. This allows perf_event_{stop,read}() to observe the correct
2495 * ->oncpu if it sees ACTIVE.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02002496 */
2497 smp_wmb();
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002498 perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE);
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01002499
2500 /*
2501 * Unthrottle events, since we scheduled we might have missed several
2502 * ticks already, also for a heavily scheduling task there is little
2503 * guarantee it'll get a tick in a timely manner.
2504 */
2505 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
2506 perf_log_throttle(event, 1);
2507 event->hw.interrupts = 0;
2508 }
2509
Alexander Shishkin44377272013-12-16 14:17:36 +02002510 perf_pmu_disable(event->pmu);
2511
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002512 perf_set_shadow_time(event, ctx);
Shaohua Li72f669c2015-02-05 15:55:31 -08002513
Alexander Shishkinec0d7722015-01-14 14:18:23 +02002514 perf_log_itrace_start(event);
2515
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002516 if (event->pmu->add(event, PERF_EF_START)) {
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002517 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02002519 ret = -EAGAIN;
2520 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002521 }
2522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523 if (!is_software_event(event))
2524 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00002525 if (!ctx->nr_active++)
2526 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002527 if (event->attr.freq && event->attr.sample_freq)
2528 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002529
2530 if (event->attr.exclusive)
2531 cpuctx->exclusive = 1;
2532
Alexander Shishkin44377272013-12-16 14:17:36 +02002533out:
2534 perf_pmu_enable(event->pmu);
2535
2536 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002537}
2538
2539static int
2540group_sched_in(struct perf_event *group_event,
2541 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002542 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002543{
Lin Ming6bde9b62010-04-23 13:56:00 +08002544 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01002545 struct pmu *pmu = ctx->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002546
2547 if (group_event->state == PERF_EVENT_STATE_OFF)
2548 return 0;
2549
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -07002550 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
Lin Ming6bde9b62010-04-23 13:56:00 +08002551
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002552 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002553 pmu->cancel_txn(pmu);
Peter Zijlstra272325c2015-04-15 11:41:58 +02002554 perf_mux_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002555 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02002556 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002557
2558 /*
2559 * Schedule in siblings as one group (if any):
2560 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002561 for_each_sibling_event(event, group_event) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002562 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002563 partial_group = event;
2564 goto group_error;
2565 }
2566 }
2567
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002568 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10002569 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002570
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002571group_error:
2572 /*
2573 * Groups can be scheduled in as one unit only, so undo any
2574 * partial group before returning:
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002575 * The events up to the failed event are scheduled out normally.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002576 */
Peter Zijlstraedb39592018-03-15 17:36:56 +01002577 for_each_sibling_event(event, group_event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002578 if (event == partial_group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002579 break;
Stephane Eraniand7842da2010-10-20 15:25:01 +02002580
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002581 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002582 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02002583 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002584
Peter Zijlstraad5133b2010-06-15 12:22:39 +02002585 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02002586
Peter Zijlstra272325c2015-04-15 11:41:58 +02002587 perf_mux_hrtimer_restart(cpuctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002589 return -EAGAIN;
2590}
2591
2592/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002593 * Work out whether we can put this event group on the CPU now.
2594 */
2595static int group_can_go_on(struct perf_event *event,
2596 struct perf_cpu_context *cpuctx,
2597 int can_add_hw)
2598{
2599 /*
2600 * Groups consisting entirely of software events can always go on.
2601 */
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -07002602 if (event->group_caps & PERF_EV_CAP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002603 return 1;
2604 /*
2605 * If an exclusive group is already on, no other hardware
2606 * events can go on.
2607 */
2608 if (cpuctx->exclusive)
2609 return 0;
2610 /*
2611 * If this group is exclusive and there are already
2612 * events on the CPU, it can't go on.
2613 */
2614 if (event->attr.exclusive && cpuctx->active_oncpu)
2615 return 0;
2616 /*
2617 * Otherwise, try to add it if all previous groups were able
2618 * to go on.
2619 */
2620 return can_add_hw;
2621}
2622
2623static void add_event_to_ctx(struct perf_event *event,
2624 struct perf_event_context *ctx)
2625{
2626 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002627 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002628}
2629
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002630static void ctx_sched_out(struct perf_event_context *ctx,
2631 struct perf_cpu_context *cpuctx,
2632 enum event_type_t event_type);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002633static void
2634ctx_sched_in(struct perf_event_context *ctx,
2635 struct perf_cpu_context *cpuctx,
2636 enum event_type_t event_type,
2637 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002638
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002639static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002640 struct perf_event_context *ctx,
2641 enum event_type_t event_type)
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002642{
2643 if (!cpuctx->task_ctx)
2644 return;
2645
2646 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2647 return;
2648
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002649 ctx_sched_out(ctx, cpuctx, event_type);
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002650}
2651
Peter Zijlstradce58552011-04-09 21:17:46 +02002652static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2653 struct perf_event_context *ctx,
2654 struct task_struct *task)
2655{
2656 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2657 if (ctx)
2658 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2659 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2660 if (ctx)
2661 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2662}
2663
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002664/*
2665 * We want to maintain the following priority of scheduling:
2666 * - CPU pinned (EVENT_CPU | EVENT_PINNED)
2667 * - task pinned (EVENT_PINNED)
2668 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE)
2669 * - task flexible (EVENT_FLEXIBLE).
2670 *
2671 * In order to avoid unscheduling and scheduling back in everything every
2672 * time an event is added, only do it for the groups of equal priority and
2673 * below.
2674 *
2675 * This can be called after a batch operation on task events, in which case
2676 * event_type is a bit mask of the types of events involved. For CPU events,
2677 * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
2678 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01002679static void ctx_resched(struct perf_cpu_context *cpuctx,
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002680 struct perf_event_context *task_ctx,
2681 enum event_type_t event_type)
Peter Zijlstra00179602015-11-30 16:26:35 +01002682{
Song Liubd903af2018-03-05 21:55:04 -08002683 enum event_type_t ctx_event_type;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002684 bool cpu_event = !!(event_type & EVENT_CPU);
2685
2686 /*
2687 * If pinned groups are involved, flexible groups also need to be
2688 * scheduled out.
2689 */
2690 if (event_type & EVENT_PINNED)
2691 event_type |= EVENT_FLEXIBLE;
2692
Song Liubd903af2018-03-05 21:55:04 -08002693 ctx_event_type = event_type & EVENT_ALL;
2694
Peter Zijlstra3e349502016-01-08 10:01:18 +01002695 perf_pmu_disable(cpuctx->ctx.pmu);
2696 if (task_ctx)
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002697 task_ctx_sched_out(cpuctx, task_ctx, event_type);
2698
2699 /*
2700 * Decide which cpu ctx groups to schedule out based on the types
2701 * of events that caused rescheduling:
2702 * - EVENT_CPU: schedule out corresponding groups;
2703 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups;
2704 * - otherwise, do nothing more.
2705 */
2706 if (cpu_event)
2707 cpu_ctx_sched_out(cpuctx, ctx_event_type);
2708 else if (ctx_event_type & EVENT_PINNED)
2709 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2710
Peter Zijlstra3e349502016-01-08 10:01:18 +01002711 perf_event_sched_in(cpuctx, task_ctx, current);
2712 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstra00179602015-11-30 16:26:35 +01002713}
2714
Stephane Eranianc68d2242019-04-08 10:32:51 -07002715void perf_pmu_resched(struct pmu *pmu)
2716{
2717 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2718 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2719
2720 perf_ctx_lock(cpuctx, task_ctx);
2721 ctx_resched(cpuctx, task_ctx, EVENT_ALL|EVENT_CPU);
2722 perf_ctx_unlock(cpuctx, task_ctx);
2723}
2724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725/*
2726 * Cross CPU call to install and enable a performance event
2727 *
Peter Zijlstraa0963092016-02-24 18:45:50 +01002728 * Very similar to remote_function() + event_function() but cannot assume that
2729 * things like ctx->is_active and cpuctx->task_ctx are set.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002731static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002732{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002733 struct perf_event *event = info;
2734 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002735 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002736 struct perf_event_context *task_ctx = cpuctx->task_ctx;
Peter Zijlstra63cae122016-12-09 14:59:00 +01002737 bool reprogram = true;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002738 int ret = 0;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002739
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002740 raw_spin_lock(&cpuctx->ctx.lock);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002741 if (ctx->task) {
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002742 raw_spin_lock(&ctx->lock);
2743 task_ctx = ctx;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002744
Peter Zijlstra63cae122016-12-09 14:59:00 +01002745 reprogram = (ctx->task == current);
2746
2747 /*
2748 * If the task is running, it must be running on this CPU,
2749 * otherwise we cannot reprogram things.
2750 *
2751 * If its not running, we don't care, ctx->lock will
2752 * serialize against it becoming runnable.
2753 */
2754 if (task_curr(ctx->task) && !reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002755 ret = -ESRCH;
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002756 goto unlock;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002757 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002758
Peter Zijlstra63cae122016-12-09 14:59:00 +01002759 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002760 } else if (task_ctx) {
2761 raw_spin_lock(&task_ctx->lock);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002762 }
2763
leilei.lin33801b92018-03-06 17:36:37 +08002764#ifdef CONFIG_CGROUP_PERF
Peter Zijlstra33238c52020-03-18 20:33:37 +01002765 if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) {
leilei.lin33801b92018-03-06 17:36:37 +08002766 /*
2767 * If the current cgroup doesn't match the event's
2768 * cgroup, we should not try to schedule it.
2769 */
2770 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx);
2771 reprogram = cgroup_is_descendant(cgrp->css.cgroup,
2772 event->cgrp->css.cgroup);
2773 }
2774#endif
2775
Peter Zijlstra63cae122016-12-09 14:59:00 +01002776 if (reprogram) {
Peter Zijlstraa0963092016-02-24 18:45:50 +01002777 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2778 add_event_to_ctx(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002779 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstraa0963092016-02-24 18:45:50 +01002780 } else {
2781 add_event_to_ctx(event, ctx);
2782 }
2783
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002784unlock:
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002785 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002786
Peter Zijlstraa0963092016-02-24 18:45:50 +01002787 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002788}
2789
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002790static bool exclusive_event_installable(struct perf_event *event,
2791 struct perf_event_context *ctx);
2792
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002793/*
Peter Zijlstraa0963092016-02-24 18:45:50 +01002794 * Attach a performance event to a context.
2795 *
2796 * Very similar to event_function_call, see comment there.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002797 */
2798static void
2799perf_install_in_context(struct perf_event_context *ctx,
2800 struct perf_event *event,
2801 int cpu)
2802{
Peter Zijlstraa0963092016-02-24 18:45:50 +01002803 struct task_struct *task = READ_ONCE(ctx->task);
Peter Zijlstra39a43642016-01-11 12:46:35 +01002804
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002805 lockdep_assert_held(&ctx->mutex);
2806
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03002807 WARN_ON_ONCE(!exclusive_event_installable(event, ctx));
2808
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002809 if (event->cpu != -1)
2810 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002811
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02002812 /*
2813 * Ensures that if we can observe event->ctx, both the event and ctx
2814 * will be 'complete'. See perf_iterate_sb_cpu().
2815 */
2816 smp_store_release(&event->ctx, ctx);
2817
Peter Zijlstradb0503e2019-10-21 16:02:39 +02002818 /*
2819 * perf_event_attr::disabled events will not run and can be initialized
2820 * without IPI. Except when this is the first event for the context, in
2821 * that case we need the magic of the IPI to set ctx->is_active.
2822 *
2823 * The IOC_ENABLE that is sure to follow the creation of a disabled
2824 * event will issue the IPI and reprogram the hardware.
2825 */
2826 if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && ctx->nr_events) {
2827 raw_spin_lock_irq(&ctx->lock);
2828 if (ctx->task == TASK_TOMBSTONE) {
2829 raw_spin_unlock_irq(&ctx->lock);
2830 return;
2831 }
2832 add_event_to_ctx(event, ctx);
2833 raw_spin_unlock_irq(&ctx->lock);
2834 return;
2835 }
2836
Peter Zijlstraa0963092016-02-24 18:45:50 +01002837 if (!task) {
2838 cpu_function_call(cpu, __perf_install_in_context, event);
Peter Zijlstra63b6da32016-01-14 16:05:37 +01002839 return;
2840 }
Peter Zijlstra6f932e52016-02-24 18:45:43 +01002841
Peter Zijlstraa0963092016-02-24 18:45:50 +01002842 /*
2843 * Should not happen, we validate the ctx is still alive before calling.
2844 */
2845 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2846 return;
2847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002848 /*
2849 * Installing events is tricky because we cannot rely on ctx->is_active
2850 * to be set in case this is the nr_events 0 -> 1 transition.
Peter Zijlstra63cae122016-12-09 14:59:00 +01002851 *
2852 * Instead we use task_curr(), which tells us if the task is running.
2853 * However, since we use task_curr() outside of rq::lock, we can race
2854 * against the actual state. This means the result can be wrong.
2855 *
2856 * If we get a false positive, we retry, this is harmless.
2857 *
2858 * If we get a false negative, things are complicated. If we are after
2859 * perf_event_context_sched_in() ctx::lock will serialize us, and the
2860 * value must be correct. If we're before, it doesn't matter since
2861 * perf_event_context_sched_in() will program the counter.
2862 *
2863 * However, this hinges on the remote context switch having observed
2864 * our task->perf_event_ctxp[] store, such that it will in fact take
2865 * ctx::lock in perf_event_context_sched_in().
2866 *
2867 * We do this by task_function_call(), if the IPI fails to hit the task
2868 * we know any future context switch of task must see the
2869 * perf_event_ctpx[] store.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002870 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002871
Peter Zijlstraa0963092016-02-24 18:45:50 +01002872 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002873 * This smp_mb() orders the task->perf_event_ctxp[] store with the
2874 * task_cpu() load, such that if the IPI then does not find the task
2875 * running, a future context switch of that task must observe the
2876 * store.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002877 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002878 smp_mb();
2879again:
2880 if (!task_function_call(task, __perf_install_in_context, event))
Peter Zijlstraa0963092016-02-24 18:45:50 +01002881 return;
2882
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002883 raw_spin_lock_irq(&ctx->lock);
2884 task = ctx->task;
Peter Zijlstraa0963092016-02-24 18:45:50 +01002885 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
2886 /*
2887 * Cannot happen because we already checked above (which also
2888 * cannot happen), and we hold ctx->mutex, which serializes us
2889 * against perf_event_exit_task_context().
2890 */
Peter Zijlstra39a43642016-01-11 12:46:35 +01002891 raw_spin_unlock_irq(&ctx->lock);
2892 return;
2893 }
Peter Zijlstraa0963092016-02-24 18:45:50 +01002894 /*
Peter Zijlstra63cae122016-12-09 14:59:00 +01002895 * If the task is not running, ctx->lock will avoid it becoming so,
2896 * thus we can safely install the event.
Peter Zijlstraa0963092016-02-24 18:45:50 +01002897 */
Peter Zijlstra63cae122016-12-09 14:59:00 +01002898 if (task_curr(task)) {
2899 raw_spin_unlock_irq(&ctx->lock);
2900 goto again;
2901 }
2902 add_event_to_ctx(event, ctx);
2903 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904}
2905
2906/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907 * Cross CPU call to enable a performance event
2908 */
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002909static void __perf_event_enable(struct perf_event *event,
2910 struct perf_cpu_context *cpuctx,
2911 struct perf_event_context *ctx,
2912 void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914 struct perf_event *leader = event->group_leader;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002915 struct perf_event_context *task_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002916
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002917 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2918 event->state <= PERF_EVENT_STATE_ERROR)
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002919 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002920
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002921 if (ctx->is_active)
2922 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2923
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02002924 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Peter Zijlstra33238c52020-03-18 20:33:37 +01002925 perf_cgroup_event_enable(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002926
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002927 if (!ctx->is_active)
2928 return;
2929
Stephane Eraniane5d13672011-02-14 11:20:01 +02002930 if (!event_filter_match(event)) {
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002931 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002932 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002933 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002934
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935 /*
2936 * If the event is in a group and isn't the group leader,
2937 * then don't put it on unless the group is on.
2938 */
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002939 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2940 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002941 return;
Peter Zijlstrabd2afa42016-02-24 18:45:49 +01002942 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002943
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002944 task_ctx = cpuctx->task_ctx;
2945 if (ctx->task)
2946 WARN_ON_ONCE(task_ctx != ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947
Alexander Shishkin487f05e2017-01-19 18:43:30 +02002948 ctx_resched(cpuctx, task_ctx, get_event_type(event));
Peter Zijlstra7b648012015-12-03 18:35:21 +01002949}
2950
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951/*
Tobias Tefke788faab2018-07-09 12:57:15 +02002952 * Enable an event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002953 *
2954 * If event->ctx is a cloned context, callers must make sure that
2955 * every task struct that event->ctx->task could possibly point to
2956 * remains valid. This condition is satisfied when called through
2957 * perf_event_for_each_child or perf_event_for_each as described
2958 * for perf_event_disable.
2959 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002960static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002961{
2962 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002963
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002964 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra6e801e012016-01-26 12:17:08 +01002965 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2966 event->state < PERF_EVENT_STATE_ERROR) {
Peter Zijlstra7b648012015-12-03 18:35:21 +01002967 raw_spin_unlock_irq(&ctx->lock);
2968 return;
2969 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970
2971 /*
2972 * If the event is in error state, clear that first.
Peter Zijlstra7b648012015-12-03 18:35:21 +01002973 *
2974 * That way, if we see the event in error state below, we know that it
2975 * has gone back into error state, as distinct from the task having
2976 * been scheduled away before the cross-call arrived.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002977 */
2978 if (event->state == PERF_EVENT_STATE_ERROR)
2979 event->state = PERF_EVENT_STATE_OFF;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002980 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002981
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01002982 event_function_call(event, __perf_event_enable, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002983}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002984
2985/*
2986 * See perf_event_disable();
2987 */
2988void perf_event_enable(struct perf_event *event)
2989{
2990 struct perf_event_context *ctx;
2991
2992 ctx = perf_event_ctx_lock(event);
2993 _perf_event_enable(event);
2994 perf_event_ctx_unlock(event, ctx);
2995}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002996EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002997
Alexander Shishkin375637b2016-04-27 18:44:46 +03002998struct stop_event_data {
2999 struct perf_event *event;
3000 unsigned int restart;
3001};
3002
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003003static int __perf_event_stop(void *info)
3004{
Alexander Shishkin375637b2016-04-27 18:44:46 +03003005 struct stop_event_data *sd = info;
3006 struct perf_event *event = sd->event;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003007
Alexander Shishkin375637b2016-04-27 18:44:46 +03003008 /* if it's already INACTIVE, do nothing */
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003009 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
3010 return 0;
3011
3012 /* matches smp_wmb() in event_sched_in() */
3013 smp_rmb();
3014
3015 /*
3016 * There is a window with interrupts enabled before we get here,
3017 * so we need to check again lest we try to stop another CPU's event.
3018 */
3019 if (READ_ONCE(event->oncpu) != smp_processor_id())
3020 return -EAGAIN;
3021
3022 event->pmu->stop(event, PERF_EF_UPDATE);
3023
Alexander Shishkin375637b2016-04-27 18:44:46 +03003024 /*
3025 * May race with the actual stop (through perf_pmu_output_stop()),
3026 * but it is only used for events with AUX ring buffer, and such
3027 * events will refuse to restart because of rb::aux_mmap_count==0,
3028 * see comments in perf_aux_output_begin().
3029 *
Tobias Tefke788faab2018-07-09 12:57:15 +02003030 * Since this is happening on an event-local CPU, no trace is lost
Alexander Shishkin375637b2016-04-27 18:44:46 +03003031 * while restarting.
3032 */
3033 if (sd->restart)
Will Deaconc9bbdd42016-08-15 11:42:45 +01003034 event->pmu->start(event, 0);
Alexander Shishkin375637b2016-04-27 18:44:46 +03003035
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02003036 return 0;
3037}
3038
Alexander Shishkin767ae082016-09-06 16:23:49 +03003039static int perf_event_stop(struct perf_event *event, int restart)
Alexander Shishkin375637b2016-04-27 18:44:46 +03003040{
3041 struct stop_event_data sd = {
3042 .event = event,
Alexander Shishkin767ae082016-09-06 16:23:49 +03003043 .restart = restart,
Alexander Shishkin375637b2016-04-27 18:44:46 +03003044 };
3045 int ret = 0;
3046
3047 do {
3048 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
3049 return 0;
3050
3051 /* matches smp_wmb() in event_sched_in() */
3052 smp_rmb();
3053
3054 /*
3055 * We only want to restart ACTIVE events, so if the event goes
3056 * inactive here (event->oncpu==-1), there's nothing more to do;
3057 * fall through with ret==-ENXIO.
3058 */
3059 ret = cpu_function_call(READ_ONCE(event->oncpu),
3060 __perf_event_stop, &sd);
3061 } while (ret == -EAGAIN);
3062
3063 return ret;
3064}
3065
3066/*
3067 * In order to contain the amount of racy and tricky in the address filter
3068 * configuration management, it is a two part process:
3069 *
3070 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
3071 * we update the addresses of corresponding vmas in
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02003072 * event::addr_filter_ranges array and bump the event::addr_filters_gen;
Alexander Shishkin375637b2016-04-27 18:44:46 +03003073 * (p2) when an event is scheduled in (pmu::add), it calls
3074 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
3075 * if the generation has changed since the previous call.
3076 *
3077 * If (p1) happens while the event is active, we restart it to force (p2).
3078 *
3079 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
3080 * pre-existing mappings, called once when new filters arrive via SET_FILTER
3081 * ioctl;
3082 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07003083 * registered mapping, called for every new mmap(), with mm::mmap_lock down
Alexander Shishkin375637b2016-04-27 18:44:46 +03003084 * for reading;
3085 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
3086 * of exec.
3087 */
3088void perf_event_addr_filters_sync(struct perf_event *event)
3089{
3090 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
3091
3092 if (!has_addr_filter(event))
3093 return;
3094
3095 raw_spin_lock(&ifh->lock);
3096 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
3097 event->pmu->addr_filters_sync(event);
3098 event->hw.addr_filters_gen = event->addr_filters_gen;
3099 }
3100 raw_spin_unlock(&ifh->lock);
3101}
3102EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
3103
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003104static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105{
3106 /*
3107 * not supported on inherited events
3108 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01003109 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003110 return -EINVAL;
3111
3112 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003113 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114
3115 return 0;
3116}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003117
3118/*
3119 * See perf_event_disable()
3120 */
3121int perf_event_refresh(struct perf_event *event, int refresh)
3122{
3123 struct perf_event_context *ctx;
3124 int ret;
3125
3126 ctx = perf_event_ctx_lock(event);
3127 ret = _perf_event_refresh(event, refresh);
3128 perf_event_ctx_unlock(event, ctx);
3129
3130 return ret;
3131}
Avi Kivity26ca5c12011-06-29 18:42:37 +03003132EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003133
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003134static int perf_event_modify_breakpoint(struct perf_event *bp,
3135 struct perf_event_attr *attr)
3136{
3137 int err;
3138
3139 _perf_event_disable(bp);
3140
3141 err = modify_user_hw_breakpoint_check(bp, attr, true);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003142
Jiri Olsabf062782018-08-27 11:12:28 +02003143 if (!bp->attr.disabled)
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003144 _perf_event_enable(bp);
Jiri Olsabf062782018-08-27 11:12:28 +02003145
3146 return err;
Milind Chabbi32ff77e2018-03-12 14:45:47 +01003147}
3148
3149static int perf_event_modify_attr(struct perf_event *event,
3150 struct perf_event_attr *attr)
3151{
3152 if (event->attr.type != attr->type)
3153 return -EINVAL;
3154
3155 switch (event->attr.type) {
3156 case PERF_TYPE_BREAKPOINT:
3157 return perf_event_modify_breakpoint(event, attr);
3158 default:
3159 /* Place holder for future additions. */
3160 return -EOPNOTSUPP;
3161 }
3162}
3163
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003164static void ctx_sched_out(struct perf_event_context *ctx,
3165 struct perf_cpu_context *cpuctx,
3166 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167{
Peter Zijlstra66681282017-11-13 14:28:38 +01003168 struct perf_event *event, *tmp;
Peter Zijlstradb24d332011-04-09 21:17:45 +02003169 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003170
3171 lockdep_assert_held(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003172
Peter Zijlstra39a43642016-01-11 12:46:35 +01003173 if (likely(!ctx->nr_events)) {
3174 /*
3175 * See __perf_remove_from_context().
3176 */
3177 WARN_ON_ONCE(ctx->is_active);
3178 if (ctx->task)
3179 WARN_ON_ONCE(cpuctx->task_ctx);
3180 return;
3181 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003182
Peter Zijlstradb24d332011-04-09 21:17:45 +02003183 ctx->is_active &= ~event_type;
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003184 if (!(ctx->is_active & EVENT_ALL))
3185 ctx->is_active = 0;
3186
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003187 if (ctx->task) {
3188 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3189 if (!ctx->is_active)
3190 cpuctx->task_ctx = NULL;
3191 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003192
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003193 /*
3194 * Always update time if it was set; not only when it changes.
3195 * Otherwise we can 'forget' to update time for any but the last
3196 * context we sched out. For example:
3197 *
3198 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
3199 * ctx_sched_out(.event_type = EVENT_PINNED)
3200 *
3201 * would only update time for the pinned events.
3202 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003203 if (is_active & EVENT_TIME) {
3204 /* update (and stop) ctx time */
3205 update_context_time(ctx);
3206 update_cgrp_time_from_cpuctx(cpuctx);
3207 }
3208
Peter Zijlstra8fdc6532016-03-29 09:26:44 +02003209 is_active ^= ctx->is_active; /* changed bits */
3210
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003211 if (!ctx->nr_active || !(is_active & EVENT_ALL))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003212 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003213
Peter Zijlstra075e0b02011-04-09 21:17:40 +02003214 perf_pmu_disable(ctx->pmu);
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003215 if (is_active & EVENT_PINNED) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003216 list_for_each_entry_safe(event, tmp, &ctx->pinned_active, active_list)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003217 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003218 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003219
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003220 if (is_active & EVENT_FLEXIBLE) {
Peter Zijlstra66681282017-11-13 14:28:38 +01003221 list_for_each_entry_safe(event, tmp, &ctx->flexible_active, active_list)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08003222 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra90c91df2020-03-05 13:38:51 +01003223
3224 /*
3225 * Since we cleared EVENT_FLEXIBLE, also clear
3226 * rotate_necessary, is will be reset by
3227 * ctx_flexible_sched_in() when needed.
3228 */
3229 ctx->rotate_necessary = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003230 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003231 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232}
3233
3234/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003235 * Test whether two contexts are equivalent, i.e. whether they have both been
3236 * cloned from the same version of the same context.
3237 *
3238 * Equivalence is measured using a generation number in the context that is
3239 * incremented on each modification to it; see unclone_ctx(), list_add_event()
3240 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003241 */
3242static int context_equiv(struct perf_event_context *ctx1,
3243 struct perf_event_context *ctx2)
3244{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003245 lockdep_assert_held(&ctx1->lock);
3246 lockdep_assert_held(&ctx2->lock);
3247
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003248 /* Pinning disables the swap optimization */
3249 if (ctx1->pin_count || ctx2->pin_count)
3250 return 0;
3251
3252 /* If ctx1 is the parent of ctx2 */
3253 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
3254 return 1;
3255
3256 /* If ctx2 is the parent of ctx1 */
3257 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
3258 return 1;
3259
3260 /*
3261 * If ctx1 and ctx2 have the same parent; we flatten the parent
3262 * hierarchy, see perf_event_init_context().
3263 */
3264 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
3265 ctx1->parent_gen == ctx2->parent_gen)
3266 return 1;
3267
3268 /* Unmatched */
3269 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003270}
3271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003272static void __perf_event_sync_stat(struct perf_event *event,
3273 struct perf_event *next_event)
3274{
3275 u64 value;
3276
3277 if (!event->attr.inherit_stat)
3278 return;
3279
3280 /*
3281 * Update the event value, we cannot use perf_event_read()
3282 * because we're in the middle of a context switch and have IRQs
3283 * disabled, which upsets smp_call_function_single(), however
3284 * we know the event must be on the current CPU, therefore we
3285 * don't need to use it.
3286 */
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003287 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01003288 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02003290 perf_event_update_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003291
3292 /*
3293 * In order to keep per-task stats reliable we need to flip the event
3294 * values when we flip the contexts.
3295 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02003296 value = local64_read(&next_event->count);
3297 value = local64_xchg(&event->count, value);
3298 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299
3300 swap(event->total_time_enabled, next_event->total_time_enabled);
3301 swap(event->total_time_running, next_event->total_time_running);
3302
3303 /*
3304 * Since we swizzled the values, update the user visible data too.
3305 */
3306 perf_event_update_userpage(event);
3307 perf_event_update_userpage(next_event);
3308}
3309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310static void perf_event_sync_stat(struct perf_event_context *ctx,
3311 struct perf_event_context *next_ctx)
3312{
3313 struct perf_event *event, *next_event;
3314
3315 if (!ctx->nr_stat)
3316 return;
3317
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01003318 update_context_time(ctx);
3319
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003320 event = list_first_entry(&ctx->event_list,
3321 struct perf_event, event_entry);
3322
3323 next_event = list_first_entry(&next_ctx->event_list,
3324 struct perf_event, event_entry);
3325
3326 while (&event->event_entry != &ctx->event_list &&
3327 &next_event->event_entry != &next_ctx->event_list) {
3328
3329 __perf_event_sync_stat(event, next_event);
3330
3331 event = list_next_entry(event, event_entry);
3332 next_event = list_next_entry(next_event, event_entry);
3333 }
3334}
3335
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003336static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
3337 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003339 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003341 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003342 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343 int do_switch = 1;
3344
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003345 if (likely(!ctx))
3346 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003348 cpuctx = __get_cpu_context(ctx);
3349 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350 return;
3351
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003353 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003354 if (!next_ctx)
3355 goto unlock;
3356
3357 parent = rcu_dereference(ctx->parent_ctx);
3358 next_parent = rcu_dereference(next_ctx->parent_ctx);
3359
3360 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02003361 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003362 goto unlock;
3363
3364 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365 /*
3366 * Looks like the two contexts are clones, so we might be
3367 * able to optimize the context switch. We lock both
3368 * contexts and check that they are clones under the
3369 * lock (including re-checking that neither has been
3370 * uncloned in the meantime). It doesn't matter which
3371 * order we take the locks because no other cpu could
3372 * be trying to lock both of these tasks.
3373 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003374 raw_spin_lock(&ctx->lock);
3375 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376 if (context_equiv(ctx, next_ctx)) {
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003377 struct pmu *pmu = ctx->pmu;
3378
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003379 WRITE_ONCE(ctx->task, next);
3380 WRITE_ONCE(next_ctx->task, task);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003381
Alexey Budankovc2b98a82019-10-23 10:13:56 +03003382 /*
3383 * PMU specific parts of task perf context can require
3384 * additional synchronization. As an example of such
3385 * synchronization see implementation details of Intel
3386 * LBR call stack data profiling;
3387 */
3388 if (pmu->swap_task_ctx)
3389 pmu->swap_task_ctx(ctx, next_ctx);
3390 else
3391 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
Yan, Zheng5a158c32014-11-04 21:56:02 -05003392
Peter Zijlstra63b6da32016-01-14 16:05:37 +01003393 /*
3394 * RCU_INIT_POINTER here is safe because we've not
3395 * modified the ctx and the above modification of
3396 * ctx->task and ctx->task_ctx_data are immaterial
3397 * since those values are always verified under
3398 * ctx->lock which we're now holding.
3399 */
3400 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
3401 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
3402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403 do_switch = 0;
3404
3405 perf_event_sync_stat(ctx, next_ctx);
3406 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003407 raw_spin_unlock(&next_ctx->lock);
3408 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02003410unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411 rcu_read_unlock();
3412
3413 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003414 raw_spin_lock(&ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02003415 task_ctx_sched_out(cpuctx, ctx, EVENT_ALL);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003416 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003417 }
3418}
3419
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003420static DEFINE_PER_CPU(struct list_head, sched_cb_list);
3421
Yan, Zhengba532502014-11-04 21:55:58 -05003422void perf_sched_cb_dec(struct pmu *pmu)
3423{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003424 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3425
Yan, Zhengba532502014-11-04 21:55:58 -05003426 this_cpu_dec(perf_sched_cb_usages);
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003427
3428 if (!--cpuctx->sched_cb_usage)
3429 list_del(&cpuctx->sched_cb_entry);
Yan, Zhengba532502014-11-04 21:55:58 -05003430}
3431
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003432
Yan, Zhengba532502014-11-04 21:55:58 -05003433void perf_sched_cb_inc(struct pmu *pmu)
3434{
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003435 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3436
3437 if (!cpuctx->sched_cb_usage++)
3438 list_add(&cpuctx->sched_cb_entry, this_cpu_ptr(&sched_cb_list));
3439
Yan, Zhengba532502014-11-04 21:55:58 -05003440 this_cpu_inc(perf_sched_cb_usages);
3441}
3442
3443/*
3444 * This function provides the context switch callback to the lower code
3445 * layer. It is invoked ONLY when the context switch callback is enabled.
Peter Zijlstra09e61b4f2016-07-06 18:02:43 +02003446 *
3447 * This callback is relevant even to per-cpu events; for example multi event
3448 * PEBS requires this to provide PID/TID information. This requires we flush
3449 * all queued PEBS records before we context switch to a new task.
Yan, Zhengba532502014-11-04 21:55:58 -05003450 */
3451static void perf_pmu_sched_task(struct task_struct *prev,
3452 struct task_struct *next,
3453 bool sched_in)
3454{
3455 struct perf_cpu_context *cpuctx;
3456 struct pmu *pmu;
Yan, Zhengba532502014-11-04 21:55:58 -05003457
3458 if (prev == next)
3459 return;
3460
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003461 list_for_each_entry(cpuctx, this_cpu_ptr(&sched_cb_list), sched_cb_entry) {
David Carrillo-Cisneros1fd7e412017-01-18 11:24:54 -08003462 pmu = cpuctx->ctx.pmu; /* software PMUs will not have sched_task */
Yan, Zhengba532502014-11-04 21:55:58 -05003463
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003464 if (WARN_ON_ONCE(!pmu->sched_task))
3465 continue;
Yan, Zhengba532502014-11-04 21:55:58 -05003466
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003467 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
3468 perf_pmu_disable(pmu);
Yan, Zhengba532502014-11-04 21:55:58 -05003469
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003470 pmu->sched_task(cpuctx->task_ctx, sched_in);
Yan, Zhengba532502014-11-04 21:55:58 -05003471
Peter Zijlstrae48c1782016-07-06 09:18:30 +02003472 perf_pmu_enable(pmu);
3473 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Yan, Zhengba532502014-11-04 21:55:58 -05003474 }
Yan, Zhengba532502014-11-04 21:55:58 -05003475}
3476
Adrian Hunter45ac1402015-07-21 12:44:02 +03003477static void perf_event_switch(struct task_struct *task,
3478 struct task_struct *next_prev, bool sched_in);
3479
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003480#define for_each_task_context_nr(ctxn) \
3481 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
3482
3483/*
3484 * Called from scheduler to remove the events of the current task,
3485 * with interrupts disabled.
3486 *
3487 * We stop each event and update the event value in event->count.
3488 *
3489 * This does not protect us against NMI, but disable()
3490 * sets the disabled bit in the control field of event _before_
3491 * accessing the event control register. If a NMI hits, then it will
3492 * not restart the event.
3493 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003494void __perf_event_task_sched_out(struct task_struct *task,
3495 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003496{
3497 int ctxn;
3498
Yan, Zhengba532502014-11-04 21:55:58 -05003499 if (__this_cpu_read(perf_sched_cb_usages))
3500 perf_pmu_sched_task(task, next, false);
3501
Adrian Hunter45ac1402015-07-21 12:44:02 +03003502 if (atomic_read(&nr_switch_events))
3503 perf_event_switch(task, next, false);
3504
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003505 for_each_task_context_nr(ctxn)
3506 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003507
3508 /*
3509 * if cgroup events exist on this CPU, then we need
3510 * to check if we have to switch out PMU state.
3511 * cgroup event are system-wide mode only
3512 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003513 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003514 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003515}
3516
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003517/*
3518 * Called with IRQs disabled
3519 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003520static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
3521 enum event_type_t event_type)
3522{
3523 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003524}
3525
Ian Rogers6eef8a712020-02-13 23:51:30 -08003526static bool perf_less_group_idx(const void *l, const void *r)
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003527{
Ian Rogers24fb6b82020-03-21 09:43:31 -07003528 const struct perf_event *le = *(const struct perf_event **)l;
3529 const struct perf_event *re = *(const struct perf_event **)r;
Ian Rogers6eef8a712020-02-13 23:51:30 -08003530
3531 return le->group_index < re->group_index;
3532}
3533
3534static void swap_ptr(void *l, void *r)
3535{
3536 void **lp = l, **rp = r;
3537
3538 swap(*lp, *rp);
3539}
3540
3541static const struct min_heap_callbacks perf_min_heap = {
3542 .elem_size = sizeof(struct perf_event *),
3543 .less = perf_less_group_idx,
3544 .swp = swap_ptr,
3545};
3546
3547static void __heap_add(struct min_heap *heap, struct perf_event *event)
3548{
3549 struct perf_event **itrs = heap->data;
3550
3551 if (event) {
3552 itrs[heap->nr] = event;
3553 heap->nr++;
3554 }
3555}
3556
Ian Rogers836196be2020-02-13 23:51:31 -08003557static noinline int visit_groups_merge(struct perf_cpu_context *cpuctx,
3558 struct perf_event_groups *groups, int cpu,
Ian Rogers6eef8a712020-02-13 23:51:30 -08003559 int (*func)(struct perf_event *, void *),
3560 void *data)
3561{
Ian Rogers95ed6c72020-02-13 23:51:33 -08003562#ifdef CONFIG_CGROUP_PERF
3563 struct cgroup_subsys_state *css = NULL;
3564#endif
Ian Rogers6eef8a712020-02-13 23:51:30 -08003565 /* Space for per CPU and/or any CPU event iterators. */
3566 struct perf_event *itrs[2];
Ian Rogers836196be2020-02-13 23:51:31 -08003567 struct min_heap event_heap;
3568 struct perf_event **evt;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003569 int ret;
3570
Ian Rogers836196be2020-02-13 23:51:31 -08003571 if (cpuctx) {
3572 event_heap = (struct min_heap){
3573 .data = cpuctx->heap,
3574 .nr = 0,
3575 .size = cpuctx->heap_size,
3576 };
Ian Rogersc2283c92020-02-13 23:51:32 -08003577
3578 lockdep_assert_held(&cpuctx->ctx.lock);
Ian Rogers95ed6c72020-02-13 23:51:33 -08003579
3580#ifdef CONFIG_CGROUP_PERF
3581 if (cpuctx->cgrp)
3582 css = &cpuctx->cgrp->css;
3583#endif
Ian Rogers836196be2020-02-13 23:51:31 -08003584 } else {
3585 event_heap = (struct min_heap){
3586 .data = itrs,
3587 .nr = 0,
3588 .size = ARRAY_SIZE(itrs),
3589 };
3590 /* Events not within a CPU context may be on any CPU. */
Ian Rogers95ed6c72020-02-13 23:51:33 -08003591 __heap_add(&event_heap, perf_event_groups_first(groups, -1, NULL));
Ian Rogers836196be2020-02-13 23:51:31 -08003592 }
3593 evt = event_heap.data;
3594
Ian Rogers95ed6c72020-02-13 23:51:33 -08003595 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, NULL));
3596
3597#ifdef CONFIG_CGROUP_PERF
3598 for (; css; css = css->parent)
3599 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, css->cgroup));
3600#endif
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003601
Ian Rogers6eef8a712020-02-13 23:51:30 -08003602 min_heapify_all(&event_heap, &perf_min_heap);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003603
Ian Rogers6eef8a712020-02-13 23:51:30 -08003604 while (event_heap.nr) {
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003605 ret = func(*evt, data);
3606 if (ret)
3607 return ret;
3608
3609 *evt = perf_event_groups_next(*evt);
Ian Rogers6eef8a712020-02-13 23:51:30 -08003610 if (*evt)
3611 min_heapify(&event_heap, 0, &perf_min_heap);
3612 else
3613 min_heap_pop(&event_heap, &perf_min_heap);
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003614 }
3615
3616 return 0;
3617}
3618
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003619static int merge_sched_in(struct perf_event *event, void *data)
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003620{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003621 struct perf_event_context *ctx = event->ctx;
3622 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
3623 int *can_add_hw = data;
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003624
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003625 if (event->state <= PERF_EVENT_STATE_OFF)
3626 return 0;
3627
3628 if (!event_filter_match(event))
3629 return 0;
3630
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003631 if (group_can_go_on(event, cpuctx, *can_add_hw)) {
3632 if (!group_sched_in(event, cpuctx, ctx))
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003633 list_add_tail(&event->active_list, get_event_list(event));
Peter Zijlstra66681282017-11-13 14:28:38 +01003634 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003635
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003636 if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra33238c52020-03-18 20:33:37 +01003637 if (event->attr.pinned) {
3638 perf_cgroup_event_disable(event, ctx);
Peter Zijlstraab6f8242019-08-07 11:17:00 +02003639 perf_event_set_state(event, PERF_EVENT_STATE_ERROR);
Peter Zijlstra33238c52020-03-18 20:33:37 +01003640 }
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003641
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003642 *can_add_hw = 0;
3643 ctx->rotate_necessary = 1;
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003644 }
3645
3646 return 0;
3647}
3648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003650ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003651 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003652{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003653 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654
Ian Rogers836196be2020-02-13 23:51:31 -08003655 if (ctx != &cpuctx->ctx)
3656 cpuctx = NULL;
3657
3658 visit_groups_merge(cpuctx, &ctx->pinned_groups,
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003659 smp_processor_id(),
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003660 merge_sched_in, &can_add_hw);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003661}
3662
3663static void
3664ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01003665 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003666{
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003667 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003668
Ian Rogers836196be2020-02-13 23:51:31 -08003669 if (ctx != &cpuctx->ctx)
3670 cpuctx = NULL;
3671
3672 visit_groups_merge(cpuctx, &ctx->flexible_groups,
Peter Zijlstra1cac7b12017-11-13 14:28:30 +01003673 smp_processor_id(),
Peter Zijlstra2c2366c2019-08-07 11:45:01 +02003674 merge_sched_in, &can_add_hw);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003675}
3676
3677static void
3678ctx_sched_in(struct perf_event_context *ctx,
3679 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003680 enum event_type_t event_type,
3681 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003682{
Peter Zijlstradb24d332011-04-09 21:17:45 +02003683 int is_active = ctx->is_active;
Peter Zijlstrac994d612016-01-08 09:20:23 +01003684 u64 now;
Stephane Eraniane5d13672011-02-14 11:20:01 +02003685
Peter Zijlstrac994d612016-01-08 09:20:23 +01003686 lockdep_assert_held(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003687
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003688 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003689 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003690
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003691 ctx->is_active |= (event_type | EVENT_TIME);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003692 if (ctx->task) {
3693 if (!is_active)
3694 cpuctx->task_ctx = ctx;
3695 else
3696 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
3697 }
3698
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003699 is_active ^= ctx->is_active; /* changed bits */
3700
3701 if (is_active & EVENT_TIME) {
3702 /* start ctx time */
3703 now = perf_clock();
3704 ctx->timestamp = now;
3705 perf_cgroup_set_timestamp(task, ctx);
3706 }
3707
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003708 /*
3709 * First go through the list and put on any pinned groups
3710 * in order to give them the best chance of going on.
3711 */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003712 if (is_active & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003713 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01003714
3715 /* Then walk through the lower prio flexible groups */
Peter Zijlstra3cbaa592016-02-24 18:45:47 +01003716 if (is_active & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01003717 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003718}
3719
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003720static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02003721 enum event_type_t event_type,
3722 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003723{
3724 struct perf_event_context *ctx = &cpuctx->ctx;
3725
Stephane Eraniane5d13672011-02-14 11:20:01 +02003726 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003727}
3728
Stephane Eraniane5d13672011-02-14 11:20:01 +02003729static void perf_event_context_sched_in(struct perf_event_context *ctx,
3730 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003731{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003732 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003734 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003735 if (cpuctx->task_ctx == ctx)
3736 return;
3737
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003738 perf_ctx_lock(cpuctx, ctx);
leilei.linfdccc3f2017-08-09 08:29:21 +08003739 /*
3740 * We must check ctx->nr_events while holding ctx->lock, such
3741 * that we serialize against perf_install_in_context().
3742 */
3743 if (!ctx->nr_events)
3744 goto unlock;
3745
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003746 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003747 /*
3748 * We want to keep the following priority order:
3749 * cpu pinned (that don't need to move), task pinned,
3750 * cpu flexible, task flexible.
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003751 *
3752 * However, if task's ctx is not carrying any pinned
3753 * events, no need to flip the cpuctx's events around.
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01003754 */
Alexey Budankov8e1a2032017-09-08 11:47:03 +03003755 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree))
Alexander Shishkinfe45baf2017-01-19 18:43:29 +02003756 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra63e30d32016-01-08 11:39:10 +01003757 perf_event_sched_in(cpuctx, ctx, task);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003758 perf_pmu_enable(ctx->pmu);
leilei.linfdccc3f2017-08-09 08:29:21 +08003759
3760unlock:
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003761 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762}
3763
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003764/*
3765 * Called from scheduler to add the events of the current task
3766 * with interrupts disabled.
3767 *
3768 * We restore the event value and then enable it.
3769 *
3770 * This does not protect us against NMI, but enable()
3771 * sets the enabled bit in the control field of event _before_
3772 * accessing the event control register. If a NMI hits, then it will
3773 * keep the event running.
3774 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02003775void __perf_event_task_sched_in(struct task_struct *prev,
3776 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003777{
3778 struct perf_event_context *ctx;
3779 int ctxn;
3780
Peter Zijlstra7e41d172016-01-08 09:21:40 +01003781 /*
3782 * If cgroup events exist on this CPU, then we need to check if we have
3783 * to switch in PMU state; cgroup event are system-wide mode only.
3784 *
3785 * Since cgroup events are CPU events, we must schedule these in before
3786 * we schedule in the task events.
3787 */
3788 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3789 perf_cgroup_sched_in(prev, task);
3790
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003791 for_each_task_context_nr(ctxn) {
3792 ctx = task->perf_event_ctxp[ctxn];
3793 if (likely(!ctx))
3794 continue;
3795
Stephane Eraniane5d13672011-02-14 11:20:01 +02003796 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003797 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003798
Adrian Hunter45ac1402015-07-21 12:44:02 +03003799 if (atomic_read(&nr_switch_events))
3800 perf_event_switch(task, prev, true);
3801
Yan, Zhengba532502014-11-04 21:55:58 -05003802 if (__this_cpu_read(perf_sched_cb_usages))
3803 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003804}
3805
Peter Zijlstraabd50712010-01-26 18:50:16 +01003806static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3807{
3808 u64 frequency = event->attr.sample_freq;
3809 u64 sec = NSEC_PER_SEC;
3810 u64 divisor, dividend;
3811
3812 int count_fls, nsec_fls, frequency_fls, sec_fls;
3813
3814 count_fls = fls64(count);
3815 nsec_fls = fls64(nsec);
3816 frequency_fls = fls64(frequency);
3817 sec_fls = 30;
3818
3819 /*
3820 * We got @count in @nsec, with a target of sample_freq HZ
3821 * the target period becomes:
3822 *
3823 * @count * 10^9
3824 * period = -------------------
3825 * @nsec * sample_freq
3826 *
3827 */
3828
3829 /*
3830 * Reduce accuracy by one bit such that @a and @b converge
3831 * to a similar magnitude.
3832 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003833#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01003834do { \
3835 if (a##_fls > b##_fls) { \
3836 a >>= 1; \
3837 a##_fls--; \
3838 } else { \
3839 b >>= 1; \
3840 b##_fls--; \
3841 } \
3842} while (0)
3843
3844 /*
3845 * Reduce accuracy until either term fits in a u64, then proceed with
3846 * the other, so that finally we can do a u64/u64 division.
3847 */
3848 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3849 REDUCE_FLS(nsec, frequency);
3850 REDUCE_FLS(sec, count);
3851 }
3852
3853 if (count_fls + sec_fls > 64) {
3854 divisor = nsec * frequency;
3855
3856 while (count_fls + sec_fls > 64) {
3857 REDUCE_FLS(count, sec);
3858 divisor >>= 1;
3859 }
3860
3861 dividend = count * sec;
3862 } else {
3863 dividend = count * sec;
3864
3865 while (nsec_fls + frequency_fls > 64) {
3866 REDUCE_FLS(nsec, frequency);
3867 dividend >>= 1;
3868 }
3869
3870 divisor = nsec * frequency;
3871 }
3872
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003873 if (!divisor)
3874 return dividend;
3875
Peter Zijlstraabd50712010-01-26 18:50:16 +01003876 return div64_u64(dividend, divisor);
3877}
3878
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003879static DEFINE_PER_CPU(int, perf_throttled_count);
3880static DEFINE_PER_CPU(u64, perf_throttled_seq);
3881
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003882static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003883{
3884 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02003885 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003886 s64 delta;
3887
Peter Zijlstraabd50712010-01-26 18:50:16 +01003888 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003889
3890 delta = (s64)(period - hwc->sample_period);
3891 delta = (delta + 7) / 8; /* low pass filter */
3892
3893 sample_period = hwc->sample_period + delta;
3894
3895 if (!sample_period)
3896 sample_period = 1;
3897
3898 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003899
Peter Zijlstrae7850592010-05-21 14:43:08 +02003900 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003901 if (disable)
3902 event->pmu->stop(event, PERF_EF_UPDATE);
3903
Peter Zijlstrae7850592010-05-21 14:43:08 +02003904 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003905
3906 if (disable)
3907 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003908 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909}
3910
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003911/*
3912 * combine freq adjustment with unthrottling to avoid two passes over the
3913 * events. At the same time, make sure, having freq events does not change
3914 * the rate of unthrottling as that would introduce bias.
3915 */
3916static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3917 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918{
3919 struct perf_event *event;
3920 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003921 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003922 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003923
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003924 /*
3925 * only need to iterate over all events iff:
3926 * - context have events in frequency mode (needs freq adjust)
3927 * - there are events to unthrottle on this cpu
3928 */
3929 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003930 return;
3931
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003932 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003933 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003934
Paul Mackerras03541f82009-10-14 16:58:03 +11003935 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003936 if (event->state != PERF_EVENT_STATE_ACTIVE)
3937 continue;
3938
Stephane Eranian5632ab12011-01-03 18:20:01 +02003939 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003940 continue;
3941
Alexander Shishkin44377272013-12-16 14:17:36 +02003942 perf_pmu_disable(event->pmu);
3943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003944 hwc = &event->hw;
3945
Jiri Olsaae23bff2013-08-24 16:45:54 +02003946 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003947 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003948 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003949 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003950 }
3951
3952 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003953 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003954
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003955 /*
3956 * stop the event and update event->count
3957 */
3958 event->pmu->stop(event, PERF_EF_UPDATE);
3959
Peter Zijlstrae7850592010-05-21 14:43:08 +02003960 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003961 delta = now - hwc->freq_count_stamp;
3962 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003964 /*
3965 * restart the event
3966 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003967 * we have stopped the event so tell that
3968 * to perf_adjust_period() to avoid stopping it
3969 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003970 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003971 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003972 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003973
3974 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003975 next:
3976 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003978
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003979 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003980 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003981}
3982
3983/*
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003984 * Move @event to the tail of the @ctx's elegible events.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003985 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003986static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003987{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003988 /*
3989 * Rotate the first entry last of non-pinned groups. Rotation might be
3990 * disabled by the inheritance code.
3991 */
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003992 if (ctx->rotate_disable)
3993 return;
Alexey Budankov8e1a2032017-09-08 11:47:03 +03003994
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01003995 perf_event_groups_delete(&ctx->flexible_groups, event);
3996 perf_event_groups_insert(&ctx->flexible_groups, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003997}
3998
Song Liu7fa343b72019-10-08 09:59:49 -07003999/* pick an event from the flexible_groups to rotate */
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004000static inline struct perf_event *
Song Liu7fa343b72019-10-08 09:59:49 -07004001ctx_event_to_rotate(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002{
Song Liu7fa343b72019-10-08 09:59:49 -07004003 struct perf_event *event;
4004
4005 /* pick the first active flexible event */
4006 event = list_first_entry_or_null(&ctx->flexible_active,
4007 struct perf_event, active_list);
4008
4009 /* if no active flexible event, pick the first event */
4010 if (!event) {
4011 event = rb_entry_safe(rb_first(&ctx->flexible_groups.tree),
4012 typeof(*event), group_node);
4013 }
4014
Peter Zijlstra90c91df2020-03-05 13:38:51 +01004015 /*
4016 * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in()
4017 * finds there are unschedulable events, it will set it again.
4018 */
4019 ctx->rotate_necessary = 0;
4020
Song Liu7fa343b72019-10-08 09:59:49 -07004021 return event;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004022}
4023
4024static bool perf_rotate_context(struct perf_cpu_context *cpuctx)
4025{
4026 struct perf_event *cpu_event = NULL, *task_event = NULL;
Ian Rogersfd7d5512019-06-01 01:27:22 -07004027 struct perf_event_context *task_ctx = NULL;
4028 int cpu_rotate, task_rotate;
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004029
4030 /*
4031 * Since we run this from IRQ context, nobody can install new
4032 * events, thus the event count values are stable.
4033 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004034
Ian Rogersfd7d5512019-06-01 01:27:22 -07004035 cpu_rotate = cpuctx->ctx.rotate_necessary;
4036 task_ctx = cpuctx->task_ctx;
4037 task_rotate = task_ctx ? task_ctx->rotate_necessary : 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004038
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004039 if (!(cpu_rotate || task_rotate))
4040 return false;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01004041
Peter Zijlstrafacc4302011-04-09 21:17:42 +02004042 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02004043 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004044
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004045 if (task_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07004046 task_event = ctx_event_to_rotate(task_ctx);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004047 if (cpu_rotate)
Song Liu7fa343b72019-10-08 09:59:49 -07004048 cpu_event = ctx_event_to_rotate(&cpuctx->ctx);
Peter Zijlstra8703a7c2017-11-13 14:28:44 +01004049
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004050 /*
4051 * As per the order given at ctx_resched() first 'pop' task flexible
4052 * and then, if needed CPU flexible.
4053 */
Ian Rogersfd7d5512019-06-01 01:27:22 -07004054 if (task_event || (task_ctx && cpu_event))
4055 ctx_sched_out(task_ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004056 if (cpu_event)
4057 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01004058
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004059 if (task_event)
Ian Rogersfd7d5512019-06-01 01:27:22 -07004060 rotate_ctx(task_ctx, task_event);
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004061 if (cpu_event)
4062 rotate_ctx(&cpuctx->ctx, cpu_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004063
Ian Rogersfd7d5512019-06-01 01:27:22 -07004064 perf_event_sched_in(cpuctx, task_ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01004065
4066 perf_pmu_enable(cpuctx->ctx.pmu);
4067 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02004068
Peter Zijlstra8d5bce02018-03-09 14:56:27 +01004069 return true;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004070}
4071
4072void perf_event_task_tick(void)
4073{
Mark Rutland2fde4f92015-01-07 15:01:54 +00004074 struct list_head *head = this_cpu_ptr(&active_ctx_list);
4075 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004076 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004077
Frederic Weisbecker16444642017-11-06 16:01:24 +01004078 lockdep_assert_irqs_disabled();
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02004079
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004080 __this_cpu_inc(perf_throttled_seq);
4081 throttled = __this_cpu_xchg(perf_throttled_count, 0);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004082 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004083
Mark Rutland2fde4f92015-01-07 15:01:54 +00004084 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004085 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004086}
4087
Frederic Weisbecker889ff012010-01-09 20:04:47 +01004088static int event_enable_on_exec(struct perf_event *event,
4089 struct perf_event_context *ctx)
4090{
4091 if (!event->attr.enable_on_exec)
4092 return 0;
4093
4094 event->attr.enable_on_exec = 0;
4095 if (event->state >= PERF_EVENT_STATE_INACTIVE)
4096 return 0;
4097
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004098 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01004099
4100 return 1;
4101}
4102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004103/*
4104 * Enable all of a task's events that have been marked enable-on-exec.
4105 * This expects task == current.
4106 */
Peter Zijlstrac1274492015-12-10 20:57:40 +01004107static void perf_event_enable_on_exec(int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108{
Peter Zijlstrac1274492015-12-10 20:57:40 +01004109 struct perf_event_context *ctx, *clone_ctx = NULL;
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004110 enum event_type_t event_type = 0;
Peter Zijlstra3e349502016-01-08 10:01:18 +01004111 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112 struct perf_event *event;
4113 unsigned long flags;
4114 int enabled = 0;
4115
4116 local_irq_save(flags);
Peter Zijlstrac1274492015-12-10 20:57:40 +01004117 ctx = current->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118 if (!ctx || !ctx->nr_events)
4119 goto out;
4120
Peter Zijlstra3e349502016-01-08 10:01:18 +01004121 cpuctx = __get_cpu_context(ctx);
4122 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra7fce2502016-02-24 18:45:48 +01004123 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004124 list_for_each_entry(event, &ctx->event_list, event_entry) {
Peter Zijlstra3e349502016-01-08 10:01:18 +01004125 enabled |= event_enable_on_exec(event, ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004126 event_type |= get_event_type(event);
4127 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004128
4129 /*
Peter Zijlstra3e349502016-01-08 10:01:18 +01004130 * Unclone and reschedule this context if we enabled any event.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004131 */
Peter Zijlstra3e349502016-01-08 10:01:18 +01004132 if (enabled) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004133 clone_ctx = unclone_ctx(ctx);
Alexander Shishkin487f05e2017-01-19 18:43:30 +02004134 ctx_resched(cpuctx, ctx, event_type);
Peter Zijlstra7bbba0e2017-02-15 16:12:20 +01004135 } else {
4136 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
Peter Zijlstra3e349502016-01-08 10:01:18 +01004137 }
4138 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004139
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004140out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004141 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004142
4143 if (clone_ctx)
4144 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004145}
4146
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004147struct perf_read_data {
4148 struct perf_event *event;
4149 bool group;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004150 int ret;
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004151};
4152
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004153static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004154{
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004155 u16 local_pkg, event_pkg;
4156
4157 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) {
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004158 int local_cpu = smp_processor_id();
4159
4160 event_pkg = topology_physical_package_id(event_cpu);
4161 local_pkg = topology_physical_package_id(local_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004162
4163 if (event_pkg == local_pkg)
4164 return local_cpu;
4165 }
4166
4167 return event_cpu;
4168}
4169
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004170/*
4171 * Cross CPU call to read the hardware event
4172 */
4173static void __perf_event_read(void *info)
4174{
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004175 struct perf_read_data *data = info;
4176 struct perf_event *sub, *event = data->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004177 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004178 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004179 struct pmu *pmu = event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180
4181 /*
4182 * If this is a task context, we need to check whether it is
4183 * the current task context of this cpu. If not it has been
4184 * scheduled out before the smp call arrived. In that case
4185 * event->count would have been updated to a recent sample
4186 * when the event was scheduled out.
4187 */
4188 if (ctx->task && cpuctx->task_ctx != ctx)
4189 return;
4190
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004191 raw_spin_lock(&ctx->lock);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004192 if (ctx->is_active & EVENT_TIME) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01004193 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004194 update_cgrp_time_from_event(event);
4195 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004196
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004197 perf_event_update_time(event);
4198 if (data->group)
4199 perf_event_update_sibling_time(event);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004200
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004201 if (event->state != PERF_EVENT_STATE_ACTIVE)
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004202 goto unlock;
4203
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004204 if (!data->group) {
4205 pmu->read(event);
4206 data->ret = 0;
4207 goto unlock;
4208 }
4209
4210 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
4211
4212 pmu->read(event);
4213
Peter Zijlstraedb39592018-03-15 17:36:56 +01004214 for_each_sibling_event(sub, event) {
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004215 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
4216 /*
4217 * Use sibling's PMU rather than @event's since
4218 * sibling could be on different (eg: software) PMU.
4219 */
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004220 sub->pmu->read(sub);
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004221 }
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004222 }
Sukadev Bhattiprolu4a00c162015-09-03 20:07:51 -07004223
4224 data->ret = pmu->commit_txn(pmu);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004225
4226unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004227 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228}
4229
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004230static inline u64 perf_event_count(struct perf_event *event)
4231{
Vikas Shivappac39a0e22017-07-25 14:14:20 -07004232 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004233}
4234
Kaixu Xiaffe86902015-08-06 07:02:32 +00004235/*
4236 * NMI-safe method to read a local event, that is an event that
4237 * is:
4238 * - either for the current task, or for this CPU
4239 * - does not have inherit set, for inherited task events
4240 * will not be local and we cannot read them atomically
4241 * - must not have a pmu::count method
4242 */
Yonghong Song7d9285e2017-10-05 09:19:19 -07004243int perf_event_read_local(struct perf_event *event, u64 *value,
4244 u64 *enabled, u64 *running)
Kaixu Xiaffe86902015-08-06 07:02:32 +00004245{
4246 unsigned long flags;
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004247 int ret = 0;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004248
4249 /*
4250 * Disabling interrupts avoids all counter scheduling (context
4251 * switches, timer based rotation and IPIs).
4252 */
4253 local_irq_save(flags);
4254
Kaixu Xiaffe86902015-08-06 07:02:32 +00004255 /*
4256 * It must not be an event with inherit set, we cannot read
4257 * all child counters from atomic context.
4258 */
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004259 if (event->attr.inherit) {
4260 ret = -EOPNOTSUPP;
4261 goto out;
4262 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004263
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004264 /* If this is a per-task event, it must be for current */
4265 if ((event->attach_state & PERF_ATTACH_TASK) &&
4266 event->hw.target != current) {
4267 ret = -EINVAL;
4268 goto out;
4269 }
4270
4271 /* If this is a per-CPU event, it must be for this CPU */
4272 if (!(event->attach_state & PERF_ATTACH_TASK) &&
4273 event->cpu != smp_processor_id()) {
4274 ret = -EINVAL;
4275 goto out;
4276 }
Kaixu Xiaffe86902015-08-06 07:02:32 +00004277
Reinette Chatrebefb1b32018-09-19 10:29:06 -07004278 /* If this is a pinned event it must be running on this CPU */
4279 if (event->attr.pinned && event->oncpu != smp_processor_id()) {
4280 ret = -EBUSY;
4281 goto out;
4282 }
4283
Kaixu Xiaffe86902015-08-06 07:02:32 +00004284 /*
4285 * If the event is currently on this CPU, its either a per-task event,
4286 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
4287 * oncpu == -1).
4288 */
4289 if (event->oncpu == smp_processor_id())
4290 event->pmu->read(event);
4291
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004292 *value = local64_read(&event->count);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004293 if (enabled || running) {
4294 u64 now = event->shadow_ctx_time + perf_clock();
4295 u64 __enabled, __running;
4296
4297 __perf_update_times(event, now, &__enabled, &__running);
4298 if (enabled)
4299 *enabled = __enabled;
4300 if (running)
4301 *running = __running;
4302 }
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004303out:
Kaixu Xiaffe86902015-08-06 07:02:32 +00004304 local_irq_restore(flags);
4305
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07004306 return ret;
Kaixu Xiaffe86902015-08-06 07:02:32 +00004307}
4308
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004309static int perf_event_read(struct perf_event *event, bool group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004310{
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004311 enum perf_event_state state = READ_ONCE(event->state);
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004312 int event_cpu, ret = 0;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004313
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004314 /*
4315 * If event is enabled and currently active on a CPU, update the
4316 * value in the event structure:
4317 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004318again:
4319 if (state == PERF_EVENT_STATE_ACTIVE) {
4320 struct perf_read_data data;
4321
4322 /*
4323 * Orders the ->state and ->oncpu loads such that if we see
4324 * ACTIVE we must also see the right ->oncpu.
4325 *
4326 * Matches the smp_wmb() from event_sched_in().
4327 */
4328 smp_rmb();
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004329
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004330 event_cpu = READ_ONCE(event->oncpu);
4331 if ((unsigned)event_cpu >= nr_cpu_ids)
4332 return 0;
4333
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004334 data = (struct perf_read_data){
4335 .event = event,
4336 .group = group,
4337 .ret = 0,
4338 };
4339
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004340 preempt_disable();
4341 event_cpu = __perf_event_read_cpu(event, event_cpu);
David Carrillo-Cisnerosd6a2f9032016-08-17 13:55:06 -07004342
Peter Zijlstra58763142016-08-30 10:15:03 +02004343 /*
4344 * Purposely ignore the smp_call_function_single() return
4345 * value.
4346 *
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004347 * If event_cpu isn't a valid CPU it means the event got
Peter Zijlstra58763142016-08-30 10:15:03 +02004348 * scheduled out and that will have updated the event count.
4349 *
4350 * Therefore, either way, we'll have an up-to-date event count
4351 * after this.
4352 */
Peter Zijlstra451d24d2017-01-31 11:27:10 +01004353 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1);
4354 preempt_enable();
Peter Zijlstra58763142016-08-30 10:15:03 +02004355 ret = data.ret;
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004356
4357 } else if (state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01004358 struct perf_event_context *ctx = event->ctx;
4359 unsigned long flags;
4360
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004361 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004362 state = event->state;
4363 if (state != PERF_EVENT_STATE_INACTIVE) {
4364 raw_spin_unlock_irqrestore(&ctx->lock, flags);
4365 goto again;
4366 }
4367
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004368 /*
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004369 * May read while context is not active (e.g., thread is
4370 * blocked), in that case we cannot update context time
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004371 */
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004372 if (ctx->is_active & EVENT_TIME) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02004373 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02004374 update_cgrp_time_from_event(event);
4375 }
Peter Zijlstra0c1cbc12017-09-05 16:26:44 +02004376
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004377 perf_event_update_time(event);
Peter Zijlstra0492d4c2015-09-03 20:07:48 -07004378 if (group)
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02004379 perf_event_update_sibling_time(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004380 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004381 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07004382
4383 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004384}
4385
4386/*
4387 * Initialize the perf_event context in a task_struct:
4388 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02004389static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004390{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004391 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004392 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00004393 INIT_LIST_HEAD(&ctx->active_ctx_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +03004394 perf_event_groups_init(&ctx->pinned_groups);
4395 perf_event_groups_init(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004396 INIT_LIST_HEAD(&ctx->event_list);
Peter Zijlstra66681282017-11-13 14:28:38 +01004397 INIT_LIST_HEAD(&ctx->pinned_active);
4398 INIT_LIST_HEAD(&ctx->flexible_active);
Elena Reshetova8c94abb2019-01-28 14:27:26 +02004399 refcount_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004400}
4401
Peter Zijlstraeb184472010-09-07 15:55:13 +02004402static struct perf_event_context *
4403alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004404{
4405 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004406
4407 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
4408 if (!ctx)
4409 return NULL;
4410
4411 __perf_event_init_context(ctx);
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -07004412 if (task)
4413 ctx->task = get_task_struct(task);
Peter Zijlstraeb184472010-09-07 15:55:13 +02004414 ctx->pmu = pmu;
4415
4416 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004417}
4418
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004419static struct task_struct *
4420find_lively_task_by_vpid(pid_t vpid)
4421{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004422 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004425 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426 task = current;
4427 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004428 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429 if (task)
4430 get_task_struct(task);
4431 rcu_read_unlock();
4432
4433 if (!task)
4434 return ERR_PTR(-ESRCH);
4435
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004436 return task;
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07004437}
4438
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004439/*
4440 * Returns a matching context with refcount and pincount.
4441 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004442static struct perf_event_context *
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004443find_get_context(struct pmu *pmu, struct task_struct *task,
4444 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004446 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004447 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004448 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004449 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004450 int ctxn, err;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004451 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004452
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01004453 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454 /* Must be root to operate on a CPU event: */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004455 err = perf_allow_cpu(&event->attr);
4456 if (err)
4457 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004459 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004460 ctx = &cpuctx->ctx;
4461 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004462 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463
4464 return ctx;
4465 }
4466
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004467 err = -EINVAL;
4468 ctxn = pmu->task_ctx_nr;
4469 if (ctxn < 0)
4470 goto errout;
4471
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004472 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
4473 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
4474 if (!task_ctx_data) {
4475 err = -ENOMEM;
4476 goto errout;
4477 }
4478 }
4479
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004480retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004481 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004483 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004484 ++ctx->pin_count;
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004485
4486 if (task_ctx_data && !ctx->task_ctx_data) {
4487 ctx->task_ctx_data = task_ctx_data;
4488 task_ctx_data = NULL;
4489 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01004490 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02004491
4492 if (clone_ctx)
4493 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004494 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02004495 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004496 err = -ENOMEM;
4497 if (!ctx)
4498 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02004499
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004500 if (task_ctx_data) {
4501 ctx->task_ctx_data = task_ctx_data;
4502 task_ctx_data = NULL;
4503 }
4504
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004505 err = 0;
4506 mutex_lock(&task->perf_event_mutex);
4507 /*
4508 * If it has already passed perf_event_exit_task().
4509 * we must see PF_EXITING, it takes this mutex too.
4510 */
4511 if (task->flags & PF_EXITING)
4512 err = -ESRCH;
4513 else if (task->perf_event_ctxp[ctxn])
4514 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004515 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004516 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004517 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004518 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01004519 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004520 mutex_unlock(&task->perf_event_mutex);
4521
4522 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02004523 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01004524
4525 if (err == -EAGAIN)
4526 goto retry;
4527 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004528 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004529 }
4530
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004531 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004532 return ctx;
4533
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004534errout:
Yan, Zheng4af57ef2014-11-04 21:56:01 -05004535 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536 return ERR_PTR(err);
4537}
4538
Li Zefan6fb29152009-10-15 11:21:42 +08004539static void perf_event_free_filter(struct perf_event *event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07004540static void perf_event_free_bpf_prog(struct perf_event *event);
Li Zefan6fb29152009-10-15 11:21:42 +08004541
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004542static void free_event_rcu(struct rcu_head *head)
4543{
4544 struct perf_event *event;
4545
4546 event = container_of(head, struct perf_event, rcu_head);
4547 if (event->ns)
4548 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08004549 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550 kfree(event);
4551}
4552
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004553static void ring_buffer_attach(struct perf_event *event,
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05004554 struct perf_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004555
Kan Liangf2fb6be2016-03-23 11:24:37 -07004556static void detach_sb_event(struct perf_event *event)
4557{
4558 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
4559
4560 raw_spin_lock(&pel->lock);
4561 list_del_rcu(&event->sb_list);
4562 raw_spin_unlock(&pel->lock);
4563}
4564
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004565static bool is_sb_event(struct perf_event *event)
Kan Liangf2fb6be2016-03-23 11:24:37 -07004566{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004567 struct perf_event_attr *attr = &event->attr;
4568
Kan Liangf2fb6be2016-03-23 11:24:37 -07004569 if (event->parent)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004570 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004571
4572 if (event->attach_state & PERF_ATTACH_TASK)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004573 return false;
Kan Liangf2fb6be2016-03-23 11:24:37 -07004574
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004575 if (attr->mmap || attr->mmap_data || attr->mmap2 ||
4576 attr->comm || attr->comm_exec ||
Song Liu76193a92019-01-17 08:15:13 -08004577 attr->task || attr->ksymbol ||
Song Liu21038f22019-02-25 16:20:05 -08004578 attr->context_switch ||
4579 attr->bpf_event)
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -07004580 return true;
4581 return false;
4582}
4583
4584static void unaccount_pmu_sb_event(struct perf_event *event)
4585{
4586 if (is_sb_event(event))
4587 detach_sb_event(event);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004588}
4589
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004590static void unaccount_event_cpu(struct perf_event *event, int cpu)
4591{
4592 if (event->parent)
4593 return;
4594
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004595 if (is_cgroup_event(event))
4596 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
4597}
4598
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004599#ifdef CONFIG_NO_HZ_FULL
4600static DEFINE_SPINLOCK(nr_freq_lock);
4601#endif
4602
4603static void unaccount_freq_event_nohz(void)
4604{
4605#ifdef CONFIG_NO_HZ_FULL
4606 spin_lock(&nr_freq_lock);
4607 if (atomic_dec_and_test(&nr_freq_events))
4608 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
4609 spin_unlock(&nr_freq_lock);
4610#endif
4611}
4612
4613static void unaccount_freq_event(void)
4614{
4615 if (tick_nohz_full_enabled())
4616 unaccount_freq_event_nohz();
4617 else
4618 atomic_dec(&nr_freq_events);
4619}
4620
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004621static void unaccount_event(struct perf_event *event)
4622{
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004623 bool dec = false;
4624
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004625 if (event->parent)
4626 return;
4627
4628 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004629 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004630 if (event->attr.mmap || event->attr.mmap_data)
4631 atomic_dec(&nr_mmap_events);
4632 if (event->attr.comm)
4633 atomic_dec(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +05304634 if (event->attr.namespaces)
4635 atomic_dec(&nr_namespaces_events);
Namhyung Kim96aaab62020-03-25 21:45:28 +09004636 if (event->attr.cgroup)
4637 atomic_dec(&nr_cgroup_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004638 if (event->attr.task)
4639 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02004640 if (event->attr.freq)
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02004641 unaccount_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +03004642 if (event->attr.context_switch) {
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004643 dec = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +03004644 atomic_dec(&nr_switch_events);
4645 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004646 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004647 dec = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004648 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004649 dec = true;
Song Liu76193a92019-01-17 08:15:13 -08004650 if (event->attr.ksymbol)
4651 atomic_dec(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -08004652 if (event->attr.bpf_event)
4653 atomic_dec(&nr_bpf_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +01004654
Peter Zijlstra9107c892016-02-24 18:45:45 +01004655 if (dec) {
4656 if (!atomic_add_unless(&perf_sched_count, -1, 1))
4657 schedule_delayed_work(&perf_sched_work, HZ);
4658 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004659
4660 unaccount_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -07004661
4662 unaccount_pmu_sb_event(event);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004663}
4664
Peter Zijlstra9107c892016-02-24 18:45:45 +01004665static void perf_sched_delayed(struct work_struct *work)
4666{
4667 mutex_lock(&perf_sched_mutex);
4668 if (atomic_dec_and_test(&perf_sched_count))
4669 static_branch_disable(&perf_sched_events);
4670 mutex_unlock(&perf_sched_mutex);
4671}
4672
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004673/*
4674 * The following implement mutual exclusion of events on "exclusive" pmus
4675 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
4676 * at a time, so we disallow creating events that might conflict, namely:
4677 *
4678 * 1) cpu-wide events in the presence of per-task events,
4679 * 2) per-task events in the presence of cpu-wide events,
4680 * 3) two matching events on the same context.
4681 *
4682 * The former two cases are handled in the allocation path (perf_event_alloc(),
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004683 * _free_event()), the latter -- before the first perf_install_in_context().
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004684 */
4685static int exclusive_event_init(struct perf_event *event)
4686{
4687 struct pmu *pmu = event->pmu;
4688
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004689 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004690 return 0;
4691
4692 /*
4693 * Prevent co-existence of per-task and cpu-wide events on the
4694 * same exclusive pmu.
4695 *
4696 * Negative pmu::exclusive_cnt means there are cpu-wide
4697 * events on this "exclusive" pmu, positive means there are
4698 * per-task events.
4699 *
4700 * Since this is called in perf_event_alloc() path, event::ctx
4701 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
4702 * to mean "per-task event", because unlike other attach states it
4703 * never gets cleared.
4704 */
4705 if (event->attach_state & PERF_ATTACH_TASK) {
4706 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
4707 return -EBUSY;
4708 } else {
4709 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
4710 return -EBUSY;
4711 }
4712
4713 return 0;
4714}
4715
4716static void exclusive_event_destroy(struct perf_event *event)
4717{
4718 struct pmu *pmu = event->pmu;
4719
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004720 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004721 return;
4722
4723 /* see comment in exclusive_event_init() */
4724 if (event->attach_state & PERF_ATTACH_TASK)
4725 atomic_dec(&pmu->exclusive_cnt);
4726 else
4727 atomic_inc(&pmu->exclusive_cnt);
4728}
4729
4730static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
4731{
Alexander Shishkin3bf62152016-09-20 18:48:11 +03004732 if ((e1->pmu == e2->pmu) &&
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004733 (e1->cpu == e2->cpu ||
4734 e1->cpu == -1 ||
4735 e2->cpu == -1))
4736 return true;
4737 return false;
4738}
4739
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004740static bool exclusive_event_installable(struct perf_event *event,
4741 struct perf_event_context *ctx)
4742{
4743 struct perf_event *iter_event;
4744 struct pmu *pmu = event->pmu;
4745
Alexander Shishkin8a58dda2019-07-01 14:07:55 +03004746 lockdep_assert_held(&ctx->mutex);
4747
4748 if (!is_exclusive_pmu(pmu))
Alexander Shishkinbed5b252015-01-30 12:31:06 +02004749 return true;
4750
4751 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
4752 if (exclusive_event_match(iter_event, event))
4753 return false;
4754 }
4755
4756 return true;
4757}
4758
Alexander Shishkin375637b2016-04-27 18:44:46 +03004759static void perf_addr_filters_splice(struct perf_event *event,
4760 struct list_head *head);
4761
Peter Zijlstra683ede42014-05-05 12:11:24 +02004762static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004763{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004764 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004765
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02004766 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004767
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04004768 security_perf_event_free(event);
4769
Frederic Weisbecker76369132011-05-19 19:55:04 +02004770 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004771 /*
4772 * Can happen when we close an event with re-directed output.
4773 *
4774 * Since we have a 0 refcount, perf_mmap_close() will skip
4775 * over us; possibly making our ring_buffer_put() the last.
4776 */
4777 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004778 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004779 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780 }
4781
Stephane Eraniane5d13672011-02-14 11:20:01 +02004782 if (is_cgroup_event(event))
4783 perf_detach_cgroup(event);
4784
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004785 if (!event->parent) {
4786 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
4787 put_callchain_buffers();
4788 }
4789
4790 perf_event_free_bpf_prog(event);
Alexander Shishkin375637b2016-04-27 18:44:46 +03004791 perf_addr_filters_splice(event, NULL);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02004792 kfree(event->addr_filter_ranges);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004793
4794 if (event->destroy)
4795 event->destroy(event);
4796
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004797 /*
4798 * Must be after ->destroy(), due to uprobe_perf_close() using
4799 * hw.target.
4800 */
Prashant Bhole621b6d22018-04-09 19:03:46 +09004801 if (event->hw.target)
4802 put_task_struct(event->hw.target);
4803
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004804 /*
4805 * perf_event_free_task() relies on put_ctx() being 'last', in particular
4806 * all task references must be cleaned up.
4807 */
4808 if (event->ctx)
4809 put_ctx(event->ctx);
4810
Alexander Shishkin62a92c82016-06-07 15:44:15 +03004811 exclusive_event_destroy(event);
4812 module_put(event->pmu->module);
Peter Zijlstraa0733e62016-01-26 12:14:40 +01004813
4814 call_rcu(&event->rcu_head, free_event_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004815}
4816
Peter Zijlstra683ede42014-05-05 12:11:24 +02004817/*
4818 * Used to free events which have a known refcount of 1, such as in error paths
4819 * where the event isn't exposed yet and inherited events.
4820 */
4821static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004822{
Peter Zijlstra683ede42014-05-05 12:11:24 +02004823 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
4824 "unexpected event refcount: %ld; ptr=%p\n",
4825 atomic_long_read(&event->refcount), event)) {
4826 /* leak to avoid use-after-free */
4827 return;
4828 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004829
Peter Zijlstra683ede42014-05-05 12:11:24 +02004830 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004831}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004832
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004833/*
Jiri Olsaf8697762014-08-01 14:33:01 +02004834 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004835 */
Jiri Olsaf8697762014-08-01 14:33:01 +02004836static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004837{
Peter Zijlstra88821352010-11-09 19:01:43 +01004838 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01004839
Peter Zijlstra88821352010-11-09 19:01:43 +01004840 rcu_read_lock();
Peter Zijlstra88821352010-11-09 19:01:43 +01004841 /*
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004842 * Matches the smp_store_release() in perf_event_exit_task(). If we
4843 * observe !owner it means the list deletion is complete and we can
4844 * indeed free this event, otherwise we need to serialize on
Peter Zijlstra88821352010-11-09 19:01:43 +01004845 * owner->perf_event_mutex.
4846 */
Will Deacon506458e2017-10-24 11:22:48 +01004847 owner = READ_ONCE(event->owner);
Peter Zijlstra88821352010-11-09 19:01:43 +01004848 if (owner) {
4849 /*
4850 * Since delayed_put_task_struct() also drops the last
4851 * task reference we can safely take a new reference
4852 * while holding the rcu_read_lock().
4853 */
4854 get_task_struct(owner);
4855 }
4856 rcu_read_unlock();
4857
4858 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004859 /*
4860 * If we're here through perf_event_exit_task() we're already
4861 * holding ctx->mutex which would be an inversion wrt. the
4862 * normal lock order.
4863 *
4864 * However we can safely take this lock because its the child
4865 * ctx->mutex.
4866 */
4867 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
4868
Peter Zijlstra88821352010-11-09 19:01:43 +01004869 /*
4870 * We have to re-check the event->owner field, if it is cleared
4871 * we raced with perf_event_exit_task(), acquiring the mutex
4872 * ensured they're done, and we can proceed with freeing the
4873 * event.
4874 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004875 if (event->owner) {
Peter Zijlstra88821352010-11-09 19:01:43 +01004876 list_del_init(&event->owner_entry);
Peter Zijlstraf47c02c2016-01-26 12:30:14 +01004877 smp_store_release(&event->owner, NULL);
4878 }
Peter Zijlstra88821352010-11-09 19:01:43 +01004879 mutex_unlock(&owner->perf_event_mutex);
4880 put_task_struct(owner);
4881 }
Jiri Olsaf8697762014-08-01 14:33:01 +02004882}
4883
Jiri Olsaf8697762014-08-01 14:33:01 +02004884static void put_event(struct perf_event *event)
4885{
Jiri Olsaf8697762014-08-01 14:33:01 +02004886 if (!atomic_long_dec_and_test(&event->refcount))
4887 return;
4888
Peter Zijlstra683ede42014-05-05 12:11:24 +02004889 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01004890}
4891
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004892/*
4893 * Kill an event dead; while event:refcount will preserve the event
4894 * object, it will not preserve its functionality. Once the last 'user'
4895 * gives up the object, we'll destroy the thing.
4896 */
Peter Zijlstra683ede42014-05-05 12:11:24 +02004897int perf_event_release_kernel(struct perf_event *event)
4898{
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004899 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004900 struct perf_event *child, *tmp;
Peter Zijlstra82d94852018-01-09 13:10:30 +01004901 LIST_HEAD(free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004902
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01004903 /*
4904 * If we got here through err_file: fput(event_file); we will not have
4905 * attached to a context yet.
4906 */
4907 if (!ctx) {
4908 WARN_ON_ONCE(event->attach_state &
4909 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
4910 goto no_ctx;
4911 }
4912
Peter Zijlstra88821352010-11-09 19:01:43 +01004913 if (!is_kernel_event(event))
4914 perf_remove_from_owner(event);
4915
Peter Zijlstra5fa7c8e2016-01-26 15:25:15 +01004916 ctx = perf_event_ctx_lock(event);
Peter Zijlstra683ede42014-05-05 12:11:24 +02004917 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004918 perf_remove_from_context(event, DETACH_GROUP);
Peter Zijlstra88821352010-11-09 19:01:43 +01004919
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004920 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004921 /*
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +01004922 * Mark this event as STATE_DEAD, there is no external reference to it
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004923 * anymore.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004924 *
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004925 * Anybody acquiring event->child_mutex after the below loop _must_
4926 * also see this, most importantly inherit_event() which will avoid
4927 * placing more children on the list.
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004928 *
4929 * Thus this guarantees that we will in fact observe and kill _ALL_
4930 * child events.
Peter Zijlstra60beda82016-01-26 14:55:02 +01004931 */
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01004932 event->state = PERF_EVENT_STATE_DEAD;
4933 raw_spin_unlock_irq(&ctx->lock);
4934
4935 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra60beda82016-01-26 14:55:02 +01004936
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004937again:
4938 mutex_lock(&event->child_mutex);
4939 list_for_each_entry(child, &event->child_list, child_list) {
Al Viroa6fa9412012-08-20 14:59:25 +01004940
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004941 /*
4942 * Cannot change, child events are not migrated, see the
4943 * comment with perf_event_ctx_lock_nested().
4944 */
Will Deacon506458e2017-10-24 11:22:48 +01004945 ctx = READ_ONCE(child->ctx);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004946 /*
4947 * Since child_mutex nests inside ctx::mutex, we must jump
4948 * through hoops. We start by grabbing a reference on the ctx.
4949 *
4950 * Since the event cannot get freed while we hold the
4951 * child_mutex, the context must also exist and have a !0
4952 * reference count.
4953 */
4954 get_ctx(ctx);
4955
4956 /*
4957 * Now that we have a ctx ref, we can drop child_mutex, and
4958 * acquire ctx::mutex without fear of it going away. Then we
4959 * can re-acquire child_mutex.
4960 */
4961 mutex_unlock(&event->child_mutex);
4962 mutex_lock(&ctx->mutex);
4963 mutex_lock(&event->child_mutex);
4964
4965 /*
4966 * Now that we hold ctx::mutex and child_mutex, revalidate our
4967 * state, if child is still the first entry, it didn't get freed
4968 * and we can continue doing so.
4969 */
4970 tmp = list_first_entry_or_null(&event->child_list,
4971 struct perf_event, child_list);
4972 if (tmp == child) {
4973 perf_remove_from_context(child, DETACH_GROUP);
Peter Zijlstra82d94852018-01-09 13:10:30 +01004974 list_move(&child->child_list, &free_list);
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02004975 /*
4976 * This matches the refcount bump in inherit_event();
4977 * this can't be the last reference.
4978 */
4979 put_event(event);
4980 }
4981
4982 mutex_unlock(&event->child_mutex);
4983 mutex_unlock(&ctx->mutex);
4984 put_ctx(ctx);
4985 goto again;
4986 }
4987 mutex_unlock(&event->child_mutex);
4988
Peter Zijlstra82d94852018-01-09 13:10:30 +01004989 list_for_each_entry_safe(child, tmp, &free_list, child_list) {
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004990 void *var = &child->ctx->refcount;
4991
Peter Zijlstra82d94852018-01-09 13:10:30 +01004992 list_del(&child->child_list);
4993 free_event(child);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +02004994
4995 /*
4996 * Wake any perf_event_free_task() waiting for this event to be
4997 * freed.
4998 */
4999 smp_mb(); /* pairs with wait_var_event() */
5000 wake_up_var(var);
Peter Zijlstra82d94852018-01-09 13:10:30 +01005001 }
5002
Peter Zijlstraa4f4bb62016-02-24 18:45:42 +01005003no_ctx:
5004 put_event(event); /* Must be the 'last' reference */
Peter Zijlstra683ede42014-05-05 12:11:24 +02005005 return 0;
5006}
5007EXPORT_SYMBOL_GPL(perf_event_release_kernel);
5008
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +02005009/*
5010 * Called when the last reference to the file is gone.
5011 */
Al Viroa6fa9412012-08-20 14:59:25 +01005012static int perf_release(struct inode *inode, struct file *file)
5013{
Peter Zijlstrac6e5b732016-01-15 16:07:41 +02005014 perf_event_release_kernel(file->private_data);
Al Viroa6fa9412012-08-20 14:59:25 +01005015 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01005016}
5017
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005018static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005019{
5020 struct perf_event *child;
5021 u64 total = 0;
5022
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005023 *enabled = 0;
5024 *running = 0;
5025
Peter Zijlstra6f105812009-11-20 22:19:56 +01005026 mutex_lock(&event->child_mutex);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005027
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005028 (void)perf_event_read(event, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005029 total += perf_event_count(event);
5030
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005031 *enabled += event->total_time_enabled +
5032 atomic64_read(&event->child_total_time_enabled);
5033 *running += event->total_time_running +
5034 atomic64_read(&event->child_total_time_running);
5035
5036 list_for_each_entry(child, &event->child_list, child_list) {
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005037 (void)perf_event_read(child, false);
Sukadev Bhattiprolu01add3e2015-09-03 20:07:46 -07005038 total += perf_event_count(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005039 *enabled += child->total_time_enabled;
5040 *running += child->total_time_running;
5041 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01005042 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005043
5044 return total;
5045}
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005046
5047u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
5048{
5049 struct perf_event_context *ctx;
5050 u64 count;
5051
5052 ctx = perf_event_ctx_lock(event);
5053 count = __perf_event_read_value(event, enabled, running);
5054 perf_event_ctx_unlock(event, ctx);
5055
5056 return count;
5057}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005058EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005059
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005060static int __perf_read_group_add(struct perf_event *leader,
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005061 u64 read_format, u64 *values)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005062{
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005063 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005064 struct perf_event *sub;
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005065 unsigned long flags;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005066 int n = 1; /* skip @nr */
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005067 int ret;
Peter Zijlstraabf48682009-11-20 22:19:49 +01005068
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005069 ret = perf_event_read(leader, true);
5070 if (ret)
5071 return ret;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005072
Peter Zijlstraa9cd8192017-09-05 13:38:24 +02005073 raw_spin_lock_irqsave(&ctx->lock, flags);
5074
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005075 /*
5076 * Since we co-schedule groups, {enabled,running} times of siblings
5077 * will be identical to those of the leader, so we only publish one
5078 * set.
5079 */
5080 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
5081 values[n++] += leader->total_time_enabled +
5082 atomic64_read(&leader->child_total_time_enabled);
5083 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005084
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005085 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
5086 values[n++] += leader->total_time_running +
5087 atomic64_read(&leader->child_total_time_running);
5088 }
5089
5090 /*
5091 * Write {count,id} tuples for every sibling.
5092 */
5093 values[n++] += perf_event_count(leader);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005094 if (read_format & PERF_FORMAT_ID)
5095 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096
Peter Zijlstraedb39592018-03-15 17:36:56 +01005097 for_each_sibling_event(sub, leader) {
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005098 values[n++] += perf_event_count(sub);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005099 if (read_format & PERF_FORMAT_ID)
5100 values[n++] = primary_event_id(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005101 }
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005102
Jiri Olsa2aeb1882017-07-20 16:14:55 +02005103 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005104 return 0;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005105}
5106
5107static int perf_read_group(struct perf_event *event,
5108 u64 read_format, char __user *buf)
5109{
5110 struct perf_event *leader = event->group_leader, *child;
5111 struct perf_event_context *ctx = leader->ctx;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005112 int ret;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005113 u64 *values;
5114
5115 lockdep_assert_held(&ctx->mutex);
5116
5117 values = kzalloc(event->read_size, GFP_KERNEL);
5118 if (!values)
5119 return -ENOMEM;
5120
5121 values[0] = 1 + leader->nr_siblings;
5122
5123 /*
5124 * By locking the child_mutex of the leader we effectively
5125 * lock the child list of all siblings.. XXX explain how.
5126 */
5127 mutex_lock(&leader->child_mutex);
5128
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005129 ret = __perf_read_group_add(leader, read_format, values);
5130 if (ret)
5131 goto unlock;
5132
5133 list_for_each_entry(child, &leader->child_list, child_list) {
5134 ret = __perf_read_group_add(child, read_format, values);
5135 if (ret)
5136 goto unlock;
5137 }
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005138
5139 mutex_unlock(&leader->child_mutex);
5140
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005141 ret = event->read_size;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005142 if (copy_to_user(buf, values, event->read_size))
5143 ret = -EFAULT;
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005144 goto out;
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005145
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005146unlock:
5147 mutex_unlock(&leader->child_mutex);
5148out:
Peter Zijlstrafa8c2692015-09-03 20:07:49 -07005149 kfree(values);
Peter Zijlstraabf48682009-11-20 22:19:49 +01005150 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005151}
5152
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005153static int perf_read_one(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005154 u64 read_format, char __user *buf)
5155{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005156 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005157 u64 values[4];
5158 int n = 0;
5159
Peter Zijlstraca0dd442017-09-05 13:23:44 +02005160 values[n++] = __perf_event_read_value(event, &enabled, &running);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01005161 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
5162 values[n++] = enabled;
5163 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
5164 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005165 if (read_format & PERF_FORMAT_ID)
5166 values[n++] = primary_event_id(event);
5167
5168 if (copy_to_user(buf, values, n * sizeof(u64)))
5169 return -EFAULT;
5170
5171 return n * sizeof(u64);
5172}
5173
Jiri Olsadc633982014-09-12 13:18:26 +02005174static bool is_event_hup(struct perf_event *event)
5175{
5176 bool no_children;
5177
Peter Zijlstraa69b0ca2016-02-24 18:45:44 +01005178 if (event->state > PERF_EVENT_STATE_EXIT)
Jiri Olsadc633982014-09-12 13:18:26 +02005179 return false;
5180
5181 mutex_lock(&event->child_mutex);
5182 no_children = list_empty(&event->child_list);
5183 mutex_unlock(&event->child_mutex);
5184 return no_children;
5185}
5186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005187/*
5188 * Read the performance event - simple non blocking version for now
5189 */
5190static ssize_t
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005191__perf_read(struct perf_event *event, char __user *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005192{
5193 u64 read_format = event->attr.read_format;
5194 int ret;
5195
5196 /*
Tobias Tefke788faab2018-07-09 12:57:15 +02005197 * Return end-of-file for a read on an event that is in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198 * error state (i.e. because it was pinned but it couldn't be
5199 * scheduled on to the CPU at some point).
5200 */
5201 if (event->state == PERF_EVENT_STATE_ERROR)
5202 return 0;
5203
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005204 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205 return -ENOSPC;
5206
5207 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005208 if (read_format & PERF_FORMAT_GROUP)
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005209 ret = perf_read_group(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005210 else
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005211 ret = perf_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005212
5213 return ret;
5214}
5215
5216static ssize_t
5217perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
5218{
5219 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005220 struct perf_event_context *ctx;
5221 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005223 ret = security_perf_event_read(event);
5224 if (ret)
5225 return ret;
5226
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005227 ctx = perf_event_ctx_lock(event);
Peter Zijlstra (Intel)b15f4952015-09-03 20:07:47 -07005228 ret = __perf_read(event, buf, count);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005229 perf_event_ctx_unlock(event, ctx);
5230
5231 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005232}
5233
Al Viro9dd95742017-07-03 00:42:43 -04005234static __poll_t perf_poll(struct file *file, poll_table *wait)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235{
5236 struct perf_event *event = file->private_data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005237 struct perf_buffer *rb;
Linus Torvaldsa9a08842018-02-11 14:34:03 -08005238 __poll_t events = EPOLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02005240 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04005241
Jiri Olsadc633982014-09-12 13:18:26 +02005242 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04005243 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005244
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005245 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005246 * Pin the event->rb by taking event->mmap_mutex; otherwise
5247 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005248 */
5249 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005250 rb = event->rb;
5251 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02005252 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005253 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254 return events;
5255}
5256
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005257static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258{
Sukadev Bhattiprolu7d889622015-09-03 20:07:50 -07005259 (void)perf_event_read(event, false);
Peter Zijlstrae7850592010-05-21 14:43:08 +02005260 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005261 perf_event_update_userpage(event);
5262}
5263
Like Xu52ba4b02019-10-27 18:52:39 +08005264/* Assume it's not an event with inherit set. */
5265u64 perf_event_pause(struct perf_event *event, bool reset)
5266{
5267 struct perf_event_context *ctx;
5268 u64 count;
5269
5270 ctx = perf_event_ctx_lock(event);
5271 WARN_ON_ONCE(event->attr.inherit);
5272 _perf_event_disable(event);
5273 count = local64_read(&event->count);
5274 if (reset)
5275 local64_set(&event->count, 0);
5276 perf_event_ctx_unlock(event, ctx);
5277
5278 return count;
5279}
5280EXPORT_SYMBOL_GPL(perf_event_pause);
5281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005282/*
5283 * Holding the top-level event's child_mutex means that any
5284 * descendant process that has inherited this event will block
Peter Zijlstra8ba289b2016-01-26 13:06:56 +01005285 * in perf_event_exit_event() if it goes to exit, thus satisfying the
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005286 * task existence requirements of perf_event_enable/disable.
5287 */
5288static void perf_event_for_each_child(struct perf_event *event,
5289 void (*func)(struct perf_event *))
5290{
5291 struct perf_event *child;
5292
5293 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005294
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295 mutex_lock(&event->child_mutex);
5296 func(event);
5297 list_for_each_entry(child, &event->child_list, child_list)
5298 func(child);
5299 mutex_unlock(&event->child_mutex);
5300}
5301
5302static void perf_event_for_each(struct perf_event *event,
5303 void (*func)(struct perf_event *))
5304{
5305 struct perf_event_context *ctx = event->ctx;
5306 struct perf_event *sibling;
5307
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005308 lockdep_assert_held(&ctx->mutex);
5309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005310 event = event->group_leader;
5311
5312 perf_event_for_each_child(event, func);
Peter Zijlstraedb39592018-03-15 17:36:56 +01005313 for_each_sibling_event(sibling, event)
Michael Ellerman724b6da2012-04-11 11:54:13 +10005314 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005315}
5316
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005317static void __perf_event_period(struct perf_event *event,
5318 struct perf_cpu_context *cpuctx,
5319 struct perf_event_context *ctx,
5320 void *info)
Peter Zijlstra00179602015-11-30 16:26:35 +01005321{
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005322 u64 value = *((u64 *)info);
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005323 bool active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005325 if (event->attr.freq) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005326 event->attr.sample_freq = value;
5327 } else {
5328 event->attr.sample_period = value;
5329 event->hw.sample_period = value;
5330 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005331
5332 active = (event->state == PERF_EVENT_STATE_ACTIVE);
5333 if (active) {
5334 perf_pmu_disable(ctx->pmu);
Peter Zijlstra1e02cd42016-03-10 15:39:24 +01005335 /*
5336 * We could be throttled; unthrottle now to avoid the tick
5337 * trying to unthrottle while we already re-started the event.
5338 */
5339 if (event->hw.interrupts == MAX_INTERRUPTS) {
5340 event->hw.interrupts = 0;
5341 perf_log_throttle(event, 1);
5342 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00005343 event->pmu->stop(event, PERF_EF_UPDATE);
5344 }
5345
5346 local64_set(&event->hw.period_left, 0);
5347
5348 if (active) {
5349 event->pmu->start(event, PERF_EF_RELOAD);
5350 perf_pmu_enable(ctx->pmu);
5351 }
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005352}
5353
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005354static int perf_event_check_period(struct perf_event *event, u64 value)
5355{
5356 return event->pmu->check_period(event, value);
5357}
5358
Like Xu3ca270f2019-10-27 18:52:38 +08005359static int _perf_event_period(struct perf_event *event, u64 value)
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005360{
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005361 if (!is_sampling_event(event))
5362 return -EINVAL;
5363
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005364 if (!value)
5365 return -EINVAL;
5366
5367 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
5368 return -EINVAL;
5369
Jiri Olsa81ec3f32019-02-04 13:35:32 +01005370 if (perf_event_check_period(event, value))
5371 return -EINVAL;
5372
Ravi Bangoria913a90b2019-06-04 09:59:53 +05305373 if (!event->attr.freq && (value & (1ULL << 63)))
5374 return -EINVAL;
5375
Peter Zijlstrafae3fde2016-01-11 15:00:50 +01005376 event_function_call(event, __perf_event_period, &value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377
Peter Zijlstrac7999c62015-08-04 19:22:49 +02005378 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005379}
5380
Like Xu3ca270f2019-10-27 18:52:38 +08005381int perf_event_period(struct perf_event *event, u64 value)
5382{
5383 struct perf_event_context *ctx;
5384 int ret;
5385
5386 ctx = perf_event_ctx_lock(event);
5387 ret = _perf_event_period(event, value);
5388 perf_event_ctx_unlock(event, ctx);
5389
5390 return ret;
5391}
5392EXPORT_SYMBOL_GPL(perf_event_period);
5393
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005394static const struct file_operations perf_fops;
5395
Al Viro2903ff02012-08-28 12:52:22 -04005396static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005397{
Al Viro2903ff02012-08-28 12:52:22 -04005398 struct fd f = fdget(fd);
5399 if (!f.file)
5400 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005401
Al Viro2903ff02012-08-28 12:52:22 -04005402 if (f.file->f_op != &perf_fops) {
5403 fdput(f);
5404 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005405 }
Al Viro2903ff02012-08-28 12:52:22 -04005406 *p = f;
5407 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005408}
5409
5410static int perf_event_set_output(struct perf_event *event,
5411 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08005412static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Alexei Starovoitov25415172015-03-25 12:49:20 -07005413static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005414static int perf_copy_attr(struct perf_event_attr __user *uattr,
5415 struct perf_event_attr *attr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005416
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005417static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005418{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005419 void (*func)(struct perf_event *);
5420 u32 flags = arg;
5421
5422 switch (cmd) {
5423 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005424 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425 break;
5426 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005427 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428 break;
5429 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005430 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431 break;
5432
5433 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005434 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005435
5436 case PERF_EVENT_IOC_PERIOD:
Like Xu3ca270f2019-10-27 18:52:38 +08005437 {
5438 u64 value;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005439
Like Xu3ca270f2019-10-27 18:52:38 +08005440 if (copy_from_user(&value, (u64 __user *)arg, sizeof(value)))
5441 return -EFAULT;
5442
5443 return _perf_event_period(event, value);
5444 }
Jiri Olsacf4957f2012-10-24 13:37:58 +02005445 case PERF_EVENT_IOC_ID:
5446 {
5447 u64 id = primary_event_id(event);
5448
5449 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
5450 return -EFAULT;
5451 return 0;
5452 }
5453
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005454 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005455 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005456 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005457 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04005458 struct perf_event *output_event;
5459 struct fd output;
5460 ret = perf_fget_light(arg, &output);
5461 if (ret)
5462 return ret;
5463 output_event = output.file->private_data;
5464 ret = perf_event_set_output(event, output_event);
5465 fdput(output);
5466 } else {
5467 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005468 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005469 return ret;
5470 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005471
Li Zefan6fb29152009-10-15 11:21:42 +08005472 case PERF_EVENT_IOC_SET_FILTER:
5473 return perf_event_set_filter(event, (void __user *)arg);
5474
Alexei Starovoitov25415172015-03-25 12:49:20 -07005475 case PERF_EVENT_IOC_SET_BPF:
5476 return perf_event_set_bpf_prog(event, arg);
5477
Wang Nan86e79722016-03-28 06:41:29 +00005478 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005479 struct perf_buffer *rb;
Wang Nan86e79722016-03-28 06:41:29 +00005480
5481 rcu_read_lock();
5482 rb = rcu_dereference(event->rb);
5483 if (!rb || !rb->nr_pages) {
5484 rcu_read_unlock();
5485 return -EINVAL;
5486 }
5487 rb_toggle_paused(rb, !!arg);
5488 rcu_read_unlock();
5489 return 0;
5490 }
Yonghong Songf371b302017-12-11 11:39:02 -08005491
5492 case PERF_EVENT_IOC_QUERY_BPF:
Yonghong Songf4e22982017-12-13 10:35:37 -08005493 return perf_event_query_prog_array(event, (void __user *)arg);
Milind Chabbi32ff77e2018-03-12 14:45:47 +01005494
5495 case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
5496 struct perf_event_attr new_attr;
5497 int err = perf_copy_attr((struct perf_event_attr __user *)arg,
5498 &new_attr);
5499
5500 if (err)
5501 return err;
5502
5503 return perf_event_modify_attr(event, &new_attr);
5504 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005505 default:
5506 return -ENOTTY;
5507 }
5508
5509 if (flags & PERF_IOC_FLAG_GROUP)
5510 perf_event_for_each(event, func);
5511 else
5512 perf_event_for_each_child(event, func);
5513
5514 return 0;
5515}
5516
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005517static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5518{
5519 struct perf_event *event = file->private_data;
5520 struct perf_event_context *ctx;
5521 long ret;
5522
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005523 /* Treat ioctl like writes as it is likely a mutating operation. */
5524 ret = security_perf_event_write(event);
5525 if (ret)
5526 return ret;
5527
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005528 ctx = perf_event_ctx_lock(event);
5529 ret = _perf_ioctl(event, cmd, arg);
5530 perf_event_ctx_unlock(event, ctx);
5531
5532 return ret;
5533}
5534
Pawel Mollb3f20782014-06-13 16:03:32 +01005535#ifdef CONFIG_COMPAT
5536static long perf_compat_ioctl(struct file *file, unsigned int cmd,
5537 unsigned long arg)
5538{
5539 switch (_IOC_NR(cmd)) {
5540 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
5541 case _IOC_NR(PERF_EVENT_IOC_ID):
Eugene Syromiatnikov82489c52018-05-21 14:34:20 +02005542 case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF):
5543 case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES):
Pawel Mollb3f20782014-06-13 16:03:32 +01005544 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
5545 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
5546 cmd &= ~IOCSIZE_MASK;
5547 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
5548 }
5549 break;
5550 }
5551 return perf_ioctl(file, cmd, arg);
5552}
5553#else
5554# define perf_compat_ioctl NULL
5555#endif
5556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557int perf_event_task_enable(void)
5558{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005559 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005560 struct perf_event *event;
5561
5562 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005563 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5564 ctx = perf_event_ctx_lock(event);
5565 perf_event_for_each_child(event, _perf_event_enable);
5566 perf_event_ctx_unlock(event, ctx);
5567 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005568 mutex_unlock(&current->perf_event_mutex);
5569
5570 return 0;
5571}
5572
5573int perf_event_task_disable(void)
5574{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005575 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576 struct perf_event *event;
5577
5578 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01005579 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
5580 ctx = perf_event_ctx_lock(event);
5581 perf_event_for_each_child(event, _perf_event_disable);
5582 perf_event_ctx_unlock(event, ctx);
5583 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584 mutex_unlock(&current->perf_event_mutex);
5585
5586 return 0;
5587}
5588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005589static int perf_event_index(struct perf_event *event)
5590{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005591 if (event->hw.state & PERF_HES_STOPPED)
5592 return 0;
5593
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594 if (event->state != PERF_EVENT_STATE_ACTIVE)
5595 return 0;
5596
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005597 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005598}
5599
Eric B Munsonc4794292011-06-23 16:34:38 -04005600static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005601 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04005602 u64 *enabled,
5603 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04005604{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005605 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04005606
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005607 *now = perf_clock();
5608 ctx_time = event->shadow_ctx_time + *now;
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +02005609 __perf_update_times(event, ctx_time, enabled, running);
Eric B Munsonc4794292011-06-23 16:34:38 -04005610}
5611
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005612static void perf_event_init_userpage(struct perf_event *event)
5613{
5614 struct perf_event_mmap_page *userpg;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005615 struct perf_buffer *rb;
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005616
5617 rcu_read_lock();
5618 rb = rcu_dereference(event->rb);
5619 if (!rb)
5620 goto unlock;
5621
5622 userpg = rb->user_page;
5623
5624 /* Allow new userspace to detect that bit 0 is deprecated */
5625 userpg->cap_bit0_is_deprecated = 1;
5626 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
Alexander Shishkine8c6dea2015-01-14 14:18:10 +02005627 userpg->data_offset = PAGE_SIZE;
5628 userpg->data_size = perf_data_size(rb);
Peter Zijlstrafa7315872013-09-19 10:16:42 +02005629
5630unlock:
5631 rcu_read_unlock();
5632}
5633
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005634void __weak arch_perf_update_userpage(
5635 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005636{
5637}
5638
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005639/*
5640 * Callers need to ensure there can be no nesting of this function, otherwise
5641 * the seqlock logic goes bad. We can not serialize this because the arch
5642 * code calls this from NMI context.
5643 */
5644void perf_event_update_userpage(struct perf_event *event)
5645{
5646 struct perf_event_mmap_page *userpg;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005647 struct perf_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005648 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649
5650 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02005651 rb = rcu_dereference(event->rb);
5652 if (!rb)
5653 goto unlock;
5654
Eric B Munson0d641202011-06-24 12:26:26 -04005655 /*
5656 * compute total_time_enabled, total_time_running
5657 * based on snapshot values taken when the event
5658 * was last scheduled in.
5659 *
5660 * we cannot simply called update_context_time()
5661 * because of locking issue as we can be called in
5662 * NMI context
5663 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005664 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005665
Frederic Weisbecker76369132011-05-19 19:55:04 +02005666 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005667 /*
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07005668 * Disable preemption to guarantee consistent time stamps are stored to
5669 * the user page.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670 */
5671 preempt_disable();
5672 ++userpg->lock;
5673 barrier();
5674 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005675 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01005676 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02005677 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005678
Eric B Munson0d641202011-06-24 12:26:26 -04005679 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005680 atomic64_read(&event->child_total_time_enabled);
5681
Eric B Munson0d641202011-06-24 12:26:26 -04005682 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005683 atomic64_read(&event->child_total_time_running);
5684
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07005685 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01005686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005687 barrier();
5688 ++userpg->lock;
5689 preempt_enable();
5690unlock:
5691 rcu_read_unlock();
5692}
Suzuki K Poulose82975c42018-01-02 11:25:26 +00005693EXPORT_SYMBOL_GPL(perf_event_update_userpage);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305695static vm_fault_t perf_mmap_fault(struct vm_fault *vmf)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005696{
Dave Jiang11bac802017-02-24 14:56:41 -08005697 struct perf_event *event = vmf->vma->vm_file->private_data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005698 struct perf_buffer *rb;
Souptick Joarder9e3ed2d2018-05-21 23:55:20 +05305699 vm_fault_t ret = VM_FAULT_SIGBUS;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005700
5701 if (vmf->flags & FAULT_FLAG_MKWRITE) {
5702 if (vmf->pgoff == 0)
5703 ret = 0;
5704 return ret;
5705 }
5706
5707 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005708 rb = rcu_dereference(event->rb);
5709 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02005710 goto unlock;
5711
5712 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
5713 goto unlock;
5714
Frederic Weisbecker76369132011-05-19 19:55:04 +02005715 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02005716 if (!vmf->page)
5717 goto unlock;
5718
5719 get_page(vmf->page);
Dave Jiang11bac802017-02-24 14:56:41 -08005720 vmf->page->mapping = vmf->vma->vm_file->f_mapping;
Peter Zijlstra906010b2009-09-21 16:08:49 +02005721 vmf->page->index = vmf->pgoff;
5722
5723 ret = 0;
5724unlock:
5725 rcu_read_unlock();
5726
5727 return ret;
5728}
5729
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005730static void ring_buffer_attach(struct perf_event *event,
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005731 struct perf_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005732{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005733 struct perf_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005734 unsigned long flags;
5735
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005736 if (event->rb) {
5737 /*
5738 * Should be impossible, we set this when removing
5739 * event->rb_entry and wait/clear when adding event->rb_entry.
5740 */
5741 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005742
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005743 old_rb = event->rb;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005744 spin_lock_irqsave(&old_rb->event_lock, flags);
5745 list_del_rcu(&event->rb_entry);
5746 spin_unlock_irqrestore(&old_rb->event_lock, flags);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005747
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005748 event->rcu_batches = get_state_synchronize_rcu();
5749 event->rcu_pending = 1;
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005750 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005751
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005752 if (rb) {
Oleg Nesterov2f993cf2015-05-30 22:04:25 +02005753 if (event->rcu_pending) {
5754 cond_synchronize_rcu(event->rcu_batches);
5755 event->rcu_pending = 0;
5756 }
5757
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005758 spin_lock_irqsave(&rb->event_lock, flags);
5759 list_add_rcu(&event->rb_entry, &rb->event_list);
5760 spin_unlock_irqrestore(&rb->event_lock, flags);
5761 }
5762
Alexander Shishkin767ae082016-09-06 16:23:49 +03005763 /*
5764 * Avoid racing with perf_mmap_close(AUX): stop the event
5765 * before swizzling the event::rb pointer; if it's getting
5766 * unmapped, its aux_mmap_count will be 0 and it won't
5767 * restart. See the comment in __perf_pmu_output_stop().
5768 *
5769 * Data will inevitably be lost when set_output is done in
5770 * mid-air, but then again, whoever does it like this is
5771 * not in for the data anyway.
5772 */
5773 if (has_aux(event))
5774 perf_event_stop(event, 0);
5775
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005776 rcu_assign_pointer(event->rb, rb);
5777
5778 if (old_rb) {
5779 ring_buffer_put(old_rb);
5780 /*
5781 * Since we detached before setting the new rb, so that we
5782 * could attach the new rb, we could have missed a wakeup.
5783 * Provide it now.
5784 */
5785 wake_up_all(&event->waitq);
5786 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005787}
5788
5789static void ring_buffer_wakeup(struct perf_event *event)
5790{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005791 struct perf_buffer *rb;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005792
5793 rcu_read_lock();
5794 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005795 if (rb) {
5796 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
5797 wake_up_all(&event->waitq);
5798 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005799 rcu_read_unlock();
5800}
5801
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005802struct perf_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005803{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005804 struct perf_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005805
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005806 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02005807 rb = rcu_dereference(event->rb);
5808 if (rb) {
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005809 if (!refcount_inc_not_zero(&rb->refcount))
Frederic Weisbecker76369132011-05-19 19:55:04 +02005810 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005811 }
5812 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005813
Frederic Weisbecker76369132011-05-19 19:55:04 +02005814 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005815}
5816
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005817void ring_buffer_put(struct perf_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005818{
Elena Reshetovafecb8ed2019-01-28 14:27:27 +02005819 if (!refcount_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005820 return;
5821
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005822 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01005823
Frederic Weisbecker76369132011-05-19 19:55:04 +02005824 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005825}
5826
5827static void perf_mmap_open(struct vm_area_struct *vma)
5828{
5829 struct perf_event *event = vma->vm_file->private_data;
5830
5831 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005832 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005833
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005834 if (vma->vm_pgoff)
5835 atomic_inc(&event->rb->aux_mmap_count);
5836
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005837 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005838 event->pmu->event_mapped(event, vma->vm_mm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005839}
5840
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005841static void perf_pmu_output_stop(struct perf_event *event);
5842
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005843/*
5844 * A buffer can be mmap()ed multiple times; either directly through the same
5845 * event, or through other events by use of perf_event_set_output().
5846 *
5847 * In order to undo the VM accounting done by perf_mmap() we need to destroy
5848 * the buffer here, where we still have a VM context. This means we need
5849 * to detach all events redirecting to us.
5850 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005851static void perf_mmap_close(struct vm_area_struct *vma)
5852{
5853 struct perf_event *event = vma->vm_file->private_data;
5854
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005855 struct perf_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005856 struct user_struct *mmap_user = rb->mmap_user;
5857 int mmap_locked = rb->mmap_locked;
5858 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005859
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005860 if (event->pmu->event_unmapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02005861 event->pmu->event_unmapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07005862
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005863 /*
5864 * rb->aux_mmap_count will always drop before rb->mmap_count and
5865 * event->mmap_count, so it is ok to use event->mmap_mutex to
5866 * serialize with perf_mmap here.
5867 */
5868 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
5869 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005870 /*
5871 * Stop all AUX events that are writing to this buffer,
5872 * so that we can free its AUX pages and corresponding PMU
5873 * data. Note that after rb::aux_mmap_count dropped to zero,
5874 * they won't start any more (see perf_aux_output_begin()).
5875 */
5876 perf_pmu_output_stop(event);
5877
5878 /* now it's safe to free the pages */
Alexander Shishkin36b3db02019-11-15 18:08:18 +02005879 atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm);
5880 atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005881
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005882 /* this has to be the last one */
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005883 rb_free_aux(rb);
Elena Reshetovaca3bb3d2019-01-28 14:27:28 +02005884 WARN_ON_ONCE(refcount_read(&rb->aux_refcount));
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02005885
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005886 mutex_unlock(&event->mmap_mutex);
5887 }
5888
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005889 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005890
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005891 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005892 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005893
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005894 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005895 mutex_unlock(&event->mmap_mutex);
5896
5897 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005898 if (atomic_read(&rb->mmap_count))
5899 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005900
5901 /*
5902 * No other mmap()s, detach from all other events that might redirect
5903 * into the now unreachable buffer. Somewhat complicated by the
5904 * fact that rb::event_lock otherwise nests inside mmap_mutex.
5905 */
5906again:
5907 rcu_read_lock();
5908 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
5909 if (!atomic_long_inc_not_zero(&event->refcount)) {
5910 /*
5911 * This event is en-route to free_event() which will
5912 * detach it and remove it from the list.
5913 */
5914 continue;
5915 }
5916 rcu_read_unlock();
5917
5918 mutex_lock(&event->mmap_mutex);
5919 /*
5920 * Check we didn't race with perf_event_set_output() which can
5921 * swizzle the rb from under us while we were waiting to
5922 * acquire mmap_mutex.
5923 *
5924 * If we find a different rb; ignore this event, a next
5925 * iteration will no longer find it on the list. We have to
5926 * still restart the iteration to make sure we're not now
5927 * iterating the wrong list.
5928 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005929 if (event->rb == rb)
5930 ring_buffer_attach(event, NULL);
5931
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005932 mutex_unlock(&event->mmap_mutex);
5933 put_event(event);
5934
5935 /*
5936 * Restart the iteration; either we're on the wrong list or
5937 * destroyed its integrity by doing a deletion.
5938 */
5939 goto again;
5940 }
5941 rcu_read_unlock();
5942
5943 /*
5944 * It could be there's still a few 0-ref events on the list; they'll
5945 * get cleaned up by free_event() -- they'll also still have their
5946 * ref on the rb and will free it whenever they are done with it.
5947 *
5948 * Aside from that, this buffer is 'fully' detached and unmapped,
5949 * undo the VM accounting.
5950 */
5951
Song Liud44248a2019-09-04 14:46:18 -07005952 atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked,
5953 &mmap_user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08005954 atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005955 free_uid(mmap_user);
5956
Peter Zijlstrab69cf532014-03-14 10:50:33 +01005957out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02005958 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959}
5960
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04005961static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005962 .open = perf_mmap_open,
Ingo Molnarfca0c112018-12-03 10:52:21 +01005963 .close = perf_mmap_close, /* non mergeable */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964 .fault = perf_mmap_fault,
5965 .page_mkwrite = perf_mmap_fault,
5966};
5967
5968static int perf_mmap(struct file *file, struct vm_area_struct *vma)
5969{
5970 struct perf_event *event = file->private_data;
5971 unsigned long user_locked, user_lock_limit;
5972 struct user_struct *user = current_user();
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05005973 struct perf_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005974 unsigned long locked, lock_limit;
5975 unsigned long vma_size;
5976 unsigned long nr_pages;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005977 long user_extra = 0, extra = 0;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02005978 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005979
Peter Zijlstrac7920612010-05-18 10:33:24 +02005980 /*
5981 * Don't allow mmap() of inherited per-task counters. This would
5982 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02005983 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02005984 */
5985 if (event->cpu == -1 && event->attr.inherit)
5986 return -EINVAL;
5987
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988 if (!(vma->vm_flags & VM_SHARED))
5989 return -EINVAL;
5990
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04005991 ret = security_perf_event_read(event);
5992 if (ret)
5993 return ret;
5994
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005995 vma_size = vma->vm_end - vma->vm_start;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02005996
5997 if (vma->vm_pgoff == 0) {
5998 nr_pages = (vma_size / PAGE_SIZE) - 1;
5999 } else {
6000 /*
6001 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
6002 * mapped, all subsequent mappings should have the same size
6003 * and offset. Must be above the normal perf buffer.
6004 */
6005 u64 aux_offset, aux_size;
6006
6007 if (!event->rb)
6008 return -EINVAL;
6009
6010 nr_pages = vma_size / PAGE_SIZE;
6011
6012 mutex_lock(&event->mmap_mutex);
6013 ret = -EINVAL;
6014
6015 rb = event->rb;
6016 if (!rb)
6017 goto aux_unlock;
6018
Mark Rutland6aa7de02017-10-23 14:07:29 -07006019 aux_offset = READ_ONCE(rb->user_page->aux_offset);
6020 aux_size = READ_ONCE(rb->user_page->aux_size);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006021
6022 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
6023 goto aux_unlock;
6024
6025 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
6026 goto aux_unlock;
6027
6028 /* already mapped with a different offset */
6029 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
6030 goto aux_unlock;
6031
6032 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
6033 goto aux_unlock;
6034
6035 /* already mapped with a different size */
6036 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
6037 goto aux_unlock;
6038
6039 if (!is_power_of_2(nr_pages))
6040 goto aux_unlock;
6041
6042 if (!atomic_inc_not_zero(&rb->mmap_count))
6043 goto aux_unlock;
6044
6045 if (rb_has_aux(rb)) {
6046 atomic_inc(&rb->aux_mmap_count);
6047 ret = 0;
6048 goto unlock;
6049 }
6050
6051 atomic_set(&rb->aux_mmap_count, 1);
6052 user_extra = nr_pages;
6053
6054 goto accounting;
6055 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006056
6057 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006058 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006059 * can do bitmasks instead of modulo.
6060 */
Kan Liang2ed11312015-03-02 02:14:26 -05006061 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006062 return -EINVAL;
6063
6064 if (vma_size != PAGE_SIZE * (1 + nr_pages))
6065 return -EINVAL;
6066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006067 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006068again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006069 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02006070 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006071 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006072 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006073 goto unlock;
6074 }
6075
6076 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
6077 /*
6078 * Raced against perf_mmap_close() through
6079 * perf_event_set_output(). Try again, hope for better
6080 * luck.
6081 */
6082 mutex_unlock(&event->mmap_mutex);
6083 goto again;
6084 }
6085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006086 goto unlock;
6087 }
6088
6089 user_extra = nr_pages + 1;
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006090
6091accounting:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
6093
6094 /*
6095 * Increase the limit linearly with more CPUs:
6096 */
6097 user_lock_limit *= num_online_cpus();
6098
Song Liu00346152020-01-23 10:11:46 -08006099 user_locked = atomic_long_read(&user->locked_vm);
6100
6101 /*
6102 * sysctl_perf_event_mlock may have changed, so that
6103 * user->locked_vm > user_lock_limit
6104 */
6105 if (user_locked > user_lock_limit)
6106 user_locked = user_lock_limit;
6107 user_locked += user_extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006108
Alexander Shishkinc4b75472019-11-20 19:06:40 +02006109 if (user_locked > user_lock_limit) {
Song Liud44248a2019-09-04 14:46:18 -07006110 /*
6111 * charge locked_vm until it hits user_lock_limit;
6112 * charge the rest from pinned_vm
6113 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114 extra = user_locked - user_lock_limit;
Song Liud44248a2019-09-04 14:46:18 -07006115 user_extra -= extra;
6116 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006117
Jiri Slaby78d7d402010-03-05 13:42:54 -08006118 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006119 lock_limit >>= PAGE_SHIFT;
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006120 locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006121
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -04006122 if ((locked > lock_limit) && perf_is_paranoid() &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006123 !capable(CAP_IPC_LOCK)) {
6124 ret = -EPERM;
6125 goto unlock;
6126 }
6127
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006128 WARN_ON(!rb && event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02006129
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006130 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02006131 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02006132
Frederic Weisbecker76369132011-05-19 19:55:04 +02006133 if (!rb) {
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006134 rb = rb_alloc(nr_pages,
6135 event->attr.watermark ? event->attr.wakeup_watermark : 0,
6136 event->cpu, flags);
6137
6138 if (!rb) {
6139 ret = -ENOMEM;
6140 goto unlock;
6141 }
6142
6143 atomic_set(&rb->mmap_count, 1);
6144 rb->mmap_user = get_current_user();
6145 rb->mmap_locked = extra;
6146
6147 ring_buffer_attach(event, rb);
6148
6149 perf_event_init_userpage(event);
6150 perf_event_update_userpage(event);
6151 } else {
Alexander Shishkin1a594132015-01-14 14:18:18 +02006152 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
6153 event->attr.aux_watermark, flags);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006154 if (!ret)
6155 rb->aux_mmap_locked = extra;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006156 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02006157
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006158unlock:
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006159 if (!ret) {
6160 atomic_long_add(user_extra, &user->locked_vm);
Davidlohr Bueso70f8a3c2019-02-06 09:59:15 -08006161 atomic64_add(extra, &vma->vm_mm->pinned_vm);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006162
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006163 atomic_inc(&event->mmap_count);
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +02006164 } else if (rb) {
6165 atomic_dec(&rb->mmap_count);
6166 }
6167aux_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006168 mutex_unlock(&event->mmap_mutex);
6169
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006170 /*
6171 * Since pinned accounting is per vm we cannot allow fork() to copy our
6172 * vma.
6173 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02006174 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006175 vma->vm_ops = &perf_mmap_vmops;
6176
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07006177 if (event->pmu->event_mapped)
Peter Zijlstrabfe334922017-08-02 19:39:30 +02006178 event->pmu->event_mapped(event, vma->vm_mm);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07006179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006180 return ret;
6181}
6182
6183static int perf_fasync(int fd, struct file *filp, int on)
6184{
Al Viro496ad9a2013-01-23 17:07:38 -05006185 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006186 struct perf_event *event = filp->private_data;
6187 int retval;
6188
Al Viro59551022016-01-22 15:40:57 -05006189 inode_lock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006190 retval = fasync_helper(fd, filp, on, &event->fasync);
Al Viro59551022016-01-22 15:40:57 -05006191 inode_unlock(inode);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006192
6193 if (retval < 0)
6194 return retval;
6195
6196 return 0;
6197}
6198
6199static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01006200 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006201 .release = perf_release,
6202 .read = perf_read,
6203 .poll = perf_poll,
6204 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01006205 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006206 .mmap = perf_mmap,
6207 .fasync = perf_fasync,
6208};
6209
6210/*
6211 * Perf event wakeup
6212 *
6213 * If there's data, ensure we set the poll() state and publish everything
6214 * to user-space before waking everybody up.
6215 */
6216
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006217static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
6218{
6219 /* only the parent has fasync state */
6220 if (event->parent)
6221 event = event->parent;
6222 return &event->fasync;
6223}
6224
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006225void perf_event_wakeup(struct perf_event *event)
6226{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006227 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006228
6229 if (event->pending_kill) {
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02006230 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231 event->pending_kill = 0;
6232 }
6233}
6234
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006235static void perf_pending_event_disable(struct perf_event *event)
6236{
6237 int cpu = READ_ONCE(event->pending_disable);
6238
6239 if (cpu < 0)
6240 return;
6241
6242 if (cpu == smp_processor_id()) {
6243 WRITE_ONCE(event->pending_disable, -1);
6244 perf_event_disable_local(event);
6245 return;
6246 }
6247
6248 /*
6249 * CPU-A CPU-B
6250 *
6251 * perf_event_disable_inatomic()
6252 * @pending_disable = CPU-A;
6253 * irq_work_queue();
6254 *
6255 * sched-out
6256 * @pending_disable = -1;
6257 *
6258 * sched-in
6259 * perf_event_disable_inatomic()
6260 * @pending_disable = CPU-B;
6261 * irq_work_queue(); // FAILS
6262 *
6263 * irq_work_run()
6264 * perf_pending_event()
6265 *
6266 * But the event runs on CPU-B and wants disabling there.
6267 */
6268 irq_work_queue_on(&event->pending, cpu);
6269}
6270
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006271static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006272{
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006273 struct perf_event *event = container_of(entry, struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01006274 int rctx;
6275
6276 rctx = perf_swevent_get_recursion_context();
6277 /*
6278 * If we 'fail' here, that's OK, it means recursion is already disabled
6279 * and we won't recurse 'further'.
6280 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006281
Peter Zijlstra1d54ad92019-04-04 15:03:00 +02006282 perf_pending_event_disable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006283
6284 if (event->pending_wakeup) {
6285 event->pending_wakeup = 0;
6286 perf_event_wakeup(event);
6287 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01006288
6289 if (rctx >= 0)
6290 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006291}
6292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006293/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08006294 * We assume there is only KVM supporting the callbacks.
6295 * Later on, we might change it to a list if there is
6296 * another virtualization implementation supporting the callbacks.
6297 */
6298struct perf_guest_info_callbacks *perf_guest_cbs;
6299
6300int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6301{
6302 perf_guest_cbs = cbs;
6303 return 0;
6304}
6305EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
6306
6307int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
6308{
6309 perf_guest_cbs = NULL;
6310 return 0;
6311}
6312EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
6313
Jiri Olsa40189942012-08-07 15:20:37 +02006314static void
6315perf_output_sample_regs(struct perf_output_handle *handle,
6316 struct pt_regs *regs, u64 mask)
6317{
6318 int bit;
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306319 DECLARE_BITMAP(_mask, 64);
Jiri Olsa40189942012-08-07 15:20:37 +02006320
Madhavan Srinivasan29dd3282016-08-17 15:06:08 +05306321 bitmap_from_u64(_mask, mask);
6322 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) {
Jiri Olsa40189942012-08-07 15:20:37 +02006323 u64 val;
6324
6325 val = perf_reg_value(regs, bit);
6326 perf_output_put(handle, val);
6327 }
6328}
6329
Stephane Eranian60e23642014-09-24 13:48:37 +02006330static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006331 struct pt_regs *regs,
6332 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02006333{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006334 if (user_mode(regs)) {
6335 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02006336 regs_user->regs = regs;
Peter Zijlstra085ebfe2019-05-29 14:37:24 +02006337 } else if (!(current->flags & PF_KTHREAD)) {
Andy Lutomirski88a7c262015-01-04 10:36:19 -08006338 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02006339 } else {
6340 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
6341 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02006342 }
6343}
6344
Stephane Eranian60e23642014-09-24 13:48:37 +02006345static void perf_sample_regs_intr(struct perf_regs *regs_intr,
6346 struct pt_regs *regs)
6347{
6348 regs_intr->regs = regs;
6349 regs_intr->abi = perf_reg_abi(current);
6350}
6351
6352
Jiri Olsac5ebced2012-08-07 15:20:40 +02006353/*
6354 * Get remaining task size from user stack pointer.
6355 *
6356 * It'd be better to take stack vma map and limit this more
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03006357 * precisely, but there's no way to get it safely under interrupt,
Jiri Olsac5ebced2012-08-07 15:20:40 +02006358 * so using TASK_SIZE as limit.
6359 */
6360static u64 perf_ustack_task_size(struct pt_regs *regs)
6361{
6362 unsigned long addr = perf_user_stack_pointer(regs);
6363
6364 if (!addr || addr >= TASK_SIZE)
6365 return 0;
6366
6367 return TASK_SIZE - addr;
6368}
6369
6370static u16
6371perf_sample_ustack_size(u16 stack_size, u16 header_size,
6372 struct pt_regs *regs)
6373{
6374 u64 task_size;
6375
6376 /* No regs, no stack pointer, no dump. */
6377 if (!regs)
6378 return 0;
6379
6380 /*
6381 * Check if we fit in with the requested stack size into the:
6382 * - TASK_SIZE
6383 * If we don't, we limit the size to the TASK_SIZE.
6384 *
6385 * - remaining sample size
6386 * If we don't, we customize the stack size to
6387 * fit in to the remaining sample size.
6388 */
6389
6390 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
6391 stack_size = min(stack_size, (u16) task_size);
6392
6393 /* Current header size plus static size and dynamic size. */
6394 header_size += 2 * sizeof(u64);
6395
6396 /* Do we fit in with the current stack dump size? */
6397 if ((u16) (header_size + stack_size) < header_size) {
6398 /*
6399 * If we overflow the maximum size for the sample,
6400 * we customize the stack dump size to fit in.
6401 */
6402 stack_size = USHRT_MAX - header_size - sizeof(u64);
6403 stack_size = round_up(stack_size, sizeof(u64));
6404 }
6405
6406 return stack_size;
6407}
6408
6409static void
6410perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
6411 struct pt_regs *regs)
6412{
6413 /* Case of a kernel thread, nothing to dump */
6414 if (!regs) {
6415 u64 size = 0;
6416 perf_output_put(handle, size);
6417 } else {
6418 unsigned long sp;
6419 unsigned int rem;
6420 u64 dyn_size;
Yabin Cui02e18442018-08-23 15:59:35 -07006421 mm_segment_t fs;
Jiri Olsac5ebced2012-08-07 15:20:40 +02006422
6423 /*
6424 * We dump:
6425 * static size
6426 * - the size requested by user or the best one we can fit
6427 * in to the sample max size
6428 * data
6429 * - user stack dump data
6430 * dynamic size
6431 * - the actual dumped size
6432 */
6433
6434 /* Static size. */
6435 perf_output_put(handle, dump_size);
6436
6437 /* Data. */
6438 sp = perf_user_stack_pointer(regs);
Yabin Cui02e18442018-08-23 15:59:35 -07006439 fs = get_fs();
6440 set_fs(USER_DS);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006441 rem = __output_copy_user(handle, (void *) sp, dump_size);
Yabin Cui02e18442018-08-23 15:59:35 -07006442 set_fs(fs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006443 dyn_size = dump_size - rem;
6444
6445 perf_output_skip(handle, rem);
6446
6447 /* Dynamic size. */
6448 perf_output_put(handle, dyn_size);
6449 }
6450}
6451
Alexander Shishkina4faf002019-10-25 17:08:33 +03006452static unsigned long perf_prepare_sample_aux(struct perf_event *event,
6453 struct perf_sample_data *data,
6454 size_t size)
6455{
6456 struct perf_event *sampler = event->aux_event;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006457 struct perf_buffer *rb;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006458
6459 data->aux_size = 0;
6460
6461 if (!sampler)
6462 goto out;
6463
6464 if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE))
6465 goto out;
6466
6467 if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id()))
6468 goto out;
6469
6470 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6471 if (!rb)
6472 goto out;
6473
6474 /*
6475 * If this is an NMI hit inside sampling code, don't take
6476 * the sample. See also perf_aux_sample_output().
6477 */
6478 if (READ_ONCE(rb->aux_in_sampling)) {
6479 data->aux_size = 0;
6480 } else {
6481 size = min_t(size_t, size, perf_aux_size(rb));
6482 data->aux_size = ALIGN(size, sizeof(u64));
6483 }
6484 ring_buffer_put(rb);
6485
6486out:
6487 return data->aux_size;
6488}
6489
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006490long perf_pmu_snapshot_aux(struct perf_buffer *rb,
Alexander Shishkina4faf002019-10-25 17:08:33 +03006491 struct perf_event *event,
6492 struct perf_output_handle *handle,
6493 unsigned long size)
6494{
6495 unsigned long flags;
6496 long ret;
6497
6498 /*
6499 * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler
6500 * paths. If we start calling them in NMI context, they may race with
6501 * the IRQ ones, that is, for example, re-starting an event that's just
6502 * been stopped, which is why we're using a separate callback that
6503 * doesn't change the event state.
6504 *
6505 * IRQs need to be disabled to prevent IPIs from racing with us.
6506 */
6507 local_irq_save(flags);
6508 /*
6509 * Guard against NMI hits inside the critical section;
6510 * see also perf_prepare_sample_aux().
6511 */
6512 WRITE_ONCE(rb->aux_in_sampling, 1);
6513 barrier();
6514
6515 ret = event->pmu->snapshot_aux(event, handle, size);
6516
6517 barrier();
6518 WRITE_ONCE(rb->aux_in_sampling, 0);
6519 local_irq_restore(flags);
6520
6521 return ret;
6522}
6523
6524static void perf_aux_sample_output(struct perf_event *event,
6525 struct perf_output_handle *handle,
6526 struct perf_sample_data *data)
6527{
6528 struct perf_event *sampler = event->aux_event;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006529 struct perf_buffer *rb;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006530 unsigned long pad;
Alexander Shishkina4faf002019-10-25 17:08:33 +03006531 long size;
6532
6533 if (WARN_ON_ONCE(!sampler || !data->aux_size))
6534 return;
6535
6536 rb = ring_buffer_get(sampler->parent ? sampler->parent : sampler);
6537 if (!rb)
6538 return;
6539
6540 size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size);
6541
6542 /*
6543 * An error here means that perf_output_copy() failed (returned a
6544 * non-zero surplus that it didn't copy), which in its current
6545 * enlightened implementation is not possible. If that changes, we'd
6546 * like to know.
6547 */
6548 if (WARN_ON_ONCE(size < 0))
6549 goto out_put;
6550
6551 /*
6552 * The pad comes from ALIGN()ing data->aux_size up to u64 in
6553 * perf_prepare_sample_aux(), so should not be more than that.
6554 */
6555 pad = data->aux_size - size;
6556 if (WARN_ON_ONCE(pad >= sizeof(u64)))
6557 pad = 8;
6558
6559 if (pad) {
6560 u64 zero = 0;
6561 perf_output_copy(handle, &zero, pad);
6562 }
6563
6564out_put:
6565 ring_buffer_put(rb);
6566}
6567
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006568static void __perf_event_header__init_id(struct perf_event_header *header,
6569 struct perf_sample_data *data,
6570 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006571{
6572 u64 sample_type = event->attr.sample_type;
6573
6574 data->type = sample_type;
6575 header->size += event->id_header_size;
6576
6577 if (sample_type & PERF_SAMPLE_TID) {
6578 /* namespace issues */
6579 data->tid_entry.pid = perf_event_pid(event, current);
6580 data->tid_entry.tid = perf_event_tid(event, current);
6581 }
6582
6583 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01006584 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006585
Adrian Hunterff3d5272013-08-27 11:23:07 +03006586 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006587 data->id = primary_event_id(event);
6588
6589 if (sample_type & PERF_SAMPLE_STREAM_ID)
6590 data->stream_id = event->id;
6591
6592 if (sample_type & PERF_SAMPLE_CPU) {
6593 data->cpu_entry.cpu = raw_smp_processor_id();
6594 data->cpu_entry.reserved = 0;
6595 }
6596}
6597
Frederic Weisbecker76369132011-05-19 19:55:04 +02006598void perf_event_header__init_id(struct perf_event_header *header,
6599 struct perf_sample_data *data,
6600 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006601{
6602 if (event->attr.sample_id_all)
6603 __perf_event_header__init_id(header, data, event);
6604}
6605
6606static void __perf_event__output_id_sample(struct perf_output_handle *handle,
6607 struct perf_sample_data *data)
6608{
6609 u64 sample_type = data->type;
6610
6611 if (sample_type & PERF_SAMPLE_TID)
6612 perf_output_put(handle, data->tid_entry);
6613
6614 if (sample_type & PERF_SAMPLE_TIME)
6615 perf_output_put(handle, data->time);
6616
6617 if (sample_type & PERF_SAMPLE_ID)
6618 perf_output_put(handle, data->id);
6619
6620 if (sample_type & PERF_SAMPLE_STREAM_ID)
6621 perf_output_put(handle, data->stream_id);
6622
6623 if (sample_type & PERF_SAMPLE_CPU)
6624 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03006625
6626 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6627 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006628}
6629
Frederic Weisbecker76369132011-05-19 19:55:04 +02006630void perf_event__output_id_sample(struct perf_event *event,
6631 struct perf_output_handle *handle,
6632 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006633{
6634 if (event->attr.sample_id_all)
6635 __perf_event__output_id_sample(handle, sample);
6636}
6637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006638static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006639 struct perf_event *event,
6640 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006641{
6642 u64 read_format = event->attr.read_format;
6643 u64 values[4];
6644 int n = 0;
6645
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006646 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006647 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006648 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649 atomic64_read(&event->child_total_time_enabled);
6650 }
6651 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02006652 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006653 atomic64_read(&event->child_total_time_running);
6654 }
6655 if (read_format & PERF_FORMAT_ID)
6656 values[n++] = primary_event_id(event);
6657
Frederic Weisbecker76369132011-05-19 19:55:04 +02006658 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006659}
6660
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006661static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02006662 struct perf_event *event,
6663 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006664{
6665 struct perf_event *leader = event->group_leader, *sub;
6666 u64 read_format = event->attr.read_format;
6667 u64 values[5];
6668 int n = 0;
6669
6670 values[n++] = 1 + leader->nr_siblings;
6671
6672 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02006673 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006674
6675 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02006676 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677
Peter Zijlstra9e5b1272018-03-09 12:52:04 +01006678 if ((leader != event) &&
6679 (leader->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006680 leader->pmu->read(leader);
6681
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006682 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006683 if (read_format & PERF_FORMAT_ID)
6684 values[n++] = primary_event_id(leader);
6685
Frederic Weisbecker76369132011-05-19 19:55:04 +02006686 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687
Peter Zijlstraedb39592018-03-15 17:36:56 +01006688 for_each_sibling_event(sub, leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006689 n = 0;
6690
Jiri Olsa6f5ab002012-10-15 20:13:45 +02006691 if ((sub != event) &&
6692 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006693 sub->pmu->read(sub);
6694
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006695 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006696 if (read_format & PERF_FORMAT_ID)
6697 values[n++] = primary_event_id(sub);
6698
Frederic Weisbecker76369132011-05-19 19:55:04 +02006699 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006700 }
6701}
6702
Stephane Eranianeed01522010-10-26 16:08:01 +02006703#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
6704 PERF_FORMAT_TOTAL_TIME_RUNNING)
6705
Peter Zijlstraba5213a2017-05-30 11:45:12 +02006706/*
6707 * XXX PERF_SAMPLE_READ vs inherited events seems difficult.
6708 *
6709 * The problem is that its both hard and excessively expensive to iterate the
6710 * child list, not to mention that its impossible to IPI the children running
6711 * on another CPU, from interrupt/NMI context.
6712 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006713static void perf_output_read(struct perf_output_handle *handle,
6714 struct perf_event *event)
6715{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006716 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02006717 u64 read_format = event->attr.read_format;
6718
6719 /*
6720 * compute total_time_enabled, total_time_running
6721 * based on snapshot values taken when the event
6722 * was last scheduled in.
6723 *
6724 * we cannot simply called update_context_time()
6725 * because of locking issue as we are called in
6726 * NMI context
6727 */
Eric B Munsonc4794292011-06-23 16:34:38 -04006728 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01006729 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02006730
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006731 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02006732 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733 else
Stephane Eranianeed01522010-10-26 16:08:01 +02006734 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006735}
6736
Kan Liangbbfd5e42020-01-27 08:53:54 -08006737static inline bool perf_sample_save_hw_index(struct perf_event *event)
6738{
6739 return event->attr.branch_sample_type & PERF_SAMPLE_BRANCH_HW_INDEX;
6740}
6741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006742void perf_output_sample(struct perf_output_handle *handle,
6743 struct perf_event_header *header,
6744 struct perf_sample_data *data,
6745 struct perf_event *event)
6746{
6747 u64 sample_type = data->type;
6748
6749 perf_output_put(handle, *header);
6750
Adrian Hunterff3d5272013-08-27 11:23:07 +03006751 if (sample_type & PERF_SAMPLE_IDENTIFIER)
6752 perf_output_put(handle, data->id);
6753
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006754 if (sample_type & PERF_SAMPLE_IP)
6755 perf_output_put(handle, data->ip);
6756
6757 if (sample_type & PERF_SAMPLE_TID)
6758 perf_output_put(handle, data->tid_entry);
6759
6760 if (sample_type & PERF_SAMPLE_TIME)
6761 perf_output_put(handle, data->time);
6762
6763 if (sample_type & PERF_SAMPLE_ADDR)
6764 perf_output_put(handle, data->addr);
6765
6766 if (sample_type & PERF_SAMPLE_ID)
6767 perf_output_put(handle, data->id);
6768
6769 if (sample_type & PERF_SAMPLE_STREAM_ID)
6770 perf_output_put(handle, data->stream_id);
6771
6772 if (sample_type & PERF_SAMPLE_CPU)
6773 perf_output_put(handle, data->cpu_entry);
6774
6775 if (sample_type & PERF_SAMPLE_PERIOD)
6776 perf_output_put(handle, data->period);
6777
6778 if (sample_type & PERF_SAMPLE_READ)
6779 perf_output_read(handle, event);
6780
6781 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
Jiri Olsa99e818c2018-01-07 17:03:50 +01006782 int size = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006783
Jiri Olsa99e818c2018-01-07 17:03:50 +01006784 size += data->callchain->nr;
6785 size *= sizeof(u64);
6786 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006787 }
6788
6789 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006790 struct perf_raw_record *raw = data->raw;
Alexei Starovoitovfa128e62015-10-20 20:02:33 -07006791
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02006792 if (raw) {
6793 struct perf_raw_frag *frag = &raw->frag;
6794
6795 perf_output_put(handle, raw->size);
6796 do {
6797 if (frag->copy) {
6798 __output_custom(handle, frag->copy,
6799 frag->data, frag->size);
6800 } else {
6801 __output_copy(handle, frag->data,
6802 frag->size);
6803 }
6804 if (perf_raw_frag_last(frag))
6805 break;
6806 frag = frag->next;
6807 } while (1);
6808 if (frag->pad)
6809 __output_skip(handle, NULL, frag->pad);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006810 } else {
6811 struct {
6812 u32 size;
6813 u32 data;
6814 } raw = {
6815 .size = sizeof(u32),
6816 .data = 0,
6817 };
6818 perf_output_put(handle, raw);
6819 }
6820 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02006821
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006822 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
6823 if (data->br_stack) {
6824 size_t size;
6825
6826 size = data->br_stack->nr
6827 * sizeof(struct perf_branch_entry);
6828
6829 perf_output_put(handle, data->br_stack->nr);
Kan Liangbbfd5e42020-01-27 08:53:54 -08006830 if (perf_sample_save_hw_index(event))
6831 perf_output_put(handle, data->br_stack->hw_idx);
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006832 perf_output_copy(handle, data->br_stack->entries, size);
6833 } else {
6834 /*
6835 * we always store at least the value of nr
6836 */
6837 u64 nr = 0;
6838 perf_output_put(handle, nr);
6839 }
6840 }
Jiri Olsa40189942012-08-07 15:20:37 +02006841
6842 if (sample_type & PERF_SAMPLE_REGS_USER) {
6843 u64 abi = data->regs_user.abi;
6844
6845 /*
6846 * If there are no regs to dump, notice it through
6847 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6848 */
6849 perf_output_put(handle, abi);
6850
6851 if (abi) {
6852 u64 mask = event->attr.sample_regs_user;
6853 perf_output_sample_regs(handle,
6854 data->regs_user.regs,
6855 mask);
6856 }
6857 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02006858
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006859 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02006860 perf_output_sample_ustack(handle,
6861 data->stack_user_size,
6862 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006863 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01006864
6865 if (sample_type & PERF_SAMPLE_WEIGHT)
6866 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01006867
6868 if (sample_type & PERF_SAMPLE_DATA_SRC)
6869 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006870
Andi Kleenfdfbbd02013-09-20 07:40:39 -07006871 if (sample_type & PERF_SAMPLE_TRANSACTION)
6872 perf_output_put(handle, data->txn);
6873
Stephane Eranian60e23642014-09-24 13:48:37 +02006874 if (sample_type & PERF_SAMPLE_REGS_INTR) {
6875 u64 abi = data->regs_intr.abi;
6876 /*
6877 * If there are no regs to dump, notice it through
6878 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
6879 */
6880 perf_output_put(handle, abi);
6881
6882 if (abi) {
6883 u64 mask = event->attr.sample_regs_intr;
6884
6885 perf_output_sample_regs(handle,
6886 data->regs_intr.regs,
6887 mask);
6888 }
6889 }
6890
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006891 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
6892 perf_output_put(handle, data->phys_addr);
6893
Namhyung Kim6546b192020-03-25 21:45:29 +09006894 if (sample_type & PERF_SAMPLE_CGROUP)
6895 perf_output_put(handle, data->cgroup);
6896
Alexander Shishkina4faf002019-10-25 17:08:33 +03006897 if (sample_type & PERF_SAMPLE_AUX) {
6898 perf_output_put(handle, data->aux_size);
6899
6900 if (data->aux_size)
6901 perf_aux_sample_output(event, handle, data);
6902 }
6903
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006904 if (!event->attr.watermark) {
6905 int wakeup_events = event->attr.wakeup_events;
6906
6907 if (wakeup_events) {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05006908 struct perf_buffer *rb = handle->rb;
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02006909 int events = local_inc_return(&rb->events);
6910
6911 if (events >= wakeup_events) {
6912 local_sub(wakeup_events, &rb->events);
6913 local_inc(&rb->wakeup);
6914 }
6915 }
6916 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006917}
6918
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006919static u64 perf_virt_to_phys(u64 virt)
6920{
6921 u64 phys_addr = 0;
6922 struct page *p = NULL;
6923
6924 if (!virt)
6925 return 0;
6926
6927 if (virt >= TASK_SIZE) {
6928 /* If it's vmalloc()d memory, leave phys_addr as 0 */
6929 if (virt_addr_valid((void *)(uintptr_t)virt) &&
6930 !(virt >= VMALLOC_START && virt < VMALLOC_END))
6931 phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt);
6932 } else {
6933 /*
6934 * Walking the pages tables for user address.
6935 * Interrupts are disabled, so it prevents any tear down
6936 * of the page tables.
Souptick Joarderdadbb612020-06-07 21:40:55 -07006937 * Try IRQ-safe get_user_page_fast_only first.
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006938 * If failed, leave phys_addr as 0.
6939 */
Jiri Olsad3296fb2020-04-07 16:14:27 +02006940 if (current->mm != NULL) {
6941 pagefault_disable();
Souptick Joarderdadbb612020-06-07 21:40:55 -07006942 if (get_user_page_fast_only(virt, 0, &p))
Jiri Olsad3296fb2020-04-07 16:14:27 +02006943 phys_addr = page_to_phys(p) + virt % PAGE_SIZE;
6944 pagefault_enable();
6945 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04006946
6947 if (p)
6948 put_page(p);
6949 }
6950
6951 return phys_addr;
6952}
6953
Jiri Olsa99e818c2018-01-07 17:03:50 +01006954static struct perf_callchain_entry __empty_callchain = { .nr = 0, };
6955
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02006956struct perf_callchain_entry *
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006957perf_callchain(struct perf_event *event, struct pt_regs *regs)
6958{
6959 bool kernel = !event->attr.exclude_callchain_kernel;
6960 bool user = !event->attr.exclude_callchain_user;
6961 /* Disallow cross-task user callchains. */
6962 bool crosstask = event->ctx->task && event->ctx->task != current;
6963 const u32 max_stack = event->attr.sample_max_stack;
Jiri Olsa99e818c2018-01-07 17:03:50 +01006964 struct perf_callchain_entry *callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006965
6966 if (!kernel && !user)
Jiri Olsa99e818c2018-01-07 17:03:50 +01006967 return &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006968
Jiri Olsa99e818c2018-01-07 17:03:50 +01006969 callchain = get_perf_callchain(regs, 0, kernel, user,
6970 max_stack, crosstask, true);
6971 return callchain ?: &__empty_callchain;
Jiri Olsa8cf7e0e2018-01-07 17:03:49 +01006972}
6973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006974void perf_prepare_sample(struct perf_event_header *header,
6975 struct perf_sample_data *data,
6976 struct perf_event *event,
6977 struct pt_regs *regs)
6978{
6979 u64 sample_type = event->attr.sample_type;
6980
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006981 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006982 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983
6984 header->misc = 0;
6985 header->misc |= perf_misc_flags(regs);
6986
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02006987 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006988
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006989 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006990 data->ip = perf_instruction_pointer(regs);
6991
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006992 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
6993 int size = 1;
6994
Peter Zijlstra6cbc3042018-05-10 15:48:41 +02006995 if (!(sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY))
6996 data->callchain = perf_callchain(event, regs);
6997
Jiri Olsa99e818c2018-01-07 17:03:50 +01006998 size += data->callchain->nr;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006999
7000 header->size += size * sizeof(u64);
7001 }
7002
7003 if (sample_type & PERF_SAMPLE_RAW) {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007004 struct perf_raw_record *raw = data->raw;
7005 int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007006
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007007 if (raw) {
7008 struct perf_raw_frag *frag = &raw->frag;
7009 u32 sum = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007010
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02007011 do {
7012 sum += frag->size;
7013 if (perf_raw_frag_last(frag))
7014 break;
7015 frag = frag->next;
7016 } while (1);
7017
7018 size = round_up(sum + sizeof(u32), sizeof(u64));
7019 raw->size = size - sizeof(u32);
7020 frag->pad = raw->size - sum;
7021 } else {
7022 size = sizeof(u64);
7023 }
7024
7025 header->size += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007026 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007027
7028 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
7029 int size = sizeof(u64); /* nr */
7030 if (data->br_stack) {
Kan Liangbbfd5e42020-01-27 08:53:54 -08007031 if (perf_sample_save_hw_index(event))
7032 size += sizeof(u64);
7033
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007034 size += data->br_stack->nr
7035 * sizeof(struct perf_branch_entry);
7036 }
7037 header->size += size;
7038 }
Jiri Olsa40189942012-08-07 15:20:37 +02007039
Peter Zijlstra25657112014-09-24 13:48:42 +02007040 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08007041 perf_sample_regs_user(&data->regs_user, regs,
7042 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02007043
Jiri Olsa40189942012-08-07 15:20:37 +02007044 if (sample_type & PERF_SAMPLE_REGS_USER) {
7045 /* regs dump ABI info */
7046 int size = sizeof(u64);
7047
Jiri Olsa40189942012-08-07 15:20:37 +02007048 if (data->regs_user.regs) {
7049 u64 mask = event->attr.sample_regs_user;
7050 size += hweight64(mask) * sizeof(u64);
7051 }
7052
7053 header->size += size;
7054 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02007055
7056 if (sample_type & PERF_SAMPLE_STACK_USER) {
7057 /*
Roy Ben Shlomo9f014e32019-09-20 20:12:53 +03007058 * Either we need PERF_SAMPLE_STACK_USER bit to be always
Jiri Olsac5ebced2012-08-07 15:20:40 +02007059 * processed as the last one or have additional check added
7060 * in case new sample type is added, because we could eat
7061 * up the rest of the sample size.
7062 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02007063 u16 stack_size = event->attr.sample_stack_user;
7064 u16 size = sizeof(u64);
7065
Jiri Olsac5ebced2012-08-07 15:20:40 +02007066 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02007067 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007068
7069 /*
7070 * If there is something to dump, add space for the dump
7071 * itself and for the field that tells the dynamic size,
7072 * which is how many have been actually dumped.
7073 */
7074 if (stack_size)
7075 size += sizeof(u64) + stack_size;
7076
7077 data->stack_user_size = stack_size;
7078 header->size += size;
7079 }
Stephane Eranian60e23642014-09-24 13:48:37 +02007080
7081 if (sample_type & PERF_SAMPLE_REGS_INTR) {
7082 /* regs dump ABI info */
7083 int size = sizeof(u64);
7084
7085 perf_sample_regs_intr(&data->regs_intr, regs);
7086
7087 if (data->regs_intr.regs) {
7088 u64 mask = event->attr.sample_regs_intr;
7089
7090 size += hweight64(mask) * sizeof(u64);
7091 }
7092
7093 header->size += size;
7094 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -04007095
7096 if (sample_type & PERF_SAMPLE_PHYS_ADDR)
7097 data->phys_addr = perf_virt_to_phys(data->addr);
Alexander Shishkina4faf002019-10-25 17:08:33 +03007098
Namhyung Kim6546b192020-03-25 21:45:29 +09007099#ifdef CONFIG_CGROUP_PERF
7100 if (sample_type & PERF_SAMPLE_CGROUP) {
7101 struct cgroup *cgrp;
7102
7103 /* protected by RCU */
7104 cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup;
7105 data->cgroup = cgroup_id(cgrp);
7106 }
7107#endif
7108
Alexander Shishkina4faf002019-10-25 17:08:33 +03007109 if (sample_type & PERF_SAMPLE_AUX) {
7110 u64 size;
7111
7112 header->size += sizeof(u64); /* size */
7113
7114 /*
7115 * Given the 16bit nature of header::size, an AUX sample can
7116 * easily overflow it, what with all the preceding sample bits.
7117 * Make sure this doesn't happen by using up to U16_MAX bytes
7118 * per sample in total (rounded down to 8 byte boundary).
7119 */
7120 size = min_t(size_t, U16_MAX - header->size,
7121 event->attr.aux_sample_size);
7122 size = rounddown(size, 8);
7123 size = perf_prepare_sample_aux(event, data, size);
7124
7125 WARN_ON_ONCE(size + header->size > U16_MAX);
7126 header->size += size;
7127 }
7128 /*
7129 * If you're adding more sample types here, you likely need to do
7130 * something about the overflowing header::size, like repurpose the
7131 * lowest 3 bits of size, which should be always zero at the moment.
7132 * This raises a more important question, do we really need 512k sized
7133 * samples and why, so good argumentation is in order for whatever you
7134 * do here next.
7135 */
7136 WARN_ON_ONCE(header->size & 7);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007137}
7138
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007139static __always_inline int
Wang Nan9ecda412016-04-05 14:11:18 +00007140__perf_event_output(struct perf_event *event,
7141 struct perf_sample_data *data,
7142 struct pt_regs *regs,
7143 int (*output_begin)(struct perf_output_handle *,
7144 struct perf_event *,
7145 unsigned int))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007146{
7147 struct perf_output_handle handle;
7148 struct perf_event_header header;
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007149 int err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007150
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007151 /* protect the callchain buffers */
7152 rcu_read_lock();
7153
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154 perf_prepare_sample(&header, data, event, regs);
7155
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007156 err = output_begin(&handle, event, header.size);
7157 if (err)
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007158 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159
7160 perf_output_sample(&handle, &header, data, event);
7161
7162 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007163
7164exit:
7165 rcu_read_unlock();
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007166 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007167}
7168
Wang Nan9ecda412016-04-05 14:11:18 +00007169void
7170perf_event_output_forward(struct perf_event *event,
7171 struct perf_sample_data *data,
7172 struct pt_regs *regs)
7173{
7174 __perf_event_output(event, data, regs, perf_output_begin_forward);
7175}
7176
7177void
7178perf_event_output_backward(struct perf_event *event,
7179 struct perf_sample_data *data,
7180 struct pt_regs *regs)
7181{
7182 __perf_event_output(event, data, regs, perf_output_begin_backward);
7183}
7184
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007185int
Wang Nan9ecda412016-04-05 14:11:18 +00007186perf_event_output(struct perf_event *event,
7187 struct perf_sample_data *data,
7188 struct pt_regs *regs)
7189{
Arnaldo Carvalho de Melo56201962019-01-11 13:20:20 -03007190 return __perf_event_output(event, data, regs, perf_output_begin);
Wang Nan9ecda412016-04-05 14:11:18 +00007191}
7192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007193/*
7194 * read event_id
7195 */
7196
7197struct perf_read_event {
7198 struct perf_event_header header;
7199
7200 u32 pid;
7201 u32 tid;
7202};
7203
7204static void
7205perf_event_read_event(struct perf_event *event,
7206 struct task_struct *task)
7207{
7208 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007209 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007210 struct perf_read_event read_event = {
7211 .header = {
7212 .type = PERF_RECORD_READ,
7213 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007214 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007215 },
7216 .pid = perf_event_pid(event, task),
7217 .tid = perf_event_tid(event, task),
7218 };
7219 int ret;
7220
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007221 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007222 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007223 if (ret)
7224 return;
7225
7226 perf_output_put(&handle, read_event);
7227 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007228 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007229
7230 perf_output_end(&handle);
7231}
7232
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007233typedef void (perf_iterate_f)(struct perf_event *event, void *data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007234
7235static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007236perf_iterate_ctx(struct perf_event_context *ctx,
7237 perf_iterate_f output,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007238 void *data, bool all)
Jiri Olsa52d857a2013-05-06 18:27:18 +02007239{
7240 struct perf_event *event;
7241
7242 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007243 if (!all) {
7244 if (event->state < PERF_EVENT_STATE_INACTIVE)
7245 continue;
7246 if (!event_filter_match(event))
7247 continue;
7248 }
7249
Jiri Olsa67516842013-07-09 18:56:31 +02007250 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007251 }
7252}
7253
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007254static void perf_iterate_sb_cpu(perf_iterate_f output, void *data)
Kan Liangf2fb6be2016-03-23 11:24:37 -07007255{
7256 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events);
7257 struct perf_event *event;
7258
7259 list_for_each_entry_rcu(event, &pel->list, sb_list) {
Peter Zijlstra0b8f1e22016-08-04 14:37:24 +02007260 /*
7261 * Skip events that are not fully formed yet; ensure that
7262 * if we observe event->ctx, both event and ctx will be
7263 * complete enough. See perf_install_in_context().
7264 */
7265 if (!smp_load_acquire(&event->ctx))
7266 continue;
7267
Kan Liangf2fb6be2016-03-23 11:24:37 -07007268 if (event->state < PERF_EVENT_STATE_INACTIVE)
7269 continue;
7270 if (!event_filter_match(event))
7271 continue;
7272 output(event, data);
7273 }
7274}
7275
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007276/*
7277 * Iterate all events that need to receive side-band events.
7278 *
7279 * For new callers; ensure that account_pmu_sb_event() includes
7280 * your event, otherwise it might not get delivered.
7281 */
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007282static void
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007283perf_iterate_sb(perf_iterate_f output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007284 struct perf_event_context *task_ctx)
7285{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007286 struct perf_event_context *ctx;
Jiri Olsa52d857a2013-05-06 18:27:18 +02007287 int ctxn;
7288
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007289 rcu_read_lock();
7290 preempt_disable();
7291
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007292 /*
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007293 * If we have task_ctx != NULL we only notify the task context itself.
7294 * The task_ctx is set only for EXIT events before releasing task
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007295 * context.
7296 */
7297 if (task_ctx) {
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007298 perf_iterate_ctx(task_ctx, output, data, false);
7299 goto done;
Jiri Olsa4e93ad62015-11-04 16:00:05 +01007300 }
7301
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007302 perf_iterate_sb_cpu(output, data);
Kan Liangf2fb6be2016-03-23 11:24:37 -07007303
7304 for_each_task_context_nr(ctxn) {
Jiri Olsa52d857a2013-05-06 18:27:18 +02007305 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
7306 if (ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007307 perf_iterate_ctx(ctx, output, data, false);
Jiri Olsa52d857a2013-05-06 18:27:18 +02007308 }
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007309done:
Kan Liangf2fb6be2016-03-23 11:24:37 -07007310 preempt_enable();
Jiri Olsa52d857a2013-05-06 18:27:18 +02007311 rcu_read_unlock();
7312}
7313
Alexander Shishkin375637b2016-04-27 18:44:46 +03007314/*
7315 * Clear all file-based filters at exec, they'll have to be
7316 * re-instated when/if these objects are mmapped again.
7317 */
7318static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
7319{
7320 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7321 struct perf_addr_filter *filter;
7322 unsigned int restart = 0, count = 0;
7323 unsigned long flags;
7324
7325 if (!has_addr_filter(event))
7326 return;
7327
7328 raw_spin_lock_irqsave(&ifh->lock, flags);
7329 list_for_each_entry(filter, &ifh->list, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07007330 if (filter->path.dentry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02007331 event->addr_filter_ranges[count].start = 0;
7332 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007333 restart++;
7334 }
7335
7336 count++;
7337 }
7338
7339 if (restart)
7340 event->addr_filters_gen++;
7341 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7342
7343 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03007344 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03007345}
7346
7347void perf_event_exec(void)
7348{
7349 struct perf_event_context *ctx;
7350 int ctxn;
7351
7352 rcu_read_lock();
7353 for_each_task_context_nr(ctxn) {
7354 ctx = current->perf_event_ctxp[ctxn];
7355 if (!ctx)
7356 continue;
7357
7358 perf_event_enable_on_exec(ctxn);
7359
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007360 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL,
Alexander Shishkin375637b2016-04-27 18:44:46 +03007361 true);
7362 }
7363 rcu_read_unlock();
7364}
7365
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007366struct remote_output {
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05007367 struct perf_buffer *rb;
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007368 int err;
7369};
7370
7371static void __perf_event_output_stop(struct perf_event *event, void *data)
7372{
7373 struct perf_event *parent = event->parent;
7374 struct remote_output *ro = data;
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -05007375 struct perf_buffer *rb = ro->rb;
Alexander Shishkin375637b2016-04-27 18:44:46 +03007376 struct stop_event_data sd = {
7377 .event = event,
7378 };
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007379
7380 if (!has_aux(event))
7381 return;
7382
7383 if (!parent)
7384 parent = event;
7385
7386 /*
7387 * In case of inheritance, it will be the parent that links to the
Alexander Shishkin767ae082016-09-06 16:23:49 +03007388 * ring-buffer, but it will be the child that's actually using it.
7389 *
7390 * We are using event::rb to determine if the event should be stopped,
7391 * however this may race with ring_buffer_attach() (through set_output),
7392 * which will make us skip the event that actually needs to be stopped.
7393 * So ring_buffer_attach() has to stop an aux event before re-assigning
7394 * its rb pointer.
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007395 */
7396 if (rcu_dereference(parent->rb) == rb)
Alexander Shishkin375637b2016-04-27 18:44:46 +03007397 ro->err = __perf_event_stop(&sd);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007398}
7399
7400static int __perf_pmu_output_stop(void *info)
7401{
7402 struct perf_event *event = info;
Alexander Shishkinf3a519e2019-10-22 10:39:40 +03007403 struct pmu *pmu = event->ctx->pmu;
Will Deacon8b6a3fe2016-08-24 10:07:14 +01007404 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007405 struct remote_output ro = {
7406 .rb = event->rb,
7407 };
7408
7409 rcu_read_lock();
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007410 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007411 if (cpuctx->task_ctx)
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007412 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop,
Alexander Shishkinb73e4fe2016-04-27 18:44:45 +03007413 &ro, false);
Alexander Shishkin95ff4ca2015-12-02 18:41:11 +02007414 rcu_read_unlock();
7415
7416 return ro.err;
7417}
7418
7419static void perf_pmu_output_stop(struct perf_event *event)
7420{
7421 struct perf_event *iter;
7422 int err, cpu;
7423
7424restart:
7425 rcu_read_lock();
7426 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
7427 /*
7428 * For per-CPU events, we need to make sure that neither they
7429 * nor their children are running; for cpu==-1 events it's
7430 * sufficient to stop the event itself if it's active, since
7431 * it can't have children.
7432 */
7433 cpu = iter->cpu;
7434 if (cpu == -1)
7435 cpu = READ_ONCE(iter->oncpu);
7436
7437 if (cpu == -1)
7438 continue;
7439
7440 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
7441 if (err == -EAGAIN) {
7442 rcu_read_unlock();
7443 goto restart;
7444 }
7445 }
7446 rcu_read_unlock();
7447}
7448
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007449/*
7450 * task tracking -- fork/exit
7451 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02007452 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007453 */
7454
7455struct perf_task_event {
7456 struct task_struct *task;
7457 struct perf_event_context *task_ctx;
7458
7459 struct {
7460 struct perf_event_header header;
7461
7462 u32 pid;
7463 u32 ppid;
7464 u32 tid;
7465 u32 ptid;
7466 u64 time;
7467 } event_id;
7468};
7469
Jiri Olsa67516842013-07-09 18:56:31 +02007470static int perf_event_task_match(struct perf_event *event)
7471{
Stephane Eranian13d7a242013-08-21 12:10:24 +02007472 return event->attr.comm || event->attr.mmap ||
7473 event->attr.mmap2 || event->attr.mmap_data ||
7474 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02007475}
7476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007477static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007478 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007479{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007480 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007481 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007482 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007483 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007484 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01007485
Jiri Olsa67516842013-07-09 18:56:31 +02007486 if (!perf_event_task_match(event))
7487 return;
7488
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007489 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007490
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007491 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007492 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02007493 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007494 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007495
7496 task_event->event_id.pid = perf_event_pid(event, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007497 task_event->event_id.tid = perf_event_tid(event, task);
Ian Rogersf3bed552020-04-17 11:28:42 -07007498
7499 if (task_event->event_id.header.type == PERF_RECORD_EXIT) {
7500 task_event->event_id.ppid = perf_event_pid(event,
7501 task->real_parent);
7502 task_event->event_id.ptid = perf_event_pid(event,
7503 task->real_parent);
7504 } else { /* PERF_RECORD_FORK */
7505 task_event->event_id.ppid = perf_event_pid(event, current);
7506 task_event->event_id.ptid = perf_event_tid(event, current);
7507 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007508
Peter Zijlstra34f43922015-02-20 14:05:38 +01007509 task_event->event_id.time = perf_event_clock(event);
7510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007511 perf_output_put(&handle, task_event->event_id);
7512
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007513 perf_event__output_id_sample(event, &handle, &sample);
7514
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007515 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007516out:
7517 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007518}
7519
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007520static void perf_event_task(struct task_struct *task,
7521 struct perf_event_context *task_ctx,
7522 int new)
7523{
7524 struct perf_task_event task_event;
7525
7526 if (!atomic_read(&nr_comm_events) &&
7527 !atomic_read(&nr_mmap_events) &&
7528 !atomic_read(&nr_task_events))
7529 return;
7530
7531 task_event = (struct perf_task_event){
7532 .task = task,
7533 .task_ctx = task_ctx,
7534 .event_id = {
7535 .header = {
7536 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
7537 .misc = 0,
7538 .size = sizeof(task_event.event_id),
7539 },
7540 /* .pid */
7541 /* .ppid */
7542 /* .tid */
7543 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01007544 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007545 },
7546 };
7547
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007548 perf_iterate_sb(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007549 &task_event,
7550 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007551}
7552
7553void perf_event_fork(struct task_struct *task)
7554{
7555 perf_event_task(task, NULL, 1);
Hari Bathinie4222672017-03-08 02:11:36 +05307556 perf_event_namespaces(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007557}
7558
7559/*
7560 * comm tracking
7561 */
7562
7563struct perf_comm_event {
7564 struct task_struct *task;
7565 char *comm;
7566 int comm_size;
7567
7568 struct {
7569 struct perf_event_header header;
7570
7571 u32 pid;
7572 u32 tid;
7573 } event_id;
7574};
7575
Jiri Olsa67516842013-07-09 18:56:31 +02007576static int perf_event_comm_match(struct perf_event *event)
7577{
7578 return event->attr.comm;
7579}
7580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007581static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007582 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007583{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007584 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007585 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007586 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007587 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007588 int ret;
7589
Jiri Olsa67516842013-07-09 18:56:31 +02007590 if (!perf_event_comm_match(event))
7591 return;
7592
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007593 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
7594 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007595 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007596
7597 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007598 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007599
7600 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
7601 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
7602
7603 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02007604 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007605 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007606
7607 perf_event__output_id_sample(event, &handle, &sample);
7608
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007609 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007610out:
7611 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007612}
7613
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007614static void perf_event_comm_event(struct perf_comm_event *comm_event)
7615{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007616 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007617 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007618
7619 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01007620 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007621 size = ALIGN(strlen(comm)+1, sizeof(u64));
7622
7623 comm_event->comm = comm;
7624 comm_event->comm_size = size;
7625
7626 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007627
Peter Zijlstraaab5b712016-05-12 17:26:46 +02007628 perf_iterate_sb(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007629 comm_event,
7630 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007631}
7632
Adrian Hunter82b89772014-05-28 11:45:04 +03007633void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007634{
7635 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007636
7637 if (!atomic_read(&nr_comm_events))
7638 return;
7639
7640 comm_event = (struct perf_comm_event){
7641 .task = task,
7642 /* .comm */
7643 /* .comm_size */
7644 .event_id = {
7645 .header = {
7646 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03007647 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007648 /* .size */
7649 },
7650 /* .pid */
7651 /* .tid */
7652 },
7653 };
7654
7655 perf_event_comm_event(&comm_event);
7656}
7657
7658/*
Hari Bathinie4222672017-03-08 02:11:36 +05307659 * namespaces tracking
7660 */
7661
7662struct perf_namespaces_event {
7663 struct task_struct *task;
7664
7665 struct {
7666 struct perf_event_header header;
7667
7668 u32 pid;
7669 u32 tid;
7670 u64 nr_namespaces;
7671 struct perf_ns_link_info link_info[NR_NAMESPACES];
7672 } event_id;
7673};
7674
7675static int perf_event_namespaces_match(struct perf_event *event)
7676{
7677 return event->attr.namespaces;
7678}
7679
7680static void perf_event_namespaces_output(struct perf_event *event,
7681 void *data)
7682{
7683 struct perf_namespaces_event *namespaces_event = data;
7684 struct perf_output_handle handle;
7685 struct perf_sample_data sample;
Jiri Olsa34900ec2017-08-09 18:14:06 +02007686 u16 header_size = namespaces_event->event_id.header.size;
Hari Bathinie4222672017-03-08 02:11:36 +05307687 int ret;
7688
7689 if (!perf_event_namespaces_match(event))
7690 return;
7691
7692 perf_event_header__init_id(&namespaces_event->event_id.header,
7693 &sample, event);
7694 ret = perf_output_begin(&handle, event,
7695 namespaces_event->event_id.header.size);
7696 if (ret)
Jiri Olsa34900ec2017-08-09 18:14:06 +02007697 goto out;
Hari Bathinie4222672017-03-08 02:11:36 +05307698
7699 namespaces_event->event_id.pid = perf_event_pid(event,
7700 namespaces_event->task);
7701 namespaces_event->event_id.tid = perf_event_tid(event,
7702 namespaces_event->task);
7703
7704 perf_output_put(&handle, namespaces_event->event_id);
7705
7706 perf_event__output_id_sample(event, &handle, &sample);
7707
7708 perf_output_end(&handle);
Jiri Olsa34900ec2017-08-09 18:14:06 +02007709out:
7710 namespaces_event->event_id.header.size = header_size;
Hari Bathinie4222672017-03-08 02:11:36 +05307711}
7712
7713static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info,
7714 struct task_struct *task,
7715 const struct proc_ns_operations *ns_ops)
7716{
7717 struct path ns_path;
7718 struct inode *ns_inode;
Aleksa Saraice623f82019-12-07 01:13:27 +11007719 int error;
Hari Bathinie4222672017-03-08 02:11:36 +05307720
7721 error = ns_get_path(&ns_path, task, ns_ops);
7722 if (!error) {
7723 ns_inode = ns_path.dentry->d_inode;
7724 ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev);
7725 ns_link_info->ino = ns_inode->i_ino;
Vasily Averin0e18dd12017-11-15 08:47:02 +03007726 path_put(&ns_path);
Hari Bathinie4222672017-03-08 02:11:36 +05307727 }
7728}
7729
7730void perf_event_namespaces(struct task_struct *task)
7731{
7732 struct perf_namespaces_event namespaces_event;
7733 struct perf_ns_link_info *ns_link_info;
7734
7735 if (!atomic_read(&nr_namespaces_events))
7736 return;
7737
7738 namespaces_event = (struct perf_namespaces_event){
7739 .task = task,
7740 .event_id = {
7741 .header = {
7742 .type = PERF_RECORD_NAMESPACES,
7743 .misc = 0,
7744 .size = sizeof(namespaces_event.event_id),
7745 },
7746 /* .pid */
7747 /* .tid */
7748 .nr_namespaces = NR_NAMESPACES,
7749 /* .link_info[NR_NAMESPACES] */
7750 },
7751 };
7752
7753 ns_link_info = namespaces_event.event_id.link_info;
7754
7755 perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX],
7756 task, &mntns_operations);
7757
7758#ifdef CONFIG_USER_NS
7759 perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX],
7760 task, &userns_operations);
7761#endif
7762#ifdef CONFIG_NET_NS
7763 perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX],
7764 task, &netns_operations);
7765#endif
7766#ifdef CONFIG_UTS_NS
7767 perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX],
7768 task, &utsns_operations);
7769#endif
7770#ifdef CONFIG_IPC_NS
7771 perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX],
7772 task, &ipcns_operations);
7773#endif
7774#ifdef CONFIG_PID_NS
7775 perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX],
7776 task, &pidns_operations);
7777#endif
7778#ifdef CONFIG_CGROUPS
7779 perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX],
7780 task, &cgroupns_operations);
7781#endif
7782
7783 perf_iterate_sb(perf_event_namespaces_output,
7784 &namespaces_event,
7785 NULL);
7786}
7787
7788/*
Namhyung Kim96aaab62020-03-25 21:45:28 +09007789 * cgroup tracking
7790 */
7791#ifdef CONFIG_CGROUP_PERF
7792
7793struct perf_cgroup_event {
7794 char *path;
7795 int path_size;
7796 struct {
7797 struct perf_event_header header;
7798 u64 id;
7799 char path[];
7800 } event_id;
7801};
7802
7803static int perf_event_cgroup_match(struct perf_event *event)
7804{
7805 return event->attr.cgroup;
7806}
7807
7808static void perf_event_cgroup_output(struct perf_event *event, void *data)
7809{
7810 struct perf_cgroup_event *cgroup_event = data;
7811 struct perf_output_handle handle;
7812 struct perf_sample_data sample;
7813 u16 header_size = cgroup_event->event_id.header.size;
7814 int ret;
7815
7816 if (!perf_event_cgroup_match(event))
7817 return;
7818
7819 perf_event_header__init_id(&cgroup_event->event_id.header,
7820 &sample, event);
7821 ret = perf_output_begin(&handle, event,
7822 cgroup_event->event_id.header.size);
7823 if (ret)
7824 goto out;
7825
7826 perf_output_put(&handle, cgroup_event->event_id);
7827 __output_copy(&handle, cgroup_event->path, cgroup_event->path_size);
7828
7829 perf_event__output_id_sample(event, &handle, &sample);
7830
7831 perf_output_end(&handle);
7832out:
7833 cgroup_event->event_id.header.size = header_size;
7834}
7835
7836static void perf_event_cgroup(struct cgroup *cgrp)
7837{
7838 struct perf_cgroup_event cgroup_event;
7839 char path_enomem[16] = "//enomem";
7840 char *pathname;
7841 size_t size;
7842
7843 if (!atomic_read(&nr_cgroup_events))
7844 return;
7845
7846 cgroup_event = (struct perf_cgroup_event){
7847 .event_id = {
7848 .header = {
7849 .type = PERF_RECORD_CGROUP,
7850 .misc = 0,
7851 .size = sizeof(cgroup_event.event_id),
7852 },
7853 .id = cgroup_id(cgrp),
7854 },
7855 };
7856
7857 pathname = kmalloc(PATH_MAX, GFP_KERNEL);
7858 if (pathname == NULL) {
7859 cgroup_event.path = path_enomem;
7860 } else {
7861 /* just to be sure to have enough space for alignment */
7862 cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64));
7863 cgroup_event.path = pathname;
7864 }
7865
7866 /*
7867 * Since our buffer works in 8 byte units we need to align our string
7868 * size to a multiple of 8. However, we must guarantee the tail end is
7869 * zero'd out to avoid leaking random bits to userspace.
7870 */
7871 size = strlen(cgroup_event.path) + 1;
7872 while (!IS_ALIGNED(size, sizeof(u64)))
7873 cgroup_event.path[size++] = '\0';
7874
7875 cgroup_event.event_id.header.size += size;
7876 cgroup_event.path_size = size;
7877
7878 perf_iterate_sb(perf_event_cgroup_output,
7879 &cgroup_event,
7880 NULL);
7881
7882 kfree(pathname);
7883}
7884
7885#endif
7886
7887/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007888 * mmap tracking
7889 */
7890
7891struct perf_mmap_event {
7892 struct vm_area_struct *vma;
7893
7894 const char *file_name;
7895 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007896 int maj, min;
7897 u64 ino;
7898 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007899 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007900
7901 struct {
7902 struct perf_event_header header;
7903
7904 u32 pid;
7905 u32 tid;
7906 u64 start;
7907 u64 len;
7908 u64 pgoff;
7909 } event_id;
7910};
7911
Jiri Olsa67516842013-07-09 18:56:31 +02007912static int perf_event_mmap_match(struct perf_event *event,
7913 void *data)
7914{
7915 struct perf_mmap_event *mmap_event = data;
7916 struct vm_area_struct *vma = mmap_event->vma;
7917 int executable = vma->vm_flags & VM_EXEC;
7918
7919 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02007920 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02007921}
7922
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007923static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02007924 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007925{
Jiri Olsa52d857a2013-05-06 18:27:18 +02007926 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007927 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007928 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007929 int size = mmap_event->event_id.header.size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007930 u32 type = mmap_event->event_id.header.type;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007931 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007932
Jiri Olsa67516842013-07-09 18:56:31 +02007933 if (!perf_event_mmap_match(event, data))
7934 return;
7935
Stephane Eranian13d7a242013-08-21 12:10:24 +02007936 if (event->attr.mmap2) {
7937 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
7938 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
7939 mmap_event->event_id.header.size += sizeof(mmap_event->min);
7940 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03007941 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007942 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
7943 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007944 }
7945
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007946 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
7947 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02007948 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007949 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007950 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007951
7952 mmap_event->event_id.pid = perf_event_pid(event, current);
7953 mmap_event->event_id.tid = perf_event_tid(event, current);
7954
7955 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007956
7957 if (event->attr.mmap2) {
7958 perf_output_put(&handle, mmap_event->maj);
7959 perf_output_put(&handle, mmap_event->min);
7960 perf_output_put(&handle, mmap_event->ino);
7961 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007962 perf_output_put(&handle, mmap_event->prot);
7963 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02007964 }
7965
Frederic Weisbecker76369132011-05-19 19:55:04 +02007966 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007967 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007968
7969 perf_event__output_id_sample(event, &handle, &sample);
7970
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007971 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02007972out:
7973 mmap_event->event_id.header.size = size;
Stephane Eraniand9c1bb22019-03-07 10:52:33 -08007974 mmap_event->event_id.header.type = type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007975}
7976
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007977static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
7978{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007979 struct vm_area_struct *vma = mmap_event->vma;
7980 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02007981 int maj = 0, min = 0;
7982 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04007983 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007984 unsigned int size;
7985 char tmp[16];
7986 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02007987 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007988
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01007989 if (vma->vm_flags & VM_READ)
7990 prot |= PROT_READ;
7991 if (vma->vm_flags & VM_WRITE)
7992 prot |= PROT_WRITE;
7993 if (vma->vm_flags & VM_EXEC)
7994 prot |= PROT_EXEC;
7995
7996 if (vma->vm_flags & VM_MAYSHARE)
7997 flags = MAP_SHARED;
7998 else
7999 flags = MAP_PRIVATE;
8000
8001 if (vma->vm_flags & VM_DENYWRITE)
8002 flags |= MAP_DENYWRITE;
8003 if (vma->vm_flags & VM_MAYEXEC)
8004 flags |= MAP_EXECUTABLE;
8005 if (vma->vm_flags & VM_LOCKED)
8006 flags |= MAP_LOCKED;
Anshuman Khandual03911132020-04-06 20:03:51 -07008007 if (is_vm_hugetlb_page(vma))
Peter Zijlstra0b3589b2017-01-26 23:15:08 +01008008 flags |= MAP_HUGETLB;
8009
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008010 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02008011 struct inode *inode;
8012 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02008013
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008014 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008015 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008016 name = "//enomem";
8017 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008018 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008019 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02008020 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008021 * need to add enough zero bytes after the string to handle
8022 * the 64bit alignment we do later.
8023 */
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +02008024 name = file_path(file, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008025 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008026 name = "//toolong";
8027 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008028 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02008029 inode = file_inode(vma->vm_file);
8030 dev = inode->i_sb->s_dev;
8031 ino = inode->i_ino;
8032 gen = inode->i_generation;
8033 maj = MAJOR(dev);
8034 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008036 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008037 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02008038 if (vma->vm_ops && vma->vm_ops->name) {
8039 name = (char *) vma->vm_ops->name(vma);
8040 if (name)
8041 goto cpy_name;
8042 }
8043
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008044 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008045 if (name)
8046 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008047
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02008048 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008049 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008050 name = "[heap]";
8051 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02008052 }
8053 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008054 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008055 name = "[stack]";
8056 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008057 }
8058
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008059 name = "//anon";
8060 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008061 }
8062
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02008063cpy_name:
8064 strlcpy(tmp, name, sizeof(tmp));
8065 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008066got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02008067 /*
8068 * Since our buffer works in 8 byte units we need to align our string
8069 * size to a multiple of 8. However, we must guarantee the tail end is
8070 * zero'd out to avoid leaking random bits to userspace.
8071 */
8072 size = strlen(name)+1;
8073 while (!IS_ALIGNED(size, sizeof(u64)))
8074 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008075
8076 mmap_event->file_name = name;
8077 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02008078 mmap_event->maj = maj;
8079 mmap_event->min = min;
8080 mmap_event->ino = ino;
8081 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008082 mmap_event->prot = prot;
8083 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008084
Stephane Eranian2fe85422013-01-24 16:10:39 +01008085 if (!(vma->vm_flags & VM_EXEC))
8086 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
8087
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008088 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
8089
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008090 perf_iterate_sb(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02008091 mmap_event,
8092 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008093
8094 kfree(buf);
8095}
8096
Alexander Shishkin375637b2016-04-27 18:44:46 +03008097/*
Alexander Shishkin375637b2016-04-27 18:44:46 +03008098 * Check whether inode and address range match filter criteria.
8099 */
8100static bool perf_addr_filter_match(struct perf_addr_filter *filter,
8101 struct file *file, unsigned long offset,
8102 unsigned long size)
8103{
Mathieu Poirier7f635ff2018-07-16 17:13:51 -06008104 /* d_inode(NULL) won't be equal to any mapped user-space file */
8105 if (!filter->path.dentry)
8106 return false;
8107
Song Liu9511bce2018-04-17 23:29:07 -07008108 if (d_inode(filter->path.dentry) != file_inode(file))
Alexander Shishkin375637b2016-04-27 18:44:46 +03008109 return false;
8110
8111 if (filter->offset > offset + size)
8112 return false;
8113
8114 if (filter->offset + filter->size < offset)
8115 return false;
8116
8117 return true;
8118}
8119
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008120static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter,
8121 struct vm_area_struct *vma,
8122 struct perf_addr_filter_range *fr)
8123{
8124 unsigned long vma_size = vma->vm_end - vma->vm_start;
8125 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
8126 struct file *file = vma->vm_file;
8127
8128 if (!perf_addr_filter_match(filter, file, off, vma_size))
8129 return false;
8130
8131 if (filter->offset < off) {
8132 fr->start = vma->vm_start;
8133 fr->size = min(vma_size, filter->size - (off - filter->offset));
8134 } else {
8135 fr->start = vma->vm_start + filter->offset - off;
8136 fr->size = min(vma->vm_end - fr->start, filter->size);
8137 }
8138
8139 return true;
8140}
8141
Alexander Shishkin375637b2016-04-27 18:44:46 +03008142static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
8143{
8144 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
8145 struct vm_area_struct *vma = data;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008146 struct perf_addr_filter *filter;
8147 unsigned int restart = 0, count = 0;
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008148 unsigned long flags;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008149
8150 if (!has_addr_filter(event))
8151 return;
8152
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008153 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03008154 return;
8155
8156 raw_spin_lock_irqsave(&ifh->lock, flags);
8157 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02008158 if (perf_addr_filter_vma_adjust(filter, vma,
8159 &event->addr_filter_ranges[count]))
Alexander Shishkin375637b2016-04-27 18:44:46 +03008160 restart++;
Alexander Shishkin375637b2016-04-27 18:44:46 +03008161
8162 count++;
8163 }
8164
8165 if (restart)
8166 event->addr_filters_gen++;
8167 raw_spin_unlock_irqrestore(&ifh->lock, flags);
8168
8169 if (restart)
Alexander Shishkin767ae082016-09-06 16:23:49 +03008170 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008171}
8172
8173/*
8174 * Adjust all task's events' filters to the new vma
8175 */
8176static void perf_addr_filters_adjust(struct vm_area_struct *vma)
8177{
8178 struct perf_event_context *ctx;
8179 int ctxn;
8180
Mathieu Poirier12b40a22016-07-18 10:43:06 -06008181 /*
8182 * Data tracing isn't supported yet and as such there is no need
8183 * to keep track of anything that isn't related to executable code:
8184 */
8185 if (!(vma->vm_flags & VM_EXEC))
8186 return;
8187
Alexander Shishkin375637b2016-04-27 18:44:46 +03008188 rcu_read_lock();
8189 for_each_task_context_nr(ctxn) {
8190 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
8191 if (!ctx)
8192 continue;
8193
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008194 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true);
Alexander Shishkin375637b2016-04-27 18:44:46 +03008195 }
8196 rcu_read_unlock();
8197}
8198
Eric B Munson3af9e852010-05-18 15:30:49 +01008199void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008200{
8201 struct perf_mmap_event mmap_event;
8202
8203 if (!atomic_read(&nr_mmap_events))
8204 return;
8205
8206 mmap_event = (struct perf_mmap_event){
8207 .vma = vma,
8208 /* .file_name */
8209 /* .file_size */
8210 .event_id = {
8211 .header = {
8212 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08008213 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008214 /* .size */
8215 },
8216 /* .pid */
8217 /* .tid */
8218 .start = vma->vm_start,
8219 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01008220 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008221 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02008222 /* .maj (attr_mmap2 only) */
8223 /* .min (attr_mmap2 only) */
8224 /* .ino (attr_mmap2 only) */
8225 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04008226 /* .prot (attr_mmap2 only) */
8227 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008228 };
8229
Alexander Shishkin375637b2016-04-27 18:44:46 +03008230 perf_addr_filters_adjust(vma);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008231 perf_event_mmap_event(&mmap_event);
8232}
8233
Alexander Shishkin68db7e92015-01-14 14:18:15 +02008234void perf_event_aux_event(struct perf_event *event, unsigned long head,
8235 unsigned long size, u64 flags)
8236{
8237 struct perf_output_handle handle;
8238 struct perf_sample_data sample;
8239 struct perf_aux_event {
8240 struct perf_event_header header;
8241 u64 offset;
8242 u64 size;
8243 u64 flags;
8244 } rec = {
8245 .header = {
8246 .type = PERF_RECORD_AUX,
8247 .misc = 0,
8248 .size = sizeof(rec),
8249 },
8250 .offset = head,
8251 .size = size,
8252 .flags = flags,
8253 };
8254 int ret;
8255
8256 perf_event_header__init_id(&rec.header, &sample, event);
8257 ret = perf_output_begin(&handle, event, rec.header.size);
8258
8259 if (ret)
8260 return;
8261
8262 perf_output_put(&handle, rec);
8263 perf_event__output_id_sample(event, &handle, &sample);
8264
8265 perf_output_end(&handle);
8266}
8267
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008268/*
Kan Liangf38b0db2015-05-10 15:13:14 -04008269 * Lost/dropped samples logging
8270 */
8271void perf_log_lost_samples(struct perf_event *event, u64 lost)
8272{
8273 struct perf_output_handle handle;
8274 struct perf_sample_data sample;
8275 int ret;
8276
8277 struct {
8278 struct perf_event_header header;
8279 u64 lost;
8280 } lost_samples_event = {
8281 .header = {
8282 .type = PERF_RECORD_LOST_SAMPLES,
8283 .misc = 0,
8284 .size = sizeof(lost_samples_event),
8285 },
8286 .lost = lost,
8287 };
8288
8289 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
8290
8291 ret = perf_output_begin(&handle, event,
8292 lost_samples_event.header.size);
8293 if (ret)
8294 return;
8295
8296 perf_output_put(&handle, lost_samples_event);
8297 perf_event__output_id_sample(event, &handle, &sample);
8298 perf_output_end(&handle);
8299}
8300
8301/*
Adrian Hunter45ac1402015-07-21 12:44:02 +03008302 * context_switch tracking
8303 */
8304
8305struct perf_switch_event {
8306 struct task_struct *task;
8307 struct task_struct *next_prev;
8308
8309 struct {
8310 struct perf_event_header header;
8311 u32 next_prev_pid;
8312 u32 next_prev_tid;
8313 } event_id;
8314};
8315
8316static int perf_event_switch_match(struct perf_event *event)
8317{
8318 return event->attr.context_switch;
8319}
8320
8321static void perf_event_switch_output(struct perf_event *event, void *data)
8322{
8323 struct perf_switch_event *se = data;
8324 struct perf_output_handle handle;
8325 struct perf_sample_data sample;
8326 int ret;
8327
8328 if (!perf_event_switch_match(event))
8329 return;
8330
8331 /* Only CPU-wide events are allowed to see next/prev pid/tid */
8332 if (event->ctx->task) {
8333 se->event_id.header.type = PERF_RECORD_SWITCH;
8334 se->event_id.header.size = sizeof(se->event_id.header);
8335 } else {
8336 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
8337 se->event_id.header.size = sizeof(se->event_id);
8338 se->event_id.next_prev_pid =
8339 perf_event_pid(event, se->next_prev);
8340 se->event_id.next_prev_tid =
8341 perf_event_tid(event, se->next_prev);
8342 }
8343
8344 perf_event_header__init_id(&se->event_id.header, &sample, event);
8345
8346 ret = perf_output_begin(&handle, event, se->event_id.header.size);
8347 if (ret)
8348 return;
8349
8350 if (event->ctx->task)
8351 perf_output_put(&handle, se->event_id.header);
8352 else
8353 perf_output_put(&handle, se->event_id);
8354
8355 perf_event__output_id_sample(event, &handle, &sample);
8356
8357 perf_output_end(&handle);
8358}
8359
8360static void perf_event_switch(struct task_struct *task,
8361 struct task_struct *next_prev, bool sched_in)
8362{
8363 struct perf_switch_event switch_event;
8364
8365 /* N.B. caller checks nr_switch_events != 0 */
8366
8367 switch_event = (struct perf_switch_event){
8368 .task = task,
8369 .next_prev = next_prev,
8370 .event_id = {
8371 .header = {
8372 /* .type */
8373 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
8374 /* .size */
8375 },
8376 /* .next_prev_pid */
8377 /* .next_prev_tid */
8378 },
8379 };
8380
Alexey Budankov101592b2018-04-09 10:25:32 +03008381 if (!sched_in && task->state == TASK_RUNNING)
8382 switch_event.event_id.header.misc |=
8383 PERF_RECORD_MISC_SWITCH_OUT_PREEMPT;
8384
Peter Zijlstraaab5b712016-05-12 17:26:46 +02008385 perf_iterate_sb(perf_event_switch_output,
Adrian Hunter45ac1402015-07-21 12:44:02 +03008386 &switch_event,
8387 NULL);
8388}
8389
8390/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008391 * IRQ throttle logging
8392 */
8393
8394static void perf_log_throttle(struct perf_event *event, int enable)
8395{
8396 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008397 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008398 int ret;
8399
8400 struct {
8401 struct perf_event_header header;
8402 u64 time;
8403 u64 id;
8404 u64 stream_id;
8405 } throttle_event = {
8406 .header = {
8407 .type = PERF_RECORD_THROTTLE,
8408 .misc = 0,
8409 .size = sizeof(throttle_event),
8410 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01008411 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008412 .id = primary_event_id(event),
8413 .stream_id = event->id,
8414 };
8415
8416 if (enable)
8417 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
8418
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008419 perf_event_header__init_id(&throttle_event.header, &sample, event);
8420
8421 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02008422 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008423 if (ret)
8424 return;
8425
8426 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02008427 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008428 perf_output_end(&handle);
8429}
8430
Song Liu76193a92019-01-17 08:15:13 -08008431/*
8432 * ksymbol register/unregister tracking
8433 */
8434
8435struct perf_ksymbol_event {
8436 const char *name;
8437 int name_len;
8438 struct {
8439 struct perf_event_header header;
8440 u64 addr;
8441 u32 len;
8442 u16 ksym_type;
8443 u16 flags;
8444 } event_id;
8445};
8446
8447static int perf_event_ksymbol_match(struct perf_event *event)
8448{
8449 return event->attr.ksymbol;
8450}
8451
8452static void perf_event_ksymbol_output(struct perf_event *event, void *data)
8453{
8454 struct perf_ksymbol_event *ksymbol_event = data;
8455 struct perf_output_handle handle;
8456 struct perf_sample_data sample;
8457 int ret;
8458
8459 if (!perf_event_ksymbol_match(event))
8460 return;
8461
8462 perf_event_header__init_id(&ksymbol_event->event_id.header,
8463 &sample, event);
8464 ret = perf_output_begin(&handle, event,
8465 ksymbol_event->event_id.header.size);
8466 if (ret)
8467 return;
8468
8469 perf_output_put(&handle, ksymbol_event->event_id);
8470 __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len);
8471 perf_event__output_id_sample(event, &handle, &sample);
8472
8473 perf_output_end(&handle);
8474}
8475
8476void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister,
8477 const char *sym)
8478{
8479 struct perf_ksymbol_event ksymbol_event;
8480 char name[KSYM_NAME_LEN];
8481 u16 flags = 0;
8482 int name_len;
8483
8484 if (!atomic_read(&nr_ksymbol_events))
8485 return;
8486
8487 if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX ||
8488 ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN)
8489 goto err;
8490
8491 strlcpy(name, sym, KSYM_NAME_LEN);
8492 name_len = strlen(name) + 1;
8493 while (!IS_ALIGNED(name_len, sizeof(u64)))
8494 name[name_len++] = '\0';
8495 BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64));
8496
8497 if (unregister)
8498 flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER;
8499
8500 ksymbol_event = (struct perf_ksymbol_event){
8501 .name = name,
8502 .name_len = name_len,
8503 .event_id = {
8504 .header = {
8505 .type = PERF_RECORD_KSYMBOL,
8506 .size = sizeof(ksymbol_event.event_id) +
8507 name_len,
8508 },
8509 .addr = addr,
8510 .len = len,
8511 .ksym_type = ksym_type,
8512 .flags = flags,
8513 },
8514 };
8515
8516 perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL);
8517 return;
8518err:
8519 WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type);
8520}
8521
Song Liu6ee52e22019-01-17 08:15:15 -08008522/*
8523 * bpf program load/unload tracking
8524 */
8525
8526struct perf_bpf_event {
8527 struct bpf_prog *prog;
8528 struct {
8529 struct perf_event_header header;
8530 u16 type;
8531 u16 flags;
8532 u32 id;
8533 u8 tag[BPF_TAG_SIZE];
8534 } event_id;
8535};
8536
8537static int perf_event_bpf_match(struct perf_event *event)
8538{
8539 return event->attr.bpf_event;
8540}
8541
8542static void perf_event_bpf_output(struct perf_event *event, void *data)
8543{
8544 struct perf_bpf_event *bpf_event = data;
8545 struct perf_output_handle handle;
8546 struct perf_sample_data sample;
8547 int ret;
8548
8549 if (!perf_event_bpf_match(event))
8550 return;
8551
8552 perf_event_header__init_id(&bpf_event->event_id.header,
8553 &sample, event);
8554 ret = perf_output_begin(&handle, event,
8555 bpf_event->event_id.header.size);
8556 if (ret)
8557 return;
8558
8559 perf_output_put(&handle, bpf_event->event_id);
8560 perf_event__output_id_sample(event, &handle, &sample);
8561
8562 perf_output_end(&handle);
8563}
8564
8565static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog,
8566 enum perf_bpf_event_type type)
8567{
8568 bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD;
Song Liu6ee52e22019-01-17 08:15:15 -08008569 int i;
8570
8571 if (prog->aux->func_cnt == 0) {
Song Liu6ee52e22019-01-17 08:15:15 -08008572 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF,
8573 (u64)(unsigned long)prog->bpf_func,
Jiri Olsabfea9a82020-03-12 20:55:59 +01008574 prog->jited_len, unregister,
8575 prog->aux->ksym.name);
Song Liu6ee52e22019-01-17 08:15:15 -08008576 } else {
8577 for (i = 0; i < prog->aux->func_cnt; i++) {
8578 struct bpf_prog *subprog = prog->aux->func[i];
8579
Song Liu6ee52e22019-01-17 08:15:15 -08008580 perf_event_ksymbol(
8581 PERF_RECORD_KSYMBOL_TYPE_BPF,
8582 (u64)(unsigned long)subprog->bpf_func,
Jiri Olsabfea9a82020-03-12 20:55:59 +01008583 subprog->jited_len, unregister,
8584 prog->aux->ksym.name);
Song Liu6ee52e22019-01-17 08:15:15 -08008585 }
8586 }
8587}
8588
8589void perf_event_bpf_event(struct bpf_prog *prog,
8590 enum perf_bpf_event_type type,
8591 u16 flags)
8592{
8593 struct perf_bpf_event bpf_event;
8594
8595 if (type <= PERF_BPF_EVENT_UNKNOWN ||
8596 type >= PERF_BPF_EVENT_MAX)
8597 return;
8598
8599 switch (type) {
8600 case PERF_BPF_EVENT_PROG_LOAD:
8601 case PERF_BPF_EVENT_PROG_UNLOAD:
8602 if (atomic_read(&nr_ksymbol_events))
8603 perf_event_bpf_emit_ksymbols(prog, type);
8604 break;
8605 default:
8606 break;
8607 }
8608
8609 if (!atomic_read(&nr_bpf_events))
8610 return;
8611
8612 bpf_event = (struct perf_bpf_event){
8613 .prog = prog,
8614 .event_id = {
8615 .header = {
8616 .type = PERF_RECORD_BPF_EVENT,
8617 .size = sizeof(bpf_event.event_id),
8618 },
8619 .type = type,
8620 .flags = flags,
8621 .id = prog->aux->id,
8622 },
8623 };
8624
8625 BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64));
8626
8627 memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE);
8628 perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL);
8629}
8630
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008631void perf_event_itrace_started(struct perf_event *event)
8632{
8633 event->attach_state |= PERF_ATTACH_ITRACE;
8634}
8635
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008636static void perf_log_itrace_start(struct perf_event *event)
8637{
8638 struct perf_output_handle handle;
8639 struct perf_sample_data sample;
8640 struct perf_aux_event {
8641 struct perf_event_header header;
8642 u32 pid;
8643 u32 tid;
8644 } rec;
8645 int ret;
8646
8647 if (event->parent)
8648 event = event->parent;
8649
8650 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
Alexander Shishkin8d4e6c42017-03-30 18:39:56 +03008651 event->attach_state & PERF_ATTACH_ITRACE)
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008652 return;
8653
Alexander Shishkinec0d7722015-01-14 14:18:23 +02008654 rec.header.type = PERF_RECORD_ITRACE_START;
8655 rec.header.misc = 0;
8656 rec.header.size = sizeof(rec);
8657 rec.pid = perf_event_pid(event, current);
8658 rec.tid = perf_event_tid(event, current);
8659
8660 perf_event_header__init_id(&rec.header, &sample, event);
8661 ret = perf_output_begin(&handle, event, rec.header.size);
8662
8663 if (ret)
8664 return;
8665
8666 perf_output_put(&handle, rec);
8667 perf_event__output_id_sample(event, &handle, &sample);
8668
8669 perf_output_end(&handle);
8670}
8671
Jiri Olsa475113d2016-12-28 14:31:03 +01008672static int
8673__perf_event_account_interrupt(struct perf_event *event, int throttle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008674{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008675 struct hw_perf_event *hwc = &event->hw;
8676 int ret = 0;
Jiri Olsa475113d2016-12-28 14:31:03 +01008677 u64 seq;
Peter Zijlstra96398822010-11-24 18:55:29 +01008678
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008679 seq = __this_cpu_read(perf_throttled_seq);
8680 if (seq != hwc->interrupts_seq) {
8681 hwc->interrupts_seq = seq;
8682 hwc->interrupts = 1;
8683 } else {
8684 hwc->interrupts++;
8685 if (unlikely(throttle
8686 && hwc->interrupts >= max_samples_per_tick)) {
8687 __this_cpu_inc(perf_throttled_count);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +02008688 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
Peter Zijlstra163ec432011-02-16 11:22:34 +01008689 hwc->interrupts = MAX_INTERRUPTS;
8690 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008691 ret = 1;
8692 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01008693 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008694
8695 if (event->attr.freq) {
8696 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01008697 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008698
Peter Zijlstraabd50712010-01-26 18:50:16 +01008699 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008700
Peter Zijlstraabd50712010-01-26 18:50:16 +01008701 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01008702 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008703 }
8704
Jiri Olsa475113d2016-12-28 14:31:03 +01008705 return ret;
8706}
8707
8708int perf_event_account_interrupt(struct perf_event *event)
8709{
8710 return __perf_event_account_interrupt(event, 1);
8711}
8712
8713/*
8714 * Generic event overflow handling, sampling.
8715 */
8716
8717static int __perf_event_overflow(struct perf_event *event,
8718 int throttle, struct perf_sample_data *data,
8719 struct pt_regs *regs)
8720{
8721 int events = atomic_read(&event->event_limit);
8722 int ret = 0;
8723
8724 /*
8725 * Non-sampling counters might still use the PMI to fold short
8726 * hardware counters, ignore those.
8727 */
8728 if (unlikely(!is_sampling_event(event)))
8729 return 0;
8730
8731 ret = __perf_event_account_interrupt(event, throttle);
8732
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008733 /*
8734 * XXX event_limit might not quite work as expected on inherited
8735 * events
8736 */
8737
8738 event->pending_kill = POLL_IN;
8739 if (events && atomic_dec_and_test(&event->event_limit)) {
8740 ret = 1;
8741 event->pending_kill = POLL_HUP;
Jiri Olsa5aab90c2016-10-26 11:48:24 +02008742
8743 perf_event_disable_inatomic(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008744 }
8745
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07008746 READ_ONCE(event->overflow_handler)(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01008747
Peter Zijlstrafed66e2cd2015-06-11 10:32:01 +02008748 if (*perf_event_fasync(event) && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008749 event->pending_wakeup = 1;
8750 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02008751 }
8752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008753 return ret;
8754}
8755
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008756int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008757 struct perf_sample_data *data,
8758 struct pt_regs *regs)
8759{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008760 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008761}
8762
8763/*
8764 * Generic software event infrastructure
8765 */
8766
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008767struct swevent_htable {
8768 struct swevent_hlist *swevent_hlist;
8769 struct mutex hlist_mutex;
8770 int hlist_refcount;
8771
8772 /* Recursion avoidance in each contexts */
8773 int recursion[PERF_NR_CONTEXTS];
8774};
8775
8776static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
8777
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008778/*
8779 * We directly increment event->count and keep a second value in
8780 * event->hw.period_left to count intervals. This period event
8781 * is kept in the range [-sample_period, 0] so that we can use the
8782 * sign as trigger.
8783 */
8784
Jiri Olsaab573842013-05-01 17:25:44 +02008785u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008786{
8787 struct hw_perf_event *hwc = &event->hw;
8788 u64 period = hwc->last_period;
8789 u64 nr, offset;
8790 s64 old, val;
8791
8792 hwc->last_period = hwc->sample_period;
8793
8794again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02008795 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008796 if (val < 0)
8797 return 0;
8798
8799 nr = div64_u64(period + val, period);
8800 offset = nr * period;
8801 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02008802 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008803 goto again;
8804
8805 return nr;
8806}
8807
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008808static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008809 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008810 struct pt_regs *regs)
8811{
8812 struct hw_perf_event *hwc = &event->hw;
8813 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008814
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008815 if (!overflow)
8816 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008817
8818 if (hwc->interrupts == MAX_INTERRUPTS)
8819 return;
8820
8821 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008822 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008823 data, regs)) {
8824 /*
8825 * We inhibit the overflow from happening when
8826 * hwc->interrupts == MAX_INTERRUPTS.
8827 */
8828 break;
8829 }
8830 throttle = 1;
8831 }
8832}
8833
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008834static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008835 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008836 struct pt_regs *regs)
8837{
8838 struct hw_perf_event *hwc = &event->hw;
8839
Peter Zijlstrae7850592010-05-21 14:43:08 +02008840 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008841
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008842 if (!regs)
8843 return;
8844
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01008845 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008846 return;
8847
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03008848 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
8849 data->period = nr;
8850 return perf_swevent_overflow(event, 1, data, regs);
8851 } else
8852 data->period = event->hw.last_period;
8853
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008854 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008855 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008856
Peter Zijlstrae7850592010-05-21 14:43:08 +02008857 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01008858 return;
8859
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008860 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008861}
8862
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008863static int perf_exclude_event(struct perf_event *event,
8864 struct pt_regs *regs)
8865{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008866 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01008867 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02008868
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008869 if (regs) {
8870 if (event->attr.exclude_user && user_mode(regs))
8871 return 1;
8872
8873 if (event->attr.exclude_kernel && !user_mode(regs))
8874 return 1;
8875 }
8876
8877 return 0;
8878}
8879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008880static int perf_swevent_match(struct perf_event *event,
8881 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08008882 u32 event_id,
8883 struct perf_sample_data *data,
8884 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008885{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008886 if (event->attr.type != type)
8887 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008889 if (event->attr.config != event_id)
8890 return 0;
8891
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01008892 if (perf_exclude_event(event, regs))
8893 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008894
8895 return 1;
8896}
8897
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008898static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008899{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008900 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008901
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008902 return hash_64(val, SWEVENT_HLIST_BITS);
8903}
8904
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008905static inline struct hlist_head *
8906__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008907{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008908 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008909
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008910 return &hlist->heads[hash];
8911}
8912
8913/* For the read side: events when they trigger */
8914static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008915find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008916{
8917 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008918
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008919 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008920 if (!hlist)
8921 return NULL;
8922
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008923 return __find_swevent_head(hlist, type, event_id);
8924}
8925
8926/* For the event head insertion and removal in the hlist */
8927static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008928find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008929{
8930 struct swevent_hlist *hlist;
8931 u32 event_id = event->attr.config;
8932 u64 type = event->attr.type;
8933
8934 /*
8935 * Event scheduling is always serialized against hlist allocation
8936 * and release. Which makes the protected version suitable here.
8937 * The context lock guarantees that.
8938 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008939 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02008940 lockdep_is_held(&event->ctx->lock));
8941 if (!hlist)
8942 return NULL;
8943
8944 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008945}
8946
8947static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008948 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008949 struct perf_sample_data *data,
8950 struct pt_regs *regs)
8951{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008952 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008953 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008954 struct hlist_head *head;
8955
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008956 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008957 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008958 if (!head)
8959 goto end;
8960
Sasha Levinb67bfe02013-02-27 17:06:00 -08008961 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08008962 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02008963 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008964 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008965end:
8966 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008967}
8968
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008969DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
8970
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01008971int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008972{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008973 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01008974
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008975 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008976}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01008977EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008978
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07008979void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008980{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05008981 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02008982
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008983 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01008984}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008985
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008986void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008987{
Ingo Molnara4234bf2009-11-23 10:57:59 +01008988 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01008989
8990 if (WARN_ON_ONCE(!regs))
8991 return;
8992
8993 perf_sample_data_init(&data, addr, 0);
8994 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
8995}
8996
8997void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
8998{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01008999 int rctx;
9000
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009001 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009002 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009003 if (unlikely(rctx < 0))
9004 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009005
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009006 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01009007
9008 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01009009fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009010 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009011}
9012
9013static void perf_swevent_read(struct perf_event *event)
9014{
9015}
9016
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009017static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009018{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05009019 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009020 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009021 struct hlist_head *head;
9022
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01009023 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009024 hwc->last_period = hwc->sample_period;
9025 perf_swevent_set_period(event);
9026 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009027
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009028 hwc->state = !(flags & PERF_EF_START);
9029
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009030 head = find_swevent_head(swhash, event);
Peter Zijlstra12ca6ad2015-12-15 13:49:05 +01009031 if (WARN_ON_ONCE(!head))
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009032 return -EINVAL;
9033
9034 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08009035 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009036
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009037 return 0;
9038}
9039
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009040static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009041{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009042 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009043}
9044
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009045static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009046{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009047 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009048}
9049
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009050static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009051{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009052 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02009053}
9054
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009055/* Deref the hlist from the update side */
9056static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009057swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009058{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009059 return rcu_dereference_protected(swhash->swevent_hlist,
9060 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009061}
9062
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009063static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009064{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009065 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009066
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02009067 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009068 return;
9069
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03009070 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08009071 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009072}
9073
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009074static void swevent_hlist_put_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009075{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009076 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009077
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009078 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009079
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009080 if (!--swhash->hlist_refcount)
9081 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009082
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009083 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009084}
9085
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009086static void swevent_hlist_put(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009087{
9088 int cpu;
9089
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009090 for_each_possible_cpu(cpu)
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009091 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009092}
9093
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009094static int swevent_hlist_get_cpu(int cpu)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009095{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009096 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009097 int err = 0;
9098
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009099 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009100 if (!swevent_hlist_deref(swhash) &&
9101 cpumask_test_cpu(cpu, perf_online_mask)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009102 struct swevent_hlist *hlist;
9103
9104 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
9105 if (!hlist) {
9106 err = -ENOMEM;
9107 goto exit;
9108 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009109 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009110 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009111 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02009112exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02009113 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009114
9115 return err;
9116}
9117
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009118static int swevent_hlist_get(void)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009119{
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009120 int err, cpu, failed_cpu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009121
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009122 mutex_lock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009123 for_each_possible_cpu(cpu) {
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009124 err = swevent_hlist_get_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009125 if (err) {
9126 failed_cpu = cpu;
9127 goto fail;
9128 }
9129 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009130 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009131 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02009132fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009133 for_each_possible_cpu(cpu) {
9134 if (cpu == failed_cpu)
9135 break;
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009136 swevent_hlist_put_cpu(cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009137 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +02009138 mutex_unlock(&pmus_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009139 return err;
9140}
9141
Ingo Molnarc5905af2012-02-24 08:31:31 +01009142struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009143
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009144static void sw_perf_event_destroy(struct perf_event *event)
9145{
9146 u64 event_id = event->attr.config;
9147
9148 WARN_ON(event->parent);
9149
Ingo Molnarc5905af2012-02-24 08:31:31 +01009150 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009151 swevent_hlist_put();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009152}
9153
9154static int perf_swevent_init(struct perf_event *event)
9155{
Tommi Rantala8176cce2013-04-13 22:49:14 +03009156 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009157
9158 if (event->attr.type != PERF_TYPE_SOFTWARE)
9159 return -ENOENT;
9160
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009161 /*
9162 * no branch sampling for software events
9163 */
9164 if (has_branch_stack(event))
9165 return -EOPNOTSUPP;
9166
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009167 switch (event_id) {
9168 case PERF_COUNT_SW_CPU_CLOCK:
9169 case PERF_COUNT_SW_TASK_CLOCK:
9170 return -ENOENT;
9171
9172 default:
9173 break;
9174 }
9175
Dan Carpenterce677832010-10-24 21:50:42 +02009176 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009177 return -ENOENT;
9178
9179 if (!event->parent) {
9180 int err;
9181
Thomas Gleixner3b364d72016-02-09 20:11:40 +00009182 err = swevent_hlist_get();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009183 if (err)
9184 return err;
9185
Ingo Molnarc5905af2012-02-24 08:31:31 +01009186 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009187 event->destroy = sw_perf_event_destroy;
9188 }
9189
9190 return 0;
9191}
9192
9193static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009194 .task_ctx_nr = perf_sw_context,
9195
Peter Zijlstra34f43922015-02-20 14:05:38 +01009196 .capabilities = PERF_PMU_CAP_NO_NMI,
9197
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009198 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009199 .add = perf_swevent_add,
9200 .del = perf_swevent_del,
9201 .start = perf_swevent_start,
9202 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009203 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009204};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009205
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009206#ifdef CONFIG_EVENT_TRACING
9207
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009208static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009209 struct perf_sample_data *data)
9210{
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02009211 void *record = data->raw->frag.data;
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009212
Peter Zijlstrab71b4372015-11-02 10:50:51 +01009213 /* only top level events have filters set */
9214 if (event->parent)
9215 event = event->parent;
9216
Frederic Weisbecker95476b62010-04-14 23:42:18 +02009217 if (likely(!event->filter) || filter_match_preds(event->filter, record))
9218 return 1;
9219 return 0;
9220}
9221
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009222static int perf_tp_event_match(struct perf_event *event,
9223 struct perf_sample_data *data,
9224 struct pt_regs *regs)
9225{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01009226 if (event->hw.state & PERF_HES_STOPPED)
9227 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02009228 /*
Song Liu9fd2e482019-05-07 09:15:45 -07009229 * If exclude_kernel, only trace user-space tracepoints (uprobes)
Peter Zijlstra580d6072010-05-20 20:54:31 +02009230 */
Song Liu9fd2e482019-05-07 09:15:45 -07009231 if (event->attr.exclude_kernel && !user_mode(regs))
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009232 return 0;
9233
9234 if (!perf_tp_filter_match(event, data))
9235 return 0;
9236
9237 return 1;
9238}
9239
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009240void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
9241 struct trace_event_call *call, u64 count,
9242 struct pt_regs *regs, struct hlist_head *head,
9243 struct task_struct *task)
9244{
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009245 if (bpf_prog_array_valid(call)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009246 *(struct pt_regs **)raw_data = regs;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009247 if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) {
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009248 perf_swevent_put_recursion_context(rctx);
9249 return;
9250 }
9251 }
9252 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009253 rctx, task);
Alexei Starovoitov85b67bc2016-04-18 20:11:50 -07009254}
9255EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
9256
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009257void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009258 struct pt_regs *regs, struct hlist_head *head, int rctx,
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009259 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009260{
9261 struct perf_sample_data data;
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009262 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009263
9264 struct perf_raw_record raw = {
Daniel Borkmann7e3f9772016-07-14 18:08:03 +02009265 .frag = {
9266 .size = entry_size,
9267 .data = record,
9268 },
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009269 };
9270
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009271 perf_sample_data_init(&data, 0, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009272 data.raw = &raw;
9273
Alexei Starovoitov1e1dcd92016-04-06 18:43:24 -07009274 perf_trace_buf_update(record, event_type);
9275
Peter Zijlstra8fd0fbb2017-10-11 09:45:29 +02009276 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009277 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009278 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009279 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02009280
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009281 /*
9282 * If we got specified a target task, also iterate its context and
9283 * deliver this event there too.
9284 */
9285 if (task && task != current) {
9286 struct perf_event_context *ctx;
9287 struct trace_entry *entry = record;
9288
9289 rcu_read_lock();
9290 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
9291 if (!ctx)
9292 goto unlock;
9293
9294 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Jiri Olsacd6fb6772018-09-23 18:13:43 +02009295 if (event->cpu != smp_processor_id())
9296 continue;
Andrew Vagine6dab5f2012-07-11 18:14:58 +04009297 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9298 continue;
9299 if (event->attr.config != entry->type)
9300 continue;
9301 if (perf_tp_event_match(event, &data, regs))
9302 perf_swevent_event(event, count, &data, regs);
9303 }
9304unlock:
9305 rcu_read_unlock();
9306 }
9307
Peter Zijlstraecc55f82010-05-21 15:11:34 +02009308 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009309}
9310EXPORT_SYMBOL_GPL(perf_tp_event);
9311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009312static void tp_perf_event_destroy(struct perf_event *event)
9313{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009314 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009315}
9316
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009317static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009318{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02009319 int err;
9320
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009321 if (event->attr.type != PERF_TYPE_TRACEPOINT)
9322 return -ENOENT;
9323
Stephane Eranian2481c5f2012-02-09 23:20:59 +01009324 /*
9325 * no branch sampling for tracepoint events
9326 */
9327 if (has_branch_stack(event))
9328 return -EOPNOTSUPP;
9329
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02009330 err = perf_trace_init(event);
9331 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009332 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009333
9334 event->destroy = tp_perf_event_destroy;
9335
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009336 return 0;
9337}
9338
9339static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02009340 .task_ctx_nr = perf_sw_context,
9341
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009342 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009343 .add = perf_trace_add,
9344 .del = perf_trace_del,
9345 .start = perf_swevent_start,
9346 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009347 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009348};
9349
Song Liu33ea4b22017-12-06 14:45:16 -08009350#if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS)
Song Liue12f03d2017-12-06 14:45:15 -08009351/*
9352 * Flags in config, used by dynamic PMU kprobe and uprobe
9353 * The flags should match following PMU_FORMAT_ATTR().
9354 *
9355 * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe
9356 * if not set, create kprobe/uprobe
Song Liua6ca88b2018-10-01 22:36:36 -07009357 *
9358 * The following values specify a reference counter (or semaphore in the
9359 * terminology of tools like dtrace, systemtap, etc.) Userspace Statically
9360 * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset.
9361 *
9362 * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset
9363 * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left
Song Liue12f03d2017-12-06 14:45:15 -08009364 */
9365enum perf_probe_config {
9366 PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */
Song Liua6ca88b2018-10-01 22:36:36 -07009367 PERF_UPROBE_REF_CTR_OFFSET_BITS = 32,
9368 PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS,
Song Liue12f03d2017-12-06 14:45:15 -08009369};
9370
9371PMU_FORMAT_ATTR(retprobe, "config:0");
Song Liua6ca88b2018-10-01 22:36:36 -07009372#endif
Song Liue12f03d2017-12-06 14:45:15 -08009373
Song Liua6ca88b2018-10-01 22:36:36 -07009374#ifdef CONFIG_KPROBE_EVENTS
9375static struct attribute *kprobe_attrs[] = {
Song Liue12f03d2017-12-06 14:45:15 -08009376 &format_attr_retprobe.attr,
9377 NULL,
9378};
9379
Song Liua6ca88b2018-10-01 22:36:36 -07009380static struct attribute_group kprobe_format_group = {
Song Liue12f03d2017-12-06 14:45:15 -08009381 .name = "format",
Song Liua6ca88b2018-10-01 22:36:36 -07009382 .attrs = kprobe_attrs,
Song Liue12f03d2017-12-06 14:45:15 -08009383};
9384
Song Liua6ca88b2018-10-01 22:36:36 -07009385static const struct attribute_group *kprobe_attr_groups[] = {
9386 &kprobe_format_group,
Song Liue12f03d2017-12-06 14:45:15 -08009387 NULL,
9388};
9389
9390static int perf_kprobe_event_init(struct perf_event *event);
9391static struct pmu perf_kprobe = {
9392 .task_ctx_nr = perf_sw_context,
9393 .event_init = perf_kprobe_event_init,
9394 .add = perf_trace_add,
9395 .del = perf_trace_del,
9396 .start = perf_swevent_start,
9397 .stop = perf_swevent_stop,
9398 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009399 .attr_groups = kprobe_attr_groups,
Song Liue12f03d2017-12-06 14:45:15 -08009400};
9401
9402static int perf_kprobe_event_init(struct perf_event *event)
9403{
9404 int err;
9405 bool is_retprobe;
9406
9407 if (event->attr.type != perf_kprobe.type)
9408 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009409
Alexey Budankovc9e09242020-04-02 11:47:01 +03009410 if (!perfmon_capable())
Song Liu32e6e962018-04-11 18:02:37 +00009411 return -EACCES;
9412
Song Liue12f03d2017-12-06 14:45:15 -08009413 /*
9414 * no branch sampling for probe events
9415 */
9416 if (has_branch_stack(event))
9417 return -EOPNOTSUPP;
9418
9419 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
9420 err = perf_kprobe_init(event, is_retprobe);
9421 if (err)
9422 return err;
9423
9424 event->destroy = perf_kprobe_destroy;
9425
9426 return 0;
9427}
9428#endif /* CONFIG_KPROBE_EVENTS */
9429
Song Liu33ea4b22017-12-06 14:45:16 -08009430#ifdef CONFIG_UPROBE_EVENTS
Song Liua6ca88b2018-10-01 22:36:36 -07009431PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63");
9432
9433static struct attribute *uprobe_attrs[] = {
9434 &format_attr_retprobe.attr,
9435 &format_attr_ref_ctr_offset.attr,
9436 NULL,
9437};
9438
9439static struct attribute_group uprobe_format_group = {
9440 .name = "format",
9441 .attrs = uprobe_attrs,
9442};
9443
9444static const struct attribute_group *uprobe_attr_groups[] = {
9445 &uprobe_format_group,
9446 NULL,
9447};
9448
Song Liu33ea4b22017-12-06 14:45:16 -08009449static int perf_uprobe_event_init(struct perf_event *event);
9450static struct pmu perf_uprobe = {
9451 .task_ctx_nr = perf_sw_context,
9452 .event_init = perf_uprobe_event_init,
9453 .add = perf_trace_add,
9454 .del = perf_trace_del,
9455 .start = perf_swevent_start,
9456 .stop = perf_swevent_stop,
9457 .read = perf_swevent_read,
Song Liua6ca88b2018-10-01 22:36:36 -07009458 .attr_groups = uprobe_attr_groups,
Song Liu33ea4b22017-12-06 14:45:16 -08009459};
9460
9461static int perf_uprobe_event_init(struct perf_event *event)
9462{
9463 int err;
Song Liua6ca88b2018-10-01 22:36:36 -07009464 unsigned long ref_ctr_offset;
Song Liu33ea4b22017-12-06 14:45:16 -08009465 bool is_retprobe;
9466
9467 if (event->attr.type != perf_uprobe.type)
9468 return -ENOENT;
Song Liu32e6e962018-04-11 18:02:37 +00009469
Alexey Budankovc9e09242020-04-02 11:47:01 +03009470 if (!perfmon_capable())
Song Liu32e6e962018-04-11 18:02:37 +00009471 return -EACCES;
9472
Song Liu33ea4b22017-12-06 14:45:16 -08009473 /*
9474 * no branch sampling for probe events
9475 */
9476 if (has_branch_stack(event))
9477 return -EOPNOTSUPP;
9478
9479 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE;
Song Liua6ca88b2018-10-01 22:36:36 -07009480 ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT;
9481 err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe);
Song Liu33ea4b22017-12-06 14:45:16 -08009482 if (err)
9483 return err;
9484
9485 event->destroy = perf_uprobe_destroy;
9486
9487 return 0;
9488}
9489#endif /* CONFIG_UPROBE_EVENTS */
9490
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009491static inline void perf_tp_register(void)
9492{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01009493 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Song Liue12f03d2017-12-06 14:45:15 -08009494#ifdef CONFIG_KPROBE_EVENTS
9495 perf_pmu_register(&perf_kprobe, "kprobe", -1);
9496#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009497#ifdef CONFIG_UPROBE_EVENTS
9498 perf_pmu_register(&perf_uprobe, "uprobe", -1);
9499#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009500}
Li Zefan6fb29152009-10-15 11:21:42 +08009501
Li Zefan6fb29152009-10-15 11:21:42 +08009502static void perf_event_free_filter(struct perf_event *event)
9503{
9504 ftrace_profile_free_filter(event);
9505}
9506
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009507#ifdef CONFIG_BPF_SYSCALL
9508static void bpf_overflow_handler(struct perf_event *event,
9509 struct perf_sample_data *data,
9510 struct pt_regs *regs)
9511{
9512 struct bpf_perf_event_data_kern ctx = {
9513 .data = data,
Yonghong Song7d9285e2017-10-05 09:19:19 -07009514 .event = event,
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009515 };
9516 int ret = 0;
9517
Hendrik Bruecknerc895f6f2017-12-04 10:56:44 +01009518 ctx.regs = perf_arch_bpf_user_pt_regs(regs);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009519 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
9520 goto out;
9521 rcu_read_lock();
Daniel Borkmann88575192016-11-26 01:28:04 +01009522 ret = BPF_PROG_RUN(event->prog, &ctx);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009523 rcu_read_unlock();
9524out:
9525 __this_cpu_dec(bpf_prog_active);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009526 if (!ret)
9527 return;
9528
9529 event->orig_overflow_handler(event, data, regs);
9530}
9531
9532static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9533{
9534 struct bpf_prog *prog;
9535
9536 if (event->overflow_handler_context)
9537 /* hw breakpoint or kernel counter */
9538 return -EINVAL;
9539
9540 if (event->prog)
9541 return -EEXIST;
9542
9543 prog = bpf_prog_get_type(prog_fd, BPF_PROG_TYPE_PERF_EVENT);
9544 if (IS_ERR(prog))
9545 return PTR_ERR(prog);
9546
Song Liu5d99cb2c2020-07-23 11:06:45 -07009547 if (event->attr.precise_ip &&
9548 prog->call_get_stack &&
9549 (!(event->attr.sample_type & __PERF_SAMPLE_CALLCHAIN_EARLY) ||
9550 event->attr.exclude_callchain_kernel ||
9551 event->attr.exclude_callchain_user)) {
9552 /*
9553 * On perf_event with precise_ip, calling bpf_get_stack()
9554 * may trigger unwinder warnings and occasional crashes.
9555 * bpf_get_[stack|stackid] works around this issue by using
9556 * callchain attached to perf_sample_data. If the
9557 * perf_event does not full (kernel and user) callchain
9558 * attached to perf_sample_data, do not allow attaching BPF
9559 * program that calls bpf_get_[stack|stackid].
9560 */
9561 bpf_prog_put(prog);
9562 return -EPROTO;
9563 }
9564
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -07009565 event->prog = prog;
9566 event->orig_overflow_handler = READ_ONCE(event->overflow_handler);
9567 WRITE_ONCE(event->overflow_handler, bpf_overflow_handler);
9568 return 0;
9569}
9570
9571static void perf_event_free_bpf_handler(struct perf_event *event)
9572{
9573 struct bpf_prog *prog = event->prog;
9574
9575 if (!prog)
9576 return;
9577
9578 WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
9579 event->prog = NULL;
9580 bpf_prog_put(prog);
9581}
9582#else
9583static int perf_event_set_bpf_handler(struct perf_event *event, u32 prog_fd)
9584{
9585 return -EOPNOTSUPP;
9586}
9587static void perf_event_free_bpf_handler(struct perf_event *event)
9588{
9589}
9590#endif
9591
Song Liue12f03d2017-12-06 14:45:15 -08009592/*
9593 * returns true if the event is a tracepoint, or a kprobe/upprobe created
9594 * with perf_event_open()
9595 */
9596static inline bool perf_event_is_tracing(struct perf_event *event)
9597{
9598 if (event->pmu == &perf_tracepoint)
9599 return true;
9600#ifdef CONFIG_KPROBE_EVENTS
9601 if (event->pmu == &perf_kprobe)
9602 return true;
9603#endif
Song Liu33ea4b22017-12-06 14:45:16 -08009604#ifdef CONFIG_UPROBE_EVENTS
9605 if (event->pmu == &perf_uprobe)
9606 return true;
9607#endif
Song Liue12f03d2017-12-06 14:45:15 -08009608 return false;
9609}
9610
Alexei Starovoitov25415172015-03-25 12:49:20 -07009611static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9612{
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009613 bool is_kprobe, is_tracepoint, is_syscall_tp;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009614 struct bpf_prog *prog;
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009615 int ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009616
Song Liue12f03d2017-12-06 14:45:15 -08009617 if (!perf_event_is_tracing(event))
Alexei Starovoitovf91840a2017-06-02 21:03:52 -07009618 return perf_event_set_bpf_handler(event, prog_fd);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009619
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009620 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
9621 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009622 is_syscall_tp = is_syscall_trace_event(event->tp_event);
9623 if (!is_kprobe && !is_tracepoint && !is_syscall_tp)
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009624 /* bpf programs can only be attached to u/kprobe or tracepoint */
Alexei Starovoitov25415172015-03-25 12:49:20 -07009625 return -EINVAL;
9626
9627 prog = bpf_prog_get(prog_fd);
9628 if (IS_ERR(prog))
9629 return PTR_ERR(prog);
9630
Alexei Starovoitov98b5c2c2016-04-06 18:43:25 -07009631 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009632 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) ||
9633 (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
Alexei Starovoitov25415172015-03-25 12:49:20 -07009634 /* valid fd, but invalid bpf program type */
9635 bpf_prog_put(prog);
9636 return -EINVAL;
9637 }
9638
Josef Bacik9802d862017-12-11 11:36:48 -05009639 /* Kprobe override only works for kprobes, not uprobes. */
9640 if (prog->kprobe_override &&
9641 !(event->tp_event->flags & TRACE_EVENT_FL_KPROBE)) {
9642 bpf_prog_put(prog);
9643 return -EINVAL;
9644 }
9645
Yonghong Songcf5f5ce2017-08-04 16:00:09 -07009646 if (is_tracepoint || is_syscall_tp) {
Alexei Starovoitov32bbe002016-04-06 18:43:28 -07009647 int off = trace_event_get_offsets(event->tp_event);
9648
9649 if (prog->aux->max_ctx_offset > off) {
9650 bpf_prog_put(prog);
9651 return -EACCES;
9652 }
9653 }
Alexei Starovoitov25415172015-03-25 12:49:20 -07009654
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009655 ret = perf_event_attach_bpf_prog(event, prog);
9656 if (ret)
9657 bpf_prog_put(prog);
9658 return ret;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009659}
9660
9661static void perf_event_free_bpf_prog(struct perf_event *event)
9662{
Song Liue12f03d2017-12-06 14:45:15 -08009663 if (!perf_event_is_tracing(event)) {
Yonghong Song0b4c6842017-10-23 23:53:07 -07009664 perf_event_free_bpf_handler(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009665 return;
Alexei Starovoitov25415172015-03-25 12:49:20 -07009666 }
Yonghong Songe87c6bc2017-10-23 23:53:08 -07009667 perf_event_detach_bpf_prog(event);
Alexei Starovoitov25415172015-03-25 12:49:20 -07009668}
9669
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009670#else
Li Zefan6fb29152009-10-15 11:21:42 +08009671
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02009672static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009673{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009674}
Li Zefan6fb29152009-10-15 11:21:42 +08009675
Li Zefan6fb29152009-10-15 11:21:42 +08009676static void perf_event_free_filter(struct perf_event *event)
9677{
9678}
9679
Alexei Starovoitov25415172015-03-25 12:49:20 -07009680static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
9681{
9682 return -ENOENT;
9683}
9684
9685static void perf_event_free_bpf_prog(struct perf_event *event)
9686{
9687}
Li Zefan07b139c2009-12-21 14:27:35 +08009688#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02009689
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009690#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009691void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009692{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009693 struct perf_sample_data sample;
9694 struct pt_regs *regs = data;
9695
Robert Richterfd0d0002012-04-02 20:19:08 +02009696 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01009697
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02009698 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02009699 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02009700}
9701#endif
9702
Alexander Shishkin375637b2016-04-27 18:44:46 +03009703/*
9704 * Allocate a new address filter
9705 */
9706static struct perf_addr_filter *
9707perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
9708{
9709 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
9710 struct perf_addr_filter *filter;
9711
9712 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
9713 if (!filter)
9714 return NULL;
9715
9716 INIT_LIST_HEAD(&filter->entry);
9717 list_add_tail(&filter->entry, filters);
9718
9719 return filter;
9720}
9721
9722static void free_filters_list(struct list_head *filters)
9723{
9724 struct perf_addr_filter *filter, *iter;
9725
9726 list_for_each_entry_safe(filter, iter, filters, entry) {
Song Liu9511bce2018-04-17 23:29:07 -07009727 path_put(&filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009728 list_del(&filter->entry);
9729 kfree(filter);
9730 }
9731}
9732
9733/*
9734 * Free existing address filters and optionally install new ones
9735 */
9736static void perf_addr_filters_splice(struct perf_event *event,
9737 struct list_head *head)
9738{
9739 unsigned long flags;
9740 LIST_HEAD(list);
9741
9742 if (!has_addr_filter(event))
9743 return;
9744
9745 /* don't bother with children, they don't have their own filters */
9746 if (event->parent)
9747 return;
9748
9749 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
9750
9751 list_splice_init(&event->addr_filters.list, &list);
9752 if (head)
9753 list_splice(head, &event->addr_filters.list);
9754
9755 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
9756
9757 free_filters_list(&list);
9758}
9759
9760/*
9761 * Scan through mm's vmas and see if one of them matches the
9762 * @filter; if so, adjust filter's address range.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07009763 * Called with mm::mmap_lock down for reading.
Alexander Shishkin375637b2016-04-27 18:44:46 +03009764 */
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009765static void perf_addr_filter_apply(struct perf_addr_filter *filter,
9766 struct mm_struct *mm,
9767 struct perf_addr_filter_range *fr)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009768{
9769 struct vm_area_struct *vma;
9770
9771 for (vma = mm->mmap; vma; vma = vma->vm_next) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009772 if (!vma->vm_file)
Alexander Shishkin375637b2016-04-27 18:44:46 +03009773 continue;
9774
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009775 if (perf_addr_filter_vma_adjust(filter, vma, fr))
9776 return;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009777 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009778}
9779
9780/*
9781 * Update event's address range filters based on the
9782 * task's existing mappings, if any.
9783 */
9784static void perf_event_addr_filters_apply(struct perf_event *event)
9785{
9786 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
9787 struct task_struct *task = READ_ONCE(event->ctx->task);
9788 struct perf_addr_filter *filter;
9789 struct mm_struct *mm = NULL;
9790 unsigned int count = 0;
9791 unsigned long flags;
9792
9793 /*
9794 * We may observe TASK_TOMBSTONE, which means that the event tear-down
9795 * will stop on the parent's child_mutex that our caller is also holding
9796 */
9797 if (task == TASK_TOMBSTONE)
9798 return;
9799
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009800 if (ifh->nr_file_filters) {
9801 mm = get_task_mm(event->ctx->task);
9802 if (!mm)
9803 goto restart;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009804
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07009805 mmap_read_lock(mm);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009806 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009807
9808 raw_spin_lock_irqsave(&ifh->lock, flags);
9809 list_for_each_entry(filter, &ifh->list, entry) {
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009810 if (filter->path.dentry) {
9811 /*
9812 * Adjust base offset if the filter is associated to a
9813 * binary that needs to be mapped:
9814 */
9815 event->addr_filter_ranges[count].start = 0;
9816 event->addr_filter_ranges[count].size = 0;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009817
Alexander Shishkinc60f83b2019-02-15 13:56:55 +02009818 perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]);
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009819 } else {
9820 event->addr_filter_ranges[count].start = filter->offset;
9821 event->addr_filter_ranges[count].size = filter->size;
9822 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009823
9824 count++;
9825 }
9826
9827 event->addr_filters_gen++;
9828 raw_spin_unlock_irqrestore(&ifh->lock, flags);
9829
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009830 if (ifh->nr_file_filters) {
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07009831 mmap_read_unlock(mm);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009832
Alexander Shishkin52a44f82019-03-29 11:12:12 +02009833 mmput(mm);
9834 }
Alexander Shishkin375637b2016-04-27 18:44:46 +03009835
9836restart:
Alexander Shishkin767ae082016-09-06 16:23:49 +03009837 perf_event_stop(event, 1);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009838}
9839
9840/*
9841 * Address range filtering: limiting the data to certain
9842 * instruction address ranges. Filters are ioctl()ed to us from
9843 * userspace as ascii strings.
9844 *
9845 * Filter string format:
9846 *
9847 * ACTION RANGE_SPEC
9848 * where ACTION is one of the
9849 * * "filter": limit the trace to this region
9850 * * "start": start tracing from this address
9851 * * "stop": stop tracing at this address/region;
9852 * RANGE_SPEC is
9853 * * for kernel addresses: <start address>[/<size>]
9854 * * for object files: <start address>[/<size>]@</path/to/object/file>
9855 *
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009856 * if <size> is not specified or is zero, the range is treated as a single
9857 * address; not valid for ACTION=="filter".
Alexander Shishkin375637b2016-04-27 18:44:46 +03009858 */
9859enum {
Alexander Shishkine96271f2016-11-18 13:38:43 +02009860 IF_ACT_NONE = -1,
Alexander Shishkin375637b2016-04-27 18:44:46 +03009861 IF_ACT_FILTER,
9862 IF_ACT_START,
9863 IF_ACT_STOP,
9864 IF_SRC_FILE,
9865 IF_SRC_KERNEL,
9866 IF_SRC_FILEADDR,
9867 IF_SRC_KERNELADDR,
9868};
9869
9870enum {
9871 IF_STATE_ACTION = 0,
9872 IF_STATE_SOURCE,
9873 IF_STATE_END,
9874};
9875
9876static const match_table_t if_tokens = {
9877 { IF_ACT_FILTER, "filter" },
9878 { IF_ACT_START, "start" },
9879 { IF_ACT_STOP, "stop" },
9880 { IF_SRC_FILE, "%u/%u@%s" },
9881 { IF_SRC_KERNEL, "%u/%u" },
9882 { IF_SRC_FILEADDR, "%u@%s" },
9883 { IF_SRC_KERNELADDR, "%u" },
Alexander Shishkine96271f2016-11-18 13:38:43 +02009884 { IF_ACT_NONE, NULL },
Alexander Shishkin375637b2016-04-27 18:44:46 +03009885};
9886
9887/*
9888 * Address filter string parser
9889 */
9890static int
9891perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
9892 struct list_head *filters)
9893{
9894 struct perf_addr_filter *filter = NULL;
9895 char *start, *orig, *filename = NULL;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009896 substring_t args[MAX_OPT_ARGS];
9897 int state = IF_STATE_ACTION, token;
9898 unsigned int kernel = 0;
9899 int ret = -EINVAL;
9900
9901 orig = fstr = kstrdup(fstr, GFP_KERNEL);
9902 if (!fstr)
9903 return -ENOMEM;
9904
9905 while ((start = strsep(&fstr, " ,\n")) != NULL) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009906 static const enum perf_addr_filter_action_t actions[] = {
9907 [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER,
9908 [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START,
9909 [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP,
9910 };
Alexander Shishkin375637b2016-04-27 18:44:46 +03009911 ret = -EINVAL;
9912
9913 if (!*start)
9914 continue;
9915
9916 /* filter definition begins */
9917 if (state == IF_STATE_ACTION) {
9918 filter = perf_addr_filter_new(event, filters);
9919 if (!filter)
9920 goto fail;
9921 }
9922
9923 token = match_token(start, if_tokens, args);
9924 switch (token) {
9925 case IF_ACT_FILTER:
9926 case IF_ACT_START:
Alexander Shishkin375637b2016-04-27 18:44:46 +03009927 case IF_ACT_STOP:
9928 if (state != IF_STATE_ACTION)
9929 goto fail;
9930
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009931 filter->action = actions[token];
Alexander Shishkin375637b2016-04-27 18:44:46 +03009932 state = IF_STATE_SOURCE;
9933 break;
9934
9935 case IF_SRC_KERNELADDR:
9936 case IF_SRC_KERNEL:
9937 kernel = 1;
Gustavo A. R. Silva10c34052019-02-12 14:54:30 -06009938 /* fall through */
Alexander Shishkin375637b2016-04-27 18:44:46 +03009939
9940 case IF_SRC_FILEADDR:
9941 case IF_SRC_FILE:
9942 if (state != IF_STATE_SOURCE)
9943 goto fail;
9944
Alexander Shishkin375637b2016-04-27 18:44:46 +03009945 *args[0].to = 0;
9946 ret = kstrtoul(args[0].from, 0, &filter->offset);
9947 if (ret)
9948 goto fail;
9949
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009950 if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) {
Alexander Shishkin375637b2016-04-27 18:44:46 +03009951 *args[1].to = 0;
9952 ret = kstrtoul(args[1].from, 0, &filter->size);
9953 if (ret)
9954 goto fail;
9955 }
9956
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06009957 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) {
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009958 int fpos = token == IF_SRC_FILE ? 2 : 1;
Mathieu Poirier4059ffd2016-07-18 10:43:05 -06009959
9960 filename = match_strdup(&args[fpos]);
Alexander Shishkin375637b2016-04-27 18:44:46 +03009961 if (!filename) {
9962 ret = -ENOMEM;
9963 goto fail;
9964 }
9965 }
9966
9967 state = IF_STATE_END;
9968 break;
9969
9970 default:
9971 goto fail;
9972 }
9973
9974 /*
9975 * Filter definition is fully parsed, validate and install it.
9976 * Make sure that it doesn't contradict itself or the event's
9977 * attribute.
9978 */
9979 if (state == IF_STATE_END) {
Alexander Shishkin9ccbfbb2017-01-26 11:40:56 +02009980 ret = -EINVAL;
Alexander Shishkin375637b2016-04-27 18:44:46 +03009981 if (kernel && event->attr.exclude_kernel)
9982 goto fail;
9983
Alexander Shishkin6ed70cf2018-03-29 15:06:48 +03009984 /*
9985 * ACTION "filter" must have a non-zero length region
9986 * specified.
9987 */
9988 if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER &&
9989 !filter->size)
9990 goto fail;
9991
Alexander Shishkin375637b2016-04-27 18:44:46 +03009992 if (!kernel) {
9993 if (!filename)
9994 goto fail;
9995
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +02009996 /*
9997 * For now, we only support file-based filters
9998 * in per-task events; doing so for CPU-wide
9999 * events requires additional context switching
10000 * trickery, since same object code will be
10001 * mapped at different virtual addresses in
10002 * different processes.
10003 */
10004 ret = -EOPNOTSUPP;
10005 if (!event->ctx->task)
10006 goto fail_free_name;
10007
Alexander Shishkin375637b2016-04-27 18:44:46 +030010008 /* look up the path and grab its inode */
Song Liu9511bce2018-04-17 23:29:07 -070010009 ret = kern_path(filename, LOOKUP_FOLLOW,
10010 &filter->path);
Alexander Shishkin375637b2016-04-27 18:44:46 +030010011 if (ret)
10012 goto fail_free_name;
10013
Alexander Shishkin375637b2016-04-27 18:44:46 +030010014 kfree(filename);
10015 filename = NULL;
10016
10017 ret = -EINVAL;
Song Liu9511bce2018-04-17 23:29:07 -070010018 if (!filter->path.dentry ||
10019 !S_ISREG(d_inode(filter->path.dentry)
10020 ->i_mode))
Alexander Shishkin375637b2016-04-27 18:44:46 +030010021 goto fail;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010022
10023 event->addr_filters.nr_file_filters++;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010024 }
10025
10026 /* ready to consume more filters */
10027 state = IF_STATE_ACTION;
10028 filter = NULL;
10029 }
10030 }
10031
10032 if (state != IF_STATE_ACTION)
10033 goto fail;
10034
10035 kfree(orig);
10036
10037 return 0;
10038
10039fail_free_name:
10040 kfree(filename);
10041fail:
10042 free_filters_list(filters);
10043 kfree(orig);
10044
10045 return ret;
10046}
10047
10048static int
10049perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
10050{
10051 LIST_HEAD(filters);
10052 int ret;
10053
10054 /*
10055 * Since this is called in perf_ioctl() path, we're already holding
10056 * ctx::mutex.
10057 */
10058 lockdep_assert_held(&event->ctx->mutex);
10059
10060 if (WARN_ON_ONCE(event->parent))
10061 return -EINVAL;
10062
Alexander Shishkin375637b2016-04-27 18:44:46 +030010063 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
10064 if (ret)
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010065 goto fail_clear_files;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010066
10067 ret = event->pmu->addr_filters_validate(&filters);
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010068 if (ret)
10069 goto fail_free_filters;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010070
10071 /* remove existing filters, if any */
10072 perf_addr_filters_splice(event, &filters);
10073
10074 /* install new filters */
10075 perf_event_for_each_child(event, perf_event_addr_filters_apply);
10076
10077 return ret;
Alexander Shishkin6ce77bf2017-01-26 11:40:57 +020010078
10079fail_free_filters:
10080 free_filters_list(&filters);
10081
10082fail_clear_files:
10083 event->addr_filters.nr_file_filters = 0;
10084
10085 return ret;
Alexander Shishkin375637b2016-04-27 18:44:46 +030010086}
10087
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010088static int perf_event_set_filter(struct perf_event *event, void __user *arg)
10089{
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010090 int ret = -EINVAL;
Song Liue12f03d2017-12-06 14:45:15 -080010091 char *filter_str;
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010092
10093 filter_str = strndup_user(arg, PAGE_SIZE);
10094 if (IS_ERR(filter_str))
10095 return PTR_ERR(filter_str);
10096
Song Liue12f03d2017-12-06 14:45:15 -080010097#ifdef CONFIG_EVENT_TRACING
10098 if (perf_event_is_tracing(event)) {
10099 struct perf_event_context *ctx = event->ctx;
10100
10101 /*
10102 * Beware, here be dragons!!
10103 *
10104 * the tracepoint muck will deadlock against ctx->mutex, but
10105 * the tracepoint stuff does not actually need it. So
10106 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
10107 * already have a reference on ctx.
10108 *
10109 * This can result in event getting moved to a different ctx,
10110 * but that does not affect the tracepoint state.
10111 */
10112 mutex_unlock(&ctx->mutex);
10113 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
10114 mutex_lock(&ctx->mutex);
10115 } else
10116#endif
10117 if (has_addr_filter(event))
Alexander Shishkin375637b2016-04-27 18:44:46 +030010118 ret = perf_event_set_addr_filter(event, filter_str);
Alexander Shishkinc796bbb2016-04-27 18:44:42 +030010119
10120 kfree(filter_str);
10121 return ret;
10122}
10123
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010124/*
10125 * hrtimer based swevent callback
10126 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010127
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010128static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010129{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010130 enum hrtimer_restart ret = HRTIMER_RESTART;
10131 struct perf_sample_data data;
10132 struct pt_regs *regs;
10133 struct perf_event *event;
10134 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010135
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010136 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010137
10138 if (event->state != PERF_EVENT_STATE_ACTIVE)
10139 return HRTIMER_NORESTART;
10140
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010141 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010142
Robert Richterfd0d0002012-04-02 20:19:08 +020010143 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010144 regs = get_irq_regs();
10145
10146 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -080010147 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +020010148 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010149 ret = HRTIMER_NORESTART;
10150 }
10151
10152 period = max_t(u64, 10000, event->hw.sample_period);
10153 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
10154
10155 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010156}
10157
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010158static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010159{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010160 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010161 s64 period;
10162
10163 if (!is_sampling_event(event))
10164 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010165
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010166 period = local64_read(&hwc->period_left);
10167 if (period) {
10168 if (period < 0)
10169 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +020010170
Franck Bui-Huu5d508e82010-11-23 16:21:45 +010010171 local64_set(&hwc->period_left, 0);
10172 } else {
10173 period = max_t(u64, 10000, hwc->sample_period);
10174 }
Thomas Gleixner3497d202015-04-14 21:09:03 +000010175 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +020010176 HRTIMER_MODE_REL_PINNED_HARD);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010177}
10178
10179static void perf_swevent_cancel_hrtimer(struct perf_event *event)
10180{
10181 struct hw_perf_event *hwc = &event->hw;
10182
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +010010183 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010184 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +020010185 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010186
10187 hrtimer_cancel(&hwc->hrtimer);
10188 }
10189}
10190
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010191static void perf_swevent_init_hrtimer(struct perf_event *event)
10192{
10193 struct hw_perf_event *hwc = &event->hw;
10194
10195 if (!is_sampling_event(event))
10196 return;
10197
Sebastian Andrzej Siewior30f90282019-07-26 20:30:53 +020010198 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010199 hwc->hrtimer.function = perf_swevent_hrtimer;
10200
10201 /*
10202 * Since hrtimers have a fixed rate, we can do a static freq->period
10203 * mapping and avoid the whole period adjust feedback stuff.
10204 */
10205 if (event->attr.freq) {
10206 long freq = event->attr.sample_freq;
10207
10208 event->attr.sample_period = NSEC_PER_SEC / freq;
10209 hwc->sample_period = event->attr.sample_period;
10210 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +090010211 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010212 event->attr.freq = 0;
10213 }
10214}
10215
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010216/*
10217 * Software event: cpu wall time clock
10218 */
10219
10220static void cpu_clock_event_update(struct perf_event *event)
10221{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010222 s64 prev;
10223 u64 now;
10224
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010225 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010226 prev = local64_xchg(&event->hw.prev_count, now);
10227 local64_add(now - prev, &event->count);
10228}
10229
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010230static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010231{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010232 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010233 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010234}
10235
10236static void cpu_clock_event_stop(struct perf_event *event, int flags)
10237{
10238 perf_swevent_cancel_hrtimer(event);
10239 cpu_clock_event_update(event);
10240}
10241
10242static int cpu_clock_event_add(struct perf_event *event, int flags)
10243{
10244 if (flags & PERF_EF_START)
10245 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -080010246 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010247
10248 return 0;
10249}
10250
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010251static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010252{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010253 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010254}
10255
10256static void cpu_clock_event_read(struct perf_event *event)
10257{
10258 cpu_clock_event_update(event);
10259}
10260
10261static int cpu_clock_event_init(struct perf_event *event)
10262{
10263 if (event->attr.type != PERF_TYPE_SOFTWARE)
10264 return -ENOENT;
10265
10266 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
10267 return -ENOENT;
10268
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010269 /*
10270 * no branch sampling for software events
10271 */
10272 if (has_branch_stack(event))
10273 return -EOPNOTSUPP;
10274
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010275 perf_swevent_init_hrtimer(event);
10276
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010277 return 0;
10278}
10279
10280static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010281 .task_ctx_nr = perf_sw_context,
10282
Peter Zijlstra34f43922015-02-20 14:05:38 +010010283 .capabilities = PERF_PMU_CAP_NO_NMI,
10284
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010285 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010286 .add = cpu_clock_event_add,
10287 .del = cpu_clock_event_del,
10288 .start = cpu_clock_event_start,
10289 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010290 .read = cpu_clock_event_read,
10291};
10292
10293/*
10294 * Software event: task time clock
10295 */
10296
10297static void task_clock_event_update(struct perf_event *event, u64 now)
10298{
10299 u64 prev;
10300 s64 delta;
10301
10302 prev = local64_xchg(&event->hw.prev_count, now);
10303 delta = now - prev;
10304 local64_add(delta, &event->count);
10305}
10306
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010307static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010308{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010309 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010310 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010311}
10312
10313static void task_clock_event_stop(struct perf_event *event, int flags)
10314{
10315 perf_swevent_cancel_hrtimer(event);
10316 task_clock_event_update(event, event->ctx->time);
10317}
10318
10319static int task_clock_event_add(struct perf_event *event, int flags)
10320{
10321 if (flags & PERF_EF_START)
10322 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -080010323 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010324
10325 return 0;
10326}
10327
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010328static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010329{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010330 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010331}
10332
10333static void task_clock_event_read(struct perf_event *event)
10334{
Peter Zijlstra768a06e2011-02-22 16:52:24 +010010335 u64 now = perf_clock();
10336 u64 delta = now - event->ctx->timestamp;
10337 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010338
10339 task_clock_event_update(event, time);
10340}
10341
10342static int task_clock_event_init(struct perf_event *event)
10343{
10344 if (event->attr.type != PERF_TYPE_SOFTWARE)
10345 return -ENOENT;
10346
10347 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
10348 return -ENOENT;
10349
Stephane Eranian2481c5f2012-02-09 23:20:59 +010010350 /*
10351 * no branch sampling for software events
10352 */
10353 if (has_branch_stack(event))
10354 return -EOPNOTSUPP;
10355
Peter Zijlstraba3dd362011-02-15 12:41:46 +010010356 perf_swevent_init_hrtimer(event);
10357
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010358 return 0;
10359}
10360
10361static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +020010362 .task_ctx_nr = perf_sw_context,
10363
Peter Zijlstra34f43922015-02-20 14:05:38 +010010364 .capabilities = PERF_PMU_CAP_NO_NMI,
10365
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010366 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +020010367 .add = task_clock_event_add,
10368 .del = task_clock_event_del,
10369 .start = task_clock_event_start,
10370 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010371 .read = task_clock_event_read,
10372};
10373
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010374static void perf_pmu_nop_void(struct pmu *pmu)
10375{
10376}
10377
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010378static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
10379{
10380}
10381
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010382static int perf_pmu_nop_int(struct pmu *pmu)
10383{
10384 return 0;
10385}
10386
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010387static int perf_event_nop_int(struct perf_event *event, u64 value)
10388{
10389 return 0;
10390}
10391
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010392static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010393
10394static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010395{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010396 __this_cpu_write(nop_txn_flags, flags);
10397
10398 if (flags & ~PERF_PMU_TXN_ADD)
10399 return;
10400
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010401 perf_pmu_disable(pmu);
10402}
10403
10404static int perf_pmu_commit_txn(struct pmu *pmu)
10405{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010406 unsigned int flags = __this_cpu_read(nop_txn_flags);
10407
10408 __this_cpu_write(nop_txn_flags, 0);
10409
10410 if (flags & ~PERF_PMU_TXN_ADD)
10411 return 0;
10412
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010413 perf_pmu_enable(pmu);
10414 return 0;
10415}
10416
10417static void perf_pmu_cancel_txn(struct pmu *pmu)
10418{
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010419 unsigned int flags = __this_cpu_read(nop_txn_flags);
10420
10421 __this_cpu_write(nop_txn_flags, 0);
10422
10423 if (flags & ~PERF_PMU_TXN_ADD)
10424 return;
10425
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010426 perf_pmu_enable(pmu);
10427}
10428
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010429static int perf_event_idx_default(struct perf_event *event)
10430{
Peter Zijlstrac719f562014-10-21 11:10:21 +020010431 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010432}
10433
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010434/*
10435 * Ensures all contexts with the same task_ctx_nr have the same
10436 * pmu_cpu_context too.
10437 */
Mark Rutland9e317042014-02-10 17:44:18 +000010438static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010439{
10440 struct pmu *pmu;
10441
10442 if (ctxn < 0)
10443 return NULL;
10444
10445 list_for_each_entry(pmu, &pmus, entry) {
10446 if (pmu->task_ctx_nr == ctxn)
10447 return pmu->pmu_cpu_context;
10448 }
10449
10450 return NULL;
10451}
10452
Peter Zijlstra51676952010-12-07 14:18:20 +010010453static void free_pmu_context(struct pmu *pmu)
10454{
Will Deacondf0062b2017-10-03 15:20:50 +010010455 /*
10456 * Static contexts such as perf_sw_context have a global lifetime
10457 * and may be shared between different PMUs. Avoid freeing them
10458 * when a single PMU is going away.
10459 */
10460 if (pmu->task_ctx_nr > perf_invalid_context)
10461 return;
10462
Peter Zijlstra51676952010-12-07 14:18:20 +010010463 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010464}
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010465
10466/*
10467 * Let userspace know that this PMU supports address range filtering:
10468 */
10469static ssize_t nr_addr_filters_show(struct device *dev,
10470 struct device_attribute *attr,
10471 char *page)
10472{
10473 struct pmu *pmu = dev_get_drvdata(dev);
10474
10475 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
10476}
10477DEVICE_ATTR_RO(nr_addr_filters);
10478
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010479static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010480
Peter Zijlstraabe43402010-11-17 23:17:37 +010010481static ssize_t
10482type_show(struct device *dev, struct device_attribute *attr, char *page)
10483{
10484 struct pmu *pmu = dev_get_drvdata(dev);
10485
10486 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
10487}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010488static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010489
Stephane Eranian62b85632013-04-03 14:21:34 +020010490static ssize_t
10491perf_event_mux_interval_ms_show(struct device *dev,
10492 struct device_attribute *attr,
10493 char *page)
10494{
10495 struct pmu *pmu = dev_get_drvdata(dev);
10496
10497 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
10498}
10499
Peter Zijlstra272325c2015-04-15 11:41:58 +020010500static DEFINE_MUTEX(mux_interval_mutex);
10501
Stephane Eranian62b85632013-04-03 14:21:34 +020010502static ssize_t
10503perf_event_mux_interval_ms_store(struct device *dev,
10504 struct device_attribute *attr,
10505 const char *buf, size_t count)
10506{
10507 struct pmu *pmu = dev_get_drvdata(dev);
10508 int timer, cpu, ret;
10509
10510 ret = kstrtoint(buf, 0, &timer);
10511 if (ret)
10512 return ret;
10513
10514 if (timer < 1)
10515 return -EINVAL;
10516
10517 /* same value, noting to do */
10518 if (timer == pmu->hrtimer_interval_ms)
10519 return count;
10520
Peter Zijlstra272325c2015-04-15 11:41:58 +020010521 mutex_lock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010522 pmu->hrtimer_interval_ms = timer;
10523
10524 /* update all cpuctx for this PMU */
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010525 cpus_read_lock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010526 for_each_online_cpu(cpu) {
Stephane Eranian62b85632013-04-03 14:21:34 +020010527 struct perf_cpu_context *cpuctx;
10528 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
10529 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
10530
Peter Zijlstra272325c2015-04-15 11:41:58 +020010531 cpu_function_call(cpu,
10532 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
Stephane Eranian62b85632013-04-03 14:21:34 +020010533 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010534 cpus_read_unlock();
Peter Zijlstra272325c2015-04-15 11:41:58 +020010535 mutex_unlock(&mux_interval_mutex);
Stephane Eranian62b85632013-04-03 14:21:34 +020010536
10537 return count;
10538}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010539static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +020010540
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010541static struct attribute *pmu_dev_attrs[] = {
10542 &dev_attr_type.attr,
10543 &dev_attr_perf_event_mux_interval_ms.attr,
10544 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010545};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010546ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +010010547
10548static int pmu_bus_running;
10549static struct bus_type pmu_bus = {
10550 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -070010551 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +010010552};
10553
10554static void pmu_dev_release(struct device *dev)
10555{
10556 kfree(dev);
10557}
10558
10559static int pmu_dev_alloc(struct pmu *pmu)
10560{
10561 int ret = -ENOMEM;
10562
10563 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
10564 if (!pmu->dev)
10565 goto out;
10566
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +010010567 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +010010568 device_initialize(pmu->dev);
10569 ret = dev_set_name(pmu->dev, "%s", pmu->name);
10570 if (ret)
10571 goto free_dev;
10572
10573 dev_set_drvdata(pmu->dev, pmu);
10574 pmu->dev->bus = &pmu_bus;
10575 pmu->dev->release = pmu_dev_release;
10576 ret = device_add(pmu->dev);
10577 if (ret)
10578 goto free_dev;
10579
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010580 /* For PMUs with address filters, throw in an extra attribute: */
10581 if (pmu->nr_addr_filters)
10582 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
10583
10584 if (ret)
10585 goto del_dev;
10586
Jiri Olsaf3a3a822019-05-12 17:55:11 +020010587 if (pmu->attr_update)
10588 ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update);
10589
10590 if (ret)
10591 goto del_dev;
10592
Peter Zijlstraabe43402010-11-17 23:17:37 +010010593out:
10594 return ret;
10595
Alexander Shishkin6e855cd2016-04-27 18:44:48 +030010596del_dev:
10597 device_del(pmu->dev);
10598
Peter Zijlstraabe43402010-11-17 23:17:37 +010010599free_dev:
10600 put_device(pmu->dev);
10601 goto out;
10602}
10603
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010604static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010605static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010606
Mischa Jonker03d8e802013-06-04 11:45:48 +020010607int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010608{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010609 int cpu, ret, max = PERF_TYPE_MAX;
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010610
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010611 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010612 ret = -ENOMEM;
10613 pmu->pmu_disable_count = alloc_percpu(int);
10614 if (!pmu->pmu_disable_count)
10615 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010616
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010617 pmu->type = -1;
10618 if (!name)
10619 goto skip_type;
10620 pmu->name = name;
10621
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010622 if (type != PERF_TYPE_SOFTWARE) {
10623 if (type >= 0)
10624 max = type;
10625
10626 ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL);
10627 if (ret < 0)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010628 goto free_pdc;
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010629
10630 WARN_ON(type >= 0 && ret != type);
10631
10632 type = ret;
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010633 }
10634 pmu->type = type;
10635
Peter Zijlstraabe43402010-11-17 23:17:37 +010010636 if (pmu_bus_running) {
10637 ret = pmu_dev_alloc(pmu);
10638 if (ret)
10639 goto free_idr;
10640 }
10641
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010642skip_type:
Peter Zijlstra26657842016-03-22 22:09:18 +010010643 if (pmu->task_ctx_nr == perf_hw_context) {
10644 static int hw_context_taken = 0;
10645
Mark Rutland5101ef22016-04-26 11:33:46 +010010646 /*
10647 * Other than systems with heterogeneous CPUs, it never makes
10648 * sense for two PMUs to share perf_hw_context. PMUs which are
10649 * uncore must use perf_invalid_context.
10650 */
10651 if (WARN_ON_ONCE(hw_context_taken &&
10652 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
Peter Zijlstra26657842016-03-22 22:09:18 +010010653 pmu->task_ctx_nr = perf_invalid_context;
10654
10655 hw_context_taken = 1;
10656 }
10657
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010658 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
10659 if (pmu->pmu_cpu_context)
10660 goto got_cpu_context;
10661
Wei Yongjunc4814202013-04-12 11:05:54 +080010662 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010663 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
10664 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +010010665 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010666
10667 for_each_possible_cpu(cpu) {
10668 struct perf_cpu_context *cpuctx;
10669
10670 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +020010671 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +010010672 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +020010673 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010674 cpuctx->ctx.pmu = pmu;
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020010675 cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask);
Stephane Eranian9e630202013-04-03 14:21:33 +020010676
Peter Zijlstra272325c2015-04-15 11:41:58 +020010677 __perf_mux_hrtimer_init(cpuctx, cpu);
Ian Rogers836196be2020-02-13 23:51:31 -080010678
10679 cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default);
10680 cpuctx->heap = cpuctx->heap_default;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010681 }
10682
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020010683got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010684 if (!pmu->start_txn) {
10685 if (pmu->pmu_enable) {
10686 /*
10687 * If we have pmu_enable/pmu_disable calls, install
10688 * transaction stubs that use that to try and batch
10689 * hardware accesses.
10690 */
10691 pmu->start_txn = perf_pmu_start_txn;
10692 pmu->commit_txn = perf_pmu_commit_txn;
10693 pmu->cancel_txn = perf_pmu_cancel_txn;
10694 } else {
Sukadev Bhattiprolufbbe0702015-09-03 20:07:45 -070010695 pmu->start_txn = perf_pmu_nop_txn;
Peter Zijlstraad5133b2010-06-15 12:22:39 +020010696 pmu->commit_txn = perf_pmu_nop_int;
10697 pmu->cancel_txn = perf_pmu_nop_void;
10698 }
10699 }
10700
10701 if (!pmu->pmu_enable) {
10702 pmu->pmu_enable = perf_pmu_nop_void;
10703 pmu->pmu_disable = perf_pmu_nop_void;
10704 }
10705
Jiri Olsa81ec3f32019-02-04 13:35:32 +010010706 if (!pmu->check_period)
10707 pmu->check_period = perf_event_nop_int;
10708
Peter Zijlstra35edc2a2011-11-20 20:36:02 +010010709 if (!pmu->event_idx)
10710 pmu->event_idx = perf_event_idx_default;
10711
Liang, Kand44f8212019-10-22 11:13:09 +020010712 /*
10713 * Ensure the TYPE_SOFTWARE PMUs are at the head of the list,
10714 * since these cannot be in the IDR. This way the linear search
10715 * is fast, provided a valid software event is provided.
10716 */
10717 if (type == PERF_TYPE_SOFTWARE || !name)
10718 list_add_rcu(&pmu->entry, &pmus);
10719 else
10720 list_add_tail_rcu(&pmu->entry, &pmus);
10721
Alexander Shishkinbed5b252015-01-30 12:31:06 +020010722 atomic_set(&pmu->exclusive_cnt, 0);
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010723 ret = 0;
10724unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010725 mutex_unlock(&pmus_lock);
10726
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010727 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010728
Peter Zijlstraabe43402010-11-17 23:17:37 +010010729free_dev:
10730 device_del(pmu->dev);
10731 put_device(pmu->dev);
10732
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010733free_idr:
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010734 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010735 idr_remove(&pmu_idr, pmu->type);
10736
Peter Zijlstra108b02c2010-09-06 14:32:03 +020010737free_pdc:
10738 free_percpu(pmu->pmu_disable_count);
10739 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010740}
Yan, Zhengc464c762014-03-18 16:56:41 +080010741EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010742
10743void perf_pmu_unregister(struct pmu *pmu)
10744{
10745 mutex_lock(&pmus_lock);
10746 list_del_rcu(&pmu->entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010747
10748 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +020010749 * We dereference the pmu list under both SRCU and regular RCU, so
10750 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010751 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010752 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +020010753 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010754
Peter Zijlstra33696fc2010-06-14 08:49:00 +020010755 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010756 if (pmu->type != PERF_TYPE_SOFTWARE)
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010757 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010758 if (pmu_bus_running) {
Jiri Olsa09338402016-10-20 13:10:11 +020010759 if (pmu->nr_addr_filters)
10760 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
10761 device_del(pmu->dev);
10762 put_device(pmu->dev);
10763 }
Peter Zijlstra51676952010-12-07 14:18:20 +010010764 free_pmu_context(pmu);
Peter Zijlstraa9f97722018-09-25 17:58:35 +020010765 mutex_unlock(&pmus_lock);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010766}
Yan, Zhengc464c762014-03-18 16:56:41 +080010767EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010768
Kan Liange321d022019-05-28 15:08:30 -070010769static inline bool has_extended_regs(struct perf_event *event)
10770{
10771 return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) ||
10772 (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK);
10773}
10774
Mark Rutlandcc34b982015-01-07 14:56:51 +000010775static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
10776{
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010777 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +000010778 int ret;
10779
10780 if (!try_module_get(pmu->module))
10781 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010782
Peter Zijlstra0c7296c2018-01-09 21:23:02 +010010783 /*
10784 * A number of pmu->event_init() methods iterate the sibling_list to,
10785 * for example, validate if the group fits on the PMU. Therefore,
10786 * if this is a sibling event, acquire the ctx->mutex to protect
10787 * the sibling_list.
10788 */
10789 if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) {
Peter Zijlstra8b10c5e2015-05-01 16:08:46 +020010790 /*
10791 * This ctx->mutex can nest when we're called through
10792 * inheritance. See the perf_event_ctx_lock_nested() comment.
10793 */
10794 ctx = perf_event_ctx_lock_nested(event->group_leader,
10795 SINGLE_DEPTH_NESTING);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010796 BUG_ON(!ctx);
10797 }
10798
Mark Rutlandcc34b982015-01-07 14:56:51 +000010799 event->pmu = pmu;
10800 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +010010801
10802 if (ctx)
10803 perf_event_ctx_unlock(event->group_leader, ctx);
10804
Andrew Murraycc6795a2019-01-10 13:53:25 +000010805 if (!ret) {
Kan Liange321d022019-05-28 15:08:30 -070010806 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) &&
10807 has_extended_regs(event))
10808 ret = -EOPNOTSUPP;
10809
Andrew Murraycc6795a2019-01-10 13:53:25 +000010810 if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE &&
Kan Liange321d022019-05-28 15:08:30 -070010811 event_has_any_exclude_flag(event))
Andrew Murraycc6795a2019-01-10 13:53:25 +000010812 ret = -EINVAL;
Kan Liange321d022019-05-28 15:08:30 -070010813
10814 if (ret && event->destroy)
10815 event->destroy(event);
Andrew Murraycc6795a2019-01-10 13:53:25 +000010816 }
10817
Mark Rutlandcc34b982015-01-07 14:56:51 +000010818 if (ret)
10819 module_put(pmu->module);
10820
10821 return ret;
10822}
10823
Geliang Tang18ab2cd2015-09-27 23:25:50 +080010824static struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010825{
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010826 int idx, type, ret;
Dan Carpenter85c617a2017-05-22 12:03:49 +030010827 struct pmu *pmu;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010828
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010829 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010830
Kan Liang40999312017-01-18 08:21:01 -050010831 /* Try parent's PMU first: */
10832 if (event->parent && event->parent->pmu) {
10833 pmu = event->parent->pmu;
10834 ret = perf_try_init_event(pmu, event);
10835 if (!ret)
10836 goto unlock;
10837 }
10838
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010839 /*
10840 * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE
10841 * are often aliases for PERF_TYPE_RAW.
10842 */
10843 type = event->attr.type;
10844 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)
10845 type = PERF_TYPE_RAW;
10846
10847again:
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010848 rcu_read_lock();
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010849 pmu = idr_find(&pmu_idr, type);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010850 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +080010851 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010852 ret = perf_try_init_event(pmu, event);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010853 if (ret == -ENOENT && event->attr.type != type) {
10854 type = event->attr.type;
10855 goto again;
10856 }
10857
Lin Ming940c5b22011-02-27 21:13:31 +080010858 if (ret)
10859 pmu = ERR_PTR(ret);
Peter Zijlstra66d258c2019-10-17 20:31:03 +020010860
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010861 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +080010862 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +010010863
Sebastian Andrzej Siewior9f0bff12019-11-19 13:14:29 +010010864 list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) {
Mark Rutlandcc34b982015-01-07 14:56:51 +000010865 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010866 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010867 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020010868
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010869 if (ret != -ENOENT) {
10870 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010871 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010872 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010873 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +020010874 pmu = ERR_PTR(-ENOENT);
10875unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020010876 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010877
10878 return pmu;
10879}
10880
Kan Liangf2fb6be2016-03-23 11:24:37 -070010881static void attach_sb_event(struct perf_event *event)
10882{
10883 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu);
10884
10885 raw_spin_lock(&pel->lock);
10886 list_add_rcu(&event->sb_list, &pel->list);
10887 raw_spin_unlock(&pel->lock);
10888}
10889
Peter Zijlstraaab5b712016-05-12 17:26:46 +020010890/*
10891 * We keep a list of all !task (and therefore per-cpu) events
10892 * that need to receive side-band records.
10893 *
10894 * This avoids having to scan all the various PMU per-cpu contexts
10895 * looking for them.
10896 */
Kan Liangf2fb6be2016-03-23 11:24:37 -070010897static void account_pmu_sb_event(struct perf_event *event)
10898{
David Carrillo-Cisnerosa4f144e2016-06-01 12:33:05 -070010899 if (is_sb_event(event))
Kan Liangf2fb6be2016-03-23 11:24:37 -070010900 attach_sb_event(event);
10901}
10902
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010903static void account_event_cpu(struct perf_event *event, int cpu)
10904{
10905 if (event->parent)
10906 return;
10907
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010908 if (is_cgroup_event(event))
10909 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
10910}
10911
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020010912/* Freq events need the tick to stay alive (see perf_event_task_tick). */
10913static void account_freq_event_nohz(void)
10914{
10915#ifdef CONFIG_NO_HZ_FULL
10916 /* Lock so we don't race with concurrent unaccount */
10917 spin_lock(&nr_freq_lock);
10918 if (atomic_inc_return(&nr_freq_events) == 1)
10919 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
10920 spin_unlock(&nr_freq_lock);
10921#endif
10922}
10923
10924static void account_freq_event(void)
10925{
10926 if (tick_nohz_full_enabled())
10927 account_freq_event_nohz();
10928 else
10929 atomic_inc(&nr_freq_events);
10930}
10931
10932
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010933static void account_event(struct perf_event *event)
10934{
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010935 bool inc = false;
10936
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010937 if (event->parent)
10938 return;
10939
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010940 if (event->attach_state & PERF_ATTACH_TASK)
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010941 inc = true;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010942 if (event->attr.mmap || event->attr.mmap_data)
10943 atomic_inc(&nr_mmap_events);
10944 if (event->attr.comm)
10945 atomic_inc(&nr_comm_events);
Hari Bathinie4222672017-03-08 02:11:36 +053010946 if (event->attr.namespaces)
10947 atomic_inc(&nr_namespaces_events);
Namhyung Kim96aaab62020-03-25 21:45:28 +090010948 if (event->attr.cgroup)
10949 atomic_inc(&nr_cgroup_events);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010950 if (event->attr.task)
10951 atomic_inc(&nr_task_events);
Frederic Weisbecker555e0c12015-07-16 17:42:29 +020010952 if (event->attr.freq)
10953 account_freq_event();
Adrian Hunter45ac1402015-07-21 12:44:02 +030010954 if (event->attr.context_switch) {
10955 atomic_inc(&nr_switch_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010956 inc = true;
Adrian Hunter45ac1402015-07-21 12:44:02 +030010957 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010958 if (has_branch_stack(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010959 inc = true;
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010960 if (is_cgroup_event(event))
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010961 inc = true;
Song Liu76193a92019-01-17 08:15:13 -080010962 if (event->attr.ksymbol)
10963 atomic_inc(&nr_ksymbol_events);
Song Liu6ee52e22019-01-17 08:15:15 -080010964 if (event->attr.bpf_event)
10965 atomic_inc(&nr_bpf_events);
Peter Zijlstra25432ae2016-01-08 11:05:09 +010010966
Peter Zijlstra9107c892016-02-24 18:45:45 +010010967 if (inc) {
Alexander Shishkin5bce9db2017-08-29 17:01:03 +030010968 /*
10969 * We need the mutex here because static_branch_enable()
10970 * must complete *before* the perf_sched_count increment
10971 * becomes visible.
10972 */
Peter Zijlstra9107c892016-02-24 18:45:45 +010010973 if (atomic_inc_not_zero(&perf_sched_count))
10974 goto enabled;
10975
10976 mutex_lock(&perf_sched_mutex);
10977 if (!atomic_read(&perf_sched_count)) {
10978 static_branch_enable(&perf_sched_events);
10979 /*
10980 * Guarantee that all CPUs observe they key change and
10981 * call the perf scheduling hooks before proceeding to
10982 * install events that need them.
10983 */
Paul E. McKenney0809d9542018-11-06 19:20:05 -080010984 synchronize_rcu();
Peter Zijlstra9107c892016-02-24 18:45:45 +010010985 }
10986 /*
10987 * Now that we have waited for the sync_sched(), allow further
10988 * increments to by-pass the mutex.
10989 */
10990 atomic_inc(&perf_sched_count);
10991 mutex_unlock(&perf_sched_mutex);
10992 }
10993enabled:
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010994
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +020010995 account_event_cpu(event, event->cpu);
Kan Liangf2fb6be2016-03-23 11:24:37 -070010996
10997 account_pmu_sb_event(event);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +020010998}
10999
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011000/*
Tobias Tefke788faab2018-07-09 12:57:15 +020011001 * Allocate and initialize an event structure
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011002 */
11003static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011004perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011005 struct task_struct *task,
11006 struct perf_event *group_leader,
11007 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +030011008 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +000011009 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011010{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +020011011 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011012 struct perf_event *event;
11013 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011014 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011015
Oleg Nesterov66832eb2011-01-18 17:10:32 +010011016 if ((unsigned)cpu >= nr_cpu_ids) {
11017 if (!task || cpu != -1)
11018 return ERR_PTR(-EINVAL);
11019 }
11020
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011021 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011022 if (!event)
11023 return ERR_PTR(-ENOMEM);
11024
11025 /*
11026 * Single events are their own group leaders, with an
11027 * empty sibling list:
11028 */
11029 if (!group_leader)
11030 group_leader = event;
11031
11032 mutex_init(&event->child_mutex);
11033 INIT_LIST_HEAD(&event->child_list);
11034
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011035 INIT_LIST_HEAD(&event->event_entry);
11036 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra66681282017-11-13 14:28:38 +010011037 INIT_LIST_HEAD(&event->active_list);
Alexey Budankov8e1a2032017-09-08 11:47:03 +030011038 init_event_group(event);
Peter Zijlstra10c6db12011-11-26 02:47:31 +010011039 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +010011040 INIT_LIST_HEAD(&event->active_entry);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011041 INIT_LIST_HEAD(&event->addr_filters.list);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +010011042 INIT_HLIST_NODE(&event->hlist_entry);
11043
Peter Zijlstra10c6db12011-11-26 02:47:31 +010011044
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011045 init_waitqueue_head(&event->waitq);
Peter Zijlstra1d54ad92019-04-04 15:03:00 +020011046 event->pending_disable = -1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +080011047 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011048
11049 mutex_init(&event->mmap_mutex);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011050 raw_spin_lock_init(&event->addr_filters.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011051
Al Viroa6fa9412012-08-20 14:59:25 +010011052 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011053 event->cpu = cpu;
11054 event->attr = *attr;
11055 event->group_leader = group_leader;
11056 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011057 event->oncpu = -1;
11058
11059 event->parent = parent_event;
11060
Eric W. Biederman17cf22c2010-03-02 14:51:53 -080011061 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011062 event->id = atomic64_inc_return(&perf_event_id);
11063
11064 event->state = PERF_EVENT_STATE_INACTIVE;
11065
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011066 if (task) {
11067 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011068 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +010011069 * XXX pmu::event_init needs to know what task to account to
11070 * and we cannot use the ctx information because we need the
11071 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011072 */
Matthew Wilcox (Oracle)7b3c92b2019-07-04 15:13:23 -070011073 event->hw.target = get_task_struct(task);
Peter Zijlstrad580ff82010-10-14 17:43:23 +020011074 }
11075
Peter Zijlstra34f43922015-02-20 14:05:38 +010011076 event->clock = &local_clock;
11077 if (parent_event)
11078 event->clock = parent_event->clock;
11079
Avi Kivity4dc0da82011-06-29 18:42:35 +030011080 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +010011081 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030011082 context = parent_event->overflow_handler_context;
Arnd Bergmannf1e4ba52016-09-06 15:10:22 +020011083#if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING)
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011084 if (overflow_handler == bpf_overflow_handler) {
Andrii Nakryiko85192db2019-11-17 09:28:03 -080011085 struct bpf_prog *prog = parent_event->prog;
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011086
Andrii Nakryiko85192db2019-11-17 09:28:03 -080011087 bpf_prog_inc(prog);
Alexei Starovoitovaa6a5f32016-09-01 18:37:24 -070011088 event->prog = prog;
11089 event->orig_overflow_handler =
11090 parent_event->orig_overflow_handler;
11091 }
11092#endif
Avi Kivity4dc0da82011-06-29 18:42:35 +030011093 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +010011094
Wang Nan18794452016-03-28 06:41:30 +000011095 if (overflow_handler) {
11096 event->overflow_handler = overflow_handler;
11097 event->overflow_handler_context = context;
Wang Nan9ecda412016-04-05 14:11:18 +000011098 } else if (is_write_backward(event)){
11099 event->overflow_handler = perf_event_output_backward;
11100 event->overflow_handler_context = NULL;
Wang Nan18794452016-03-28 06:41:30 +000011101 } else {
Wang Nan9ecda412016-04-05 14:11:18 +000011102 event->overflow_handler = perf_event_output_forward;
Wang Nan18794452016-03-28 06:41:30 +000011103 event->overflow_handler_context = NULL;
11104 }
Frederic Weisbecker97eaf532009-10-18 15:33:50 +020011105
Jiri Olsa0231bb52013-02-01 11:23:45 +010011106 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011107
11108 pmu = NULL;
11109
11110 hwc = &event->hw;
11111 hwc->sample_period = attr->sample_period;
11112 if (attr->freq && attr->sample_freq)
11113 hwc->sample_period = 1;
11114 hwc->last_period = hwc->sample_period;
11115
Peter Zijlstrae7850592010-05-21 14:43:08 +020011116 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011117
11118 /*
Peter Zijlstraba5213a2017-05-30 11:45:12 +020011119 * We currently do not support PERF_SAMPLE_READ on inherited events.
11120 * See perf_output_read().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011121 */
Peter Zijlstraba5213a2017-05-30 11:45:12 +020011122 if (attr->inherit && (attr->sample_type & PERF_SAMPLE_READ))
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011123 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011124
Yan, Zhenga46a2302014-11-04 21:56:06 -050011125 if (!has_branch_stack(event))
11126 event->attr.branch_sample_type = 0;
11127
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020011128 pmu = perf_init_event(event);
Dan Carpenter85c617a2017-05-22 12:03:49 +030011129 if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011130 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011131 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011132 }
11133
Peter Zijlstra09f4e8f2019-11-06 12:51:04 +010011134 /*
11135 * Disallow uncore-cgroup events, they don't make sense as the cgroup will
11136 * be different on other CPUs in the uncore mask.
11137 */
11138 if (pmu->task_ctx_nr == perf_invalid_context && cgroup_fd != -1) {
11139 err = -EINVAL;
11140 goto err_pmu;
11141 }
11142
Alexander Shishkinab437622019-08-06 11:46:00 +030011143 if (event->attr.aux_output &&
11144 !(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT)) {
11145 err = -EOPNOTSUPP;
11146 goto err_pmu;
11147 }
11148
Peter Zijlstra98add2a2020-02-13 23:51:28 -080011149 if (cgroup_fd != -1) {
11150 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
11151 if (err)
11152 goto err_pmu;
11153 }
11154
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011155 err = exclusive_event_init(event);
11156 if (err)
11157 goto err_pmu;
11158
Alexander Shishkin375637b2016-04-27 18:44:46 +030011159 if (has_addr_filter(event)) {
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011160 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters,
11161 sizeof(struct perf_addr_filter_range),
11162 GFP_KERNEL);
11163 if (!event->addr_filter_ranges) {
Dan Carpenter36cc2b92017-05-22 12:04:18 +030011164 err = -ENOMEM;
Alexander Shishkin375637b2016-04-27 18:44:46 +030011165 goto err_per_task;
Dan Carpenter36cc2b92017-05-22 12:04:18 +030011166 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030011167
Alexander Shishkin18736ee2019-02-15 13:56:54 +020011168 /*
11169 * Clone the parent's vma offsets: they are valid until exec()
11170 * even if the mm is not shared with the parent.
11171 */
11172 if (event->parent) {
11173 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
11174
11175 raw_spin_lock_irq(&ifh->lock);
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011176 memcpy(event->addr_filter_ranges,
11177 event->parent->addr_filter_ranges,
11178 pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range));
Alexander Shishkin18736ee2019-02-15 13:56:54 +020011179 raw_spin_unlock_irq(&ifh->lock);
11180 }
11181
Alexander Shishkin375637b2016-04-27 18:44:46 +030011182 /* force hw sync on the address filters */
11183 event->addr_filters_gen = 1;
11184 }
11185
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011186 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +020011187 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
Arnaldo Carvalho de Melo97c79a32016-04-28 13:16:33 -030011188 err = get_callchain_buffers(attr->sample_max_stack);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011189 if (err)
Alexander Shishkin375637b2016-04-27 18:44:46 +030011190 goto err_addr_filters;
Stephane Eraniand010b332012-02-09 23:21:00 +010011191 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011192 }
11193
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011194 err = security_perf_event_alloc(event);
11195 if (err)
11196 goto err_callchain_buffer;
11197
Alexander Shishkin927a5572016-03-02 13:24:14 +020011198 /* symmetric to unaccount_event() in _free_event() */
11199 account_event(event);
11200
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011201 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011202
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011203err_callchain_buffer:
11204 if (!event->parent) {
11205 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
11206 put_callchain_buffers();
11207 }
Alexander Shishkin375637b2016-04-27 18:44:46 +030011208err_addr_filters:
Alexander Shishkinc60f83b2019-02-15 13:56:55 +020011209 kfree(event->addr_filter_ranges);
Alexander Shishkin375637b2016-04-27 18:44:46 +030011210
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011211err_per_task:
11212 exclusive_event_destroy(event);
11213
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011214err_pmu:
Peter Zijlstra98add2a2020-02-13 23:51:28 -080011215 if (is_cgroup_event(event))
11216 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011217 if (event->destroy)
11218 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +080011219 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011220err_ns:
11221 if (event->ns)
11222 put_pid_ns(event->ns);
Prashant Bhole621b6d22018-04-09 19:03:46 +090011223 if (event->hw.target)
11224 put_task_struct(event->hw.target);
Frederic Weisbecker90983b12013-07-23 02:31:00 +020011225 kfree(event);
11226
11227 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011228}
11229
11230static int perf_copy_attr(struct perf_event_attr __user *uattr,
11231 struct perf_event_attr *attr)
11232{
11233 u32 size;
11234 int ret;
11235
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011236 /* Zero the full structure, so that a short copy will be nice. */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011237 memset(attr, 0, sizeof(*attr));
11238
11239 ret = get_user(size, &uattr->size);
11240 if (ret)
11241 return ret;
11242
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011243 /* ABI compatibility quirk: */
11244 if (!size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011245 size = PERF_ATTR_SIZE_VER0;
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011246 if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011247 goto err_size;
11248
Aleksa Saraic2ba8f42019-10-01 11:10:55 +100011249 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
11250 if (ret) {
11251 if (ret == -E2BIG)
11252 goto err_size;
11253 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011254 }
11255
Meng Xuf12f42a2017-08-23 17:07:50 -040011256 attr->size = size;
11257
Alexander Shishkina4faf002019-10-25 17:08:33 +030011258 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011259 return -EINVAL;
11260
11261 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
11262 return -EINVAL;
11263
11264 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
11265 return -EINVAL;
11266
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011267 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
11268 u64 mask = attr->branch_sample_type;
11269
11270 /* only using defined bits */
11271 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
11272 return -EINVAL;
11273
11274 /* at least one branch bit must be set */
11275 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
11276 return -EINVAL;
11277
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011278 /* propagate priv level, when not set for branch */
11279 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
11280
11281 /* exclude_kernel checked on syscall entry */
11282 if (!attr->exclude_kernel)
11283 mask |= PERF_SAMPLE_BRANCH_KERNEL;
11284
11285 if (!attr->exclude_user)
11286 mask |= PERF_SAMPLE_BRANCH_USER;
11287
11288 if (!attr->exclude_hv)
11289 mask |= PERF_SAMPLE_BRANCH_HV;
11290 /*
11291 * adjust user setting (for HW filter setup)
11292 */
11293 attr->branch_sample_type = mask;
11294 }
Stephane Eraniane7122092013-06-06 11:02:04 +020011295 /* privileged levels capture (kernel, hv): check permissions */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011296 if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
11297 ret = perf_allow_kernel(attr);
11298 if (ret)
11299 return ret;
11300 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +010011301 }
Jiri Olsa40189942012-08-07 15:20:37 +020011302
Jiri Olsac5ebced2012-08-07 15:20:40 +020011303 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +020011304 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +020011305 if (ret)
11306 return ret;
11307 }
11308
11309 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
11310 if (!arch_perf_have_user_stack_dump())
11311 return -ENOSYS;
11312
11313 /*
11314 * We have __u32 type for the size, but so far
11315 * we can only use __u16 as maximum due to the
11316 * __u16 sample size limit.
11317 */
11318 if (attr->sample_stack_user >= USHRT_MAX)
Jiri Olsa78b562f2018-04-15 11:23:50 +020011319 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020011320 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
Jiri Olsa78b562f2018-04-15 11:23:50 +020011321 return -EINVAL;
Jiri Olsac5ebced2012-08-07 15:20:40 +020011322 }
Jiri Olsa40189942012-08-07 15:20:37 +020011323
Jiri Olsa5f970522018-03-12 14:45:46 +010011324 if (!attr->sample_max_stack)
11325 attr->sample_max_stack = sysctl_perf_event_max_stack;
11326
Stephane Eranian60e23642014-09-24 13:48:37 +020011327 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
11328 ret = perf_reg_validate(attr->sample_regs_intr);
Namhyung Kim6546b192020-03-25 21:45:29 +090011329
11330#ifndef CONFIG_CGROUP_PERF
11331 if (attr->sample_type & PERF_SAMPLE_CGROUP)
11332 return -EINVAL;
11333#endif
11334
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011335out:
11336 return ret;
11337
11338err_size:
11339 put_user(sizeof(*attr), &uattr->size);
11340 ret = -E2BIG;
11341 goto out;
11342}
11343
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011344static int
11345perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011346{
Steven Rostedt (VMware)56de4e82019-12-13 13:21:30 -050011347 struct perf_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011348 int ret = -EINVAL;
11349
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011350 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011351 goto set;
11352
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011353 /* don't allow circular references */
11354 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011355 goto out;
11356
Peter Zijlstra0f139302010-05-20 14:35:15 +020011357 /*
11358 * Don't allow cross-cpu buffers
11359 */
11360 if (output_event->cpu != event->cpu)
11361 goto out;
11362
11363 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +020011364 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +020011365 */
11366 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
11367 goto out;
11368
Peter Zijlstra34f43922015-02-20 14:05:38 +010011369 /*
11370 * Mixing clocks in the same buffer is trouble you don't need.
11371 */
11372 if (output_event->clock != event->clock)
11373 goto out;
11374
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011375 /*
Wang Nan9ecda412016-04-05 14:11:18 +000011376 * Either writing ring buffer from beginning or from end.
11377 * Mixing is not allowed.
11378 */
11379 if (is_write_backward(output_event) != is_write_backward(event))
11380 goto out;
11381
11382 /*
Peter Zijlstra45bfb2e2015-01-14 14:18:11 +020011383 * If both events generate aux data, they must be on the same PMU
11384 */
11385 if (has_aux(event) && has_aux(output_event) &&
11386 event->pmu != output_event->pmu)
11387 goto out;
11388
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011389set:
11390 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011391 /* Can't redirect output if we've got an active mmap() */
11392 if (atomic_read(&event->mmap_count))
11393 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011394
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011395 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +020011396 /* get the rb we want to redirect to */
11397 rb = ring_buffer_get(output_event);
11398 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011399 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011400 }
11401
Peter Zijlstrab69cf532014-03-14 10:50:33 +010011402 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +020011403
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011404 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011405unlock:
11406 mutex_unlock(&event->mmap_mutex);
11407
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011408out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011409 return ret;
11410}
11411
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011412static void mutex_lock_double(struct mutex *a, struct mutex *b)
11413{
11414 if (b < a)
11415 swap(a, b);
11416
11417 mutex_lock(a);
11418 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
11419}
11420
Peter Zijlstra34f43922015-02-20 14:05:38 +010011421static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
11422{
11423 bool nmi_safe = false;
11424
11425 switch (clk_id) {
11426 case CLOCK_MONOTONIC:
11427 event->clock = &ktime_get_mono_fast_ns;
11428 nmi_safe = true;
11429 break;
11430
11431 case CLOCK_MONOTONIC_RAW:
11432 event->clock = &ktime_get_raw_fast_ns;
11433 nmi_safe = true;
11434 break;
11435
11436 case CLOCK_REALTIME:
11437 event->clock = &ktime_get_real_ns;
11438 break;
11439
11440 case CLOCK_BOOTTIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011441 event->clock = &ktime_get_boottime_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011442 break;
11443
11444 case CLOCK_TAI:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +020011445 event->clock = &ktime_get_clocktai_ns;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011446 break;
11447
11448 default:
11449 return -EINVAL;
11450 }
11451
11452 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
11453 return -EINVAL;
11454
11455 return 0;
11456}
11457
Peter Zijlstra321027c2017-01-11 21:09:50 +010011458/*
11459 * Variation on perf_event_ctx_lock_nested(), except we take two context
11460 * mutexes.
11461 */
11462static struct perf_event_context *
11463__perf_event_ctx_lock_double(struct perf_event *group_leader,
11464 struct perf_event_context *ctx)
11465{
11466 struct perf_event_context *gctx;
11467
11468again:
11469 rcu_read_lock();
11470 gctx = READ_ONCE(group_leader->ctx);
Elena Reshetova8c94abb2019-01-28 14:27:26 +020011471 if (!refcount_inc_not_zero(&gctx->refcount)) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011472 rcu_read_unlock();
11473 goto again;
11474 }
11475 rcu_read_unlock();
11476
11477 mutex_lock_double(&gctx->mutex, &ctx->mutex);
11478
11479 if (group_leader->ctx != gctx) {
11480 mutex_unlock(&ctx->mutex);
11481 mutex_unlock(&gctx->mutex);
11482 put_ctx(gctx);
11483 goto again;
11484 }
11485
11486 return gctx;
11487}
11488
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011489/**
11490 * sys_perf_event_open - open a performance event, associate it to a task/cpu
11491 *
11492 * @attr_uptr: event_id type attributes for monitoring/sampling
11493 * @pid: target pid
11494 * @cpu: target cpu
11495 * @group_fd: group leader event fd
11496 */
11497SYSCALL_DEFINE5(perf_event_open,
11498 struct perf_event_attr __user *, attr_uptr,
11499 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
11500{
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011501 struct perf_event *group_leader = NULL, *output_event = NULL;
11502 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011503 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011504 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011505 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -040011506 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -070011507 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011508 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -040011509 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011510 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011511 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +010011512 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +000011513 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011514
11515 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +020011516 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011517 return -EINVAL;
11518
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011519 /* Do we allow access to perf_event_open(2) ? */
11520 err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
11521 if (err)
11522 return err;
11523
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011524 err = perf_copy_attr(attr_uptr, &attr);
11525 if (err)
11526 return err;
11527
11528 if (!attr.exclude_kernel) {
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011529 err = perf_allow_kernel(&attr);
11530 if (err)
11531 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011532 }
11533
Hari Bathinie4222672017-03-08 02:11:36 +053011534 if (attr.namespaces) {
Alexey Budankov18aa1852020-04-02 11:46:24 +030011535 if (!perfmon_capable())
Hari Bathinie4222672017-03-08 02:11:36 +053011536 return -EACCES;
11537 }
11538
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011539 if (attr.freq) {
11540 if (attr.sample_freq > sysctl_perf_event_sample_rate)
11541 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +020011542 } else {
11543 if (attr.sample_period & (1ULL << 63))
11544 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011545 }
11546
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011547 /* Only privileged users can get physical addresses */
Joel Fernandes (Google)da97e182019-10-14 13:03:08 -040011548 if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
11549 err = perf_allow_kernel(&attr);
11550 if (err)
11551 return err;
11552 }
Kan Liangfc7ce9c2017-08-28 20:52:49 -040011553
David Howellsb0c8fdc2019-08-19 17:18:00 -070011554 err = security_locked_down(LOCKDOWN_PERF);
11555 if (err && (attr.sample_type & PERF_SAMPLE_REGS_INTR))
11556 /* REGS_INTR can leak data, lockdown must prevent this */
11557 return err;
11558
11559 err = 0;
11560
Stephane Eraniane5d13672011-02-14 11:20:01 +020011561 /*
11562 * In cgroup mode, the pid argument is used to pass the fd
11563 * opened to the cgroup directory in cgroupfs. The cpu argument
11564 * designates the cpu on which to monitor threads from that
11565 * cgroup.
11566 */
11567 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
11568 return -EINVAL;
11569
Yann Droneauda21b0b32014-01-05 21:36:33 +010011570 if (flags & PERF_FLAG_FD_CLOEXEC)
11571 f_flags |= O_CLOEXEC;
11572
11573 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011574 if (event_fd < 0)
11575 return event_fd;
11576
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011577 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -040011578 err = perf_fget_light(group_fd, &group);
11579 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011580 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -040011581 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011582 if (flags & PERF_FLAG_FD_OUTPUT)
11583 output_event = group_leader;
11584 if (flags & PERF_FLAG_FD_NO_GROUP)
11585 group_leader = NULL;
11586 }
11587
Stephane Eraniane5d13672011-02-14 11:20:01 +020011588 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011589 task = find_lively_task_by_vpid(pid);
11590 if (IS_ERR(task)) {
11591 err = PTR_ERR(task);
11592 goto err_group_fd;
11593 }
11594 }
11595
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020011596 if (task && group_leader &&
11597 group_leader->attr.inherit != attr.inherit) {
11598 err = -EINVAL;
11599 goto err_task;
11600 }
11601
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011602 if (task) {
Bernd Edlinger69143032020-03-20 21:27:55 +010011603 err = mutex_lock_interruptible(&task->signal->exec_update_mutex);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011604 if (err)
Alexander Levine5aeee52017-06-03 03:39:13 +000011605 goto err_task;
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011606
11607 /*
11608 * Reuse ptrace permission checks for now.
11609 *
Bernd Edlinger69143032020-03-20 21:27:55 +010011610 * We must hold exec_update_mutex across this and any potential
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011611 * perf_install_in_context() call for this new event to
11612 * serialize against exec() altering our credentials (and the
11613 * perf_event_exit_task() that could imply).
11614 */
11615 err = -EACCES;
11616 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
11617 goto err_cred;
11618 }
11619
Matt Fleming79dff512015-01-23 18:45:42 +000011620 if (flags & PERF_FLAG_PID_CGROUP)
11621 cgroup_fd = pid;
11622
Avi Kivity4dc0da82011-06-29 18:42:35 +030011623 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000011624 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011625 if (IS_ERR(event)) {
11626 err = PTR_ERR(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011627 goto err_cred;
Stephane Eraniand14b12d2010-09-17 11:28:47 +020011628 }
11629
Vince Weaver53b25332014-05-16 17:12:12 -040011630 if (is_sampling_event(event)) {
11631 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
Vineet Guptaa1396552016-05-09 15:07:40 +053011632 err = -EOPNOTSUPP;
Vince Weaver53b25332014-05-16 17:12:12 -040011633 goto err_alloc;
11634 }
11635 }
11636
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011637 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011638 * Special case software events and allow them to be part of
11639 * any hardware group.
11640 */
11641 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011642
Peter Zijlstra34f43922015-02-20 14:05:38 +010011643 if (attr.use_clockid) {
11644 err = perf_event_set_clock(event, attr.clockid);
11645 if (err)
11646 goto err_alloc;
11647 }
11648
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011649 if (pmu->task_ctx_nr == perf_sw_context)
11650 event->event_caps |= PERF_EV_CAP_SOFTWARE;
11651
Song Liua1150c22018-05-03 12:47:16 -070011652 if (group_leader) {
11653 if (is_software_event(event) &&
11654 !in_software_context(group_leader)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011655 /*
Song Liua1150c22018-05-03 12:47:16 -070011656 * If the event is a sw event, but the group_leader
11657 * is on hw context.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011658 *
Song Liua1150c22018-05-03 12:47:16 -070011659 * Allow the addition of software events to hw
11660 * groups, this is safe because software events
11661 * never fail to schedule.
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011662 */
Song Liua1150c22018-05-03 12:47:16 -070011663 pmu = group_leader->ctx->pmu;
11664 } else if (!is_software_event(event) &&
11665 is_software_event(group_leader) &&
David Carrillo-Cisneros4ff6a8d2016-08-17 13:55:05 -070011666 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011667 /*
11668 * In case the group is a pure software group, and we
11669 * try to add a hardware event, move the whole group to
11670 * the hardware context.
11671 */
11672 move_group = 1;
11673 }
11674 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011675
11676 /*
11677 * Get the target context (task or percpu):
11678 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050011679 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011680 if (IS_ERR(ctx)) {
11681 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011682 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011683 }
11684
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011685 /*
11686 * Look up the group leader (we will attach this event to it):
11687 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011688 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011689 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011690
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011691 /*
11692 * Do not allow a recursive hierarchy (this new sibling
11693 * becoming part of another group-sibling):
11694 */
11695 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011696 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +010011697
11698 /* All events in a group should have the same clock */
11699 if (group_leader->clock != event->clock)
11700 goto err_context;
11701
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011702 /*
Mark Rutland64aee2a2017-06-22 15:41:38 +010011703 * Make sure we're both events for the same CPU;
11704 * grouping events for different CPUs is broken; since
11705 * you can never concurrently schedule them anyhow.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011706 */
Mark Rutland64aee2a2017-06-22 15:41:38 +010011707 if (group_leader->cpu != event->cpu)
11708 goto err_context;
Peter Zijlstrac3c87e72015-01-23 11:19:48 +010011709
Mark Rutland64aee2a2017-06-22 15:41:38 +010011710 /*
11711 * Make sure we're both on the same task, or both
11712 * per-CPU events.
11713 */
11714 if (group_leader->ctx->task != ctx->task)
11715 goto err_context;
11716
11717 /*
11718 * Do not allow to attach to a group in a different task
11719 * or CPU context. If we're moving SW events, we'll fix
11720 * this up later, so allow that.
11721 */
11722 if (!move_group && group_leader->ctx != ctx)
11723 goto err_context;
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011724
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011725 /*
11726 * Only a group leader can be exclusive or pinned
11727 */
11728 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011729 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011730 }
11731
11732 if (output_event) {
11733 err = perf_event_set_output(event, output_event);
11734 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011735 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +020011736 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011737
Yann Droneauda21b0b32014-01-05 21:36:33 +010011738 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
11739 f_flags);
Al Viroea635c62010-05-26 17:40:29 -040011740 if (IS_ERR(event_file)) {
11741 err = PTR_ERR(event_file);
Alexander Shishkin201c2f82016-03-21 10:02:42 +020011742 event_file = NULL;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011743 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -040011744 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011745
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011746 if (move_group) {
Peter Zijlstra321027c2017-01-11 21:09:50 +010011747 gctx = __perf_event_ctx_lock_double(group_leader, ctx);
11748
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011749 if (gctx->task == TASK_TOMBSTONE) {
11750 err = -ESRCH;
11751 goto err_locked;
11752 }
Peter Zijlstra321027c2017-01-11 21:09:50 +010011753
11754 /*
11755 * Check if we raced against another sys_perf_event_open() call
11756 * moving the software group underneath us.
11757 */
11758 if (!(group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) {
11759 /*
11760 * If someone moved the group out from under us, check
11761 * if this new event wound up on the same ctx, if so
11762 * its the regular !move_group case, otherwise fail.
11763 */
11764 if (gctx != ctx) {
11765 err = -EINVAL;
11766 goto err_locked;
11767 } else {
11768 perf_event_ctx_unlock(group_leader, gctx);
11769 move_group = 0;
11770 }
11771 }
Alexander Shishkin8a58dda2019-07-01 14:07:55 +030011772
11773 /*
11774 * Failure to create exclusive events returns -EBUSY.
11775 */
11776 err = -EBUSY;
11777 if (!exclusive_event_installable(group_leader, ctx))
11778 goto err_locked;
11779
11780 for_each_sibling_event(sibling, group_leader) {
11781 if (!exclusive_event_installable(sibling, ctx))
11782 goto err_locked;
11783 }
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011784 } else {
11785 mutex_lock(&ctx->mutex);
11786 }
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011787
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011788 if (ctx->task == TASK_TOMBSTONE) {
11789 err = -ESRCH;
11790 goto err_locked;
11791 }
11792
Peter Zijlstraa7239682015-09-09 19:06:33 +020011793 if (!perf_event_validate_size(event)) {
11794 err = -E2BIG;
11795 goto err_locked;
11796 }
11797
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011798 if (!task) {
11799 /*
11800 * Check if the @cpu we're creating an event for is online.
11801 *
11802 * We use the perf_cpu_context::ctx::mutex to serialize against
11803 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
11804 */
11805 struct perf_cpu_context *cpuctx =
11806 container_of(ctx, struct perf_cpu_context, ctx);
11807
11808 if (!cpuctx->online) {
11809 err = -ENODEV;
11810 goto err_locked;
11811 }
11812 }
11813
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011814 if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) {
11815 err = -EINVAL;
Alexander Shishkinab437622019-08-06 11:46:00 +030011816 goto err_locked;
Mark Rutlandda9ec3d2020-01-06 12:03:39 +000011817 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011818
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011819 /*
11820 * Must be under the same ctx::mutex as perf_install_in_context(),
11821 * because we need to serialize with concurrent event creation.
11822 */
11823 if (!exclusive_event_installable(event, ctx)) {
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011824 err = -EBUSY;
11825 goto err_locked;
11826 }
11827
11828 WARN_ON_ONCE(ctx->parent_ctx);
11829
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011830 /*
11831 * This is the point on no return; we cannot fail hereafter. This is
11832 * where we start modifying current state.
11833 */
11834
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011835 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011836 /*
11837 * See perf_event_ctx_lock() for comments on the details
11838 * of swizzling perf_event::ctx.
11839 */
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011840 perf_remove_from_context(group_leader, 0);
Peter Zijlstra279b5162017-02-16 10:28:37 +010011841 put_ctx(gctx);
Jiri Olsa0231bb52013-02-01 11:23:45 +010011842
Peter Zijlstraedb39592018-03-15 17:36:56 +010011843 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010011844 perf_remove_from_context(sibling, 0);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011845 put_ctx(gctx);
11846 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011847
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011848 /*
11849 * Wait for everybody to stop referencing the events through
11850 * the old lists, before installing it on new lists.
11851 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080011852 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011853
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011854 /*
11855 * Install the group siblings before the group leader.
11856 *
11857 * Because a group leader will try and install the entire group
11858 * (through the sibling list, which is still in-tact), we can
11859 * end up with siblings installed in the wrong context.
11860 *
11861 * By installing siblings first we NO-OP because they're not
11862 * reachable through the group lists.
11863 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010011864 for_each_sibling_event(sibling, group_leader) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011865 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +010011866 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011867 get_ctx(ctx);
11868 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010011869
11870 /*
11871 * Removing from the context ends up with disabled
11872 * event. What we want here is event in the initial
11873 * startup state, ready to be add into new context.
11874 */
11875 perf_event__state_init(group_leader);
11876 perf_install_in_context(ctx, group_leader, group_leader->cpu);
11877 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +020011878 }
11879
Peter Zijlstraf73e22a2015-09-09 20:48:22 +020011880 /*
11881 * Precalculate sample_data sizes; do while holding ctx::mutex such
11882 * that we're serialized against further additions and before
11883 * perf_install_in_context() which is the point the event is active and
11884 * can use these values.
11885 */
11886 perf_event__header_size(event);
11887 perf_event__id_header_size(event);
Alexander Shishkinbed5b252015-01-30 12:31:06 +020011888
Peter Zijlstra78cd2c72016-01-25 14:08:45 +010011889 event->owner = current;
11890
Yan, Zhenge2d37cd2012-06-15 14:31:32 +080011891 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010011892 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010011893
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011894 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010011895 perf_event_ctx_unlock(group_leader, gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011896 mutex_unlock(&ctx->mutex);
11897
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011898 if (task) {
Bernd Edlinger69143032020-03-20 21:27:55 +010011899 mutex_unlock(&task->signal->exec_update_mutex);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011900 put_task_struct(task);
11901 }
11902
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011903 mutex_lock(&current->perf_event_mutex);
11904 list_add_tail(&event->owner_entry, &current->perf_event_list);
11905 mutex_unlock(&current->perf_event_mutex);
11906
Peter Zijlstra8a495422010-05-27 15:47:49 +020011907 /*
11908 * Drop the reference on the group_event after placing the
11909 * new event on the sibling_list. This ensures destruction
11910 * of the group leader will find the pointer to itself in
11911 * perf_group_detach().
11912 */
Al Viro2903ff02012-08-28 12:52:22 -040011913 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040011914 fd_install(event_fd, event_file);
11915 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011916
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011917err_locked:
11918 if (move_group)
Peter Zijlstra321027c2017-01-11 21:09:50 +010011919 perf_event_ctx_unlock(group_leader, gctx);
Peter Zijlstraf55fc2a2015-09-09 19:06:33 +020011920 mutex_unlock(&ctx->mutex);
11921/* err_file: */
11922 fput(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011923err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010011924 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -040011925 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +020011926err_alloc:
Peter Zijlstra13005622016-02-24 18:45:41 +010011927 /*
11928 * If event_file is set, the fput() above will have called ->release()
11929 * and that will take care of freeing the event.
11930 */
11931 if (!event_file)
11932 free_event(event);
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020011933err_cred:
11934 if (task)
Bernd Edlinger69143032020-03-20 21:27:55 +010011935 mutex_unlock(&task->signal->exec_update_mutex);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +020011936err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +020011937 if (task)
11938 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +020011939err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -040011940 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -040011941err_fd:
11942 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020011943 return err;
11944}
11945
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011946/**
11947 * perf_event_create_kernel_counter
11948 *
11949 * @attr: attributes of the counter to create
11950 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -070011951 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011952 */
11953struct perf_event *
11954perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -070011955 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +030011956 perf_overflow_handler_t overflow_handler,
11957 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011958{
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011959 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011960 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011961 int err;
11962
Alexander Shishkindce5aff2019-10-30 15:47:31 +020011963 /*
11964 * Grouping is not supported for kernel events, neither is 'AUX',
11965 * make sure the caller's intentions are adjusted.
11966 */
11967 if (attr->aux_output)
11968 return ERR_PTR(-EINVAL);
11969
Avi Kivity4dc0da82011-06-29 18:42:35 +030011970 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +000011971 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010011972 if (IS_ERR(event)) {
11973 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011974 goto err;
11975 }
11976
Jiri Olsaf8697762014-08-01 14:33:01 +020011977 /* Mark owner so we could distinguish it from user events. */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010011978 event->owner = TASK_TOMBSTONE;
Jiri Olsaf8697762014-08-01 14:33:01 +020011979
Alexander Shishkinf25d8ba2019-10-30 15:47:30 +020011980 /*
11981 * Get the target context (task or percpu):
11982 */
Yan, Zheng4af57ef2014-11-04 21:56:01 -050011983 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011984 if (IS_ERR(ctx)) {
11985 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020011986 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010011987 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011988
Arjan van de Venfb0459d2009-09-25 12:25:56 +020011989 WARN_ON_ONCE(ctx->parent_ctx);
11990 mutex_lock(&ctx->mutex);
Peter Zijlstra84c4e622016-02-24 18:45:40 +010011991 if (ctx->task == TASK_TOMBSTONE) {
11992 err = -ESRCH;
11993 goto err_unlock;
11994 }
11995
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020011996 if (!task) {
11997 /*
11998 * Check if the @cpu we're creating an event for is online.
11999 *
12000 * We use the perf_cpu_context::ctx::mutex to serialize against
12001 * the hotplug notifiers. See perf_event_{init,exit}_cpu().
12002 */
12003 struct perf_cpu_context *cpuctx =
12004 container_of(ctx, struct perf_cpu_context, ctx);
12005 if (!cpuctx->online) {
12006 err = -ENODEV;
12007 goto err_unlock;
12008 }
12009 }
12010
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012011 if (!exclusive_event_installable(event, ctx)) {
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012012 err = -EBUSY;
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012013 goto err_unlock;
Alexander Shishkinbed5b252015-01-30 12:31:06 +020012014 }
12015
Leonard Crestez4ce54af2019-07-24 15:53:24 +030012016 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012017 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012018 mutex_unlock(&ctx->mutex);
12019
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012020 return event;
12021
Peter Zijlstra84c4e622016-02-24 18:45:40 +010012022err_unlock:
12023 mutex_unlock(&ctx->mutex);
12024 perf_unpin_context(ctx);
12025 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +020012026err_free:
12027 free_event(event);
12028err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +010012029 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +020012030}
12031EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
12032
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012033void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
12034{
12035 struct perf_event_context *src_ctx;
12036 struct perf_event_context *dst_ctx;
12037 struct perf_event *event, *tmp;
12038 LIST_HEAD(events);
12039
12040 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
12041 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
12042
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012043 /*
12044 * See perf_event_ctx_lock() for comments on the details
12045 * of swizzling perf_event::ctx.
12046 */
12047 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012048 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
12049 event_entry) {
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012050 perf_remove_from_context(event, 0);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020012051 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012052 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +020012053 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012054 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012055
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012056 /*
12057 * Wait for the events to quiesce before re-instating them.
12058 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012059 synchronize_rcu();
12060
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +010012061 /*
12062 * Re-instate events in 2 passes.
12063 *
12064 * Skip over group leaders and only install siblings on this first
12065 * pass, siblings will not get enabled without a leader, however a
12066 * leader will enable its siblings, even if those are still on the old
12067 * context.
12068 */
12069 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
12070 if (event->group_leader == event)
12071 continue;
12072
12073 list_del(&event->migrate_entry);
12074 if (event->state >= PERF_EVENT_STATE_OFF)
12075 event->state = PERF_EVENT_STATE_INACTIVE;
12076 account_event_cpu(event, dst_cpu);
12077 perf_install_in_context(dst_ctx, event, dst_cpu);
12078 get_ctx(dst_ctx);
12079 }
12080
12081 /*
12082 * Once all the siblings are setup properly, install the group leaders
12083 * to make it go.
12084 */
Peter Zijlstra98861672013-10-03 16:02:23 +020012085 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
12086 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012087 if (event->state >= PERF_EVENT_STATE_OFF)
12088 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +020012089 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012090 perf_install_in_context(dst_ctx, event, dst_cpu);
12091 get_ctx(dst_ctx);
12092 }
12093 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +010012094 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +080012095}
12096EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
12097
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012098static void sync_child_event(struct perf_event *child_event,
12099 struct task_struct *child)
12100{
12101 struct perf_event *parent_event = child_event->parent;
12102 u64 child_val;
12103
12104 if (child_event->attr.inherit_stat)
12105 perf_event_read_event(child_event, child);
12106
Peter Zijlstrab5e58792010-05-21 14:43:12 +020012107 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012108
12109 /*
12110 * Add back the child's count to the parent's count:
12111 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +020012112 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012113 atomic64_add(child_event->total_time_enabled,
12114 &parent_event->child_total_time_enabled);
12115 atomic64_add(child_event->total_time_running,
12116 &parent_event->child_total_time_running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012117}
12118
12119static void
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012120perf_event_exit_event(struct perf_event *child_event,
12121 struct perf_event_context *child_ctx,
12122 struct task_struct *child)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012123{
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012124 struct perf_event *parent_event = child_event->parent;
12125
Peter Zijlstra1903d502014-07-15 17:27:27 +020012126 /*
12127 * Do not destroy the 'original' grouping; because of the context
12128 * switch optimization the original events could've ended up in a
12129 * random child task.
12130 *
12131 * If we were to destroy the original group, all group related
12132 * operations would cease to function properly after this random
12133 * child dies.
12134 *
12135 * Do destroy all inherited groups, we don't care about those
12136 * and being thorough is better.
12137 */
Peter Zijlstra32132a32016-01-11 15:40:59 +010012138 raw_spin_lock_irq(&child_ctx->lock);
12139 WARN_ON_ONCE(child_ctx->is_active);
12140
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012141 if (parent_event)
Peter Zijlstra32132a32016-01-11 15:40:59 +010012142 perf_group_detach(child_event);
12143 list_del_event(child_event, child_ctx);
Peter Zijlstra0d3d73a2017-09-05 14:16:28 +020012144 perf_event_set_state(child_event, PERF_EVENT_STATE_EXIT); /* is_event_hup() */
Peter Zijlstra32132a32016-01-11 15:40:59 +010012145 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012146
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012147 /*
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012148 * Parent events are governed by their filedesc, retain them.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012149 */
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012150 if (!parent_event) {
Jiri Olsa179033b2014-08-07 11:48:26 -040012151 perf_event_wakeup(child_event);
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012152 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012153 }
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012154 /*
12155 * Child events can be cleaned up.
12156 */
12157
12158 sync_child_event(child_event, child);
12159
12160 /*
12161 * Remove this event from the parent's list
12162 */
12163 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
12164 mutex_lock(&parent_event->child_mutex);
12165 list_del_init(&child_event->child_list);
12166 mutex_unlock(&parent_event->child_mutex);
12167
12168 /*
12169 * Kick perf_poll() for is_event_hup().
12170 */
12171 perf_event_wakeup(parent_event);
12172 free_event(child_event);
12173 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012174}
12175
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012176static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012177{
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012178 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012179 struct perf_event *child_event, *next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012180
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012181 WARN_ON_ONCE(child != current);
12182
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012183 child_ctx = perf_pin_task_context(child, ctxn);
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012184 if (!child_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012185 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012186
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012187 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012188 * In order to reduce the amount of tricky in ctx tear-down, we hold
12189 * ctx::mutex over the entire thing. This serializes against almost
12190 * everything that wants to access the ctx.
12191 *
12192 * The exception is sys_perf_event_open() /
12193 * perf_event_create_kernel_count() which does find_get_context()
12194 * without ctx::mutex (it cannot because of the move_group double mutex
12195 * lock thing). See the comments in perf_install_in_context().
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012196 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012197 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012198
12199 /*
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012200 * In a single ctx::lock section, de-schedule the events and detach the
12201 * context from the task such that we cannot ever get it scheduled back
12202 * in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012203 */
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012204 raw_spin_lock_irq(&child_ctx->lock);
Alexander Shishkin487f05e2017-01-19 18:43:30 +020012205 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx, EVENT_ALL);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020012206
12207 /*
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012208 * Now that the context is inactive, destroy the task <-> ctx relation
12209 * and mark the context dead.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012210 */
Peter Zijlstra63b6da32016-01-14 16:05:37 +010012211 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
12212 put_ctx(child_ctx); /* cannot be last */
12213 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
12214 put_task_struct(current); /* cannot be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012215
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012216 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra6a3351b2016-01-25 14:09:54 +010012217 raw_spin_unlock_irq(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012218
Peter Zijlstra211de6e2014-09-30 19:23:08 +020012219 if (clone_ctx)
12220 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +020012221
12222 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012223 * Report the task dead after unscheduling the events so that we
12224 * won't get any samples after PERF_RECORD_EXIT. We can however still
12225 * get a few PERF_RECORD_READ events.
12226 */
12227 perf_event_task(child, child_ctx, 0);
12228
Peter Zijlstraebf905f2014-05-29 19:00:24 +020012229 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Peter Zijlstra8ba289b2016-01-26 13:06:56 +010012230 perf_event_exit_event(child_event, child_ctx, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012231
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012232 mutex_unlock(&child_ctx->mutex);
12233
12234 put_ctx(child_ctx);
12235}
12236
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012237/*
12238 * When a child task exits, feed back event values to parent events.
Peter Zijlstra79c9ce52016-04-26 11:36:53 +020012239 *
Bernd Edlinger69143032020-03-20 21:27:55 +010012240 * Can be called with exec_update_mutex held when called from
Eric W. Biederman96ecee22020-05-03 06:48:17 -050012241 * setup_new_exec().
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012242 */
12243void perf_event_exit_task(struct task_struct *child)
12244{
Peter Zijlstra88821352010-11-09 19:01:43 +010012245 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012246 int ctxn;
12247
Peter Zijlstra88821352010-11-09 19:01:43 +010012248 mutex_lock(&child->perf_event_mutex);
12249 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
12250 owner_entry) {
12251 list_del_init(&event->owner_entry);
12252
12253 /*
12254 * Ensure the list deletion is visible before we clear
12255 * the owner, closes a race against perf_release() where
12256 * we need to serialize on the owner->perf_event_mutex.
12257 */
Peter Zijlstraf47c02c2016-01-26 12:30:14 +010012258 smp_store_release(&event->owner, NULL);
Peter Zijlstra88821352010-11-09 19:01:43 +010012259 }
12260 mutex_unlock(&child->perf_event_mutex);
12261
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012262 for_each_task_context_nr(ctxn)
12263 perf_event_exit_task_context(child, ctxn);
Jiri Olsa4e93ad62015-11-04 16:00:05 +010012264
12265 /*
12266 * The perf_event_exit_task_context calls perf_event_task
12267 * with child's task_ctx, which generates EXIT events for
12268 * child contexts and sets child->perf_event_ctxp[] to NULL.
12269 * At this point we need to send EXIT events to cpu contexts.
12270 */
12271 perf_event_task(child, NULL, 0);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012272}
12273
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012274static void perf_free_event(struct perf_event *event,
12275 struct perf_event_context *ctx)
12276{
12277 struct perf_event *parent = event->parent;
12278
12279 if (WARN_ON_ONCE(!parent))
12280 return;
12281
12282 mutex_lock(&parent->child_mutex);
12283 list_del_init(&event->child_list);
12284 mutex_unlock(&parent->child_mutex);
12285
Al Viroa6fa9412012-08-20 14:59:25 +010012286 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012287
Peter Zijlstra652884f2015-01-23 11:20:10 +010012288 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +020012289 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012290 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +010012291 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012292 free_event(event);
12293}
12294
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012295/*
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012296 * Free a context as created by inheritance by perf_event_init_task() below,
12297 * used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +010012298 *
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012299 * Even though the task has never lived, the context and events have been
12300 * exposed through the child_list, so we must take care tearing it all down.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012301 */
12302void perf_event_free_task(struct task_struct *task)
12303{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012304 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012305 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012306 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012307
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012308 for_each_task_context_nr(ctxn) {
12309 ctx = task->perf_event_ctxp[ctxn];
12310 if (!ctx)
12311 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012312
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012313 mutex_lock(&ctx->mutex);
Peter Zijlstrae552a832017-03-16 13:47:48 +010012314 raw_spin_lock_irq(&ctx->lock);
12315 /*
12316 * Destroy the task <-> ctx relation and mark the context dead.
12317 *
12318 * This is important because even though the task hasn't been
12319 * exposed yet the context has been (through child_list).
12320 */
12321 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], NULL);
12322 WRITE_ONCE(ctx->task, TASK_TOMBSTONE);
12323 put_task_struct(task); /* cannot be last */
12324 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012325
Peter Zijlstra15121c72017-03-16 13:47:50 +010012326 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012327 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012328
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012329 mutex_unlock(&ctx->mutex);
Peter Zijlstra1cf8dfe2019-07-13 11:21:25 +020012330
12331 /*
12332 * perf_event_release_kernel() could've stolen some of our
12333 * child events and still have them on its free_list. In that
12334 * case we must wait for these events to have been freed (in
12335 * particular all their references to this task must've been
12336 * dropped).
12337 *
12338 * Without this copy_process() will unconditionally free this
12339 * task (irrespective of its reference count) and
12340 * _free_event()'s put_task_struct(event->hw.target) will be a
12341 * use-after-free.
12342 *
12343 * Wait for all events to drop their context reference.
12344 */
12345 wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1);
12346 put_ctx(ctx); /* must be last */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012347 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012348}
12349
Peter Zijlstra4e231c72010-09-09 21:01:59 +020012350void perf_event_delayed_put(struct task_struct *task)
12351{
12352 int ctxn;
12353
12354 for_each_task_context_nr(ctxn)
12355 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
12356}
12357
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012358struct file *perf_event_get(unsigned int fd)
Kaixu Xiaffe86902015-08-06 07:02:32 +000012359{
Al Viro02e5ad92019-06-26 20:43:53 -040012360 struct file *file = fget(fd);
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012361 if (!file)
12362 return ERR_PTR(-EBADF);
Kaixu Xiaffe86902015-08-06 07:02:32 +000012363
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012364 if (file->f_op != &perf_fops) {
12365 fput(file);
12366 return ERR_PTR(-EBADF);
12367 }
Kaixu Xiaffe86902015-08-06 07:02:32 +000012368
Alexei Starovoitove03e7ee2016-01-25 20:59:49 -080012369 return file;
Kaixu Xiaffe86902015-08-06 07:02:32 +000012370}
12371
Yonghong Songf8d959a2018-05-24 11:21:08 -070012372const struct perf_event *perf_get_event(struct file *file)
12373{
12374 if (file->f_op != &perf_fops)
12375 return ERR_PTR(-EINVAL);
12376
12377 return file->private_data;
12378}
12379
Kaixu Xiaffe86902015-08-06 07:02:32 +000012380const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
12381{
12382 if (!event)
12383 return ERR_PTR(-EINVAL);
12384
12385 return &event->attr;
12386}
12387
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012388/*
Tobias Tefke788faab2018-07-09 12:57:15 +020012389 * Inherit an event from parent task to child task.
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012390 *
12391 * Returns:
12392 * - valid pointer on success
12393 * - NULL for orphaned events
12394 * - IS_ERR() on error
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012395 */
12396static struct perf_event *
12397inherit_event(struct perf_event *parent_event,
12398 struct task_struct *parent,
12399 struct perf_event_context *parent_ctx,
12400 struct task_struct *child,
12401 struct perf_event *group_leader,
12402 struct perf_event_context *child_ctx)
12403{
Peter Zijlstra8ca2bd42017-09-05 14:12:35 +020012404 enum perf_event_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012405 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +020012406 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012407
12408 /*
12409 * Instead of creating recursive hierarchies of events,
12410 * we link inherited events back to the original parent,
12411 * which has a filp for sure, which we use as the reference
12412 * count:
12413 */
12414 if (parent_event->parent)
12415 parent_event = parent_event->parent;
12416
12417 child_event = perf_event_alloc(&parent_event->attr,
12418 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +020012419 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012420 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +000012421 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012422 if (IS_ERR(child_event))
12423 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +010012424
Jiri Olsa313ccb92018-01-07 17:03:47 +010012425
12426 if ((child_event->attach_state & PERF_ATTACH_TASK_DATA) &&
12427 !child_ctx->task_ctx_data) {
12428 struct pmu *pmu = child_event->pmu;
12429
12430 child_ctx->task_ctx_data = kzalloc(pmu->task_ctx_size,
12431 GFP_KERNEL);
12432 if (!child_ctx->task_ctx_data) {
12433 free_event(child_event);
Alexander Shishkin697d8772019-11-05 09:57:02 +020012434 return ERR_PTR(-ENOMEM);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012435 }
12436 }
12437
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012438 /*
12439 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
12440 * must be under the same lock in order to serialize against
12441 * perf_event_release_kernel(), such that either we must observe
12442 * is_orphaned_event() or they will observe us on the child_list.
12443 */
12444 mutex_lock(&parent_event->child_mutex);
Jiri Olsafadfe7b2014-08-01 14:33:02 +020012445 if (is_orphaned_event(parent_event) ||
12446 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Peter Zijlstrac6e5b732016-01-15 16:07:41 +020012447 mutex_unlock(&parent_event->child_mutex);
Jiri Olsa313ccb92018-01-07 17:03:47 +010012448 /* task_ctx_data is freed with child_ctx */
Al Viroa6fa9412012-08-20 14:59:25 +010012449 free_event(child_event);
12450 return NULL;
12451 }
12452
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012453 get_ctx(child_ctx);
12454
12455 /*
12456 * Make the child state follow the state of the parent event,
12457 * not its attr.disabled bit. We hold the parent's mutex,
12458 * so we won't race with perf_event_{en, dis}able_family.
12459 */
Jiri Olsa1929def2014-09-12 13:18:27 +020012460 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012461 child_event->state = PERF_EVENT_STATE_INACTIVE;
12462 else
12463 child_event->state = PERF_EVENT_STATE_OFF;
12464
12465 if (parent_event->attr.freq) {
12466 u64 sample_period = parent_event->hw.sample_period;
12467 struct hw_perf_event *hwc = &child_event->hw;
12468
12469 hwc->sample_period = sample_period;
12470 hwc->last_period = sample_period;
12471
12472 local64_set(&hwc->period_left, sample_period);
12473 }
12474
12475 child_event->ctx = child_ctx;
12476 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +030012477 child_event->overflow_handler_context
12478 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012479
12480 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -020012481 * Precalculate sample_data sizes
12482 */
12483 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -020012484 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -020012485
12486 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012487 * Link it up in the child's context:
12488 */
Peter Zijlstracee010e2010-09-10 12:51:54 +020012489 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012490 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +020012491 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012492
12493 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012494 * Link this into the parent event's child list
12495 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012496 list_add_tail(&child_event->child_list, &parent_event->child_list);
12497 mutex_unlock(&parent_event->child_mutex);
12498
12499 return child_event;
12500}
12501
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012502/*
12503 * Inherits an event group.
12504 *
12505 * This will quietly suppress orphaned events; !inherit_event() is not an error.
12506 * This matches with perf_event_release_kernel() removing all child events.
12507 *
12508 * Returns:
12509 * - 0 on success
12510 * - <0 on error
12511 */
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012512static int inherit_group(struct perf_event *parent_event,
12513 struct task_struct *parent,
12514 struct perf_event_context *parent_ctx,
12515 struct task_struct *child,
12516 struct perf_event_context *child_ctx)
12517{
12518 struct perf_event *leader;
12519 struct perf_event *sub;
12520 struct perf_event *child_ctr;
12521
12522 leader = inherit_event(parent_event, parent, parent_ctx,
12523 child, NULL, child_ctx);
12524 if (IS_ERR(leader))
12525 return PTR_ERR(leader);
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012526 /*
12527 * @leader can be NULL here because of is_orphaned_event(). In this
12528 * case inherit_event() will create individual events, similar to what
12529 * perf_group_detach() would do anyway.
12530 */
Peter Zijlstraedb39592018-03-15 17:36:56 +010012531 for_each_sibling_event(sub, parent_event) {
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012532 child_ctr = inherit_event(sub, parent, parent_ctx,
12533 child, leader, child_ctx);
12534 if (IS_ERR(child_ctr))
12535 return PTR_ERR(child_ctr);
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012536
Alexander Shishkin00496fe2019-11-01 17:12:48 +020012537 if (sub->aux_event == parent_event && child_ctr &&
Alexander Shishkinf733c6b2019-10-04 15:57:29 +030012538 !perf_get_aux_event(child_ctr, leader))
12539 return -EINVAL;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +020012540 }
12541 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012542}
12543
Peter Zijlstrad8a8cfc2017-03-16 13:47:51 +010012544/*
12545 * Creates the child task context and tries to inherit the event-group.
12546 *
12547 * Clears @inherited_all on !attr.inherited or error. Note that we'll leave
12548 * inherited_all set when we 'fail' to inherit an orphaned event; this is
12549 * consistent with perf_event_release_kernel() removing all child events.
12550 *
12551 * Returns:
12552 * - 0 on success
12553 * - <0 on error
12554 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012555static int
12556inherit_task_group(struct perf_event *event, struct task_struct *parent,
12557 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012558 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012559 int *inherited_all)
12560{
12561 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012562 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012563
12564 if (!event->attr.inherit) {
12565 *inherited_all = 0;
12566 return 0;
12567 }
12568
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012569 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012570 if (!child_ctx) {
12571 /*
12572 * This is executed from the parent task context, so
12573 * inherit events that have been marked for cloning.
12574 * First allocate and initialize a context for the
12575 * child.
12576 */
Jiri Olsa734df5a2013-07-09 17:44:10 +020012577 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012578 if (!child_ctx)
12579 return -ENOMEM;
12580
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012581 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012582 }
12583
12584 ret = inherit_group(event, parent, parent_ctx,
12585 child, child_ctx);
12586
12587 if (ret)
12588 *inherited_all = 0;
12589
12590 return ret;
12591}
12592
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012593/*
12594 * Initialize the perf_event context in task_struct
12595 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +020012596static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012597{
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012598 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012599 struct perf_event_context *cloned_ctx;
12600 struct perf_event *event;
12601 struct task_struct *parent = current;
12602 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012603 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012604 int ret = 0;
12605
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012606 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012607 return 0;
12608
12609 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012610 * If the parent's context is a clone, pin it so it won't get
12611 * swapped under us.
12612 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012613 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +020012614 if (!parent_ctx)
12615 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012616
12617 /*
12618 * No need to check if parent_ctx != NULL here; since we saw
12619 * it non-NULL earlier, the only reason for it to become NULL
12620 * is if we exit, and since we're currently in the middle of
12621 * a fork we can't be exiting at the same time.
12622 */
12623
12624 /*
12625 * Lock the parent list. No need to lock the child - not PID
12626 * hashed yet and not running, so nobody can access it.
12627 */
12628 mutex_lock(&parent_ctx->mutex);
12629
12630 /*
12631 * We dont have to disable NMIs - we are only looking at
12632 * the list, not manipulating it:
12633 */
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012634 perf_event_groups_for_each(event, &parent_ctx->pinned_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012635 ret = inherit_task_group(event, parent, parent_ctx,
12636 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012637 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012638 goto out_unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012639 }
12640
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012641 /*
12642 * We can't hold ctx->lock when iterating the ->flexible_group list due
12643 * to allocations, but we need to prevent rotation because
12644 * rotate_ctx() will change the list from interrupt context.
12645 */
12646 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12647 parent_ctx->rotate_disable = 1;
12648 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
12649
Peter Zijlstra6e6804d2017-11-13 14:28:41 +010012650 perf_event_groups_for_each(event, &parent_ctx->flexible_groups) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012651 ret = inherit_task_group(event, parent, parent_ctx,
12652 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012653 if (ret)
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012654 goto out_unlock;
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012655 }
12656
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012657 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
12658 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +010012659
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012660 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +010012661
Peter Zijlstra05cbaa22009-12-30 16:00:35 +010012662 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012663 /*
12664 * Mark the child context as a clone of the parent
12665 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012666 *
12667 * Note that if the parent is a clone, the holding of
12668 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012669 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012670 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012671 if (cloned_ctx) {
12672 child_ctx->parent_ctx = cloned_ctx;
12673 child_ctx->parent_gen = parent_ctx->parent_gen;
12674 } else {
12675 child_ctx->parent_ctx = parent_ctx;
12676 child_ctx->parent_gen = parent_ctx->generation;
12677 }
12678 get_ctx(child_ctx->parent_ctx);
12679 }
12680
Peter Zijlstrac5ed5142011-01-17 13:45:37 +010012681 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Peter Zijlstrae7cc4862017-03-16 13:47:49 +010012682out_unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012683 mutex_unlock(&parent_ctx->mutex);
12684
12685 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010012686 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012687
12688 return ret;
12689}
12690
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012691/*
12692 * Initialize the perf_event context in task_struct
12693 */
12694int perf_event_init_task(struct task_struct *child)
12695{
12696 int ctxn, ret;
12697
Oleg Nesterov8550d7c2011-01-19 19:22:28 +010012698 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
12699 mutex_init(&child->perf_event_mutex);
12700 INIT_LIST_HEAD(&child->perf_event_list);
12701
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012702 for_each_task_context_nr(ctxn) {
12703 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012704 if (ret) {
12705 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012706 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -070012707 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +020012708 }
12709
12710 return 0;
12711}
12712
Paul Mackerras220b1402010-03-10 20:45:52 +110012713static void __init perf_event_init_all_cpus(void)
12714{
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012715 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +110012716 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +110012717
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012718 zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL);
12719
Paul Mackerras220b1402010-03-10 20:45:52 +110012720 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012721 swhash = &per_cpu(swevent_htable, cpu);
12722 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +000012723 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Kan Liangf2fb6be2016-03-23 11:24:37 -070012724
12725 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu));
12726 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu));
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012727
David Carrillo-Cisneros058fe1c2017-01-18 11:24:53 -080012728#ifdef CONFIG_CGROUP_PERF
12729 INIT_LIST_HEAD(&per_cpu(cgrp_cpuctx_list, cpu));
12730#endif
Peter Zijlstrae48c1782016-07-06 09:18:30 +020012731 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +110012732 }
12733}
12734
Valdis Kletnieksd18bf422019-03-12 04:06:37 -040012735static void perf_swevent_init_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012736{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012737 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012738
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012739 mutex_lock(&swhash->hlist_mutex);
Thomas Gleixner059fcd82016-02-09 20:11:34 +000012740 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012741 struct swevent_hlist *hlist;
12742
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012743 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
12744 WARN_ON(!hlist);
12745 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020012746 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +020012747 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012748}
12749
Dave Young2965faa2015-09-09 15:38:55 -070012750#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012751static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012752{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012753 struct perf_event_context *ctx = __info;
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012754 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
12755 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012756
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012757 raw_spin_lock(&ctx->lock);
Peter Zijlstra0ee098c2017-09-05 13:24:28 +020012758 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012759 list_for_each_entry(event, &ctx->event_list, event_entry)
Peter Zijlstra45a0e072016-01-26 13:09:48 +010012760 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
Peter Zijlstrafae3fde2016-01-11 15:00:50 +010012761 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012762}
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012763
12764static void perf_event_exit_cpu_context(int cpu)
12765{
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012766 struct perf_cpu_context *cpuctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012767 struct perf_event_context *ctx;
12768 struct pmu *pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012769
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012770 mutex_lock(&pmus_lock);
12771 list_for_each_entry(pmu, &pmus, entry) {
12772 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12773 ctx = &cpuctx->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012774
12775 mutex_lock(&ctx->mutex);
12776 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012777 cpuctx->online = 0;
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012778 mutex_unlock(&ctx->mutex);
12779 }
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012780 cpumask_clear_cpu(cpu, perf_online_mask);
12781 mutex_unlock(&pmus_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012782}
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012783#else
Peter Zijlstra108b02c2010-09-06 14:32:03 +020012784
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012785static void perf_event_exit_cpu_context(int cpu) { }
12786
12787#endif
12788
Thomas Gleixnera63fbed2017-05-24 10:15:34 +020012789int perf_event_init_cpu(unsigned int cpu)
12790{
12791 struct perf_cpu_context *cpuctx;
12792 struct perf_event_context *ctx;
12793 struct pmu *pmu;
12794
12795 perf_swevent_init_cpu(cpu);
12796
12797 mutex_lock(&pmus_lock);
12798 cpumask_set_cpu(cpu, perf_online_mask);
12799 list_for_each_entry(pmu, &pmus, entry) {
12800 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
12801 ctx = &cpuctx->ctx;
12802
12803 mutex_lock(&ctx->mutex);
12804 cpuctx->online = 1;
12805 mutex_unlock(&ctx->mutex);
12806 }
12807 mutex_unlock(&pmus_lock);
12808
12809 return 0;
12810}
12811
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012812int perf_event_exit_cpu(unsigned int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012813{
Peter Zijlstrae3703f82014-02-24 12:06:12 +010012814 perf_event_exit_cpu_context(cpu);
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012815 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012816}
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012817
Peter Zijlstrac2774432010-12-08 15:29:02 +010012818static int
12819perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
12820{
12821 int cpu;
12822
12823 for_each_online_cpu(cpu)
12824 perf_event_exit_cpu(cpu);
12825
12826 return NOTIFY_OK;
12827}
12828
12829/*
12830 * Run the perf reboot notifier at the very last possible moment so that
12831 * the generic watchdog code runs as long as possible.
12832 */
12833static struct notifier_block perf_reboot_notifier = {
12834 .notifier_call = perf_reboot,
12835 .priority = INT_MIN,
12836};
12837
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012838void __init perf_event_init(void)
12839{
Jason Wessel3c502e72010-11-04 17:33:01 -050012840 int ret;
12841
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012842 idr_init(&pmu_idr);
12843
Paul Mackerras220b1402010-03-10 20:45:52 +110012844 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012845 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +010012846 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
12847 perf_pmu_register(&perf_cpu_clock, NULL, -1);
12848 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +020012849 perf_tp_register();
Thomas Gleixner00e16c32016-07-13 17:16:09 +000012850 perf_event_init_cpu(smp_processor_id());
Peter Zijlstrac2774432010-12-08 15:29:02 +010012851 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -050012852
12853 ret = init_hw_breakpoint();
12854 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +020012855
Jiri Olsab01c3a02012-03-23 15:41:20 +010012856 /*
12857 * Build time assertion that we keep the data_head at the intended
12858 * location. IOW, validation we got the __reserved[] size right.
12859 */
12860 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
12861 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020012862}
Peter Zijlstraabe43402010-11-17 23:17:37 +010012863
Cody P Schaferfd979c02015-01-30 13:45:57 -080012864ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
12865 char *page)
12866{
12867 struct perf_pmu_events_attr *pmu_attr =
12868 container_of(attr, struct perf_pmu_events_attr, attr);
12869
12870 if (pmu_attr->event_str)
12871 return sprintf(page, "%s\n", pmu_attr->event_str);
12872
12873 return 0;
12874}
Thomas Gleixner675965b2016-02-22 22:19:27 +000012875EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
Cody P Schaferfd979c02015-01-30 13:45:57 -080012876
Peter Zijlstraabe43402010-11-17 23:17:37 +010012877static int __init perf_event_sysfs_init(void)
12878{
12879 struct pmu *pmu;
12880 int ret;
12881
12882 mutex_lock(&pmus_lock);
12883
12884 ret = bus_register(&pmu_bus);
12885 if (ret)
12886 goto unlock;
12887
12888 list_for_each_entry(pmu, &pmus, entry) {
12889 if (!pmu->name || pmu->type < 0)
12890 continue;
12891
12892 ret = pmu_dev_alloc(pmu);
12893 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
12894 }
12895 pmu_bus_running = 1;
12896 ret = 0;
12897
12898unlock:
12899 mutex_unlock(&pmus_lock);
12900
12901 return ret;
12902}
12903device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +020012904
12905#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -040012906static struct cgroup_subsys_state *
12907perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020012908{
12909 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +020012910
Li Zefan1b15d052011-03-03 14:26:06 +080012911 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +020012912 if (!jc)
12913 return ERR_PTR(-ENOMEM);
12914
Stephane Eraniane5d13672011-02-14 11:20:01 +020012915 jc->info = alloc_percpu(struct perf_cgroup_info);
12916 if (!jc->info) {
12917 kfree(jc);
12918 return ERR_PTR(-ENOMEM);
12919 }
12920
Stephane Eraniane5d13672011-02-14 11:20:01 +020012921 return &jc->css;
12922}
12923
Tejun Heoeb954192013-08-08 20:11:23 -040012924static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +020012925{
Tejun Heoeb954192013-08-08 20:11:23 -040012926 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
12927
Stephane Eraniane5d13672011-02-14 11:20:01 +020012928 free_percpu(jc->info);
12929 kfree(jc);
12930}
12931
Namhyung Kim96aaab62020-03-25 21:45:28 +090012932static int perf_cgroup_css_online(struct cgroup_subsys_state *css)
12933{
12934 perf_event_cgroup(css->cgroup);
12935 return 0;
12936}
12937
Stephane Eraniane5d13672011-02-14 11:20:01 +020012938static int __perf_cgroup_move(void *info)
12939{
12940 struct task_struct *task = info;
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010012941 rcu_read_lock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020012942 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
Stephane Eranianddaaf4e2015-11-12 11:00:03 +010012943 rcu_read_unlock();
Stephane Eraniane5d13672011-02-14 11:20:01 +020012944 return 0;
12945}
12946
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050012947static void perf_cgroup_attach(struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +020012948{
Tejun Heobb9d97b2011-12-12 18:12:21 -080012949 struct task_struct *task;
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050012950 struct cgroup_subsys_state *css;
Tejun Heobb9d97b2011-12-12 18:12:21 -080012951
Tejun Heo1f7dd3e52015-12-03 10:18:21 -050012952 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -080012953 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +020012954}
12955
Tejun Heo073219e2014-02-08 10:36:58 -050012956struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -080012957 .css_alloc = perf_cgroup_css_alloc,
12958 .css_free = perf_cgroup_css_free,
Namhyung Kim96aaab62020-03-25 21:45:28 +090012959 .css_online = perf_cgroup_css_online,
Tejun Heobb9d97b2011-12-12 18:12:21 -080012960 .attach = perf_cgroup_attach,
Tejun Heo968ebff2017-01-29 14:35:20 -050012961 /*
12962 * Implicitly enable on dfl hierarchy so that perf events can
12963 * always be filtered by cgroup2 path as long as perf_event
12964 * controller is not mounted on a legacy hierarchy.
12965 */
12966 .implicit_on_dfl = true,
Tejun Heo8cfd8142017-07-21 11:14:51 -040012967 .threaded = true,
Stephane Eraniane5d13672011-02-14 11:20:01 +020012968};
12969#endif /* CONFIG_CGROUP_PERF */