blob: c40c2cac2d8e3eaf8106d8746aa83bb72e5899e5 [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>
Matt Fleming39bed6c2015-01-23 18:45:40 +000037#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080039#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050040#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020041#include <linux/mm_types.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
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);
Yan, Zhengba532502014-11-04 21:55:58 -0500156static DEFINE_PER_CPU(int, perf_sched_cb_usages);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200157
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200158static atomic_t nr_mmap_events __read_mostly;
159static atomic_t nr_comm_events __read_mostly;
160static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200161static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200163static LIST_HEAD(pmus);
164static DEFINE_MUTEX(pmus_lock);
165static struct srcu_struct pmus_srcu;
166
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200167/*
168 * perf event paranoia level:
169 * -1 - not paranoid at all
170 * 0 - disallow raw tracepoint access for unpriv
171 * 1 - disallow cpu events for unpriv
172 * 2 - disallow kernel profiling for unpriv
173 */
174int sysctl_perf_event_paranoid __read_mostly = 1;
175
Frederic Weisbecker20443382011-03-31 03:33:29 +0200176/* Minimum for 512 kiB + 1 user control page */
177int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178
179/*
180 * max perf event sample rate
181 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700182#define DEFAULT_MAX_SAMPLE_RATE 100000
183#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
184#define DEFAULT_CPU_TIME_MAX_PERCENT 25
185
186int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
187
188static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
189static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
190
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200191static int perf_sample_allowed_ns __read_mostly =
192 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700193
194void update_perf_cpu_limits(void)
195{
196 u64 tmp = perf_sample_period_ns;
197
198 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200199 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200200 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700201}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100202
Stephane Eranian9e630202013-04-03 14:21:33 +0200203static int perf_rotate_context(struct perf_cpu_context *cpuctx);
204
Peter Zijlstra163ec432011-02-16 11:22:34 +0100205int perf_proc_update_handler(struct ctl_table *table, int write,
206 void __user *buffer, size_t *lenp,
207 loff_t *ppos)
208{
Knut Petersen723478c2013-09-25 14:29:37 +0200209 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100210
211 if (ret || !write)
212 return ret;
213
214 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700215 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
216 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100217
218 return 0;
219}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200220
Dave Hansen14c63f12013-06-21 08:51:36 -0700221int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
222
223int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
224 void __user *buffer, size_t *lenp,
225 loff_t *ppos)
226{
227 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
228
229 if (ret || !write)
230 return ret;
231
232 update_perf_cpu_limits();
233
234 return 0;
235}
236
237/*
238 * perf samples are done in some very critical code paths (NMIs).
239 * If they take too much CPU time, the system can lock up and not
240 * get any real work done. This will drop the sample rate when
241 * we detect that events are taking too long.
242 */
243#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200244static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700245
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100246static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700247{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100248 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700249 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200250 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100251
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500252 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100253 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
254
255 printk_ratelimited(KERN_WARNING
256 "perf interrupt took too long (%lld > %lld), lowering "
257 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100258 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100259 sysctl_perf_event_sample_rate);
260}
261
262static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
263
264void perf_sample_event_took(u64 sample_len_ns)
265{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200266 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100267 u64 avg_local_sample_len;
268 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700269
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200270 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700271 return;
272
273 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500274 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700275 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
276 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500277 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700278
279 /*
280 * note: this will be biased artifically low until we have
281 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
282 * from having to maintain a count.
283 */
284 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
285
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200286 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700287 return;
288
289 if (max_samples_per_tick <= 1)
290 return;
291
292 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
293 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
294 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
295
Dave Hansen14c63f12013-06-21 08:51:36 -0700296 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100297
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100298 if (!irq_work_queue(&perf_duration_work)) {
299 early_printk("perf interrupt took too long (%lld > %lld), lowering "
300 "kernel.perf_event_max_sample_rate to %d\n",
301 avg_local_sample_len, allowed_ns >> 1,
302 sysctl_perf_event_sample_rate);
303 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700304}
305
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200306static atomic64_t perf_event_id;
307
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200308static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
309 enum event_type_t event_type);
310
311static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200312 enum event_type_t event_type,
313 struct task_struct *task);
314
315static void update_context_time(struct perf_event_context *ctx);
316static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200317
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200318void __weak perf_event_print_debug(void) { }
319
Matt Fleming84c79912010-10-03 21:41:13 +0100320extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200321{
Matt Fleming84c79912010-10-03 21:41:13 +0100322 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200323}
324
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200325static inline u64 perf_clock(void)
326{
327 return local_clock();
328}
329
Peter Zijlstra34f43922015-02-20 14:05:38 +0100330static inline u64 perf_event_clock(struct perf_event *event)
331{
332 return event->clock();
333}
334
Stephane Eraniane5d13672011-02-14 11:20:01 +0200335static inline struct perf_cpu_context *
336__get_cpu_context(struct perf_event_context *ctx)
337{
338 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
339}
340
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200341static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
342 struct perf_event_context *ctx)
343{
344 raw_spin_lock(&cpuctx->ctx.lock);
345 if (ctx)
346 raw_spin_lock(&ctx->lock);
347}
348
349static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
350 struct perf_event_context *ctx)
351{
352 if (ctx)
353 raw_spin_unlock(&ctx->lock);
354 raw_spin_unlock(&cpuctx->ctx.lock);
355}
356
Stephane Eraniane5d13672011-02-14 11:20:01 +0200357#ifdef CONFIG_CGROUP_PERF
358
Stephane Eraniane5d13672011-02-14 11:20:01 +0200359static inline bool
360perf_cgroup_match(struct perf_event *event)
361{
362 struct perf_event_context *ctx = event->ctx;
363 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
364
Tejun Heoef824fa2013-04-08 19:00:38 -0700365 /* @event doesn't care about cgroup */
366 if (!event->cgrp)
367 return true;
368
369 /* wants specific cgroup scope but @cpuctx isn't associated with any */
370 if (!cpuctx->cgrp)
371 return false;
372
373 /*
374 * Cgroup scoping is recursive. An event enabled for a cgroup is
375 * also enabled for all its descendant cgroups. If @cpuctx's
376 * cgroup is a descendant of @event's (the test covers identity
377 * case), it's a match.
378 */
379 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
380 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200381}
382
Stephane Eraniane5d13672011-02-14 11:20:01 +0200383static inline void perf_detach_cgroup(struct perf_event *event)
384{
Zefan Li4e2ba652014-09-19 16:53:14 +0800385 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200386 event->cgrp = NULL;
387}
388
389static inline int is_cgroup_event(struct perf_event *event)
390{
391 return event->cgrp != NULL;
392}
393
394static inline u64 perf_cgroup_event_time(struct perf_event *event)
395{
396 struct perf_cgroup_info *t;
397
398 t = per_cpu_ptr(event->cgrp->info, event->cpu);
399 return t->time;
400}
401
402static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
403{
404 struct perf_cgroup_info *info;
405 u64 now;
406
407 now = perf_clock();
408
409 info = this_cpu_ptr(cgrp->info);
410
411 info->time += now - info->timestamp;
412 info->timestamp = now;
413}
414
415static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
416{
417 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
418 if (cgrp_out)
419 __update_cgrp_time(cgrp_out);
420}
421
422static inline void update_cgrp_time_from_event(struct perf_event *event)
423{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200424 struct perf_cgroup *cgrp;
425
Stephane Eraniane5d13672011-02-14 11:20:01 +0200426 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200427 * ensure we access cgroup data only when needed and
428 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200429 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200430 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200431 return;
432
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200433 cgrp = perf_cgroup_from_task(current);
434 /*
435 * Do not update time when cgroup is not active
436 */
437 if (cgrp == event->cgrp)
438 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200439}
440
441static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200442perf_cgroup_set_timestamp(struct task_struct *task,
443 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200444{
445 struct perf_cgroup *cgrp;
446 struct perf_cgroup_info *info;
447
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200448 /*
449 * ctx->lock held by caller
450 * ensure we do not access cgroup data
451 * unless we have the cgroup pinned (css_get)
452 */
453 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200454 return;
455
456 cgrp = perf_cgroup_from_task(task);
457 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200458 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200459}
460
461#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
462#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
463
464/*
465 * reschedule events based on the cgroup constraint of task.
466 *
467 * mode SWOUT : schedule out everything
468 * mode SWIN : schedule in based on cgroup for next
469 */
470void perf_cgroup_switch(struct task_struct *task, int mode)
471{
472 struct perf_cpu_context *cpuctx;
473 struct pmu *pmu;
474 unsigned long flags;
475
476 /*
477 * disable interrupts to avoid geting nr_cgroup
478 * changes via __perf_event_disable(). Also
479 * avoids preemption.
480 */
481 local_irq_save(flags);
482
483 /*
484 * we reschedule only in the presence of cgroup
485 * constrained events.
486 */
487 rcu_read_lock();
488
489 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200490 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200491 if (cpuctx->unique_pmu != pmu)
492 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200493
Stephane Eraniane5d13672011-02-14 11:20:01 +0200494 /*
495 * perf_cgroup_events says at least one
496 * context on this CPU has cgroup events.
497 *
498 * ctx->nr_cgroups reports the number of cgroup
499 * events for a context.
500 */
501 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200502 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
503 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200504
505 if (mode & PERF_CGROUP_SWOUT) {
506 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
507 /*
508 * must not be done before ctxswout due
509 * to event_filter_match() in event_sched_out()
510 */
511 cpuctx->cgrp = NULL;
512 }
513
514 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200515 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200516 /*
517 * set cgrp before ctxsw in to allow
518 * event_filter_match() to not have to pass
519 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200520 */
521 cpuctx->cgrp = perf_cgroup_from_task(task);
522 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
523 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200524 perf_pmu_enable(cpuctx->ctx.pmu);
525 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200526 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200527 }
528
529 rcu_read_unlock();
530
531 local_irq_restore(flags);
532}
533
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200534static inline void perf_cgroup_sched_out(struct task_struct *task,
535 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200536{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200537 struct perf_cgroup *cgrp1;
538 struct perf_cgroup *cgrp2 = NULL;
539
540 /*
541 * we come here when we know perf_cgroup_events > 0
542 */
543 cgrp1 = perf_cgroup_from_task(task);
544
545 /*
546 * next is NULL when called from perf_event_enable_on_exec()
547 * that will systematically cause a cgroup_switch()
548 */
549 if (next)
550 cgrp2 = perf_cgroup_from_task(next);
551
552 /*
553 * only schedule out current cgroup events if we know
554 * that we are switching to a different cgroup. Otherwise,
555 * do no touch the cgroup events.
556 */
557 if (cgrp1 != cgrp2)
558 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559}
560
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200561static inline void perf_cgroup_sched_in(struct task_struct *prev,
562 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200563{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200564 struct perf_cgroup *cgrp1;
565 struct perf_cgroup *cgrp2 = NULL;
566
567 /*
568 * we come here when we know perf_cgroup_events > 0
569 */
570 cgrp1 = perf_cgroup_from_task(task);
571
572 /* prev can never be NULL */
573 cgrp2 = perf_cgroup_from_task(prev);
574
575 /*
576 * only need to schedule in cgroup events if we are changing
577 * cgroup during ctxsw. Cgroup events were not scheduled
578 * out of ctxsw out if that was not the case.
579 */
580 if (cgrp1 != cgrp2)
581 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582}
583
584static inline int perf_cgroup_connect(int fd, struct perf_event *event,
585 struct perf_event_attr *attr,
586 struct perf_event *group_leader)
587{
588 struct perf_cgroup *cgrp;
589 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400590 struct fd f = fdget(fd);
591 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200592
Al Viro2903ff02012-08-28 12:52:22 -0400593 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200594 return -EBADF;
595
Al Virob5830432014-10-31 01:22:04 -0400596 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400597 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800598 if (IS_ERR(css)) {
599 ret = PTR_ERR(css);
600 goto out;
601 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200602
603 cgrp = container_of(css, struct perf_cgroup, css);
604 event->cgrp = cgrp;
605
606 /*
607 * all events in a group must monitor
608 * the same cgroup because a task belongs
609 * to only one perf cgroup at a time
610 */
611 if (group_leader && group_leader->cgrp != cgrp) {
612 perf_detach_cgroup(event);
613 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200614 }
Li Zefan3db272c2011-03-03 14:25:37 +0800615out:
Al Viro2903ff02012-08-28 12:52:22 -0400616 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200617 return ret;
618}
619
620static inline void
621perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
622{
623 struct perf_cgroup_info *t;
624 t = per_cpu_ptr(event->cgrp->info, event->cpu);
625 event->shadow_ctx_time = now - t->timestamp;
626}
627
628static inline void
629perf_cgroup_defer_enabled(struct perf_event *event)
630{
631 /*
632 * when the current task's perf cgroup does not match
633 * the event's, we need to remember to call the
634 * perf_mark_enable() function the first time a task with
635 * a matching perf cgroup is scheduled in.
636 */
637 if (is_cgroup_event(event) && !perf_cgroup_match(event))
638 event->cgrp_defer_enabled = 1;
639}
640
641static inline void
642perf_cgroup_mark_enabled(struct perf_event *event,
643 struct perf_event_context *ctx)
644{
645 struct perf_event *sub;
646 u64 tstamp = perf_event_time(event);
647
648 if (!event->cgrp_defer_enabled)
649 return;
650
651 event->cgrp_defer_enabled = 0;
652
653 event->tstamp_enabled = tstamp - event->total_time_enabled;
654 list_for_each_entry(sub, &event->sibling_list, group_entry) {
655 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
656 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
657 sub->cgrp_defer_enabled = 0;
658 }
659 }
660}
661#else /* !CONFIG_CGROUP_PERF */
662
663static inline bool
664perf_cgroup_match(struct perf_event *event)
665{
666 return true;
667}
668
669static inline void perf_detach_cgroup(struct perf_event *event)
670{}
671
672static inline int is_cgroup_event(struct perf_event *event)
673{
674 return 0;
675}
676
677static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
678{
679 return 0;
680}
681
682static inline void update_cgrp_time_from_event(struct perf_event *event)
683{
684}
685
686static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
687{
688}
689
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200690static inline void perf_cgroup_sched_out(struct task_struct *task,
691 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200692{
693}
694
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200695static inline void perf_cgroup_sched_in(struct task_struct *prev,
696 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200697{
698}
699
700static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
701 struct perf_event_attr *attr,
702 struct perf_event *group_leader)
703{
704 return -EINVAL;
705}
706
707static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200708perf_cgroup_set_timestamp(struct task_struct *task,
709 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200710{
711}
712
713void
714perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
715{
716}
717
718static inline void
719perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
720{
721}
722
723static inline u64 perf_cgroup_event_time(struct perf_event *event)
724{
725 return 0;
726}
727
728static inline void
729perf_cgroup_defer_enabled(struct perf_event *event)
730{
731}
732
733static inline void
734perf_cgroup_mark_enabled(struct perf_event *event,
735 struct perf_event_context *ctx)
736{
737}
738#endif
739
Stephane Eranian9e630202013-04-03 14:21:33 +0200740/*
741 * set default to be dependent on timer tick just
742 * like original code
743 */
744#define PERF_CPU_HRTIMER (1000 / HZ)
745/*
746 * function must be called with interrupts disbled
747 */
748static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
749{
750 struct perf_cpu_context *cpuctx;
751 enum hrtimer_restart ret = HRTIMER_NORESTART;
752 int rotations = 0;
753
754 WARN_ON(!irqs_disabled());
755
756 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
757
758 rotations = perf_rotate_context(cpuctx);
759
760 /*
761 * arm timer if needed
762 */
763 if (rotations) {
764 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
765 ret = HRTIMER_RESTART;
766 }
767
768 return ret;
769}
770
771/* CPU is going down */
772void perf_cpu_hrtimer_cancel(int cpu)
773{
774 struct perf_cpu_context *cpuctx;
775 struct pmu *pmu;
776 unsigned long flags;
777
778 if (WARN_ON(cpu != smp_processor_id()))
779 return;
780
781 local_irq_save(flags);
782
783 rcu_read_lock();
784
785 list_for_each_entry_rcu(pmu, &pmus, entry) {
786 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
787
788 if (pmu->task_ctx_nr == perf_sw_context)
789 continue;
790
791 hrtimer_cancel(&cpuctx->hrtimer);
792 }
793
794 rcu_read_unlock();
795
796 local_irq_restore(flags);
797}
798
799static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
800{
801 struct hrtimer *hr = &cpuctx->hrtimer;
802 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200803 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200804
805 /* no multiplexing needed for SW PMU */
806 if (pmu->task_ctx_nr == perf_sw_context)
807 return;
808
Stephane Eranian62b85632013-04-03 14:21:34 +0200809 /*
810 * check default is sane, if not set then force to
811 * default interval (1/tick)
812 */
813 timer = pmu->hrtimer_interval_ms;
814 if (timer < 1)
815 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
816
817 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200818
819 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
820 hr->function = perf_cpu_hrtimer_handler;
821}
822
823static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
824{
825 struct hrtimer *hr = &cpuctx->hrtimer;
826 struct pmu *pmu = cpuctx->ctx.pmu;
827
828 /* not for SW PMU */
829 if (pmu->task_ctx_nr == perf_sw_context)
830 return;
831
832 if (hrtimer_active(hr))
833 return;
834
835 if (!hrtimer_callback_running(hr))
836 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
837 0, HRTIMER_MODE_REL_PINNED, 0);
838}
839
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200840void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200841{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200842 int *count = this_cpu_ptr(pmu->pmu_disable_count);
843 if (!(*count)++)
844 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200845}
846
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200847void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200848{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200849 int *count = this_cpu_ptr(pmu->pmu_disable_count);
850 if (!--(*count))
851 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200852}
853
Mark Rutland2fde4f92015-01-07 15:01:54 +0000854static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200855
856/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000857 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
858 * perf_event_task_tick() are fully serialized because they're strictly cpu
859 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
860 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200861 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000862static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200863{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000864 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200865
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200866 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200867
Mark Rutland2fde4f92015-01-07 15:01:54 +0000868 WARN_ON(!list_empty(&ctx->active_ctx_list));
869
870 list_add(&ctx->active_ctx_list, head);
871}
872
873static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
874{
875 WARN_ON(!irqs_disabled());
876
877 WARN_ON(list_empty(&ctx->active_ctx_list));
878
879 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200880}
881
882static void get_ctx(struct perf_event_context *ctx)
883{
884 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
885}
886
Yan, Zheng4af57ef282014-11-04 21:56:01 -0500887static void free_ctx(struct rcu_head *head)
888{
889 struct perf_event_context *ctx;
890
891 ctx = container_of(head, struct perf_event_context, rcu_head);
892 kfree(ctx->task_ctx_data);
893 kfree(ctx);
894}
895
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200896static void put_ctx(struct perf_event_context *ctx)
897{
898 if (atomic_dec_and_test(&ctx->refcount)) {
899 if (ctx->parent_ctx)
900 put_ctx(ctx->parent_ctx);
901 if (ctx->task)
902 put_task_struct(ctx->task);
Yan, Zheng4af57ef282014-11-04 21:56:01 -0500903 call_rcu(&ctx->rcu_head, free_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200904 }
905}
906
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200907/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100908 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
909 * perf_pmu_migrate_context() we need some magic.
910 *
911 * Those places that change perf_event::ctx will hold both
912 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
913 *
914 * Lock ordering is by mutex address. There is one other site where
915 * perf_event_context::mutex nests and that is put_event(). But remember that
916 * that is a parent<->child context relation, and migration does not affect
917 * children, therefore these two orderings should not interact.
918 *
919 * The change in perf_event::ctx does not affect children (as claimed above)
920 * because the sys_perf_event_open() case will install a new event and break
921 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
922 * concerned with cpuctx and that doesn't have children.
923 *
924 * The places that change perf_event::ctx will issue:
925 *
926 * perf_remove_from_context();
927 * synchronize_rcu();
928 * perf_install_in_context();
929 *
930 * to affect the change. The remove_from_context() + synchronize_rcu() should
931 * quiesce the event, after which we can install it in the new location. This
932 * means that only external vectors (perf_fops, prctl) can perturb the event
933 * while in transit. Therefore all such accessors should also acquire
934 * perf_event_context::mutex to serialize against this.
935 *
936 * However; because event->ctx can change while we're waiting to acquire
937 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
938 * function.
939 *
940 * Lock order:
941 * task_struct::perf_event_mutex
942 * perf_event_context::mutex
943 * perf_event_context::lock
944 * perf_event::child_mutex;
945 * perf_event::mmap_mutex
946 * mmap_sem
947 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100948static struct perf_event_context *
949perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100950{
951 struct perf_event_context *ctx;
952
953again:
954 rcu_read_lock();
955 ctx = ACCESS_ONCE(event->ctx);
956 if (!atomic_inc_not_zero(&ctx->refcount)) {
957 rcu_read_unlock();
958 goto again;
959 }
960 rcu_read_unlock();
961
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100962 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100963 if (event->ctx != ctx) {
964 mutex_unlock(&ctx->mutex);
965 put_ctx(ctx);
966 goto again;
967 }
968
969 return ctx;
970}
971
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100972static inline struct perf_event_context *
973perf_event_ctx_lock(struct perf_event *event)
974{
975 return perf_event_ctx_lock_nested(event, 0);
976}
977
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100978static void perf_event_ctx_unlock(struct perf_event *event,
979 struct perf_event_context *ctx)
980{
981 mutex_unlock(&ctx->mutex);
982 put_ctx(ctx);
983}
984
985/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200986 * This must be done under the ctx->lock, such as to serialize against
987 * context_equiv(), therefore we cannot call put_ctx() since that might end up
988 * calling scheduler related locks and ctx->lock nests inside those.
989 */
990static __must_check struct perf_event_context *
991unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200992{
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200993 struct perf_event_context *parent_ctx = ctx->parent_ctx;
994
995 lockdep_assert_held(&ctx->lock);
996
997 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200998 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200999 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001000
1001 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001002}
1003
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001004static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1005{
1006 /*
1007 * only top level events have the pid namespace they were created in
1008 */
1009 if (event->parent)
1010 event = event->parent;
1011
1012 return task_tgid_nr_ns(p, event->ns);
1013}
1014
1015static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1016{
1017 /*
1018 * only top level events have the pid namespace they were created in
1019 */
1020 if (event->parent)
1021 event = event->parent;
1022
1023 return task_pid_nr_ns(p, event->ns);
1024}
1025
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026/*
1027 * If we inherit events we want to return the parent event id
1028 * to userspace.
1029 */
1030static u64 primary_event_id(struct perf_event *event)
1031{
1032 u64 id = event->id;
1033
1034 if (event->parent)
1035 id = event->parent->id;
1036
1037 return id;
1038}
1039
1040/*
1041 * Get the perf_event_context for a task and lock it.
1042 * This has to cope with with the fact that until it is locked,
1043 * the context could get moved to another task.
1044 */
1045static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001046perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001047{
1048 struct perf_event_context *ctx;
1049
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001050retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001051 /*
1052 * One of the few rules of preemptible RCU is that one cannot do
1053 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1054 * part of the read side critical section was preemptible -- see
1055 * rcu_read_unlock_special().
1056 *
1057 * Since ctx->lock nests under rq->lock we must ensure the entire read
1058 * side critical section is non-preemptible.
1059 */
1060 preempt_disable();
1061 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001062 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001063 if (ctx) {
1064 /*
1065 * If this context is a clone of another, it might
1066 * get swapped for another underneath us by
1067 * perf_event_task_sched_out, though the
1068 * rcu_read_lock() protects us from any context
1069 * getting freed. Lock the context and check if it
1070 * got swapped before we could get the lock, and retry
1071 * if so. If we locked the right context, then it
1072 * can't get swapped on us any more.
1073 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001074 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001075 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001076 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001077 rcu_read_unlock();
1078 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001079 goto retry;
1080 }
1081
1082 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001083 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001084 ctx = NULL;
1085 }
1086 }
1087 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001088 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089 return ctx;
1090}
1091
1092/*
1093 * Get the context for a task and increment its pin_count so it
1094 * can't get swapped to another task. This also increments its
1095 * reference count so that the context can't get freed.
1096 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001097static struct perf_event_context *
1098perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099{
1100 struct perf_event_context *ctx;
1101 unsigned long flags;
1102
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001103 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001104 if (ctx) {
1105 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001106 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001107 }
1108 return ctx;
1109}
1110
1111static void perf_unpin_context(struct perf_event_context *ctx)
1112{
1113 unsigned long flags;
1114
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001115 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001116 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001117 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118}
1119
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001120/*
1121 * Update the record of the current time in a context.
1122 */
1123static void update_context_time(struct perf_event_context *ctx)
1124{
1125 u64 now = perf_clock();
1126
1127 ctx->time += now - ctx->timestamp;
1128 ctx->timestamp = now;
1129}
1130
Stephane Eranian41587552011-01-03 18:20:01 +02001131static u64 perf_event_time(struct perf_event *event)
1132{
1133 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001134
1135 if (is_cgroup_event(event))
1136 return perf_cgroup_event_time(event);
1137
Stephane Eranian41587552011-01-03 18:20:01 +02001138 return ctx ? ctx->time : 0;
1139}
1140
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001141/*
1142 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001143 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001144 */
1145static void update_event_times(struct perf_event *event)
1146{
1147 struct perf_event_context *ctx = event->ctx;
1148 u64 run_end;
1149
1150 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1151 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1152 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001153 /*
1154 * in cgroup mode, time_enabled represents
1155 * the time the event was enabled AND active
1156 * tasks were in the monitored cgroup. This is
1157 * independent of the activity of the context as
1158 * there may be a mix of cgroup and non-cgroup events.
1159 *
1160 * That is why we treat cgroup events differently
1161 * here.
1162 */
1163 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001164 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001165 else if (ctx->is_active)
1166 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001167 else
1168 run_end = event->tstamp_stopped;
1169
1170 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001171
1172 if (event->state == PERF_EVENT_STATE_INACTIVE)
1173 run_end = event->tstamp_stopped;
1174 else
Stephane Eranian41587552011-01-03 18:20:01 +02001175 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001176
1177 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001178
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001179}
1180
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001181/*
1182 * Update total_time_enabled and total_time_running for all events in a group.
1183 */
1184static void update_group_times(struct perf_event *leader)
1185{
1186 struct perf_event *event;
1187
1188 update_event_times(leader);
1189 list_for_each_entry(event, &leader->sibling_list, group_entry)
1190 update_event_times(event);
1191}
1192
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001193static struct list_head *
1194ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1195{
1196 if (event->attr.pinned)
1197 return &ctx->pinned_groups;
1198 else
1199 return &ctx->flexible_groups;
1200}
1201
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001202/*
1203 * Add a event from the lists for its context.
1204 * Must be called with ctx->mutex and ctx->lock held.
1205 */
1206static void
1207list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1208{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001209 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1210 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001211
1212 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001213 * If we're a stand alone event or group leader, we go to the context
1214 * list, group events are kept attached to the group so that
1215 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001217 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001218 struct list_head *list;
1219
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001220 if (is_software_event(event))
1221 event->group_flags |= PERF_GROUP_SOFTWARE;
1222
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001223 list = ctx_group_list(event, ctx);
1224 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 }
1226
Peter Zijlstra08309372011-03-03 11:31:20 +01001227 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001228 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001229
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001230 list_add_rcu(&event->event_entry, &ctx->event_list);
1231 ctx->nr_events++;
1232 if (event->attr.inherit_stat)
1233 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001234
1235 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001236}
1237
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001238/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001239 * Initialize event state based on the perf_event_attr::disabled.
1240 */
1241static inline void perf_event__state_init(struct perf_event *event)
1242{
1243 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1244 PERF_EVENT_STATE_INACTIVE;
1245}
1246
1247/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001248 * Called at perf_event creation and when events are attached/detached from a
1249 * group.
1250 */
1251static void perf_event__read_size(struct perf_event *event)
1252{
1253 int entry = sizeof(u64); /* value */
1254 int size = 0;
1255 int nr = 1;
1256
1257 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1258 size += sizeof(u64);
1259
1260 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1261 size += sizeof(u64);
1262
1263 if (event->attr.read_format & PERF_FORMAT_ID)
1264 entry += sizeof(u64);
1265
1266 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1267 nr += event->group_leader->nr_siblings;
1268 size += sizeof(u64);
1269 }
1270
1271 size += entry * nr;
1272 event->read_size = size;
1273}
1274
1275static void perf_event__header_size(struct perf_event *event)
1276{
1277 struct perf_sample_data *data;
1278 u64 sample_type = event->attr.sample_type;
1279 u16 size = 0;
1280
1281 perf_event__read_size(event);
1282
1283 if (sample_type & PERF_SAMPLE_IP)
1284 size += sizeof(data->ip);
1285
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001286 if (sample_type & PERF_SAMPLE_ADDR)
1287 size += sizeof(data->addr);
1288
1289 if (sample_type & PERF_SAMPLE_PERIOD)
1290 size += sizeof(data->period);
1291
Andi Kleenc3feedf2013-01-24 16:10:28 +01001292 if (sample_type & PERF_SAMPLE_WEIGHT)
1293 size += sizeof(data->weight);
1294
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001295 if (sample_type & PERF_SAMPLE_READ)
1296 size += event->read_size;
1297
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001298 if (sample_type & PERF_SAMPLE_DATA_SRC)
1299 size += sizeof(data->data_src.val);
1300
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001301 if (sample_type & PERF_SAMPLE_TRANSACTION)
1302 size += sizeof(data->txn);
1303
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001304 event->header_size = size;
1305}
1306
1307static void perf_event__id_header_size(struct perf_event *event)
1308{
1309 struct perf_sample_data *data;
1310 u64 sample_type = event->attr.sample_type;
1311 u16 size = 0;
1312
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001313 if (sample_type & PERF_SAMPLE_TID)
1314 size += sizeof(data->tid_entry);
1315
1316 if (sample_type & PERF_SAMPLE_TIME)
1317 size += sizeof(data->time);
1318
Adrian Hunterff3d5272013-08-27 11:23:07 +03001319 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1320 size += sizeof(data->id);
1321
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001322 if (sample_type & PERF_SAMPLE_ID)
1323 size += sizeof(data->id);
1324
1325 if (sample_type & PERF_SAMPLE_STREAM_ID)
1326 size += sizeof(data->stream_id);
1327
1328 if (sample_type & PERF_SAMPLE_CPU)
1329 size += sizeof(data->cpu_entry);
1330
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001331 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001332}
1333
Peter Zijlstra8a495422010-05-27 15:47:49 +02001334static void perf_group_attach(struct perf_event *event)
1335{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001336 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001337
Peter Zijlstra74c33372010-10-15 11:40:29 +02001338 /*
1339 * We can have double attach due to group movement in perf_event_open.
1340 */
1341 if (event->attach_state & PERF_ATTACH_GROUP)
1342 return;
1343
Peter Zijlstra8a495422010-05-27 15:47:49 +02001344 event->attach_state |= PERF_ATTACH_GROUP;
1345
1346 if (group_leader == event)
1347 return;
1348
Peter Zijlstra652884f2015-01-23 11:20:10 +01001349 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1350
Peter Zijlstra8a495422010-05-27 15:47:49 +02001351 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1352 !is_software_event(event))
1353 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1354
1355 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1356 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001357
1358 perf_event__header_size(group_leader);
1359
1360 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1361 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001362}
1363
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001364/*
1365 * Remove a event from the lists for its context.
1366 * Must be called with ctx->mutex and ctx->lock held.
1367 */
1368static void
1369list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1370{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001371 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001372
1373 WARN_ON_ONCE(event->ctx != ctx);
1374 lockdep_assert_held(&ctx->lock);
1375
Peter Zijlstra8a495422010-05-27 15:47:49 +02001376 /*
1377 * We can have double detach due to exit/hot-unplug + close.
1378 */
1379 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001381
1382 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1383
Stephane Eranian68cacd22011-03-23 16:03:06 +01001384 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001385 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001386 cpuctx = __get_cpu_context(ctx);
1387 /*
1388 * if there are no more cgroup events
1389 * then cler cgrp to avoid stale pointer
1390 * in update_cgrp_time_from_cpuctx()
1391 */
1392 if (!ctx->nr_cgroups)
1393 cpuctx->cgrp = NULL;
1394 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396 ctx->nr_events--;
1397 if (event->attr.inherit_stat)
1398 ctx->nr_stat--;
1399
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001400 list_del_rcu(&event->event_entry);
1401
Peter Zijlstra8a495422010-05-27 15:47:49 +02001402 if (event->group_leader == event)
1403 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001404
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001405 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001406
1407 /*
1408 * If event was in error state, then keep it
1409 * that way, otherwise bogus counts will be
1410 * returned on read(). The only way to get out
1411 * of error state is by explicit re-enabling
1412 * of the event
1413 */
1414 if (event->state > PERF_EVENT_STATE_OFF)
1415 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001416
1417 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001418}
1419
Peter Zijlstra8a495422010-05-27 15:47:49 +02001420static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001421{
1422 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001423 struct list_head *list = NULL;
1424
1425 /*
1426 * We can have double detach due to exit/hot-unplug + close.
1427 */
1428 if (!(event->attach_state & PERF_ATTACH_GROUP))
1429 return;
1430
1431 event->attach_state &= ~PERF_ATTACH_GROUP;
1432
1433 /*
1434 * If this is a sibling, remove it from its group.
1435 */
1436 if (event->group_leader != event) {
1437 list_del_init(&event->group_entry);
1438 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001439 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001440 }
1441
1442 if (!list_empty(&event->group_entry))
1443 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001444
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445 /*
1446 * If this was a group event with sibling events then
1447 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001448 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001449 */
1450 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001451 if (list)
1452 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001454
1455 /* Inherit group flags from the previous leader */
1456 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001457
1458 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001460
1461out:
1462 perf_event__header_size(event->group_leader);
1463
1464 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1465 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001466}
1467
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001468/*
1469 * User event without the task.
1470 */
1471static bool is_orphaned_event(struct perf_event *event)
1472{
1473 return event && !is_kernel_event(event) && !event->owner;
1474}
1475
1476/*
1477 * Event has a parent but parent's task finished and it's
1478 * alive only because of children holding refference.
1479 */
1480static bool is_orphaned_child(struct perf_event *event)
1481{
1482 return is_orphaned_event(event->parent);
1483}
1484
1485static void orphans_remove_work(struct work_struct *work);
1486
1487static void schedule_orphans_remove(struct perf_event_context *ctx)
1488{
1489 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1490 return;
1491
1492 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1493 get_ctx(ctx);
1494 ctx->orphans_remove_sched = true;
1495 }
1496}
1497
1498static int __init perf_workqueue_init(void)
1499{
1500 perf_wq = create_singlethread_workqueue("perf");
1501 WARN(!perf_wq, "failed to create perf workqueue\n");
1502 return perf_wq ? 0 : -1;
1503}
1504
1505core_initcall(perf_workqueue_init);
1506
Stephane Eranianfa66f072010-08-26 16:40:01 +02001507static inline int
1508event_filter_match(struct perf_event *event)
1509{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001510 return (event->cpu == -1 || event->cpu == smp_processor_id())
1511 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001512}
1513
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001514static void
1515event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516 struct perf_cpu_context *cpuctx,
1517 struct perf_event_context *ctx)
1518{
Stephane Eranian41587552011-01-03 18:20:01 +02001519 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001520 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001521
1522 WARN_ON_ONCE(event->ctx != ctx);
1523 lockdep_assert_held(&ctx->lock);
1524
Stephane Eranianfa66f072010-08-26 16:40:01 +02001525 /*
1526 * An event which could not be activated because of
1527 * filter mismatch still needs to have its timings
1528 * maintained, otherwise bogus information is return
1529 * via read() for time_enabled, time_running:
1530 */
1531 if (event->state == PERF_EVENT_STATE_INACTIVE
1532 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001533 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001534 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001535 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001536 }
1537
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001539 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001540
Alexander Shishkin44377272013-12-16 14:17:36 +02001541 perf_pmu_disable(event->pmu);
1542
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001543 event->state = PERF_EVENT_STATE_INACTIVE;
1544 if (event->pending_disable) {
1545 event->pending_disable = 0;
1546 event->state = PERF_EVENT_STATE_OFF;
1547 }
Stephane Eranian41587552011-01-03 18:20:01 +02001548 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001549 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550 event->oncpu = -1;
1551
1552 if (!is_software_event(event))
1553 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001554 if (!--ctx->nr_active)
1555 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001556 if (event->attr.freq && event->attr.sample_freq)
1557 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001558 if (event->attr.exclusive || !cpuctx->active_oncpu)
1559 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001560
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001561 if (is_orphaned_child(event))
1562 schedule_orphans_remove(ctx);
1563
Alexander Shishkin44377272013-12-16 14:17:36 +02001564 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001565}
1566
1567static void
1568group_sched_out(struct perf_event *group_event,
1569 struct perf_cpu_context *cpuctx,
1570 struct perf_event_context *ctx)
1571{
1572 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001573 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574
1575 event_sched_out(group_event, cpuctx, ctx);
1576
1577 /*
1578 * Schedule out siblings (if any):
1579 */
1580 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1581 event_sched_out(event, cpuctx, ctx);
1582
Stephane Eranianfa66f072010-08-26 16:40:01 +02001583 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 cpuctx->exclusive = 0;
1585}
1586
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001587struct remove_event {
1588 struct perf_event *event;
1589 bool detach_group;
1590};
1591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001592/*
1593 * Cross CPU call to remove a performance event
1594 *
1595 * We disable the event on the hardware level first. After that we
1596 * remove it from the context list.
1597 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001598static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001600 struct remove_event *re = info;
1601 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001602 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001603 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001605 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001606 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001607 if (re->detach_group)
1608 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001610 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1611 ctx->is_active = 0;
1612 cpuctx->task_ctx = NULL;
1613 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001614 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001615
1616 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617}
1618
1619
1620/*
1621 * Remove the event from a task's (or a CPU's) list of events.
1622 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623 * CPU events are removed with a smp call. For task events we only
1624 * call when the task is on a CPU.
1625 *
1626 * If event->ctx is a cloned context, callers must make sure that
1627 * every task struct that event->ctx->task could possibly point to
1628 * remains valid. This is OK when called from perf_release since
1629 * that only calls us on the top-level context, which can't be a clone.
1630 * When called from perf_event_exit_task, it's OK because the
1631 * context has been detached from its task.
1632 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001633static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001634{
1635 struct perf_event_context *ctx = event->ctx;
1636 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001637 struct remove_event re = {
1638 .event = event,
1639 .detach_group = detach_group,
1640 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001641
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001642 lockdep_assert_held(&ctx->mutex);
1643
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001644 if (!task) {
1645 /*
Mark Rutland226424e2014-11-05 16:11:44 +00001646 * Per cpu events are removed via an smp call. The removal can
1647 * fail if the CPU is currently offline, but in that case we
1648 * already called __perf_remove_from_context from
1649 * perf_event_exit_cpu.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001651 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652 return;
1653 }
1654
1655retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001656 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001657 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001659 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001661 * If we failed to find a running task, but find the context active now
1662 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001664 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001665 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001666 /*
1667 * Reload the task pointer, it might have been changed by
1668 * a concurrent perf_event_context_sched_out().
1669 */
1670 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671 goto retry;
1672 }
1673
1674 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001675 * Since the task isn't running, its safe to remove the event, us
1676 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001677 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001678 if (detach_group)
1679 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001680 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001681 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682}
1683
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001685 * Cross CPU call to disable a performance event
1686 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301687int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688{
1689 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001691 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692
1693 /*
1694 * If this is a per-task event, need to check whether this
1695 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001696 *
1697 * Can trigger due to concurrent perf_event_context_sched_out()
1698 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 */
1700 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001701 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001702
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001703 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704
1705 /*
1706 * If the event is on, turn it off.
1707 * If it is in error state, leave it in error state.
1708 */
1709 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1710 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001711 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712 update_group_times(event);
1713 if (event == event->group_leader)
1714 group_sched_out(event, cpuctx, ctx);
1715 else
1716 event_sched_out(event, cpuctx, ctx);
1717 event->state = PERF_EVENT_STATE_OFF;
1718 }
1719
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001720 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001721
1722 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723}
1724
1725/*
1726 * Disable a event.
1727 *
1728 * If event->ctx is a cloned context, callers must make sure that
1729 * every task struct that event->ctx->task could possibly point to
1730 * remains valid. This condition is satisifed when called through
1731 * perf_event_for_each_child or perf_event_for_each because they
1732 * hold the top-level event's child_mutex, so any descendant that
1733 * goes to exit will block in sync_child_event.
1734 * When called from perf_pending_event it's OK because event->ctx
1735 * is the current context on this CPU and preemption is disabled,
1736 * hence we can't get into perf_event_task_sched_out for this context.
1737 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001738static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739{
1740 struct perf_event_context *ctx = event->ctx;
1741 struct task_struct *task = ctx->task;
1742
1743 if (!task) {
1744 /*
1745 * Disable the event on the cpu that it's on
1746 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001747 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748 return;
1749 }
1750
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001751retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001752 if (!task_function_call(task, __perf_event_disable, event))
1753 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001754
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001755 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001756 /*
1757 * If the event is still active, we need to retry the cross-call.
1758 */
1759 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001760 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001761 /*
1762 * Reload the task pointer, it might have been changed by
1763 * a concurrent perf_event_context_sched_out().
1764 */
1765 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766 goto retry;
1767 }
1768
1769 /*
1770 * Since we have the lock this context can't be scheduled
1771 * in, so we can change the state safely.
1772 */
1773 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1774 update_group_times(event);
1775 event->state = PERF_EVENT_STATE_OFF;
1776 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001777 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001778}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001779
1780/*
1781 * Strictly speaking kernel users cannot create groups and therefore this
1782 * interface does not need the perf_event_ctx_lock() magic.
1783 */
1784void perf_event_disable(struct perf_event *event)
1785{
1786 struct perf_event_context *ctx;
1787
1788 ctx = perf_event_ctx_lock(event);
1789 _perf_event_disable(event);
1790 perf_event_ctx_unlock(event, ctx);
1791}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001792EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793
Stephane Eraniane5d13672011-02-14 11:20:01 +02001794static void perf_set_shadow_time(struct perf_event *event,
1795 struct perf_event_context *ctx,
1796 u64 tstamp)
1797{
1798 /*
1799 * use the correct time source for the time snapshot
1800 *
1801 * We could get by without this by leveraging the
1802 * fact that to get to this function, the caller
1803 * has most likely already called update_context_time()
1804 * and update_cgrp_time_xx() and thus both timestamp
1805 * are identical (or very close). Given that tstamp is,
1806 * already adjusted for cgroup, we could say that:
1807 * tstamp - ctx->timestamp
1808 * is equivalent to
1809 * tstamp - cgrp->timestamp.
1810 *
1811 * Then, in perf_output_read(), the calculation would
1812 * work with no changes because:
1813 * - event is guaranteed scheduled in
1814 * - no scheduled out in between
1815 * - thus the timestamp would be the same
1816 *
1817 * But this is a bit hairy.
1818 *
1819 * So instead, we have an explicit cgroup call to remain
1820 * within the time time source all along. We believe it
1821 * is cleaner and simpler to understand.
1822 */
1823 if (is_cgroup_event(event))
1824 perf_cgroup_set_shadow_time(event, tstamp);
1825 else
1826 event->shadow_ctx_time = tstamp - ctx->timestamp;
1827}
1828
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001829#define MAX_INTERRUPTS (~0ULL)
1830
1831static void perf_log_throttle(struct perf_event *event, int enable);
1832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001834event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001835 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001836 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837{
Stephane Eranian41587552011-01-03 18:20:01 +02001838 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001839 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001840
Peter Zijlstra63342412014-05-05 11:49:16 +02001841 lockdep_assert_held(&ctx->lock);
1842
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001843 if (event->state <= PERF_EVENT_STATE_OFF)
1844 return 0;
1845
1846 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001847 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001848
1849 /*
1850 * Unthrottle events, since we scheduled we might have missed several
1851 * ticks already, also for a heavily scheduling task there is little
1852 * guarantee it'll get a tick in a timely manner.
1853 */
1854 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1855 perf_log_throttle(event, 1);
1856 event->hw.interrupts = 0;
1857 }
1858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859 /*
1860 * The new state must be visible before we turn it on in the hardware:
1861 */
1862 smp_wmb();
1863
Alexander Shishkin44377272013-12-16 14:17:36 +02001864 perf_pmu_disable(event->pmu);
1865
Shaohua Li72f669c2015-02-05 15:55:31 -08001866 event->tstamp_running += tstamp - event->tstamp_stopped;
1867
1868 perf_set_shadow_time(event, ctx, tstamp);
1869
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001870 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871 event->state = PERF_EVENT_STATE_INACTIVE;
1872 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001873 ret = -EAGAIN;
1874 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001875 }
1876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877 if (!is_software_event(event))
1878 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001879 if (!ctx->nr_active++)
1880 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001881 if (event->attr.freq && event->attr.sample_freq)
1882 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001883
1884 if (event->attr.exclusive)
1885 cpuctx->exclusive = 1;
1886
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001887 if (is_orphaned_child(event))
1888 schedule_orphans_remove(ctx);
1889
Alexander Shishkin44377272013-12-16 14:17:36 +02001890out:
1891 perf_pmu_enable(event->pmu);
1892
1893 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894}
1895
1896static int
1897group_sched_in(struct perf_event *group_event,
1898 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001899 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900{
Lin Ming6bde9b62010-04-23 13:56:00 +08001901 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001902 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001903 u64 now = ctx->time;
1904 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001905
1906 if (group_event->state == PERF_EVENT_STATE_OFF)
1907 return 0;
1908
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001909 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001910
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001911 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001912 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001913 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001914 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001915 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916
1917 /*
1918 * Schedule in siblings as one group (if any):
1919 */
1920 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001921 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001922 partial_group = event;
1923 goto group_error;
1924 }
1925 }
1926
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001927 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001928 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930group_error:
1931 /*
1932 * Groups can be scheduled in as one unit only, so undo any
1933 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001934 * The events up to the failed event are scheduled out normally,
1935 * tstamp_stopped will be updated.
1936 *
1937 * The failed events and the remaining siblings need to have
1938 * their timings updated as if they had gone thru event_sched_in()
1939 * and event_sched_out(). This is required to get consistent timings
1940 * across the group. This also takes care of the case where the group
1941 * could never be scheduled by ensuring tstamp_stopped is set to mark
1942 * the time the event was actually stopped, such that time delta
1943 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001944 */
1945 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1946 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001947 simulate = true;
1948
1949 if (simulate) {
1950 event->tstamp_running += now - event->tstamp_stopped;
1951 event->tstamp_stopped = now;
1952 } else {
1953 event_sched_out(event, cpuctx, ctx);
1954 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001955 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001956 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001958 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001959
Stephane Eranian9e630202013-04-03 14:21:33 +02001960 perf_cpu_hrtimer_restart(cpuctx);
1961
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 return -EAGAIN;
1963}
1964
1965/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966 * Work out whether we can put this event group on the CPU now.
1967 */
1968static int group_can_go_on(struct perf_event *event,
1969 struct perf_cpu_context *cpuctx,
1970 int can_add_hw)
1971{
1972 /*
1973 * Groups consisting entirely of software events can always go on.
1974 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001975 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976 return 1;
1977 /*
1978 * If an exclusive group is already on, no other hardware
1979 * events can go on.
1980 */
1981 if (cpuctx->exclusive)
1982 return 0;
1983 /*
1984 * If this group is exclusive and there are already
1985 * events on the CPU, it can't go on.
1986 */
1987 if (event->attr.exclusive && cpuctx->active_oncpu)
1988 return 0;
1989 /*
1990 * Otherwise, try to add it if all previous groups were able
1991 * to go on.
1992 */
1993 return can_add_hw;
1994}
1995
1996static void add_event_to_ctx(struct perf_event *event,
1997 struct perf_event_context *ctx)
1998{
Stephane Eranian41587552011-01-03 18:20:01 +02001999 u64 tstamp = perf_event_time(event);
2000
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002002 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002003 event->tstamp_enabled = tstamp;
2004 event->tstamp_running = tstamp;
2005 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006}
2007
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002008static void task_ctx_sched_out(struct perf_event_context *ctx);
2009static void
2010ctx_sched_in(struct perf_event_context *ctx,
2011 struct perf_cpu_context *cpuctx,
2012 enum event_type_t event_type,
2013 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002014
Peter Zijlstradce58552011-04-09 21:17:46 +02002015static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2016 struct perf_event_context *ctx,
2017 struct task_struct *task)
2018{
2019 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2020 if (ctx)
2021 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2022 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2023 if (ctx)
2024 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2025}
2026
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002027/*
2028 * Cross CPU call to install and enable a performance event
2029 *
2030 * Must be called with ctx->mutex held
2031 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002032static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002033{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034 struct perf_event *event = info;
2035 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002036 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002037 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2038 struct task_struct *task = current;
2039
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002040 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002041 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042
2043 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002044 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002046 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002047 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002048
2049 /*
2050 * If the context we're installing events in is not the
2051 * active task_ctx, flip them.
2052 */
2053 if (ctx->task && task_ctx != ctx) {
2054 if (task_ctx)
2055 raw_spin_unlock(&task_ctx->lock);
2056 raw_spin_lock(&ctx->lock);
2057 task_ctx = ctx;
2058 }
2059
2060 if (task_ctx) {
2061 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002062 task = task_ctx->task;
2063 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002064
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002065 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002067 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002068 /*
2069 * update cgrp time only if current cgrp
2070 * matches event->cgrp. Must be done before
2071 * calling add_event_to_ctx()
2072 */
2073 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075 add_event_to_ctx(event, ctx);
2076
2077 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002078 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002079 */
Peter Zijlstradce58552011-04-09 21:17:46 +02002080 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002081
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002082 perf_pmu_enable(cpuctx->ctx.pmu);
2083 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002084
2085 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002086}
2087
2088/*
2089 * Attach a performance event to a context
2090 *
2091 * First we add the event to the list with the hardware enable bit
2092 * in event->hw_config cleared.
2093 *
2094 * If the event is attached to a task which is on a CPU we use a smp
2095 * call to enable it in the task context. The task might have been
2096 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097 */
2098static void
2099perf_install_in_context(struct perf_event_context *ctx,
2100 struct perf_event *event,
2101 int cpu)
2102{
2103 struct task_struct *task = ctx->task;
2104
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002105 lockdep_assert_held(&ctx->mutex);
2106
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002107 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002108 if (event->cpu != -1)
2109 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002110
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111 if (!task) {
2112 /*
2113 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002114 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002115 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002116 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117 return;
2118 }
2119
2120retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002121 if (!task_function_call(task, __perf_install_in_context, event))
2122 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002124 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002126 * If we failed to find a running task, but find the context active now
2127 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002128 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002129 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002130 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07002131 /*
2132 * Reload the task pointer, it might have been changed by
2133 * a concurrent perf_event_context_sched_out().
2134 */
2135 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 goto retry;
2137 }
2138
2139 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002140 * Since the task isn't running, its safe to add the event, us holding
2141 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002143 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002144 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145}
2146
2147/*
2148 * Put a event into inactive state and update time fields.
2149 * Enabling the leader of a group effectively enables all
2150 * the group members that aren't explicitly disabled, so we
2151 * have to update their ->tstamp_enabled also.
2152 * Note: this works for group members as well as group leaders
2153 * since the non-leader members' sibling_lists will be empty.
2154 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002155static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002156{
2157 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002158 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002159
2160 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002161 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002162 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002163 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2164 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002165 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166}
2167
2168/*
2169 * Cross CPU call to enable a performance event
2170 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002171static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172{
2173 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174 struct perf_event_context *ctx = event->ctx;
2175 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002176 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177 int err;
2178
Jiri Olsa06f41792013-07-09 17:44:11 +02002179 /*
2180 * There's a time window between 'ctx->is_active' check
2181 * in perf_event_enable function and this place having:
2182 * - IRQs on
2183 * - ctx->lock unlocked
2184 *
2185 * where the task could be killed and 'ctx' deactivated
2186 * by perf_event_exit_task.
2187 */
2188 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002189 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002191 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002192 update_context_time(ctx);
2193
2194 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2195 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002196
2197 /*
2198 * set current task's cgroup time reference point
2199 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002200 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002201
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002202 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203
Stephane Eraniane5d13672011-02-14 11:20:01 +02002204 if (!event_filter_match(event)) {
2205 if (is_cgroup_event(event))
2206 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002207 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002208 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002210 /*
2211 * If the event is in a group and isn't the group leader,
2212 * then don't put it on unless the group is on.
2213 */
2214 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2215 goto unlock;
2216
2217 if (!group_can_go_on(event, cpuctx, 1)) {
2218 err = -EEXIST;
2219 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002220 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002221 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002222 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002223 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002224 }
2225
2226 if (err) {
2227 /*
2228 * If this event can't go on and it's part of a
2229 * group, then the whole group has to come off.
2230 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002231 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002233 perf_cpu_hrtimer_restart(cpuctx);
2234 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002235 if (leader->attr.pinned) {
2236 update_group_times(leader);
2237 leader->state = PERF_EVENT_STATE_ERROR;
2238 }
2239 }
2240
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002241unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002242 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002243
2244 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245}
2246
2247/*
2248 * Enable a event.
2249 *
2250 * If event->ctx is a cloned context, callers must make sure that
2251 * every task struct that event->ctx->task could possibly point to
2252 * remains valid. This condition is satisfied when called through
2253 * perf_event_for_each_child or perf_event_for_each as described
2254 * for perf_event_disable.
2255 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002256static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002257{
2258 struct perf_event_context *ctx = event->ctx;
2259 struct task_struct *task = ctx->task;
2260
2261 if (!task) {
2262 /*
2263 * Enable the event on the cpu that it's on
2264 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002265 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266 return;
2267 }
2268
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002269 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2271 goto out;
2272
2273 /*
2274 * If the event is in error state, clear that first.
2275 * That way, if we see the event in error state below, we
2276 * know that it has gone back into error state, as distinct
2277 * from the task having been scheduled away before the
2278 * cross-call arrived.
2279 */
2280 if (event->state == PERF_EVENT_STATE_ERROR)
2281 event->state = PERF_EVENT_STATE_OFF;
2282
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002283retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002284 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002285 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002286 goto out;
2287 }
2288
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002289 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002290
2291 if (!task_function_call(task, __perf_event_enable, event))
2292 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002294 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002295
2296 /*
2297 * If the context is active and the event is still off,
2298 * we need to retry the cross-call.
2299 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002300 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2301 /*
2302 * task could have been flipped by a concurrent
2303 * perf_event_context_sched_out()
2304 */
2305 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002307 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002309out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002310 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002312
2313/*
2314 * See perf_event_disable();
2315 */
2316void perf_event_enable(struct perf_event *event)
2317{
2318 struct perf_event_context *ctx;
2319
2320 ctx = perf_event_ctx_lock(event);
2321 _perf_event_enable(event);
2322 perf_event_ctx_unlock(event, ctx);
2323}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002324EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002326static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002327{
2328 /*
2329 * not supported on inherited events
2330 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002331 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002332 return -EINVAL;
2333
2334 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002335 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336
2337 return 0;
2338}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002339
2340/*
2341 * See perf_event_disable()
2342 */
2343int perf_event_refresh(struct perf_event *event, int refresh)
2344{
2345 struct perf_event_context *ctx;
2346 int ret;
2347
2348 ctx = perf_event_ctx_lock(event);
2349 ret = _perf_event_refresh(event, refresh);
2350 perf_event_ctx_unlock(event, ctx);
2351
2352 return ret;
2353}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002354EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002356static void ctx_sched_out(struct perf_event_context *ctx,
2357 struct perf_cpu_context *cpuctx,
2358 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002359{
2360 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002361 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002362
Peter Zijlstradb24d332011-04-09 21:17:45 +02002363 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002365 return;
2366
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002368 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002369 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002370 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002371
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002372 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002373 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002374 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2375 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002376 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002377
Peter Zijlstradb24d332011-04-09 21:17:45 +02002378 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002379 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002380 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002381 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002382 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383}
2384
2385/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002386 * Test whether two contexts are equivalent, i.e. whether they have both been
2387 * cloned from the same version of the same context.
2388 *
2389 * Equivalence is measured using a generation number in the context that is
2390 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2391 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392 */
2393static int context_equiv(struct perf_event_context *ctx1,
2394 struct perf_event_context *ctx2)
2395{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002396 lockdep_assert_held(&ctx1->lock);
2397 lockdep_assert_held(&ctx2->lock);
2398
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002399 /* Pinning disables the swap optimization */
2400 if (ctx1->pin_count || ctx2->pin_count)
2401 return 0;
2402
2403 /* If ctx1 is the parent of ctx2 */
2404 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2405 return 1;
2406
2407 /* If ctx2 is the parent of ctx1 */
2408 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2409 return 1;
2410
2411 /*
2412 * If ctx1 and ctx2 have the same parent; we flatten the parent
2413 * hierarchy, see perf_event_init_context().
2414 */
2415 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2416 ctx1->parent_gen == ctx2->parent_gen)
2417 return 1;
2418
2419 /* Unmatched */
2420 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421}
2422
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002423static void __perf_event_sync_stat(struct perf_event *event,
2424 struct perf_event *next_event)
2425{
2426 u64 value;
2427
2428 if (!event->attr.inherit_stat)
2429 return;
2430
2431 /*
2432 * Update the event value, we cannot use perf_event_read()
2433 * because we're in the middle of a context switch and have IRQs
2434 * disabled, which upsets smp_call_function_single(), however
2435 * we know the event must be on the current CPU, therefore we
2436 * don't need to use it.
2437 */
2438 switch (event->state) {
2439 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002440 event->pmu->read(event);
2441 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002442
2443 case PERF_EVENT_STATE_INACTIVE:
2444 update_event_times(event);
2445 break;
2446
2447 default:
2448 break;
2449 }
2450
2451 /*
2452 * In order to keep per-task stats reliable we need to flip the event
2453 * values when we flip the contexts.
2454 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002455 value = local64_read(&next_event->count);
2456 value = local64_xchg(&event->count, value);
2457 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002458
2459 swap(event->total_time_enabled, next_event->total_time_enabled);
2460 swap(event->total_time_running, next_event->total_time_running);
2461
2462 /*
2463 * Since we swizzled the values, update the user visible data too.
2464 */
2465 perf_event_update_userpage(event);
2466 perf_event_update_userpage(next_event);
2467}
2468
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002469static void perf_event_sync_stat(struct perf_event_context *ctx,
2470 struct perf_event_context *next_ctx)
2471{
2472 struct perf_event *event, *next_event;
2473
2474 if (!ctx->nr_stat)
2475 return;
2476
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002477 update_context_time(ctx);
2478
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479 event = list_first_entry(&ctx->event_list,
2480 struct perf_event, event_entry);
2481
2482 next_event = list_first_entry(&next_ctx->event_list,
2483 struct perf_event, event_entry);
2484
2485 while (&event->event_entry != &ctx->event_list &&
2486 &next_event->event_entry != &next_ctx->event_list) {
2487
2488 __perf_event_sync_stat(event, next_event);
2489
2490 event = list_next_entry(event, event_entry);
2491 next_event = list_next_entry(next_event, event_entry);
2492 }
2493}
2494
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002495static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2496 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002497{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002498 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002499 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002500 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002501 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002502 int do_switch = 1;
2503
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002504 if (likely(!ctx))
2505 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002506
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002507 cpuctx = __get_cpu_context(ctx);
2508 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002509 return;
2510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002511 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002512 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002513 if (!next_ctx)
2514 goto unlock;
2515
2516 parent = rcu_dereference(ctx->parent_ctx);
2517 next_parent = rcu_dereference(next_ctx->parent_ctx);
2518
2519 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002520 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002521 goto unlock;
2522
2523 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002524 /*
2525 * Looks like the two contexts are clones, so we might be
2526 * able to optimize the context switch. We lock both
2527 * contexts and check that they are clones under the
2528 * lock (including re-checking that neither has been
2529 * uncloned in the meantime). It doesn't matter which
2530 * order we take the locks because no other cpu could
2531 * be trying to lock both of these tasks.
2532 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002533 raw_spin_lock(&ctx->lock);
2534 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002535 if (context_equiv(ctx, next_ctx)) {
2536 /*
2537 * XXX do we need a memory barrier of sorts
2538 * wrt to rcu_dereference() of perf_event_ctxp
2539 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002540 task->perf_event_ctxp[ctxn] = next_ctx;
2541 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002542 ctx->task = next;
2543 next_ctx->task = task;
Yan, Zheng5a158c32014-11-04 21:56:02 -05002544
2545 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002547 do_switch = 0;
2548
2549 perf_event_sync_stat(ctx, next_ctx);
2550 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002551 raw_spin_unlock(&next_ctx->lock);
2552 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002553 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002554unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002555 rcu_read_unlock();
2556
2557 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002558 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002559 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002560 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002561 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002562 }
2563}
2564
Yan, Zhengba532502014-11-04 21:55:58 -05002565void perf_sched_cb_dec(struct pmu *pmu)
2566{
2567 this_cpu_dec(perf_sched_cb_usages);
2568}
2569
2570void perf_sched_cb_inc(struct pmu *pmu)
2571{
2572 this_cpu_inc(perf_sched_cb_usages);
2573}
2574
2575/*
2576 * This function provides the context switch callback to the lower code
2577 * layer. It is invoked ONLY when the context switch callback is enabled.
2578 */
2579static void perf_pmu_sched_task(struct task_struct *prev,
2580 struct task_struct *next,
2581 bool sched_in)
2582{
2583 struct perf_cpu_context *cpuctx;
2584 struct pmu *pmu;
2585 unsigned long flags;
2586
2587 if (prev == next)
2588 return;
2589
2590 local_irq_save(flags);
2591
2592 rcu_read_lock();
2593
2594 list_for_each_entry_rcu(pmu, &pmus, entry) {
2595 if (pmu->sched_task) {
2596 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2597
2598 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2599
2600 perf_pmu_disable(pmu);
2601
2602 pmu->sched_task(cpuctx->task_ctx, sched_in);
2603
2604 perf_pmu_enable(pmu);
2605
2606 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2607 }
2608 }
2609
2610 rcu_read_unlock();
2611
2612 local_irq_restore(flags);
2613}
2614
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002615#define for_each_task_context_nr(ctxn) \
2616 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2617
2618/*
2619 * Called from scheduler to remove the events of the current task,
2620 * with interrupts disabled.
2621 *
2622 * We stop each event and update the event value in event->count.
2623 *
2624 * This does not protect us against NMI, but disable()
2625 * sets the disabled bit in the control field of event _before_
2626 * accessing the event control register. If a NMI hits, then it will
2627 * not restart the event.
2628 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002629void __perf_event_task_sched_out(struct task_struct *task,
2630 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002631{
2632 int ctxn;
2633
Yan, Zhengba532502014-11-04 21:55:58 -05002634 if (__this_cpu_read(perf_sched_cb_usages))
2635 perf_pmu_sched_task(task, next, false);
2636
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002637 for_each_task_context_nr(ctxn)
2638 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002639
2640 /*
2641 * if cgroup events exist on this CPU, then we need
2642 * to check if we have to switch out PMU state.
2643 * cgroup event are system-wide mode only
2644 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002645 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002646 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002647}
2648
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002649static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002650{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002651 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002652
2653 if (!cpuctx->task_ctx)
2654 return;
2655
2656 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2657 return;
2658
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002659 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002660 cpuctx->task_ctx = NULL;
2661}
2662
2663/*
2664 * Called with IRQs disabled
2665 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002666static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2667 enum event_type_t event_type)
2668{
2669 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670}
2671
2672static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002673ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002674 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002675{
2676 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002677
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002678 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2679 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002680 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002681 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682 continue;
2683
Stephane Eraniane5d13672011-02-14 11:20:01 +02002684 /* may need to reset tstamp_enabled */
2685 if (is_cgroup_event(event))
2686 perf_cgroup_mark_enabled(event, ctx);
2687
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002688 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002689 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002690
2691 /*
2692 * If this pinned group hasn't been scheduled,
2693 * put it in error state.
2694 */
2695 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2696 update_group_times(event);
2697 event->state = PERF_EVENT_STATE_ERROR;
2698 }
2699 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002700}
2701
2702static void
2703ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002704 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002705{
2706 struct perf_event *event;
2707 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002708
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002709 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2710 /* Ignore events in OFF or ERROR state */
2711 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002712 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713 /*
2714 * Listen to the 'cpu' scheduling filter constraint
2715 * of events:
2716 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002717 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002718 continue;
2719
Stephane Eraniane5d13672011-02-14 11:20:01 +02002720 /* may need to reset tstamp_enabled */
2721 if (is_cgroup_event(event))
2722 perf_cgroup_mark_enabled(event, ctx);
2723
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002724 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002725 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002726 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002727 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002729}
2730
2731static void
2732ctx_sched_in(struct perf_event_context *ctx,
2733 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002734 enum event_type_t event_type,
2735 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002736{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002737 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002738 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002739
Peter Zijlstradb24d332011-04-09 21:17:45 +02002740 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002741 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002742 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002743
Stephane Eraniane5d13672011-02-14 11:20:01 +02002744 now = perf_clock();
2745 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002746 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002747 /*
2748 * First go through the list and put on any pinned groups
2749 * in order to give them the best chance of going on.
2750 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002751 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002752 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002753
2754 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002755 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002756 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002757}
2758
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002759static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002760 enum event_type_t event_type,
2761 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002762{
2763 struct perf_event_context *ctx = &cpuctx->ctx;
2764
Stephane Eraniane5d13672011-02-14 11:20:01 +02002765 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002766}
2767
Stephane Eraniane5d13672011-02-14 11:20:01 +02002768static void perf_event_context_sched_in(struct perf_event_context *ctx,
2769 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002771 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002772
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002773 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002774 if (cpuctx->task_ctx == ctx)
2775 return;
2776
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002777 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002778 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002779 /*
2780 * We want to keep the following priority order:
2781 * cpu pinned (that don't need to move), task pinned,
2782 * cpu flexible, task flexible.
2783 */
2784 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2785
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002786 if (ctx->nr_events)
2787 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002788
Gleb Natapov86b47c22011-11-22 16:08:21 +02002789 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2790
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002791 perf_pmu_enable(ctx->pmu);
2792 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002793}
2794
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002795/*
2796 * Called from scheduler to add the events of the current task
2797 * with interrupts disabled.
2798 *
2799 * We restore the event value and then enable it.
2800 *
2801 * This does not protect us against NMI, but enable()
2802 * sets the enabled bit in the control field of event _before_
2803 * accessing the event control register. If a NMI hits, then it will
2804 * keep the event running.
2805 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002806void __perf_event_task_sched_in(struct task_struct *prev,
2807 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002808{
2809 struct perf_event_context *ctx;
2810 int ctxn;
2811
2812 for_each_task_context_nr(ctxn) {
2813 ctx = task->perf_event_ctxp[ctxn];
2814 if (likely(!ctx))
2815 continue;
2816
Stephane Eraniane5d13672011-02-14 11:20:01 +02002817 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002818 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002819 /*
2820 * if cgroup events exist on this CPU, then we need
2821 * to check if we have to switch in PMU state.
2822 * cgroup event are system-wide mode only
2823 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002824 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002825 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002826
Yan, Zhengba532502014-11-04 21:55:58 -05002827 if (__this_cpu_read(perf_sched_cb_usages))
2828 perf_pmu_sched_task(prev, task, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829}
2830
Peter Zijlstraabd50712010-01-26 18:50:16 +01002831static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2832{
2833 u64 frequency = event->attr.sample_freq;
2834 u64 sec = NSEC_PER_SEC;
2835 u64 divisor, dividend;
2836
2837 int count_fls, nsec_fls, frequency_fls, sec_fls;
2838
2839 count_fls = fls64(count);
2840 nsec_fls = fls64(nsec);
2841 frequency_fls = fls64(frequency);
2842 sec_fls = 30;
2843
2844 /*
2845 * We got @count in @nsec, with a target of sample_freq HZ
2846 * the target period becomes:
2847 *
2848 * @count * 10^9
2849 * period = -------------------
2850 * @nsec * sample_freq
2851 *
2852 */
2853
2854 /*
2855 * Reduce accuracy by one bit such that @a and @b converge
2856 * to a similar magnitude.
2857 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002858#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002859do { \
2860 if (a##_fls > b##_fls) { \
2861 a >>= 1; \
2862 a##_fls--; \
2863 } else { \
2864 b >>= 1; \
2865 b##_fls--; \
2866 } \
2867} while (0)
2868
2869 /*
2870 * Reduce accuracy until either term fits in a u64, then proceed with
2871 * the other, so that finally we can do a u64/u64 division.
2872 */
2873 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2874 REDUCE_FLS(nsec, frequency);
2875 REDUCE_FLS(sec, count);
2876 }
2877
2878 if (count_fls + sec_fls > 64) {
2879 divisor = nsec * frequency;
2880
2881 while (count_fls + sec_fls > 64) {
2882 REDUCE_FLS(count, sec);
2883 divisor >>= 1;
2884 }
2885
2886 dividend = count * sec;
2887 } else {
2888 dividend = count * sec;
2889
2890 while (nsec_fls + frequency_fls > 64) {
2891 REDUCE_FLS(nsec, frequency);
2892 dividend >>= 1;
2893 }
2894
2895 divisor = nsec * frequency;
2896 }
2897
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002898 if (!divisor)
2899 return dividend;
2900
Peter Zijlstraabd50712010-01-26 18:50:16 +01002901 return div64_u64(dividend, divisor);
2902}
2903
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002904static DEFINE_PER_CPU(int, perf_throttled_count);
2905static DEFINE_PER_CPU(u64, perf_throttled_seq);
2906
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002907static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002908{
2909 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002910 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002911 s64 delta;
2912
Peter Zijlstraabd50712010-01-26 18:50:16 +01002913 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914
2915 delta = (s64)(period - hwc->sample_period);
2916 delta = (delta + 7) / 8; /* low pass filter */
2917
2918 sample_period = hwc->sample_period + delta;
2919
2920 if (!sample_period)
2921 sample_period = 1;
2922
2923 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002924
Peter Zijlstrae7850592010-05-21 14:43:08 +02002925 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002926 if (disable)
2927 event->pmu->stop(event, PERF_EF_UPDATE);
2928
Peter Zijlstrae7850592010-05-21 14:43:08 +02002929 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002930
2931 if (disable)
2932 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002933 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002934}
2935
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002936/*
2937 * combine freq adjustment with unthrottling to avoid two passes over the
2938 * events. At the same time, make sure, having freq events does not change
2939 * the rate of unthrottling as that would introduce bias.
2940 */
2941static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2942 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002943{
2944 struct perf_event *event;
2945 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002946 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002947 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002949 /*
2950 * only need to iterate over all events iff:
2951 * - context have events in frequency mode (needs freq adjust)
2952 * - there are events to unthrottle on this cpu
2953 */
2954 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002955 return;
2956
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002957 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002958 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002959
Paul Mackerras03541f82009-10-14 16:58:03 +11002960 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002961 if (event->state != PERF_EVENT_STATE_ACTIVE)
2962 continue;
2963
Stephane Eranian5632ab12011-01-03 18:20:01 +02002964 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002965 continue;
2966
Alexander Shishkin44377272013-12-16 14:17:36 +02002967 perf_pmu_disable(event->pmu);
2968
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969 hwc = &event->hw;
2970
Jiri Olsaae23bff2013-08-24 16:45:54 +02002971 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002972 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002973 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002974 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002975 }
2976
2977 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002978 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002979
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002980 /*
2981 * stop the event and update event->count
2982 */
2983 event->pmu->stop(event, PERF_EF_UPDATE);
2984
Peter Zijlstrae7850592010-05-21 14:43:08 +02002985 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002986 delta = now - hwc->freq_count_stamp;
2987 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002988
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002989 /*
2990 * restart the event
2991 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002992 * we have stopped the event so tell that
2993 * to perf_adjust_period() to avoid stopping it
2994 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002995 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002996 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002997 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002998
2999 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003000 next:
3001 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003003
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003004 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003005 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006}
3007
3008/*
3009 * Round-robin a context's events:
3010 */
3011static void rotate_ctx(struct perf_event_context *ctx)
3012{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003013 /*
3014 * Rotate the first entry last of non-pinned groups. Rotation might be
3015 * disabled by the inheritance code.
3016 */
3017 if (!ctx->rotate_disable)
3018 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019}
3020
Stephane Eranian9e630202013-04-03 14:21:33 +02003021static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003023 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003024 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003025
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003026 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003027 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3028 rotate = 1;
3029 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003031 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003032 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003033 if (ctx->nr_events != ctx->nr_active)
3034 rotate = 1;
3035 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003037 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003038 goto done;
3039
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003040 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003041 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003043 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3044 if (ctx)
3045 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003046
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003047 rotate_ctx(&cpuctx->ctx);
3048 if (ctx)
3049 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003050
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003051 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003052
3053 perf_pmu_enable(cpuctx->ctx.pmu);
3054 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003055done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003056
3057 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003058}
3059
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003060#ifdef CONFIG_NO_HZ_FULL
3061bool perf_event_can_stop_tick(void)
3062{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003063 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003064 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003065 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003066 else
3067 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003068}
3069#endif
3070
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003071void perf_event_task_tick(void)
3072{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003073 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3074 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003075 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003076
3077 WARN_ON(!irqs_disabled());
3078
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003079 __this_cpu_inc(perf_throttled_seq);
3080 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3081
Mark Rutland2fde4f92015-01-07 15:01:54 +00003082 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003083 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084}
3085
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003086static int event_enable_on_exec(struct perf_event *event,
3087 struct perf_event_context *ctx)
3088{
3089 if (!event->attr.enable_on_exec)
3090 return 0;
3091
3092 event->attr.enable_on_exec = 0;
3093 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3094 return 0;
3095
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003096 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003097
3098 return 1;
3099}
3100
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003101/*
3102 * Enable all of a task's events that have been marked enable-on-exec.
3103 * This expects task == current.
3104 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003105static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003107 struct perf_event_context *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 struct perf_event *event;
3109 unsigned long flags;
3110 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003111 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112
3113 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003114 if (!ctx || !ctx->nr_events)
3115 goto out;
3116
Stephane Eraniane566b762011-04-06 02:54:54 +02003117 /*
3118 * We must ctxsw out cgroup events to avoid conflict
3119 * when invoking perf_task_event_sched_in() later on
3120 * in this function. Otherwise we end up trying to
3121 * ctxswin cgroup events which are already scheduled
3122 * in.
3123 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003124 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003125
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003126 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003127 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003129 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003130 ret = event_enable_on_exec(event, ctx);
3131 if (ret)
3132 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003133 }
3134
3135 /*
3136 * Unclone this context if we enabled any event.
3137 */
3138 if (enabled)
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003139 clone_ctx = unclone_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003140
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003141 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142
Stephane Eraniane566b762011-04-06 02:54:54 +02003143 /*
3144 * Also calls ctxswin for cgroup events, if any:
3145 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003146 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003147out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003149
3150 if (clone_ctx)
3151 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003152}
3153
Peter Zijlstrae041e322014-05-21 17:32:19 +02003154void perf_event_exec(void)
3155{
3156 struct perf_event_context *ctx;
3157 int ctxn;
3158
3159 rcu_read_lock();
3160 for_each_task_context_nr(ctxn) {
3161 ctx = current->perf_event_ctxp[ctxn];
3162 if (!ctx)
3163 continue;
3164
3165 perf_event_enable_on_exec(ctx);
3166 }
3167 rcu_read_unlock();
3168}
3169
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003170/*
3171 * Cross CPU call to read the hardware event
3172 */
3173static void __perf_event_read(void *info)
3174{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175 struct perf_event *event = info;
3176 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003177 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003178
3179 /*
3180 * If this is a task context, we need to check whether it is
3181 * the current task context of this cpu. If not it has been
3182 * scheduled out before the smp call arrived. In that case
3183 * event->count would have been updated to a recent sample
3184 * when the event was scheduled out.
3185 */
3186 if (ctx->task && cpuctx->task_ctx != ctx)
3187 return;
3188
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003189 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003190 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003191 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003192 update_cgrp_time_from_event(event);
3193 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003194 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003195 if (event->state == PERF_EVENT_STATE_ACTIVE)
3196 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003197 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198}
3199
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003200static inline u64 perf_event_count(struct perf_event *event)
3201{
Matt Flemingeacd3ec2015-01-23 18:45:41 +00003202 if (event->pmu->count)
3203 return event->pmu->count(event);
3204
3205 return __perf_event_count(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003206}
3207
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208static u64 perf_event_read(struct perf_event *event)
3209{
3210 /*
3211 * If event is enabled and currently active on a CPU, update the
3212 * value in the event structure:
3213 */
3214 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3215 smp_call_function_single(event->oncpu,
3216 __perf_event_read, event, 1);
3217 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003218 struct perf_event_context *ctx = event->ctx;
3219 unsigned long flags;
3220
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003221 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003222 /*
3223 * may read while context is not active
3224 * (e.g., thread is blocked), in that case
3225 * we cannot update context time
3226 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003227 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003228 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003229 update_cgrp_time_from_event(event);
3230 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003232 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003233 }
3234
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003235 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003236}
3237
3238/*
3239 * Initialize the perf_event context in a task_struct:
3240 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003241static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003242{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003243 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003244 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003245 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003246 INIT_LIST_HEAD(&ctx->pinned_groups);
3247 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003248 INIT_LIST_HEAD(&ctx->event_list);
3249 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003250 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003251}
3252
Peter Zijlstraeb184472010-09-07 15:55:13 +02003253static struct perf_event_context *
3254alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003255{
3256 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003257
3258 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3259 if (!ctx)
3260 return NULL;
3261
3262 __perf_event_init_context(ctx);
3263 if (task) {
3264 ctx->task = task;
3265 get_task_struct(task);
3266 }
3267 ctx->pmu = pmu;
3268
3269 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003270}
3271
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003272static struct task_struct *
3273find_lively_task_by_vpid(pid_t vpid)
3274{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003275 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003276 int err;
3277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003279 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280 task = current;
3281 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003282 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283 if (task)
3284 get_task_struct(task);
3285 rcu_read_unlock();
3286
3287 if (!task)
3288 return ERR_PTR(-ESRCH);
3289
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003290 /* Reuse ptrace permission checks for now. */
3291 err = -EACCES;
3292 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3293 goto errout;
3294
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003295 return task;
3296errout:
3297 put_task_struct(task);
3298 return ERR_PTR(err);
3299
3300}
3301
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003302/*
3303 * Returns a matching context with refcount and pincount.
3304 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003305static struct perf_event_context *
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003306find_get_context(struct pmu *pmu, struct task_struct *task,
3307 struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003309 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310 struct perf_cpu_context *cpuctx;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003311 void *task_ctx_data = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003312 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003313 int ctxn, err;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003314 int cpu = event->cpu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003316 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317 /* Must be root to operate on a CPU event: */
3318 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3319 return ERR_PTR(-EACCES);
3320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321 /*
3322 * We could be clever and allow to attach a event to an
3323 * offline CPU and activate it when the CPU comes up, but
3324 * that's for later.
3325 */
3326 if (!cpu_online(cpu))
3327 return ERR_PTR(-ENODEV);
3328
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003329 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330 ctx = &cpuctx->ctx;
3331 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003332 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333
3334 return ctx;
3335 }
3336
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003337 err = -EINVAL;
3338 ctxn = pmu->task_ctx_nr;
3339 if (ctxn < 0)
3340 goto errout;
3341
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003342 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3343 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3344 if (!task_ctx_data) {
3345 err = -ENOMEM;
3346 goto errout;
3347 }
3348 }
3349
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003350retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003351 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003353 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003354 ++ctx->pin_count;
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003355
3356 if (task_ctx_data && !ctx->task_ctx_data) {
3357 ctx->task_ctx_data = task_ctx_data;
3358 task_ctx_data = NULL;
3359 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003360 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003361
3362 if (clone_ctx)
3363 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003364 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003365 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366 err = -ENOMEM;
3367 if (!ctx)
3368 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003369
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003370 if (task_ctx_data) {
3371 ctx->task_ctx_data = task_ctx_data;
3372 task_ctx_data = NULL;
3373 }
3374
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003375 err = 0;
3376 mutex_lock(&task->perf_event_mutex);
3377 /*
3378 * If it has already passed perf_event_exit_task().
3379 * we must see PF_EXITING, it takes this mutex too.
3380 */
3381 if (task->flags & PF_EXITING)
3382 err = -ESRCH;
3383 else if (task->perf_event_ctxp[ctxn])
3384 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003385 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003386 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003387 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003388 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003389 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003390 mutex_unlock(&task->perf_event_mutex);
3391
3392 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003393 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003394
3395 if (err == -EAGAIN)
3396 goto retry;
3397 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399 }
3400
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003401 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 return ctx;
3403
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003404errout:
Yan, Zheng4af57ef282014-11-04 21:56:01 -05003405 kfree(task_ctx_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406 return ERR_PTR(err);
3407}
3408
Li Zefan6fb29152009-10-15 11:21:42 +08003409static void perf_event_free_filter(struct perf_event *event);
3410
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411static void free_event_rcu(struct rcu_head *head)
3412{
3413 struct perf_event *event;
3414
3415 event = container_of(head, struct perf_event, rcu_head);
3416 if (event->ns)
3417 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003418 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003419 kfree(event);
3420}
3421
Frederic Weisbecker76369132011-05-19 19:55:04 +02003422static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003423static void ring_buffer_attach(struct perf_event *event,
3424 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003426static void unaccount_event_cpu(struct perf_event *event, int cpu)
3427{
3428 if (event->parent)
3429 return;
3430
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003431 if (is_cgroup_event(event))
3432 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3433}
3434
3435static void unaccount_event(struct perf_event *event)
3436{
3437 if (event->parent)
3438 return;
3439
3440 if (event->attach_state & PERF_ATTACH_TASK)
3441 static_key_slow_dec_deferred(&perf_sched_events);
3442 if (event->attr.mmap || event->attr.mmap_data)
3443 atomic_dec(&nr_mmap_events);
3444 if (event->attr.comm)
3445 atomic_dec(&nr_comm_events);
3446 if (event->attr.task)
3447 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003448 if (event->attr.freq)
3449 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003450 if (is_cgroup_event(event))
3451 static_key_slow_dec_deferred(&perf_sched_events);
3452 if (has_branch_stack(event))
3453 static_key_slow_dec_deferred(&perf_sched_events);
3454
3455 unaccount_event_cpu(event, event->cpu);
3456}
3457
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003458static void __free_event(struct perf_event *event)
3459{
3460 if (!event->parent) {
3461 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3462 put_callchain_buffers();
3463 }
3464
3465 if (event->destroy)
3466 event->destroy(event);
3467
3468 if (event->ctx)
3469 put_ctx(event->ctx);
3470
Yan, Zhengc464c762014-03-18 16:56:41 +08003471 if (event->pmu)
3472 module_put(event->pmu->module);
3473
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003474 call_rcu(&event->rcu_head, free_event_rcu);
3475}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003476
3477static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003479 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003480
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003481 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003482
Frederic Weisbecker76369132011-05-19 19:55:04 +02003483 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003484 /*
3485 * Can happen when we close an event with re-directed output.
3486 *
3487 * Since we have a 0 refcount, perf_mmap_close() will skip
3488 * over us; possibly making our ring_buffer_put() the last.
3489 */
3490 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003491 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003492 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003493 }
3494
Stephane Eraniane5d13672011-02-14 11:20:01 +02003495 if (is_cgroup_event(event))
3496 perf_detach_cgroup(event);
3497
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003498 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499}
3500
Peter Zijlstra683ede42014-05-05 12:11:24 +02003501/*
3502 * Used to free events which have a known refcount of 1, such as in error paths
3503 * where the event isn't exposed yet and inherited events.
3504 */
3505static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003506{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003507 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3508 "unexpected event refcount: %ld; ptr=%p\n",
3509 atomic_long_read(&event->refcount), event)) {
3510 /* leak to avoid use-after-free */
3511 return;
3512 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003513
Peter Zijlstra683ede42014-05-05 12:11:24 +02003514 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003515}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003516
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003517/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003518 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003519 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003520static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003521{
Peter Zijlstra88821352010-11-09 19:01:43 +01003522 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003523
Peter Zijlstra88821352010-11-09 19:01:43 +01003524 rcu_read_lock();
3525 owner = ACCESS_ONCE(event->owner);
3526 /*
3527 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3528 * !owner it means the list deletion is complete and we can indeed
3529 * free this event, otherwise we need to serialize on
3530 * owner->perf_event_mutex.
3531 */
3532 smp_read_barrier_depends();
3533 if (owner) {
3534 /*
3535 * Since delayed_put_task_struct() also drops the last
3536 * task reference we can safely take a new reference
3537 * while holding the rcu_read_lock().
3538 */
3539 get_task_struct(owner);
3540 }
3541 rcu_read_unlock();
3542
3543 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003544 /*
3545 * If we're here through perf_event_exit_task() we're already
3546 * holding ctx->mutex which would be an inversion wrt. the
3547 * normal lock order.
3548 *
3549 * However we can safely take this lock because its the child
3550 * ctx->mutex.
3551 */
3552 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3553
Peter Zijlstra88821352010-11-09 19:01:43 +01003554 /*
3555 * We have to re-check the event->owner field, if it is cleared
3556 * we raced with perf_event_exit_task(), acquiring the mutex
3557 * ensured they're done, and we can proceed with freeing the
3558 * event.
3559 */
3560 if (event->owner)
3561 list_del_init(&event->owner_entry);
3562 mutex_unlock(&owner->perf_event_mutex);
3563 put_task_struct(owner);
3564 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003565}
3566
3567/*
3568 * Called when the last reference to the file is gone.
3569 */
3570static void put_event(struct perf_event *event)
3571{
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003572 struct perf_event_context *ctx;
Jiri Olsaf8697762014-08-01 14:33:01 +02003573
3574 if (!atomic_long_dec_and_test(&event->refcount))
3575 return;
3576
3577 if (!is_kernel_event(event))
3578 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003579
Peter Zijlstra683ede42014-05-05 12:11:24 +02003580 /*
3581 * There are two ways this annotation is useful:
3582 *
3583 * 1) there is a lock recursion from perf_event_exit_task
3584 * see the comment there.
3585 *
3586 * 2) there is a lock-inversion with mmap_sem through
3587 * perf_event_read_group(), which takes faults while
3588 * holding ctx->mutex, however this is called after
3589 * the last filedesc died, so there is no possibility
3590 * to trigger the AB-BA case.
3591 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003592 ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3593 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003594 perf_remove_from_context(event, true);
Leon Yud415a7f2015-02-26 20:43:33 +08003595 perf_event_ctx_unlock(event, ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003596
3597 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003598}
3599
Peter Zijlstra683ede42014-05-05 12:11:24 +02003600int perf_event_release_kernel(struct perf_event *event)
3601{
3602 put_event(event);
3603 return 0;
3604}
3605EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3606
Al Viroa6fa9412012-08-20 14:59:25 +01003607static int perf_release(struct inode *inode, struct file *file)
3608{
3609 put_event(file->private_data);
3610 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003611}
3612
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003613/*
3614 * Remove all orphanes events from the context.
3615 */
3616static void orphans_remove_work(struct work_struct *work)
3617{
3618 struct perf_event_context *ctx;
3619 struct perf_event *event, *tmp;
3620
3621 ctx = container_of(work, struct perf_event_context,
3622 orphans_remove.work);
3623
3624 mutex_lock(&ctx->mutex);
3625 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3626 struct perf_event *parent_event = event->parent;
3627
3628 if (!is_orphaned_child(event))
3629 continue;
3630
3631 perf_remove_from_context(event, true);
3632
3633 mutex_lock(&parent_event->child_mutex);
3634 list_del_init(&event->child_list);
3635 mutex_unlock(&parent_event->child_mutex);
3636
3637 free_event(event);
3638 put_event(parent_event);
3639 }
3640
3641 raw_spin_lock_irq(&ctx->lock);
3642 ctx->orphans_remove_sched = false;
3643 raw_spin_unlock_irq(&ctx->lock);
3644 mutex_unlock(&ctx->mutex);
3645
3646 put_ctx(ctx);
3647}
3648
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003649u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003650{
3651 struct perf_event *child;
3652 u64 total = 0;
3653
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003654 *enabled = 0;
3655 *running = 0;
3656
Peter Zijlstra6f105812009-11-20 22:19:56 +01003657 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003658 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003659 *enabled += event->total_time_enabled +
3660 atomic64_read(&event->child_total_time_enabled);
3661 *running += event->total_time_running +
3662 atomic64_read(&event->child_total_time_running);
3663
3664 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003665 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003666 *enabled += child->total_time_enabled;
3667 *running += child->total_time_running;
3668 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003669 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003670
3671 return total;
3672}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003673EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003674
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003675static int perf_event_read_group(struct perf_event *event,
3676 u64 read_format, char __user *buf)
3677{
3678 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003679 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003680 int n = 0, size = 0, ret;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003681 u64 count, enabled, running;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003682 u64 values[5];
Peter Zijlstraabf48682009-11-20 22:19:49 +01003683
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003684 lockdep_assert_held(&ctx->mutex);
3685
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003686 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003687
3688 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003689 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3690 values[n++] = enabled;
3691 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3692 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003693 values[n++] = count;
3694 if (read_format & PERF_FORMAT_ID)
3695 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003696
3697 size = n * sizeof(u64);
3698
3699 if (copy_to_user(buf, values, size))
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003700 return -EFAULT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003701
Peter Zijlstra6f105812009-11-20 22:19:56 +01003702 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003703
3704 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003705 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003707 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003708 if (read_format & PERF_FORMAT_ID)
3709 values[n++] = primary_event_id(sub);
3710
3711 size = n * sizeof(u64);
3712
Stephane Eranian184d3da2009-11-23 21:40:49 -08003713 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003714 return -EFAULT;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003715 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003716
3717 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003718 }
3719
Peter Zijlstraabf48682009-11-20 22:19:49 +01003720 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721}
3722
3723static int perf_event_read_one(struct perf_event *event,
3724 u64 read_format, char __user *buf)
3725{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003726 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003727 u64 values[4];
3728 int n = 0;
3729
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003730 values[n++] = perf_event_read_value(event, &enabled, &running);
3731 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3732 values[n++] = enabled;
3733 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3734 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003735 if (read_format & PERF_FORMAT_ID)
3736 values[n++] = primary_event_id(event);
3737
3738 if (copy_to_user(buf, values, n * sizeof(u64)))
3739 return -EFAULT;
3740
3741 return n * sizeof(u64);
3742}
3743
Jiri Olsadc633982014-09-12 13:18:26 +02003744static bool is_event_hup(struct perf_event *event)
3745{
3746 bool no_children;
3747
3748 if (event->state != PERF_EVENT_STATE_EXIT)
3749 return false;
3750
3751 mutex_lock(&event->child_mutex);
3752 no_children = list_empty(&event->child_list);
3753 mutex_unlock(&event->child_mutex);
3754 return no_children;
3755}
3756
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757/*
3758 * Read the performance event - simple non blocking version for now
3759 */
3760static ssize_t
3761perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3762{
3763 u64 read_format = event->attr.read_format;
3764 int ret;
3765
3766 /*
3767 * Return end-of-file for a read on a event that is in
3768 * error state (i.e. because it was pinned but it couldn't be
3769 * scheduled on to the CPU at some point).
3770 */
3771 if (event->state == PERF_EVENT_STATE_ERROR)
3772 return 0;
3773
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003774 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003775 return -ENOSPC;
3776
3777 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003778 if (read_format & PERF_FORMAT_GROUP)
3779 ret = perf_event_read_group(event, read_format, buf);
3780 else
3781 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782
3783 return ret;
3784}
3785
3786static ssize_t
3787perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3788{
3789 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003790 struct perf_event_context *ctx;
3791 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003792
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003793 ctx = perf_event_ctx_lock(event);
3794 ret = perf_read_hw(event, buf, count);
3795 perf_event_ctx_unlock(event, ctx);
3796
3797 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003798}
3799
3800static unsigned int perf_poll(struct file *file, poll_table *wait)
3801{
3802 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003803 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02003804 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003805
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02003806 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04003807
Jiri Olsadc633982014-09-12 13:18:26 +02003808 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04003809 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003810
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003811 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003812 * Pin the event->rb by taking event->mmap_mutex; otherwise
3813 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003814 */
3815 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003816 rb = event->rb;
3817 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003818 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003819 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003820 return events;
3821}
3822
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003823static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003824{
3825 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003826 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003827 perf_event_update_userpage(event);
3828}
3829
3830/*
3831 * Holding the top-level event's child_mutex means that any
3832 * descendant process that has inherited this event will block
3833 * in sync_child_event if it goes to exit, thus satisfying the
3834 * task existence requirements of perf_event_enable/disable.
3835 */
3836static void perf_event_for_each_child(struct perf_event *event,
3837 void (*func)(struct perf_event *))
3838{
3839 struct perf_event *child;
3840
3841 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003842
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003843 mutex_lock(&event->child_mutex);
3844 func(event);
3845 list_for_each_entry(child, &event->child_list, child_list)
3846 func(child);
3847 mutex_unlock(&event->child_mutex);
3848}
3849
3850static void perf_event_for_each(struct perf_event *event,
3851 void (*func)(struct perf_event *))
3852{
3853 struct perf_event_context *ctx = event->ctx;
3854 struct perf_event *sibling;
3855
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003856 lockdep_assert_held(&ctx->mutex);
3857
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003858 event = event->group_leader;
3859
3860 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003861 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003862 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003863}
3864
3865static int perf_event_period(struct perf_event *event, u64 __user *arg)
3866{
3867 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003868 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003869 u64 value;
3870
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003871 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872 return -EINVAL;
3873
John Blackwoodad0cf342010-09-28 18:03:11 -04003874 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003875 return -EFAULT;
3876
3877 if (!value)
3878 return -EINVAL;
3879
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003880 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881 if (event->attr.freq) {
3882 if (value > sysctl_perf_event_sample_rate) {
3883 ret = -EINVAL;
3884 goto unlock;
3885 }
3886
3887 event->attr.sample_freq = value;
3888 } else {
3889 event->attr.sample_period = value;
3890 event->hw.sample_period = value;
3891 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003892
3893 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3894 if (active) {
3895 perf_pmu_disable(ctx->pmu);
3896 event->pmu->stop(event, PERF_EF_UPDATE);
3897 }
3898
3899 local64_set(&event->hw.period_left, 0);
3900
3901 if (active) {
3902 event->pmu->start(event, PERF_EF_RELOAD);
3903 perf_pmu_enable(ctx->pmu);
3904 }
3905
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003906unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003907 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003908
3909 return ret;
3910}
3911
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003912static const struct file_operations perf_fops;
3913
Al Viro2903ff02012-08-28 12:52:22 -04003914static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003915{
Al Viro2903ff02012-08-28 12:52:22 -04003916 struct fd f = fdget(fd);
3917 if (!f.file)
3918 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003919
Al Viro2903ff02012-08-28 12:52:22 -04003920 if (f.file->f_op != &perf_fops) {
3921 fdput(f);
3922 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003923 }
Al Viro2903ff02012-08-28 12:52:22 -04003924 *p = f;
3925 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003926}
3927
3928static int perf_event_set_output(struct perf_event *event,
3929 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003930static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003931
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003932static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003933{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003934 void (*func)(struct perf_event *);
3935 u32 flags = arg;
3936
3937 switch (cmd) {
3938 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003939 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003940 break;
3941 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003942 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003943 break;
3944 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003945 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003946 break;
3947
3948 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003949 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003950
3951 case PERF_EVENT_IOC_PERIOD:
3952 return perf_event_period(event, (u64 __user *)arg);
3953
Jiri Olsacf4957f2012-10-24 13:37:58 +02003954 case PERF_EVENT_IOC_ID:
3955 {
3956 u64 id = primary_event_id(event);
3957
3958 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3959 return -EFAULT;
3960 return 0;
3961 }
3962
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003964 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003965 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003966 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003967 struct perf_event *output_event;
3968 struct fd output;
3969 ret = perf_fget_light(arg, &output);
3970 if (ret)
3971 return ret;
3972 output_event = output.file->private_data;
3973 ret = perf_event_set_output(event, output_event);
3974 fdput(output);
3975 } else {
3976 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003977 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003978 return ret;
3979 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003980
Li Zefan6fb29152009-10-15 11:21:42 +08003981 case PERF_EVENT_IOC_SET_FILTER:
3982 return perf_event_set_filter(event, (void __user *)arg);
3983
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003984 default:
3985 return -ENOTTY;
3986 }
3987
3988 if (flags & PERF_IOC_FLAG_GROUP)
3989 perf_event_for_each(event, func);
3990 else
3991 perf_event_for_each_child(event, func);
3992
3993 return 0;
3994}
3995
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003996static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3997{
3998 struct perf_event *event = file->private_data;
3999 struct perf_event_context *ctx;
4000 long ret;
4001
4002 ctx = perf_event_ctx_lock(event);
4003 ret = _perf_ioctl(event, cmd, arg);
4004 perf_event_ctx_unlock(event, ctx);
4005
4006 return ret;
4007}
4008
Pawel Mollb3f20782014-06-13 16:03:32 +01004009#ifdef CONFIG_COMPAT
4010static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4011 unsigned long arg)
4012{
4013 switch (_IOC_NR(cmd)) {
4014 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4015 case _IOC_NR(PERF_EVENT_IOC_ID):
4016 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4017 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4018 cmd &= ~IOCSIZE_MASK;
4019 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4020 }
4021 break;
4022 }
4023 return perf_ioctl(file, cmd, arg);
4024}
4025#else
4026# define perf_compat_ioctl NULL
4027#endif
4028
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004029int perf_event_task_enable(void)
4030{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004031 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004032 struct perf_event *event;
4033
4034 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004035 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4036 ctx = perf_event_ctx_lock(event);
4037 perf_event_for_each_child(event, _perf_event_enable);
4038 perf_event_ctx_unlock(event, ctx);
4039 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004040 mutex_unlock(&current->perf_event_mutex);
4041
4042 return 0;
4043}
4044
4045int perf_event_task_disable(void)
4046{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004047 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004048 struct perf_event *event;
4049
4050 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004051 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4052 ctx = perf_event_ctx_lock(event);
4053 perf_event_for_each_child(event, _perf_event_disable);
4054 perf_event_ctx_unlock(event, ctx);
4055 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004056 mutex_unlock(&current->perf_event_mutex);
4057
4058 return 0;
4059}
4060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004061static int perf_event_index(struct perf_event *event)
4062{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004063 if (event->hw.state & PERF_HES_STOPPED)
4064 return 0;
4065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004066 if (event->state != PERF_EVENT_STATE_ACTIVE)
4067 return 0;
4068
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004069 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004070}
4071
Eric B Munsonc4794292011-06-23 16:34:38 -04004072static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004073 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004074 u64 *enabled,
4075 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004076{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004077 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004078
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004079 *now = perf_clock();
4080 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004081 *enabled = ctx_time - event->tstamp_enabled;
4082 *running = ctx_time - event->tstamp_running;
4083}
4084
Peter Zijlstrafa731582013-09-19 10:16:42 +02004085static void perf_event_init_userpage(struct perf_event *event)
4086{
4087 struct perf_event_mmap_page *userpg;
4088 struct ring_buffer *rb;
4089
4090 rcu_read_lock();
4091 rb = rcu_dereference(event->rb);
4092 if (!rb)
4093 goto unlock;
4094
4095 userpg = rb->user_page;
4096
4097 /* Allow new userspace to detect that bit 0 is deprecated */
4098 userpg->cap_bit0_is_deprecated = 1;
4099 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
4100
4101unlock:
4102 rcu_read_unlock();
4103}
4104
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004105void __weak arch_perf_update_userpage(
4106 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004107{
4108}
4109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110/*
4111 * Callers need to ensure there can be no nesting of this function, otherwise
4112 * the seqlock logic goes bad. We can not serialize this because the arch
4113 * code calls this from NMI context.
4114 */
4115void perf_event_update_userpage(struct perf_event *event)
4116{
4117 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004118 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004119 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120
4121 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004122 rb = rcu_dereference(event->rb);
4123 if (!rb)
4124 goto unlock;
4125
Eric B Munson0d641202011-06-24 12:26:26 -04004126 /*
4127 * compute total_time_enabled, total_time_running
4128 * based on snapshot values taken when the event
4129 * was last scheduled in.
4130 *
4131 * we cannot simply called update_context_time()
4132 * because of locking issue as we can be called in
4133 * NMI context
4134 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004135 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136
Frederic Weisbecker76369132011-05-19 19:55:04 +02004137 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138 /*
4139 * Disable preemption so as to not let the corresponding user-space
4140 * spin too long if we get preempted.
4141 */
4142 preempt_disable();
4143 ++userpg->lock;
4144 barrier();
4145 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004146 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004147 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004148 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004149
Eric B Munson0d641202011-06-24 12:26:26 -04004150 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004151 atomic64_read(&event->child_total_time_enabled);
4152
Eric B Munson0d641202011-06-24 12:26:26 -04004153 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154 atomic64_read(&event->child_total_time_running);
4155
Andy Lutomirskic1317ec2014-10-24 15:58:11 -07004156 arch_perf_update_userpage(event, userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004157
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004158 barrier();
4159 ++userpg->lock;
4160 preempt_enable();
4161unlock:
4162 rcu_read_unlock();
4163}
4164
Peter Zijlstra906010b2009-09-21 16:08:49 +02004165static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4166{
4167 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004168 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004169 int ret = VM_FAULT_SIGBUS;
4170
4171 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4172 if (vmf->pgoff == 0)
4173 ret = 0;
4174 return ret;
4175 }
4176
4177 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004178 rb = rcu_dereference(event->rb);
4179 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004180 goto unlock;
4181
4182 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4183 goto unlock;
4184
Frederic Weisbecker76369132011-05-19 19:55:04 +02004185 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004186 if (!vmf->page)
4187 goto unlock;
4188
4189 get_page(vmf->page);
4190 vmf->page->mapping = vma->vm_file->f_mapping;
4191 vmf->page->index = vmf->pgoff;
4192
4193 ret = 0;
4194unlock:
4195 rcu_read_unlock();
4196
4197 return ret;
4198}
4199
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004200static void ring_buffer_attach(struct perf_event *event,
4201 struct ring_buffer *rb)
4202{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004203 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004204 unsigned long flags;
4205
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004206 if (event->rb) {
4207 /*
4208 * Should be impossible, we set this when removing
4209 * event->rb_entry and wait/clear when adding event->rb_entry.
4210 */
4211 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004212
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004213 old_rb = event->rb;
4214 event->rcu_batches = get_state_synchronize_rcu();
4215 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004216
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004217 spin_lock_irqsave(&old_rb->event_lock, flags);
4218 list_del_rcu(&event->rb_entry);
4219 spin_unlock_irqrestore(&old_rb->event_lock, flags);
4220 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004221
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004222 if (event->rcu_pending && rb) {
4223 cond_synchronize_rcu(event->rcu_batches);
4224 event->rcu_pending = 0;
4225 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004226
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004227 if (rb) {
4228 spin_lock_irqsave(&rb->event_lock, flags);
4229 list_add_rcu(&event->rb_entry, &rb->event_list);
4230 spin_unlock_irqrestore(&rb->event_lock, flags);
4231 }
4232
4233 rcu_assign_pointer(event->rb, rb);
4234
4235 if (old_rb) {
4236 ring_buffer_put(old_rb);
4237 /*
4238 * Since we detached before setting the new rb, so that we
4239 * could attach the new rb, we could have missed a wakeup.
4240 * Provide it now.
4241 */
4242 wake_up_all(&event->waitq);
4243 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004244}
4245
4246static void ring_buffer_wakeup(struct perf_event *event)
4247{
4248 struct ring_buffer *rb;
4249
4250 rcu_read_lock();
4251 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004252 if (rb) {
4253 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4254 wake_up_all(&event->waitq);
4255 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004256 rcu_read_unlock();
4257}
4258
Frederic Weisbecker76369132011-05-19 19:55:04 +02004259static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004260{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004261 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004262
Frederic Weisbecker76369132011-05-19 19:55:04 +02004263 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
4264 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004265}
4266
Frederic Weisbecker76369132011-05-19 19:55:04 +02004267static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004269 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004270
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004271 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004272 rb = rcu_dereference(event->rb);
4273 if (rb) {
4274 if (!atomic_inc_not_zero(&rb->refcount))
4275 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004276 }
4277 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004278
Frederic Weisbecker76369132011-05-19 19:55:04 +02004279 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004280}
4281
Frederic Weisbecker76369132011-05-19 19:55:04 +02004282static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004283{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004284 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004285 return;
4286
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004287 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004288
Frederic Weisbecker76369132011-05-19 19:55:04 +02004289 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004290}
4291
4292static void perf_mmap_open(struct vm_area_struct *vma)
4293{
4294 struct perf_event *event = vma->vm_file->private_data;
4295
4296 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004297 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004298
4299 if (event->pmu->event_mapped)
4300 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004301}
4302
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004303/*
4304 * A buffer can be mmap()ed multiple times; either directly through the same
4305 * event, or through other events by use of perf_event_set_output().
4306 *
4307 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4308 * the buffer here, where we still have a VM context. This means we need
4309 * to detach all events redirecting to us.
4310 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311static void perf_mmap_close(struct vm_area_struct *vma)
4312{
4313 struct perf_event *event = vma->vm_file->private_data;
4314
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004315 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004316 struct user_struct *mmap_user = rb->mmap_user;
4317 int mmap_locked = rb->mmap_locked;
4318 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004319
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004320 if (event->pmu->event_unmapped)
4321 event->pmu->event_unmapped(event);
4322
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004323 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004324
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004325 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004326 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004327
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004328 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004329 mutex_unlock(&event->mmap_mutex);
4330
4331 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004332 if (atomic_read(&rb->mmap_count))
4333 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004334
4335 /*
4336 * No other mmap()s, detach from all other events that might redirect
4337 * into the now unreachable buffer. Somewhat complicated by the
4338 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4339 */
4340again:
4341 rcu_read_lock();
4342 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4343 if (!atomic_long_inc_not_zero(&event->refcount)) {
4344 /*
4345 * This event is en-route to free_event() which will
4346 * detach it and remove it from the list.
4347 */
4348 continue;
4349 }
4350 rcu_read_unlock();
4351
4352 mutex_lock(&event->mmap_mutex);
4353 /*
4354 * Check we didn't race with perf_event_set_output() which can
4355 * swizzle the rb from under us while we were waiting to
4356 * acquire mmap_mutex.
4357 *
4358 * If we find a different rb; ignore this event, a next
4359 * iteration will no longer find it on the list. We have to
4360 * still restart the iteration to make sure we're not now
4361 * iterating the wrong list.
4362 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004363 if (event->rb == rb)
4364 ring_buffer_attach(event, NULL);
4365
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004366 mutex_unlock(&event->mmap_mutex);
4367 put_event(event);
4368
4369 /*
4370 * Restart the iteration; either we're on the wrong list or
4371 * destroyed its integrity by doing a deletion.
4372 */
4373 goto again;
4374 }
4375 rcu_read_unlock();
4376
4377 /*
4378 * It could be there's still a few 0-ref events on the list; they'll
4379 * get cleaned up by free_event() -- they'll also still have their
4380 * ref on the rb and will free it whenever they are done with it.
4381 *
4382 * Aside from that, this buffer is 'fully' detached and unmapped,
4383 * undo the VM accounting.
4384 */
4385
4386 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4387 vma->vm_mm->pinned_vm -= mmap_locked;
4388 free_uid(mmap_user);
4389
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004390out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004391 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004392}
4393
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004394static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395 .open = perf_mmap_open,
4396 .close = perf_mmap_close,
4397 .fault = perf_mmap_fault,
4398 .page_mkwrite = perf_mmap_fault,
4399};
4400
4401static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4402{
4403 struct perf_event *event = file->private_data;
4404 unsigned long user_locked, user_lock_limit;
4405 struct user_struct *user = current_user();
4406 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004407 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004408 unsigned long vma_size;
4409 unsigned long nr_pages;
4410 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004411 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004412
Peter Zijlstrac7920612010-05-18 10:33:24 +02004413 /*
4414 * Don't allow mmap() of inherited per-task counters. This would
4415 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004416 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004417 */
4418 if (event->cpu == -1 && event->attr.inherit)
4419 return -EINVAL;
4420
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004421 if (!(vma->vm_flags & VM_SHARED))
4422 return -EINVAL;
4423
4424 vma_size = vma->vm_end - vma->vm_start;
4425 nr_pages = (vma_size / PAGE_SIZE) - 1;
4426
4427 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004428 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429 * can do bitmasks instead of modulo.
4430 */
Kan Liang2ed11312015-03-02 02:14:26 -05004431 if (nr_pages != 0 && !is_power_of_2(nr_pages))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432 return -EINVAL;
4433
4434 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4435 return -EINVAL;
4436
4437 if (vma->vm_pgoff != 0)
4438 return -EINVAL;
4439
4440 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004441again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004442 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004443 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004444 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004446 goto unlock;
4447 }
4448
4449 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4450 /*
4451 * Raced against perf_mmap_close() through
4452 * perf_event_set_output(). Try again, hope for better
4453 * luck.
4454 */
4455 mutex_unlock(&event->mmap_mutex);
4456 goto again;
4457 }
4458
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004459 goto unlock;
4460 }
4461
4462 user_extra = nr_pages + 1;
4463 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4464
4465 /*
4466 * Increase the limit linearly with more CPUs:
4467 */
4468 user_lock_limit *= num_online_cpus();
4469
4470 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4471
4472 extra = 0;
4473 if (user_locked > user_lock_limit)
4474 extra = user_locked - user_lock_limit;
4475
Jiri Slaby78d7d402010-03-05 13:42:54 -08004476 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004478 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004479
4480 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4481 !capable(CAP_IPC_LOCK)) {
4482 ret = -EPERM;
4483 goto unlock;
4484 }
4485
Frederic Weisbecker76369132011-05-19 19:55:04 +02004486 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004487
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004488 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004489 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004490
Vince Weaver4ec83632011-06-01 15:15:36 -04004491 rb = rb_alloc(nr_pages,
4492 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4493 event->cpu, flags);
4494
Frederic Weisbecker76369132011-05-19 19:55:04 +02004495 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004496 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004498 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004499
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004500 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004501 rb->mmap_locked = extra;
4502 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004503
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004504 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004505 vma->vm_mm->pinned_vm += extra;
4506
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004507 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004508
Peter Zijlstrafa731582013-09-19 10:16:42 +02004509 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004510 perf_event_update_userpage(event);
4511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004513 if (!ret)
4514 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004515 mutex_unlock(&event->mmap_mutex);
4516
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004517 /*
4518 * Since pinned accounting is per vm we cannot allow fork() to copy our
4519 * vma.
4520 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004521 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004522 vma->vm_ops = &perf_mmap_vmops;
4523
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004524 if (event->pmu->event_mapped)
4525 event->pmu->event_mapped(event);
4526
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004527 return ret;
4528}
4529
4530static int perf_fasync(int fd, struct file *filp, int on)
4531{
Al Viro496ad9a2013-01-23 17:07:38 -05004532 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533 struct perf_event *event = filp->private_data;
4534 int retval;
4535
4536 mutex_lock(&inode->i_mutex);
4537 retval = fasync_helper(fd, filp, on, &event->fasync);
4538 mutex_unlock(&inode->i_mutex);
4539
4540 if (retval < 0)
4541 return retval;
4542
4543 return 0;
4544}
4545
4546static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004547 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548 .release = perf_release,
4549 .read = perf_read,
4550 .poll = perf_poll,
4551 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004552 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004553 .mmap = perf_mmap,
4554 .fasync = perf_fasync,
4555};
4556
4557/*
4558 * Perf event wakeup
4559 *
4560 * If there's data, ensure we set the poll() state and publish everything
4561 * to user-space before waking everybody up.
4562 */
4563
4564void perf_event_wakeup(struct perf_event *event)
4565{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004566 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567
4568 if (event->pending_kill) {
4569 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4570 event->pending_kill = 0;
4571 }
4572}
4573
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004574static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004575{
4576 struct perf_event *event = container_of(entry,
4577 struct perf_event, pending);
Peter Zijlstrad5252112015-02-19 18:03:11 +01004578 int rctx;
4579
4580 rctx = perf_swevent_get_recursion_context();
4581 /*
4582 * If we 'fail' here, that's OK, it means recursion is already disabled
4583 * and we won't recurse 'further'.
4584 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004585
4586 if (event->pending_disable) {
4587 event->pending_disable = 0;
4588 __perf_event_disable(event);
4589 }
4590
4591 if (event->pending_wakeup) {
4592 event->pending_wakeup = 0;
4593 perf_event_wakeup(event);
4594 }
Peter Zijlstrad5252112015-02-19 18:03:11 +01004595
4596 if (rctx >= 0)
4597 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004598}
4599
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004600/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004601 * We assume there is only KVM supporting the callbacks.
4602 * Later on, we might change it to a list if there is
4603 * another virtualization implementation supporting the callbacks.
4604 */
4605struct perf_guest_info_callbacks *perf_guest_cbs;
4606
4607int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4608{
4609 perf_guest_cbs = cbs;
4610 return 0;
4611}
4612EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4613
4614int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4615{
4616 perf_guest_cbs = NULL;
4617 return 0;
4618}
4619EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4620
Jiri Olsa40189942012-08-07 15:20:37 +02004621static void
4622perf_output_sample_regs(struct perf_output_handle *handle,
4623 struct pt_regs *regs, u64 mask)
4624{
4625 int bit;
4626
4627 for_each_set_bit(bit, (const unsigned long *) &mask,
4628 sizeof(mask) * BITS_PER_BYTE) {
4629 u64 val;
4630
4631 val = perf_reg_value(regs, bit);
4632 perf_output_put(handle, val);
4633 }
4634}
4635
Stephane Eranian60e23642014-09-24 13:48:37 +02004636static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004637 struct pt_regs *regs,
4638 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004639{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004640 if (user_mode(regs)) {
4641 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004642 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004643 } else if (current->mm) {
4644 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004645 } else {
4646 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
4647 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02004648 }
4649}
4650
Stephane Eranian60e23642014-09-24 13:48:37 +02004651static void perf_sample_regs_intr(struct perf_regs *regs_intr,
4652 struct pt_regs *regs)
4653{
4654 regs_intr->regs = regs;
4655 regs_intr->abi = perf_reg_abi(current);
4656}
4657
4658
Jiri Olsac5ebced2012-08-07 15:20:40 +02004659/*
4660 * Get remaining task size from user stack pointer.
4661 *
4662 * It'd be better to take stack vma map and limit this more
4663 * precisly, but there's no way to get it safely under interrupt,
4664 * so using TASK_SIZE as limit.
4665 */
4666static u64 perf_ustack_task_size(struct pt_regs *regs)
4667{
4668 unsigned long addr = perf_user_stack_pointer(regs);
4669
4670 if (!addr || addr >= TASK_SIZE)
4671 return 0;
4672
4673 return TASK_SIZE - addr;
4674}
4675
4676static u16
4677perf_sample_ustack_size(u16 stack_size, u16 header_size,
4678 struct pt_regs *regs)
4679{
4680 u64 task_size;
4681
4682 /* No regs, no stack pointer, no dump. */
4683 if (!regs)
4684 return 0;
4685
4686 /*
4687 * Check if we fit in with the requested stack size into the:
4688 * - TASK_SIZE
4689 * If we don't, we limit the size to the TASK_SIZE.
4690 *
4691 * - remaining sample size
4692 * If we don't, we customize the stack size to
4693 * fit in to the remaining sample size.
4694 */
4695
4696 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4697 stack_size = min(stack_size, (u16) task_size);
4698
4699 /* Current header size plus static size and dynamic size. */
4700 header_size += 2 * sizeof(u64);
4701
4702 /* Do we fit in with the current stack dump size? */
4703 if ((u16) (header_size + stack_size) < header_size) {
4704 /*
4705 * If we overflow the maximum size for the sample,
4706 * we customize the stack dump size to fit in.
4707 */
4708 stack_size = USHRT_MAX - header_size - sizeof(u64);
4709 stack_size = round_up(stack_size, sizeof(u64));
4710 }
4711
4712 return stack_size;
4713}
4714
4715static void
4716perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4717 struct pt_regs *regs)
4718{
4719 /* Case of a kernel thread, nothing to dump */
4720 if (!regs) {
4721 u64 size = 0;
4722 perf_output_put(handle, size);
4723 } else {
4724 unsigned long sp;
4725 unsigned int rem;
4726 u64 dyn_size;
4727
4728 /*
4729 * We dump:
4730 * static size
4731 * - the size requested by user or the best one we can fit
4732 * in to the sample max size
4733 * data
4734 * - user stack dump data
4735 * dynamic size
4736 * - the actual dumped size
4737 */
4738
4739 /* Static size. */
4740 perf_output_put(handle, dump_size);
4741
4742 /* Data. */
4743 sp = perf_user_stack_pointer(regs);
4744 rem = __output_copy_user(handle, (void *) sp, dump_size);
4745 dyn_size = dump_size - rem;
4746
4747 perf_output_skip(handle, rem);
4748
4749 /* Dynamic size. */
4750 perf_output_put(handle, dyn_size);
4751 }
4752}
4753
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004754static void __perf_event_header__init_id(struct perf_event_header *header,
4755 struct perf_sample_data *data,
4756 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004757{
4758 u64 sample_type = event->attr.sample_type;
4759
4760 data->type = sample_type;
4761 header->size += event->id_header_size;
4762
4763 if (sample_type & PERF_SAMPLE_TID) {
4764 /* namespace issues */
4765 data->tid_entry.pid = perf_event_pid(event, current);
4766 data->tid_entry.tid = perf_event_tid(event, current);
4767 }
4768
4769 if (sample_type & PERF_SAMPLE_TIME)
Peter Zijlstra34f43922015-02-20 14:05:38 +01004770 data->time = perf_event_clock(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004771
Adrian Hunterff3d5272013-08-27 11:23:07 +03004772 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004773 data->id = primary_event_id(event);
4774
4775 if (sample_type & PERF_SAMPLE_STREAM_ID)
4776 data->stream_id = event->id;
4777
4778 if (sample_type & PERF_SAMPLE_CPU) {
4779 data->cpu_entry.cpu = raw_smp_processor_id();
4780 data->cpu_entry.reserved = 0;
4781 }
4782}
4783
Frederic Weisbecker76369132011-05-19 19:55:04 +02004784void perf_event_header__init_id(struct perf_event_header *header,
4785 struct perf_sample_data *data,
4786 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004787{
4788 if (event->attr.sample_id_all)
4789 __perf_event_header__init_id(header, data, event);
4790}
4791
4792static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4793 struct perf_sample_data *data)
4794{
4795 u64 sample_type = data->type;
4796
4797 if (sample_type & PERF_SAMPLE_TID)
4798 perf_output_put(handle, data->tid_entry);
4799
4800 if (sample_type & PERF_SAMPLE_TIME)
4801 perf_output_put(handle, data->time);
4802
4803 if (sample_type & PERF_SAMPLE_ID)
4804 perf_output_put(handle, data->id);
4805
4806 if (sample_type & PERF_SAMPLE_STREAM_ID)
4807 perf_output_put(handle, data->stream_id);
4808
4809 if (sample_type & PERF_SAMPLE_CPU)
4810 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004811
4812 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4813 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004814}
4815
Frederic Weisbecker76369132011-05-19 19:55:04 +02004816void perf_event__output_id_sample(struct perf_event *event,
4817 struct perf_output_handle *handle,
4818 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004819{
4820 if (event->attr.sample_id_all)
4821 __perf_event__output_id_sample(handle, sample);
4822}
4823
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004824static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004825 struct perf_event *event,
4826 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004827{
4828 u64 read_format = event->attr.read_format;
4829 u64 values[4];
4830 int n = 0;
4831
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004832 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004833 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004834 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004835 atomic64_read(&event->child_total_time_enabled);
4836 }
4837 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004838 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004839 atomic64_read(&event->child_total_time_running);
4840 }
4841 if (read_format & PERF_FORMAT_ID)
4842 values[n++] = primary_event_id(event);
4843
Frederic Weisbecker76369132011-05-19 19:55:04 +02004844 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004845}
4846
4847/*
4848 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4849 */
4850static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004851 struct perf_event *event,
4852 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004853{
4854 struct perf_event *leader = event->group_leader, *sub;
4855 u64 read_format = event->attr.read_format;
4856 u64 values[5];
4857 int n = 0;
4858
4859 values[n++] = 1 + leader->nr_siblings;
4860
4861 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004862 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863
4864 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004865 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866
4867 if (leader != event)
4868 leader->pmu->read(leader);
4869
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004870 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871 if (read_format & PERF_FORMAT_ID)
4872 values[n++] = primary_event_id(leader);
4873
Frederic Weisbecker76369132011-05-19 19:55:04 +02004874 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004875
4876 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4877 n = 0;
4878
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004879 if ((sub != event) &&
4880 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004881 sub->pmu->read(sub);
4882
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004883 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004884 if (read_format & PERF_FORMAT_ID)
4885 values[n++] = primary_event_id(sub);
4886
Frederic Weisbecker76369132011-05-19 19:55:04 +02004887 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004888 }
4889}
4890
Stephane Eranianeed01522010-10-26 16:08:01 +02004891#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4892 PERF_FORMAT_TOTAL_TIME_RUNNING)
4893
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894static void perf_output_read(struct perf_output_handle *handle,
4895 struct perf_event *event)
4896{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004897 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004898 u64 read_format = event->attr.read_format;
4899
4900 /*
4901 * compute total_time_enabled, total_time_running
4902 * based on snapshot values taken when the event
4903 * was last scheduled in.
4904 *
4905 * we cannot simply called update_context_time()
4906 * because of locking issue as we are called in
4907 * NMI context
4908 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004909 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004910 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004912 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004913 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004914 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004915 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004916}
4917
4918void perf_output_sample(struct perf_output_handle *handle,
4919 struct perf_event_header *header,
4920 struct perf_sample_data *data,
4921 struct perf_event *event)
4922{
4923 u64 sample_type = data->type;
4924
4925 perf_output_put(handle, *header);
4926
Adrian Hunterff3d5272013-08-27 11:23:07 +03004927 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4928 perf_output_put(handle, data->id);
4929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004930 if (sample_type & PERF_SAMPLE_IP)
4931 perf_output_put(handle, data->ip);
4932
4933 if (sample_type & PERF_SAMPLE_TID)
4934 perf_output_put(handle, data->tid_entry);
4935
4936 if (sample_type & PERF_SAMPLE_TIME)
4937 perf_output_put(handle, data->time);
4938
4939 if (sample_type & PERF_SAMPLE_ADDR)
4940 perf_output_put(handle, data->addr);
4941
4942 if (sample_type & PERF_SAMPLE_ID)
4943 perf_output_put(handle, data->id);
4944
4945 if (sample_type & PERF_SAMPLE_STREAM_ID)
4946 perf_output_put(handle, data->stream_id);
4947
4948 if (sample_type & PERF_SAMPLE_CPU)
4949 perf_output_put(handle, data->cpu_entry);
4950
4951 if (sample_type & PERF_SAMPLE_PERIOD)
4952 perf_output_put(handle, data->period);
4953
4954 if (sample_type & PERF_SAMPLE_READ)
4955 perf_output_read(handle, event);
4956
4957 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4958 if (data->callchain) {
4959 int size = 1;
4960
4961 if (data->callchain)
4962 size += data->callchain->nr;
4963
4964 size *= sizeof(u64);
4965
Frederic Weisbecker76369132011-05-19 19:55:04 +02004966 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004967 } else {
4968 u64 nr = 0;
4969 perf_output_put(handle, nr);
4970 }
4971 }
4972
4973 if (sample_type & PERF_SAMPLE_RAW) {
4974 if (data->raw) {
4975 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004976 __output_copy(handle, data->raw->data,
4977 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004978 } else {
4979 struct {
4980 u32 size;
4981 u32 data;
4982 } raw = {
4983 .size = sizeof(u32),
4984 .data = 0,
4985 };
4986 perf_output_put(handle, raw);
4987 }
4988 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004989
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004990 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4991 if (data->br_stack) {
4992 size_t size;
4993
4994 size = data->br_stack->nr
4995 * sizeof(struct perf_branch_entry);
4996
4997 perf_output_put(handle, data->br_stack->nr);
4998 perf_output_copy(handle, data->br_stack->entries, size);
4999 } else {
5000 /*
5001 * we always store at least the value of nr
5002 */
5003 u64 nr = 0;
5004 perf_output_put(handle, nr);
5005 }
5006 }
Jiri Olsa40189942012-08-07 15:20:37 +02005007
5008 if (sample_type & PERF_SAMPLE_REGS_USER) {
5009 u64 abi = data->regs_user.abi;
5010
5011 /*
5012 * If there are no regs to dump, notice it through
5013 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5014 */
5015 perf_output_put(handle, abi);
5016
5017 if (abi) {
5018 u64 mask = event->attr.sample_regs_user;
5019 perf_output_sample_regs(handle,
5020 data->regs_user.regs,
5021 mask);
5022 }
5023 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005024
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005025 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005026 perf_output_sample_ustack(handle,
5027 data->stack_user_size,
5028 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005029 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005030
5031 if (sample_type & PERF_SAMPLE_WEIGHT)
5032 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005033
5034 if (sample_type & PERF_SAMPLE_DATA_SRC)
5035 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005036
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005037 if (sample_type & PERF_SAMPLE_TRANSACTION)
5038 perf_output_put(handle, data->txn);
5039
Stephane Eranian60e23642014-09-24 13:48:37 +02005040 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5041 u64 abi = data->regs_intr.abi;
5042 /*
5043 * If there are no regs to dump, notice it through
5044 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5045 */
5046 perf_output_put(handle, abi);
5047
5048 if (abi) {
5049 u64 mask = event->attr.sample_regs_intr;
5050
5051 perf_output_sample_regs(handle,
5052 data->regs_intr.regs,
5053 mask);
5054 }
5055 }
5056
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005057 if (!event->attr.watermark) {
5058 int wakeup_events = event->attr.wakeup_events;
5059
5060 if (wakeup_events) {
5061 struct ring_buffer *rb = handle->rb;
5062 int events = local_inc_return(&rb->events);
5063
5064 if (events >= wakeup_events) {
5065 local_sub(wakeup_events, &rb->events);
5066 local_inc(&rb->wakeup);
5067 }
5068 }
5069 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005070}
5071
5072void perf_prepare_sample(struct perf_event_header *header,
5073 struct perf_sample_data *data,
5074 struct perf_event *event,
5075 struct pt_regs *regs)
5076{
5077 u64 sample_type = event->attr.sample_type;
5078
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005079 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005080 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005081
5082 header->misc = 0;
5083 header->misc |= perf_misc_flags(regs);
5084
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005085 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005086
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005087 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005088 data->ip = perf_instruction_pointer(regs);
5089
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005090 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5091 int size = 1;
5092
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005093 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005094
5095 if (data->callchain)
5096 size += data->callchain->nr;
5097
5098 header->size += size * sizeof(u64);
5099 }
5100
5101 if (sample_type & PERF_SAMPLE_RAW) {
5102 int size = sizeof(u32);
5103
5104 if (data->raw)
5105 size += data->raw->size;
5106 else
5107 size += sizeof(u32);
5108
5109 WARN_ON_ONCE(size & (sizeof(u64)-1));
5110 header->size += size;
5111 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005112
5113 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5114 int size = sizeof(u64); /* nr */
5115 if (data->br_stack) {
5116 size += data->br_stack->nr
5117 * sizeof(struct perf_branch_entry);
5118 }
5119 header->size += size;
5120 }
Jiri Olsa40189942012-08-07 15:20:37 +02005121
Peter Zijlstra25657112014-09-24 13:48:42 +02005122 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005123 perf_sample_regs_user(&data->regs_user, regs,
5124 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005125
Jiri Olsa40189942012-08-07 15:20:37 +02005126 if (sample_type & PERF_SAMPLE_REGS_USER) {
5127 /* regs dump ABI info */
5128 int size = sizeof(u64);
5129
Jiri Olsa40189942012-08-07 15:20:37 +02005130 if (data->regs_user.regs) {
5131 u64 mask = event->attr.sample_regs_user;
5132 size += hweight64(mask) * sizeof(u64);
5133 }
5134
5135 header->size += size;
5136 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005137
5138 if (sample_type & PERF_SAMPLE_STACK_USER) {
5139 /*
5140 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5141 * processed as the last one or have additional check added
5142 * in case new sample type is added, because we could eat
5143 * up the rest of the sample size.
5144 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005145 u16 stack_size = event->attr.sample_stack_user;
5146 u16 size = sizeof(u64);
5147
Jiri Olsac5ebced2012-08-07 15:20:40 +02005148 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005149 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005150
5151 /*
5152 * If there is something to dump, add space for the dump
5153 * itself and for the field that tells the dynamic size,
5154 * which is how many have been actually dumped.
5155 */
5156 if (stack_size)
5157 size += sizeof(u64) + stack_size;
5158
5159 data->stack_user_size = stack_size;
5160 header->size += size;
5161 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005162
5163 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5164 /* regs dump ABI info */
5165 int size = sizeof(u64);
5166
5167 perf_sample_regs_intr(&data->regs_intr, regs);
5168
5169 if (data->regs_intr.regs) {
5170 u64 mask = event->attr.sample_regs_intr;
5171
5172 size += hweight64(mask) * sizeof(u64);
5173 }
5174
5175 header->size += size;
5176 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177}
5178
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005179static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180 struct perf_sample_data *data,
5181 struct pt_regs *regs)
5182{
5183 struct perf_output_handle handle;
5184 struct perf_event_header header;
5185
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005186 /* protect the callchain buffers */
5187 rcu_read_lock();
5188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005189 perf_prepare_sample(&header, data, event, regs);
5190
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005191 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005192 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005193
5194 perf_output_sample(&handle, &header, data, event);
5195
5196 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005197
5198exit:
5199 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005200}
5201
5202/*
5203 * read event_id
5204 */
5205
5206struct perf_read_event {
5207 struct perf_event_header header;
5208
5209 u32 pid;
5210 u32 tid;
5211};
5212
5213static void
5214perf_event_read_event(struct perf_event *event,
5215 struct task_struct *task)
5216{
5217 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005218 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005219 struct perf_read_event read_event = {
5220 .header = {
5221 .type = PERF_RECORD_READ,
5222 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005223 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005224 },
5225 .pid = perf_event_pid(event, task),
5226 .tid = perf_event_tid(event, task),
5227 };
5228 int ret;
5229
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005230 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005231 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005232 if (ret)
5233 return;
5234
5235 perf_output_put(&handle, read_event);
5236 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005237 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005238
5239 perf_output_end(&handle);
5240}
5241
Jiri Olsa52d857a2013-05-06 18:27:18 +02005242typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5243
5244static void
5245perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005246 perf_event_aux_output_cb output,
5247 void *data)
5248{
5249 struct perf_event *event;
5250
5251 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5252 if (event->state < PERF_EVENT_STATE_INACTIVE)
5253 continue;
5254 if (!event_filter_match(event))
5255 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005256 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005257 }
5258}
5259
5260static void
Jiri Olsa67516842013-07-09 18:56:31 +02005261perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005262 struct perf_event_context *task_ctx)
5263{
5264 struct perf_cpu_context *cpuctx;
5265 struct perf_event_context *ctx;
5266 struct pmu *pmu;
5267 int ctxn;
5268
5269 rcu_read_lock();
5270 list_for_each_entry_rcu(pmu, &pmus, entry) {
5271 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5272 if (cpuctx->unique_pmu != pmu)
5273 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005274 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005275 if (task_ctx)
5276 goto next;
5277 ctxn = pmu->task_ctx_nr;
5278 if (ctxn < 0)
5279 goto next;
5280 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5281 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005282 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005283next:
5284 put_cpu_ptr(pmu->pmu_cpu_context);
5285 }
5286
5287 if (task_ctx) {
5288 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02005289 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005290 preempt_enable();
5291 }
5292 rcu_read_unlock();
5293}
5294
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295/*
5296 * task tracking -- fork/exit
5297 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005298 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299 */
5300
5301struct perf_task_event {
5302 struct task_struct *task;
5303 struct perf_event_context *task_ctx;
5304
5305 struct {
5306 struct perf_event_header header;
5307
5308 u32 pid;
5309 u32 ppid;
5310 u32 tid;
5311 u32 ptid;
5312 u64 time;
5313 } event_id;
5314};
5315
Jiri Olsa67516842013-07-09 18:56:31 +02005316static int perf_event_task_match(struct perf_event *event)
5317{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005318 return event->attr.comm || event->attr.mmap ||
5319 event->attr.mmap2 || event->attr.mmap_data ||
5320 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005321}
5322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005324 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005325{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005326 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005327 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005328 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005329 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005330 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005331
Jiri Olsa67516842013-07-09 18:56:31 +02005332 if (!perf_event_task_match(event))
5333 return;
5334
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005335 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005337 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005338 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005339 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005340 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341
5342 task_event->event_id.pid = perf_event_pid(event, task);
5343 task_event->event_id.ppid = perf_event_pid(event, current);
5344
5345 task_event->event_id.tid = perf_event_tid(event, task);
5346 task_event->event_id.ptid = perf_event_tid(event, current);
5347
Peter Zijlstra34f43922015-02-20 14:05:38 +01005348 task_event->event_id.time = perf_event_clock(event);
5349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005350 perf_output_put(&handle, task_event->event_id);
5351
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005352 perf_event__output_id_sample(event, &handle, &sample);
5353
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005354 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005355out:
5356 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005357}
5358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005359static void perf_event_task(struct task_struct *task,
5360 struct perf_event_context *task_ctx,
5361 int new)
5362{
5363 struct perf_task_event task_event;
5364
5365 if (!atomic_read(&nr_comm_events) &&
5366 !atomic_read(&nr_mmap_events) &&
5367 !atomic_read(&nr_task_events))
5368 return;
5369
5370 task_event = (struct perf_task_event){
5371 .task = task,
5372 .task_ctx = task_ctx,
5373 .event_id = {
5374 .header = {
5375 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5376 .misc = 0,
5377 .size = sizeof(task_event.event_id),
5378 },
5379 /* .pid */
5380 /* .ppid */
5381 /* .tid */
5382 /* .ptid */
Peter Zijlstra34f43922015-02-20 14:05:38 +01005383 /* .time */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005384 },
5385 };
5386
Jiri Olsa67516842013-07-09 18:56:31 +02005387 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005388 &task_event,
5389 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005390}
5391
5392void perf_event_fork(struct task_struct *task)
5393{
5394 perf_event_task(task, NULL, 1);
5395}
5396
5397/*
5398 * comm tracking
5399 */
5400
5401struct perf_comm_event {
5402 struct task_struct *task;
5403 char *comm;
5404 int comm_size;
5405
5406 struct {
5407 struct perf_event_header header;
5408
5409 u32 pid;
5410 u32 tid;
5411 } event_id;
5412};
5413
Jiri Olsa67516842013-07-09 18:56:31 +02005414static int perf_event_comm_match(struct perf_event *event)
5415{
5416 return event->attr.comm;
5417}
5418
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005419static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005420 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005421{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005422 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005424 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005426 int ret;
5427
Jiri Olsa67516842013-07-09 18:56:31 +02005428 if (!perf_event_comm_match(event))
5429 return;
5430
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005431 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5432 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005433 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005434
5435 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005436 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005437
5438 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5439 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5440
5441 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005442 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005444
5445 perf_event__output_id_sample(event, &handle, &sample);
5446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005447 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005448out:
5449 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005450}
5451
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452static void perf_event_comm_event(struct perf_comm_event *comm_event)
5453{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005454 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005455 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456
5457 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005458 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005459 size = ALIGN(strlen(comm)+1, sizeof(u64));
5460
5461 comm_event->comm = comm;
5462 comm_event->comm_size = size;
5463
5464 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005465
Jiri Olsa67516842013-07-09 18:56:31 +02005466 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005467 comm_event,
5468 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469}
5470
Adrian Hunter82b89772014-05-28 11:45:04 +03005471void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005472{
5473 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005474
5475 if (!atomic_read(&nr_comm_events))
5476 return;
5477
5478 comm_event = (struct perf_comm_event){
5479 .task = task,
5480 /* .comm */
5481 /* .comm_size */
5482 .event_id = {
5483 .header = {
5484 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005485 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005486 /* .size */
5487 },
5488 /* .pid */
5489 /* .tid */
5490 },
5491 };
5492
5493 perf_event_comm_event(&comm_event);
5494}
5495
5496/*
5497 * mmap tracking
5498 */
5499
5500struct perf_mmap_event {
5501 struct vm_area_struct *vma;
5502
5503 const char *file_name;
5504 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005505 int maj, min;
5506 u64 ino;
5507 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005508 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005509
5510 struct {
5511 struct perf_event_header header;
5512
5513 u32 pid;
5514 u32 tid;
5515 u64 start;
5516 u64 len;
5517 u64 pgoff;
5518 } event_id;
5519};
5520
Jiri Olsa67516842013-07-09 18:56:31 +02005521static int perf_event_mmap_match(struct perf_event *event,
5522 void *data)
5523{
5524 struct perf_mmap_event *mmap_event = data;
5525 struct vm_area_struct *vma = mmap_event->vma;
5526 int executable = vma->vm_flags & VM_EXEC;
5527
5528 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005529 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005530}
5531
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005533 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005534{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005535 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005536 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005537 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005538 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005539 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005540
Jiri Olsa67516842013-07-09 18:56:31 +02005541 if (!perf_event_mmap_match(event, data))
5542 return;
5543
Stephane Eranian13d7a242013-08-21 12:10:24 +02005544 if (event->attr.mmap2) {
5545 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5546 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5547 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5548 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005549 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005550 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5551 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005552 }
5553
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005554 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5555 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005556 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005558 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005559
5560 mmap_event->event_id.pid = perf_event_pid(event, current);
5561 mmap_event->event_id.tid = perf_event_tid(event, current);
5562
5563 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005564
5565 if (event->attr.mmap2) {
5566 perf_output_put(&handle, mmap_event->maj);
5567 perf_output_put(&handle, mmap_event->min);
5568 perf_output_put(&handle, mmap_event->ino);
5569 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005570 perf_output_put(&handle, mmap_event->prot);
5571 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005572 }
5573
Frederic Weisbecker76369132011-05-19 19:55:04 +02005574 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005575 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005576
5577 perf_event__output_id_sample(event, &handle, &sample);
5578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005579 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005580out:
5581 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005582}
5583
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5585{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005586 struct vm_area_struct *vma = mmap_event->vma;
5587 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005588 int maj = 0, min = 0;
5589 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005590 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005591 unsigned int size;
5592 char tmp[16];
5593 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005594 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005595
5596 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005597 struct inode *inode;
5598 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005599
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005600 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005602 name = "//enomem";
5603 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005604 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005605 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005606 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005607 * need to add enough zero bytes after the string to handle
5608 * the 64bit alignment we do later.
5609 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005610 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005611 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005612 name = "//toolong";
5613 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005614 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005615 inode = file_inode(vma->vm_file);
5616 dev = inode->i_sb->s_dev;
5617 ino = inode->i_ino;
5618 gen = inode->i_generation;
5619 maj = MAJOR(dev);
5620 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005621
5622 if (vma->vm_flags & VM_READ)
5623 prot |= PROT_READ;
5624 if (vma->vm_flags & VM_WRITE)
5625 prot |= PROT_WRITE;
5626 if (vma->vm_flags & VM_EXEC)
5627 prot |= PROT_EXEC;
5628
5629 if (vma->vm_flags & VM_MAYSHARE)
5630 flags = MAP_SHARED;
5631 else
5632 flags = MAP_PRIVATE;
5633
5634 if (vma->vm_flags & VM_DENYWRITE)
5635 flags |= MAP_DENYWRITE;
5636 if (vma->vm_flags & VM_MAYEXEC)
5637 flags |= MAP_EXECUTABLE;
5638 if (vma->vm_flags & VM_LOCKED)
5639 flags |= MAP_LOCKED;
5640 if (vma->vm_flags & VM_HUGETLB)
5641 flags |= MAP_HUGETLB;
5642
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005643 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005645 if (vma->vm_ops && vma->vm_ops->name) {
5646 name = (char *) vma->vm_ops->name(vma);
5647 if (name)
5648 goto cpy_name;
5649 }
5650
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005651 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005652 if (name)
5653 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005654
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005655 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005656 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005657 name = "[heap]";
5658 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005659 }
5660 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005661 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005662 name = "[stack]";
5663 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005664 }
5665
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005666 name = "//anon";
5667 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005668 }
5669
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005670cpy_name:
5671 strlcpy(tmp, name, sizeof(tmp));
5672 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005673got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005674 /*
5675 * Since our buffer works in 8 byte units we need to align our string
5676 * size to a multiple of 8. However, we must guarantee the tail end is
5677 * zero'd out to avoid leaking random bits to userspace.
5678 */
5679 size = strlen(name)+1;
5680 while (!IS_ALIGNED(size, sizeof(u64)))
5681 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682
5683 mmap_event->file_name = name;
5684 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005685 mmap_event->maj = maj;
5686 mmap_event->min = min;
5687 mmap_event->ino = ino;
5688 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005689 mmap_event->prot = prot;
5690 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005691
Stephane Eranian2fe85422013-01-24 16:10:39 +01005692 if (!(vma->vm_flags & VM_EXEC))
5693 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5694
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5696
Jiri Olsa67516842013-07-09 18:56:31 +02005697 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005698 mmap_event,
5699 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700
5701 kfree(buf);
5702}
5703
Eric B Munson3af9e852010-05-18 15:30:49 +01005704void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005705{
5706 struct perf_mmap_event mmap_event;
5707
5708 if (!atomic_read(&nr_mmap_events))
5709 return;
5710
5711 mmap_event = (struct perf_mmap_event){
5712 .vma = vma,
5713 /* .file_name */
5714 /* .file_size */
5715 .event_id = {
5716 .header = {
5717 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005718 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005719 /* .size */
5720 },
5721 /* .pid */
5722 /* .tid */
5723 .start = vma->vm_start,
5724 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005725 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005727 /* .maj (attr_mmap2 only) */
5728 /* .min (attr_mmap2 only) */
5729 /* .ino (attr_mmap2 only) */
5730 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005731 /* .prot (attr_mmap2 only) */
5732 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005733 };
5734
5735 perf_event_mmap_event(&mmap_event);
5736}
5737
5738/*
5739 * IRQ throttle logging
5740 */
5741
5742static void perf_log_throttle(struct perf_event *event, int enable)
5743{
5744 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005745 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005746 int ret;
5747
5748 struct {
5749 struct perf_event_header header;
5750 u64 time;
5751 u64 id;
5752 u64 stream_id;
5753 } throttle_event = {
5754 .header = {
5755 .type = PERF_RECORD_THROTTLE,
5756 .misc = 0,
5757 .size = sizeof(throttle_event),
5758 },
Peter Zijlstra34f43922015-02-20 14:05:38 +01005759 .time = perf_event_clock(event),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760 .id = primary_event_id(event),
5761 .stream_id = event->id,
5762 };
5763
5764 if (enable)
5765 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5766
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005767 perf_event_header__init_id(&throttle_event.header, &sample, event);
5768
5769 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005770 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005771 if (ret)
5772 return;
5773
5774 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005775 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005776 perf_output_end(&handle);
5777}
5778
5779/*
5780 * Generic event overflow handling, sampling.
5781 */
5782
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005783static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005784 int throttle, struct perf_sample_data *data,
5785 struct pt_regs *regs)
5786{
5787 int events = atomic_read(&event->event_limit);
5788 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005789 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005790 int ret = 0;
5791
Peter Zijlstra96398822010-11-24 18:55:29 +01005792 /*
5793 * Non-sampling counters might still use the PMI to fold short
5794 * hardware counters, ignore those.
5795 */
5796 if (unlikely(!is_sampling_event(event)))
5797 return 0;
5798
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005799 seq = __this_cpu_read(perf_throttled_seq);
5800 if (seq != hwc->interrupts_seq) {
5801 hwc->interrupts_seq = seq;
5802 hwc->interrupts = 1;
5803 } else {
5804 hwc->interrupts++;
5805 if (unlikely(throttle
5806 && hwc->interrupts >= max_samples_per_tick)) {
5807 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005808 hwc->interrupts = MAX_INTERRUPTS;
5809 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005810 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811 ret = 1;
5812 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005813 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005814
5815 if (event->attr.freq) {
5816 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005817 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005818
Peter Zijlstraabd50712010-01-26 18:50:16 +01005819 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820
Peter Zijlstraabd50712010-01-26 18:50:16 +01005821 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005822 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005823 }
5824
5825 /*
5826 * XXX event_limit might not quite work as expected on inherited
5827 * events
5828 */
5829
5830 event->pending_kill = POLL_IN;
5831 if (events && atomic_dec_and_test(&event->event_limit)) {
5832 ret = 1;
5833 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005834 event->pending_disable = 1;
5835 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005836 }
5837
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005838 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005839 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005840 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005841 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005842
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005843 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005844 event->pending_wakeup = 1;
5845 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005846 }
5847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005848 return ret;
5849}
5850
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005851int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005852 struct perf_sample_data *data,
5853 struct pt_regs *regs)
5854{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005855 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005856}
5857
5858/*
5859 * Generic software event infrastructure
5860 */
5861
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005862struct swevent_htable {
5863 struct swevent_hlist *swevent_hlist;
5864 struct mutex hlist_mutex;
5865 int hlist_refcount;
5866
5867 /* Recursion avoidance in each contexts */
5868 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005869
5870 /* Keeps track of cpu being initialized/exited */
5871 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005872};
5873
5874static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5875
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005876/*
5877 * We directly increment event->count and keep a second value in
5878 * event->hw.period_left to count intervals. This period event
5879 * is kept in the range [-sample_period, 0] so that we can use the
5880 * sign as trigger.
5881 */
5882
Jiri Olsaab573842013-05-01 17:25:44 +02005883u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005884{
5885 struct hw_perf_event *hwc = &event->hw;
5886 u64 period = hwc->last_period;
5887 u64 nr, offset;
5888 s64 old, val;
5889
5890 hwc->last_period = hwc->sample_period;
5891
5892again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005893 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005894 if (val < 0)
5895 return 0;
5896
5897 nr = div64_u64(period + val, period);
5898 offset = nr * period;
5899 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005900 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901 goto again;
5902
5903 return nr;
5904}
5905
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005906static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005907 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908 struct pt_regs *regs)
5909{
5910 struct hw_perf_event *hwc = &event->hw;
5911 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005912
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005913 if (!overflow)
5914 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005915
5916 if (hwc->interrupts == MAX_INTERRUPTS)
5917 return;
5918
5919 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005920 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005921 data, regs)) {
5922 /*
5923 * We inhibit the overflow from happening when
5924 * hwc->interrupts == MAX_INTERRUPTS.
5925 */
5926 break;
5927 }
5928 throttle = 1;
5929 }
5930}
5931
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005932static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005933 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005934 struct pt_regs *regs)
5935{
5936 struct hw_perf_event *hwc = &event->hw;
5937
Peter Zijlstrae7850592010-05-21 14:43:08 +02005938 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005939
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005940 if (!regs)
5941 return;
5942
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005943 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005944 return;
5945
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005946 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5947 data->period = nr;
5948 return perf_swevent_overflow(event, 1, data, regs);
5949 } else
5950 data->period = event->hw.last_period;
5951
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005952 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005953 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005954
Peter Zijlstrae7850592010-05-21 14:43:08 +02005955 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005956 return;
5957
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005958 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959}
5960
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005961static int perf_exclude_event(struct perf_event *event,
5962 struct pt_regs *regs)
5963{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005964 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005965 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005966
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005967 if (regs) {
5968 if (event->attr.exclude_user && user_mode(regs))
5969 return 1;
5970
5971 if (event->attr.exclude_kernel && !user_mode(regs))
5972 return 1;
5973 }
5974
5975 return 0;
5976}
5977
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005978static int perf_swevent_match(struct perf_event *event,
5979 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005980 u32 event_id,
5981 struct perf_sample_data *data,
5982 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005983{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984 if (event->attr.type != type)
5985 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005986
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005987 if (event->attr.config != event_id)
5988 return 0;
5989
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005990 if (perf_exclude_event(event, regs))
5991 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005992
5993 return 1;
5994}
5995
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005996static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005997{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005998 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005999
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006000 return hash_64(val, SWEVENT_HLIST_BITS);
6001}
6002
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006003static inline struct hlist_head *
6004__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006005{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006006 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006007
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006008 return &hlist->heads[hash];
6009}
6010
6011/* For the read side: events when they trigger */
6012static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006013find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006014{
6015 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006016
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006017 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006018 if (!hlist)
6019 return NULL;
6020
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006021 return __find_swevent_head(hlist, type, event_id);
6022}
6023
6024/* For the event head insertion and removal in the hlist */
6025static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006026find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006027{
6028 struct swevent_hlist *hlist;
6029 u32 event_id = event->attr.config;
6030 u64 type = event->attr.type;
6031
6032 /*
6033 * Event scheduling is always serialized against hlist allocation
6034 * and release. Which makes the protected version suitable here.
6035 * The context lock guarantees that.
6036 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006037 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006038 lockdep_is_held(&event->ctx->lock));
6039 if (!hlist)
6040 return NULL;
6041
6042 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006043}
6044
6045static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006046 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006047 struct perf_sample_data *data,
6048 struct pt_regs *regs)
6049{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006050 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006051 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006052 struct hlist_head *head;
6053
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006054 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006055 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006056 if (!head)
6057 goto end;
6058
Sasha Levinb67bfe02013-02-27 17:06:00 -08006059 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006060 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006061 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006062 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006063end:
6064 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006065}
6066
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006067DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6068
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006069int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006070{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006071 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006072
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006073 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006074}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006075EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006076
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006077inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006078{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006079 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006080
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006081 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006082}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006083
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006084void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006085{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006086 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006087
6088 if (WARN_ON_ONCE(!regs))
6089 return;
6090
6091 perf_sample_data_init(&data, addr, 0);
6092 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6093}
6094
6095void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6096{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006097 int rctx;
6098
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006099 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006100 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006101 if (unlikely(rctx < 0))
6102 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006104 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006105
6106 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006107fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006108 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006109}
6110
6111static void perf_swevent_read(struct perf_event *event)
6112{
6113}
6114
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006115static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006116{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006117 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006119 struct hlist_head *head;
6120
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006121 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006122 hwc->last_period = hwc->sample_period;
6123 perf_swevent_set_period(event);
6124 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006125
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006126 hwc->state = !(flags & PERF_EF_START);
6127
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006128 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02006129 if (!head) {
6130 /*
6131 * We can race with cpu hotplug code. Do not
6132 * WARN if the cpu just got unplugged.
6133 */
6134 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006135 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02006136 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006137
6138 hlist_add_head_rcu(&event->hlist_entry, head);
Shaohua Li6a694a62015-02-05 15:55:32 -08006139 perf_event_update_userpage(event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006140
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006141 return 0;
6142}
6143
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006144static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006145{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006146 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006147}
6148
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006149static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006150{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006151 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006152}
6153
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006154static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006155{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006156 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006157}
6158
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006159/* Deref the hlist from the update side */
6160static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006161swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006162{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006163 return rcu_dereference_protected(swhash->swevent_hlist,
6164 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006165}
6166
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006167static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006168{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006169 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006170
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006171 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006172 return;
6173
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006174 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006175 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006176}
6177
6178static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6179{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006180 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006181
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006182 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006183
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006184 if (!--swhash->hlist_refcount)
6185 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006186
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006187 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006188}
6189
6190static void swevent_hlist_put(struct perf_event *event)
6191{
6192 int cpu;
6193
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006194 for_each_possible_cpu(cpu)
6195 swevent_hlist_put_cpu(event, cpu);
6196}
6197
6198static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6199{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006200 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006201 int err = 0;
6202
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006203 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006204
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006205 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006206 struct swevent_hlist *hlist;
6207
6208 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6209 if (!hlist) {
6210 err = -ENOMEM;
6211 goto exit;
6212 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006213 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006214 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006215 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006216exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006217 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006218
6219 return err;
6220}
6221
6222static int swevent_hlist_get(struct perf_event *event)
6223{
6224 int err;
6225 int cpu, failed_cpu;
6226
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006227 get_online_cpus();
6228 for_each_possible_cpu(cpu) {
6229 err = swevent_hlist_get_cpu(event, cpu);
6230 if (err) {
6231 failed_cpu = cpu;
6232 goto fail;
6233 }
6234 }
6235 put_online_cpus();
6236
6237 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006238fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006239 for_each_possible_cpu(cpu) {
6240 if (cpu == failed_cpu)
6241 break;
6242 swevent_hlist_put_cpu(event, cpu);
6243 }
6244
6245 put_online_cpus();
6246 return err;
6247}
6248
Ingo Molnarc5905af2012-02-24 08:31:31 +01006249struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006250
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006251static void sw_perf_event_destroy(struct perf_event *event)
6252{
6253 u64 event_id = event->attr.config;
6254
6255 WARN_ON(event->parent);
6256
Ingo Molnarc5905af2012-02-24 08:31:31 +01006257 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006258 swevent_hlist_put(event);
6259}
6260
6261static int perf_swevent_init(struct perf_event *event)
6262{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006263 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006264
6265 if (event->attr.type != PERF_TYPE_SOFTWARE)
6266 return -ENOENT;
6267
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006268 /*
6269 * no branch sampling for software events
6270 */
6271 if (has_branch_stack(event))
6272 return -EOPNOTSUPP;
6273
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006274 switch (event_id) {
6275 case PERF_COUNT_SW_CPU_CLOCK:
6276 case PERF_COUNT_SW_TASK_CLOCK:
6277 return -ENOENT;
6278
6279 default:
6280 break;
6281 }
6282
Dan Carpenterce677832010-10-24 21:50:42 +02006283 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006284 return -ENOENT;
6285
6286 if (!event->parent) {
6287 int err;
6288
6289 err = swevent_hlist_get(event);
6290 if (err)
6291 return err;
6292
Ingo Molnarc5905af2012-02-24 08:31:31 +01006293 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006294 event->destroy = sw_perf_event_destroy;
6295 }
6296
6297 return 0;
6298}
6299
6300static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006301 .task_ctx_nr = perf_sw_context,
6302
Peter Zijlstra34f43922015-02-20 14:05:38 +01006303 .capabilities = PERF_PMU_CAP_NO_NMI,
6304
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006305 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006306 .add = perf_swevent_add,
6307 .del = perf_swevent_del,
6308 .start = perf_swevent_start,
6309 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006310 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006311};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006312
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006313#ifdef CONFIG_EVENT_TRACING
6314
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006315static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006316 struct perf_sample_data *data)
6317{
6318 void *record = data->raw->data;
6319
6320 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6321 return 1;
6322 return 0;
6323}
6324
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006325static int perf_tp_event_match(struct perf_event *event,
6326 struct perf_sample_data *data,
6327 struct pt_regs *regs)
6328{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006329 if (event->hw.state & PERF_HES_STOPPED)
6330 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006331 /*
6332 * All tracepoints are from kernel-space.
6333 */
6334 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006335 return 0;
6336
6337 if (!perf_tp_filter_match(event, data))
6338 return 0;
6339
6340 return 1;
6341}
6342
6343void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006344 struct pt_regs *regs, struct hlist_head *head, int rctx,
6345 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006346{
6347 struct perf_sample_data data;
6348 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006349
6350 struct perf_raw_record raw = {
6351 .size = entry_size,
6352 .data = record,
6353 };
6354
Robert Richterfd0d0002012-04-02 20:19:08 +02006355 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006356 data.raw = &raw;
6357
Sasha Levinb67bfe02013-02-27 17:06:00 -08006358 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006359 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006360 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006361 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006362
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006363 /*
6364 * If we got specified a target task, also iterate its context and
6365 * deliver this event there too.
6366 */
6367 if (task && task != current) {
6368 struct perf_event_context *ctx;
6369 struct trace_entry *entry = record;
6370
6371 rcu_read_lock();
6372 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6373 if (!ctx)
6374 goto unlock;
6375
6376 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6377 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6378 continue;
6379 if (event->attr.config != entry->type)
6380 continue;
6381 if (perf_tp_event_match(event, &data, regs))
6382 perf_swevent_event(event, count, &data, regs);
6383 }
6384unlock:
6385 rcu_read_unlock();
6386 }
6387
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006388 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006389}
6390EXPORT_SYMBOL_GPL(perf_tp_event);
6391
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006392static void tp_perf_event_destroy(struct perf_event *event)
6393{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006394 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006395}
6396
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006397static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006398{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006399 int err;
6400
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006401 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6402 return -ENOENT;
6403
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006404 /*
6405 * no branch sampling for tracepoint events
6406 */
6407 if (has_branch_stack(event))
6408 return -EOPNOTSUPP;
6409
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006410 err = perf_trace_init(event);
6411 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006412 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006413
6414 event->destroy = tp_perf_event_destroy;
6415
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006416 return 0;
6417}
6418
6419static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006420 .task_ctx_nr = perf_sw_context,
6421
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006422 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006423 .add = perf_trace_add,
6424 .del = perf_trace_del,
6425 .start = perf_swevent_start,
6426 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006427 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006428};
6429
6430static inline void perf_tp_register(void)
6431{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006432 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006433}
Li Zefan6fb29152009-10-15 11:21:42 +08006434
6435static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6436{
6437 char *filter_str;
6438 int ret;
6439
6440 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6441 return -EINVAL;
6442
6443 filter_str = strndup_user(arg, PAGE_SIZE);
6444 if (IS_ERR(filter_str))
6445 return PTR_ERR(filter_str);
6446
6447 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6448
6449 kfree(filter_str);
6450 return ret;
6451}
6452
6453static void perf_event_free_filter(struct perf_event *event)
6454{
6455 ftrace_profile_free_filter(event);
6456}
6457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006458#else
Li Zefan6fb29152009-10-15 11:21:42 +08006459
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006460static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006461{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006462}
Li Zefan6fb29152009-10-15 11:21:42 +08006463
6464static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6465{
6466 return -ENOENT;
6467}
6468
6469static void perf_event_free_filter(struct perf_event *event)
6470{
6471}
6472
Li Zefan07b139c2009-12-21 14:27:35 +08006473#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006474
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006475#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006476void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006477{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006478 struct perf_sample_data sample;
6479 struct pt_regs *regs = data;
6480
Robert Richterfd0d0002012-04-02 20:19:08 +02006481 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006482
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006483 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006484 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006485}
6486#endif
6487
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006488/*
6489 * hrtimer based swevent callback
6490 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006491
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006492static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006493{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006494 enum hrtimer_restart ret = HRTIMER_RESTART;
6495 struct perf_sample_data data;
6496 struct pt_regs *regs;
6497 struct perf_event *event;
6498 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006499
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006500 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006501
6502 if (event->state != PERF_EVENT_STATE_ACTIVE)
6503 return HRTIMER_NORESTART;
6504
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006505 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006506
Robert Richterfd0d0002012-04-02 20:19:08 +02006507 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006508 regs = get_irq_regs();
6509
6510 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006511 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006512 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006513 ret = HRTIMER_NORESTART;
6514 }
6515
6516 period = max_t(u64, 10000, event->hw.sample_period);
6517 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6518
6519 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006520}
6521
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006522static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006523{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006524 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006525 s64 period;
6526
6527 if (!is_sampling_event(event))
6528 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006529
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006530 period = local64_read(&hwc->period_left);
6531 if (period) {
6532 if (period < 0)
6533 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006534
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006535 local64_set(&hwc->period_left, 0);
6536 } else {
6537 period = max_t(u64, 10000, hwc->sample_period);
6538 }
6539 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006540 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006541 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006542}
6543
6544static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6545{
6546 struct hw_perf_event *hwc = &event->hw;
6547
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006548 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006549 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006550 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006551
6552 hrtimer_cancel(&hwc->hrtimer);
6553 }
6554}
6555
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006556static void perf_swevent_init_hrtimer(struct perf_event *event)
6557{
6558 struct hw_perf_event *hwc = &event->hw;
6559
6560 if (!is_sampling_event(event))
6561 return;
6562
6563 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6564 hwc->hrtimer.function = perf_swevent_hrtimer;
6565
6566 /*
6567 * Since hrtimers have a fixed rate, we can do a static freq->period
6568 * mapping and avoid the whole period adjust feedback stuff.
6569 */
6570 if (event->attr.freq) {
6571 long freq = event->attr.sample_freq;
6572
6573 event->attr.sample_period = NSEC_PER_SEC / freq;
6574 hwc->sample_period = event->attr.sample_period;
6575 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006576 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006577 event->attr.freq = 0;
6578 }
6579}
6580
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006581/*
6582 * Software event: cpu wall time clock
6583 */
6584
6585static void cpu_clock_event_update(struct perf_event *event)
6586{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006587 s64 prev;
6588 u64 now;
6589
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006590 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006591 prev = local64_xchg(&event->hw.prev_count, now);
6592 local64_add(now - prev, &event->count);
6593}
6594
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006595static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006596{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006597 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006598 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006599}
6600
6601static void cpu_clock_event_stop(struct perf_event *event, int flags)
6602{
6603 perf_swevent_cancel_hrtimer(event);
6604 cpu_clock_event_update(event);
6605}
6606
6607static int cpu_clock_event_add(struct perf_event *event, int flags)
6608{
6609 if (flags & PERF_EF_START)
6610 cpu_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08006611 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006612
6613 return 0;
6614}
6615
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006616static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006617{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006618 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006619}
6620
6621static void cpu_clock_event_read(struct perf_event *event)
6622{
6623 cpu_clock_event_update(event);
6624}
6625
6626static int cpu_clock_event_init(struct perf_event *event)
6627{
6628 if (event->attr.type != PERF_TYPE_SOFTWARE)
6629 return -ENOENT;
6630
6631 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6632 return -ENOENT;
6633
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006634 /*
6635 * no branch sampling for software events
6636 */
6637 if (has_branch_stack(event))
6638 return -EOPNOTSUPP;
6639
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006640 perf_swevent_init_hrtimer(event);
6641
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006642 return 0;
6643}
6644
6645static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006646 .task_ctx_nr = perf_sw_context,
6647
Peter Zijlstra34f43922015-02-20 14:05:38 +01006648 .capabilities = PERF_PMU_CAP_NO_NMI,
6649
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006650 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006651 .add = cpu_clock_event_add,
6652 .del = cpu_clock_event_del,
6653 .start = cpu_clock_event_start,
6654 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006655 .read = cpu_clock_event_read,
6656};
6657
6658/*
6659 * Software event: task time clock
6660 */
6661
6662static void task_clock_event_update(struct perf_event *event, u64 now)
6663{
6664 u64 prev;
6665 s64 delta;
6666
6667 prev = local64_xchg(&event->hw.prev_count, now);
6668 delta = now - prev;
6669 local64_add(delta, &event->count);
6670}
6671
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006672static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006673{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006674 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006675 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006676}
6677
6678static void task_clock_event_stop(struct perf_event *event, int flags)
6679{
6680 perf_swevent_cancel_hrtimer(event);
6681 task_clock_event_update(event, event->ctx->time);
6682}
6683
6684static int task_clock_event_add(struct perf_event *event, int flags)
6685{
6686 if (flags & PERF_EF_START)
6687 task_clock_event_start(event, flags);
Shaohua Li6a694a62015-02-05 15:55:32 -08006688 perf_event_update_userpage(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006689
6690 return 0;
6691}
6692
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006693static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006694{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006695 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006696}
6697
6698static void task_clock_event_read(struct perf_event *event)
6699{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006700 u64 now = perf_clock();
6701 u64 delta = now - event->ctx->timestamp;
6702 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006703
6704 task_clock_event_update(event, time);
6705}
6706
6707static int task_clock_event_init(struct perf_event *event)
6708{
6709 if (event->attr.type != PERF_TYPE_SOFTWARE)
6710 return -ENOENT;
6711
6712 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6713 return -ENOENT;
6714
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006715 /*
6716 * no branch sampling for software events
6717 */
6718 if (has_branch_stack(event))
6719 return -EOPNOTSUPP;
6720
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006721 perf_swevent_init_hrtimer(event);
6722
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006723 return 0;
6724}
6725
6726static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006727 .task_ctx_nr = perf_sw_context,
6728
Peter Zijlstra34f43922015-02-20 14:05:38 +01006729 .capabilities = PERF_PMU_CAP_NO_NMI,
6730
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006731 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006732 .add = task_clock_event_add,
6733 .del = task_clock_event_del,
6734 .start = task_clock_event_start,
6735 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006736 .read = task_clock_event_read,
6737};
6738
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006739static void perf_pmu_nop_void(struct pmu *pmu)
6740{
6741}
6742
6743static int perf_pmu_nop_int(struct pmu *pmu)
6744{
6745 return 0;
6746}
6747
6748static void perf_pmu_start_txn(struct pmu *pmu)
6749{
6750 perf_pmu_disable(pmu);
6751}
6752
6753static int perf_pmu_commit_txn(struct pmu *pmu)
6754{
6755 perf_pmu_enable(pmu);
6756 return 0;
6757}
6758
6759static void perf_pmu_cancel_txn(struct pmu *pmu)
6760{
6761 perf_pmu_enable(pmu);
6762}
6763
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006764static int perf_event_idx_default(struct perf_event *event)
6765{
Peter Zijlstrac719f562014-10-21 11:10:21 +02006766 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006767}
6768
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006769/*
6770 * Ensures all contexts with the same task_ctx_nr have the same
6771 * pmu_cpu_context too.
6772 */
Mark Rutland9e317042014-02-10 17:44:18 +00006773static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006774{
6775 struct pmu *pmu;
6776
6777 if (ctxn < 0)
6778 return NULL;
6779
6780 list_for_each_entry(pmu, &pmus, entry) {
6781 if (pmu->task_ctx_nr == ctxn)
6782 return pmu->pmu_cpu_context;
6783 }
6784
6785 return NULL;
6786}
6787
Peter Zijlstra51676952010-12-07 14:18:20 +01006788static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006789{
Peter Zijlstra51676952010-12-07 14:18:20 +01006790 int cpu;
6791
6792 for_each_possible_cpu(cpu) {
6793 struct perf_cpu_context *cpuctx;
6794
6795 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6796
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006797 if (cpuctx->unique_pmu == old_pmu)
6798 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006799 }
6800}
6801
6802static void free_pmu_context(struct pmu *pmu)
6803{
6804 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006805
6806 mutex_lock(&pmus_lock);
6807 /*
6808 * Like a real lame refcount.
6809 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006810 list_for_each_entry(i, &pmus, entry) {
6811 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6812 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006813 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006814 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006815 }
6816
Peter Zijlstra51676952010-12-07 14:18:20 +01006817 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006818out:
6819 mutex_unlock(&pmus_lock);
6820}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006821static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006822
Peter Zijlstraabe43402010-11-17 23:17:37 +01006823static ssize_t
6824type_show(struct device *dev, struct device_attribute *attr, char *page)
6825{
6826 struct pmu *pmu = dev_get_drvdata(dev);
6827
6828 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6829}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006830static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006831
Stephane Eranian62b85632013-04-03 14:21:34 +02006832static ssize_t
6833perf_event_mux_interval_ms_show(struct device *dev,
6834 struct device_attribute *attr,
6835 char *page)
6836{
6837 struct pmu *pmu = dev_get_drvdata(dev);
6838
6839 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6840}
6841
6842static ssize_t
6843perf_event_mux_interval_ms_store(struct device *dev,
6844 struct device_attribute *attr,
6845 const char *buf, size_t count)
6846{
6847 struct pmu *pmu = dev_get_drvdata(dev);
6848 int timer, cpu, ret;
6849
6850 ret = kstrtoint(buf, 0, &timer);
6851 if (ret)
6852 return ret;
6853
6854 if (timer < 1)
6855 return -EINVAL;
6856
6857 /* same value, noting to do */
6858 if (timer == pmu->hrtimer_interval_ms)
6859 return count;
6860
6861 pmu->hrtimer_interval_ms = timer;
6862
6863 /* update all cpuctx for this PMU */
6864 for_each_possible_cpu(cpu) {
6865 struct perf_cpu_context *cpuctx;
6866 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6867 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6868
6869 if (hrtimer_active(&cpuctx->hrtimer))
6870 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6871 }
6872
6873 return count;
6874}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006875static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006876
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006877static struct attribute *pmu_dev_attrs[] = {
6878 &dev_attr_type.attr,
6879 &dev_attr_perf_event_mux_interval_ms.attr,
6880 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006881};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006882ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006883
6884static int pmu_bus_running;
6885static struct bus_type pmu_bus = {
6886 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006887 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006888};
6889
6890static void pmu_dev_release(struct device *dev)
6891{
6892 kfree(dev);
6893}
6894
6895static int pmu_dev_alloc(struct pmu *pmu)
6896{
6897 int ret = -ENOMEM;
6898
6899 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6900 if (!pmu->dev)
6901 goto out;
6902
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006903 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006904 device_initialize(pmu->dev);
6905 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6906 if (ret)
6907 goto free_dev;
6908
6909 dev_set_drvdata(pmu->dev, pmu);
6910 pmu->dev->bus = &pmu_bus;
6911 pmu->dev->release = pmu_dev_release;
6912 ret = device_add(pmu->dev);
6913 if (ret)
6914 goto free_dev;
6915
6916out:
6917 return ret;
6918
6919free_dev:
6920 put_device(pmu->dev);
6921 goto out;
6922}
6923
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006924static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006925static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006926
Mischa Jonker03d8e802013-06-04 11:45:48 +02006927int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006928{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006929 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006930
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006931 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006932 ret = -ENOMEM;
6933 pmu->pmu_disable_count = alloc_percpu(int);
6934 if (!pmu->pmu_disable_count)
6935 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006936
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006937 pmu->type = -1;
6938 if (!name)
6939 goto skip_type;
6940 pmu->name = name;
6941
6942 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006943 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6944 if (type < 0) {
6945 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006946 goto free_pdc;
6947 }
6948 }
6949 pmu->type = type;
6950
Peter Zijlstraabe43402010-11-17 23:17:37 +01006951 if (pmu_bus_running) {
6952 ret = pmu_dev_alloc(pmu);
6953 if (ret)
6954 goto free_idr;
6955 }
6956
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006957skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006958 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6959 if (pmu->pmu_cpu_context)
6960 goto got_cpu_context;
6961
Wei Yongjunc4814202013-04-12 11:05:54 +08006962 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006963 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6964 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006965 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006966
6967 for_each_possible_cpu(cpu) {
6968 struct perf_cpu_context *cpuctx;
6969
6970 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006971 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006972 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006973 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006974 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006975
6976 __perf_cpu_hrtimer_init(cpuctx, cpu);
6977
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006978 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006979 }
6980
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006981got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006982 if (!pmu->start_txn) {
6983 if (pmu->pmu_enable) {
6984 /*
6985 * If we have pmu_enable/pmu_disable calls, install
6986 * transaction stubs that use that to try and batch
6987 * hardware accesses.
6988 */
6989 pmu->start_txn = perf_pmu_start_txn;
6990 pmu->commit_txn = perf_pmu_commit_txn;
6991 pmu->cancel_txn = perf_pmu_cancel_txn;
6992 } else {
6993 pmu->start_txn = perf_pmu_nop_void;
6994 pmu->commit_txn = perf_pmu_nop_int;
6995 pmu->cancel_txn = perf_pmu_nop_void;
6996 }
6997 }
6998
6999 if (!pmu->pmu_enable) {
7000 pmu->pmu_enable = perf_pmu_nop_void;
7001 pmu->pmu_disable = perf_pmu_nop_void;
7002 }
7003
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01007004 if (!pmu->event_idx)
7005 pmu->event_idx = perf_event_idx_default;
7006
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007007 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007008 ret = 0;
7009unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007010 mutex_unlock(&pmus_lock);
7011
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007012 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007013
Peter Zijlstraabe43402010-11-17 23:17:37 +01007014free_dev:
7015 device_del(pmu->dev);
7016 put_device(pmu->dev);
7017
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007018free_idr:
7019 if (pmu->type >= PERF_TYPE_MAX)
7020 idr_remove(&pmu_idr, pmu->type);
7021
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007022free_pdc:
7023 free_percpu(pmu->pmu_disable_count);
7024 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007025}
Yan, Zhengc464c762014-03-18 16:56:41 +08007026EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007027
7028void perf_pmu_unregister(struct pmu *pmu)
7029{
7030 mutex_lock(&pmus_lock);
7031 list_del_rcu(&pmu->entry);
7032 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007033
7034 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007035 * We dereference the pmu list under both SRCU and regular RCU, so
7036 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007037 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007038 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007039 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007040
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007041 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007042 if (pmu->type >= PERF_TYPE_MAX)
7043 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007044 device_del(pmu->dev);
7045 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007046 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007047}
Yan, Zhengc464c762014-03-18 16:56:41 +08007048EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007049
Mark Rutlandcc34b982015-01-07 14:56:51 +00007050static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7051{
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007052 struct perf_event_context *ctx = NULL;
Mark Rutlandcc34b982015-01-07 14:56:51 +00007053 int ret;
7054
7055 if (!try_module_get(pmu->module))
7056 return -ENODEV;
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007057
7058 if (event->group_leader != event) {
7059 ctx = perf_event_ctx_lock(event->group_leader);
7060 BUG_ON(!ctx);
7061 }
7062
Mark Rutlandcc34b982015-01-07 14:56:51 +00007063 event->pmu = pmu;
7064 ret = pmu->event_init(event);
Peter Zijlstraccd41c82015-02-25 15:56:04 +01007065
7066 if (ctx)
7067 perf_event_ctx_unlock(event->group_leader, ctx);
7068
Mark Rutlandcc34b982015-01-07 14:56:51 +00007069 if (ret)
7070 module_put(pmu->module);
7071
7072 return ret;
7073}
7074
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007075struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007076{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007077 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007078 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007079 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007080
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007081 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007082
7083 rcu_read_lock();
7084 pmu = idr_find(&pmu_idr, event->attr.type);
7085 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007086 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007087 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007088 if (ret)
7089 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007090 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007091 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007092
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007093 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007094 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007095 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007096 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007097
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007098 if (ret != -ENOENT) {
7099 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007100 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007101 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007102 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007103 pmu = ERR_PTR(-ENOENT);
7104unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007105 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007106
7107 return pmu;
7108}
7109
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007110static void account_event_cpu(struct perf_event *event, int cpu)
7111{
7112 if (event->parent)
7113 return;
7114
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007115 if (is_cgroup_event(event))
7116 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7117}
7118
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007119static void account_event(struct perf_event *event)
7120{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007121 if (event->parent)
7122 return;
7123
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007124 if (event->attach_state & PERF_ATTACH_TASK)
7125 static_key_slow_inc(&perf_sched_events.key);
7126 if (event->attr.mmap || event->attr.mmap_data)
7127 atomic_inc(&nr_mmap_events);
7128 if (event->attr.comm)
7129 atomic_inc(&nr_comm_events);
7130 if (event->attr.task)
7131 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007132 if (event->attr.freq) {
7133 if (atomic_inc_return(&nr_freq_events) == 1)
7134 tick_nohz_full_kick_all();
7135 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007136 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007137 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007138 if (is_cgroup_event(event))
7139 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007140
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007141 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007142}
7143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007144/*
7145 * Allocate and initialize a event structure
7146 */
7147static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007148perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007149 struct task_struct *task,
7150 struct perf_event *group_leader,
7151 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007152 perf_overflow_handler_t overflow_handler,
Matt Fleming79dff512015-01-23 18:45:42 +00007153 void *context, int cgroup_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007155 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007156 struct perf_event *event;
7157 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007158 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007160 if ((unsigned)cpu >= nr_cpu_ids) {
7161 if (!task || cpu != -1)
7162 return ERR_PTR(-EINVAL);
7163 }
7164
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007165 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007166 if (!event)
7167 return ERR_PTR(-ENOMEM);
7168
7169 /*
7170 * Single events are their own group leaders, with an
7171 * empty sibling list:
7172 */
7173 if (!group_leader)
7174 group_leader = event;
7175
7176 mutex_init(&event->child_mutex);
7177 INIT_LIST_HEAD(&event->child_list);
7178
7179 INIT_LIST_HEAD(&event->group_entry);
7180 INIT_LIST_HEAD(&event->event_entry);
7181 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007182 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007183 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007184 INIT_HLIST_NODE(&event->hlist_entry);
7185
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007187 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007188 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007189
7190 mutex_init(&event->mmap_mutex);
7191
Al Viroa6fa9412012-08-20 14:59:25 +01007192 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007193 event->cpu = cpu;
7194 event->attr = *attr;
7195 event->group_leader = group_leader;
7196 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007197 event->oncpu = -1;
7198
7199 event->parent = parent_event;
7200
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007201 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007202 event->id = atomic64_inc_return(&perf_event_id);
7203
7204 event->state = PERF_EVENT_STATE_INACTIVE;
7205
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007206 if (task) {
7207 event->attach_state = PERF_ATTACH_TASK;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007208 /*
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007209 * XXX pmu::event_init needs to know what task to account to
7210 * and we cannot use the ctx information because we need the
7211 * pmu before we get a ctx.
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007212 */
Peter Zijlstra50f16a82015-03-05 22:10:19 +01007213 event->hw.target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007214 }
7215
Peter Zijlstra34f43922015-02-20 14:05:38 +01007216 event->clock = &local_clock;
7217 if (parent_event)
7218 event->clock = parent_event->clock;
7219
Avi Kivity4dc0da82011-06-29 18:42:35 +03007220 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007221 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007222 context = parent_event->overflow_handler_context;
7223 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007224
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007225 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007226 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007227
Jiri Olsa0231bb52013-02-01 11:23:45 +01007228 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007229
7230 pmu = NULL;
7231
7232 hwc = &event->hw;
7233 hwc->sample_period = attr->sample_period;
7234 if (attr->freq && attr->sample_freq)
7235 hwc->sample_period = 1;
7236 hwc->last_period = hwc->sample_period;
7237
Peter Zijlstrae7850592010-05-21 14:43:08 +02007238 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007239
7240 /*
7241 * we currently do not support PERF_FORMAT_GROUP on inherited events
7242 */
7243 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007244 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007245
Yan, Zhenga46a2302014-11-04 21:56:06 -05007246 if (!has_branch_stack(event))
7247 event->attr.branch_sample_type = 0;
7248
Matt Fleming79dff512015-01-23 18:45:42 +00007249 if (cgroup_fd != -1) {
7250 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
7251 if (err)
7252 goto err_ns;
7253 }
7254
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007255 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007256 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007257 goto err_ns;
7258 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007259 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007260 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007261 }
7262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007263 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007264 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7265 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007266 if (err)
7267 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01007268 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007269 }
7270
7271 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007272
7273err_pmu:
7274 if (event->destroy)
7275 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007276 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007277err_ns:
Matt Fleming79dff512015-01-23 18:45:42 +00007278 if (is_cgroup_event(event))
7279 perf_detach_cgroup(event);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007280 if (event->ns)
7281 put_pid_ns(event->ns);
7282 kfree(event);
7283
7284 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007285}
7286
7287static int perf_copy_attr(struct perf_event_attr __user *uattr,
7288 struct perf_event_attr *attr)
7289{
7290 u32 size;
7291 int ret;
7292
7293 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7294 return -EFAULT;
7295
7296 /*
7297 * zero the full structure, so that a short copy will be nice.
7298 */
7299 memset(attr, 0, sizeof(*attr));
7300
7301 ret = get_user(size, &uattr->size);
7302 if (ret)
7303 return ret;
7304
7305 if (size > PAGE_SIZE) /* silly large */
7306 goto err_size;
7307
7308 if (!size) /* abi compat */
7309 size = PERF_ATTR_SIZE_VER0;
7310
7311 if (size < PERF_ATTR_SIZE_VER0)
7312 goto err_size;
7313
7314 /*
7315 * If we're handed a bigger struct than we know of,
7316 * ensure all the unknown bits are 0 - i.e. new
7317 * user-space does not rely on any kernel feature
7318 * extensions we dont know about yet.
7319 */
7320 if (size > sizeof(*attr)) {
7321 unsigned char __user *addr;
7322 unsigned char __user *end;
7323 unsigned char val;
7324
7325 addr = (void __user *)uattr + sizeof(*attr);
7326 end = (void __user *)uattr + size;
7327
7328 for (; addr < end; addr++) {
7329 ret = get_user(val, addr);
7330 if (ret)
7331 return ret;
7332 if (val)
7333 goto err_size;
7334 }
7335 size = sizeof(*attr);
7336 }
7337
7338 ret = copy_from_user(attr, uattr, size);
7339 if (ret)
7340 return -EFAULT;
7341
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307342 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007343 return -EINVAL;
7344
7345 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7346 return -EINVAL;
7347
7348 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
7349 return -EINVAL;
7350
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007351 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
7352 u64 mask = attr->branch_sample_type;
7353
7354 /* only using defined bits */
7355 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
7356 return -EINVAL;
7357
7358 /* at least one branch bit must be set */
7359 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
7360 return -EINVAL;
7361
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007362 /* propagate priv level, when not set for branch */
7363 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
7364
7365 /* exclude_kernel checked on syscall entry */
7366 if (!attr->exclude_kernel)
7367 mask |= PERF_SAMPLE_BRANCH_KERNEL;
7368
7369 if (!attr->exclude_user)
7370 mask |= PERF_SAMPLE_BRANCH_USER;
7371
7372 if (!attr->exclude_hv)
7373 mask |= PERF_SAMPLE_BRANCH_HV;
7374 /*
7375 * adjust user setting (for HW filter setup)
7376 */
7377 attr->branch_sample_type = mask;
7378 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007379 /* privileged levels capture (kernel, hv): check permissions */
7380 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007381 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7382 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007383 }
Jiri Olsa40189942012-08-07 15:20:37 +02007384
Jiri Olsac5ebced2012-08-07 15:20:40 +02007385 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007386 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007387 if (ret)
7388 return ret;
7389 }
7390
7391 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7392 if (!arch_perf_have_user_stack_dump())
7393 return -ENOSYS;
7394
7395 /*
7396 * We have __u32 type for the size, but so far
7397 * we can only use __u16 as maximum due to the
7398 * __u16 sample size limit.
7399 */
7400 if (attr->sample_stack_user >= USHRT_MAX)
7401 ret = -EINVAL;
7402 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7403 ret = -EINVAL;
7404 }
Jiri Olsa40189942012-08-07 15:20:37 +02007405
Stephane Eranian60e23642014-09-24 13:48:37 +02007406 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
7407 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007408out:
7409 return ret;
7410
7411err_size:
7412 put_user(sizeof(*attr), &uattr->size);
7413 ret = -E2BIG;
7414 goto out;
7415}
7416
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007417static int
7418perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007419{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007420 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007421 int ret = -EINVAL;
7422
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007423 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007424 goto set;
7425
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007426 /* don't allow circular references */
7427 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007428 goto out;
7429
Peter Zijlstra0f139302010-05-20 14:35:15 +02007430 /*
7431 * Don't allow cross-cpu buffers
7432 */
7433 if (output_event->cpu != event->cpu)
7434 goto out;
7435
7436 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007437 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007438 */
7439 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7440 goto out;
7441
Peter Zijlstra34f43922015-02-20 14:05:38 +01007442 /*
7443 * Mixing clocks in the same buffer is trouble you don't need.
7444 */
7445 if (output_event->clock != event->clock)
7446 goto out;
7447
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007448set:
7449 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007450 /* Can't redirect output if we've got an active mmap() */
7451 if (atomic_read(&event->mmap_count))
7452 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007453
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007454 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007455 /* get the rb we want to redirect to */
7456 rb = ring_buffer_get(output_event);
7457 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007458 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007459 }
7460
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007461 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007463 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007464unlock:
7465 mutex_unlock(&event->mmap_mutex);
7466
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007467out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007468 return ret;
7469}
7470
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007471static void mutex_lock_double(struct mutex *a, struct mutex *b)
7472{
7473 if (b < a)
7474 swap(a, b);
7475
7476 mutex_lock(a);
7477 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
7478}
7479
Peter Zijlstra34f43922015-02-20 14:05:38 +01007480static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
7481{
7482 bool nmi_safe = false;
7483
7484 switch (clk_id) {
7485 case CLOCK_MONOTONIC:
7486 event->clock = &ktime_get_mono_fast_ns;
7487 nmi_safe = true;
7488 break;
7489
7490 case CLOCK_MONOTONIC_RAW:
7491 event->clock = &ktime_get_raw_fast_ns;
7492 nmi_safe = true;
7493 break;
7494
7495 case CLOCK_REALTIME:
7496 event->clock = &ktime_get_real_ns;
7497 break;
7498
7499 case CLOCK_BOOTTIME:
7500 event->clock = &ktime_get_boot_ns;
7501 break;
7502
7503 case CLOCK_TAI:
7504 event->clock = &ktime_get_tai_ns;
7505 break;
7506
7507 default:
7508 return -EINVAL;
7509 }
7510
7511 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
7512 return -EINVAL;
7513
7514 return 0;
7515}
7516
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007517/**
7518 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7519 *
7520 * @attr_uptr: event_id type attributes for monitoring/sampling
7521 * @pid: target pid
7522 * @cpu: target cpu
7523 * @group_fd: group leader event fd
7524 */
7525SYSCALL_DEFINE5(perf_event_open,
7526 struct perf_event_attr __user *, attr_uptr,
7527 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7528{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007529 struct perf_event *group_leader = NULL, *output_event = NULL;
7530 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007531 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007532 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007533 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007534 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007535 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007536 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007537 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007538 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007539 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007540 int f_flags = O_RDWR;
Matt Fleming79dff512015-01-23 18:45:42 +00007541 int cgroup_fd = -1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007542
7543 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007544 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007545 return -EINVAL;
7546
7547 err = perf_copy_attr(attr_uptr, &attr);
7548 if (err)
7549 return err;
7550
7551 if (!attr.exclude_kernel) {
7552 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7553 return -EACCES;
7554 }
7555
7556 if (attr.freq) {
7557 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7558 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007559 } else {
7560 if (attr.sample_period & (1ULL << 63))
7561 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007562 }
7563
Stephane Eraniane5d13672011-02-14 11:20:01 +02007564 /*
7565 * In cgroup mode, the pid argument is used to pass the fd
7566 * opened to the cgroup directory in cgroupfs. The cpu argument
7567 * designates the cpu on which to monitor threads from that
7568 * cgroup.
7569 */
7570 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7571 return -EINVAL;
7572
Yann Droneauda21b0b32014-01-05 21:36:33 +01007573 if (flags & PERF_FLAG_FD_CLOEXEC)
7574 f_flags |= O_CLOEXEC;
7575
7576 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007577 if (event_fd < 0)
7578 return event_fd;
7579
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007580 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007581 err = perf_fget_light(group_fd, &group);
7582 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007583 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007584 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007585 if (flags & PERF_FLAG_FD_OUTPUT)
7586 output_event = group_leader;
7587 if (flags & PERF_FLAG_FD_NO_GROUP)
7588 group_leader = NULL;
7589 }
7590
Stephane Eraniane5d13672011-02-14 11:20:01 +02007591 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007592 task = find_lively_task_by_vpid(pid);
7593 if (IS_ERR(task)) {
7594 err = PTR_ERR(task);
7595 goto err_group_fd;
7596 }
7597 }
7598
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007599 if (task && group_leader &&
7600 group_leader->attr.inherit != attr.inherit) {
7601 err = -EINVAL;
7602 goto err_task;
7603 }
7604
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007605 get_online_cpus();
7606
Matt Fleming79dff512015-01-23 18:45:42 +00007607 if (flags & PERF_FLAG_PID_CGROUP)
7608 cgroup_fd = pid;
7609
Avi Kivity4dc0da82011-06-29 18:42:35 +03007610 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00007611 NULL, NULL, cgroup_fd);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007612 if (IS_ERR(event)) {
7613 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007614 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007615 }
7616
Vince Weaver53b25332014-05-16 17:12:12 -04007617 if (is_sampling_event(event)) {
7618 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7619 err = -ENOTSUPP;
7620 goto err_alloc;
7621 }
7622 }
7623
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007624 account_event(event);
7625
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007626 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007627 * Special case software events and allow them to be part of
7628 * any hardware group.
7629 */
7630 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007631
Peter Zijlstra34f43922015-02-20 14:05:38 +01007632 if (attr.use_clockid) {
7633 err = perf_event_set_clock(event, attr.clockid);
7634 if (err)
7635 goto err_alloc;
7636 }
7637
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007638 if (group_leader &&
7639 (is_software_event(event) != is_software_event(group_leader))) {
7640 if (is_software_event(event)) {
7641 /*
7642 * If event and group_leader are not both a software
7643 * event, and event is, then group leader is not.
7644 *
7645 * Allow the addition of software events to !software
7646 * groups, this is safe because software events never
7647 * fail to schedule.
7648 */
7649 pmu = group_leader->pmu;
7650 } else if (is_software_event(group_leader) &&
7651 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7652 /*
7653 * In case the group is a pure software group, and we
7654 * try to add a hardware event, move the whole group to
7655 * the hardware context.
7656 */
7657 move_group = 1;
7658 }
7659 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007660
7661 /*
7662 * Get the target context (task or percpu):
7663 */
Yan, Zheng4af57ef282014-11-04 21:56:01 -05007664 ctx = find_get_context(pmu, task, event);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007665 if (IS_ERR(ctx)) {
7666 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007667 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007668 }
7669
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007670 if (task) {
7671 put_task_struct(task);
7672 task = NULL;
7673 }
7674
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007675 /*
7676 * Look up the group leader (we will attach this event to it):
7677 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007678 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007679 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007680
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007681 /*
7682 * Do not allow a recursive hierarchy (this new sibling
7683 * becoming part of another group-sibling):
7684 */
7685 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007686 goto err_context;
Peter Zijlstra34f43922015-02-20 14:05:38 +01007687
7688 /* All events in a group should have the same clock */
7689 if (group_leader->clock != event->clock)
7690 goto err_context;
7691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007692 /*
7693 * Do not allow to attach to a group in a different
7694 * task or CPU context:
7695 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007696 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01007697 /*
7698 * Make sure we're both on the same task, or both
7699 * per-cpu events.
7700 */
7701 if (group_leader->ctx->task != ctx->task)
7702 goto err_context;
7703
7704 /*
7705 * Make sure we're both events for the same CPU;
7706 * grouping events for different CPUs is broken; since
7707 * you can never concurrently schedule them anyhow.
7708 */
7709 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007710 goto err_context;
7711 } else {
7712 if (group_leader->ctx != ctx)
7713 goto err_context;
7714 }
7715
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007716 /*
7717 * Only a group leader can be exclusive or pinned
7718 */
7719 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007720 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007721 }
7722
7723 if (output_event) {
7724 err = perf_event_set_output(event, output_event);
7725 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007726 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007727 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007728
Yann Droneauda21b0b32014-01-05 21:36:33 +01007729 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7730 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007731 if (IS_ERR(event_file)) {
7732 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007733 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007734 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007735
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007736 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007737 gctx = group_leader->ctx;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007738
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007739 /*
7740 * See perf_event_ctx_lock() for comments on the details
7741 * of swizzling perf_event::ctx.
7742 */
7743 mutex_lock_double(&gctx->mutex, &ctx->mutex);
7744
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007745 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007746
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007747 list_for_each_entry(sibling, &group_leader->sibling_list,
7748 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007749 perf_remove_from_context(sibling, false);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007750 put_ctx(gctx);
7751 }
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007752 } else {
7753 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007754 }
7755
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007756 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007757
7758 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007759 /*
7760 * Wait for everybody to stop referencing the events through
7761 * the old lists, before installing it on new lists.
7762 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007763 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007764
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007765 /*
7766 * Install the group siblings before the group leader.
7767 *
7768 * Because a group leader will try and install the entire group
7769 * (through the sibling list, which is still in-tact), we can
7770 * end up with siblings installed in the wrong context.
7771 *
7772 * By installing siblings first we NO-OP because they're not
7773 * reachable through the group lists.
7774 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007775 list_for_each_entry(sibling, &group_leader->sibling_list,
7776 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007777 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01007778 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007779 get_ctx(ctx);
7780 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007781
7782 /*
7783 * Removing from the context ends up with disabled
7784 * event. What we want here is event in the initial
7785 * startup state, ready to be add into new context.
7786 */
7787 perf_event__state_init(group_leader);
7788 perf_install_in_context(ctx, group_leader, group_leader->cpu);
7789 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007790 }
7791
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007792 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007793 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007794
7795 if (move_group) {
7796 mutex_unlock(&gctx->mutex);
7797 put_ctx(gctx);
7798 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007799 mutex_unlock(&ctx->mutex);
7800
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007801 put_online_cpus();
7802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007803 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007804
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007805 mutex_lock(&current->perf_event_mutex);
7806 list_add_tail(&event->owner_entry, &current->perf_event_list);
7807 mutex_unlock(&current->perf_event_mutex);
7808
Peter Zijlstra8a495422010-05-27 15:47:49 +02007809 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007810 * Precalculate sample_data sizes
7811 */
7812 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007813 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007814
7815 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007816 * Drop the reference on the group_event after placing the
7817 * new event on the sibling_list. This ensures destruction
7818 * of the group leader will find the pointer to itself in
7819 * perf_group_detach().
7820 */
Al Viro2903ff02012-08-28 12:52:22 -04007821 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007822 fd_install(event_fd, event_file);
7823 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007824
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007825err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007826 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007827 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007828err_alloc:
7829 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007830err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007831 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007832err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007833 if (task)
7834 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007835err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007836 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007837err_fd:
7838 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007839 return err;
7840}
7841
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007842/**
7843 * perf_event_create_kernel_counter
7844 *
7845 * @attr: attributes of the counter to create
7846 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007847 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007848 */
7849struct perf_event *
7850perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007851 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007852 perf_overflow_handler_t overflow_handler,
7853 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007854{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007855 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007856 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007857 int err;
7858
7859 /*
7860 * Get the target context (task or percpu):
7861 */
7862
Avi Kivity4dc0da82011-06-29 18:42:35 +03007863 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
Matt Fleming79dff512015-01-23 18:45:42 +00007864 overflow_handler, context, -1);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007865 if (IS_ERR(event)) {
7866 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007867 goto err;
7868 }
7869
Jiri Olsaf8697762014-08-01 14:33:01 +02007870 /* Mark owner so we could distinguish it from user events. */
7871 event->owner = EVENT_OWNER_KERNEL;
7872
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007873 account_event(event);
7874
Yan, Zheng4af57ef282014-11-04 21:56:01 -05007875 ctx = find_get_context(event->pmu, task, event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007876 if (IS_ERR(ctx)) {
7877 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007878 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007879 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007880
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007881 WARN_ON_ONCE(ctx->parent_ctx);
7882 mutex_lock(&ctx->mutex);
7883 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007884 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007885 mutex_unlock(&ctx->mutex);
7886
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007887 return event;
7888
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007889err_free:
7890 free_event(event);
7891err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007892 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007893}
7894EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7895
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007896void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7897{
7898 struct perf_event_context *src_ctx;
7899 struct perf_event_context *dst_ctx;
7900 struct perf_event *event, *tmp;
7901 LIST_HEAD(events);
7902
7903 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7904 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7905
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007906 /*
7907 * See perf_event_ctx_lock() for comments on the details
7908 * of swizzling perf_event::ctx.
7909 */
7910 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007911 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7912 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007913 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007914 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007915 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007916 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007917 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007918
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007919 /*
7920 * Wait for the events to quiesce before re-instating them.
7921 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007922 synchronize_rcu();
7923
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007924 /*
7925 * Re-instate events in 2 passes.
7926 *
7927 * Skip over group leaders and only install siblings on this first
7928 * pass, siblings will not get enabled without a leader, however a
7929 * leader will enable its siblings, even if those are still on the old
7930 * context.
7931 */
7932 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7933 if (event->group_leader == event)
7934 continue;
7935
7936 list_del(&event->migrate_entry);
7937 if (event->state >= PERF_EVENT_STATE_OFF)
7938 event->state = PERF_EVENT_STATE_INACTIVE;
7939 account_event_cpu(event, dst_cpu);
7940 perf_install_in_context(dst_ctx, event, dst_cpu);
7941 get_ctx(dst_ctx);
7942 }
7943
7944 /*
7945 * Once all the siblings are setup properly, install the group leaders
7946 * to make it go.
7947 */
Peter Zijlstra98861672013-10-03 16:02:23 +02007948 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7949 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007950 if (event->state >= PERF_EVENT_STATE_OFF)
7951 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007952 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007953 perf_install_in_context(dst_ctx, event, dst_cpu);
7954 get_ctx(dst_ctx);
7955 }
7956 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007957 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007958}
7959EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7960
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007961static void sync_child_event(struct perf_event *child_event,
7962 struct task_struct *child)
7963{
7964 struct perf_event *parent_event = child_event->parent;
7965 u64 child_val;
7966
7967 if (child_event->attr.inherit_stat)
7968 perf_event_read_event(child_event, child);
7969
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007970 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007971
7972 /*
7973 * Add back the child's count to the parent's count:
7974 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007975 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007976 atomic64_add(child_event->total_time_enabled,
7977 &parent_event->child_total_time_enabled);
7978 atomic64_add(child_event->total_time_running,
7979 &parent_event->child_total_time_running);
7980
7981 /*
7982 * Remove this event from the parent's list
7983 */
7984 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7985 mutex_lock(&parent_event->child_mutex);
7986 list_del_init(&child_event->child_list);
7987 mutex_unlock(&parent_event->child_mutex);
7988
7989 /*
Jiri Olsadc633982014-09-12 13:18:26 +02007990 * Make sure user/parent get notified, that we just
7991 * lost one event.
7992 */
7993 perf_event_wakeup(parent_event);
7994
7995 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007996 * Release the parent event, if this was the last
7997 * reference to it.
7998 */
Al Viroa6fa9412012-08-20 14:59:25 +01007999 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008000}
8001
8002static void
8003__perf_event_exit_task(struct perf_event *child_event,
8004 struct perf_event_context *child_ctx,
8005 struct task_struct *child)
8006{
Peter Zijlstra1903d502014-07-15 17:27:27 +02008007 /*
8008 * Do not destroy the 'original' grouping; because of the context
8009 * switch optimization the original events could've ended up in a
8010 * random child task.
8011 *
8012 * If we were to destroy the original group, all group related
8013 * operations would cease to function properly after this random
8014 * child dies.
8015 *
8016 * Do destroy all inherited groups, we don't care about those
8017 * and being thorough is better.
8018 */
8019 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008021 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01008022 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008023 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01008024 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008025 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01008026 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008027 sync_child_event(child_event, child);
8028 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04008029 } else {
8030 child_event->state = PERF_EVENT_STATE_EXIT;
8031 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008032 }
8033}
8034
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008035static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008036{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008037 struct perf_event *child_event, *next;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008038 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008039 unsigned long flags;
8040
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008041 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008042 perf_event_task(child, NULL, 0);
8043 return;
8044 }
8045
8046 local_irq_save(flags);
8047 /*
8048 * We can't reschedule here because interrupts are disabled,
8049 * and either child is current or it is a task that can't be
8050 * scheduled, so we are now safe from rescheduling changing
8051 * our context.
8052 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01008053 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008054
8055 /*
8056 * Take the context lock here so that if find_get_context is
8057 * reading child->perf_event_ctxp, we wait until it has
8058 * incremented the context's refcount before we do put_ctx below.
8059 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01008060 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02008061 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008062 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008063
8064 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008065 * If this context is a clone; unclone it so it can't get
8066 * swapped to another process while we're removing all
8067 * the events from it.
8068 */
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008069 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01008070 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01008071 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008072
Peter Zijlstra211de6e2014-09-30 19:23:08 +02008073 if (clone_ctx)
8074 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02008075
8076 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008077 * Report the task dead after unscheduling the events so that we
8078 * won't get any samples after PERF_RECORD_EXIT. We can however still
8079 * get a few PERF_RECORD_READ events.
8080 */
8081 perf_event_task(child, child_ctx, 0);
8082
8083 /*
8084 * We can recurse on the same lock type through:
8085 *
8086 * __perf_event_exit_task()
8087 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01008088 * put_event()
8089 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008090 *
8091 * But since its the parent context it won't be the same instance.
8092 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02008093 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008094
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008095 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008096 __perf_event_exit_task(child_event, child_ctx, child);
8097
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008098 mutex_unlock(&child_ctx->mutex);
8099
8100 put_ctx(child_ctx);
8101}
8102
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008103/*
8104 * When a child task exits, feed back event values to parent events.
8105 */
8106void perf_event_exit_task(struct task_struct *child)
8107{
Peter Zijlstra88821352010-11-09 19:01:43 +01008108 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008109 int ctxn;
8110
Peter Zijlstra88821352010-11-09 19:01:43 +01008111 mutex_lock(&child->perf_event_mutex);
8112 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8113 owner_entry) {
8114 list_del_init(&event->owner_entry);
8115
8116 /*
8117 * Ensure the list deletion is visible before we clear
8118 * the owner, closes a race against perf_release() where
8119 * we need to serialize on the owner->perf_event_mutex.
8120 */
8121 smp_wmb();
8122 event->owner = NULL;
8123 }
8124 mutex_unlock(&child->perf_event_mutex);
8125
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008126 for_each_task_context_nr(ctxn)
8127 perf_event_exit_task_context(child, ctxn);
8128}
8129
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008130static void perf_free_event(struct perf_event *event,
8131 struct perf_event_context *ctx)
8132{
8133 struct perf_event *parent = event->parent;
8134
8135 if (WARN_ON_ONCE(!parent))
8136 return;
8137
8138 mutex_lock(&parent->child_mutex);
8139 list_del_init(&event->child_list);
8140 mutex_unlock(&parent->child_mutex);
8141
Al Viroa6fa9412012-08-20 14:59:25 +01008142 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008143
Peter Zijlstra652884f2015-01-23 11:20:10 +01008144 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008145 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008146 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008147 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008148 free_event(event);
8149}
8150
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008151/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008152 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008153 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008154 *
8155 * Not all locks are strictly required, but take them anyway to be nice and
8156 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008157 */
8158void perf_event_free_task(struct task_struct *task)
8159{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008160 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008161 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008162 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008163
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008164 for_each_task_context_nr(ctxn) {
8165 ctx = task->perf_event_ctxp[ctxn];
8166 if (!ctx)
8167 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008168
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008169 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008170again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008171 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8172 group_entry)
8173 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008174
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008175 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8176 group_entry)
8177 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008178
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008179 if (!list_empty(&ctx->pinned_groups) ||
8180 !list_empty(&ctx->flexible_groups))
8181 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008182
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008183 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008184
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008185 put_ctx(ctx);
8186 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008187}
8188
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008189void perf_event_delayed_put(struct task_struct *task)
8190{
8191 int ctxn;
8192
8193 for_each_task_context_nr(ctxn)
8194 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8195}
8196
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008197/*
8198 * inherit a event from parent task to child task:
8199 */
8200static struct perf_event *
8201inherit_event(struct perf_event *parent_event,
8202 struct task_struct *parent,
8203 struct perf_event_context *parent_ctx,
8204 struct task_struct *child,
8205 struct perf_event *group_leader,
8206 struct perf_event_context *child_ctx)
8207{
Jiri Olsa1929def2014-09-12 13:18:27 +02008208 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008209 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008210 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008211
8212 /*
8213 * Instead of creating recursive hierarchies of events,
8214 * we link inherited events back to the original parent,
8215 * which has a filp for sure, which we use as the reference
8216 * count:
8217 */
8218 if (parent_event->parent)
8219 parent_event = parent_event->parent;
8220
8221 child_event = perf_event_alloc(&parent_event->attr,
8222 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008223 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008224 group_leader, parent_event,
Matt Fleming79dff512015-01-23 18:45:42 +00008225 NULL, NULL, -1);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008226 if (IS_ERR(child_event))
8227 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008228
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008229 if (is_orphaned_event(parent_event) ||
8230 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01008231 free_event(child_event);
8232 return NULL;
8233 }
8234
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008235 get_ctx(child_ctx);
8236
8237 /*
8238 * Make the child state follow the state of the parent event,
8239 * not its attr.disabled bit. We hold the parent's mutex,
8240 * so we won't race with perf_event_{en, dis}able_family.
8241 */
Jiri Olsa1929def2014-09-12 13:18:27 +02008242 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008243 child_event->state = PERF_EVENT_STATE_INACTIVE;
8244 else
8245 child_event->state = PERF_EVENT_STATE_OFF;
8246
8247 if (parent_event->attr.freq) {
8248 u64 sample_period = parent_event->hw.sample_period;
8249 struct hw_perf_event *hwc = &child_event->hw;
8250
8251 hwc->sample_period = sample_period;
8252 hwc->last_period = sample_period;
8253
8254 local64_set(&hwc->period_left, sample_period);
8255 }
8256
8257 child_event->ctx = child_ctx;
8258 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008259 child_event->overflow_handler_context
8260 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008261
8262 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02008263 * Precalculate sample_data sizes
8264 */
8265 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02008266 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02008267
8268 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008269 * Link it up in the child's context:
8270 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02008271 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008272 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02008273 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008274
8275 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008276 * Link this into the parent event's child list
8277 */
8278 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8279 mutex_lock(&parent_event->child_mutex);
8280 list_add_tail(&child_event->child_list, &parent_event->child_list);
8281 mutex_unlock(&parent_event->child_mutex);
8282
8283 return child_event;
8284}
8285
8286static int inherit_group(struct perf_event *parent_event,
8287 struct task_struct *parent,
8288 struct perf_event_context *parent_ctx,
8289 struct task_struct *child,
8290 struct perf_event_context *child_ctx)
8291{
8292 struct perf_event *leader;
8293 struct perf_event *sub;
8294 struct perf_event *child_ctr;
8295
8296 leader = inherit_event(parent_event, parent, parent_ctx,
8297 child, NULL, child_ctx);
8298 if (IS_ERR(leader))
8299 return PTR_ERR(leader);
8300 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
8301 child_ctr = inherit_event(sub, parent, parent_ctx,
8302 child, leader, child_ctx);
8303 if (IS_ERR(child_ctr))
8304 return PTR_ERR(child_ctr);
8305 }
8306 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008307}
8308
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008309static int
8310inherit_task_group(struct perf_event *event, struct task_struct *parent,
8311 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008312 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008313 int *inherited_all)
8314{
8315 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008316 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008317
8318 if (!event->attr.inherit) {
8319 *inherited_all = 0;
8320 return 0;
8321 }
8322
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008323 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008324 if (!child_ctx) {
8325 /*
8326 * This is executed from the parent task context, so
8327 * inherit events that have been marked for cloning.
8328 * First allocate and initialize a context for the
8329 * child.
8330 */
8331
Jiri Olsa734df5a2013-07-09 17:44:10 +02008332 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008333 if (!child_ctx)
8334 return -ENOMEM;
8335
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008336 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008337 }
8338
8339 ret = inherit_group(event, parent, parent_ctx,
8340 child, child_ctx);
8341
8342 if (ret)
8343 *inherited_all = 0;
8344
8345 return ret;
8346}
8347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008348/*
8349 * Initialize the perf_event context in task_struct
8350 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02008351static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008352{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008353 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008354 struct perf_event_context *cloned_ctx;
8355 struct perf_event *event;
8356 struct task_struct *parent = current;
8357 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008358 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008359 int ret = 0;
8360
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008361 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008362 return 0;
8363
8364 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008365 * If the parent's context is a clone, pin it so it won't get
8366 * swapped under us.
8367 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008368 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02008369 if (!parent_ctx)
8370 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008371
8372 /*
8373 * No need to check if parent_ctx != NULL here; since we saw
8374 * it non-NULL earlier, the only reason for it to become NULL
8375 * is if we exit, and since we're currently in the middle of
8376 * a fork we can't be exiting at the same time.
8377 */
8378
8379 /*
8380 * Lock the parent list. No need to lock the child - not PID
8381 * hashed yet and not running, so nobody can access it.
8382 */
8383 mutex_lock(&parent_ctx->mutex);
8384
8385 /*
8386 * We dont have to disable NMIs - we are only looking at
8387 * the list, not manipulating it:
8388 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008389 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008390 ret = inherit_task_group(event, parent, parent_ctx,
8391 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008392 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008393 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008394 }
8395
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008396 /*
8397 * We can't hold ctx->lock when iterating the ->flexible_group list due
8398 * to allocations, but we need to prevent rotation because
8399 * rotate_ctx() will change the list from interrupt context.
8400 */
8401 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8402 parent_ctx->rotate_disable = 1;
8403 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
8404
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008405 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008406 ret = inherit_task_group(event, parent, parent_ctx,
8407 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008408 if (ret)
8409 break;
8410 }
8411
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008412 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8413 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008414
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008415 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008416
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01008417 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008418 /*
8419 * Mark the child context as a clone of the parent
8420 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008421 *
8422 * Note that if the parent is a clone, the holding of
8423 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008424 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008425 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008426 if (cloned_ctx) {
8427 child_ctx->parent_ctx = cloned_ctx;
8428 child_ctx->parent_gen = parent_ctx->parent_gen;
8429 } else {
8430 child_ctx->parent_ctx = parent_ctx;
8431 child_ctx->parent_gen = parent_ctx->generation;
8432 }
8433 get_ctx(child_ctx->parent_ctx);
8434 }
8435
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008436 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008437 mutex_unlock(&parent_ctx->mutex);
8438
8439 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008440 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008441
8442 return ret;
8443}
8444
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008445/*
8446 * Initialize the perf_event context in task_struct
8447 */
8448int perf_event_init_task(struct task_struct *child)
8449{
8450 int ctxn, ret;
8451
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01008452 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
8453 mutex_init(&child->perf_event_mutex);
8454 INIT_LIST_HEAD(&child->perf_event_list);
8455
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008456 for_each_task_context_nr(ctxn) {
8457 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008458 if (ret) {
8459 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008460 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008461 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008462 }
8463
8464 return 0;
8465}
8466
Paul Mackerras220b1402010-03-10 20:45:52 +11008467static void __init perf_event_init_all_cpus(void)
8468{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008469 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11008470 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11008471
8472 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008473 swhash = &per_cpu(swevent_htable, cpu);
8474 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00008475 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11008476 }
8477}
8478
Paul Gortmaker0db06282013-06-19 14:53:51 -04008479static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008480{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008481 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008482
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008483 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008484 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008485 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008486 struct swevent_hlist *hlist;
8487
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008488 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
8489 WARN_ON(!hlist);
8490 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008491 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008492 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008493}
8494
Peter Zijlstrac2774432010-12-08 15:29:02 +01008495#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008496static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008497{
Mark Rutland226424e2014-11-05 16:11:44 +00008498 struct remove_event re = { .detach_group = true };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008499 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008500
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008501 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008502 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8503 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008504 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008505}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008506
8507static void perf_event_exit_cpu_context(int cpu)
8508{
8509 struct perf_event_context *ctx;
8510 struct pmu *pmu;
8511 int idx;
8512
8513 idx = srcu_read_lock(&pmus_srcu);
8514 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008515 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008516
8517 mutex_lock(&ctx->mutex);
8518 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8519 mutex_unlock(&ctx->mutex);
8520 }
8521 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008522}
8523
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008524static void perf_event_exit_cpu(int cpu)
8525{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008526 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008527
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008528 perf_event_exit_cpu_context(cpu);
8529
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008530 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008531 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008532 swevent_hlist_release(swhash);
8533 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008534}
8535#else
8536static inline void perf_event_exit_cpu(int cpu) { }
8537#endif
8538
Peter Zijlstrac2774432010-12-08 15:29:02 +01008539static int
8540perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8541{
8542 int cpu;
8543
8544 for_each_online_cpu(cpu)
8545 perf_event_exit_cpu(cpu);
8546
8547 return NOTIFY_OK;
8548}
8549
8550/*
8551 * Run the perf reboot notifier at the very last possible moment so that
8552 * the generic watchdog code runs as long as possible.
8553 */
8554static struct notifier_block perf_reboot_notifier = {
8555 .notifier_call = perf_reboot,
8556 .priority = INT_MIN,
8557};
8558
Paul Gortmaker0db06282013-06-19 14:53:51 -04008559static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008560perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8561{
8562 unsigned int cpu = (long)hcpu;
8563
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008564 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008565
8566 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008567 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008568 perf_event_init_cpu(cpu);
8569 break;
8570
Peter Zijlstra5e116372010-06-11 13:35:08 +02008571 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008572 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008573 perf_event_exit_cpu(cpu);
8574 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008575 default:
8576 break;
8577 }
8578
8579 return NOTIFY_OK;
8580}
8581
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008582void __init perf_event_init(void)
8583{
Jason Wessel3c502e72010-11-04 17:33:01 -05008584 int ret;
8585
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008586 idr_init(&pmu_idr);
8587
Paul Mackerras220b1402010-03-10 20:45:52 +11008588 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008589 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008590 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8591 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8592 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008593 perf_tp_register();
8594 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008595 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008596
8597 ret = init_hw_breakpoint();
8598 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008599
8600 /* do not patch jump label more than once per second */
8601 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008602
8603 /*
8604 * Build time assertion that we keep the data_head at the intended
8605 * location. IOW, validation we got the __reserved[] size right.
8606 */
8607 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8608 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008609}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008610
Cody P Schaferfd979c02015-01-30 13:45:57 -08008611ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
8612 char *page)
8613{
8614 struct perf_pmu_events_attr *pmu_attr =
8615 container_of(attr, struct perf_pmu_events_attr, attr);
8616
8617 if (pmu_attr->event_str)
8618 return sprintf(page, "%s\n", pmu_attr->event_str);
8619
8620 return 0;
8621}
8622
Peter Zijlstraabe43402010-11-17 23:17:37 +01008623static int __init perf_event_sysfs_init(void)
8624{
8625 struct pmu *pmu;
8626 int ret;
8627
8628 mutex_lock(&pmus_lock);
8629
8630 ret = bus_register(&pmu_bus);
8631 if (ret)
8632 goto unlock;
8633
8634 list_for_each_entry(pmu, &pmus, entry) {
8635 if (!pmu->name || pmu->type < 0)
8636 continue;
8637
8638 ret = pmu_dev_alloc(pmu);
8639 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8640 }
8641 pmu_bus_running = 1;
8642 ret = 0;
8643
8644unlock:
8645 mutex_unlock(&pmus_lock);
8646
8647 return ret;
8648}
8649device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008650
8651#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008652static struct cgroup_subsys_state *
8653perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008654{
8655 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008656
Li Zefan1b15d052011-03-03 14:26:06 +08008657 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008658 if (!jc)
8659 return ERR_PTR(-ENOMEM);
8660
Stephane Eraniane5d13672011-02-14 11:20:01 +02008661 jc->info = alloc_percpu(struct perf_cgroup_info);
8662 if (!jc->info) {
8663 kfree(jc);
8664 return ERR_PTR(-ENOMEM);
8665 }
8666
Stephane Eraniane5d13672011-02-14 11:20:01 +02008667 return &jc->css;
8668}
8669
Tejun Heoeb954192013-08-08 20:11:23 -04008670static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008671{
Tejun Heoeb954192013-08-08 20:11:23 -04008672 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8673
Stephane Eraniane5d13672011-02-14 11:20:01 +02008674 free_percpu(jc->info);
8675 kfree(jc);
8676}
8677
8678static int __perf_cgroup_move(void *info)
8679{
8680 struct task_struct *task = info;
8681 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8682 return 0;
8683}
8684
Tejun Heoeb954192013-08-08 20:11:23 -04008685static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8686 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008687{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008688 struct task_struct *task;
8689
Tejun Heo924f0d92014-02-13 06:58:41 -05008690 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008691 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008692}
8693
Tejun Heoeb954192013-08-08 20:11:23 -04008694static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8695 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008696 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008697{
8698 /*
8699 * cgroup_exit() is called in the copy_process() failure path.
8700 * Ignore this case since the task hasn't ran yet, this avoids
8701 * trying to poke a half freed task state from generic code.
8702 */
8703 if (!(task->flags & PF_EXITING))
8704 return;
8705
Tejun Heobb9d97b2011-12-12 18:12:21 -08008706 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008707}
8708
Tejun Heo073219e2014-02-08 10:36:58 -05008709struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008710 .css_alloc = perf_cgroup_css_alloc,
8711 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008712 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008713 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008714};
8715#endif /* CONFIG_CGROUP_PERF */