blob: 6c8b31b7efb69a5b16a3b0d147819ce3636b7973 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
37#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080038#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Li Zefan877c6852013-03-05 11:38:08 +080041#include <linux/cgroup.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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045
Frederic Weisbecker76369132011-05-19 19:55:04 +020046#include "internal.h"
47
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048#include <asm/irq_regs.h>
49
Jiri Olsafadfe7b2014-08-01 14:33:02 +020050static struct workqueue_struct *perf_wq;
51
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020053 struct task_struct *p;
54 int (*func)(void *info);
55 void *info;
56 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010057};
58
59static void remote_function(void *data)
60{
61 struct remote_function_call *tfc = data;
62 struct task_struct *p = tfc->p;
63
64 if (p) {
65 tfc->ret = -EAGAIN;
66 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
67 return;
68 }
69
70 tfc->ret = tfc->func(tfc->info);
71}
72
73/**
74 * task_function_call - call a function on the cpu on which a task runs
75 * @p: the task to evaluate
76 * @func: the function to be called
77 * @info: the function call argument
78 *
79 * Calls the function @func when the task is currently running. This might
80 * be on the current CPU, which just calls the function directly
81 *
82 * returns: @func return value, or
83 * -ESRCH - when the process isn't running
84 * -EAGAIN - when the process moved away
85 */
86static int
87task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
88{
89 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020090 .p = p,
91 .func = func,
92 .info = info,
93 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010094 };
95
96 if (task_curr(p))
97 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
98
99 return data.ret;
100}
101
102/**
103 * cpu_function_call - call a function on the cpu
104 * @func: the function to be called
105 * @info: the function call argument
106 *
107 * Calls the function @func on the remote cpu.
108 *
109 * returns: @func return value or -ENXIO when the cpu is offline
110 */
111static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
112{
113 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200114 .p = NULL,
115 .func = func,
116 .info = info,
117 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100118 };
119
120 smp_call_function_single(cpu, remote_function, &data, 1);
121
122 return data.ret;
123}
124
Jiri Olsaf8697762014-08-01 14:33:01 +0200125#define EVENT_OWNER_KERNEL ((void *) -1)
126
127static bool is_kernel_event(struct perf_event *event)
128{
129 return event->owner == EVENT_OWNER_KERNEL;
130}
131
Stephane Eraniane5d13672011-02-14 11:20:01 +0200132#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
133 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100134 PERF_FLAG_PID_CGROUP |\
135 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200136
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100137/*
138 * branch priv levels that need permission checks
139 */
140#define PERF_SAMPLE_BRANCH_PERM_PLM \
141 (PERF_SAMPLE_BRANCH_KERNEL |\
142 PERF_SAMPLE_BRANCH_HV)
143
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200144enum event_type_t {
145 EVENT_FLEXIBLE = 0x1,
146 EVENT_PINNED = 0x2,
147 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
148};
149
Stephane Eraniane5d13672011-02-14 11:20:01 +0200150/*
151 * perf_sched_events : >0 events exist
152 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
153 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100154struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200155static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100156static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Yan, Zhengba532502014-11-04 21:55:58 -0500157static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200158
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200159static atomic_t nr_mmap_events __read_mostly;
160static atomic_t nr_comm_events __read_mostly;
161static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200162static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200163
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200164static LIST_HEAD(pmus);
165static DEFINE_MUTEX(pmus_lock);
166static struct srcu_struct pmus_srcu;
167
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200168/*
169 * perf event paranoia level:
170 * -1 - not paranoid at all
171 * 0 - disallow raw tracepoint access for unpriv
172 * 1 - disallow cpu events for unpriv
173 * 2 - disallow kernel profiling for unpriv
174 */
175int sysctl_perf_event_paranoid __read_mostly = 1;
176
Frederic Weisbecker20443382011-03-31 03:33:29 +0200177/* Minimum for 512 kiB + 1 user control page */
178int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200179
180/*
181 * max perf event sample rate
182 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700183#define DEFAULT_MAX_SAMPLE_RATE 100000
184#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
185#define DEFAULT_CPU_TIME_MAX_PERCENT 25
186
187int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
188
189static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
190static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
191
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200192static int perf_sample_allowed_ns __read_mostly =
193 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700194
195void update_perf_cpu_limits(void)
196{
197 u64 tmp = perf_sample_period_ns;
198
199 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200200 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200201 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700202}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100203
Stephane Eranian9e630202013-04-03 14:21:33 +0200204static int perf_rotate_context(struct perf_cpu_context *cpuctx);
205
Peter Zijlstra163ec432011-02-16 11:22:34 +0100206int perf_proc_update_handler(struct ctl_table *table, int write,
207 void __user *buffer, size_t *lenp,
208 loff_t *ppos)
209{
Knut Petersen723478c2013-09-25 14:29:37 +0200210 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100211
212 if (ret || !write)
213 return ret;
214
215 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700216 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
217 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100218
219 return 0;
220}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200221
Dave Hansen14c63f12013-06-21 08:51:36 -0700222int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
223
224int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
225 void __user *buffer, size_t *lenp,
226 loff_t *ppos)
227{
228 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
229
230 if (ret || !write)
231 return ret;
232
233 update_perf_cpu_limits();
234
235 return 0;
236}
237
238/*
239 * perf samples are done in some very critical code paths (NMIs).
240 * If they take too much CPU time, the system can lock up and not
241 * get any real work done. This will drop the sample rate when
242 * we detect that events are taking too long.
243 */
244#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200245static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700246
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100247static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700248{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100249 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700250 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200251 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100252
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500253 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100254 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
255
256 printk_ratelimited(KERN_WARNING
257 "perf interrupt took too long (%lld > %lld), lowering "
258 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100259 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100260 sysctl_perf_event_sample_rate);
261}
262
263static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
264
265void perf_sample_event_took(u64 sample_len_ns)
266{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200267 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100268 u64 avg_local_sample_len;
269 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700270
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200271 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700272 return;
273
274 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500275 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700276 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
277 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500278 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700279
280 /*
281 * note: this will be biased artifically low until we have
282 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
283 * from having to maintain a count.
284 */
285 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
286
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200287 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700288 return;
289
290 if (max_samples_per_tick <= 1)
291 return;
292
293 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
294 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
295 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
296
Dave Hansen14c63f12013-06-21 08:51:36 -0700297 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100298
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100299 if (!irq_work_queue(&perf_duration_work)) {
300 early_printk("perf interrupt took too long (%lld > %lld), lowering "
301 "kernel.perf_event_max_sample_rate to %d\n",
302 avg_local_sample_len, allowed_ns >> 1,
303 sysctl_perf_event_sample_rate);
304 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700305}
306
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200307static atomic64_t perf_event_id;
308
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200309static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
310 enum event_type_t event_type);
311
312static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200313 enum event_type_t event_type,
314 struct task_struct *task);
315
316static void update_context_time(struct perf_event_context *ctx);
317static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200318
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200319void __weak perf_event_print_debug(void) { }
320
Matt Fleming84c79912010-10-03 21:41:13 +0100321extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200322{
Matt Fleming84c79912010-10-03 21:41:13 +0100323 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200324}
325
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200326static inline u64 perf_clock(void)
327{
328 return local_clock();
329}
330
Stephane Eraniane5d13672011-02-14 11:20:01 +0200331static inline struct perf_cpu_context *
332__get_cpu_context(struct perf_event_context *ctx)
333{
334 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
335}
336
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200337static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
338 struct perf_event_context *ctx)
339{
340 raw_spin_lock(&cpuctx->ctx.lock);
341 if (ctx)
342 raw_spin_lock(&ctx->lock);
343}
344
345static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
346 struct perf_event_context *ctx)
347{
348 if (ctx)
349 raw_spin_unlock(&ctx->lock);
350 raw_spin_unlock(&cpuctx->ctx.lock);
351}
352
Stephane Eraniane5d13672011-02-14 11:20:01 +0200353#ifdef CONFIG_CGROUP_PERF
354
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200355/*
Li Zefan877c6852013-03-05 11:38:08 +0800356 * perf_cgroup_info keeps track of time_enabled for a cgroup.
357 * This is a per-cpu dynamically allocated data structure.
358 */
359struct perf_cgroup_info {
360 u64 time;
361 u64 timestamp;
362};
363
364struct perf_cgroup {
365 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900366 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800367};
368
369/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200370 * Must ensure cgroup is pinned (css_get) before calling
371 * this function. In other words, we cannot call this function
372 * if there is no cgroup event for the current CPU context.
373 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200374static inline struct perf_cgroup *
375perf_cgroup_from_task(struct task_struct *task)
376{
Tejun Heo073219e2014-02-08 10:36:58 -0500377 return container_of(task_css(task, perf_event_cgrp_id),
Tejun Heo8af01f52013-08-08 20:11:22 -0400378 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200379}
380
381static inline bool
382perf_cgroup_match(struct perf_event *event)
383{
384 struct perf_event_context *ctx = event->ctx;
385 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
386
Tejun Heoef824fa2013-04-08 19:00:38 -0700387 /* @event doesn't care about cgroup */
388 if (!event->cgrp)
389 return true;
390
391 /* wants specific cgroup scope but @cpuctx isn't associated with any */
392 if (!cpuctx->cgrp)
393 return false;
394
395 /*
396 * Cgroup scoping is recursive. An event enabled for a cgroup is
397 * also enabled for all its descendant cgroups. If @cpuctx's
398 * cgroup is a descendant of @event's (the test covers identity
399 * case), it's a match.
400 */
401 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
402 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200403}
404
Stephane Eraniane5d13672011-02-14 11:20:01 +0200405static inline void perf_detach_cgroup(struct perf_event *event)
406{
Zefan Li4e2ba652014-09-19 16:53:14 +0800407 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200408 event->cgrp = NULL;
409}
410
411static inline int is_cgroup_event(struct perf_event *event)
412{
413 return event->cgrp != NULL;
414}
415
416static inline u64 perf_cgroup_event_time(struct perf_event *event)
417{
418 struct perf_cgroup_info *t;
419
420 t = per_cpu_ptr(event->cgrp->info, event->cpu);
421 return t->time;
422}
423
424static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
425{
426 struct perf_cgroup_info *info;
427 u64 now;
428
429 now = perf_clock();
430
431 info = this_cpu_ptr(cgrp->info);
432
433 info->time += now - info->timestamp;
434 info->timestamp = now;
435}
436
437static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
438{
439 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
440 if (cgrp_out)
441 __update_cgrp_time(cgrp_out);
442}
443
444static inline void update_cgrp_time_from_event(struct perf_event *event)
445{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200446 struct perf_cgroup *cgrp;
447
Stephane Eraniane5d13672011-02-14 11:20:01 +0200448 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200449 * ensure we access cgroup data only when needed and
450 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200451 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200452 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200453 return;
454
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200455 cgrp = perf_cgroup_from_task(current);
456 /*
457 * Do not update time when cgroup is not active
458 */
459 if (cgrp == event->cgrp)
460 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200461}
462
463static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200464perf_cgroup_set_timestamp(struct task_struct *task,
465 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200466{
467 struct perf_cgroup *cgrp;
468 struct perf_cgroup_info *info;
469
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200470 /*
471 * ctx->lock held by caller
472 * ensure we do not access cgroup data
473 * unless we have the cgroup pinned (css_get)
474 */
475 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200476 return;
477
478 cgrp = perf_cgroup_from_task(task);
479 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200480 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200481}
482
483#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
484#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
485
486/*
487 * reschedule events based on the cgroup constraint of task.
488 *
489 * mode SWOUT : schedule out everything
490 * mode SWIN : schedule in based on cgroup for next
491 */
492void perf_cgroup_switch(struct task_struct *task, int mode)
493{
494 struct perf_cpu_context *cpuctx;
495 struct pmu *pmu;
496 unsigned long flags;
497
498 /*
499 * disable interrupts to avoid geting nr_cgroup
500 * changes via __perf_event_disable(). Also
501 * avoids preemption.
502 */
503 local_irq_save(flags);
504
505 /*
506 * we reschedule only in the presence of cgroup
507 * constrained events.
508 */
509 rcu_read_lock();
510
511 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200512 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200513 if (cpuctx->unique_pmu != pmu)
514 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200515
Stephane Eraniane5d13672011-02-14 11:20:01 +0200516 /*
517 * perf_cgroup_events says at least one
518 * context on this CPU has cgroup events.
519 *
520 * ctx->nr_cgroups reports the number of cgroup
521 * events for a context.
522 */
523 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200524 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
525 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200526
527 if (mode & PERF_CGROUP_SWOUT) {
528 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
529 /*
530 * must not be done before ctxswout due
531 * to event_filter_match() in event_sched_out()
532 */
533 cpuctx->cgrp = NULL;
534 }
535
536 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200537 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200538 /*
539 * set cgrp before ctxsw in to allow
540 * event_filter_match() to not have to pass
541 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200542 */
543 cpuctx->cgrp = perf_cgroup_from_task(task);
544 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
545 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200546 perf_pmu_enable(cpuctx->ctx.pmu);
547 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200548 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200549 }
550
551 rcu_read_unlock();
552
553 local_irq_restore(flags);
554}
555
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200556static inline void perf_cgroup_sched_out(struct task_struct *task,
557 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200558{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200559 struct perf_cgroup *cgrp1;
560 struct perf_cgroup *cgrp2 = NULL;
561
562 /*
563 * we come here when we know perf_cgroup_events > 0
564 */
565 cgrp1 = perf_cgroup_from_task(task);
566
567 /*
568 * next is NULL when called from perf_event_enable_on_exec()
569 * that will systematically cause a cgroup_switch()
570 */
571 if (next)
572 cgrp2 = perf_cgroup_from_task(next);
573
574 /*
575 * only schedule out current cgroup events if we know
576 * that we are switching to a different cgroup. Otherwise,
577 * do no touch the cgroup events.
578 */
579 if (cgrp1 != cgrp2)
580 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200581}
582
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200583static inline void perf_cgroup_sched_in(struct task_struct *prev,
584 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200585{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200586 struct perf_cgroup *cgrp1;
587 struct perf_cgroup *cgrp2 = NULL;
588
589 /*
590 * we come here when we know perf_cgroup_events > 0
591 */
592 cgrp1 = perf_cgroup_from_task(task);
593
594 /* prev can never be NULL */
595 cgrp2 = perf_cgroup_from_task(prev);
596
597 /*
598 * only need to schedule in cgroup events if we are changing
599 * cgroup during ctxsw. Cgroup events were not scheduled
600 * out of ctxsw out if that was not the case.
601 */
602 if (cgrp1 != cgrp2)
603 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200604}
605
606static inline int perf_cgroup_connect(int fd, struct perf_event *event,
607 struct perf_event_attr *attr,
608 struct perf_event *group_leader)
609{
610 struct perf_cgroup *cgrp;
611 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400612 struct fd f = fdget(fd);
613 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200614
Al Viro2903ff02012-08-28 12:52:22 -0400615 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200616 return -EBADF;
617
Al Virob5830432014-10-31 01:22:04 -0400618 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400619 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800620 if (IS_ERR(css)) {
621 ret = PTR_ERR(css);
622 goto out;
623 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200624
625 cgrp = container_of(css, struct perf_cgroup, css);
626 event->cgrp = cgrp;
627
628 /*
629 * all events in a group must monitor
630 * the same cgroup because a task belongs
631 * to only one perf cgroup at a time
632 */
633 if (group_leader && group_leader->cgrp != cgrp) {
634 perf_detach_cgroup(event);
635 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200636 }
Li Zefan3db272c2011-03-03 14:25:37 +0800637out:
Al Viro2903ff02012-08-28 12:52:22 -0400638 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200639 return ret;
640}
641
642static inline void
643perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
644{
645 struct perf_cgroup_info *t;
646 t = per_cpu_ptr(event->cgrp->info, event->cpu);
647 event->shadow_ctx_time = now - t->timestamp;
648}
649
650static inline void
651perf_cgroup_defer_enabled(struct perf_event *event)
652{
653 /*
654 * when the current task's perf cgroup does not match
655 * the event's, we need to remember to call the
656 * perf_mark_enable() function the first time a task with
657 * a matching perf cgroup is scheduled in.
658 */
659 if (is_cgroup_event(event) && !perf_cgroup_match(event))
660 event->cgrp_defer_enabled = 1;
661}
662
663static inline void
664perf_cgroup_mark_enabled(struct perf_event *event,
665 struct perf_event_context *ctx)
666{
667 struct perf_event *sub;
668 u64 tstamp = perf_event_time(event);
669
670 if (!event->cgrp_defer_enabled)
671 return;
672
673 event->cgrp_defer_enabled = 0;
674
675 event->tstamp_enabled = tstamp - event->total_time_enabled;
676 list_for_each_entry(sub, &event->sibling_list, group_entry) {
677 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
678 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
679 sub->cgrp_defer_enabled = 0;
680 }
681 }
682}
683#else /* !CONFIG_CGROUP_PERF */
684
685static inline bool
686perf_cgroup_match(struct perf_event *event)
687{
688 return true;
689}
690
691static inline void perf_detach_cgroup(struct perf_event *event)
692{}
693
694static inline int is_cgroup_event(struct perf_event *event)
695{
696 return 0;
697}
698
699static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
700{
701 return 0;
702}
703
704static inline void update_cgrp_time_from_event(struct perf_event *event)
705{
706}
707
708static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
709{
710}
711
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200712static inline void perf_cgroup_sched_out(struct task_struct *task,
713 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200714{
715}
716
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200717static inline void perf_cgroup_sched_in(struct task_struct *prev,
718 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200719{
720}
721
722static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
723 struct perf_event_attr *attr,
724 struct perf_event *group_leader)
725{
726 return -EINVAL;
727}
728
729static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200730perf_cgroup_set_timestamp(struct task_struct *task,
731 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200732{
733}
734
735void
736perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
737{
738}
739
740static inline void
741perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
742{
743}
744
745static inline u64 perf_cgroup_event_time(struct perf_event *event)
746{
747 return 0;
748}
749
750static inline void
751perf_cgroup_defer_enabled(struct perf_event *event)
752{
753}
754
755static inline void
756perf_cgroup_mark_enabled(struct perf_event *event,
757 struct perf_event_context *ctx)
758{
759}
760#endif
761
Stephane Eranian9e630202013-04-03 14:21:33 +0200762/*
763 * set default to be dependent on timer tick just
764 * like original code
765 */
766#define PERF_CPU_HRTIMER (1000 / HZ)
767/*
768 * function must be called with interrupts disbled
769 */
770static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
771{
772 struct perf_cpu_context *cpuctx;
773 enum hrtimer_restart ret = HRTIMER_NORESTART;
774 int rotations = 0;
775
776 WARN_ON(!irqs_disabled());
777
778 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
779
780 rotations = perf_rotate_context(cpuctx);
781
782 /*
783 * arm timer if needed
784 */
785 if (rotations) {
786 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
787 ret = HRTIMER_RESTART;
788 }
789
790 return ret;
791}
792
793/* CPU is going down */
794void perf_cpu_hrtimer_cancel(int cpu)
795{
796 struct perf_cpu_context *cpuctx;
797 struct pmu *pmu;
798 unsigned long flags;
799
800 if (WARN_ON(cpu != smp_processor_id()))
801 return;
802
803 local_irq_save(flags);
804
805 rcu_read_lock();
806
807 list_for_each_entry_rcu(pmu, &pmus, entry) {
808 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
809
810 if (pmu->task_ctx_nr == perf_sw_context)
811 continue;
812
813 hrtimer_cancel(&cpuctx->hrtimer);
814 }
815
816 rcu_read_unlock();
817
818 local_irq_restore(flags);
819}
820
821static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
822{
823 struct hrtimer *hr = &cpuctx->hrtimer;
824 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200825 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200826
827 /* no multiplexing needed for SW PMU */
828 if (pmu->task_ctx_nr == perf_sw_context)
829 return;
830
Stephane Eranian62b85632013-04-03 14:21:34 +0200831 /*
832 * check default is sane, if not set then force to
833 * default interval (1/tick)
834 */
835 timer = pmu->hrtimer_interval_ms;
836 if (timer < 1)
837 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
838
839 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200840
841 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
842 hr->function = perf_cpu_hrtimer_handler;
843}
844
845static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
846{
847 struct hrtimer *hr = &cpuctx->hrtimer;
848 struct pmu *pmu = cpuctx->ctx.pmu;
849
850 /* not for SW PMU */
851 if (pmu->task_ctx_nr == perf_sw_context)
852 return;
853
854 if (hrtimer_active(hr))
855 return;
856
857 if (!hrtimer_callback_running(hr))
858 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
859 0, HRTIMER_MODE_REL_PINNED, 0);
860}
861
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200862void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200864 int *count = this_cpu_ptr(pmu->pmu_disable_count);
865 if (!(*count)++)
866 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867}
868
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200869void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200870{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200871 int *count = this_cpu_ptr(pmu->pmu_disable_count);
872 if (!--(*count))
873 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200874}
875
Mark Rutland2fde4f92015-01-07 15:01:54 +0000876static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200877
878/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000879 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
880 * perf_event_task_tick() are fully serialized because they're strictly cpu
881 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
882 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200883 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000884static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200885{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000886 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200887
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200888 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200889
Mark Rutland2fde4f92015-01-07 15:01:54 +0000890 WARN_ON(!list_empty(&ctx->active_ctx_list));
891
892 list_add(&ctx->active_ctx_list, head);
893}
894
895static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
896{
897 WARN_ON(!irqs_disabled());
898
899 WARN_ON(list_empty(&ctx->active_ctx_list));
900
901 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200902}
903
904static void get_ctx(struct perf_event_context *ctx)
905{
906 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
907}
908
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200909static void put_ctx(struct perf_event_context *ctx)
910{
911 if (atomic_dec_and_test(&ctx->refcount)) {
912 if (ctx->parent_ctx)
913 put_ctx(ctx->parent_ctx);
914 if (ctx->task)
915 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800916 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917 }
918}
919
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200920/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100921 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
922 * perf_pmu_migrate_context() we need some magic.
923 *
924 * Those places that change perf_event::ctx will hold both
925 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
926 *
927 * Lock ordering is by mutex address. There is one other site where
928 * perf_event_context::mutex nests and that is put_event(). But remember that
929 * that is a parent<->child context relation, and migration does not affect
930 * children, therefore these two orderings should not interact.
931 *
932 * The change in perf_event::ctx does not affect children (as claimed above)
933 * because the sys_perf_event_open() case will install a new event and break
934 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
935 * concerned with cpuctx and that doesn't have children.
936 *
937 * The places that change perf_event::ctx will issue:
938 *
939 * perf_remove_from_context();
940 * synchronize_rcu();
941 * perf_install_in_context();
942 *
943 * to affect the change. The remove_from_context() + synchronize_rcu() should
944 * quiesce the event, after which we can install it in the new location. This
945 * means that only external vectors (perf_fops, prctl) can perturb the event
946 * while in transit. Therefore all such accessors should also acquire
947 * perf_event_context::mutex to serialize against this.
948 *
949 * However; because event->ctx can change while we're waiting to acquire
950 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
951 * function.
952 *
953 * Lock order:
954 * task_struct::perf_event_mutex
955 * perf_event_context::mutex
956 * perf_event_context::lock
957 * perf_event::child_mutex;
958 * perf_event::mmap_mutex
959 * mmap_sem
960 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100961static struct perf_event_context *
962perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100963{
964 struct perf_event_context *ctx;
965
966again:
967 rcu_read_lock();
968 ctx = ACCESS_ONCE(event->ctx);
969 if (!atomic_inc_not_zero(&ctx->refcount)) {
970 rcu_read_unlock();
971 goto again;
972 }
973 rcu_read_unlock();
974
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100975 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100976 if (event->ctx != ctx) {
977 mutex_unlock(&ctx->mutex);
978 put_ctx(ctx);
979 goto again;
980 }
981
982 return ctx;
983}
984
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100985static inline struct perf_event_context *
986perf_event_ctx_lock(struct perf_event *event)
987{
988 return perf_event_ctx_lock_nested(event, 0);
989}
990
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100991static void perf_event_ctx_unlock(struct perf_event *event,
992 struct perf_event_context *ctx)
993{
994 mutex_unlock(&ctx->mutex);
995 put_ctx(ctx);
996}
997
998/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200999 * This must be done under the ctx->lock, such as to serialize against
1000 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1001 * calling scheduler related locks and ctx->lock nests inside those.
1002 */
1003static __must_check struct perf_event_context *
1004unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001006 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1007
1008 lockdep_assert_held(&ctx->lock);
1009
1010 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001011 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001012 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001013
1014 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001015}
1016
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001017static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1018{
1019 /*
1020 * only top level events have the pid namespace they were created in
1021 */
1022 if (event->parent)
1023 event = event->parent;
1024
1025 return task_tgid_nr_ns(p, event->ns);
1026}
1027
1028static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1029{
1030 /*
1031 * only top level events have the pid namespace they were created in
1032 */
1033 if (event->parent)
1034 event = event->parent;
1035
1036 return task_pid_nr_ns(p, event->ns);
1037}
1038
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001039/*
1040 * If we inherit events we want to return the parent event id
1041 * to userspace.
1042 */
1043static u64 primary_event_id(struct perf_event *event)
1044{
1045 u64 id = event->id;
1046
1047 if (event->parent)
1048 id = event->parent->id;
1049
1050 return id;
1051}
1052
1053/*
1054 * Get the perf_event_context for a task and lock it.
1055 * This has to cope with with the fact that until it is locked,
1056 * the context could get moved to another task.
1057 */
1058static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001059perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001060{
1061 struct perf_event_context *ctx;
1062
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001063retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001064 /*
1065 * One of the few rules of preemptible RCU is that one cannot do
1066 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1067 * part of the read side critical section was preemptible -- see
1068 * rcu_read_unlock_special().
1069 *
1070 * Since ctx->lock nests under rq->lock we must ensure the entire read
1071 * side critical section is non-preemptible.
1072 */
1073 preempt_disable();
1074 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001075 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001076 if (ctx) {
1077 /*
1078 * If this context is a clone of another, it might
1079 * get swapped for another underneath us by
1080 * perf_event_task_sched_out, though the
1081 * rcu_read_lock() protects us from any context
1082 * getting freed. Lock the context and check if it
1083 * got swapped before we could get the lock, and retry
1084 * if so. If we locked the right context, then it
1085 * can't get swapped on us any more.
1086 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001087 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001088 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001089 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001090 rcu_read_unlock();
1091 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001092 goto retry;
1093 }
1094
1095 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001096 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001097 ctx = NULL;
1098 }
1099 }
1100 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001101 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001102 return ctx;
1103}
1104
1105/*
1106 * Get the context for a task and increment its pin_count so it
1107 * can't get swapped to another task. This also increments its
1108 * reference count so that the context can't get freed.
1109 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001110static struct perf_event_context *
1111perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001112{
1113 struct perf_event_context *ctx;
1114 unsigned long flags;
1115
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001116 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117 if (ctx) {
1118 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001119 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001120 }
1121 return ctx;
1122}
1123
1124static void perf_unpin_context(struct perf_event_context *ctx)
1125{
1126 unsigned long flags;
1127
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001128 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001129 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001130 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001131}
1132
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001133/*
1134 * Update the record of the current time in a context.
1135 */
1136static void update_context_time(struct perf_event_context *ctx)
1137{
1138 u64 now = perf_clock();
1139
1140 ctx->time += now - ctx->timestamp;
1141 ctx->timestamp = now;
1142}
1143
Stephane Eranian41587552011-01-03 18:20:01 +02001144static u64 perf_event_time(struct perf_event *event)
1145{
1146 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001147
1148 if (is_cgroup_event(event))
1149 return perf_cgroup_event_time(event);
1150
Stephane Eranian41587552011-01-03 18:20:01 +02001151 return ctx ? ctx->time : 0;
1152}
1153
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001154/*
1155 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001156 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001157 */
1158static void update_event_times(struct perf_event *event)
1159{
1160 struct perf_event_context *ctx = event->ctx;
1161 u64 run_end;
1162
1163 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1164 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1165 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001166 /*
1167 * in cgroup mode, time_enabled represents
1168 * the time the event was enabled AND active
1169 * tasks were in the monitored cgroup. This is
1170 * independent of the activity of the context as
1171 * there may be a mix of cgroup and non-cgroup events.
1172 *
1173 * That is why we treat cgroup events differently
1174 * here.
1175 */
1176 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001177 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001178 else if (ctx->is_active)
1179 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001180 else
1181 run_end = event->tstamp_stopped;
1182
1183 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001184
1185 if (event->state == PERF_EVENT_STATE_INACTIVE)
1186 run_end = event->tstamp_stopped;
1187 else
Stephane Eranian41587552011-01-03 18:20:01 +02001188 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001189
1190 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001191
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001192}
1193
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001194/*
1195 * Update total_time_enabled and total_time_running for all events in a group.
1196 */
1197static void update_group_times(struct perf_event *leader)
1198{
1199 struct perf_event *event;
1200
1201 update_event_times(leader);
1202 list_for_each_entry(event, &leader->sibling_list, group_entry)
1203 update_event_times(event);
1204}
1205
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001206static struct list_head *
1207ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1208{
1209 if (event->attr.pinned)
1210 return &ctx->pinned_groups;
1211 else
1212 return &ctx->flexible_groups;
1213}
1214
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215/*
1216 * Add a event from the lists for its context.
1217 * Must be called with ctx->mutex and ctx->lock held.
1218 */
1219static void
1220list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1221{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001222 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1223 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001224
1225 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001226 * If we're a stand alone event or group leader, we go to the context
1227 * list, group events are kept attached to the group so that
1228 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001229 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001230 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001231 struct list_head *list;
1232
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001233 if (is_software_event(event))
1234 event->group_flags |= PERF_GROUP_SOFTWARE;
1235
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001236 list = ctx_group_list(event, ctx);
1237 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238 }
1239
Peter Zijlstra08309372011-03-03 11:31:20 +01001240 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001241 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001242
Stephane Eraniand010b332012-02-09 23:21:00 +01001243 if (has_branch_stack(event))
1244 ctx->nr_branch_stack++;
1245
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246 list_add_rcu(&event->event_entry, &ctx->event_list);
1247 ctx->nr_events++;
1248 if (event->attr.inherit_stat)
1249 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001250
1251 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252}
1253
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001254/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001255 * Initialize event state based on the perf_event_attr::disabled.
1256 */
1257static inline void perf_event__state_init(struct perf_event *event)
1258{
1259 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1260 PERF_EVENT_STATE_INACTIVE;
1261}
1262
1263/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001264 * Called at perf_event creation and when events are attached/detached from a
1265 * group.
1266 */
1267static void perf_event__read_size(struct perf_event *event)
1268{
1269 int entry = sizeof(u64); /* value */
1270 int size = 0;
1271 int nr = 1;
1272
1273 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1274 size += sizeof(u64);
1275
1276 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1277 size += sizeof(u64);
1278
1279 if (event->attr.read_format & PERF_FORMAT_ID)
1280 entry += sizeof(u64);
1281
1282 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1283 nr += event->group_leader->nr_siblings;
1284 size += sizeof(u64);
1285 }
1286
1287 size += entry * nr;
1288 event->read_size = size;
1289}
1290
1291static void perf_event__header_size(struct perf_event *event)
1292{
1293 struct perf_sample_data *data;
1294 u64 sample_type = event->attr.sample_type;
1295 u16 size = 0;
1296
1297 perf_event__read_size(event);
1298
1299 if (sample_type & PERF_SAMPLE_IP)
1300 size += sizeof(data->ip);
1301
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001302 if (sample_type & PERF_SAMPLE_ADDR)
1303 size += sizeof(data->addr);
1304
1305 if (sample_type & PERF_SAMPLE_PERIOD)
1306 size += sizeof(data->period);
1307
Andi Kleenc3feedf2013-01-24 16:10:28 +01001308 if (sample_type & PERF_SAMPLE_WEIGHT)
1309 size += sizeof(data->weight);
1310
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001311 if (sample_type & PERF_SAMPLE_READ)
1312 size += event->read_size;
1313
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001314 if (sample_type & PERF_SAMPLE_DATA_SRC)
1315 size += sizeof(data->data_src.val);
1316
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001317 if (sample_type & PERF_SAMPLE_TRANSACTION)
1318 size += sizeof(data->txn);
1319
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001320 event->header_size = size;
1321}
1322
1323static void perf_event__id_header_size(struct perf_event *event)
1324{
1325 struct perf_sample_data *data;
1326 u64 sample_type = event->attr.sample_type;
1327 u16 size = 0;
1328
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001329 if (sample_type & PERF_SAMPLE_TID)
1330 size += sizeof(data->tid_entry);
1331
1332 if (sample_type & PERF_SAMPLE_TIME)
1333 size += sizeof(data->time);
1334
Adrian Hunterff3d5272013-08-27 11:23:07 +03001335 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1336 size += sizeof(data->id);
1337
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001338 if (sample_type & PERF_SAMPLE_ID)
1339 size += sizeof(data->id);
1340
1341 if (sample_type & PERF_SAMPLE_STREAM_ID)
1342 size += sizeof(data->stream_id);
1343
1344 if (sample_type & PERF_SAMPLE_CPU)
1345 size += sizeof(data->cpu_entry);
1346
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001347 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001348}
1349
Peter Zijlstra8a495422010-05-27 15:47:49 +02001350static void perf_group_attach(struct perf_event *event)
1351{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001352 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001353
Peter Zijlstra74c33372010-10-15 11:40:29 +02001354 /*
1355 * We can have double attach due to group movement in perf_event_open.
1356 */
1357 if (event->attach_state & PERF_ATTACH_GROUP)
1358 return;
1359
Peter Zijlstra8a495422010-05-27 15:47:49 +02001360 event->attach_state |= PERF_ATTACH_GROUP;
1361
1362 if (group_leader == event)
1363 return;
1364
Peter Zijlstra652884f2015-01-23 11:20:10 +01001365 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1366
Peter Zijlstra8a495422010-05-27 15:47:49 +02001367 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1368 !is_software_event(event))
1369 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1370
1371 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1372 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001373
1374 perf_event__header_size(group_leader);
1375
1376 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1377 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001378}
1379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380/*
1381 * Remove a event from the lists for its context.
1382 * Must be called with ctx->mutex and ctx->lock held.
1383 */
1384static void
1385list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1386{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001387 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001388
1389 WARN_ON_ONCE(event->ctx != ctx);
1390 lockdep_assert_held(&ctx->lock);
1391
Peter Zijlstra8a495422010-05-27 15:47:49 +02001392 /*
1393 * We can have double detach due to exit/hot-unplug + close.
1394 */
1395 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001397
1398 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1399
Stephane Eranian68cacd22011-03-23 16:03:06 +01001400 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001401 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001402 cpuctx = __get_cpu_context(ctx);
1403 /*
1404 * if there are no more cgroup events
1405 * then cler cgrp to avoid stale pointer
1406 * in update_cgrp_time_from_cpuctx()
1407 */
1408 if (!ctx->nr_cgroups)
1409 cpuctx->cgrp = NULL;
1410 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001411
Stephane Eraniand010b332012-02-09 23:21:00 +01001412 if (has_branch_stack(event))
1413 ctx->nr_branch_stack--;
1414
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415 ctx->nr_events--;
1416 if (event->attr.inherit_stat)
1417 ctx->nr_stat--;
1418
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001419 list_del_rcu(&event->event_entry);
1420
Peter Zijlstra8a495422010-05-27 15:47:49 +02001421 if (event->group_leader == event)
1422 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001424 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001425
1426 /*
1427 * If event was in error state, then keep it
1428 * that way, otherwise bogus counts will be
1429 * returned on read(). The only way to get out
1430 * of error state is by explicit re-enabling
1431 * of the event
1432 */
1433 if (event->state > PERF_EVENT_STATE_OFF)
1434 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001435
1436 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001437}
1438
Peter Zijlstra8a495422010-05-27 15:47:49 +02001439static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001440{
1441 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001442 struct list_head *list = NULL;
1443
1444 /*
1445 * We can have double detach due to exit/hot-unplug + close.
1446 */
1447 if (!(event->attach_state & PERF_ATTACH_GROUP))
1448 return;
1449
1450 event->attach_state &= ~PERF_ATTACH_GROUP;
1451
1452 /*
1453 * If this is a sibling, remove it from its group.
1454 */
1455 if (event->group_leader != event) {
1456 list_del_init(&event->group_entry);
1457 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001458 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001459 }
1460
1461 if (!list_empty(&event->group_entry))
1462 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464 /*
1465 * If this was a group event with sibling events then
1466 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001467 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001468 */
1469 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001470 if (list)
1471 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001473
1474 /* Inherit group flags from the previous leader */
1475 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001476
1477 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001478 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001479
1480out:
1481 perf_event__header_size(event->group_leader);
1482
1483 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1484 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001485}
1486
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001487/*
1488 * User event without the task.
1489 */
1490static bool is_orphaned_event(struct perf_event *event)
1491{
1492 return event && !is_kernel_event(event) && !event->owner;
1493}
1494
1495/*
1496 * Event has a parent but parent's task finished and it's
1497 * alive only because of children holding refference.
1498 */
1499static bool is_orphaned_child(struct perf_event *event)
1500{
1501 return is_orphaned_event(event->parent);
1502}
1503
1504static void orphans_remove_work(struct work_struct *work);
1505
1506static void schedule_orphans_remove(struct perf_event_context *ctx)
1507{
1508 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1509 return;
1510
1511 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1512 get_ctx(ctx);
1513 ctx->orphans_remove_sched = true;
1514 }
1515}
1516
1517static int __init perf_workqueue_init(void)
1518{
1519 perf_wq = create_singlethread_workqueue("perf");
1520 WARN(!perf_wq, "failed to create perf workqueue\n");
1521 return perf_wq ? 0 : -1;
1522}
1523
1524core_initcall(perf_workqueue_init);
1525
Stephane Eranianfa66f072010-08-26 16:40:01 +02001526static inline int
1527event_filter_match(struct perf_event *event)
1528{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001529 return (event->cpu == -1 || event->cpu == smp_processor_id())
1530 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001531}
1532
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001533static void
1534event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001535 struct perf_cpu_context *cpuctx,
1536 struct perf_event_context *ctx)
1537{
Stephane Eranian41587552011-01-03 18:20:01 +02001538 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001539 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001540
1541 WARN_ON_ONCE(event->ctx != ctx);
1542 lockdep_assert_held(&ctx->lock);
1543
Stephane Eranianfa66f072010-08-26 16:40:01 +02001544 /*
1545 * An event which could not be activated because of
1546 * filter mismatch still needs to have its timings
1547 * maintained, otherwise bogus information is return
1548 * via read() for time_enabled, time_running:
1549 */
1550 if (event->state == PERF_EVENT_STATE_INACTIVE
1551 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001552 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001553 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001554 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001555 }
1556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001558 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559
Alexander Shishkin44377272013-12-16 14:17:36 +02001560 perf_pmu_disable(event->pmu);
1561
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001562 event->state = PERF_EVENT_STATE_INACTIVE;
1563 if (event->pending_disable) {
1564 event->pending_disable = 0;
1565 event->state = PERF_EVENT_STATE_OFF;
1566 }
Stephane Eranian41587552011-01-03 18:20:01 +02001567 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001568 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569 event->oncpu = -1;
1570
1571 if (!is_software_event(event))
1572 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001573 if (!--ctx->nr_active)
1574 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001575 if (event->attr.freq && event->attr.sample_freq)
1576 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577 if (event->attr.exclusive || !cpuctx->active_oncpu)
1578 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001579
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001580 if (is_orphaned_child(event))
1581 schedule_orphans_remove(ctx);
1582
Alexander Shishkin44377272013-12-16 14:17:36 +02001583 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584}
1585
1586static void
1587group_sched_out(struct perf_event *group_event,
1588 struct perf_cpu_context *cpuctx,
1589 struct perf_event_context *ctx)
1590{
1591 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001592 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593
1594 event_sched_out(group_event, cpuctx, ctx);
1595
1596 /*
1597 * Schedule out siblings (if any):
1598 */
1599 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1600 event_sched_out(event, cpuctx, ctx);
1601
Stephane Eranianfa66f072010-08-26 16:40:01 +02001602 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603 cpuctx->exclusive = 0;
1604}
1605
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001606struct remove_event {
1607 struct perf_event *event;
1608 bool detach_group;
1609};
1610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611/*
1612 * Cross CPU call to remove a performance event
1613 *
1614 * We disable the event on the hardware level first. After that we
1615 * remove it from the context list.
1616 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001617static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001619 struct remove_event *re = info;
1620 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001621 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001622 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001624 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001626 if (re->detach_group)
1627 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001629 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1630 ctx->is_active = 0;
1631 cpuctx->task_ctx = NULL;
1632 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001633 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001634
1635 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001636}
1637
1638
1639/*
1640 * Remove the event from a task's (or a CPU's) list of events.
1641 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642 * CPU events are removed with a smp call. For task events we only
1643 * call when the task is on a CPU.
1644 *
1645 * If event->ctx is a cloned context, callers must make sure that
1646 * every task struct that event->ctx->task could possibly point to
1647 * remains valid. This is OK when called from perf_release since
1648 * that only calls us on the top-level context, which can't be a clone.
1649 * When called from perf_event_exit_task, it's OK because the
1650 * context has been detached from its task.
1651 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001652static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653{
1654 struct perf_event_context *ctx = event->ctx;
1655 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001656 struct remove_event re = {
1657 .event = event,
1658 .detach_group = detach_group,
1659 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001661 lockdep_assert_held(&ctx->mutex);
1662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 if (!task) {
1664 /*
Mark Rutland226424e2014-11-05 16:11:44 +00001665 * Per cpu events are removed via an smp call. The removal can
1666 * fail if the CPU is currently offline, but in that case we
1667 * already called __perf_remove_from_context from
1668 * perf_event_exit_cpu.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001669 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001670 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671 return;
1672 }
1673
1674retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001675 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001676 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001677
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001678 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001680 * If we failed to find a running task, but find the context active now
1681 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001683 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001684 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001685 /*
1686 * Reload the task pointer, it might have been changed by
1687 * a concurrent perf_event_context_sched_out().
1688 */
1689 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690 goto retry;
1691 }
1692
1693 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001694 * Since the task isn't running, its safe to remove the event, us
1695 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001697 if (detach_group)
1698 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001699 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001700 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701}
1702
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704 * Cross CPU call to disable a performance event
1705 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301706int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001707{
1708 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001710 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711
1712 /*
1713 * If this is a per-task event, need to check whether this
1714 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001715 *
1716 * Can trigger due to concurrent perf_event_context_sched_out()
1717 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718 */
1719 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001720 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001722 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723
1724 /*
1725 * If the event is on, turn it off.
1726 * If it is in error state, leave it in error state.
1727 */
1728 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1729 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001730 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731 update_group_times(event);
1732 if (event == event->group_leader)
1733 group_sched_out(event, cpuctx, ctx);
1734 else
1735 event_sched_out(event, cpuctx, ctx);
1736 event->state = PERF_EVENT_STATE_OFF;
1737 }
1738
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001739 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001740
1741 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742}
1743
1744/*
1745 * Disable a event.
1746 *
1747 * If event->ctx is a cloned context, callers must make sure that
1748 * every task struct that event->ctx->task could possibly point to
1749 * remains valid. This condition is satisifed when called through
1750 * perf_event_for_each_child or perf_event_for_each because they
1751 * hold the top-level event's child_mutex, so any descendant that
1752 * goes to exit will block in sync_child_event.
1753 * When called from perf_pending_event it's OK because event->ctx
1754 * is the current context on this CPU and preemption is disabled,
1755 * hence we can't get into perf_event_task_sched_out for this context.
1756 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001757static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758{
1759 struct perf_event_context *ctx = event->ctx;
1760 struct task_struct *task = ctx->task;
1761
1762 if (!task) {
1763 /*
1764 * Disable the event on the cpu that it's on
1765 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001766 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767 return;
1768 }
1769
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001770retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001771 if (!task_function_call(task, __perf_event_disable, event))
1772 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001774 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775 /*
1776 * If the event is still active, we need to retry the cross-call.
1777 */
1778 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001779 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001780 /*
1781 * Reload the task pointer, it might have been changed by
1782 * a concurrent perf_event_context_sched_out().
1783 */
1784 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785 goto retry;
1786 }
1787
1788 /*
1789 * Since we have the lock this context can't be scheduled
1790 * in, so we can change the state safely.
1791 */
1792 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1793 update_group_times(event);
1794 event->state = PERF_EVENT_STATE_OFF;
1795 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001796 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001798
1799/*
1800 * Strictly speaking kernel users cannot create groups and therefore this
1801 * interface does not need the perf_event_ctx_lock() magic.
1802 */
1803void perf_event_disable(struct perf_event *event)
1804{
1805 struct perf_event_context *ctx;
1806
1807 ctx = perf_event_ctx_lock(event);
1808 _perf_event_disable(event);
1809 perf_event_ctx_unlock(event, ctx);
1810}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001811EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812
Stephane Eraniane5d13672011-02-14 11:20:01 +02001813static void perf_set_shadow_time(struct perf_event *event,
1814 struct perf_event_context *ctx,
1815 u64 tstamp)
1816{
1817 /*
1818 * use the correct time source for the time snapshot
1819 *
1820 * We could get by without this by leveraging the
1821 * fact that to get to this function, the caller
1822 * has most likely already called update_context_time()
1823 * and update_cgrp_time_xx() and thus both timestamp
1824 * are identical (or very close). Given that tstamp is,
1825 * already adjusted for cgroup, we could say that:
1826 * tstamp - ctx->timestamp
1827 * is equivalent to
1828 * tstamp - cgrp->timestamp.
1829 *
1830 * Then, in perf_output_read(), the calculation would
1831 * work with no changes because:
1832 * - event is guaranteed scheduled in
1833 * - no scheduled out in between
1834 * - thus the timestamp would be the same
1835 *
1836 * But this is a bit hairy.
1837 *
1838 * So instead, we have an explicit cgroup call to remain
1839 * within the time time source all along. We believe it
1840 * is cleaner and simpler to understand.
1841 */
1842 if (is_cgroup_event(event))
1843 perf_cgroup_set_shadow_time(event, tstamp);
1844 else
1845 event->shadow_ctx_time = tstamp - ctx->timestamp;
1846}
1847
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001848#define MAX_INTERRUPTS (~0ULL)
1849
1850static void perf_log_throttle(struct perf_event *event, int enable);
1851
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001852static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001853event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001854 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001855 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856{
Stephane Eranian41587552011-01-03 18:20:01 +02001857 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001858 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001859
Peter Zijlstra63342412014-05-05 11:49:16 +02001860 lockdep_assert_held(&ctx->lock);
1861
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001862 if (event->state <= PERF_EVENT_STATE_OFF)
1863 return 0;
1864
1865 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001866 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001867
1868 /*
1869 * Unthrottle events, since we scheduled we might have missed several
1870 * ticks already, also for a heavily scheduling task there is little
1871 * guarantee it'll get a tick in a timely manner.
1872 */
1873 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1874 perf_log_throttle(event, 1);
1875 event->hw.interrupts = 0;
1876 }
1877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878 /*
1879 * The new state must be visible before we turn it on in the hardware:
1880 */
1881 smp_wmb();
1882
Alexander Shishkin44377272013-12-16 14:17:36 +02001883 perf_pmu_disable(event->pmu);
1884
Shaohua Li72f669c2015-02-05 15:55:31 -08001885 event->tstamp_running += tstamp - event->tstamp_stopped;
1886
1887 perf_set_shadow_time(event, ctx, tstamp);
1888
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001889 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890 event->state = PERF_EVENT_STATE_INACTIVE;
1891 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001892 ret = -EAGAIN;
1893 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 }
1895
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896 if (!is_software_event(event))
1897 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001898 if (!ctx->nr_active++)
1899 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001900 if (event->attr.freq && event->attr.sample_freq)
1901 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902
1903 if (event->attr.exclusive)
1904 cpuctx->exclusive = 1;
1905
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001906 if (is_orphaned_child(event))
1907 schedule_orphans_remove(ctx);
1908
Alexander Shishkin44377272013-12-16 14:17:36 +02001909out:
1910 perf_pmu_enable(event->pmu);
1911
1912 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001913}
1914
1915static int
1916group_sched_in(struct perf_event *group_event,
1917 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001918 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919{
Lin Ming6bde9b62010-04-23 13:56:00 +08001920 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001921 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001922 u64 now = ctx->time;
1923 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001924
1925 if (group_event->state == PERF_EVENT_STATE_OFF)
1926 return 0;
1927
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001928 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001929
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001930 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001931 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001932 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001933 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001934 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001935
1936 /*
1937 * Schedule in siblings as one group (if any):
1938 */
1939 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001940 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001941 partial_group = event;
1942 goto group_error;
1943 }
1944 }
1945
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001946 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001947 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001948
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001949group_error:
1950 /*
1951 * Groups can be scheduled in as one unit only, so undo any
1952 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001953 * The events up to the failed event are scheduled out normally,
1954 * tstamp_stopped will be updated.
1955 *
1956 * The failed events and the remaining siblings need to have
1957 * their timings updated as if they had gone thru event_sched_in()
1958 * and event_sched_out(). This is required to get consistent timings
1959 * across the group. This also takes care of the case where the group
1960 * could never be scheduled by ensuring tstamp_stopped is set to mark
1961 * the time the event was actually stopped, such that time delta
1962 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963 */
1964 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1965 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001966 simulate = true;
1967
1968 if (simulate) {
1969 event->tstamp_running += now - event->tstamp_stopped;
1970 event->tstamp_stopped = now;
1971 } else {
1972 event_sched_out(event, cpuctx, ctx);
1973 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001975 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001977 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001978
Stephane Eranian9e630202013-04-03 14:21:33 +02001979 perf_cpu_hrtimer_restart(cpuctx);
1980
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001981 return -EAGAIN;
1982}
1983
1984/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001985 * Work out whether we can put this event group on the CPU now.
1986 */
1987static int group_can_go_on(struct perf_event *event,
1988 struct perf_cpu_context *cpuctx,
1989 int can_add_hw)
1990{
1991 /*
1992 * Groups consisting entirely of software events can always go on.
1993 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001994 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995 return 1;
1996 /*
1997 * If an exclusive group is already on, no other hardware
1998 * events can go on.
1999 */
2000 if (cpuctx->exclusive)
2001 return 0;
2002 /*
2003 * If this group is exclusive and there are already
2004 * events on the CPU, it can't go on.
2005 */
2006 if (event->attr.exclusive && cpuctx->active_oncpu)
2007 return 0;
2008 /*
2009 * Otherwise, try to add it if all previous groups were able
2010 * to go on.
2011 */
2012 return can_add_hw;
2013}
2014
2015static void add_event_to_ctx(struct perf_event *event,
2016 struct perf_event_context *ctx)
2017{
Stephane Eranian41587552011-01-03 18:20:01 +02002018 u64 tstamp = perf_event_time(event);
2019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002021 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002022 event->tstamp_enabled = tstamp;
2023 event->tstamp_running = tstamp;
2024 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002025}
2026
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002027static void task_ctx_sched_out(struct perf_event_context *ctx);
2028static void
2029ctx_sched_in(struct perf_event_context *ctx,
2030 struct perf_cpu_context *cpuctx,
2031 enum event_type_t event_type,
2032 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002033
Peter Zijlstradce58552011-04-09 21:17:46 +02002034static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2035 struct perf_event_context *ctx,
2036 struct task_struct *task)
2037{
2038 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2039 if (ctx)
2040 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2041 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2042 if (ctx)
2043 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2044}
2045
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002046/*
2047 * Cross CPU call to install and enable a performance event
2048 *
2049 * Must be called with ctx->mutex held
2050 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002051static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053 struct perf_event *event = info;
2054 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002055 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002056 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2057 struct task_struct *task = current;
2058
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002059 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002060 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061
2062 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002063 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002065 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002066 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002067
2068 /*
2069 * If the context we're installing events in is not the
2070 * active task_ctx, flip them.
2071 */
2072 if (ctx->task && task_ctx != ctx) {
2073 if (task_ctx)
2074 raw_spin_unlock(&task_ctx->lock);
2075 raw_spin_lock(&ctx->lock);
2076 task_ctx = ctx;
2077 }
2078
2079 if (task_ctx) {
2080 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002081 task = task_ctx->task;
2082 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002083
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002084 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002086 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002087 /*
2088 * update cgrp time only if current cgrp
2089 * matches event->cgrp. Must be done before
2090 * calling add_event_to_ctx()
2091 */
2092 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094 add_event_to_ctx(event, ctx);
2095
2096 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002097 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 */
Peter Zijlstradce58552011-04-09 21:17:46 +02002099 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002100
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002101 perf_pmu_enable(cpuctx->ctx.pmu);
2102 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002103
2104 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105}
2106
2107/*
2108 * Attach a performance event to a context
2109 *
2110 * First we add the event to the list with the hardware enable bit
2111 * in event->hw_config cleared.
2112 *
2113 * If the event is attached to a task which is on a CPU we use a smp
2114 * call to enable it in the task context. The task might have been
2115 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116 */
2117static void
2118perf_install_in_context(struct perf_event_context *ctx,
2119 struct perf_event *event,
2120 int cpu)
2121{
2122 struct task_struct *task = ctx->task;
2123
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002124 lockdep_assert_held(&ctx->mutex);
2125
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002126 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002127 if (event->cpu != -1)
2128 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002130 if (!task) {
2131 /*
2132 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002133 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002134 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002135 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 return;
2137 }
2138
2139retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002140 if (!task_function_call(task, __perf_install_in_context, event))
2141 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002143 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002144 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002145 * If we failed to find a running task, but find the context active now
2146 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002148 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002149 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07002150 /*
2151 * Reload the task pointer, it might have been changed by
2152 * a concurrent perf_event_context_sched_out().
2153 */
2154 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155 goto retry;
2156 }
2157
2158 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002159 * Since the task isn't running, its safe to add the event, us holding
2160 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002161 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002162 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002163 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002164}
2165
2166/*
2167 * Put a event into inactive state and update time fields.
2168 * Enabling the leader of a group effectively enables all
2169 * the group members that aren't explicitly disabled, so we
2170 * have to update their ->tstamp_enabled also.
2171 * Note: this works for group members as well as group leaders
2172 * since the non-leader members' sibling_lists will be empty.
2173 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002174static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175{
2176 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002177 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002178
2179 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002180 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002181 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002182 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2183 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002184 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002185}
2186
2187/*
2188 * Cross CPU call to enable a performance event
2189 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002190static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191{
2192 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002193 struct perf_event_context *ctx = event->ctx;
2194 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002195 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002196 int err;
2197
Jiri Olsa06f41792013-07-09 17:44:11 +02002198 /*
2199 * There's a time window between 'ctx->is_active' check
2200 * in perf_event_enable function and this place having:
2201 * - IRQs on
2202 * - ctx->lock unlocked
2203 *
2204 * where the task could be killed and 'ctx' deactivated
2205 * by perf_event_exit_task.
2206 */
2207 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002208 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002209
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002210 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002211 update_context_time(ctx);
2212
2213 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2214 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002215
2216 /*
2217 * set current task's cgroup time reference point
2218 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002219 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002220
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002221 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002222
Stephane Eraniane5d13672011-02-14 11:20:01 +02002223 if (!event_filter_match(event)) {
2224 if (is_cgroup_event(event))
2225 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002226 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002227 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229 /*
2230 * If the event is in a group and isn't the group leader,
2231 * then don't put it on unless the group is on.
2232 */
2233 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2234 goto unlock;
2235
2236 if (!group_can_go_on(event, cpuctx, 1)) {
2237 err = -EEXIST;
2238 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002239 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002240 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002241 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002242 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002243 }
2244
2245 if (err) {
2246 /*
2247 * If this event can't go on and it's part of a
2248 * group, then the whole group has to come off.
2249 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002250 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002252 perf_cpu_hrtimer_restart(cpuctx);
2253 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254 if (leader->attr.pinned) {
2255 update_group_times(leader);
2256 leader->state = PERF_EVENT_STATE_ERROR;
2257 }
2258 }
2259
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002260unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002261 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002262
2263 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264}
2265
2266/*
2267 * Enable a event.
2268 *
2269 * If event->ctx is a cloned context, callers must make sure that
2270 * every task struct that event->ctx->task could possibly point to
2271 * remains valid. This condition is satisfied when called through
2272 * perf_event_for_each_child or perf_event_for_each as described
2273 * for perf_event_disable.
2274 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002275static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002276{
2277 struct perf_event_context *ctx = event->ctx;
2278 struct task_struct *task = ctx->task;
2279
2280 if (!task) {
2281 /*
2282 * Enable the event on the cpu that it's on
2283 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002284 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002285 return;
2286 }
2287
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002288 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002289 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2290 goto out;
2291
2292 /*
2293 * If the event is in error state, clear that first.
2294 * That way, if we see the event in error state below, we
2295 * know that it has gone back into error state, as distinct
2296 * from the task having been scheduled away before the
2297 * cross-call arrived.
2298 */
2299 if (event->state == PERF_EVENT_STATE_ERROR)
2300 event->state = PERF_EVENT_STATE_OFF;
2301
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002302retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002303 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002304 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002305 goto out;
2306 }
2307
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002308 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002309
2310 if (!task_function_call(task, __perf_event_enable, event))
2311 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002313 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314
2315 /*
2316 * If the context is active and the event is still off,
2317 * we need to retry the cross-call.
2318 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002319 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2320 /*
2321 * task could have been flipped by a concurrent
2322 * perf_event_context_sched_out()
2323 */
2324 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002326 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002327
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002328out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002329 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002330}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002331
2332/*
2333 * See perf_event_disable();
2334 */
2335void perf_event_enable(struct perf_event *event)
2336{
2337 struct perf_event_context *ctx;
2338
2339 ctx = perf_event_ctx_lock(event);
2340 _perf_event_enable(event);
2341 perf_event_ctx_unlock(event, ctx);
2342}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002343EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002344
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002345static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002346{
2347 /*
2348 * not supported on inherited events
2349 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002350 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002351 return -EINVAL;
2352
2353 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002354 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355
2356 return 0;
2357}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002358
2359/*
2360 * See perf_event_disable()
2361 */
2362int perf_event_refresh(struct perf_event *event, int refresh)
2363{
2364 struct perf_event_context *ctx;
2365 int ret;
2366
2367 ctx = perf_event_ctx_lock(event);
2368 ret = _perf_event_refresh(event, refresh);
2369 perf_event_ctx_unlock(event, ctx);
2370
2371 return ret;
2372}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002373EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002374
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002375static void ctx_sched_out(struct perf_event_context *ctx,
2376 struct perf_cpu_context *cpuctx,
2377 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002378{
2379 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002380 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002381
Peter Zijlstradb24d332011-04-09 21:17:45 +02002382 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002384 return;
2385
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002386 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002387 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002388 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002389 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002390
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002391 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002392 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002393 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2394 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002395 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002396
Peter Zijlstradb24d332011-04-09 21:17:45 +02002397 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002398 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002399 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002400 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002401 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002402}
2403
2404/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002405 * Test whether two contexts are equivalent, i.e. whether they have both been
2406 * cloned from the same version of the same context.
2407 *
2408 * Equivalence is measured using a generation number in the context that is
2409 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2410 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002411 */
2412static int context_equiv(struct perf_event_context *ctx1,
2413 struct perf_event_context *ctx2)
2414{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002415 lockdep_assert_held(&ctx1->lock);
2416 lockdep_assert_held(&ctx2->lock);
2417
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002418 /* Pinning disables the swap optimization */
2419 if (ctx1->pin_count || ctx2->pin_count)
2420 return 0;
2421
2422 /* If ctx1 is the parent of ctx2 */
2423 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2424 return 1;
2425
2426 /* If ctx2 is the parent of ctx1 */
2427 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2428 return 1;
2429
2430 /*
2431 * If ctx1 and ctx2 have the same parent; we flatten the parent
2432 * hierarchy, see perf_event_init_context().
2433 */
2434 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2435 ctx1->parent_gen == ctx2->parent_gen)
2436 return 1;
2437
2438 /* Unmatched */
2439 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002440}
2441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002442static void __perf_event_sync_stat(struct perf_event *event,
2443 struct perf_event *next_event)
2444{
2445 u64 value;
2446
2447 if (!event->attr.inherit_stat)
2448 return;
2449
2450 /*
2451 * Update the event value, we cannot use perf_event_read()
2452 * because we're in the middle of a context switch and have IRQs
2453 * disabled, which upsets smp_call_function_single(), however
2454 * we know the event must be on the current CPU, therefore we
2455 * don't need to use it.
2456 */
2457 switch (event->state) {
2458 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002459 event->pmu->read(event);
2460 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002461
2462 case PERF_EVENT_STATE_INACTIVE:
2463 update_event_times(event);
2464 break;
2465
2466 default:
2467 break;
2468 }
2469
2470 /*
2471 * In order to keep per-task stats reliable we need to flip the event
2472 * values when we flip the contexts.
2473 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002474 value = local64_read(&next_event->count);
2475 value = local64_xchg(&event->count, value);
2476 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002477
2478 swap(event->total_time_enabled, next_event->total_time_enabled);
2479 swap(event->total_time_running, next_event->total_time_running);
2480
2481 /*
2482 * Since we swizzled the values, update the user visible data too.
2483 */
2484 perf_event_update_userpage(event);
2485 perf_event_update_userpage(next_event);
2486}
2487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488static void perf_event_sync_stat(struct perf_event_context *ctx,
2489 struct perf_event_context *next_ctx)
2490{
2491 struct perf_event *event, *next_event;
2492
2493 if (!ctx->nr_stat)
2494 return;
2495
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002496 update_context_time(ctx);
2497
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002498 event = list_first_entry(&ctx->event_list,
2499 struct perf_event, event_entry);
2500
2501 next_event = list_first_entry(&next_ctx->event_list,
2502 struct perf_event, event_entry);
2503
2504 while (&event->event_entry != &ctx->event_list &&
2505 &next_event->event_entry != &next_ctx->event_list) {
2506
2507 __perf_event_sync_stat(event, next_event);
2508
2509 event = list_next_entry(event, event_entry);
2510 next_event = list_next_entry(next_event, event_entry);
2511 }
2512}
2513
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002514static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2515 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002517 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002518 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002519 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002520 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002521 int do_switch = 1;
2522
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002523 if (likely(!ctx))
2524 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002525
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002526 cpuctx = __get_cpu_context(ctx);
2527 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002528 return;
2529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002530 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002531 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002532 if (!next_ctx)
2533 goto unlock;
2534
2535 parent = rcu_dereference(ctx->parent_ctx);
2536 next_parent = rcu_dereference(next_ctx->parent_ctx);
2537
2538 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002539 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002540 goto unlock;
2541
2542 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002543 /*
2544 * Looks like the two contexts are clones, so we might be
2545 * able to optimize the context switch. We lock both
2546 * contexts and check that they are clones under the
2547 * lock (including re-checking that neither has been
2548 * uncloned in the meantime). It doesn't matter which
2549 * order we take the locks because no other cpu could
2550 * be trying to lock both of these tasks.
2551 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002552 raw_spin_lock(&ctx->lock);
2553 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002554 if (context_equiv(ctx, next_ctx)) {
2555 /*
2556 * XXX do we need a memory barrier of sorts
2557 * wrt to rcu_dereference() of perf_event_ctxp
2558 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002559 task->perf_event_ctxp[ctxn] = next_ctx;
2560 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002561 ctx->task = next;
2562 next_ctx->task = task;
2563 do_switch = 0;
2564
2565 perf_event_sync_stat(ctx, next_ctx);
2566 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002567 raw_spin_unlock(&next_ctx->lock);
2568 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002569 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002570unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002571 rcu_read_unlock();
2572
2573 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002574 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002575 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002576 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002577 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002578 }
2579}
2580
Yan, Zhengba532502014-11-04 21:55:58 -05002581void perf_sched_cb_dec(struct pmu *pmu)
2582{
2583 this_cpu_dec(perf_sched_cb_usages);
2584}
2585
2586void perf_sched_cb_inc(struct pmu *pmu)
2587{
2588 this_cpu_inc(perf_sched_cb_usages);
2589}
2590
2591/*
2592 * This function provides the context switch callback to the lower code
2593 * layer. It is invoked ONLY when the context switch callback is enabled.
2594 */
2595static void perf_pmu_sched_task(struct task_struct *prev,
2596 struct task_struct *next,
2597 bool sched_in)
2598{
2599 struct perf_cpu_context *cpuctx;
2600 struct pmu *pmu;
2601 unsigned long flags;
2602
2603 if (prev == next)
2604 return;
2605
2606 local_irq_save(flags);
2607
2608 rcu_read_lock();
2609
2610 list_for_each_entry_rcu(pmu, &pmus, entry) {
2611 if (pmu->sched_task) {
2612 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2613
2614 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2615
2616 perf_pmu_disable(pmu);
2617
2618 pmu->sched_task(cpuctx->task_ctx, sched_in);
2619
2620 perf_pmu_enable(pmu);
2621
2622 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2623 }
2624 }
2625
2626 rcu_read_unlock();
2627
2628 local_irq_restore(flags);
2629}
2630
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002631#define for_each_task_context_nr(ctxn) \
2632 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2633
2634/*
2635 * Called from scheduler to remove the events of the current task,
2636 * with interrupts disabled.
2637 *
2638 * We stop each event and update the event value in event->count.
2639 *
2640 * This does not protect us against NMI, but disable()
2641 * sets the disabled bit in the control field of event _before_
2642 * accessing the event control register. If a NMI hits, then it will
2643 * not restart the event.
2644 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002645void __perf_event_task_sched_out(struct task_struct *task,
2646 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002647{
2648 int ctxn;
2649
Yan, Zhengba532502014-11-04 21:55:58 -05002650 if (__this_cpu_read(perf_sched_cb_usages))
2651 perf_pmu_sched_task(task, next, false);
2652
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002653 for_each_task_context_nr(ctxn)
2654 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002655
2656 /*
2657 * if cgroup events exist on this CPU, then we need
2658 * to check if we have to switch out PMU state.
2659 * cgroup event are system-wide mode only
2660 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002661 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002662 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002663}
2664
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002665static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002666{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002667 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002668
2669 if (!cpuctx->task_ctx)
2670 return;
2671
2672 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2673 return;
2674
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002675 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002676 cpuctx->task_ctx = NULL;
2677}
2678
2679/*
2680 * Called with IRQs disabled
2681 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002682static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2683 enum event_type_t event_type)
2684{
2685 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002686}
2687
2688static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002689ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002690 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691{
2692 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002694 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2695 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002697 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002698 continue;
2699
Stephane Eraniane5d13672011-02-14 11:20:01 +02002700 /* may need to reset tstamp_enabled */
2701 if (is_cgroup_event(event))
2702 perf_cgroup_mark_enabled(event, ctx);
2703
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002704 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002705 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002706
2707 /*
2708 * If this pinned group hasn't been scheduled,
2709 * put it in error state.
2710 */
2711 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2712 update_group_times(event);
2713 event->state = PERF_EVENT_STATE_ERROR;
2714 }
2715 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002716}
2717
2718static void
2719ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002720 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002721{
2722 struct perf_event *event;
2723 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002725 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2726 /* Ignore events in OFF or ERROR state */
2727 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729 /*
2730 * Listen to the 'cpu' scheduling filter constraint
2731 * of events:
2732 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002733 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734 continue;
2735
Stephane Eraniane5d13672011-02-14 11:20:01 +02002736 /* may need to reset tstamp_enabled */
2737 if (is_cgroup_event(event))
2738 perf_cgroup_mark_enabled(event, ctx);
2739
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002740 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002741 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002743 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002745}
2746
2747static void
2748ctx_sched_in(struct perf_event_context *ctx,
2749 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002750 enum event_type_t event_type,
2751 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002752{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002753 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002754 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002755
Peter Zijlstradb24d332011-04-09 21:17:45 +02002756 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002757 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002758 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002759
Stephane Eraniane5d13672011-02-14 11:20:01 +02002760 now = perf_clock();
2761 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002762 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002763 /*
2764 * First go through the list and put on any pinned groups
2765 * in order to give them the best chance of going on.
2766 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002767 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002768 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002769
2770 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002771 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002772 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773}
2774
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002775static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002776 enum event_type_t event_type,
2777 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002778{
2779 struct perf_event_context *ctx = &cpuctx->ctx;
2780
Stephane Eraniane5d13672011-02-14 11:20:01 +02002781 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002782}
2783
Stephane Eraniane5d13672011-02-14 11:20:01 +02002784static void perf_event_context_sched_in(struct perf_event_context *ctx,
2785 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002786{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002787 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002788
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002789 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002790 if (cpuctx->task_ctx == ctx)
2791 return;
2792
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002793 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002794 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002795 /*
2796 * We want to keep the following priority order:
2797 * cpu pinned (that don't need to move), task pinned,
2798 * cpu flexible, task flexible.
2799 */
2800 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2801
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002802 if (ctx->nr_events)
2803 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002804
Gleb Natapov86b47c22011-11-22 16:08:21 +02002805 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2806
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002807 perf_pmu_enable(ctx->pmu);
2808 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002809}
2810
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002811/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002812 * When sampling the branck stack in system-wide, it may be necessary
2813 * to flush the stack on context switch. This happens when the branch
2814 * stack does not tag its entries with the pid of the current task.
2815 * Otherwise it becomes impossible to associate a branch entry with a
2816 * task. This ambiguity is more likely to appear when the branch stack
2817 * supports priv level filtering and the user sets it to monitor only
2818 * at the user level (which could be a useful measurement in system-wide
2819 * mode). In that case, the risk is high of having a branch stack with
2820 * branch from multiple tasks. Flushing may mean dropping the existing
2821 * entries or stashing them somewhere in the PMU specific code layer.
2822 *
2823 * This function provides the context switch callback to the lower code
2824 * layer. It is invoked ONLY when there is at least one system-wide context
2825 * with at least one active event using taken branch sampling.
2826 */
2827static void perf_branch_stack_sched_in(struct task_struct *prev,
2828 struct task_struct *task)
2829{
2830 struct perf_cpu_context *cpuctx;
2831 struct pmu *pmu;
2832 unsigned long flags;
2833
2834 /* no need to flush branch stack if not changing task */
2835 if (prev == task)
2836 return;
2837
2838 local_irq_save(flags);
2839
2840 rcu_read_lock();
2841
2842 list_for_each_entry_rcu(pmu, &pmus, entry) {
2843 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2844
2845 /*
2846 * check if the context has at least one
2847 * event using PERF_SAMPLE_BRANCH_STACK
2848 */
2849 if (cpuctx->ctx.nr_branch_stack > 0
2850 && pmu->flush_branch_stack) {
2851
Stephane Eraniand010b332012-02-09 23:21:00 +01002852 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2853
2854 perf_pmu_disable(pmu);
2855
2856 pmu->flush_branch_stack();
2857
2858 perf_pmu_enable(pmu);
2859
2860 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2861 }
2862 }
2863
2864 rcu_read_unlock();
2865
2866 local_irq_restore(flags);
2867}
2868
2869/*
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002870 * Called from scheduler to add the events of the current task
2871 * with interrupts disabled.
2872 *
2873 * We restore the event value and then enable it.
2874 *
2875 * This does not protect us against NMI, but enable()
2876 * sets the enabled bit in the control field of event _before_
2877 * accessing the event control register. If a NMI hits, then it will
2878 * keep the event running.
2879 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002880void __perf_event_task_sched_in(struct task_struct *prev,
2881 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002882{
2883 struct perf_event_context *ctx;
2884 int ctxn;
2885
2886 for_each_task_context_nr(ctxn) {
2887 ctx = task->perf_event_ctxp[ctxn];
2888 if (likely(!ctx))
2889 continue;
2890
Stephane Eraniane5d13672011-02-14 11:20:01 +02002891 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002892 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002893 /*
2894 * if cgroup events exist on this CPU, then we need
2895 * to check if we have to switch in PMU state.
2896 * cgroup event are system-wide mode only
2897 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002898 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002899 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002900
2901 /* check for system-wide branch_stack events */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002902 if (atomic_read(this_cpu_ptr(&perf_branch_stack_events)))
Stephane Eraniand010b332012-02-09 23:21:00 +01002903 perf_branch_stack_sched_in(prev, task);
Yan, Zhengba532502014-11-04 21:55:58 -05002904
2905 if (__this_cpu_read(perf_sched_cb_usages))
2906 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907}
2908
Peter Zijlstraabd50712010-01-26 18:50:16 +01002909static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2910{
2911 u64 frequency = event->attr.sample_freq;
2912 u64 sec = NSEC_PER_SEC;
2913 u64 divisor, dividend;
2914
2915 int count_fls, nsec_fls, frequency_fls, sec_fls;
2916
2917 count_fls = fls64(count);
2918 nsec_fls = fls64(nsec);
2919 frequency_fls = fls64(frequency);
2920 sec_fls = 30;
2921
2922 /*
2923 * We got @count in @nsec, with a target of sample_freq HZ
2924 * the target period becomes:
2925 *
2926 * @count * 10^9
2927 * period = -------------------
2928 * @nsec * sample_freq
2929 *
2930 */
2931
2932 /*
2933 * Reduce accuracy by one bit such that @a and @b converge
2934 * to a similar magnitude.
2935 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002936#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002937do { \
2938 if (a##_fls > b##_fls) { \
2939 a >>= 1; \
2940 a##_fls--; \
2941 } else { \
2942 b >>= 1; \
2943 b##_fls--; \
2944 } \
2945} while (0)
2946
2947 /*
2948 * Reduce accuracy until either term fits in a u64, then proceed with
2949 * the other, so that finally we can do a u64/u64 division.
2950 */
2951 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2952 REDUCE_FLS(nsec, frequency);
2953 REDUCE_FLS(sec, count);
2954 }
2955
2956 if (count_fls + sec_fls > 64) {
2957 divisor = nsec * frequency;
2958
2959 while (count_fls + sec_fls > 64) {
2960 REDUCE_FLS(count, sec);
2961 divisor >>= 1;
2962 }
2963
2964 dividend = count * sec;
2965 } else {
2966 dividend = count * sec;
2967
2968 while (nsec_fls + frequency_fls > 64) {
2969 REDUCE_FLS(nsec, frequency);
2970 dividend >>= 1;
2971 }
2972
2973 divisor = nsec * frequency;
2974 }
2975
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002976 if (!divisor)
2977 return dividend;
2978
Peter Zijlstraabd50712010-01-26 18:50:16 +01002979 return div64_u64(dividend, divisor);
2980}
2981
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002982static DEFINE_PER_CPU(int, perf_throttled_count);
2983static DEFINE_PER_CPU(u64, perf_throttled_seq);
2984
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002985static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002986{
2987 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002988 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989 s64 delta;
2990
Peter Zijlstraabd50712010-01-26 18:50:16 +01002991 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992
2993 delta = (s64)(period - hwc->sample_period);
2994 delta = (delta + 7) / 8; /* low pass filter */
2995
2996 sample_period = hwc->sample_period + delta;
2997
2998 if (!sample_period)
2999 sample_period = 1;
3000
3001 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003002
Peter Zijlstrae7850592010-05-21 14:43:08 +02003003 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003004 if (disable)
3005 event->pmu->stop(event, PERF_EF_UPDATE);
3006
Peter Zijlstrae7850592010-05-21 14:43:08 +02003007 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003008
3009 if (disable)
3010 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003011 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003012}
3013
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003014/*
3015 * combine freq adjustment with unthrottling to avoid two passes over the
3016 * events. At the same time, make sure, having freq events does not change
3017 * the rate of unthrottling as that would introduce bias.
3018 */
3019static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3020 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003021{
3022 struct perf_event *event;
3023 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003024 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01003025 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003027 /*
3028 * only need to iterate over all events iff:
3029 * - context have events in frequency mode (needs freq adjust)
3030 * - there are events to unthrottle on this cpu
3031 */
3032 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003033 return;
3034
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003035 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003036 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003037
Paul Mackerras03541f82009-10-14 16:58:03 +11003038 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003039 if (event->state != PERF_EVENT_STATE_ACTIVE)
3040 continue;
3041
Stephane Eranian5632ab12011-01-03 18:20:01 +02003042 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003043 continue;
3044
Alexander Shishkin44377272013-12-16 14:17:36 +02003045 perf_pmu_disable(event->pmu);
3046
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047 hwc = &event->hw;
3048
Jiri Olsaae23bff2013-08-24 16:45:54 +02003049 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003050 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003052 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053 }
3054
3055 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02003056 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003058 /*
3059 * stop the event and update event->count
3060 */
3061 event->pmu->stop(event, PERF_EF_UPDATE);
3062
Peter Zijlstrae7850592010-05-21 14:43:08 +02003063 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003064 delta = now - hwc->freq_count_stamp;
3065 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003067 /*
3068 * restart the event
3069 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003070 * we have stopped the event so tell that
3071 * to perf_adjust_period() to avoid stopping it
3072 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003073 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003074 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003075 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003076
3077 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003078 next:
3079 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003080 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003081
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003082 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003083 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084}
3085
3086/*
3087 * Round-robin a context's events:
3088 */
3089static void rotate_ctx(struct perf_event_context *ctx)
3090{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003091 /*
3092 * Rotate the first entry last of non-pinned groups. Rotation might be
3093 * disabled by the inheritance code.
3094 */
3095 if (!ctx->rotate_disable)
3096 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003097}
3098
Stephane Eranian9e630202013-04-03 14:21:33 +02003099static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003100{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003101 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003102 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003103
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003104 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003105 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3106 rotate = 1;
3107 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003109 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003110 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003111 if (ctx->nr_events != ctx->nr_active)
3112 rotate = 1;
3113 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003115 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003116 goto done;
3117
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003118 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003119 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003121 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3122 if (ctx)
3123 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003124
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003125 rotate_ctx(&cpuctx->ctx);
3126 if (ctx)
3127 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003129 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003130
3131 perf_pmu_enable(cpuctx->ctx.pmu);
3132 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003133done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003134
3135 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003136}
3137
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003138#ifdef CONFIG_NO_HZ_FULL
3139bool perf_event_can_stop_tick(void)
3140{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003141 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003142 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003143 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003144 else
3145 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003146}
3147#endif
3148
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003149void perf_event_task_tick(void)
3150{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003151 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3152 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003153 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003154
3155 WARN_ON(!irqs_disabled());
3156
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003157 __this_cpu_inc(perf_throttled_seq);
3158 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3159
Mark Rutland2fde4f92015-01-07 15:01:54 +00003160 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003161 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003162}
3163
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003164static int event_enable_on_exec(struct perf_event *event,
3165 struct perf_event_context *ctx)
3166{
3167 if (!event->attr.enable_on_exec)
3168 return 0;
3169
3170 event->attr.enable_on_exec = 0;
3171 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3172 return 0;
3173
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003174 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003175
3176 return 1;
3177}
3178
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003179/*
3180 * Enable all of a task's events that have been marked enable-on-exec.
3181 * This expects task == current.
3182 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003183static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003184{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003185 struct perf_event_context *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003186 struct perf_event *event;
3187 unsigned long flags;
3188 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003189 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003190
3191 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192 if (!ctx || !ctx->nr_events)
3193 goto out;
3194
Stephane Eraniane566b762011-04-06 02:54:54 +02003195 /*
3196 * We must ctxsw out cgroup events to avoid conflict
3197 * when invoking perf_task_event_sched_in() later on
3198 * in this function. Otherwise we end up trying to
3199 * ctxswin cgroup events which are already scheduled
3200 * in.
3201 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003202 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003204 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003205 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003207 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003208 ret = event_enable_on_exec(event, ctx);
3209 if (ret)
3210 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211 }
3212
3213 /*
3214 * Unclone this context if we enabled any event.
3215 */
3216 if (enabled)
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003217 clone_ctx = unclone_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003219 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220
Stephane Eraniane566b762011-04-06 02:54:54 +02003221 /*
3222 * Also calls ctxswin for cgroup events, if any:
3223 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003224 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003225out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003226 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003227
3228 if (clone_ctx)
3229 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003230}
3231
Peter Zijlstrae041e322014-05-21 17:32:19 +02003232void perf_event_exec(void)
3233{
3234 struct perf_event_context *ctx;
3235 int ctxn;
3236
3237 rcu_read_lock();
3238 for_each_task_context_nr(ctxn) {
3239 ctx = current->perf_event_ctxp[ctxn];
3240 if (!ctx)
3241 continue;
3242
3243 perf_event_enable_on_exec(ctx);
3244 }
3245 rcu_read_unlock();
3246}
3247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003248/*
3249 * Cross CPU call to read the hardware event
3250 */
3251static void __perf_event_read(void *info)
3252{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253 struct perf_event *event = info;
3254 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003255 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003256
3257 /*
3258 * If this is a task context, we need to check whether it is
3259 * the current task context of this cpu. If not it has been
3260 * scheduled out before the smp call arrived. In that case
3261 * event->count would have been updated to a recent sample
3262 * when the event was scheduled out.
3263 */
3264 if (ctx->task && cpuctx->task_ctx != ctx)
3265 return;
3266
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003267 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003268 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003269 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003270 update_cgrp_time_from_event(event);
3271 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003272 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003273 if (event->state == PERF_EVENT_STATE_ACTIVE)
3274 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003275 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003276}
3277
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003278static inline u64 perf_event_count(struct perf_event *event)
3279{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003280 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003281}
3282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283static u64 perf_event_read(struct perf_event *event)
3284{
3285 /*
3286 * If event is enabled and currently active on a CPU, update the
3287 * value in the event structure:
3288 */
3289 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3290 smp_call_function_single(event->oncpu,
3291 __perf_event_read, event, 1);
3292 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003293 struct perf_event_context *ctx = event->ctx;
3294 unsigned long flags;
3295
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003296 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003297 /*
3298 * may read while context is not active
3299 * (e.g., thread is blocked), in that case
3300 * we cannot update context time
3301 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003302 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003303 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003304 update_cgrp_time_from_event(event);
3305 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003306 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003307 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308 }
3309
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003310 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003311}
3312
3313/*
3314 * Initialize the perf_event context in a task_struct:
3315 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003316static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003318 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003319 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003320 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003321 INIT_LIST_HEAD(&ctx->pinned_groups);
3322 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323 INIT_LIST_HEAD(&ctx->event_list);
3324 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003325 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326}
3327
Peter Zijlstraeb184472010-09-07 15:55:13 +02003328static struct perf_event_context *
3329alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330{
3331 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003332
3333 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3334 if (!ctx)
3335 return NULL;
3336
3337 __perf_event_init_context(ctx);
3338 if (task) {
3339 ctx->task = task;
3340 get_task_struct(task);
3341 }
3342 ctx->pmu = pmu;
3343
3344 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345}
3346
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003347static struct task_struct *
3348find_lively_task_by_vpid(pid_t vpid)
3349{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351 int err;
3352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003354 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003355 task = current;
3356 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003357 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358 if (task)
3359 get_task_struct(task);
3360 rcu_read_unlock();
3361
3362 if (!task)
3363 return ERR_PTR(-ESRCH);
3364
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365 /* Reuse ptrace permission checks for now. */
3366 err = -EACCES;
3367 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3368 goto errout;
3369
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003370 return task;
3371errout:
3372 put_task_struct(task);
3373 return ERR_PTR(err);
3374
3375}
3376
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003377/*
3378 * Returns a matching context with refcount and pincount.
3379 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003380static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003381find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003383 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003385 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003386 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003388 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389 /* Must be root to operate on a CPU event: */
3390 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3391 return ERR_PTR(-EACCES);
3392
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393 /*
3394 * We could be clever and allow to attach a event to an
3395 * offline CPU and activate it when the CPU comes up, but
3396 * that's for later.
3397 */
3398 if (!cpu_online(cpu))
3399 return ERR_PTR(-ENODEV);
3400
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003401 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 ctx = &cpuctx->ctx;
3403 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003404 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405
3406 return ctx;
3407 }
3408
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003409 err = -EINVAL;
3410 ctxn = pmu->task_ctx_nr;
3411 if (ctxn < 0)
3412 goto errout;
3413
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003414retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003415 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003416 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003417 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003418 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003419 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003420
3421 if (clone_ctx)
3422 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003423 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003424 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425 err = -ENOMEM;
3426 if (!ctx)
3427 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003428
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003429 err = 0;
3430 mutex_lock(&task->perf_event_mutex);
3431 /*
3432 * If it has already passed perf_event_exit_task().
3433 * we must see PF_EXITING, it takes this mutex too.
3434 */
3435 if (task->flags & PF_EXITING)
3436 err = -ESRCH;
3437 else if (task->perf_event_ctxp[ctxn])
3438 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003439 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003440 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003441 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003442 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003443 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003444 mutex_unlock(&task->perf_event_mutex);
3445
3446 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003447 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003448
3449 if (err == -EAGAIN)
3450 goto retry;
3451 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453 }
3454
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455 return ctx;
3456
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003457errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458 return ERR_PTR(err);
3459}
3460
Li Zefan6fb29152009-10-15 11:21:42 +08003461static void perf_event_free_filter(struct perf_event *event);
3462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003463static void free_event_rcu(struct rcu_head *head)
3464{
3465 struct perf_event *event;
3466
3467 event = container_of(head, struct perf_event, rcu_head);
3468 if (event->ns)
3469 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003470 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003471 kfree(event);
3472}
3473
Frederic Weisbecker76369132011-05-19 19:55:04 +02003474static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003475static void ring_buffer_attach(struct perf_event *event,
3476 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003478static void unaccount_event_cpu(struct perf_event *event, int cpu)
3479{
3480 if (event->parent)
3481 return;
3482
3483 if (has_branch_stack(event)) {
3484 if (!(event->attach_state & PERF_ATTACH_TASK))
3485 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3486 }
3487 if (is_cgroup_event(event))
3488 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3489}
3490
3491static void unaccount_event(struct perf_event *event)
3492{
3493 if (event->parent)
3494 return;
3495
3496 if (event->attach_state & PERF_ATTACH_TASK)
3497 static_key_slow_dec_deferred(&perf_sched_events);
3498 if (event->attr.mmap || event->attr.mmap_data)
3499 atomic_dec(&nr_mmap_events);
3500 if (event->attr.comm)
3501 atomic_dec(&nr_comm_events);
3502 if (event->attr.task)
3503 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003504 if (event->attr.freq)
3505 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003506 if (is_cgroup_event(event))
3507 static_key_slow_dec_deferred(&perf_sched_events);
3508 if (has_branch_stack(event))
3509 static_key_slow_dec_deferred(&perf_sched_events);
3510
3511 unaccount_event_cpu(event, event->cpu);
3512}
3513
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003514static void __free_event(struct perf_event *event)
3515{
3516 if (!event->parent) {
3517 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3518 put_callchain_buffers();
3519 }
3520
3521 if (event->destroy)
3522 event->destroy(event);
3523
3524 if (event->ctx)
3525 put_ctx(event->ctx);
3526
Yan, Zhengc464c762014-03-18 16:56:41 +08003527 if (event->pmu)
3528 module_put(event->pmu->module);
3529
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003530 call_rcu(&event->rcu_head, free_event_rcu);
3531}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003532
3533static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003534{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003535 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003536
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003537 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003538
Frederic Weisbecker76369132011-05-19 19:55:04 +02003539 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003540 /*
3541 * Can happen when we close an event with re-directed output.
3542 *
3543 * Since we have a 0 refcount, perf_mmap_close() will skip
3544 * over us; possibly making our ring_buffer_put() the last.
3545 */
3546 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003547 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003548 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003549 }
3550
Stephane Eraniane5d13672011-02-14 11:20:01 +02003551 if (is_cgroup_event(event))
3552 perf_detach_cgroup(event);
3553
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003554 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003555}
3556
Peter Zijlstra683ede42014-05-05 12:11:24 +02003557/*
3558 * Used to free events which have a known refcount of 1, such as in error paths
3559 * where the event isn't exposed yet and inherited events.
3560 */
3561static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003562{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003563 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3564 "unexpected event refcount: %ld; ptr=%p\n",
3565 atomic_long_read(&event->refcount), event)) {
3566 /* leak to avoid use-after-free */
3567 return;
3568 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003569
Peter Zijlstra683ede42014-05-05 12:11:24 +02003570 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003571}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003572
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003573/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003574 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003575 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003576static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003577{
Peter Zijlstra88821352010-11-09 19:01:43 +01003578 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003579
Peter Zijlstra88821352010-11-09 19:01:43 +01003580 rcu_read_lock();
3581 owner = ACCESS_ONCE(event->owner);
3582 /*
3583 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3584 * !owner it means the list deletion is complete and we can indeed
3585 * free this event, otherwise we need to serialize on
3586 * owner->perf_event_mutex.
3587 */
3588 smp_read_barrier_depends();
3589 if (owner) {
3590 /*
3591 * Since delayed_put_task_struct() also drops the last
3592 * task reference we can safely take a new reference
3593 * while holding the rcu_read_lock().
3594 */
3595 get_task_struct(owner);
3596 }
3597 rcu_read_unlock();
3598
3599 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003600 /*
3601 * If we're here through perf_event_exit_task() we're already
3602 * holding ctx->mutex which would be an inversion wrt. the
3603 * normal lock order.
3604 *
3605 * However we can safely take this lock because its the child
3606 * ctx->mutex.
3607 */
3608 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3609
Peter Zijlstra88821352010-11-09 19:01:43 +01003610 /*
3611 * We have to re-check the event->owner field, if it is cleared
3612 * we raced with perf_event_exit_task(), acquiring the mutex
3613 * ensured they're done, and we can proceed with freeing the
3614 * event.
3615 */
3616 if (event->owner)
3617 list_del_init(&event->owner_entry);
3618 mutex_unlock(&owner->perf_event_mutex);
3619 put_task_struct(owner);
3620 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003621}
3622
3623/*
3624 * Called when the last reference to the file is gone.
3625 */
3626static void put_event(struct perf_event *event)
3627{
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003628 struct perf_event_context *ctx;
Jiri Olsaf8697762014-08-01 14:33:01 +02003629
3630 if (!atomic_long_dec_and_test(&event->refcount))
3631 return;
3632
3633 if (!is_kernel_event(event))
3634 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003635
Peter Zijlstra683ede42014-05-05 12:11:24 +02003636 /*
3637 * There are two ways this annotation is useful:
3638 *
3639 * 1) there is a lock recursion from perf_event_exit_task
3640 * see the comment there.
3641 *
3642 * 2) there is a lock-inversion with mmap_sem through
3643 * perf_event_read_group(), which takes faults while
3644 * holding ctx->mutex, however this is called after
3645 * the last filedesc died, so there is no possibility
3646 * to trigger the AB-BA case.
3647 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003648 ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3649 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003650 perf_remove_from_context(event, true);
3651 mutex_unlock(&ctx->mutex);
3652
3653 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003654}
3655
Peter Zijlstra683ede42014-05-05 12:11:24 +02003656int perf_event_release_kernel(struct perf_event *event)
3657{
3658 put_event(event);
3659 return 0;
3660}
3661EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3662
Al Viroa6fa9412012-08-20 14:59:25 +01003663static int perf_release(struct inode *inode, struct file *file)
3664{
3665 put_event(file->private_data);
3666 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003667}
3668
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003669/*
3670 * Remove all orphanes events from the context.
3671 */
3672static void orphans_remove_work(struct work_struct *work)
3673{
3674 struct perf_event_context *ctx;
3675 struct perf_event *event, *tmp;
3676
3677 ctx = container_of(work, struct perf_event_context,
3678 orphans_remove.work);
3679
3680 mutex_lock(&ctx->mutex);
3681 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3682 struct perf_event *parent_event = event->parent;
3683
3684 if (!is_orphaned_child(event))
3685 continue;
3686
3687 perf_remove_from_context(event, true);
3688
3689 mutex_lock(&parent_event->child_mutex);
3690 list_del_init(&event->child_list);
3691 mutex_unlock(&parent_event->child_mutex);
3692
3693 free_event(event);
3694 put_event(parent_event);
3695 }
3696
3697 raw_spin_lock_irq(&ctx->lock);
3698 ctx->orphans_remove_sched = false;
3699 raw_spin_unlock_irq(&ctx->lock);
3700 mutex_unlock(&ctx->mutex);
3701
3702 put_ctx(ctx);
3703}
3704
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003705u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706{
3707 struct perf_event *child;
3708 u64 total = 0;
3709
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003710 *enabled = 0;
3711 *running = 0;
3712
Peter Zijlstra6f105812009-11-20 22:19:56 +01003713 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003714 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003715 *enabled += event->total_time_enabled +
3716 atomic64_read(&event->child_total_time_enabled);
3717 *running += event->total_time_running +
3718 atomic64_read(&event->child_total_time_running);
3719
3720 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003722 *enabled += child->total_time_enabled;
3723 *running += child->total_time_running;
3724 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003725 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003726
3727 return total;
3728}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003729EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003730
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003731static int perf_event_read_group(struct perf_event *event,
3732 u64 read_format, char __user *buf)
3733{
3734 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003735 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003736 int n = 0, size = 0, ret;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003737 u64 count, enabled, running;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003738 u64 values[5];
Peter Zijlstraabf48682009-11-20 22:19:49 +01003739
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003740 lockdep_assert_held(&ctx->mutex);
3741
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003742 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003743
3744 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003745 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3746 values[n++] = enabled;
3747 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3748 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003749 values[n++] = count;
3750 if (read_format & PERF_FORMAT_ID)
3751 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003752
3753 size = n * sizeof(u64);
3754
3755 if (copy_to_user(buf, values, size))
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003756 return -EFAULT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757
Peter Zijlstra6f105812009-11-20 22:19:56 +01003758 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759
3760 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003761 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003763 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003764 if (read_format & PERF_FORMAT_ID)
3765 values[n++] = primary_event_id(sub);
3766
3767 size = n * sizeof(u64);
3768
Stephane Eranian184d3da2009-11-23 21:40:49 -08003769 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003770 return -EFAULT;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003771 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003772
3773 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003774 }
3775
Peter Zijlstraabf48682009-11-20 22:19:49 +01003776 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777}
3778
3779static int perf_event_read_one(struct perf_event *event,
3780 u64 read_format, char __user *buf)
3781{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003782 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003783 u64 values[4];
3784 int n = 0;
3785
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003786 values[n++] = perf_event_read_value(event, &enabled, &running);
3787 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3788 values[n++] = enabled;
3789 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3790 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003791 if (read_format & PERF_FORMAT_ID)
3792 values[n++] = primary_event_id(event);
3793
3794 if (copy_to_user(buf, values, n * sizeof(u64)))
3795 return -EFAULT;
3796
3797 return n * sizeof(u64);
3798}
3799
Jiri Olsadc633982014-09-12 13:18:26 +02003800static bool is_event_hup(struct perf_event *event)
3801{
3802 bool no_children;
3803
3804 if (event->state != PERF_EVENT_STATE_EXIT)
3805 return false;
3806
3807 mutex_lock(&event->child_mutex);
3808 no_children = list_empty(&event->child_list);
3809 mutex_unlock(&event->child_mutex);
3810 return no_children;
3811}
3812
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003813/*
3814 * Read the performance event - simple non blocking version for now
3815 */
3816static ssize_t
3817perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3818{
3819 u64 read_format = event->attr.read_format;
3820 int ret;
3821
3822 /*
3823 * Return end-of-file for a read on a event that is in
3824 * error state (i.e. because it was pinned but it couldn't be
3825 * scheduled on to the CPU at some point).
3826 */
3827 if (event->state == PERF_EVENT_STATE_ERROR)
3828 return 0;
3829
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003830 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003831 return -ENOSPC;
3832
3833 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003834 if (read_format & PERF_FORMAT_GROUP)
3835 ret = perf_event_read_group(event, read_format, buf);
3836 else
3837 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003838
3839 return ret;
3840}
3841
3842static ssize_t
3843perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3844{
3845 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003846 struct perf_event_context *ctx;
3847 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003849 ctx = perf_event_ctx_lock(event);
3850 ret = perf_read_hw(event, buf, count);
3851 perf_event_ctx_unlock(event, ctx);
3852
3853 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003854}
3855
3856static unsigned int perf_poll(struct file *file, poll_table *wait)
3857{
3858 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003859 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02003860 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003861
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02003862 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04003863
Jiri Olsadc633982014-09-12 13:18:26 +02003864 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04003865 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003866
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003867 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003868 * Pin the event->rb by taking event->mmap_mutex; otherwise
3869 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003870 */
3871 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003872 rb = event->rb;
3873 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003874 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003875 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003876 return events;
3877}
3878
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003879static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880{
3881 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003882 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003883 perf_event_update_userpage(event);
3884}
3885
3886/*
3887 * Holding the top-level event's child_mutex means that any
3888 * descendant process that has inherited this event will block
3889 * in sync_child_event if it goes to exit, thus satisfying the
3890 * task existence requirements of perf_event_enable/disable.
3891 */
3892static void perf_event_for_each_child(struct perf_event *event,
3893 void (*func)(struct perf_event *))
3894{
3895 struct perf_event *child;
3896
3897 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003898
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899 mutex_lock(&event->child_mutex);
3900 func(event);
3901 list_for_each_entry(child, &event->child_list, child_list)
3902 func(child);
3903 mutex_unlock(&event->child_mutex);
3904}
3905
3906static void perf_event_for_each(struct perf_event *event,
3907 void (*func)(struct perf_event *))
3908{
3909 struct perf_event_context *ctx = event->ctx;
3910 struct perf_event *sibling;
3911
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003912 lockdep_assert_held(&ctx->mutex);
3913
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003914 event = event->group_leader;
3915
3916 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003917 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003918 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003919}
3920
3921static int perf_event_period(struct perf_event *event, u64 __user *arg)
3922{
3923 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003924 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925 u64 value;
3926
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003927 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003928 return -EINVAL;
3929
John Blackwoodad0cf342010-09-28 18:03:11 -04003930 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003931 return -EFAULT;
3932
3933 if (!value)
3934 return -EINVAL;
3935
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003936 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003937 if (event->attr.freq) {
3938 if (value > sysctl_perf_event_sample_rate) {
3939 ret = -EINVAL;
3940 goto unlock;
3941 }
3942
3943 event->attr.sample_freq = value;
3944 } else {
3945 event->attr.sample_period = value;
3946 event->hw.sample_period = value;
3947 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003948
3949 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3950 if (active) {
3951 perf_pmu_disable(ctx->pmu);
3952 event->pmu->stop(event, PERF_EF_UPDATE);
3953 }
3954
3955 local64_set(&event->hw.period_left, 0);
3956
3957 if (active) {
3958 event->pmu->start(event, PERF_EF_RELOAD);
3959 perf_pmu_enable(ctx->pmu);
3960 }
3961
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003962unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003963 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964
3965 return ret;
3966}
3967
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003968static const struct file_operations perf_fops;
3969
Al Viro2903ff02012-08-28 12:52:22 -04003970static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003971{
Al Viro2903ff02012-08-28 12:52:22 -04003972 struct fd f = fdget(fd);
3973 if (!f.file)
3974 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003975
Al Viro2903ff02012-08-28 12:52:22 -04003976 if (f.file->f_op != &perf_fops) {
3977 fdput(f);
3978 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003979 }
Al Viro2903ff02012-08-28 12:52:22 -04003980 *p = f;
3981 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003982}
3983
3984static int perf_event_set_output(struct perf_event *event,
3985 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003986static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003987
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003988static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990 void (*func)(struct perf_event *);
3991 u32 flags = arg;
3992
3993 switch (cmd) {
3994 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003995 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003996 break;
3997 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003998 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003999 break;
4000 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004001 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002 break;
4003
4004 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004005 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004006
4007 case PERF_EVENT_IOC_PERIOD:
4008 return perf_event_period(event, (u64 __user *)arg);
4009
Jiri Olsacf4957f2012-10-24 13:37:58 +02004010 case PERF_EVENT_IOC_ID:
4011 {
4012 u64 id = primary_event_id(event);
4013
4014 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4015 return -EFAULT;
4016 return 0;
4017 }
4018
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004019 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004020 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004021 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004022 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04004023 struct perf_event *output_event;
4024 struct fd output;
4025 ret = perf_fget_light(arg, &output);
4026 if (ret)
4027 return ret;
4028 output_event = output.file->private_data;
4029 ret = perf_event_set_output(event, output_event);
4030 fdput(output);
4031 } else {
4032 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004033 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004034 return ret;
4035 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004036
Li Zefan6fb29152009-10-15 11:21:42 +08004037 case PERF_EVENT_IOC_SET_FILTER:
4038 return perf_event_set_filter(event, (void __user *)arg);
4039
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004040 default:
4041 return -ENOTTY;
4042 }
4043
4044 if (flags & PERF_IOC_FLAG_GROUP)
4045 perf_event_for_each(event, func);
4046 else
4047 perf_event_for_each_child(event, func);
4048
4049 return 0;
4050}
4051
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004052static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4053{
4054 struct perf_event *event = file->private_data;
4055 struct perf_event_context *ctx;
4056 long ret;
4057
4058 ctx = perf_event_ctx_lock(event);
4059 ret = _perf_ioctl(event, cmd, arg);
4060 perf_event_ctx_unlock(event, ctx);
4061
4062 return ret;
4063}
4064
Pawel Mollb3f20782014-06-13 16:03:32 +01004065#ifdef CONFIG_COMPAT
4066static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4067 unsigned long arg)
4068{
4069 switch (_IOC_NR(cmd)) {
4070 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4071 case _IOC_NR(PERF_EVENT_IOC_ID):
4072 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4073 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4074 cmd &= ~IOCSIZE_MASK;
4075 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4076 }
4077 break;
4078 }
4079 return perf_ioctl(file, cmd, arg);
4080}
4081#else
4082# define perf_compat_ioctl NULL
4083#endif
4084
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085int perf_event_task_enable(void)
4086{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004087 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 struct perf_event *event;
4089
4090 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004091 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4092 ctx = perf_event_ctx_lock(event);
4093 perf_event_for_each_child(event, _perf_event_enable);
4094 perf_event_ctx_unlock(event, ctx);
4095 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096 mutex_unlock(&current->perf_event_mutex);
4097
4098 return 0;
4099}
4100
4101int perf_event_task_disable(void)
4102{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004103 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004104 struct perf_event *event;
4105
4106 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004107 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4108 ctx = perf_event_ctx_lock(event);
4109 perf_event_for_each_child(event, _perf_event_disable);
4110 perf_event_ctx_unlock(event, ctx);
4111 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112 mutex_unlock(&current->perf_event_mutex);
4113
4114 return 0;
4115}
4116
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117static int perf_event_index(struct perf_event *event)
4118{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004119 if (event->hw.state & PERF_HES_STOPPED)
4120 return 0;
4121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004122 if (event->state != PERF_EVENT_STATE_ACTIVE)
4123 return 0;
4124
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004125 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004126}
4127
Eric B Munsonc4794292011-06-23 16:34:38 -04004128static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004129 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004130 u64 *enabled,
4131 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004132{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004133 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004134
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004135 *now = perf_clock();
4136 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004137 *enabled = ctx_time - event->tstamp_enabled;
4138 *running = ctx_time - event->tstamp_running;
4139}
4140
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004141static void perf_event_init_userpage(struct perf_event *event)
4142{
4143 struct perf_event_mmap_page *userpg;
4144 struct ring_buffer *rb;
4145
4146 rcu_read_lock();
4147 rb = rcu_dereference(event->rb);
4148 if (!rb)
4149 goto unlock;
4150
4151 userpg = rb->user_page;
4152
4153 /* Allow new userspace to detect that bit 0 is deprecated */
4154 userpg->cap_bit0_is_deprecated = 1;
4155 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
4156
4157unlock:
4158 rcu_read_unlock();
4159}
4160
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004161void __weak arch_perf_update_userpage(
4162 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004163{
4164}
4165
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166/*
4167 * Callers need to ensure there can be no nesting of this function, otherwise
4168 * the seqlock logic goes bad. We can not serialize this because the arch
4169 * code calls this from NMI context.
4170 */
4171void perf_event_update_userpage(struct perf_event *event)
4172{
4173 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004174 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004175 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004176
4177 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004178 rb = rcu_dereference(event->rb);
4179 if (!rb)
4180 goto unlock;
4181
Eric B Munson0d641202011-06-24 12:26:26 -04004182 /*
4183 * compute total_time_enabled, total_time_running
4184 * based on snapshot values taken when the event
4185 * was last scheduled in.
4186 *
4187 * we cannot simply called update_context_time()
4188 * because of locking issue as we can be called in
4189 * NMI context
4190 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004191 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192
Frederic Weisbecker76369132011-05-19 19:55:04 +02004193 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004194 /*
4195 * Disable preemption so as to not let the corresponding user-space
4196 * spin too long if we get preempted.
4197 */
4198 preempt_disable();
4199 ++userpg->lock;
4200 barrier();
4201 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004202 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004203 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004204 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205
Eric B Munson0d641202011-06-24 12:26:26 -04004206 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004207 atomic64_read(&event->child_total_time_enabled);
4208
Eric B Munson0d641202011-06-24 12:26:26 -04004209 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210 atomic64_read(&event->child_total_time_running);
4211
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004212 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004214 barrier();
4215 ++userpg->lock;
4216 preempt_enable();
4217unlock:
4218 rcu_read_unlock();
4219}
4220
Peter Zijlstra906010b2009-09-21 16:08:49 +02004221static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4222{
4223 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004224 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004225 int ret = VM_FAULT_SIGBUS;
4226
4227 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4228 if (vmf->pgoff == 0)
4229 ret = 0;
4230 return ret;
4231 }
4232
4233 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004234 rb = rcu_dereference(event->rb);
4235 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004236 goto unlock;
4237
4238 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4239 goto unlock;
4240
Frederic Weisbecker76369132011-05-19 19:55:04 +02004241 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004242 if (!vmf->page)
4243 goto unlock;
4244
4245 get_page(vmf->page);
4246 vmf->page->mapping = vma->vm_file->f_mapping;
4247 vmf->page->index = vmf->pgoff;
4248
4249 ret = 0;
4250unlock:
4251 rcu_read_unlock();
4252
4253 return ret;
4254}
4255
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004256static void ring_buffer_attach(struct perf_event *event,
4257 struct ring_buffer *rb)
4258{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004259 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004260 unsigned long flags;
4261
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004262 if (event->rb) {
4263 /*
4264 * Should be impossible, we set this when removing
4265 * event->rb_entry and wait/clear when adding event->rb_entry.
4266 */
4267 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004268
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004269 old_rb = event->rb;
4270 event->rcu_batches = get_state_synchronize_rcu();
4271 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004272
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004273 spin_lock_irqsave(&old_rb->event_lock, flags);
4274 list_del_rcu(&event->rb_entry);
4275 spin_unlock_irqrestore(&old_rb->event_lock, flags);
4276 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004277
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004278 if (event->rcu_pending && rb) {
4279 cond_synchronize_rcu(event->rcu_batches);
4280 event->rcu_pending = 0;
4281 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004282
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004283 if (rb) {
4284 spin_lock_irqsave(&rb->event_lock, flags);
4285 list_add_rcu(&event->rb_entry, &rb->event_list);
4286 spin_unlock_irqrestore(&rb->event_lock, flags);
4287 }
4288
4289 rcu_assign_pointer(event->rb, rb);
4290
4291 if (old_rb) {
4292 ring_buffer_put(old_rb);
4293 /*
4294 * Since we detached before setting the new rb, so that we
4295 * could attach the new rb, we could have missed a wakeup.
4296 * Provide it now.
4297 */
4298 wake_up_all(&event->waitq);
4299 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004300}
4301
4302static void ring_buffer_wakeup(struct perf_event *event)
4303{
4304 struct ring_buffer *rb;
4305
4306 rcu_read_lock();
4307 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004308 if (rb) {
4309 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4310 wake_up_all(&event->waitq);
4311 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004312 rcu_read_unlock();
4313}
4314
Frederic Weisbecker76369132011-05-19 19:55:04 +02004315static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004316{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004317 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004318
Frederic Weisbecker76369132011-05-19 19:55:04 +02004319 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
4320 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004321}
4322
Frederic Weisbecker76369132011-05-19 19:55:04 +02004323static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004324{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004325 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004327 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004328 rb = rcu_dereference(event->rb);
4329 if (rb) {
4330 if (!atomic_inc_not_zero(&rb->refcount))
4331 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004332 }
4333 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004334
Frederic Weisbecker76369132011-05-19 19:55:04 +02004335 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004336}
4337
Frederic Weisbecker76369132011-05-19 19:55:04 +02004338static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004339{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004340 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004341 return;
4342
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004343 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004344
Frederic Weisbecker76369132011-05-19 19:55:04 +02004345 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346}
4347
4348static void perf_mmap_open(struct vm_area_struct *vma)
4349{
4350 struct perf_event *event = vma->vm_file->private_data;
4351
4352 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004353 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004354
4355 if (event->pmu->event_mapped)
4356 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357}
4358
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004359/*
4360 * A buffer can be mmap()ed multiple times; either directly through the same
4361 * event, or through other events by use of perf_event_set_output().
4362 *
4363 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4364 * the buffer here, where we still have a VM context. This means we need
4365 * to detach all events redirecting to us.
4366 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004367static void perf_mmap_close(struct vm_area_struct *vma)
4368{
4369 struct perf_event *event = vma->vm_file->private_data;
4370
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004371 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004372 struct user_struct *mmap_user = rb->mmap_user;
4373 int mmap_locked = rb->mmap_locked;
4374 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004375
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004376 if (event->pmu->event_unmapped)
4377 event->pmu->event_unmapped(event);
4378
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004379 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004380
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004381 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004382 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004383
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004384 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004385 mutex_unlock(&event->mmap_mutex);
4386
4387 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004388 if (atomic_read(&rb->mmap_count))
4389 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004390
4391 /*
4392 * No other mmap()s, detach from all other events that might redirect
4393 * into the now unreachable buffer. Somewhat complicated by the
4394 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4395 */
4396again:
4397 rcu_read_lock();
4398 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4399 if (!atomic_long_inc_not_zero(&event->refcount)) {
4400 /*
4401 * This event is en-route to free_event() which will
4402 * detach it and remove it from the list.
4403 */
4404 continue;
4405 }
4406 rcu_read_unlock();
4407
4408 mutex_lock(&event->mmap_mutex);
4409 /*
4410 * Check we didn't race with perf_event_set_output() which can
4411 * swizzle the rb from under us while we were waiting to
4412 * acquire mmap_mutex.
4413 *
4414 * If we find a different rb; ignore this event, a next
4415 * iteration will no longer find it on the list. We have to
4416 * still restart the iteration to make sure we're not now
4417 * iterating the wrong list.
4418 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004419 if (event->rb == rb)
4420 ring_buffer_attach(event, NULL);
4421
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004422 mutex_unlock(&event->mmap_mutex);
4423 put_event(event);
4424
4425 /*
4426 * Restart the iteration; either we're on the wrong list or
4427 * destroyed its integrity by doing a deletion.
4428 */
4429 goto again;
4430 }
4431 rcu_read_unlock();
4432
4433 /*
4434 * It could be there's still a few 0-ref events on the list; they'll
4435 * get cleaned up by free_event() -- they'll also still have their
4436 * ref on the rb and will free it whenever they are done with it.
4437 *
4438 * Aside from that, this buffer is 'fully' detached and unmapped,
4439 * undo the VM accounting.
4440 */
4441
4442 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4443 vma->vm_mm->pinned_vm -= mmap_locked;
4444 free_uid(mmap_user);
4445
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004446out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004447 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448}
4449
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004450static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451 .open = perf_mmap_open,
4452 .close = perf_mmap_close,
4453 .fault = perf_mmap_fault,
4454 .page_mkwrite = perf_mmap_fault,
4455};
4456
4457static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4458{
4459 struct perf_event *event = file->private_data;
4460 unsigned long user_locked, user_lock_limit;
4461 struct user_struct *user = current_user();
4462 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004463 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464 unsigned long vma_size;
4465 unsigned long nr_pages;
4466 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004467 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004468
Peter Zijlstrac7920612010-05-18 10:33:24 +02004469 /*
4470 * Don't allow mmap() of inherited per-task counters. This would
4471 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004472 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004473 */
4474 if (event->cpu == -1 && event->attr.inherit)
4475 return -EINVAL;
4476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477 if (!(vma->vm_flags & VM_SHARED))
4478 return -EINVAL;
4479
4480 vma_size = vma->vm_end - vma->vm_start;
4481 nr_pages = (vma_size / PAGE_SIZE) - 1;
4482
4483 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004484 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004485 * can do bitmasks instead of modulo.
4486 */
4487 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4488 return -EINVAL;
4489
4490 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4491 return -EINVAL;
4492
4493 if (vma->vm_pgoff != 0)
4494 return -EINVAL;
4495
4496 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004497again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004498 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004499 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004500 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004502 goto unlock;
4503 }
4504
4505 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4506 /*
4507 * Raced against perf_mmap_close() through
4508 * perf_event_set_output(). Try again, hope for better
4509 * luck.
4510 */
4511 mutex_unlock(&event->mmap_mutex);
4512 goto again;
4513 }
4514
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004515 goto unlock;
4516 }
4517
4518 user_extra = nr_pages + 1;
4519 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4520
4521 /*
4522 * Increase the limit linearly with more CPUs:
4523 */
4524 user_lock_limit *= num_online_cpus();
4525
4526 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4527
4528 extra = 0;
4529 if (user_locked > user_lock_limit)
4530 extra = user_locked - user_lock_limit;
4531
Jiri Slaby78d7d402010-03-05 13:42:54 -08004532 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004534 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004535
4536 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4537 !capable(CAP_IPC_LOCK)) {
4538 ret = -EPERM;
4539 goto unlock;
4540 }
4541
Frederic Weisbecker76369132011-05-19 19:55:04 +02004542 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004543
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004544 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004545 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004546
Vince Weaver4ec83632011-06-01 15:15:36 -04004547 rb = rb_alloc(nr_pages,
4548 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4549 event->cpu, flags);
4550
Frederic Weisbecker76369132011-05-19 19:55:04 +02004551 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004552 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004553 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004554 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004555
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004556 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004557 rb->mmap_locked = extra;
4558 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004559
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004560 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004561 vma->vm_mm->pinned_vm += extra;
4562
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004563 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004564
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004565 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004566 perf_event_update_userpage(event);
4567
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004568unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004569 if (!ret)
4570 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004571 mutex_unlock(&event->mmap_mutex);
4572
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004573 /*
4574 * Since pinned accounting is per vm we cannot allow fork() to copy our
4575 * vma.
4576 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004577 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578 vma->vm_ops = &perf_mmap_vmops;
4579
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004580 if (event->pmu->event_mapped)
4581 event->pmu->event_mapped(event);
4582
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004583 return ret;
4584}
4585
4586static int perf_fasync(int fd, struct file *filp, int on)
4587{
Al Viro496ad9a2013-01-23 17:07:38 -05004588 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004589 struct perf_event *event = filp->private_data;
4590 int retval;
4591
4592 mutex_lock(&inode->i_mutex);
4593 retval = fasync_helper(fd, filp, on, &event->fasync);
4594 mutex_unlock(&inode->i_mutex);
4595
4596 if (retval < 0)
4597 return retval;
4598
4599 return 0;
4600}
4601
4602static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004603 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004604 .release = perf_release,
4605 .read = perf_read,
4606 .poll = perf_poll,
4607 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004608 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004609 .mmap = perf_mmap,
4610 .fasync = perf_fasync,
4611};
4612
4613/*
4614 * Perf event wakeup
4615 *
4616 * If there's data, ensure we set the poll() state and publish everything
4617 * to user-space before waking everybody up.
4618 */
4619
4620void perf_event_wakeup(struct perf_event *event)
4621{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004622 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004623
4624 if (event->pending_kill) {
4625 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4626 event->pending_kill = 0;
4627 }
4628}
4629
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004630static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004631{
4632 struct perf_event *event = container_of(entry,
4633 struct perf_event, pending);
4634
4635 if (event->pending_disable) {
4636 event->pending_disable = 0;
4637 __perf_event_disable(event);
4638 }
4639
4640 if (event->pending_wakeup) {
4641 event->pending_wakeup = 0;
4642 perf_event_wakeup(event);
4643 }
4644}
4645
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004646/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004647 * We assume there is only KVM supporting the callbacks.
4648 * Later on, we might change it to a list if there is
4649 * another virtualization implementation supporting the callbacks.
4650 */
4651struct perf_guest_info_callbacks *perf_guest_cbs;
4652
4653int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4654{
4655 perf_guest_cbs = cbs;
4656 return 0;
4657}
4658EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4659
4660int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4661{
4662 perf_guest_cbs = NULL;
4663 return 0;
4664}
4665EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4666
Jiri Olsa40189942012-08-07 15:20:37 +02004667static void
4668perf_output_sample_regs(struct perf_output_handle *handle,
4669 struct pt_regs *regs, u64 mask)
4670{
4671 int bit;
4672
4673 for_each_set_bit(bit, (const unsigned long *) &mask,
4674 sizeof(mask) * BITS_PER_BYTE) {
4675 u64 val;
4676
4677 val = perf_reg_value(regs, bit);
4678 perf_output_put(handle, val);
4679 }
4680}
4681
Stephane Eranian60e23642014-09-24 13:48:37 +02004682static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004683 struct pt_regs *regs,
4684 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004685{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004686 if (user_mode(regs)) {
4687 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004688 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004689 } else if (current->mm) {
4690 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004691 } else {
4692 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
4693 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02004694 }
4695}
4696
Stephane Eranian60e23642014-09-24 13:48:37 +02004697static void perf_sample_regs_intr(struct perf_regs *regs_intr,
4698 struct pt_regs *regs)
4699{
4700 regs_intr->regs = regs;
4701 regs_intr->abi = perf_reg_abi(current);
4702}
4703
4704
Jiri Olsac5ebced2012-08-07 15:20:40 +02004705/*
4706 * Get remaining task size from user stack pointer.
4707 *
4708 * It'd be better to take stack vma map and limit this more
4709 * precisly, but there's no way to get it safely under interrupt,
4710 * so using TASK_SIZE as limit.
4711 */
4712static u64 perf_ustack_task_size(struct pt_regs *regs)
4713{
4714 unsigned long addr = perf_user_stack_pointer(regs);
4715
4716 if (!addr || addr >= TASK_SIZE)
4717 return 0;
4718
4719 return TASK_SIZE - addr;
4720}
4721
4722static u16
4723perf_sample_ustack_size(u16 stack_size, u16 header_size,
4724 struct pt_regs *regs)
4725{
4726 u64 task_size;
4727
4728 /* No regs, no stack pointer, no dump. */
4729 if (!regs)
4730 return 0;
4731
4732 /*
4733 * Check if we fit in with the requested stack size into the:
4734 * - TASK_SIZE
4735 * If we don't, we limit the size to the TASK_SIZE.
4736 *
4737 * - remaining sample size
4738 * If we don't, we customize the stack size to
4739 * fit in to the remaining sample size.
4740 */
4741
4742 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4743 stack_size = min(stack_size, (u16) task_size);
4744
4745 /* Current header size plus static size and dynamic size. */
4746 header_size += 2 * sizeof(u64);
4747
4748 /* Do we fit in with the current stack dump size? */
4749 if ((u16) (header_size + stack_size) < header_size) {
4750 /*
4751 * If we overflow the maximum size for the sample,
4752 * we customize the stack dump size to fit in.
4753 */
4754 stack_size = USHRT_MAX - header_size - sizeof(u64);
4755 stack_size = round_up(stack_size, sizeof(u64));
4756 }
4757
4758 return stack_size;
4759}
4760
4761static void
4762perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4763 struct pt_regs *regs)
4764{
4765 /* Case of a kernel thread, nothing to dump */
4766 if (!regs) {
4767 u64 size = 0;
4768 perf_output_put(handle, size);
4769 } else {
4770 unsigned long sp;
4771 unsigned int rem;
4772 u64 dyn_size;
4773
4774 /*
4775 * We dump:
4776 * static size
4777 * - the size requested by user or the best one we can fit
4778 * in to the sample max size
4779 * data
4780 * - user stack dump data
4781 * dynamic size
4782 * - the actual dumped size
4783 */
4784
4785 /* Static size. */
4786 perf_output_put(handle, dump_size);
4787
4788 /* Data. */
4789 sp = perf_user_stack_pointer(regs);
4790 rem = __output_copy_user(handle, (void *) sp, dump_size);
4791 dyn_size = dump_size - rem;
4792
4793 perf_output_skip(handle, rem);
4794
4795 /* Dynamic size. */
4796 perf_output_put(handle, dyn_size);
4797 }
4798}
4799
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004800static void __perf_event_header__init_id(struct perf_event_header *header,
4801 struct perf_sample_data *data,
4802 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004803{
4804 u64 sample_type = event->attr.sample_type;
4805
4806 data->type = sample_type;
4807 header->size += event->id_header_size;
4808
4809 if (sample_type & PERF_SAMPLE_TID) {
4810 /* namespace issues */
4811 data->tid_entry.pid = perf_event_pid(event, current);
4812 data->tid_entry.tid = perf_event_tid(event, current);
4813 }
4814
4815 if (sample_type & PERF_SAMPLE_TIME)
4816 data->time = perf_clock();
4817
Adrian Hunterff3d5272013-08-27 11:23:07 +03004818 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004819 data->id = primary_event_id(event);
4820
4821 if (sample_type & PERF_SAMPLE_STREAM_ID)
4822 data->stream_id = event->id;
4823
4824 if (sample_type & PERF_SAMPLE_CPU) {
4825 data->cpu_entry.cpu = raw_smp_processor_id();
4826 data->cpu_entry.reserved = 0;
4827 }
4828}
4829
Frederic Weisbecker76369132011-05-19 19:55:04 +02004830void perf_event_header__init_id(struct perf_event_header *header,
4831 struct perf_sample_data *data,
4832 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004833{
4834 if (event->attr.sample_id_all)
4835 __perf_event_header__init_id(header, data, event);
4836}
4837
4838static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4839 struct perf_sample_data *data)
4840{
4841 u64 sample_type = data->type;
4842
4843 if (sample_type & PERF_SAMPLE_TID)
4844 perf_output_put(handle, data->tid_entry);
4845
4846 if (sample_type & PERF_SAMPLE_TIME)
4847 perf_output_put(handle, data->time);
4848
4849 if (sample_type & PERF_SAMPLE_ID)
4850 perf_output_put(handle, data->id);
4851
4852 if (sample_type & PERF_SAMPLE_STREAM_ID)
4853 perf_output_put(handle, data->stream_id);
4854
4855 if (sample_type & PERF_SAMPLE_CPU)
4856 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004857
4858 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4859 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004860}
4861
Frederic Weisbecker76369132011-05-19 19:55:04 +02004862void perf_event__output_id_sample(struct perf_event *event,
4863 struct perf_output_handle *handle,
4864 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004865{
4866 if (event->attr.sample_id_all)
4867 __perf_event__output_id_sample(handle, sample);
4868}
4869
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004871 struct perf_event *event,
4872 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004873{
4874 u64 read_format = event->attr.read_format;
4875 u64 values[4];
4876 int n = 0;
4877
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004878 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004879 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004880 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004881 atomic64_read(&event->child_total_time_enabled);
4882 }
4883 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004884 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004885 atomic64_read(&event->child_total_time_running);
4886 }
4887 if (read_format & PERF_FORMAT_ID)
4888 values[n++] = primary_event_id(event);
4889
Frederic Weisbecker76369132011-05-19 19:55:04 +02004890 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004891}
4892
4893/*
4894 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4895 */
4896static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004897 struct perf_event *event,
4898 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004899{
4900 struct perf_event *leader = event->group_leader, *sub;
4901 u64 read_format = event->attr.read_format;
4902 u64 values[5];
4903 int n = 0;
4904
4905 values[n++] = 1 + leader->nr_siblings;
4906
4907 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004908 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004909
4910 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004911 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004912
4913 if (leader != event)
4914 leader->pmu->read(leader);
4915
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004916 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004917 if (read_format & PERF_FORMAT_ID)
4918 values[n++] = primary_event_id(leader);
4919
Frederic Weisbecker76369132011-05-19 19:55:04 +02004920 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004921
4922 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4923 n = 0;
4924
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004925 if ((sub != event) &&
4926 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004927 sub->pmu->read(sub);
4928
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004929 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004930 if (read_format & PERF_FORMAT_ID)
4931 values[n++] = primary_event_id(sub);
4932
Frederic Weisbecker76369132011-05-19 19:55:04 +02004933 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004934 }
4935}
4936
Stephane Eranianeed01522010-10-26 16:08:01 +02004937#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4938 PERF_FORMAT_TOTAL_TIME_RUNNING)
4939
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004940static void perf_output_read(struct perf_output_handle *handle,
4941 struct perf_event *event)
4942{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004943 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004944 u64 read_format = event->attr.read_format;
4945
4946 /*
4947 * compute total_time_enabled, total_time_running
4948 * based on snapshot values taken when the event
4949 * was last scheduled in.
4950 *
4951 * we cannot simply called update_context_time()
4952 * because of locking issue as we are called in
4953 * NMI context
4954 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004955 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004956 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004957
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004958 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004959 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004960 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004961 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004962}
4963
4964void perf_output_sample(struct perf_output_handle *handle,
4965 struct perf_event_header *header,
4966 struct perf_sample_data *data,
4967 struct perf_event *event)
4968{
4969 u64 sample_type = data->type;
4970
4971 perf_output_put(handle, *header);
4972
Adrian Hunterff3d5272013-08-27 11:23:07 +03004973 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4974 perf_output_put(handle, data->id);
4975
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004976 if (sample_type & PERF_SAMPLE_IP)
4977 perf_output_put(handle, data->ip);
4978
4979 if (sample_type & PERF_SAMPLE_TID)
4980 perf_output_put(handle, data->tid_entry);
4981
4982 if (sample_type & PERF_SAMPLE_TIME)
4983 perf_output_put(handle, data->time);
4984
4985 if (sample_type & PERF_SAMPLE_ADDR)
4986 perf_output_put(handle, data->addr);
4987
4988 if (sample_type & PERF_SAMPLE_ID)
4989 perf_output_put(handle, data->id);
4990
4991 if (sample_type & PERF_SAMPLE_STREAM_ID)
4992 perf_output_put(handle, data->stream_id);
4993
4994 if (sample_type & PERF_SAMPLE_CPU)
4995 perf_output_put(handle, data->cpu_entry);
4996
4997 if (sample_type & PERF_SAMPLE_PERIOD)
4998 perf_output_put(handle, data->period);
4999
5000 if (sample_type & PERF_SAMPLE_READ)
5001 perf_output_read(handle, event);
5002
5003 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5004 if (data->callchain) {
5005 int size = 1;
5006
5007 if (data->callchain)
5008 size += data->callchain->nr;
5009
5010 size *= sizeof(u64);
5011
Frederic Weisbecker76369132011-05-19 19:55:04 +02005012 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005013 } else {
5014 u64 nr = 0;
5015 perf_output_put(handle, nr);
5016 }
5017 }
5018
5019 if (sample_type & PERF_SAMPLE_RAW) {
5020 if (data->raw) {
5021 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005022 __output_copy(handle, data->raw->data,
5023 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005024 } else {
5025 struct {
5026 u32 size;
5027 u32 data;
5028 } raw = {
5029 .size = sizeof(u32),
5030 .data = 0,
5031 };
5032 perf_output_put(handle, raw);
5033 }
5034 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005035
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005036 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5037 if (data->br_stack) {
5038 size_t size;
5039
5040 size = data->br_stack->nr
5041 * sizeof(struct perf_branch_entry);
5042
5043 perf_output_put(handle, data->br_stack->nr);
5044 perf_output_copy(handle, data->br_stack->entries, size);
5045 } else {
5046 /*
5047 * we always store at least the value of nr
5048 */
5049 u64 nr = 0;
5050 perf_output_put(handle, nr);
5051 }
5052 }
Jiri Olsa40189942012-08-07 15:20:37 +02005053
5054 if (sample_type & PERF_SAMPLE_REGS_USER) {
5055 u64 abi = data->regs_user.abi;
5056
5057 /*
5058 * If there are no regs to dump, notice it through
5059 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5060 */
5061 perf_output_put(handle, abi);
5062
5063 if (abi) {
5064 u64 mask = event->attr.sample_regs_user;
5065 perf_output_sample_regs(handle,
5066 data->regs_user.regs,
5067 mask);
5068 }
5069 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005070
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005071 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005072 perf_output_sample_ustack(handle,
5073 data->stack_user_size,
5074 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005075 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005076
5077 if (sample_type & PERF_SAMPLE_WEIGHT)
5078 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005079
5080 if (sample_type & PERF_SAMPLE_DATA_SRC)
5081 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005082
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005083 if (sample_type & PERF_SAMPLE_TRANSACTION)
5084 perf_output_put(handle, data->txn);
5085
Stephane Eranian60e23642014-09-24 13:48:37 +02005086 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5087 u64 abi = data->regs_intr.abi;
5088 /*
5089 * If there are no regs to dump, notice it through
5090 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5091 */
5092 perf_output_put(handle, abi);
5093
5094 if (abi) {
5095 u64 mask = event->attr.sample_regs_intr;
5096
5097 perf_output_sample_regs(handle,
5098 data->regs_intr.regs,
5099 mask);
5100 }
5101 }
5102
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005103 if (!event->attr.watermark) {
5104 int wakeup_events = event->attr.wakeup_events;
5105
5106 if (wakeup_events) {
5107 struct ring_buffer *rb = handle->rb;
5108 int events = local_inc_return(&rb->events);
5109
5110 if (events >= wakeup_events) {
5111 local_sub(wakeup_events, &rb->events);
5112 local_inc(&rb->wakeup);
5113 }
5114 }
5115 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005116}
5117
5118void perf_prepare_sample(struct perf_event_header *header,
5119 struct perf_sample_data *data,
5120 struct perf_event *event,
5121 struct pt_regs *regs)
5122{
5123 u64 sample_type = event->attr.sample_type;
5124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005125 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005126 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005127
5128 header->misc = 0;
5129 header->misc |= perf_misc_flags(regs);
5130
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005131 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005132
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005133 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134 data->ip = perf_instruction_pointer(regs);
5135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005136 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5137 int size = 1;
5138
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005139 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005140
5141 if (data->callchain)
5142 size += data->callchain->nr;
5143
5144 header->size += size * sizeof(u64);
5145 }
5146
5147 if (sample_type & PERF_SAMPLE_RAW) {
5148 int size = sizeof(u32);
5149
5150 if (data->raw)
5151 size += data->raw->size;
5152 else
5153 size += sizeof(u32);
5154
5155 WARN_ON_ONCE(size & (sizeof(u64)-1));
5156 header->size += size;
5157 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005158
5159 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5160 int size = sizeof(u64); /* nr */
5161 if (data->br_stack) {
5162 size += data->br_stack->nr
5163 * sizeof(struct perf_branch_entry);
5164 }
5165 header->size += size;
5166 }
Jiri Olsa40189942012-08-07 15:20:37 +02005167
Peter Zijlstra25657112014-09-24 13:48:42 +02005168 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005169 perf_sample_regs_user(&data->regs_user, regs,
5170 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005171
Jiri Olsa40189942012-08-07 15:20:37 +02005172 if (sample_type & PERF_SAMPLE_REGS_USER) {
5173 /* regs dump ABI info */
5174 int size = sizeof(u64);
5175
Jiri Olsa40189942012-08-07 15:20:37 +02005176 if (data->regs_user.regs) {
5177 u64 mask = event->attr.sample_regs_user;
5178 size += hweight64(mask) * sizeof(u64);
5179 }
5180
5181 header->size += size;
5182 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005183
5184 if (sample_type & PERF_SAMPLE_STACK_USER) {
5185 /*
5186 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5187 * processed as the last one or have additional check added
5188 * in case new sample type is added, because we could eat
5189 * up the rest of the sample size.
5190 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005191 u16 stack_size = event->attr.sample_stack_user;
5192 u16 size = sizeof(u64);
5193
Jiri Olsac5ebced2012-08-07 15:20:40 +02005194 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005195 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005196
5197 /*
5198 * If there is something to dump, add space for the dump
5199 * itself and for the field that tells the dynamic size,
5200 * which is how many have been actually dumped.
5201 */
5202 if (stack_size)
5203 size += sizeof(u64) + stack_size;
5204
5205 data->stack_user_size = stack_size;
5206 header->size += size;
5207 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005208
5209 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5210 /* regs dump ABI info */
5211 int size = sizeof(u64);
5212
5213 perf_sample_regs_intr(&data->regs_intr, regs);
5214
5215 if (data->regs_intr.regs) {
5216 u64 mask = event->attr.sample_regs_intr;
5217
5218 size += hweight64(mask) * sizeof(u64);
5219 }
5220
5221 header->size += size;
5222 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005223}
5224
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005225static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005226 struct perf_sample_data *data,
5227 struct pt_regs *regs)
5228{
5229 struct perf_output_handle handle;
5230 struct perf_event_header header;
5231
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005232 /* protect the callchain buffers */
5233 rcu_read_lock();
5234
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235 perf_prepare_sample(&header, data, event, regs);
5236
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005237 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005238 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239
5240 perf_output_sample(&handle, &header, data, event);
5241
5242 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005243
5244exit:
5245 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246}
5247
5248/*
5249 * read event_id
5250 */
5251
5252struct perf_read_event {
5253 struct perf_event_header header;
5254
5255 u32 pid;
5256 u32 tid;
5257};
5258
5259static void
5260perf_event_read_event(struct perf_event *event,
5261 struct task_struct *task)
5262{
5263 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005264 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265 struct perf_read_event read_event = {
5266 .header = {
5267 .type = PERF_RECORD_READ,
5268 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005269 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005270 },
5271 .pid = perf_event_pid(event, task),
5272 .tid = perf_event_tid(event, task),
5273 };
5274 int ret;
5275
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005276 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005277 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 if (ret)
5279 return;
5280
5281 perf_output_put(&handle, read_event);
5282 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005283 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284
5285 perf_output_end(&handle);
5286}
5287
Jiri Olsa52d857a2013-05-06 18:27:18 +02005288typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5289
5290static void
5291perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005292 perf_event_aux_output_cb output,
5293 void *data)
5294{
5295 struct perf_event *event;
5296
5297 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5298 if (event->state < PERF_EVENT_STATE_INACTIVE)
5299 continue;
5300 if (!event_filter_match(event))
5301 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005302 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005303 }
5304}
5305
5306static void
Jiri Olsa67516842013-07-09 18:56:31 +02005307perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005308 struct perf_event_context *task_ctx)
5309{
5310 struct perf_cpu_context *cpuctx;
5311 struct perf_event_context *ctx;
5312 struct pmu *pmu;
5313 int ctxn;
5314
5315 rcu_read_lock();
5316 list_for_each_entry_rcu(pmu, &pmus, entry) {
5317 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5318 if (cpuctx->unique_pmu != pmu)
5319 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005320 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005321 if (task_ctx)
5322 goto next;
5323 ctxn = pmu->task_ctx_nr;
5324 if (ctxn < 0)
5325 goto next;
5326 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5327 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005328 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005329next:
5330 put_cpu_ptr(pmu->pmu_cpu_context);
5331 }
5332
5333 if (task_ctx) {
5334 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02005335 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005336 preempt_enable();
5337 }
5338 rcu_read_unlock();
5339}
5340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341/*
5342 * task tracking -- fork/exit
5343 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005344 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345 */
5346
5347struct perf_task_event {
5348 struct task_struct *task;
5349 struct perf_event_context *task_ctx;
5350
5351 struct {
5352 struct perf_event_header header;
5353
5354 u32 pid;
5355 u32 ppid;
5356 u32 tid;
5357 u32 ptid;
5358 u64 time;
5359 } event_id;
5360};
5361
Jiri Olsa67516842013-07-09 18:56:31 +02005362static int perf_event_task_match(struct perf_event *event)
5363{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005364 return event->attr.comm || event->attr.mmap ||
5365 event->attr.mmap2 || event->attr.mmap_data ||
5366 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005367}
5368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005370 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005371{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005372 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005373 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005374 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005375 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005376 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005377
Jiri Olsa67516842013-07-09 18:56:31 +02005378 if (!perf_event_task_match(event))
5379 return;
5380
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005381 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005382
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005383 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005384 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005385 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005386 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005387
5388 task_event->event_id.pid = perf_event_pid(event, task);
5389 task_event->event_id.ppid = perf_event_pid(event, current);
5390
5391 task_event->event_id.tid = perf_event_tid(event, task);
5392 task_event->event_id.ptid = perf_event_tid(event, current);
5393
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005394 perf_output_put(&handle, task_event->event_id);
5395
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005396 perf_event__output_id_sample(event, &handle, &sample);
5397
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005398 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005399out:
5400 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005401}
5402
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005403static void perf_event_task(struct task_struct *task,
5404 struct perf_event_context *task_ctx,
5405 int new)
5406{
5407 struct perf_task_event task_event;
5408
5409 if (!atomic_read(&nr_comm_events) &&
5410 !atomic_read(&nr_mmap_events) &&
5411 !atomic_read(&nr_task_events))
5412 return;
5413
5414 task_event = (struct perf_task_event){
5415 .task = task,
5416 .task_ctx = task_ctx,
5417 .event_id = {
5418 .header = {
5419 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5420 .misc = 0,
5421 .size = sizeof(task_event.event_id),
5422 },
5423 /* .pid */
5424 /* .ppid */
5425 /* .tid */
5426 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005427 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428 },
5429 };
5430
Jiri Olsa67516842013-07-09 18:56:31 +02005431 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005432 &task_event,
5433 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005434}
5435
5436void perf_event_fork(struct task_struct *task)
5437{
5438 perf_event_task(task, NULL, 1);
5439}
5440
5441/*
5442 * comm tracking
5443 */
5444
5445struct perf_comm_event {
5446 struct task_struct *task;
5447 char *comm;
5448 int comm_size;
5449
5450 struct {
5451 struct perf_event_header header;
5452
5453 u32 pid;
5454 u32 tid;
5455 } event_id;
5456};
5457
Jiri Olsa67516842013-07-09 18:56:31 +02005458static int perf_event_comm_match(struct perf_event *event)
5459{
5460 return event->attr.comm;
5461}
5462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005463static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005464 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005465{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005466 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005467 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005468 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005470 int ret;
5471
Jiri Olsa67516842013-07-09 18:56:31 +02005472 if (!perf_event_comm_match(event))
5473 return;
5474
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005475 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5476 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005477 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005478
5479 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005480 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481
5482 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5483 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5484
5485 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005486 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005487 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005488
5489 perf_event__output_id_sample(event, &handle, &sample);
5490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005491 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005492out:
5493 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005494}
5495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005496static void perf_event_comm_event(struct perf_comm_event *comm_event)
5497{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005498 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005499 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500
5501 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005502 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005503 size = ALIGN(strlen(comm)+1, sizeof(u64));
5504
5505 comm_event->comm = comm;
5506 comm_event->comm_size = size;
5507
5508 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005509
Jiri Olsa67516842013-07-09 18:56:31 +02005510 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005511 comm_event,
5512 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005513}
5514
Adrian Hunter82b89772014-05-28 11:45:04 +03005515void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005516{
5517 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005518
5519 if (!atomic_read(&nr_comm_events))
5520 return;
5521
5522 comm_event = (struct perf_comm_event){
5523 .task = task,
5524 /* .comm */
5525 /* .comm_size */
5526 .event_id = {
5527 .header = {
5528 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005529 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005530 /* .size */
5531 },
5532 /* .pid */
5533 /* .tid */
5534 },
5535 };
5536
5537 perf_event_comm_event(&comm_event);
5538}
5539
5540/*
5541 * mmap tracking
5542 */
5543
5544struct perf_mmap_event {
5545 struct vm_area_struct *vma;
5546
5547 const char *file_name;
5548 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005549 int maj, min;
5550 u64 ino;
5551 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005552 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005553
5554 struct {
5555 struct perf_event_header header;
5556
5557 u32 pid;
5558 u32 tid;
5559 u64 start;
5560 u64 len;
5561 u64 pgoff;
5562 } event_id;
5563};
5564
Jiri Olsa67516842013-07-09 18:56:31 +02005565static int perf_event_mmap_match(struct perf_event *event,
5566 void *data)
5567{
5568 struct perf_mmap_event *mmap_event = data;
5569 struct vm_area_struct *vma = mmap_event->vma;
5570 int executable = vma->vm_flags & VM_EXEC;
5571
5572 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005573 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005574}
5575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005577 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005578{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005579 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005580 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005581 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005582 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005583 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584
Jiri Olsa67516842013-07-09 18:56:31 +02005585 if (!perf_event_mmap_match(event, data))
5586 return;
5587
Stephane Eranian13d7a242013-08-21 12:10:24 +02005588 if (event->attr.mmap2) {
5589 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5590 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5591 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5592 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005593 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005594 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5595 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005596 }
5597
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005598 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5599 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005600 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005602 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005603
5604 mmap_event->event_id.pid = perf_event_pid(event, current);
5605 mmap_event->event_id.tid = perf_event_tid(event, current);
5606
5607 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005608
5609 if (event->attr.mmap2) {
5610 perf_output_put(&handle, mmap_event->maj);
5611 perf_output_put(&handle, mmap_event->min);
5612 perf_output_put(&handle, mmap_event->ino);
5613 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005614 perf_output_put(&handle, mmap_event->prot);
5615 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005616 }
5617
Frederic Weisbecker76369132011-05-19 19:55:04 +02005618 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005619 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005620
5621 perf_event__output_id_sample(event, &handle, &sample);
5622
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005623 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005624out:
5625 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005626}
5627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005628static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5629{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005630 struct vm_area_struct *vma = mmap_event->vma;
5631 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005632 int maj = 0, min = 0;
5633 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005634 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005635 unsigned int size;
5636 char tmp[16];
5637 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005638 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005639
5640 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005641 struct inode *inode;
5642 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005643
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005644 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005646 name = "//enomem";
5647 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005648 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005650 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005651 * need to add enough zero bytes after the string to handle
5652 * the 64bit alignment we do later.
5653 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005654 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005655 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005656 name = "//toolong";
5657 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005658 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005659 inode = file_inode(vma->vm_file);
5660 dev = inode->i_sb->s_dev;
5661 ino = inode->i_ino;
5662 gen = inode->i_generation;
5663 maj = MAJOR(dev);
5664 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005665
5666 if (vma->vm_flags & VM_READ)
5667 prot |= PROT_READ;
5668 if (vma->vm_flags & VM_WRITE)
5669 prot |= PROT_WRITE;
5670 if (vma->vm_flags & VM_EXEC)
5671 prot |= PROT_EXEC;
5672
5673 if (vma->vm_flags & VM_MAYSHARE)
5674 flags = MAP_SHARED;
5675 else
5676 flags = MAP_PRIVATE;
5677
5678 if (vma->vm_flags & VM_DENYWRITE)
5679 flags |= MAP_DENYWRITE;
5680 if (vma->vm_flags & VM_MAYEXEC)
5681 flags |= MAP_EXECUTABLE;
5682 if (vma->vm_flags & VM_LOCKED)
5683 flags |= MAP_LOCKED;
5684 if (vma->vm_flags & VM_HUGETLB)
5685 flags |= MAP_HUGETLB;
5686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005687 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005688 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005689 if (vma->vm_ops && vma->vm_ops->name) {
5690 name = (char *) vma->vm_ops->name(vma);
5691 if (name)
5692 goto cpy_name;
5693 }
5694
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005695 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005696 if (name)
5697 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005698
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005699 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005701 name = "[heap]";
5702 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005703 }
5704 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005705 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005706 name = "[stack]";
5707 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005708 }
5709
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005710 name = "//anon";
5711 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005712 }
5713
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005714cpy_name:
5715 strlcpy(tmp, name, sizeof(tmp));
5716 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005717got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005718 /*
5719 * Since our buffer works in 8 byte units we need to align our string
5720 * size to a multiple of 8. However, we must guarantee the tail end is
5721 * zero'd out to avoid leaking random bits to userspace.
5722 */
5723 size = strlen(name)+1;
5724 while (!IS_ALIGNED(size, sizeof(u64)))
5725 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726
5727 mmap_event->file_name = name;
5728 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005729 mmap_event->maj = maj;
5730 mmap_event->min = min;
5731 mmap_event->ino = ino;
5732 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005733 mmap_event->prot = prot;
5734 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005735
Stephane Eranian2fe85422013-01-24 16:10:39 +01005736 if (!(vma->vm_flags & VM_EXEC))
5737 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005739 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5740
Jiri Olsa67516842013-07-09 18:56:31 +02005741 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005742 mmap_event,
5743 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005744
5745 kfree(buf);
5746}
5747
Eric B Munson3af9e852010-05-18 15:30:49 +01005748void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005749{
5750 struct perf_mmap_event mmap_event;
5751
5752 if (!atomic_read(&nr_mmap_events))
5753 return;
5754
5755 mmap_event = (struct perf_mmap_event){
5756 .vma = vma,
5757 /* .file_name */
5758 /* .file_size */
5759 .event_id = {
5760 .header = {
5761 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005762 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005763 /* .size */
5764 },
5765 /* .pid */
5766 /* .tid */
5767 .start = vma->vm_start,
5768 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005769 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005771 /* .maj (attr_mmap2 only) */
5772 /* .min (attr_mmap2 only) */
5773 /* .ino (attr_mmap2 only) */
5774 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005775 /* .prot (attr_mmap2 only) */
5776 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005777 };
5778
5779 perf_event_mmap_event(&mmap_event);
5780}
5781
5782/*
5783 * IRQ throttle logging
5784 */
5785
5786static void perf_log_throttle(struct perf_event *event, int enable)
5787{
5788 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005789 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005790 int ret;
5791
5792 struct {
5793 struct perf_event_header header;
5794 u64 time;
5795 u64 id;
5796 u64 stream_id;
5797 } throttle_event = {
5798 .header = {
5799 .type = PERF_RECORD_THROTTLE,
5800 .misc = 0,
5801 .size = sizeof(throttle_event),
5802 },
5803 .time = perf_clock(),
5804 .id = primary_event_id(event),
5805 .stream_id = event->id,
5806 };
5807
5808 if (enable)
5809 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5810
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005811 perf_event_header__init_id(&throttle_event.header, &sample, event);
5812
5813 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005814 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005815 if (ret)
5816 return;
5817
5818 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005819 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820 perf_output_end(&handle);
5821}
5822
5823/*
5824 * Generic event overflow handling, sampling.
5825 */
5826
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005827static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005828 int throttle, struct perf_sample_data *data,
5829 struct pt_regs *regs)
5830{
5831 int events = atomic_read(&event->event_limit);
5832 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005833 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005834 int ret = 0;
5835
Peter Zijlstra96398822010-11-24 18:55:29 +01005836 /*
5837 * Non-sampling counters might still use the PMI to fold short
5838 * hardware counters, ignore those.
5839 */
5840 if (unlikely(!is_sampling_event(event)))
5841 return 0;
5842
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005843 seq = __this_cpu_read(perf_throttled_seq);
5844 if (seq != hwc->interrupts_seq) {
5845 hwc->interrupts_seq = seq;
5846 hwc->interrupts = 1;
5847 } else {
5848 hwc->interrupts++;
5849 if (unlikely(throttle
5850 && hwc->interrupts >= max_samples_per_tick)) {
5851 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005852 hwc->interrupts = MAX_INTERRUPTS;
5853 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005854 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005855 ret = 1;
5856 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005857 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005858
5859 if (event->attr.freq) {
5860 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005861 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005862
Peter Zijlstraabd50712010-01-26 18:50:16 +01005863 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005864
Peter Zijlstraabd50712010-01-26 18:50:16 +01005865 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005866 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005867 }
5868
5869 /*
5870 * XXX event_limit might not quite work as expected on inherited
5871 * events
5872 */
5873
5874 event->pending_kill = POLL_IN;
5875 if (events && atomic_dec_and_test(&event->event_limit)) {
5876 ret = 1;
5877 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005878 event->pending_disable = 1;
5879 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005880 }
5881
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005882 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005883 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005884 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005885 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005886
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005887 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005888 event->pending_wakeup = 1;
5889 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005890 }
5891
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005892 return ret;
5893}
5894
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005895int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896 struct perf_sample_data *data,
5897 struct pt_regs *regs)
5898{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005899 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900}
5901
5902/*
5903 * Generic software event infrastructure
5904 */
5905
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005906struct swevent_htable {
5907 struct swevent_hlist *swevent_hlist;
5908 struct mutex hlist_mutex;
5909 int hlist_refcount;
5910
5911 /* Recursion avoidance in each contexts */
5912 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005913
5914 /* Keeps track of cpu being initialized/exited */
5915 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005916};
5917
5918static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005920/*
5921 * We directly increment event->count and keep a second value in
5922 * event->hw.period_left to count intervals. This period event
5923 * is kept in the range [-sample_period, 0] so that we can use the
5924 * sign as trigger.
5925 */
5926
Jiri Olsaab573842013-05-01 17:25:44 +02005927u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928{
5929 struct hw_perf_event *hwc = &event->hw;
5930 u64 period = hwc->last_period;
5931 u64 nr, offset;
5932 s64 old, val;
5933
5934 hwc->last_period = hwc->sample_period;
5935
5936again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005937 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005938 if (val < 0)
5939 return 0;
5940
5941 nr = div64_u64(period + val, period);
5942 offset = nr * period;
5943 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005944 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005945 goto again;
5946
5947 return nr;
5948}
5949
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005950static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005951 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005952 struct pt_regs *regs)
5953{
5954 struct hw_perf_event *hwc = &event->hw;
5955 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005956
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005957 if (!overflow)
5958 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959
5960 if (hwc->interrupts == MAX_INTERRUPTS)
5961 return;
5962
5963 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005964 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005965 data, regs)) {
5966 /*
5967 * We inhibit the overflow from happening when
5968 * hwc->interrupts == MAX_INTERRUPTS.
5969 */
5970 break;
5971 }
5972 throttle = 1;
5973 }
5974}
5975
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005976static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005977 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005978 struct pt_regs *regs)
5979{
5980 struct hw_perf_event *hwc = &event->hw;
5981
Peter Zijlstrae7850592010-05-21 14:43:08 +02005982 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005983
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984 if (!regs)
5985 return;
5986
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005987 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005988 return;
5989
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005990 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5991 data->period = nr;
5992 return perf_swevent_overflow(event, 1, data, regs);
5993 } else
5994 data->period = event->hw.last_period;
5995
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005996 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005997 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005998
Peter Zijlstrae7850592010-05-21 14:43:08 +02005999 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01006000 return;
6001
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006002 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006003}
6004
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006005static int perf_exclude_event(struct perf_event *event,
6006 struct pt_regs *regs)
6007{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006008 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01006009 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006010
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006011 if (regs) {
6012 if (event->attr.exclude_user && user_mode(regs))
6013 return 1;
6014
6015 if (event->attr.exclude_kernel && !user_mode(regs))
6016 return 1;
6017 }
6018
6019 return 0;
6020}
6021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006022static int perf_swevent_match(struct perf_event *event,
6023 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08006024 u32 event_id,
6025 struct perf_sample_data *data,
6026 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006027{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006028 if (event->attr.type != type)
6029 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006030
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006031 if (event->attr.config != event_id)
6032 return 0;
6033
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006034 if (perf_exclude_event(event, regs))
6035 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006036
6037 return 1;
6038}
6039
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006040static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006041{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006042 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006043
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006044 return hash_64(val, SWEVENT_HLIST_BITS);
6045}
6046
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006047static inline struct hlist_head *
6048__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006049{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006050 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006051
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006052 return &hlist->heads[hash];
6053}
6054
6055/* For the read side: events when they trigger */
6056static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006057find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006058{
6059 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006060
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006061 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006062 if (!hlist)
6063 return NULL;
6064
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006065 return __find_swevent_head(hlist, type, event_id);
6066}
6067
6068/* For the event head insertion and removal in the hlist */
6069static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006070find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006071{
6072 struct swevent_hlist *hlist;
6073 u32 event_id = event->attr.config;
6074 u64 type = event->attr.type;
6075
6076 /*
6077 * Event scheduling is always serialized against hlist allocation
6078 * and release. Which makes the protected version suitable here.
6079 * The context lock guarantees that.
6080 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006081 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006082 lockdep_is_held(&event->ctx->lock));
6083 if (!hlist)
6084 return NULL;
6085
6086 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006087}
6088
6089static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006090 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006091 struct perf_sample_data *data,
6092 struct pt_regs *regs)
6093{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006094 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006095 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006096 struct hlist_head *head;
6097
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006098 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006099 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006100 if (!head)
6101 goto end;
6102
Sasha Levinb67bfe02013-02-27 17:06:00 -08006103 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006104 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006105 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006106 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006107end:
6108 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006109}
6110
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006111DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6112
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006113int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006115 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006116
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006117 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006119EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006120
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006121inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006122{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006123 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006124
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006125 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006126}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006127
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006128void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006129{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006130 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006131
6132 if (WARN_ON_ONCE(!regs))
6133 return;
6134
6135 perf_sample_data_init(&data, addr, 0);
6136 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6137}
6138
6139void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6140{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006141 int rctx;
6142
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006143 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006144 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006145 if (unlikely(rctx < 0))
6146 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006147
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006148 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006149
6150 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006151fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006152 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006153}
6154
6155static void perf_swevent_read(struct perf_event *event)
6156{
6157}
6158
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006159static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006160{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006161 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006162 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006163 struct hlist_head *head;
6164
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006165 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006166 hwc->last_period = hwc->sample_period;
6167 perf_swevent_set_period(event);
6168 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006169
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006170 hwc->state = !(flags & PERF_EF_START);
6171
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006172 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02006173 if (!head) {
6174 /*
6175 * We can race with cpu hotplug code. Do not
6176 * WARN if the cpu just got unplugged.
6177 */
6178 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006179 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02006180 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006181
6182 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006183 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006184
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006185 return 0;
6186}
6187
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006188static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006189{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006190 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006191}
6192
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006193static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006194{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006195 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006196}
6197
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006198static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006199{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006200 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006201}
6202
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006203/* Deref the hlist from the update side */
6204static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006205swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006206{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006207 return rcu_dereference_protected(swhash->swevent_hlist,
6208 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006209}
6210
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006211static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006212{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006213 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006214
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006215 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006216 return;
6217
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006218 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006219 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006220}
6221
6222static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6223{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006224 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006225
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006226 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006227
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006228 if (!--swhash->hlist_refcount)
6229 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006230
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006231 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006232}
6233
6234static void swevent_hlist_put(struct perf_event *event)
6235{
6236 int cpu;
6237
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006238 for_each_possible_cpu(cpu)
6239 swevent_hlist_put_cpu(event, cpu);
6240}
6241
6242static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6243{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006244 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006245 int err = 0;
6246
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006247 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006248
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006249 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006250 struct swevent_hlist *hlist;
6251
6252 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6253 if (!hlist) {
6254 err = -ENOMEM;
6255 goto exit;
6256 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006257 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006258 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006259 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006260exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006261 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006262
6263 return err;
6264}
6265
6266static int swevent_hlist_get(struct perf_event *event)
6267{
6268 int err;
6269 int cpu, failed_cpu;
6270
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006271 get_online_cpus();
6272 for_each_possible_cpu(cpu) {
6273 err = swevent_hlist_get_cpu(event, cpu);
6274 if (err) {
6275 failed_cpu = cpu;
6276 goto fail;
6277 }
6278 }
6279 put_online_cpus();
6280
6281 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006282fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006283 for_each_possible_cpu(cpu) {
6284 if (cpu == failed_cpu)
6285 break;
6286 swevent_hlist_put_cpu(event, cpu);
6287 }
6288
6289 put_online_cpus();
6290 return err;
6291}
6292
Ingo Molnarc5905af2012-02-24 08:31:31 +01006293struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006294
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006295static void sw_perf_event_destroy(struct perf_event *event)
6296{
6297 u64 event_id = event->attr.config;
6298
6299 WARN_ON(event->parent);
6300
Ingo Molnarc5905af2012-02-24 08:31:31 +01006301 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006302 swevent_hlist_put(event);
6303}
6304
6305static int perf_swevent_init(struct perf_event *event)
6306{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006307 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006308
6309 if (event->attr.type != PERF_TYPE_SOFTWARE)
6310 return -ENOENT;
6311
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006312 /*
6313 * no branch sampling for software events
6314 */
6315 if (has_branch_stack(event))
6316 return -EOPNOTSUPP;
6317
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006318 switch (event_id) {
6319 case PERF_COUNT_SW_CPU_CLOCK:
6320 case PERF_COUNT_SW_TASK_CLOCK:
6321 return -ENOENT;
6322
6323 default:
6324 break;
6325 }
6326
Dan Carpenterce677832010-10-24 21:50:42 +02006327 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006328 return -ENOENT;
6329
6330 if (!event->parent) {
6331 int err;
6332
6333 err = swevent_hlist_get(event);
6334 if (err)
6335 return err;
6336
Ingo Molnarc5905af2012-02-24 08:31:31 +01006337 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006338 event->destroy = sw_perf_event_destroy;
6339 }
6340
6341 return 0;
6342}
6343
6344static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006345 .task_ctx_nr = perf_sw_context,
6346
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006347 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006348 .add = perf_swevent_add,
6349 .del = perf_swevent_del,
6350 .start = perf_swevent_start,
6351 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006352 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006353};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006354
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006355#ifdef CONFIG_EVENT_TRACING
6356
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006357static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006358 struct perf_sample_data *data)
6359{
6360 void *record = data->raw->data;
6361
6362 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6363 return 1;
6364 return 0;
6365}
6366
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006367static int perf_tp_event_match(struct perf_event *event,
6368 struct perf_sample_data *data,
6369 struct pt_regs *regs)
6370{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006371 if (event->hw.state & PERF_HES_STOPPED)
6372 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006373 /*
6374 * All tracepoints are from kernel-space.
6375 */
6376 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006377 return 0;
6378
6379 if (!perf_tp_filter_match(event, data))
6380 return 0;
6381
6382 return 1;
6383}
6384
6385void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006386 struct pt_regs *regs, struct hlist_head *head, int rctx,
6387 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006388{
6389 struct perf_sample_data data;
6390 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006391
6392 struct perf_raw_record raw = {
6393 .size = entry_size,
6394 .data = record,
6395 };
6396
Robert Richterfd0d0002012-04-02 20:19:08 +02006397 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006398 data.raw = &raw;
6399
Sasha Levinb67bfe02013-02-27 17:06:00 -08006400 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006401 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006402 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006403 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006404
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006405 /*
6406 * If we got specified a target task, also iterate its context and
6407 * deliver this event there too.
6408 */
6409 if (task && task != current) {
6410 struct perf_event_context *ctx;
6411 struct trace_entry *entry = record;
6412
6413 rcu_read_lock();
6414 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6415 if (!ctx)
6416 goto unlock;
6417
6418 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6419 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6420 continue;
6421 if (event->attr.config != entry->type)
6422 continue;
6423 if (perf_tp_event_match(event, &data, regs))
6424 perf_swevent_event(event, count, &data, regs);
6425 }
6426unlock:
6427 rcu_read_unlock();
6428 }
6429
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006430 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006431}
6432EXPORT_SYMBOL_GPL(perf_tp_event);
6433
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006434static void tp_perf_event_destroy(struct perf_event *event)
6435{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006436 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006437}
6438
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006439static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006440{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006441 int err;
6442
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006443 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6444 return -ENOENT;
6445
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006446 /*
6447 * no branch sampling for tracepoint events
6448 */
6449 if (has_branch_stack(event))
6450 return -EOPNOTSUPP;
6451
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006452 err = perf_trace_init(event);
6453 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006454 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006455
6456 event->destroy = tp_perf_event_destroy;
6457
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006458 return 0;
6459}
6460
6461static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006462 .task_ctx_nr = perf_sw_context,
6463
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006464 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006465 .add = perf_trace_add,
6466 .del = perf_trace_del,
6467 .start = perf_swevent_start,
6468 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006469 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006470};
6471
6472static inline void perf_tp_register(void)
6473{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006474 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006475}
Li Zefan6fb29152009-10-15 11:21:42 +08006476
6477static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6478{
6479 char *filter_str;
6480 int ret;
6481
6482 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6483 return -EINVAL;
6484
6485 filter_str = strndup_user(arg, PAGE_SIZE);
6486 if (IS_ERR(filter_str))
6487 return PTR_ERR(filter_str);
6488
6489 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6490
6491 kfree(filter_str);
6492 return ret;
6493}
6494
6495static void perf_event_free_filter(struct perf_event *event)
6496{
6497 ftrace_profile_free_filter(event);
6498}
6499
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006500#else
Li Zefan6fb29152009-10-15 11:21:42 +08006501
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006502static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006503{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504}
Li Zefan6fb29152009-10-15 11:21:42 +08006505
6506static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6507{
6508 return -ENOENT;
6509}
6510
6511static void perf_event_free_filter(struct perf_event *event)
6512{
6513}
6514
Li Zefan07b139c2009-12-21 14:27:35 +08006515#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006516
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006517#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006518void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006519{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006520 struct perf_sample_data sample;
6521 struct pt_regs *regs = data;
6522
Robert Richterfd0d0002012-04-02 20:19:08 +02006523 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006524
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006525 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006526 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006527}
6528#endif
6529
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006530/*
6531 * hrtimer based swevent callback
6532 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006533
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006534static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006535{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006536 enum hrtimer_restart ret = HRTIMER_RESTART;
6537 struct perf_sample_data data;
6538 struct pt_regs *regs;
6539 struct perf_event *event;
6540 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006542 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006543
6544 if (event->state != PERF_EVENT_STATE_ACTIVE)
6545 return HRTIMER_NORESTART;
6546
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006547 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006548
Robert Richterfd0d0002012-04-02 20:19:08 +02006549 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006550 regs = get_irq_regs();
6551
6552 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006553 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006554 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006555 ret = HRTIMER_NORESTART;
6556 }
6557
6558 period = max_t(u64, 10000, event->hw.sample_period);
6559 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6560
6561 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006562}
6563
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006564static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006565{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006566 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006567 s64 period;
6568
6569 if (!is_sampling_event(event))
6570 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006571
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006572 period = local64_read(&hwc->period_left);
6573 if (period) {
6574 if (period < 0)
6575 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006576
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006577 local64_set(&hwc->period_left, 0);
6578 } else {
6579 period = max_t(u64, 10000, hwc->sample_period);
6580 }
6581 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006582 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006583 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006584}
6585
6586static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6587{
6588 struct hw_perf_event *hwc = &event->hw;
6589
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006590 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006591 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006592 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006593
6594 hrtimer_cancel(&hwc->hrtimer);
6595 }
6596}
6597
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006598static void perf_swevent_init_hrtimer(struct perf_event *event)
6599{
6600 struct hw_perf_event *hwc = &event->hw;
6601
6602 if (!is_sampling_event(event))
6603 return;
6604
6605 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6606 hwc->hrtimer.function = perf_swevent_hrtimer;
6607
6608 /*
6609 * Since hrtimers have a fixed rate, we can do a static freq->period
6610 * mapping and avoid the whole period adjust feedback stuff.
6611 */
6612 if (event->attr.freq) {
6613 long freq = event->attr.sample_freq;
6614
6615 event->attr.sample_period = NSEC_PER_SEC / freq;
6616 hwc->sample_period = event->attr.sample_period;
6617 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006618 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006619 event->attr.freq = 0;
6620 }
6621}
6622
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006623/*
6624 * Software event: cpu wall time clock
6625 */
6626
6627static void cpu_clock_event_update(struct perf_event *event)
6628{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006629 s64 prev;
6630 u64 now;
6631
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006632 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006633 prev = local64_xchg(&event->hw.prev_count, now);
6634 local64_add(now - prev, &event->count);
6635}
6636
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006637static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006638{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006639 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006640 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006641}
6642
6643static void cpu_clock_event_stop(struct perf_event *event, int flags)
6644{
6645 perf_swevent_cancel_hrtimer(event);
6646 cpu_clock_event_update(event);
6647}
6648
6649static int cpu_clock_event_add(struct perf_event *event, int flags)
6650{
6651 if (flags & PERF_EF_START)
6652 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08006653 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006654
6655 return 0;
6656}
6657
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006658static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006659{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006660 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006661}
6662
6663static void cpu_clock_event_read(struct perf_event *event)
6664{
6665 cpu_clock_event_update(event);
6666}
6667
6668static int cpu_clock_event_init(struct perf_event *event)
6669{
6670 if (event->attr.type != PERF_TYPE_SOFTWARE)
6671 return -ENOENT;
6672
6673 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6674 return -ENOENT;
6675
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006676 /*
6677 * no branch sampling for software events
6678 */
6679 if (has_branch_stack(event))
6680 return -EOPNOTSUPP;
6681
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006682 perf_swevent_init_hrtimer(event);
6683
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006684 return 0;
6685}
6686
6687static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006688 .task_ctx_nr = perf_sw_context,
6689
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006690 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006691 .add = cpu_clock_event_add,
6692 .del = cpu_clock_event_del,
6693 .start = cpu_clock_event_start,
6694 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006695 .read = cpu_clock_event_read,
6696};
6697
6698/*
6699 * Software event: task time clock
6700 */
6701
6702static void task_clock_event_update(struct perf_event *event, u64 now)
6703{
6704 u64 prev;
6705 s64 delta;
6706
6707 prev = local64_xchg(&event->hw.prev_count, now);
6708 delta = now - prev;
6709 local64_add(delta, &event->count);
6710}
6711
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006712static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006713{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006714 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006715 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006716}
6717
6718static void task_clock_event_stop(struct perf_event *event, int flags)
6719{
6720 perf_swevent_cancel_hrtimer(event);
6721 task_clock_event_update(event, event->ctx->time);
6722}
6723
6724static int task_clock_event_add(struct perf_event *event, int flags)
6725{
6726 if (flags & PERF_EF_START)
6727 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08006728 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006729
6730 return 0;
6731}
6732
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006733static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006734{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006735 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006736}
6737
6738static void task_clock_event_read(struct perf_event *event)
6739{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006740 u64 now = perf_clock();
6741 u64 delta = now - event->ctx->timestamp;
6742 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006743
6744 task_clock_event_update(event, time);
6745}
6746
6747static int task_clock_event_init(struct perf_event *event)
6748{
6749 if (event->attr.type != PERF_TYPE_SOFTWARE)
6750 return -ENOENT;
6751
6752 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6753 return -ENOENT;
6754
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006755 /*
6756 * no branch sampling for software events
6757 */
6758 if (has_branch_stack(event))
6759 return -EOPNOTSUPP;
6760
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006761 perf_swevent_init_hrtimer(event);
6762
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006763 return 0;
6764}
6765
6766static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006767 .task_ctx_nr = perf_sw_context,
6768
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006769 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006770 .add = task_clock_event_add,
6771 .del = task_clock_event_del,
6772 .start = task_clock_event_start,
6773 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006774 .read = task_clock_event_read,
6775};
6776
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006777static void perf_pmu_nop_void(struct pmu *pmu)
6778{
6779}
6780
6781static int perf_pmu_nop_int(struct pmu *pmu)
6782{
6783 return 0;
6784}
6785
6786static void perf_pmu_start_txn(struct pmu *pmu)
6787{
6788 perf_pmu_disable(pmu);
6789}
6790
6791static int perf_pmu_commit_txn(struct pmu *pmu)
6792{
6793 perf_pmu_enable(pmu);
6794 return 0;
6795}
6796
6797static void perf_pmu_cancel_txn(struct pmu *pmu)
6798{
6799 perf_pmu_enable(pmu);
6800}
6801
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006802static int perf_event_idx_default(struct perf_event *event)
6803{
Peter Zijlstrac719f562014-10-21 11:10:21 +02006804 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006805}
6806
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006807/*
6808 * Ensures all contexts with the same task_ctx_nr have the same
6809 * pmu_cpu_context too.
6810 */
Mark Rutland9e317042014-02-10 17:44:18 +00006811static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006812{
6813 struct pmu *pmu;
6814
6815 if (ctxn < 0)
6816 return NULL;
6817
6818 list_for_each_entry(pmu, &pmus, entry) {
6819 if (pmu->task_ctx_nr == ctxn)
6820 return pmu->pmu_cpu_context;
6821 }
6822
6823 return NULL;
6824}
6825
Peter Zijlstra51676952010-12-07 14:18:20 +01006826static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006827{
Peter Zijlstra51676952010-12-07 14:18:20 +01006828 int cpu;
6829
6830 for_each_possible_cpu(cpu) {
6831 struct perf_cpu_context *cpuctx;
6832
6833 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6834
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006835 if (cpuctx->unique_pmu == old_pmu)
6836 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006837 }
6838}
6839
6840static void free_pmu_context(struct pmu *pmu)
6841{
6842 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006843
6844 mutex_lock(&pmus_lock);
6845 /*
6846 * Like a real lame refcount.
6847 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006848 list_for_each_entry(i, &pmus, entry) {
6849 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6850 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006851 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006852 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006853 }
6854
Peter Zijlstra51676952010-12-07 14:18:20 +01006855 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006856out:
6857 mutex_unlock(&pmus_lock);
6858}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006859static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006860
Peter Zijlstraabe43402010-11-17 23:17:37 +01006861static ssize_t
6862type_show(struct device *dev, struct device_attribute *attr, char *page)
6863{
6864 struct pmu *pmu = dev_get_drvdata(dev);
6865
6866 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6867}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006868static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006869
Stephane Eranian62b85632013-04-03 14:21:34 +02006870static ssize_t
6871perf_event_mux_interval_ms_show(struct device *dev,
6872 struct device_attribute *attr,
6873 char *page)
6874{
6875 struct pmu *pmu = dev_get_drvdata(dev);
6876
6877 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6878}
6879
6880static ssize_t
6881perf_event_mux_interval_ms_store(struct device *dev,
6882 struct device_attribute *attr,
6883 const char *buf, size_t count)
6884{
6885 struct pmu *pmu = dev_get_drvdata(dev);
6886 int timer, cpu, ret;
6887
6888 ret = kstrtoint(buf, 0, &timer);
6889 if (ret)
6890 return ret;
6891
6892 if (timer < 1)
6893 return -EINVAL;
6894
6895 /* same value, noting to do */
6896 if (timer == pmu->hrtimer_interval_ms)
6897 return count;
6898
6899 pmu->hrtimer_interval_ms = timer;
6900
6901 /* update all cpuctx for this PMU */
6902 for_each_possible_cpu(cpu) {
6903 struct perf_cpu_context *cpuctx;
6904 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6905 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6906
6907 if (hrtimer_active(&cpuctx->hrtimer))
6908 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6909 }
6910
6911 return count;
6912}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006913static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006914
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006915static struct attribute *pmu_dev_attrs[] = {
6916 &dev_attr_type.attr,
6917 &dev_attr_perf_event_mux_interval_ms.attr,
6918 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006919};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006920ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006921
6922static int pmu_bus_running;
6923static struct bus_type pmu_bus = {
6924 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006925 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006926};
6927
6928static void pmu_dev_release(struct device *dev)
6929{
6930 kfree(dev);
6931}
6932
6933static int pmu_dev_alloc(struct pmu *pmu)
6934{
6935 int ret = -ENOMEM;
6936
6937 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6938 if (!pmu->dev)
6939 goto out;
6940
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006941 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006942 device_initialize(pmu->dev);
6943 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6944 if (ret)
6945 goto free_dev;
6946
6947 dev_set_drvdata(pmu->dev, pmu);
6948 pmu->dev->bus = &pmu_bus;
6949 pmu->dev->release = pmu_dev_release;
6950 ret = device_add(pmu->dev);
6951 if (ret)
6952 goto free_dev;
6953
6954out:
6955 return ret;
6956
6957free_dev:
6958 put_device(pmu->dev);
6959 goto out;
6960}
6961
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006962static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006963static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006964
Mischa Jonker03d8e802013-06-04 11:45:48 +02006965int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006966{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006967 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006968
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006969 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006970 ret = -ENOMEM;
6971 pmu->pmu_disable_count = alloc_percpu(int);
6972 if (!pmu->pmu_disable_count)
6973 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006974
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006975 pmu->type = -1;
6976 if (!name)
6977 goto skip_type;
6978 pmu->name = name;
6979
6980 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006981 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6982 if (type < 0) {
6983 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006984 goto free_pdc;
6985 }
6986 }
6987 pmu->type = type;
6988
Peter Zijlstraabe43402010-11-17 23:17:37 +01006989 if (pmu_bus_running) {
6990 ret = pmu_dev_alloc(pmu);
6991 if (ret)
6992 goto free_idr;
6993 }
6994
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006995skip_type:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006996 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6997 if (pmu->pmu_cpu_context)
6998 goto got_cpu_context;
6999
Wei Yongjunc4814202013-04-12 11:05:54 +08007000 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007001 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
7002 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01007003 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007004
7005 for_each_possible_cpu(cpu) {
7006 struct perf_cpu_context *cpuctx;
7007
7008 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02007009 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01007010 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02007011 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007012 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02007013
7014 __perf_cpu_hrtimer_init(cpuctx, cpu);
7015
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02007016 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007017 }
7018
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007019got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02007020 if (!pmu->start_txn) {
7021 if (pmu->pmu_enable) {
7022 /*
7023 * If we have pmu_enable/pmu_disable calls, install
7024 * transaction stubs that use that to try and batch
7025 * hardware accesses.
7026 */
7027 pmu->start_txn = perf_pmu_start_txn;
7028 pmu->commit_txn = perf_pmu_commit_txn;
7029 pmu->cancel_txn = perf_pmu_cancel_txn;
7030 } else {
7031 pmu->start_txn = perf_pmu_nop_void;
7032 pmu->commit_txn = perf_pmu_nop_int;
7033 pmu->cancel_txn = perf_pmu_nop_void;
7034 }
7035 }
7036
7037 if (!pmu->pmu_enable) {
7038 pmu->pmu_enable = perf_pmu_nop_void;
7039 pmu->pmu_disable = perf_pmu_nop_void;
7040 }
7041
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007042 if (!pmu->event_idx)
7043 pmu->event_idx = perf_event_idx_default;
7044
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007045 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007046 ret = 0;
7047unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007048 mutex_unlock(&pmus_lock);
7049
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007050 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007051
Peter Zijlstraabe43402010-11-17 23:17:37 +01007052free_dev:
7053 device_del(pmu->dev);
7054 put_device(pmu->dev);
7055
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007056free_idr:
7057 if (pmu->type >= PERF_TYPE_MAX)
7058 idr_remove(&pmu_idr, pmu->type);
7059
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007060free_pdc:
7061 free_percpu(pmu->pmu_disable_count);
7062 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007063}
Yan, Zhengc464c762014-03-18 16:56:41 +08007064EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007065
7066void perf_pmu_unregister(struct pmu *pmu)
7067{
7068 mutex_lock(&pmus_lock);
7069 list_del_rcu(&pmu->entry);
7070 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071
7072 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007073 * We dereference the pmu list under both SRCU and regular RCU, so
7074 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007076 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007077 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007078
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007079 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007080 if (pmu->type >= PERF_TYPE_MAX)
7081 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007082 device_del(pmu->dev);
7083 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007084 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007085}
Yan, Zhengc464c762014-03-18 16:56:41 +08007086EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007087
Mark Rutlandcc34b982015-01-07 14:56:51 +00007088static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7089{
7090 int ret;
7091
7092 if (!try_module_get(pmu->module))
7093 return -ENODEV;
7094 event->pmu = pmu;
7095 ret = pmu->event_init(event);
7096 if (ret)
7097 module_put(pmu->module);
7098
7099 return ret;
7100}
7101
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007102struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007103{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007104 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007105 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007106 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007107
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007108 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007109
7110 rcu_read_lock();
7111 pmu = idr_find(&pmu_idr, event->attr.type);
7112 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007113 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007114 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007115 if (ret)
7116 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007117 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007118 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007119
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007120 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007121 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007122 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007123 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007124
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007125 if (ret != -ENOENT) {
7126 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007127 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007128 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007129 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007130 pmu = ERR_PTR(-ENOENT);
7131unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007132 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007133
7134 return pmu;
7135}
7136
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007137static void account_event_cpu(struct perf_event *event, int cpu)
7138{
7139 if (event->parent)
7140 return;
7141
7142 if (has_branch_stack(event)) {
7143 if (!(event->attach_state & PERF_ATTACH_TASK))
7144 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
7145 }
7146 if (is_cgroup_event(event))
7147 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7148}
7149
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007150static void account_event(struct perf_event *event)
7151{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007152 if (event->parent)
7153 return;
7154
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007155 if (event->attach_state & PERF_ATTACH_TASK)
7156 static_key_slow_inc(&perf_sched_events.key);
7157 if (event->attr.mmap || event->attr.mmap_data)
7158 atomic_inc(&nr_mmap_events);
7159 if (event->attr.comm)
7160 atomic_inc(&nr_comm_events);
7161 if (event->attr.task)
7162 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007163 if (event->attr.freq) {
7164 if (atomic_inc_return(&nr_freq_events) == 1)
7165 tick_nohz_full_kick_all();
7166 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007167 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007168 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007169 if (is_cgroup_event(event))
7170 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007171
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007172 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007173}
7174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007175/*
7176 * Allocate and initialize a event structure
7177 */
7178static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007179perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007180 struct task_struct *task,
7181 struct perf_event *group_leader,
7182 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007183 perf_overflow_handler_t overflow_handler,
7184 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007185{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007186 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007187 struct perf_event *event;
7188 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007189 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007190
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007191 if ((unsigned)cpu >= nr_cpu_ids) {
7192 if (!task || cpu != -1)
7193 return ERR_PTR(-EINVAL);
7194 }
7195
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007196 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007197 if (!event)
7198 return ERR_PTR(-ENOMEM);
7199
7200 /*
7201 * Single events are their own group leaders, with an
7202 * empty sibling list:
7203 */
7204 if (!group_leader)
7205 group_leader = event;
7206
7207 mutex_init(&event->child_mutex);
7208 INIT_LIST_HEAD(&event->child_list);
7209
7210 INIT_LIST_HEAD(&event->group_entry);
7211 INIT_LIST_HEAD(&event->event_entry);
7212 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007213 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007214 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007215 INIT_HLIST_NODE(&event->hlist_entry);
7216
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007217
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007218 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007219 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007220
7221 mutex_init(&event->mmap_mutex);
7222
Al Viroa6fa9412012-08-20 14:59:25 +01007223 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007224 event->cpu = cpu;
7225 event->attr = *attr;
7226 event->group_leader = group_leader;
7227 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007228 event->oncpu = -1;
7229
7230 event->parent = parent_event;
7231
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007232 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007233 event->id = atomic64_inc_return(&perf_event_id);
7234
7235 event->state = PERF_EVENT_STATE_INACTIVE;
7236
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007237 if (task) {
7238 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01007239
7240 if (attr->type == PERF_TYPE_TRACEPOINT)
7241 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007242#ifdef CONFIG_HAVE_HW_BREAKPOINT
7243 /*
7244 * hw_breakpoint is a bit difficult here..
7245 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01007246 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007247 event->hw.bp_target = task;
7248#endif
7249 }
7250
Avi Kivity4dc0da82011-06-29 18:42:35 +03007251 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007252 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007253 context = parent_event->overflow_handler_context;
7254 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007255
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007256 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007257 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007258
Jiri Olsa0231bb52013-02-01 11:23:45 +01007259 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007260
7261 pmu = NULL;
7262
7263 hwc = &event->hw;
7264 hwc->sample_period = attr->sample_period;
7265 if (attr->freq && attr->sample_freq)
7266 hwc->sample_period = 1;
7267 hwc->last_period = hwc->sample_period;
7268
Peter Zijlstrae7850592010-05-21 14:43:08 +02007269 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007270
7271 /*
7272 * we currently do not support PERF_FORMAT_GROUP on inherited events
7273 */
7274 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007275 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007276
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007277 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007278 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007279 goto err_ns;
7280 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007281 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007282 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007283 }
7284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007285 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007286 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7287 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007288 if (err)
7289 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01007290 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007291 }
7292
7293 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007294
7295err_pmu:
7296 if (event->destroy)
7297 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007298 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007299err_ns:
7300 if (event->ns)
7301 put_pid_ns(event->ns);
7302 kfree(event);
7303
7304 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007305}
7306
7307static int perf_copy_attr(struct perf_event_attr __user *uattr,
7308 struct perf_event_attr *attr)
7309{
7310 u32 size;
7311 int ret;
7312
7313 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7314 return -EFAULT;
7315
7316 /*
7317 * zero the full structure, so that a short copy will be nice.
7318 */
7319 memset(attr, 0, sizeof(*attr));
7320
7321 ret = get_user(size, &uattr->size);
7322 if (ret)
7323 return ret;
7324
7325 if (size > PAGE_SIZE) /* silly large */
7326 goto err_size;
7327
7328 if (!size) /* abi compat */
7329 size = PERF_ATTR_SIZE_VER0;
7330
7331 if (size < PERF_ATTR_SIZE_VER0)
7332 goto err_size;
7333
7334 /*
7335 * If we're handed a bigger struct than we know of,
7336 * ensure all the unknown bits are 0 - i.e. new
7337 * user-space does not rely on any kernel feature
7338 * extensions we dont know about yet.
7339 */
7340 if (size > sizeof(*attr)) {
7341 unsigned char __user *addr;
7342 unsigned char __user *end;
7343 unsigned char val;
7344
7345 addr = (void __user *)uattr + sizeof(*attr);
7346 end = (void __user *)uattr + size;
7347
7348 for (; addr < end; addr++) {
7349 ret = get_user(val, addr);
7350 if (ret)
7351 return ret;
7352 if (val)
7353 goto err_size;
7354 }
7355 size = sizeof(*attr);
7356 }
7357
7358 ret = copy_from_user(attr, uattr, size);
7359 if (ret)
7360 return -EFAULT;
7361
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307362 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007363 return -EINVAL;
7364
7365 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7366 return -EINVAL;
7367
7368 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
7369 return -EINVAL;
7370
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007371 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
7372 u64 mask = attr->branch_sample_type;
7373
7374 /* only using defined bits */
7375 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
7376 return -EINVAL;
7377
7378 /* at least one branch bit must be set */
7379 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
7380 return -EINVAL;
7381
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007382 /* propagate priv level, when not set for branch */
7383 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
7384
7385 /* exclude_kernel checked on syscall entry */
7386 if (!attr->exclude_kernel)
7387 mask |= PERF_SAMPLE_BRANCH_KERNEL;
7388
7389 if (!attr->exclude_user)
7390 mask |= PERF_SAMPLE_BRANCH_USER;
7391
7392 if (!attr->exclude_hv)
7393 mask |= PERF_SAMPLE_BRANCH_HV;
7394 /*
7395 * adjust user setting (for HW filter setup)
7396 */
7397 attr->branch_sample_type = mask;
7398 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007399 /* privileged levels capture (kernel, hv): check permissions */
7400 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007401 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7402 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007403 }
Jiri Olsa40189942012-08-07 15:20:37 +02007404
Jiri Olsac5ebced2012-08-07 15:20:40 +02007405 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007406 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007407 if (ret)
7408 return ret;
7409 }
7410
7411 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7412 if (!arch_perf_have_user_stack_dump())
7413 return -ENOSYS;
7414
7415 /*
7416 * We have __u32 type for the size, but so far
7417 * we can only use __u16 as maximum due to the
7418 * __u16 sample size limit.
7419 */
7420 if (attr->sample_stack_user >= USHRT_MAX)
7421 ret = -EINVAL;
7422 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7423 ret = -EINVAL;
7424 }
Jiri Olsa40189942012-08-07 15:20:37 +02007425
Stephane Eranian60e23642014-09-24 13:48:37 +02007426 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
7427 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007428out:
7429 return ret;
7430
7431err_size:
7432 put_user(sizeof(*attr), &uattr->size);
7433 ret = -E2BIG;
7434 goto out;
7435}
7436
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007437static int
7438perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007439{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007440 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007441 int ret = -EINVAL;
7442
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007443 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007444 goto set;
7445
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007446 /* don't allow circular references */
7447 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007448 goto out;
7449
Peter Zijlstra0f139302010-05-20 14:35:15 +02007450 /*
7451 * Don't allow cross-cpu buffers
7452 */
7453 if (output_event->cpu != event->cpu)
7454 goto out;
7455
7456 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007457 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007458 */
7459 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7460 goto out;
7461
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007462set:
7463 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007464 /* Can't redirect output if we've got an active mmap() */
7465 if (atomic_read(&event->mmap_count))
7466 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007467
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007468 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007469 /* get the rb we want to redirect to */
7470 rb = ring_buffer_get(output_event);
7471 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007472 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007473 }
7474
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007475 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007477 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007478unlock:
7479 mutex_unlock(&event->mmap_mutex);
7480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007481out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007482 return ret;
7483}
7484
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007485static void mutex_lock_double(struct mutex *a, struct mutex *b)
7486{
7487 if (b < a)
7488 swap(a, b);
7489
7490 mutex_lock(a);
7491 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
7492}
7493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007494/**
7495 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7496 *
7497 * @attr_uptr: event_id type attributes for monitoring/sampling
7498 * @pid: target pid
7499 * @cpu: target cpu
7500 * @group_fd: group leader event fd
7501 */
7502SYSCALL_DEFINE5(perf_event_open,
7503 struct perf_event_attr __user *, attr_uptr,
7504 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7505{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007506 struct perf_event *group_leader = NULL, *output_event = NULL;
7507 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007508 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007509 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007510 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007511 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007512 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007513 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007514 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007515 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007516 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007517 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007518
7519 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007520 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007521 return -EINVAL;
7522
7523 err = perf_copy_attr(attr_uptr, &attr);
7524 if (err)
7525 return err;
7526
7527 if (!attr.exclude_kernel) {
7528 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7529 return -EACCES;
7530 }
7531
7532 if (attr.freq) {
7533 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7534 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007535 } else {
7536 if (attr.sample_period & (1ULL << 63))
7537 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007538 }
7539
Stephane Eraniane5d13672011-02-14 11:20:01 +02007540 /*
7541 * In cgroup mode, the pid argument is used to pass the fd
7542 * opened to the cgroup directory in cgroupfs. The cpu argument
7543 * designates the cpu on which to monitor threads from that
7544 * cgroup.
7545 */
7546 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7547 return -EINVAL;
7548
Yann Droneauda21b0b32014-01-05 21:36:33 +01007549 if (flags & PERF_FLAG_FD_CLOEXEC)
7550 f_flags |= O_CLOEXEC;
7551
7552 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007553 if (event_fd < 0)
7554 return event_fd;
7555
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007556 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007557 err = perf_fget_light(group_fd, &group);
7558 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007559 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007560 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007561 if (flags & PERF_FLAG_FD_OUTPUT)
7562 output_event = group_leader;
7563 if (flags & PERF_FLAG_FD_NO_GROUP)
7564 group_leader = NULL;
7565 }
7566
Stephane Eraniane5d13672011-02-14 11:20:01 +02007567 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007568 task = find_lively_task_by_vpid(pid);
7569 if (IS_ERR(task)) {
7570 err = PTR_ERR(task);
7571 goto err_group_fd;
7572 }
7573 }
7574
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007575 if (task && group_leader &&
7576 group_leader->attr.inherit != attr.inherit) {
7577 err = -EINVAL;
7578 goto err_task;
7579 }
7580
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007581 get_online_cpus();
7582
Avi Kivity4dc0da82011-06-29 18:42:35 +03007583 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7584 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007585 if (IS_ERR(event)) {
7586 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007587 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007588 }
7589
Stephane Eraniane5d13672011-02-14 11:20:01 +02007590 if (flags & PERF_FLAG_PID_CGROUP) {
7591 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007592 if (err) {
7593 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007594 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007595 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007596 }
7597
Vince Weaver53b25332014-05-16 17:12:12 -04007598 if (is_sampling_event(event)) {
7599 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7600 err = -ENOTSUPP;
7601 goto err_alloc;
7602 }
7603 }
7604
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007605 account_event(event);
7606
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007607 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007608 * Special case software events and allow them to be part of
7609 * any hardware group.
7610 */
7611 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007612
7613 if (group_leader &&
7614 (is_software_event(event) != is_software_event(group_leader))) {
7615 if (is_software_event(event)) {
7616 /*
7617 * If event and group_leader are not both a software
7618 * event, and event is, then group leader is not.
7619 *
7620 * Allow the addition of software events to !software
7621 * groups, this is safe because software events never
7622 * fail to schedule.
7623 */
7624 pmu = group_leader->pmu;
7625 } else if (is_software_event(group_leader) &&
7626 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7627 /*
7628 * In case the group is a pure software group, and we
7629 * try to add a hardware event, move the whole group to
7630 * the hardware context.
7631 */
7632 move_group = 1;
7633 }
7634 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007635
7636 /*
7637 * Get the target context (task or percpu):
7638 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007639 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007640 if (IS_ERR(ctx)) {
7641 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007642 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007643 }
7644
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007645 if (task) {
7646 put_task_struct(task);
7647 task = NULL;
7648 }
7649
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007650 /*
7651 * Look up the group leader (we will attach this event to it):
7652 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007653 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007654 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007656 /*
7657 * Do not allow a recursive hierarchy (this new sibling
7658 * becoming part of another group-sibling):
7659 */
7660 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007661 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007662 /*
7663 * Do not allow to attach to a group in a different
7664 * task or CPU context:
7665 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007666 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01007667 /*
7668 * Make sure we're both on the same task, or both
7669 * per-cpu events.
7670 */
7671 if (group_leader->ctx->task != ctx->task)
7672 goto err_context;
7673
7674 /*
7675 * Make sure we're both events for the same CPU;
7676 * grouping events for different CPUs is broken; since
7677 * you can never concurrently schedule them anyhow.
7678 */
7679 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007680 goto err_context;
7681 } else {
7682 if (group_leader->ctx != ctx)
7683 goto err_context;
7684 }
7685
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007686 /*
7687 * Only a group leader can be exclusive or pinned
7688 */
7689 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007690 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007691 }
7692
7693 if (output_event) {
7694 err = perf_event_set_output(event, output_event);
7695 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007696 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007697 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007698
Yann Droneauda21b0b32014-01-05 21:36:33 +01007699 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7700 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007701 if (IS_ERR(event_file)) {
7702 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007703 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007704 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007705
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007706 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007707 gctx = group_leader->ctx;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007708
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007709 /*
7710 * See perf_event_ctx_lock() for comments on the details
7711 * of swizzling perf_event::ctx.
7712 */
7713 mutex_lock_double(&gctx->mutex, &ctx->mutex);
7714
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007715 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007716
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007717 list_for_each_entry(sibling, &group_leader->sibling_list,
7718 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007719 perf_remove_from_context(sibling, false);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007720 put_ctx(gctx);
7721 }
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007722 } else {
7723 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007724 }
7725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007726 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007727
7728 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007729 /*
7730 * Wait for everybody to stop referencing the events through
7731 * the old lists, before installing it on new lists.
7732 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007733 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007734
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007735 /*
7736 * Install the group siblings before the group leader.
7737 *
7738 * Because a group leader will try and install the entire group
7739 * (through the sibling list, which is still in-tact), we can
7740 * end up with siblings installed in the wrong context.
7741 *
7742 * By installing siblings first we NO-OP because they're not
7743 * reachable through the group lists.
7744 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007745 list_for_each_entry(sibling, &group_leader->sibling_list,
7746 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007747 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01007748 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007749 get_ctx(ctx);
7750 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007751
7752 /*
7753 * Removing from the context ends up with disabled
7754 * event. What we want here is event in the initial
7755 * startup state, ready to be add into new context.
7756 */
7757 perf_event__state_init(group_leader);
7758 perf_install_in_context(ctx, group_leader, group_leader->cpu);
7759 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007760 }
7761
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007762 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007763 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007764
7765 if (move_group) {
7766 mutex_unlock(&gctx->mutex);
7767 put_ctx(gctx);
7768 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007769 mutex_unlock(&ctx->mutex);
7770
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007771 put_online_cpus();
7772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007773 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007775 mutex_lock(&current->perf_event_mutex);
7776 list_add_tail(&event->owner_entry, &current->perf_event_list);
7777 mutex_unlock(&current->perf_event_mutex);
7778
Peter Zijlstra8a495422010-05-27 15:47:49 +02007779 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007780 * Precalculate sample_data sizes
7781 */
7782 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007783 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007784
7785 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007786 * Drop the reference on the group_event after placing the
7787 * new event on the sibling_list. This ensures destruction
7788 * of the group leader will find the pointer to itself in
7789 * perf_group_detach().
7790 */
Al Viro2903ff02012-08-28 12:52:22 -04007791 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007792 fd_install(event_fd, event_file);
7793 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007794
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007795err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007796 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007797 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007798err_alloc:
7799 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007800err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007801 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007802err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007803 if (task)
7804 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007805err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007806 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007807err_fd:
7808 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007809 return err;
7810}
7811
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007812/**
7813 * perf_event_create_kernel_counter
7814 *
7815 * @attr: attributes of the counter to create
7816 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007817 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007818 */
7819struct perf_event *
7820perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007821 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007822 perf_overflow_handler_t overflow_handler,
7823 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007824{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007825 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007826 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007827 int err;
7828
7829 /*
7830 * Get the target context (task or percpu):
7831 */
7832
Avi Kivity4dc0da82011-06-29 18:42:35 +03007833 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7834 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007835 if (IS_ERR(event)) {
7836 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007837 goto err;
7838 }
7839
Jiri Olsaf8697762014-08-01 14:33:01 +02007840 /* Mark owner so we could distinguish it from user events. */
7841 event->owner = EVENT_OWNER_KERNEL;
7842
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007843 account_event(event);
7844
Matt Helsley38a81da2010-09-13 13:01:20 -07007845 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007846 if (IS_ERR(ctx)) {
7847 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007848 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007849 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007850
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007851 WARN_ON_ONCE(ctx->parent_ctx);
7852 mutex_lock(&ctx->mutex);
7853 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007854 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007855 mutex_unlock(&ctx->mutex);
7856
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007857 return event;
7858
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007859err_free:
7860 free_event(event);
7861err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007862 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007863}
7864EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7865
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007866void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7867{
7868 struct perf_event_context *src_ctx;
7869 struct perf_event_context *dst_ctx;
7870 struct perf_event *event, *tmp;
7871 LIST_HEAD(events);
7872
7873 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7874 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7875
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007876 /*
7877 * See perf_event_ctx_lock() for comments on the details
7878 * of swizzling perf_event::ctx.
7879 */
7880 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007881 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7882 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007883 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007884 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007885 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007886 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007887 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007888
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007889 /*
7890 * Wait for the events to quiesce before re-instating them.
7891 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007892 synchronize_rcu();
7893
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007894 /*
7895 * Re-instate events in 2 passes.
7896 *
7897 * Skip over group leaders and only install siblings on this first
7898 * pass, siblings will not get enabled without a leader, however a
7899 * leader will enable its siblings, even if those are still on the old
7900 * context.
7901 */
7902 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7903 if (event->group_leader == event)
7904 continue;
7905
7906 list_del(&event->migrate_entry);
7907 if (event->state >= PERF_EVENT_STATE_OFF)
7908 event->state = PERF_EVENT_STATE_INACTIVE;
7909 account_event_cpu(event, dst_cpu);
7910 perf_install_in_context(dst_ctx, event, dst_cpu);
7911 get_ctx(dst_ctx);
7912 }
7913
7914 /*
7915 * Once all the siblings are setup properly, install the group leaders
7916 * to make it go.
7917 */
Peter Zijlstra98861672013-10-03 16:02:23 +02007918 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7919 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007920 if (event->state >= PERF_EVENT_STATE_OFF)
7921 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007922 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007923 perf_install_in_context(dst_ctx, event, dst_cpu);
7924 get_ctx(dst_ctx);
7925 }
7926 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007927 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007928}
7929EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7930
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007931static void sync_child_event(struct perf_event *child_event,
7932 struct task_struct *child)
7933{
7934 struct perf_event *parent_event = child_event->parent;
7935 u64 child_val;
7936
7937 if (child_event->attr.inherit_stat)
7938 perf_event_read_event(child_event, child);
7939
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007940 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007941
7942 /*
7943 * Add back the child's count to the parent's count:
7944 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007945 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007946 atomic64_add(child_event->total_time_enabled,
7947 &parent_event->child_total_time_enabled);
7948 atomic64_add(child_event->total_time_running,
7949 &parent_event->child_total_time_running);
7950
7951 /*
7952 * Remove this event from the parent's list
7953 */
7954 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7955 mutex_lock(&parent_event->child_mutex);
7956 list_del_init(&child_event->child_list);
7957 mutex_unlock(&parent_event->child_mutex);
7958
7959 /*
Jiri Olsadc633982014-09-12 13:18:26 +02007960 * Make sure user/parent get notified, that we just
7961 * lost one event.
7962 */
7963 perf_event_wakeup(parent_event);
7964
7965 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007966 * Release the parent event, if this was the last
7967 * reference to it.
7968 */
Al Viroa6fa9412012-08-20 14:59:25 +01007969 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007970}
7971
7972static void
7973__perf_event_exit_task(struct perf_event *child_event,
7974 struct perf_event_context *child_ctx,
7975 struct task_struct *child)
7976{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007977 /*
7978 * Do not destroy the 'original' grouping; because of the context
7979 * switch optimization the original events could've ended up in a
7980 * random child task.
7981 *
7982 * If we were to destroy the original group, all group related
7983 * operations would cease to function properly after this random
7984 * child dies.
7985 *
7986 * Do destroy all inherited groups, we don't care about those
7987 * and being thorough is better.
7988 */
7989 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007990
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007991 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007992 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007993 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007994 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007995 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007996 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007997 sync_child_event(child_event, child);
7998 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04007999 } else {
8000 child_event->state = PERF_EVENT_STATE_EXIT;
8001 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008002 }
8003}
8004
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008005static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008006{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008007 struct perf_event *child_event, *next;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008008 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008009 unsigned long flags;
8010
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008011 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008012 perf_event_task(child, NULL, 0);
8013 return;
8014 }
8015
8016 local_irq_save(flags);
8017 /*
8018 * We can't reschedule here because interrupts are disabled,
8019 * and either child is current or it is a task that can't be
8020 * scheduled, so we are now safe from rescheduling changing
8021 * our context.
8022 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01008023 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008024
8025 /*
8026 * Take the context lock here so that if find_get_context is
8027 * reading child->perf_event_ctxp, we wait until it has
8028 * incremented the context's refcount before we do put_ctx below.
8029 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01008030 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02008031 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008032 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008033
8034 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008035 * If this context is a clone; unclone it so it can't get
8036 * swapped to another process while we're removing all
8037 * the events from it.
8038 */
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008039 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01008040 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01008041 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008042
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008043 if (clone_ctx)
8044 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008045
8046 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008047 * Report the task dead after unscheduling the events so that we
8048 * won't get any samples after PERF_RECORD_EXIT. We can however still
8049 * get a few PERF_RECORD_READ events.
8050 */
8051 perf_event_task(child, child_ctx, 0);
8052
8053 /*
8054 * We can recurse on the same lock type through:
8055 *
8056 * __perf_event_exit_task()
8057 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01008058 * put_event()
8059 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008060 *
8061 * But since its the parent context it won't be the same instance.
8062 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02008063 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008064
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008065 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008066 __perf_event_exit_task(child_event, child_ctx, child);
8067
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008068 mutex_unlock(&child_ctx->mutex);
8069
8070 put_ctx(child_ctx);
8071}
8072
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008073/*
8074 * When a child task exits, feed back event values to parent events.
8075 */
8076void perf_event_exit_task(struct task_struct *child)
8077{
Peter Zijlstra88821352010-11-09 19:01:43 +01008078 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008079 int ctxn;
8080
Peter Zijlstra88821352010-11-09 19:01:43 +01008081 mutex_lock(&child->perf_event_mutex);
8082 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8083 owner_entry) {
8084 list_del_init(&event->owner_entry);
8085
8086 /*
8087 * Ensure the list deletion is visible before we clear
8088 * the owner, closes a race against perf_release() where
8089 * we need to serialize on the owner->perf_event_mutex.
8090 */
8091 smp_wmb();
8092 event->owner = NULL;
8093 }
8094 mutex_unlock(&child->perf_event_mutex);
8095
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008096 for_each_task_context_nr(ctxn)
8097 perf_event_exit_task_context(child, ctxn);
8098}
8099
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008100static void perf_free_event(struct perf_event *event,
8101 struct perf_event_context *ctx)
8102{
8103 struct perf_event *parent = event->parent;
8104
8105 if (WARN_ON_ONCE(!parent))
8106 return;
8107
8108 mutex_lock(&parent->child_mutex);
8109 list_del_init(&event->child_list);
8110 mutex_unlock(&parent->child_mutex);
8111
Al Viroa6fa9412012-08-20 14:59:25 +01008112 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008113
Peter Zijlstra652884f2015-01-23 11:20:10 +01008114 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008115 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008116 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008117 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008118 free_event(event);
8119}
8120
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008121/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008122 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008123 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008124 *
8125 * Not all locks are strictly required, but take them anyway to be nice and
8126 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008127 */
8128void perf_event_free_task(struct task_struct *task)
8129{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008130 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008131 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008132 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008133
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008134 for_each_task_context_nr(ctxn) {
8135 ctx = task->perf_event_ctxp[ctxn];
8136 if (!ctx)
8137 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008138
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008139 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008140again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008141 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8142 group_entry)
8143 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008144
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008145 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8146 group_entry)
8147 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008148
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008149 if (!list_empty(&ctx->pinned_groups) ||
8150 !list_empty(&ctx->flexible_groups))
8151 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008152
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008153 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008154
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008155 put_ctx(ctx);
8156 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008157}
8158
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008159void perf_event_delayed_put(struct task_struct *task)
8160{
8161 int ctxn;
8162
8163 for_each_task_context_nr(ctxn)
8164 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8165}
8166
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008167/*
8168 * inherit a event from parent task to child task:
8169 */
8170static struct perf_event *
8171inherit_event(struct perf_event *parent_event,
8172 struct task_struct *parent,
8173 struct perf_event_context *parent_ctx,
8174 struct task_struct *child,
8175 struct perf_event *group_leader,
8176 struct perf_event_context *child_ctx)
8177{
Jiri Olsa1929def2014-09-12 13:18:27 +02008178 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008179 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008180 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008181
8182 /*
8183 * Instead of creating recursive hierarchies of events,
8184 * we link inherited events back to the original parent,
8185 * which has a filp for sure, which we use as the reference
8186 * count:
8187 */
8188 if (parent_event->parent)
8189 parent_event = parent_event->parent;
8190
8191 child_event = perf_event_alloc(&parent_event->attr,
8192 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008193 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008194 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008195 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008196 if (IS_ERR(child_event))
8197 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008198
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008199 if (is_orphaned_event(parent_event) ||
8200 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01008201 free_event(child_event);
8202 return NULL;
8203 }
8204
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008205 get_ctx(child_ctx);
8206
8207 /*
8208 * Make the child state follow the state of the parent event,
8209 * not its attr.disabled bit. We hold the parent's mutex,
8210 * so we won't race with perf_event_{en, dis}able_family.
8211 */
Jiri Olsa1929def2014-09-12 13:18:27 +02008212 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008213 child_event->state = PERF_EVENT_STATE_INACTIVE;
8214 else
8215 child_event->state = PERF_EVENT_STATE_OFF;
8216
8217 if (parent_event->attr.freq) {
8218 u64 sample_period = parent_event->hw.sample_period;
8219 struct hw_perf_event *hwc = &child_event->hw;
8220
8221 hwc->sample_period = sample_period;
8222 hwc->last_period = sample_period;
8223
8224 local64_set(&hwc->period_left, sample_period);
8225 }
8226
8227 child_event->ctx = child_ctx;
8228 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008229 child_event->overflow_handler_context
8230 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008231
8232 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02008233 * Precalculate sample_data sizes
8234 */
8235 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02008236 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02008237
8238 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008239 * Link it up in the child's context:
8240 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02008241 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008242 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02008243 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008244
8245 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008246 * Link this into the parent event's child list
8247 */
8248 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8249 mutex_lock(&parent_event->child_mutex);
8250 list_add_tail(&child_event->child_list, &parent_event->child_list);
8251 mutex_unlock(&parent_event->child_mutex);
8252
8253 return child_event;
8254}
8255
8256static int inherit_group(struct perf_event *parent_event,
8257 struct task_struct *parent,
8258 struct perf_event_context *parent_ctx,
8259 struct task_struct *child,
8260 struct perf_event_context *child_ctx)
8261{
8262 struct perf_event *leader;
8263 struct perf_event *sub;
8264 struct perf_event *child_ctr;
8265
8266 leader = inherit_event(parent_event, parent, parent_ctx,
8267 child, NULL, child_ctx);
8268 if (IS_ERR(leader))
8269 return PTR_ERR(leader);
8270 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
8271 child_ctr = inherit_event(sub, parent, parent_ctx,
8272 child, leader, child_ctx);
8273 if (IS_ERR(child_ctr))
8274 return PTR_ERR(child_ctr);
8275 }
8276 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008277}
8278
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008279static int
8280inherit_task_group(struct perf_event *event, struct task_struct *parent,
8281 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008282 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008283 int *inherited_all)
8284{
8285 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008286 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008287
8288 if (!event->attr.inherit) {
8289 *inherited_all = 0;
8290 return 0;
8291 }
8292
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008293 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008294 if (!child_ctx) {
8295 /*
8296 * This is executed from the parent task context, so
8297 * inherit events that have been marked for cloning.
8298 * First allocate and initialize a context for the
8299 * child.
8300 */
8301
Jiri Olsa734df5a2013-07-09 17:44:10 +02008302 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008303 if (!child_ctx)
8304 return -ENOMEM;
8305
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008306 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008307 }
8308
8309 ret = inherit_group(event, parent, parent_ctx,
8310 child, child_ctx);
8311
8312 if (ret)
8313 *inherited_all = 0;
8314
8315 return ret;
8316}
8317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008318/*
8319 * Initialize the perf_event context in task_struct
8320 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02008321static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008322{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008323 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008324 struct perf_event_context *cloned_ctx;
8325 struct perf_event *event;
8326 struct task_struct *parent = current;
8327 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008328 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008329 int ret = 0;
8330
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008331 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008332 return 0;
8333
8334 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008335 * If the parent's context is a clone, pin it so it won't get
8336 * swapped under us.
8337 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008338 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02008339 if (!parent_ctx)
8340 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008341
8342 /*
8343 * No need to check if parent_ctx != NULL here; since we saw
8344 * it non-NULL earlier, the only reason for it to become NULL
8345 * is if we exit, and since we're currently in the middle of
8346 * a fork we can't be exiting at the same time.
8347 */
8348
8349 /*
8350 * Lock the parent list. No need to lock the child - not PID
8351 * hashed yet and not running, so nobody can access it.
8352 */
8353 mutex_lock(&parent_ctx->mutex);
8354
8355 /*
8356 * We dont have to disable NMIs - we are only looking at
8357 * the list, not manipulating it:
8358 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008359 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008360 ret = inherit_task_group(event, parent, parent_ctx,
8361 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008362 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008363 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008364 }
8365
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008366 /*
8367 * We can't hold ctx->lock when iterating the ->flexible_group list due
8368 * to allocations, but we need to prevent rotation because
8369 * rotate_ctx() will change the list from interrupt context.
8370 */
8371 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8372 parent_ctx->rotate_disable = 1;
8373 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
8374
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008375 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008376 ret = inherit_task_group(event, parent, parent_ctx,
8377 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008378 if (ret)
8379 break;
8380 }
8381
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008382 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8383 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008384
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008385 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008386
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01008387 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008388 /*
8389 * Mark the child context as a clone of the parent
8390 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008391 *
8392 * Note that if the parent is a clone, the holding of
8393 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008394 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008395 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008396 if (cloned_ctx) {
8397 child_ctx->parent_ctx = cloned_ctx;
8398 child_ctx->parent_gen = parent_ctx->parent_gen;
8399 } else {
8400 child_ctx->parent_ctx = parent_ctx;
8401 child_ctx->parent_gen = parent_ctx->generation;
8402 }
8403 get_ctx(child_ctx->parent_ctx);
8404 }
8405
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008406 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008407 mutex_unlock(&parent_ctx->mutex);
8408
8409 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008410 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008411
8412 return ret;
8413}
8414
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008415/*
8416 * Initialize the perf_event context in task_struct
8417 */
8418int perf_event_init_task(struct task_struct *child)
8419{
8420 int ctxn, ret;
8421
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01008422 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
8423 mutex_init(&child->perf_event_mutex);
8424 INIT_LIST_HEAD(&child->perf_event_list);
8425
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008426 for_each_task_context_nr(ctxn) {
8427 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008428 if (ret) {
8429 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008430 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008431 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008432 }
8433
8434 return 0;
8435}
8436
Paul Mackerras220b1402010-03-10 20:45:52 +11008437static void __init perf_event_init_all_cpus(void)
8438{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008439 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11008440 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11008441
8442 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008443 swhash = &per_cpu(swevent_htable, cpu);
8444 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00008445 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11008446 }
8447}
8448
Paul Gortmaker0db06282013-06-19 14:53:51 -04008449static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008450{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008451 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008452
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008453 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008454 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008455 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008456 struct swevent_hlist *hlist;
8457
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008458 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
8459 WARN_ON(!hlist);
8460 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008461 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008462 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008463}
8464
Peter Zijlstrac2774432010-12-08 15:29:02 +01008465#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008466static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008467{
Mark Rutland226424e2014-11-05 16:11:44 +00008468 struct remove_event re = { .detach_group = true };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008469 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008470
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008471 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008472 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8473 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008474 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008475}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008476
8477static void perf_event_exit_cpu_context(int cpu)
8478{
8479 struct perf_event_context *ctx;
8480 struct pmu *pmu;
8481 int idx;
8482
8483 idx = srcu_read_lock(&pmus_srcu);
8484 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008485 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008486
8487 mutex_lock(&ctx->mutex);
8488 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8489 mutex_unlock(&ctx->mutex);
8490 }
8491 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008492}
8493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008494static void perf_event_exit_cpu(int cpu)
8495{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008496 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008497
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008498 perf_event_exit_cpu_context(cpu);
8499
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008500 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008501 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008502 swevent_hlist_release(swhash);
8503 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008504}
8505#else
8506static inline void perf_event_exit_cpu(int cpu) { }
8507#endif
8508
Peter Zijlstrac2774432010-12-08 15:29:02 +01008509static int
8510perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8511{
8512 int cpu;
8513
8514 for_each_online_cpu(cpu)
8515 perf_event_exit_cpu(cpu);
8516
8517 return NOTIFY_OK;
8518}
8519
8520/*
8521 * Run the perf reboot notifier at the very last possible moment so that
8522 * the generic watchdog code runs as long as possible.
8523 */
8524static struct notifier_block perf_reboot_notifier = {
8525 .notifier_call = perf_reboot,
8526 .priority = INT_MIN,
8527};
8528
Paul Gortmaker0db06282013-06-19 14:53:51 -04008529static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008530perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8531{
8532 unsigned int cpu = (long)hcpu;
8533
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008534 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008535
8536 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008537 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008538 perf_event_init_cpu(cpu);
8539 break;
8540
Peter Zijlstra5e116372010-06-11 13:35:08 +02008541 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008542 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008543 perf_event_exit_cpu(cpu);
8544 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008545 default:
8546 break;
8547 }
8548
8549 return NOTIFY_OK;
8550}
8551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008552void __init perf_event_init(void)
8553{
Jason Wessel3c502e72010-11-04 17:33:01 -05008554 int ret;
8555
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008556 idr_init(&pmu_idr);
8557
Paul Mackerras220b1402010-03-10 20:45:52 +11008558 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008559 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008560 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8561 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8562 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008563 perf_tp_register();
8564 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008565 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008566
8567 ret = init_hw_breakpoint();
8568 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008569
8570 /* do not patch jump label more than once per second */
8571 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008572
8573 /*
8574 * Build time assertion that we keep the data_head at the intended
8575 * location. IOW, validation we got the __reserved[] size right.
8576 */
8577 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8578 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008579}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008580
8581static int __init perf_event_sysfs_init(void)
8582{
8583 struct pmu *pmu;
8584 int ret;
8585
8586 mutex_lock(&pmus_lock);
8587
8588 ret = bus_register(&pmu_bus);
8589 if (ret)
8590 goto unlock;
8591
8592 list_for_each_entry(pmu, &pmus, entry) {
8593 if (!pmu->name || pmu->type < 0)
8594 continue;
8595
8596 ret = pmu_dev_alloc(pmu);
8597 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8598 }
8599 pmu_bus_running = 1;
8600 ret = 0;
8601
8602unlock:
8603 mutex_unlock(&pmus_lock);
8604
8605 return ret;
8606}
8607device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008608
8609#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008610static struct cgroup_subsys_state *
8611perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008612{
8613 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008614
Li Zefan1b15d052011-03-03 14:26:06 +08008615 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008616 if (!jc)
8617 return ERR_PTR(-ENOMEM);
8618
Stephane Eraniane5d13672011-02-14 11:20:01 +02008619 jc->info = alloc_percpu(struct perf_cgroup_info);
8620 if (!jc->info) {
8621 kfree(jc);
8622 return ERR_PTR(-ENOMEM);
8623 }
8624
Stephane Eraniane5d13672011-02-14 11:20:01 +02008625 return &jc->css;
8626}
8627
Tejun Heoeb954192013-08-08 20:11:23 -04008628static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008629{
Tejun Heoeb954192013-08-08 20:11:23 -04008630 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8631
Stephane Eraniane5d13672011-02-14 11:20:01 +02008632 free_percpu(jc->info);
8633 kfree(jc);
8634}
8635
8636static int __perf_cgroup_move(void *info)
8637{
8638 struct task_struct *task = info;
8639 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8640 return 0;
8641}
8642
Tejun Heoeb954192013-08-08 20:11:23 -04008643static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8644 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008645{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008646 struct task_struct *task;
8647
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008648 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008649 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008650}
8651
Tejun Heoeb954192013-08-08 20:11:23 -04008652static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8653 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef52012-01-31 13:47:36 +08008654 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008655{
8656 /*
8657 * cgroup_exit() is called in the copy_process() failure path.
8658 * Ignore this case since the task hasn't ran yet, this avoids
8659 * trying to poke a half freed task state from generic code.
8660 */
8661 if (!(task->flags & PF_EXITING))
8662 return;
8663
Tejun Heobb9d97b2011-12-12 18:12:21 -08008664 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008665}
8666
Tejun Heo073219e2014-02-08 10:36:58 -05008667struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008668 .css_alloc = perf_cgroup_css_alloc,
8669 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008670 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008671 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008672};
8673#endif /* CONFIG_CGROUP_PERF */