blob: 963bf139e2b244fa1a4e2efb9a697e02b0c8dfa5 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
37#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080038#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Li Zefan877c6852013-03-05 11:38:08 +080041#include <linux/cgroup.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045
Frederic Weisbecker76369132011-05-19 19:55:04 +020046#include "internal.h"
47
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048#include <asm/irq_regs.h>
49
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010050struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020051 struct task_struct *p;
52 int (*func)(void *info);
53 void *info;
54 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010055};
56
57static void remote_function(void *data)
58{
59 struct remote_function_call *tfc = data;
60 struct task_struct *p = tfc->p;
61
62 if (p) {
63 tfc->ret = -EAGAIN;
64 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
65 return;
66 }
67
68 tfc->ret = tfc->func(tfc->info);
69}
70
71/**
72 * task_function_call - call a function on the cpu on which a task runs
73 * @p: the task to evaluate
74 * @func: the function to be called
75 * @info: the function call argument
76 *
77 * Calls the function @func when the task is currently running. This might
78 * be on the current CPU, which just calls the function directly
79 *
80 * returns: @func return value, or
81 * -ESRCH - when the process isn't running
82 * -EAGAIN - when the process moved away
83 */
84static int
85task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
86{
87 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020088 .p = p,
89 .func = func,
90 .info = info,
91 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010092 };
93
94 if (task_curr(p))
95 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
96
97 return data.ret;
98}
99
100/**
101 * cpu_function_call - call a function on the cpu
102 * @func: the function to be called
103 * @info: the function call argument
104 *
105 * Calls the function @func on the remote cpu.
106 *
107 * returns: @func return value or -ENXIO when the cpu is offline
108 */
109static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
110{
111 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200112 .p = NULL,
113 .func = func,
114 .info = info,
115 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100116 };
117
118 smp_call_function_single(cpu, remote_function, &data, 1);
119
120 return data.ret;
121}
122
Stephane Eraniane5d13672011-02-14 11:20:01 +0200123#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
124 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100125 PERF_FLAG_PID_CGROUP |\
126 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200127
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100128/*
129 * branch priv levels that need permission checks
130 */
131#define PERF_SAMPLE_BRANCH_PERM_PLM \
132 (PERF_SAMPLE_BRANCH_KERNEL |\
133 PERF_SAMPLE_BRANCH_HV)
134
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200135enum event_type_t {
136 EVENT_FLEXIBLE = 0x1,
137 EVENT_PINNED = 0x2,
138 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
139};
140
Stephane Eraniane5d13672011-02-14 11:20:01 +0200141/*
142 * perf_sched_events : >0 events exist
143 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
144 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100145struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200146static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100147static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200148
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200149static atomic_t nr_mmap_events __read_mostly;
150static atomic_t nr_comm_events __read_mostly;
151static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200152static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200153
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200154static LIST_HEAD(pmus);
155static DEFINE_MUTEX(pmus_lock);
156static struct srcu_struct pmus_srcu;
157
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200158/*
159 * perf event paranoia level:
160 * -1 - not paranoid at all
161 * 0 - disallow raw tracepoint access for unpriv
162 * 1 - disallow cpu events for unpriv
163 * 2 - disallow kernel profiling for unpriv
164 */
165int sysctl_perf_event_paranoid __read_mostly = 1;
166
Frederic Weisbecker20443382011-03-31 03:33:29 +0200167/* Minimum for 512 kiB + 1 user control page */
168int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200169
170/*
171 * max perf event sample rate
172 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700173#define DEFAULT_MAX_SAMPLE_RATE 100000
174#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
175#define DEFAULT_CPU_TIME_MAX_PERCENT 25
176
177int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
178
179static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
180static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
181
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200182static int perf_sample_allowed_ns __read_mostly =
183 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700184
185void update_perf_cpu_limits(void)
186{
187 u64 tmp = perf_sample_period_ns;
188
189 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200190 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200191 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700192}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100193
Stephane Eranian9e630202013-04-03 14:21:33 +0200194static int perf_rotate_context(struct perf_cpu_context *cpuctx);
195
Peter Zijlstra163ec432011-02-16 11:22:34 +0100196int perf_proc_update_handler(struct ctl_table *table, int write,
197 void __user *buffer, size_t *lenp,
198 loff_t *ppos)
199{
Knut Petersen723478c2013-09-25 14:29:37 +0200200 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100201
202 if (ret || !write)
203 return ret;
204
205 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700206 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
207 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100208
209 return 0;
210}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200211
Dave Hansen14c63f12013-06-21 08:51:36 -0700212int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
213
214int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
215 void __user *buffer, size_t *lenp,
216 loff_t *ppos)
217{
218 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
219
220 if (ret || !write)
221 return ret;
222
223 update_perf_cpu_limits();
224
225 return 0;
226}
227
228/*
229 * perf samples are done in some very critical code paths (NMIs).
230 * If they take too much CPU time, the system can lock up and not
231 * get any real work done. This will drop the sample rate when
232 * we detect that events are taking too long.
233 */
234#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200235static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700236
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100237static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700238{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100239 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700240 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200241 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100242
243 local_samples_len = __get_cpu_var(running_sample_length);
244 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
245
246 printk_ratelimited(KERN_WARNING
247 "perf interrupt took too long (%lld > %lld), lowering "
248 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100249 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100250 sysctl_perf_event_sample_rate);
251}
252
253static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
254
255void perf_sample_event_took(u64 sample_len_ns)
256{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200257 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100258 u64 avg_local_sample_len;
259 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700260
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200261 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700262 return;
263
264 /* decay the counter by 1 average sample */
265 local_samples_len = __get_cpu_var(running_sample_length);
266 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
267 local_samples_len += sample_len_ns;
268 __get_cpu_var(running_sample_length) = local_samples_len;
269
270 /*
271 * note: this will be biased artifically low until we have
272 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
273 * from having to maintain a count.
274 */
275 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
276
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200277 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700278 return;
279
280 if (max_samples_per_tick <= 1)
281 return;
282
283 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
284 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
285 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
286
Dave Hansen14c63f12013-06-21 08:51:36 -0700287 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100288
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100289 if (!irq_work_queue(&perf_duration_work)) {
290 early_printk("perf interrupt took too long (%lld > %lld), lowering "
291 "kernel.perf_event_max_sample_rate to %d\n",
292 avg_local_sample_len, allowed_ns >> 1,
293 sysctl_perf_event_sample_rate);
294 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700295}
296
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200297static atomic64_t perf_event_id;
298
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200299static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
300 enum event_type_t event_type);
301
302static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200303 enum event_type_t event_type,
304 struct task_struct *task);
305
306static void update_context_time(struct perf_event_context *ctx);
307static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200308
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200309void __weak perf_event_print_debug(void) { }
310
Matt Fleming84c79912010-10-03 21:41:13 +0100311extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200312{
Matt Fleming84c79912010-10-03 21:41:13 +0100313 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200314}
315
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200316static inline u64 perf_clock(void)
317{
318 return local_clock();
319}
320
Stephane Eraniane5d13672011-02-14 11:20:01 +0200321static inline struct perf_cpu_context *
322__get_cpu_context(struct perf_event_context *ctx)
323{
324 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
325}
326
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200327static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
328 struct perf_event_context *ctx)
329{
330 raw_spin_lock(&cpuctx->ctx.lock);
331 if (ctx)
332 raw_spin_lock(&ctx->lock);
333}
334
335static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
336 struct perf_event_context *ctx)
337{
338 if (ctx)
339 raw_spin_unlock(&ctx->lock);
340 raw_spin_unlock(&cpuctx->ctx.lock);
341}
342
Stephane Eraniane5d13672011-02-14 11:20:01 +0200343#ifdef CONFIG_CGROUP_PERF
344
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200345/*
Li Zefan877c6852013-03-05 11:38:08 +0800346 * perf_cgroup_info keeps track of time_enabled for a cgroup.
347 * This is a per-cpu dynamically allocated data structure.
348 */
349struct perf_cgroup_info {
350 u64 time;
351 u64 timestamp;
352};
353
354struct perf_cgroup {
355 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900356 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800357};
358
359/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200360 * Must ensure cgroup is pinned (css_get) before calling
361 * this function. In other words, we cannot call this function
362 * if there is no cgroup event for the current CPU context.
363 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200364static inline struct perf_cgroup *
365perf_cgroup_from_task(struct task_struct *task)
366{
Tejun Heo073219e2014-02-08 10:36:58 -0500367 return container_of(task_css(task, perf_event_cgrp_id),
Tejun Heo8af01f52013-08-08 20:11:22 -0400368 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200369}
370
371static inline bool
372perf_cgroup_match(struct perf_event *event)
373{
374 struct perf_event_context *ctx = event->ctx;
375 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
376
Tejun Heoef824fa2013-04-08 19:00:38 -0700377 /* @event doesn't care about cgroup */
378 if (!event->cgrp)
379 return true;
380
381 /* wants specific cgroup scope but @cpuctx isn't associated with any */
382 if (!cpuctx->cgrp)
383 return false;
384
385 /*
386 * Cgroup scoping is recursive. An event enabled for a cgroup is
387 * also enabled for all its descendant cgroups. If @cpuctx's
388 * cgroup is a descendant of @event's (the test covers identity
389 * case), it's a match.
390 */
391 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
392 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200393}
394
Stephane Eraniane5d13672011-02-14 11:20:01 +0200395static inline void perf_put_cgroup(struct perf_event *event)
396{
397 css_put(&event->cgrp->css);
398}
399
400static inline void perf_detach_cgroup(struct perf_event *event)
401{
402 perf_put_cgroup(event);
403 event->cgrp = NULL;
404}
405
406static inline int is_cgroup_event(struct perf_event *event)
407{
408 return event->cgrp != NULL;
409}
410
411static inline u64 perf_cgroup_event_time(struct perf_event *event)
412{
413 struct perf_cgroup_info *t;
414
415 t = per_cpu_ptr(event->cgrp->info, event->cpu);
416 return t->time;
417}
418
419static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
420{
421 struct perf_cgroup_info *info;
422 u64 now;
423
424 now = perf_clock();
425
426 info = this_cpu_ptr(cgrp->info);
427
428 info->time += now - info->timestamp;
429 info->timestamp = now;
430}
431
432static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
433{
434 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
435 if (cgrp_out)
436 __update_cgrp_time(cgrp_out);
437}
438
439static inline void update_cgrp_time_from_event(struct perf_event *event)
440{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200441 struct perf_cgroup *cgrp;
442
Stephane Eraniane5d13672011-02-14 11:20:01 +0200443 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200444 * ensure we access cgroup data only when needed and
445 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200446 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200447 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200448 return;
449
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200450 cgrp = perf_cgroup_from_task(current);
451 /*
452 * Do not update time when cgroup is not active
453 */
454 if (cgrp == event->cgrp)
455 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200456}
457
458static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200459perf_cgroup_set_timestamp(struct task_struct *task,
460 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200461{
462 struct perf_cgroup *cgrp;
463 struct perf_cgroup_info *info;
464
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200465 /*
466 * ctx->lock held by caller
467 * ensure we do not access cgroup data
468 * unless we have the cgroup pinned (css_get)
469 */
470 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200471 return;
472
473 cgrp = perf_cgroup_from_task(task);
474 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200475 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200476}
477
478#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
479#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
480
481/*
482 * reschedule events based on the cgroup constraint of task.
483 *
484 * mode SWOUT : schedule out everything
485 * mode SWIN : schedule in based on cgroup for next
486 */
487void perf_cgroup_switch(struct task_struct *task, int mode)
488{
489 struct perf_cpu_context *cpuctx;
490 struct pmu *pmu;
491 unsigned long flags;
492
493 /*
494 * disable interrupts to avoid geting nr_cgroup
495 * changes via __perf_event_disable(). Also
496 * avoids preemption.
497 */
498 local_irq_save(flags);
499
500 /*
501 * we reschedule only in the presence of cgroup
502 * constrained events.
503 */
504 rcu_read_lock();
505
506 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200507 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200508 if (cpuctx->unique_pmu != pmu)
509 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200510
Stephane Eraniane5d13672011-02-14 11:20:01 +0200511 /*
512 * perf_cgroup_events says at least one
513 * context on this CPU has cgroup events.
514 *
515 * ctx->nr_cgroups reports the number of cgroup
516 * events for a context.
517 */
518 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200519 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
520 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200521
522 if (mode & PERF_CGROUP_SWOUT) {
523 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
524 /*
525 * must not be done before ctxswout due
526 * to event_filter_match() in event_sched_out()
527 */
528 cpuctx->cgrp = NULL;
529 }
530
531 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200532 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200533 /*
534 * set cgrp before ctxsw in to allow
535 * event_filter_match() to not have to pass
536 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200537 */
538 cpuctx->cgrp = perf_cgroup_from_task(task);
539 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
540 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200541 perf_pmu_enable(cpuctx->ctx.pmu);
542 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200543 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200544 }
545
546 rcu_read_unlock();
547
548 local_irq_restore(flags);
549}
550
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200551static inline void perf_cgroup_sched_out(struct task_struct *task,
552 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200553{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200554 struct perf_cgroup *cgrp1;
555 struct perf_cgroup *cgrp2 = NULL;
556
557 /*
558 * we come here when we know perf_cgroup_events > 0
559 */
560 cgrp1 = perf_cgroup_from_task(task);
561
562 /*
563 * next is NULL when called from perf_event_enable_on_exec()
564 * that will systematically cause a cgroup_switch()
565 */
566 if (next)
567 cgrp2 = perf_cgroup_from_task(next);
568
569 /*
570 * only schedule out current cgroup events if we know
571 * that we are switching to a different cgroup. Otherwise,
572 * do no touch the cgroup events.
573 */
574 if (cgrp1 != cgrp2)
575 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200576}
577
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200578static inline void perf_cgroup_sched_in(struct task_struct *prev,
579 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200581 struct perf_cgroup *cgrp1;
582 struct perf_cgroup *cgrp2 = NULL;
583
584 /*
585 * we come here when we know perf_cgroup_events > 0
586 */
587 cgrp1 = perf_cgroup_from_task(task);
588
589 /* prev can never be NULL */
590 cgrp2 = perf_cgroup_from_task(prev);
591
592 /*
593 * only need to schedule in cgroup events if we are changing
594 * cgroup during ctxsw. Cgroup events were not scheduled
595 * out of ctxsw out if that was not the case.
596 */
597 if (cgrp1 != cgrp2)
598 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200599}
600
601static inline int perf_cgroup_connect(int fd, struct perf_event *event,
602 struct perf_event_attr *attr,
603 struct perf_event *group_leader)
604{
605 struct perf_cgroup *cgrp;
606 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400607 struct fd f = fdget(fd);
608 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200609
Al Viro2903ff02012-08-28 12:52:22 -0400610 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200611 return -EBADF;
612
Tejun Heoec903c02014-05-13 12:11:01 -0400613 css = css_tryget_online_from_dir(f.file->f_dentry,
614 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800615 if (IS_ERR(css)) {
616 ret = PTR_ERR(css);
617 goto out;
618 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200619
620 cgrp = container_of(css, struct perf_cgroup, css);
621 event->cgrp = cgrp;
622
623 /*
624 * all events in a group must monitor
625 * the same cgroup because a task belongs
626 * to only one perf cgroup at a time
627 */
628 if (group_leader && group_leader->cgrp != cgrp) {
629 perf_detach_cgroup(event);
630 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200631 }
Li Zefan3db272c2011-03-03 14:25:37 +0800632out:
Al Viro2903ff02012-08-28 12:52:22 -0400633 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200634 return ret;
635}
636
637static inline void
638perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
639{
640 struct perf_cgroup_info *t;
641 t = per_cpu_ptr(event->cgrp->info, event->cpu);
642 event->shadow_ctx_time = now - t->timestamp;
643}
644
645static inline void
646perf_cgroup_defer_enabled(struct perf_event *event)
647{
648 /*
649 * when the current task's perf cgroup does not match
650 * the event's, we need to remember to call the
651 * perf_mark_enable() function the first time a task with
652 * a matching perf cgroup is scheduled in.
653 */
654 if (is_cgroup_event(event) && !perf_cgroup_match(event))
655 event->cgrp_defer_enabled = 1;
656}
657
658static inline void
659perf_cgroup_mark_enabled(struct perf_event *event,
660 struct perf_event_context *ctx)
661{
662 struct perf_event *sub;
663 u64 tstamp = perf_event_time(event);
664
665 if (!event->cgrp_defer_enabled)
666 return;
667
668 event->cgrp_defer_enabled = 0;
669
670 event->tstamp_enabled = tstamp - event->total_time_enabled;
671 list_for_each_entry(sub, &event->sibling_list, group_entry) {
672 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
673 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
674 sub->cgrp_defer_enabled = 0;
675 }
676 }
677}
678#else /* !CONFIG_CGROUP_PERF */
679
680static inline bool
681perf_cgroup_match(struct perf_event *event)
682{
683 return true;
684}
685
686static inline void perf_detach_cgroup(struct perf_event *event)
687{}
688
689static inline int is_cgroup_event(struct perf_event *event)
690{
691 return 0;
692}
693
694static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
695{
696 return 0;
697}
698
699static inline void update_cgrp_time_from_event(struct perf_event *event)
700{
701}
702
703static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
704{
705}
706
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200707static inline void perf_cgroup_sched_out(struct task_struct *task,
708 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200709{
710}
711
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200712static inline void perf_cgroup_sched_in(struct task_struct *prev,
713 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200714{
715}
716
717static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
718 struct perf_event_attr *attr,
719 struct perf_event *group_leader)
720{
721 return -EINVAL;
722}
723
724static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200725perf_cgroup_set_timestamp(struct task_struct *task,
726 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200727{
728}
729
730void
731perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
732{
733}
734
735static inline void
736perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
737{
738}
739
740static inline u64 perf_cgroup_event_time(struct perf_event *event)
741{
742 return 0;
743}
744
745static inline void
746perf_cgroup_defer_enabled(struct perf_event *event)
747{
748}
749
750static inline void
751perf_cgroup_mark_enabled(struct perf_event *event,
752 struct perf_event_context *ctx)
753{
754}
755#endif
756
Stephane Eranian9e630202013-04-03 14:21:33 +0200757/*
758 * set default to be dependent on timer tick just
759 * like original code
760 */
761#define PERF_CPU_HRTIMER (1000 / HZ)
762/*
763 * function must be called with interrupts disbled
764 */
765static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
766{
767 struct perf_cpu_context *cpuctx;
768 enum hrtimer_restart ret = HRTIMER_NORESTART;
769 int rotations = 0;
770
771 WARN_ON(!irqs_disabled());
772
773 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
774
775 rotations = perf_rotate_context(cpuctx);
776
777 /*
778 * arm timer if needed
779 */
780 if (rotations) {
781 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
782 ret = HRTIMER_RESTART;
783 }
784
785 return ret;
786}
787
788/* CPU is going down */
789void perf_cpu_hrtimer_cancel(int cpu)
790{
791 struct perf_cpu_context *cpuctx;
792 struct pmu *pmu;
793 unsigned long flags;
794
795 if (WARN_ON(cpu != smp_processor_id()))
796 return;
797
798 local_irq_save(flags);
799
800 rcu_read_lock();
801
802 list_for_each_entry_rcu(pmu, &pmus, entry) {
803 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
804
805 if (pmu->task_ctx_nr == perf_sw_context)
806 continue;
807
808 hrtimer_cancel(&cpuctx->hrtimer);
809 }
810
811 rcu_read_unlock();
812
813 local_irq_restore(flags);
814}
815
816static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
817{
818 struct hrtimer *hr = &cpuctx->hrtimer;
819 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200820 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200821
822 /* no multiplexing needed for SW PMU */
823 if (pmu->task_ctx_nr == perf_sw_context)
824 return;
825
Stephane Eranian62b85632013-04-03 14:21:34 +0200826 /*
827 * check default is sane, if not set then force to
828 * default interval (1/tick)
829 */
830 timer = pmu->hrtimer_interval_ms;
831 if (timer < 1)
832 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
833
834 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200835
836 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
837 hr->function = perf_cpu_hrtimer_handler;
838}
839
840static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
841{
842 struct hrtimer *hr = &cpuctx->hrtimer;
843 struct pmu *pmu = cpuctx->ctx.pmu;
844
845 /* not for SW PMU */
846 if (pmu->task_ctx_nr == perf_sw_context)
847 return;
848
849 if (hrtimer_active(hr))
850 return;
851
852 if (!hrtimer_callback_running(hr))
853 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
854 0, HRTIMER_MODE_REL_PINNED, 0);
855}
856
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200857void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200858{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200859 int *count = this_cpu_ptr(pmu->pmu_disable_count);
860 if (!(*count)++)
861 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862}
863
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200864void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200865{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200866 int *count = this_cpu_ptr(pmu->pmu_disable_count);
867 if (!--(*count))
868 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200869}
870
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200871static DEFINE_PER_CPU(struct list_head, rotation_list);
872
873/*
874 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
875 * because they're strictly cpu affine and rotate_start is called with IRQs
876 * disabled, while rotate_context is called from IRQ context.
877 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200878static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200879{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200880 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200881 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200882
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200883 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200884
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200885 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200886 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200887}
888
889static void get_ctx(struct perf_event_context *ctx)
890{
891 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
892}
893
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200894static void put_ctx(struct perf_event_context *ctx)
895{
896 if (atomic_dec_and_test(&ctx->refcount)) {
897 if (ctx->parent_ctx)
898 put_ctx(ctx->parent_ctx);
899 if (ctx->task)
900 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800901 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200902 }
903}
904
905static void unclone_ctx(struct perf_event_context *ctx)
906{
907 if (ctx->parent_ctx) {
908 put_ctx(ctx->parent_ctx);
909 ctx->parent_ctx = NULL;
910 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200911 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200912}
913
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200914static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
915{
916 /*
917 * only top level events have the pid namespace they were created in
918 */
919 if (event->parent)
920 event = event->parent;
921
922 return task_tgid_nr_ns(p, event->ns);
923}
924
925static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
926{
927 /*
928 * only top level events have the pid namespace they were created in
929 */
930 if (event->parent)
931 event = event->parent;
932
933 return task_pid_nr_ns(p, event->ns);
934}
935
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200936/*
937 * If we inherit events we want to return the parent event id
938 * to userspace.
939 */
940static u64 primary_event_id(struct perf_event *event)
941{
942 u64 id = event->id;
943
944 if (event->parent)
945 id = event->parent->id;
946
947 return id;
948}
949
950/*
951 * Get the perf_event_context for a task and lock it.
952 * This has to cope with with the fact that until it is locked,
953 * the context could get moved to another task.
954 */
955static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200956perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200957{
958 struct perf_event_context *ctx;
959
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200960retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200961 /*
962 * One of the few rules of preemptible RCU is that one cannot do
963 * rcu_read_unlock() while holding a scheduler (or nested) lock when
964 * part of the read side critical section was preemptible -- see
965 * rcu_read_unlock_special().
966 *
967 * Since ctx->lock nests under rq->lock we must ensure the entire read
968 * side critical section is non-preemptible.
969 */
970 preempt_disable();
971 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200972 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200973 if (ctx) {
974 /*
975 * If this context is a clone of another, it might
976 * get swapped for another underneath us by
977 * perf_event_task_sched_out, though the
978 * rcu_read_lock() protects us from any context
979 * getting freed. Lock the context and check if it
980 * got swapped before we could get the lock, and retry
981 * if so. If we locked the right context, then it
982 * can't get swapped on us any more.
983 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100984 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200985 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100986 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200987 rcu_read_unlock();
988 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200989 goto retry;
990 }
991
992 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100993 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200994 ctx = NULL;
995 }
996 }
997 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200998 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200999 return ctx;
1000}
1001
1002/*
1003 * Get the context for a task and increment its pin_count so it
1004 * can't get swapped to another task. This also increments its
1005 * reference count so that the context can't get freed.
1006 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001007static struct perf_event_context *
1008perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009{
1010 struct perf_event_context *ctx;
1011 unsigned long flags;
1012
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001013 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014 if (ctx) {
1015 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001016 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001017 }
1018 return ctx;
1019}
1020
1021static void perf_unpin_context(struct perf_event_context *ctx)
1022{
1023 unsigned long flags;
1024
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001025 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001027 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028}
1029
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001030/*
1031 * Update the record of the current time in a context.
1032 */
1033static void update_context_time(struct perf_event_context *ctx)
1034{
1035 u64 now = perf_clock();
1036
1037 ctx->time += now - ctx->timestamp;
1038 ctx->timestamp = now;
1039}
1040
Stephane Eranian41587552011-01-03 18:20:01 +02001041static u64 perf_event_time(struct perf_event *event)
1042{
1043 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001044
1045 if (is_cgroup_event(event))
1046 return perf_cgroup_event_time(event);
1047
Stephane Eranian41587552011-01-03 18:20:01 +02001048 return ctx ? ctx->time : 0;
1049}
1050
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001051/*
1052 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001053 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001054 */
1055static void update_event_times(struct perf_event *event)
1056{
1057 struct perf_event_context *ctx = event->ctx;
1058 u64 run_end;
1059
1060 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1061 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1062 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001063 /*
1064 * in cgroup mode, time_enabled represents
1065 * the time the event was enabled AND active
1066 * tasks were in the monitored cgroup. This is
1067 * independent of the activity of the context as
1068 * there may be a mix of cgroup and non-cgroup events.
1069 *
1070 * That is why we treat cgroup events differently
1071 * here.
1072 */
1073 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001074 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001075 else if (ctx->is_active)
1076 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001077 else
1078 run_end = event->tstamp_stopped;
1079
1080 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001081
1082 if (event->state == PERF_EVENT_STATE_INACTIVE)
1083 run_end = event->tstamp_stopped;
1084 else
Stephane Eranian41587552011-01-03 18:20:01 +02001085 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001086
1087 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001088
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001089}
1090
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001091/*
1092 * Update total_time_enabled and total_time_running for all events in a group.
1093 */
1094static void update_group_times(struct perf_event *leader)
1095{
1096 struct perf_event *event;
1097
1098 update_event_times(leader);
1099 list_for_each_entry(event, &leader->sibling_list, group_entry)
1100 update_event_times(event);
1101}
1102
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001103static struct list_head *
1104ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1105{
1106 if (event->attr.pinned)
1107 return &ctx->pinned_groups;
1108 else
1109 return &ctx->flexible_groups;
1110}
1111
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001112/*
1113 * Add a event from the lists for its context.
1114 * Must be called with ctx->mutex and ctx->lock held.
1115 */
1116static void
1117list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1118{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001119 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1120 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121
1122 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001123 * If we're a stand alone event or group leader, we go to the context
1124 * list, group events are kept attached to the group so that
1125 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001127 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001128 struct list_head *list;
1129
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001130 if (is_software_event(event))
1131 event->group_flags |= PERF_GROUP_SOFTWARE;
1132
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001133 list = ctx_group_list(event, ctx);
1134 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135 }
1136
Peter Zijlstra08309372011-03-03 11:31:20 +01001137 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001138 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001139
Stephane Eraniand010b332012-02-09 23:21:00 +01001140 if (has_branch_stack(event))
1141 ctx->nr_branch_stack++;
1142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001144 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001145 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001146 ctx->nr_events++;
1147 if (event->attr.inherit_stat)
1148 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001149
1150 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151}
1152
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001153/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001154 * Initialize event state based on the perf_event_attr::disabled.
1155 */
1156static inline void perf_event__state_init(struct perf_event *event)
1157{
1158 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1159 PERF_EVENT_STATE_INACTIVE;
1160}
1161
1162/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001163 * Called at perf_event creation and when events are attached/detached from a
1164 * group.
1165 */
1166static void perf_event__read_size(struct perf_event *event)
1167{
1168 int entry = sizeof(u64); /* value */
1169 int size = 0;
1170 int nr = 1;
1171
1172 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1173 size += sizeof(u64);
1174
1175 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1176 size += sizeof(u64);
1177
1178 if (event->attr.read_format & PERF_FORMAT_ID)
1179 entry += sizeof(u64);
1180
1181 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1182 nr += event->group_leader->nr_siblings;
1183 size += sizeof(u64);
1184 }
1185
1186 size += entry * nr;
1187 event->read_size = size;
1188}
1189
1190static void perf_event__header_size(struct perf_event *event)
1191{
1192 struct perf_sample_data *data;
1193 u64 sample_type = event->attr.sample_type;
1194 u16 size = 0;
1195
1196 perf_event__read_size(event);
1197
1198 if (sample_type & PERF_SAMPLE_IP)
1199 size += sizeof(data->ip);
1200
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001201 if (sample_type & PERF_SAMPLE_ADDR)
1202 size += sizeof(data->addr);
1203
1204 if (sample_type & PERF_SAMPLE_PERIOD)
1205 size += sizeof(data->period);
1206
Andi Kleenc3feedf2013-01-24 16:10:28 +01001207 if (sample_type & PERF_SAMPLE_WEIGHT)
1208 size += sizeof(data->weight);
1209
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001210 if (sample_type & PERF_SAMPLE_READ)
1211 size += event->read_size;
1212
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001213 if (sample_type & PERF_SAMPLE_DATA_SRC)
1214 size += sizeof(data->data_src.val);
1215
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001216 if (sample_type & PERF_SAMPLE_TRANSACTION)
1217 size += sizeof(data->txn);
1218
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001219 event->header_size = size;
1220}
1221
1222static void perf_event__id_header_size(struct perf_event *event)
1223{
1224 struct perf_sample_data *data;
1225 u64 sample_type = event->attr.sample_type;
1226 u16 size = 0;
1227
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001228 if (sample_type & PERF_SAMPLE_TID)
1229 size += sizeof(data->tid_entry);
1230
1231 if (sample_type & PERF_SAMPLE_TIME)
1232 size += sizeof(data->time);
1233
Adrian Hunterff3d5272013-08-27 11:23:07 +03001234 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1235 size += sizeof(data->id);
1236
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001237 if (sample_type & PERF_SAMPLE_ID)
1238 size += sizeof(data->id);
1239
1240 if (sample_type & PERF_SAMPLE_STREAM_ID)
1241 size += sizeof(data->stream_id);
1242
1243 if (sample_type & PERF_SAMPLE_CPU)
1244 size += sizeof(data->cpu_entry);
1245
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001246 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001247}
1248
Peter Zijlstra8a495422010-05-27 15:47:49 +02001249static void perf_group_attach(struct perf_event *event)
1250{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001251 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001252
Peter Zijlstra74c33372010-10-15 11:40:29 +02001253 /*
1254 * We can have double attach due to group movement in perf_event_open.
1255 */
1256 if (event->attach_state & PERF_ATTACH_GROUP)
1257 return;
1258
Peter Zijlstra8a495422010-05-27 15:47:49 +02001259 event->attach_state |= PERF_ATTACH_GROUP;
1260
1261 if (group_leader == event)
1262 return;
1263
1264 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1265 !is_software_event(event))
1266 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1267
1268 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1269 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001270
1271 perf_event__header_size(group_leader);
1272
1273 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1274 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001275}
1276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001277/*
1278 * Remove a event from the lists for its context.
1279 * Must be called with ctx->mutex and ctx->lock held.
1280 */
1281static void
1282list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1283{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001284 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001285 /*
1286 * We can have double detach due to exit/hot-unplug + close.
1287 */
1288 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001289 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001290
1291 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1292
Stephane Eranian68cacd22011-03-23 16:03:06 +01001293 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001294 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001295 cpuctx = __get_cpu_context(ctx);
1296 /*
1297 * if there are no more cgroup events
1298 * then cler cgrp to avoid stale pointer
1299 * in update_cgrp_time_from_cpuctx()
1300 */
1301 if (!ctx->nr_cgroups)
1302 cpuctx->cgrp = NULL;
1303 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001304
Stephane Eraniand010b332012-02-09 23:21:00 +01001305 if (has_branch_stack(event))
1306 ctx->nr_branch_stack--;
1307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 ctx->nr_events--;
1309 if (event->attr.inherit_stat)
1310 ctx->nr_stat--;
1311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312 list_del_rcu(&event->event_entry);
1313
Peter Zijlstra8a495422010-05-27 15:47:49 +02001314 if (event->group_leader == event)
1315 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001317 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001318
1319 /*
1320 * If event was in error state, then keep it
1321 * that way, otherwise bogus counts will be
1322 * returned on read(). The only way to get out
1323 * of error state is by explicit re-enabling
1324 * of the event
1325 */
1326 if (event->state > PERF_EVENT_STATE_OFF)
1327 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001328
1329 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001330}
1331
Peter Zijlstra8a495422010-05-27 15:47:49 +02001332static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001333{
1334 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001335 struct list_head *list = NULL;
1336
1337 /*
1338 * We can have double detach due to exit/hot-unplug + close.
1339 */
1340 if (!(event->attach_state & PERF_ATTACH_GROUP))
1341 return;
1342
1343 event->attach_state &= ~PERF_ATTACH_GROUP;
1344
1345 /*
1346 * If this is a sibling, remove it from its group.
1347 */
1348 if (event->group_leader != event) {
1349 list_del_init(&event->group_entry);
1350 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001351 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001352 }
1353
1354 if (!list_empty(&event->group_entry))
1355 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001356
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001357 /*
1358 * If this was a group event with sibling events then
1359 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001360 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 */
1362 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001363 if (list)
1364 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001365 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001366
1367 /* Inherit group flags from the previous leader */
1368 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001369 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001370
1371out:
1372 perf_event__header_size(event->group_leader);
1373
1374 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1375 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376}
1377
Stephane Eranianfa66f072010-08-26 16:40:01 +02001378static inline int
1379event_filter_match(struct perf_event *event)
1380{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001381 return (event->cpu == -1 || event->cpu == smp_processor_id())
1382 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001383}
1384
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001385static void
1386event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387 struct perf_cpu_context *cpuctx,
1388 struct perf_event_context *ctx)
1389{
Stephane Eranian41587552011-01-03 18:20:01 +02001390 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001391 u64 delta;
1392 /*
1393 * An event which could not be activated because of
1394 * filter mismatch still needs to have its timings
1395 * maintained, otherwise bogus information is return
1396 * via read() for time_enabled, time_running:
1397 */
1398 if (event->state == PERF_EVENT_STATE_INACTIVE
1399 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001400 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001401 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001402 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001403 }
1404
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001405 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001406 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407
Alexander Shishkin44377272013-12-16 14:17:36 +02001408 perf_pmu_disable(event->pmu);
1409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 event->state = PERF_EVENT_STATE_INACTIVE;
1411 if (event->pending_disable) {
1412 event->pending_disable = 0;
1413 event->state = PERF_EVENT_STATE_OFF;
1414 }
Stephane Eranian41587552011-01-03 18:20:01 +02001415 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001416 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417 event->oncpu = -1;
1418
1419 if (!is_software_event(event))
1420 cpuctx->active_oncpu--;
1421 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001422 if (event->attr.freq && event->attr.sample_freq)
1423 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001424 if (event->attr.exclusive || !cpuctx->active_oncpu)
1425 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001426
1427 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428}
1429
1430static void
1431group_sched_out(struct perf_event *group_event,
1432 struct perf_cpu_context *cpuctx,
1433 struct perf_event_context *ctx)
1434{
1435 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001436 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001437
1438 event_sched_out(group_event, cpuctx, ctx);
1439
1440 /*
1441 * Schedule out siblings (if any):
1442 */
1443 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1444 event_sched_out(event, cpuctx, ctx);
1445
Stephane Eranianfa66f072010-08-26 16:40:01 +02001446 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447 cpuctx->exclusive = 0;
1448}
1449
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001450struct remove_event {
1451 struct perf_event *event;
1452 bool detach_group;
1453};
1454
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001455/*
1456 * Cross CPU call to remove a performance event
1457 *
1458 * We disable the event on the hardware level first. After that we
1459 * remove it from the context list.
1460 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001461static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001462{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001463 struct remove_event *re = info;
1464 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001466 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001467
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001468 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001470 if (re->detach_group)
1471 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001473 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1474 ctx->is_active = 0;
1475 cpuctx->task_ctx = NULL;
1476 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001477 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001478
1479 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001480}
1481
1482
1483/*
1484 * Remove the event from a task's (or a CPU's) list of events.
1485 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486 * CPU events are removed with a smp call. For task events we only
1487 * call when the task is on a CPU.
1488 *
1489 * If event->ctx is a cloned context, callers must make sure that
1490 * every task struct that event->ctx->task could possibly point to
1491 * remains valid. This is OK when called from perf_release since
1492 * that only calls us on the top-level context, which can't be a clone.
1493 * When called from perf_event_exit_task, it's OK because the
1494 * context has been detached from its task.
1495 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001496static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497{
1498 struct perf_event_context *ctx = event->ctx;
1499 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001500 struct remove_event re = {
1501 .event = event,
1502 .detach_group = detach_group,
1503 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001505 lockdep_assert_held(&ctx->mutex);
1506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001507 if (!task) {
1508 /*
1509 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001510 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001512 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513 return;
1514 }
1515
1516retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001517 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001518 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001520 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001521 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001522 * If we failed to find a running task, but find the context active now
1523 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001524 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001525 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001526 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001527 /*
1528 * Reload the task pointer, it might have been changed by
1529 * a concurrent perf_event_context_sched_out().
1530 */
1531 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001532 goto retry;
1533 }
1534
1535 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001536 * Since the task isn't running, its safe to remove the event, us
1537 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001539 if (detach_group)
1540 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001541 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001542 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001543}
1544
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001545/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546 * Cross CPU call to disable a performance event
1547 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301548int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549{
1550 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001551 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001552 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553
1554 /*
1555 * If this is a per-task event, need to check whether this
1556 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001557 *
1558 * Can trigger due to concurrent perf_event_context_sched_out()
1559 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560 */
1561 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001562 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001564 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001565
1566 /*
1567 * If the event is on, turn it off.
1568 * If it is in error state, leave it in error state.
1569 */
1570 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1571 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001572 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573 update_group_times(event);
1574 if (event == event->group_leader)
1575 group_sched_out(event, cpuctx, ctx);
1576 else
1577 event_sched_out(event, cpuctx, ctx);
1578 event->state = PERF_EVENT_STATE_OFF;
1579 }
1580
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001581 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001582
1583 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584}
1585
1586/*
1587 * Disable a event.
1588 *
1589 * If event->ctx is a cloned context, callers must make sure that
1590 * every task struct that event->ctx->task could possibly point to
1591 * remains valid. This condition is satisifed when called through
1592 * perf_event_for_each_child or perf_event_for_each because they
1593 * hold the top-level event's child_mutex, so any descendant that
1594 * goes to exit will block in sync_child_event.
1595 * When called from perf_pending_event it's OK because event->ctx
1596 * is the current context on this CPU and preemption is disabled,
1597 * hence we can't get into perf_event_task_sched_out for this context.
1598 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001599void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001600{
1601 struct perf_event_context *ctx = event->ctx;
1602 struct task_struct *task = ctx->task;
1603
1604 if (!task) {
1605 /*
1606 * Disable the event on the cpu that it's on
1607 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001608 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609 return;
1610 }
1611
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001612retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001613 if (!task_function_call(task, __perf_event_disable, event))
1614 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001615
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001616 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617 /*
1618 * If the event is still active, we need to retry the cross-call.
1619 */
1620 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001621 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001622 /*
1623 * Reload the task pointer, it might have been changed by
1624 * a concurrent perf_event_context_sched_out().
1625 */
1626 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 goto retry;
1628 }
1629
1630 /*
1631 * Since we have the lock this context can't be scheduled
1632 * in, so we can change the state safely.
1633 */
1634 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1635 update_group_times(event);
1636 event->state = PERF_EVENT_STATE_OFF;
1637 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001638 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001640EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001641
Stephane Eraniane5d13672011-02-14 11:20:01 +02001642static void perf_set_shadow_time(struct perf_event *event,
1643 struct perf_event_context *ctx,
1644 u64 tstamp)
1645{
1646 /*
1647 * use the correct time source for the time snapshot
1648 *
1649 * We could get by without this by leveraging the
1650 * fact that to get to this function, the caller
1651 * has most likely already called update_context_time()
1652 * and update_cgrp_time_xx() and thus both timestamp
1653 * are identical (or very close). Given that tstamp is,
1654 * already adjusted for cgroup, we could say that:
1655 * tstamp - ctx->timestamp
1656 * is equivalent to
1657 * tstamp - cgrp->timestamp.
1658 *
1659 * Then, in perf_output_read(), the calculation would
1660 * work with no changes because:
1661 * - event is guaranteed scheduled in
1662 * - no scheduled out in between
1663 * - thus the timestamp would be the same
1664 *
1665 * But this is a bit hairy.
1666 *
1667 * So instead, we have an explicit cgroup call to remain
1668 * within the time time source all along. We believe it
1669 * is cleaner and simpler to understand.
1670 */
1671 if (is_cgroup_event(event))
1672 perf_cgroup_set_shadow_time(event, tstamp);
1673 else
1674 event->shadow_ctx_time = tstamp - ctx->timestamp;
1675}
1676
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001677#define MAX_INTERRUPTS (~0ULL)
1678
1679static void perf_log_throttle(struct perf_event *event, int enable);
1680
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001681static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001682event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001684 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001685{
Stephane Eranian41587552011-01-03 18:20:01 +02001686 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001687 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001688
Peter Zijlstra63342412014-05-05 11:49:16 +02001689 lockdep_assert_held(&ctx->lock);
1690
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691 if (event->state <= PERF_EVENT_STATE_OFF)
1692 return 0;
1693
1694 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001695 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001696
1697 /*
1698 * Unthrottle events, since we scheduled we might have missed several
1699 * ticks already, also for a heavily scheduling task there is little
1700 * guarantee it'll get a tick in a timely manner.
1701 */
1702 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1703 perf_log_throttle(event, 1);
1704 event->hw.interrupts = 0;
1705 }
1706
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001707 /*
1708 * The new state must be visible before we turn it on in the hardware:
1709 */
1710 smp_wmb();
1711
Alexander Shishkin44377272013-12-16 14:17:36 +02001712 perf_pmu_disable(event->pmu);
1713
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001714 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 event->state = PERF_EVENT_STATE_INACTIVE;
1716 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001717 ret = -EAGAIN;
1718 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719 }
1720
Stephane Eranian41587552011-01-03 18:20:01 +02001721 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001722
Stephane Eraniane5d13672011-02-14 11:20:01 +02001723 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001725 if (!is_software_event(event))
1726 cpuctx->active_oncpu++;
1727 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001728 if (event->attr.freq && event->attr.sample_freq)
1729 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730
1731 if (event->attr.exclusive)
1732 cpuctx->exclusive = 1;
1733
Alexander Shishkin44377272013-12-16 14:17:36 +02001734out:
1735 perf_pmu_enable(event->pmu);
1736
1737 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738}
1739
1740static int
1741group_sched_in(struct perf_event *group_event,
1742 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001743 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001744{
Lin Ming6bde9b62010-04-23 13:56:00 +08001745 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001746 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001747 u64 now = ctx->time;
1748 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001749
1750 if (group_event->state == PERF_EVENT_STATE_OFF)
1751 return 0;
1752
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001753 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001754
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001755 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001756 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001757 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001759 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760
1761 /*
1762 * Schedule in siblings as one group (if any):
1763 */
1764 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001765 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766 partial_group = event;
1767 goto group_error;
1768 }
1769 }
1770
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001771 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001772 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001773
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774group_error:
1775 /*
1776 * Groups can be scheduled in as one unit only, so undo any
1777 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001778 * The events up to the failed event are scheduled out normally,
1779 * tstamp_stopped will be updated.
1780 *
1781 * The failed events and the remaining siblings need to have
1782 * their timings updated as if they had gone thru event_sched_in()
1783 * and event_sched_out(). This is required to get consistent timings
1784 * across the group. This also takes care of the case where the group
1785 * could never be scheduled by ensuring tstamp_stopped is set to mark
1786 * the time the event was actually stopped, such that time delta
1787 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001788 */
1789 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1790 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001791 simulate = true;
1792
1793 if (simulate) {
1794 event->tstamp_running += now - event->tstamp_stopped;
1795 event->tstamp_stopped = now;
1796 } else {
1797 event_sched_out(event, cpuctx, ctx);
1798 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001800 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001801
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001802 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001803
Stephane Eranian9e630202013-04-03 14:21:33 +02001804 perf_cpu_hrtimer_restart(cpuctx);
1805
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806 return -EAGAIN;
1807}
1808
1809/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001810 * Work out whether we can put this event group on the CPU now.
1811 */
1812static int group_can_go_on(struct perf_event *event,
1813 struct perf_cpu_context *cpuctx,
1814 int can_add_hw)
1815{
1816 /*
1817 * Groups consisting entirely of software events can always go on.
1818 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001819 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820 return 1;
1821 /*
1822 * If an exclusive group is already on, no other hardware
1823 * events can go on.
1824 */
1825 if (cpuctx->exclusive)
1826 return 0;
1827 /*
1828 * If this group is exclusive and there are already
1829 * events on the CPU, it can't go on.
1830 */
1831 if (event->attr.exclusive && cpuctx->active_oncpu)
1832 return 0;
1833 /*
1834 * Otherwise, try to add it if all previous groups were able
1835 * to go on.
1836 */
1837 return can_add_hw;
1838}
1839
1840static void add_event_to_ctx(struct perf_event *event,
1841 struct perf_event_context *ctx)
1842{
Stephane Eranian41587552011-01-03 18:20:01 +02001843 u64 tstamp = perf_event_time(event);
1844
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001846 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001847 event->tstamp_enabled = tstamp;
1848 event->tstamp_running = tstamp;
1849 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850}
1851
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001852static void task_ctx_sched_out(struct perf_event_context *ctx);
1853static void
1854ctx_sched_in(struct perf_event_context *ctx,
1855 struct perf_cpu_context *cpuctx,
1856 enum event_type_t event_type,
1857 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001858
Peter Zijlstradce58552011-04-09 21:17:46 +02001859static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1860 struct perf_event_context *ctx,
1861 struct task_struct *task)
1862{
1863 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1864 if (ctx)
1865 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1866 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1867 if (ctx)
1868 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1869}
1870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871/*
1872 * Cross CPU call to install and enable a performance event
1873 *
1874 * Must be called with ctx->mutex held
1875 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001876static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878 struct perf_event *event = info;
1879 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001880 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001881 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1882 struct task_struct *task = current;
1883
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001884 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001885 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001886
1887 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001888 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001890 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001891 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001892
1893 /*
1894 * If the context we're installing events in is not the
1895 * active task_ctx, flip them.
1896 */
1897 if (ctx->task && task_ctx != ctx) {
1898 if (task_ctx)
1899 raw_spin_unlock(&task_ctx->lock);
1900 raw_spin_lock(&ctx->lock);
1901 task_ctx = ctx;
1902 }
1903
1904 if (task_ctx) {
1905 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001906 task = task_ctx->task;
1907 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001908
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001909 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001910
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001912 /*
1913 * update cgrp time only if current cgrp
1914 * matches event->cgrp. Must be done before
1915 * calling add_event_to_ctx()
1916 */
1917 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919 add_event_to_ctx(event, ctx);
1920
1921 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001922 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001924 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001925
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001926 perf_pmu_enable(cpuctx->ctx.pmu);
1927 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001928
1929 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930}
1931
1932/*
1933 * Attach a performance event to a context
1934 *
1935 * First we add the event to the list with the hardware enable bit
1936 * in event->hw_config cleared.
1937 *
1938 * If the event is attached to a task which is on a CPU we use a smp
1939 * call to enable it in the task context. The task might have been
1940 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001941 */
1942static void
1943perf_install_in_context(struct perf_event_context *ctx,
1944 struct perf_event *event,
1945 int cpu)
1946{
1947 struct task_struct *task = ctx->task;
1948
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001949 lockdep_assert_held(&ctx->mutex);
1950
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001951 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001952 if (event->cpu != -1)
1953 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001955 if (!task) {
1956 /*
1957 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001958 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001959 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001960 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001961 return;
1962 }
1963
1964retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001965 if (!task_function_call(task, __perf_install_in_context, event))
1966 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001967
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001968 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001969 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001970 * If we failed to find a running task, but find the context active now
1971 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001973 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001974 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001975 /*
1976 * Reload the task pointer, it might have been changed by
1977 * a concurrent perf_event_context_sched_out().
1978 */
1979 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 goto retry;
1981 }
1982
1983 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001984 * Since the task isn't running, its safe to add the event, us holding
1985 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001986 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001987 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001988 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989}
1990
1991/*
1992 * Put a event into inactive state and update time fields.
1993 * Enabling the leader of a group effectively enables all
1994 * the group members that aren't explicitly disabled, so we
1995 * have to update their ->tstamp_enabled also.
1996 * Note: this works for group members as well as group leaders
1997 * since the non-leader members' sibling_lists will be empty.
1998 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001999static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000{
2001 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002002 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002003
2004 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002005 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002006 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002007 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2008 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002009 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010}
2011
2012/*
2013 * Cross CPU call to enable a performance event
2014 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002015static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016{
2017 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 struct perf_event_context *ctx = event->ctx;
2019 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002020 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 int err;
2022
Jiri Olsa06f41792013-07-09 17:44:11 +02002023 /*
2024 * There's a time window between 'ctx->is_active' check
2025 * in perf_event_enable function and this place having:
2026 * - IRQs on
2027 * - ctx->lock unlocked
2028 *
2029 * where the task could be killed and 'ctx' deactivated
2030 * by perf_event_exit_task.
2031 */
2032 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002033 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002035 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002036 update_context_time(ctx);
2037
2038 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2039 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002040
2041 /*
2042 * set current task's cgroup time reference point
2043 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002044 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002045
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002046 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047
Stephane Eraniane5d13672011-02-14 11:20:01 +02002048 if (!event_filter_match(event)) {
2049 if (is_cgroup_event(event))
2050 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002051 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002052 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002053
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054 /*
2055 * If the event is in a group and isn't the group leader,
2056 * then don't put it on unless the group is on.
2057 */
2058 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2059 goto unlock;
2060
2061 if (!group_can_go_on(event, cpuctx, 1)) {
2062 err = -EEXIST;
2063 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002065 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002067 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002068 }
2069
2070 if (err) {
2071 /*
2072 * If this event can't go on and it's part of a
2073 * group, then the whole group has to come off.
2074 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002075 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002076 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002077 perf_cpu_hrtimer_restart(cpuctx);
2078 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002079 if (leader->attr.pinned) {
2080 update_group_times(leader);
2081 leader->state = PERF_EVENT_STATE_ERROR;
2082 }
2083 }
2084
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002085unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002086 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002087
2088 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002089}
2090
2091/*
2092 * Enable a event.
2093 *
2094 * If event->ctx is a cloned context, callers must make sure that
2095 * every task struct that event->ctx->task could possibly point to
2096 * remains valid. This condition is satisfied when called through
2097 * perf_event_for_each_child or perf_event_for_each as described
2098 * for perf_event_disable.
2099 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002100void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101{
2102 struct perf_event_context *ctx = event->ctx;
2103 struct task_struct *task = ctx->task;
2104
2105 if (!task) {
2106 /*
2107 * Enable the event on the cpu that it's on
2108 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002109 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002110 return;
2111 }
2112
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002113 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2115 goto out;
2116
2117 /*
2118 * If the event is in error state, clear that first.
2119 * That way, if we see the event in error state below, we
2120 * know that it has gone back into error state, as distinct
2121 * from the task having been scheduled away before the
2122 * cross-call arrived.
2123 */
2124 if (event->state == PERF_EVENT_STATE_ERROR)
2125 event->state = PERF_EVENT_STATE_OFF;
2126
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002127retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002128 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002129 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002130 goto out;
2131 }
2132
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002133 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002134
2135 if (!task_function_call(task, __perf_event_enable, event))
2136 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002138 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002139
2140 /*
2141 * If the context is active and the event is still off,
2142 * we need to retry the cross-call.
2143 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002144 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2145 /*
2146 * task could have been flipped by a concurrent
2147 * perf_event_context_sched_out()
2148 */
2149 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002150 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002151 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002152
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002153out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002154 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002156EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002157
Avi Kivity26ca5c12011-06-29 18:42:37 +03002158int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002159{
2160 /*
2161 * not supported on inherited events
2162 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002163 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002164 return -EINVAL;
2165
2166 atomic_add(refresh, &event->event_limit);
2167 perf_event_enable(event);
2168
2169 return 0;
2170}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002171EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002173static void ctx_sched_out(struct perf_event_context *ctx,
2174 struct perf_cpu_context *cpuctx,
2175 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176{
2177 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002178 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002179
Peter Zijlstradb24d332011-04-09 21:17:45 +02002180 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002181 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002182 return;
2183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002185 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002186 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002187 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002188
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002189 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002190 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002191 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2192 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002193 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002194
Peter Zijlstradb24d332011-04-09 21:17:45 +02002195 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002196 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002197 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002198 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002199 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002200}
2201
2202/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002203 * Test whether two contexts are equivalent, i.e. whether they have both been
2204 * cloned from the same version of the same context.
2205 *
2206 * Equivalence is measured using a generation number in the context that is
2207 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2208 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002209 */
2210static int context_equiv(struct perf_event_context *ctx1,
2211 struct perf_event_context *ctx2)
2212{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002213 /* Pinning disables the swap optimization */
2214 if (ctx1->pin_count || ctx2->pin_count)
2215 return 0;
2216
2217 /* If ctx1 is the parent of ctx2 */
2218 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2219 return 1;
2220
2221 /* If ctx2 is the parent of ctx1 */
2222 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2223 return 1;
2224
2225 /*
2226 * If ctx1 and ctx2 have the same parent; we flatten the parent
2227 * hierarchy, see perf_event_init_context().
2228 */
2229 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2230 ctx1->parent_gen == ctx2->parent_gen)
2231 return 1;
2232
2233 /* Unmatched */
2234 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002235}
2236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237static void __perf_event_sync_stat(struct perf_event *event,
2238 struct perf_event *next_event)
2239{
2240 u64 value;
2241
2242 if (!event->attr.inherit_stat)
2243 return;
2244
2245 /*
2246 * Update the event value, we cannot use perf_event_read()
2247 * because we're in the middle of a context switch and have IRQs
2248 * disabled, which upsets smp_call_function_single(), however
2249 * we know the event must be on the current CPU, therefore we
2250 * don't need to use it.
2251 */
2252 switch (event->state) {
2253 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002254 event->pmu->read(event);
2255 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256
2257 case PERF_EVENT_STATE_INACTIVE:
2258 update_event_times(event);
2259 break;
2260
2261 default:
2262 break;
2263 }
2264
2265 /*
2266 * In order to keep per-task stats reliable we need to flip the event
2267 * values when we flip the contexts.
2268 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002269 value = local64_read(&next_event->count);
2270 value = local64_xchg(&event->count, value);
2271 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272
2273 swap(event->total_time_enabled, next_event->total_time_enabled);
2274 swap(event->total_time_running, next_event->total_time_running);
2275
2276 /*
2277 * Since we swizzled the values, update the user visible data too.
2278 */
2279 perf_event_update_userpage(event);
2280 perf_event_update_userpage(next_event);
2281}
2282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283static void perf_event_sync_stat(struct perf_event_context *ctx,
2284 struct perf_event_context *next_ctx)
2285{
2286 struct perf_event *event, *next_event;
2287
2288 if (!ctx->nr_stat)
2289 return;
2290
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002291 update_context_time(ctx);
2292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293 event = list_first_entry(&ctx->event_list,
2294 struct perf_event, event_entry);
2295
2296 next_event = list_first_entry(&next_ctx->event_list,
2297 struct perf_event, event_entry);
2298
2299 while (&event->event_entry != &ctx->event_list &&
2300 &next_event->event_entry != &next_ctx->event_list) {
2301
2302 __perf_event_sync_stat(event, next_event);
2303
2304 event = list_next_entry(event, event_entry);
2305 next_event = list_next_entry(next_event, event_entry);
2306 }
2307}
2308
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002309static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2310 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002312 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002313 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002314 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002315 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316 int do_switch = 1;
2317
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002318 if (likely(!ctx))
2319 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002320
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002321 cpuctx = __get_cpu_context(ctx);
2322 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323 return;
2324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002326 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002327 if (!next_ctx)
2328 goto unlock;
2329
2330 parent = rcu_dereference(ctx->parent_ctx);
2331 next_parent = rcu_dereference(next_ctx->parent_ctx);
2332
2333 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa1f9a7262014-06-24 10:20:25 +02002334 if (!parent || !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002335 goto unlock;
2336
2337 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338 /*
2339 * Looks like the two contexts are clones, so we might be
2340 * able to optimize the context switch. We lock both
2341 * contexts and check that they are clones under the
2342 * lock (including re-checking that neither has been
2343 * uncloned in the meantime). It doesn't matter which
2344 * order we take the locks because no other cpu could
2345 * be trying to lock both of these tasks.
2346 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002347 raw_spin_lock(&ctx->lock);
2348 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002349 if (context_equiv(ctx, next_ctx)) {
2350 /*
2351 * XXX do we need a memory barrier of sorts
2352 * wrt to rcu_dereference() of perf_event_ctxp
2353 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002354 task->perf_event_ctxp[ctxn] = next_ctx;
2355 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356 ctx->task = next;
2357 next_ctx->task = task;
2358 do_switch = 0;
2359
2360 perf_event_sync_stat(ctx, next_ctx);
2361 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002362 raw_spin_unlock(&next_ctx->lock);
2363 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002365unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366 rcu_read_unlock();
2367
2368 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002369 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002370 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002372 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373 }
2374}
2375
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002376#define for_each_task_context_nr(ctxn) \
2377 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2378
2379/*
2380 * Called from scheduler to remove the events of the current task,
2381 * with interrupts disabled.
2382 *
2383 * We stop each event and update the event value in event->count.
2384 *
2385 * This does not protect us against NMI, but disable()
2386 * sets the disabled bit in the control field of event _before_
2387 * accessing the event control register. If a NMI hits, then it will
2388 * not restart the event.
2389 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002390void __perf_event_task_sched_out(struct task_struct *task,
2391 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002392{
2393 int ctxn;
2394
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002395 for_each_task_context_nr(ctxn)
2396 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002397
2398 /*
2399 * if cgroup events exist on this CPU, then we need
2400 * to check if we have to switch out PMU state.
2401 * cgroup event are system-wide mode only
2402 */
2403 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002404 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002405}
2406
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002407static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002409 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410
2411 if (!cpuctx->task_ctx)
2412 return;
2413
2414 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2415 return;
2416
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002417 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002418 cpuctx->task_ctx = NULL;
2419}
2420
2421/*
2422 * Called with IRQs disabled
2423 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002424static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2425 enum event_type_t event_type)
2426{
2427 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428}
2429
2430static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002431ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002432 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433{
2434 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002435
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002436 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2437 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002439 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002440 continue;
2441
Stephane Eraniane5d13672011-02-14 11:20:01 +02002442 /* may need to reset tstamp_enabled */
2443 if (is_cgroup_event(event))
2444 perf_cgroup_mark_enabled(event, ctx);
2445
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002446 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002447 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002448
2449 /*
2450 * If this pinned group hasn't been scheduled,
2451 * put it in error state.
2452 */
2453 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2454 update_group_times(event);
2455 event->state = PERF_EVENT_STATE_ERROR;
2456 }
2457 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002458}
2459
2460static void
2461ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002462 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002463{
2464 struct perf_event *event;
2465 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002466
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002467 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2468 /* Ignore events in OFF or ERROR state */
2469 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002471 /*
2472 * Listen to the 'cpu' scheduling filter constraint
2473 * of events:
2474 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002475 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002476 continue;
2477
Stephane Eraniane5d13672011-02-14 11:20:01 +02002478 /* may need to reset tstamp_enabled */
2479 if (is_cgroup_event(event))
2480 perf_cgroup_mark_enabled(event, ctx);
2481
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002482 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002483 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002484 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002485 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002487}
2488
2489static void
2490ctx_sched_in(struct perf_event_context *ctx,
2491 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002492 enum event_type_t event_type,
2493 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002494{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002495 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002496 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002497
Peter Zijlstradb24d332011-04-09 21:17:45 +02002498 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002499 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002500 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002501
Stephane Eraniane5d13672011-02-14 11:20:01 +02002502 now = perf_clock();
2503 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002504 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002505 /*
2506 * First go through the list and put on any pinned groups
2507 * in order to give them the best chance of going on.
2508 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002509 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002510 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002511
2512 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002513 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002514 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002515}
2516
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002517static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002518 enum event_type_t event_type,
2519 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002520{
2521 struct perf_event_context *ctx = &cpuctx->ctx;
2522
Stephane Eraniane5d13672011-02-14 11:20:01 +02002523 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002524}
2525
Stephane Eraniane5d13672011-02-14 11:20:01 +02002526static void perf_event_context_sched_in(struct perf_event_context *ctx,
2527 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002528{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002529 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002530
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002531 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002532 if (cpuctx->task_ctx == ctx)
2533 return;
2534
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002535 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002536 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002537 /*
2538 * We want to keep the following priority order:
2539 * cpu pinned (that don't need to move), task pinned,
2540 * cpu flexible, task flexible.
2541 */
2542 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2543
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002544 if (ctx->nr_events)
2545 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002546
Gleb Natapov86b47c22011-11-22 16:08:21 +02002547 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2548
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002549 perf_pmu_enable(ctx->pmu);
2550 perf_ctx_unlock(cpuctx, ctx);
2551
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002552 /*
2553 * Since these rotations are per-cpu, we need to ensure the
2554 * cpu-context we got scheduled on is actually rotating.
2555 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002556 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002557}
2558
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002559/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002560 * When sampling the branck stack in system-wide, it may be necessary
2561 * to flush the stack on context switch. This happens when the branch
2562 * stack does not tag its entries with the pid of the current task.
2563 * Otherwise it becomes impossible to associate a branch entry with a
2564 * task. This ambiguity is more likely to appear when the branch stack
2565 * supports priv level filtering and the user sets it to monitor only
2566 * at the user level (which could be a useful measurement in system-wide
2567 * mode). In that case, the risk is high of having a branch stack with
2568 * branch from multiple tasks. Flushing may mean dropping the existing
2569 * entries or stashing them somewhere in the PMU specific code layer.
2570 *
2571 * This function provides the context switch callback to the lower code
2572 * layer. It is invoked ONLY when there is at least one system-wide context
2573 * with at least one active event using taken branch sampling.
2574 */
2575static void perf_branch_stack_sched_in(struct task_struct *prev,
2576 struct task_struct *task)
2577{
2578 struct perf_cpu_context *cpuctx;
2579 struct pmu *pmu;
2580 unsigned long flags;
2581
2582 /* no need to flush branch stack if not changing task */
2583 if (prev == task)
2584 return;
2585
2586 local_irq_save(flags);
2587
2588 rcu_read_lock();
2589
2590 list_for_each_entry_rcu(pmu, &pmus, entry) {
2591 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2592
2593 /*
2594 * check if the context has at least one
2595 * event using PERF_SAMPLE_BRANCH_STACK
2596 */
2597 if (cpuctx->ctx.nr_branch_stack > 0
2598 && pmu->flush_branch_stack) {
2599
Stephane Eraniand010b332012-02-09 23:21:00 +01002600 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2601
2602 perf_pmu_disable(pmu);
2603
2604 pmu->flush_branch_stack();
2605
2606 perf_pmu_enable(pmu);
2607
2608 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2609 }
2610 }
2611
2612 rcu_read_unlock();
2613
2614 local_irq_restore(flags);
2615}
2616
2617/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002618 * Called from scheduler to add the events of the current task
2619 * with interrupts disabled.
2620 *
2621 * We restore the event value and then enable it.
2622 *
2623 * This does not protect us against NMI, but enable()
2624 * sets the enabled bit in the control field of event _before_
2625 * accessing the event control register. If a NMI hits, then it will
2626 * keep the event running.
2627 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002628void __perf_event_task_sched_in(struct task_struct *prev,
2629 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002630{
2631 struct perf_event_context *ctx;
2632 int ctxn;
2633
2634 for_each_task_context_nr(ctxn) {
2635 ctx = task->perf_event_ctxp[ctxn];
2636 if (likely(!ctx))
2637 continue;
2638
Stephane Eraniane5d13672011-02-14 11:20:01 +02002639 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002640 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002641 /*
2642 * if cgroup events exist on this CPU, then we need
2643 * to check if we have to switch in PMU state.
2644 * cgroup event are system-wide mode only
2645 */
2646 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002647 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002648
2649 /* check for system-wide branch_stack events */
2650 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2651 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002652}
2653
Peter Zijlstraabd50712010-01-26 18:50:16 +01002654static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2655{
2656 u64 frequency = event->attr.sample_freq;
2657 u64 sec = NSEC_PER_SEC;
2658 u64 divisor, dividend;
2659
2660 int count_fls, nsec_fls, frequency_fls, sec_fls;
2661
2662 count_fls = fls64(count);
2663 nsec_fls = fls64(nsec);
2664 frequency_fls = fls64(frequency);
2665 sec_fls = 30;
2666
2667 /*
2668 * We got @count in @nsec, with a target of sample_freq HZ
2669 * the target period becomes:
2670 *
2671 * @count * 10^9
2672 * period = -------------------
2673 * @nsec * sample_freq
2674 *
2675 */
2676
2677 /*
2678 * Reduce accuracy by one bit such that @a and @b converge
2679 * to a similar magnitude.
2680 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002681#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002682do { \
2683 if (a##_fls > b##_fls) { \
2684 a >>= 1; \
2685 a##_fls--; \
2686 } else { \
2687 b >>= 1; \
2688 b##_fls--; \
2689 } \
2690} while (0)
2691
2692 /*
2693 * Reduce accuracy until either term fits in a u64, then proceed with
2694 * the other, so that finally we can do a u64/u64 division.
2695 */
2696 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2697 REDUCE_FLS(nsec, frequency);
2698 REDUCE_FLS(sec, count);
2699 }
2700
2701 if (count_fls + sec_fls > 64) {
2702 divisor = nsec * frequency;
2703
2704 while (count_fls + sec_fls > 64) {
2705 REDUCE_FLS(count, sec);
2706 divisor >>= 1;
2707 }
2708
2709 dividend = count * sec;
2710 } else {
2711 dividend = count * sec;
2712
2713 while (nsec_fls + frequency_fls > 64) {
2714 REDUCE_FLS(nsec, frequency);
2715 dividend >>= 1;
2716 }
2717
2718 divisor = nsec * frequency;
2719 }
2720
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002721 if (!divisor)
2722 return dividend;
2723
Peter Zijlstraabd50712010-01-26 18:50:16 +01002724 return div64_u64(dividend, divisor);
2725}
2726
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002727static DEFINE_PER_CPU(int, perf_throttled_count);
2728static DEFINE_PER_CPU(u64, perf_throttled_seq);
2729
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002730static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731{
2732 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002733 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734 s64 delta;
2735
Peter Zijlstraabd50712010-01-26 18:50:16 +01002736 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737
2738 delta = (s64)(period - hwc->sample_period);
2739 delta = (delta + 7) / 8; /* low pass filter */
2740
2741 sample_period = hwc->sample_period + delta;
2742
2743 if (!sample_period)
2744 sample_period = 1;
2745
2746 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002747
Peter Zijlstrae7850592010-05-21 14:43:08 +02002748 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002749 if (disable)
2750 event->pmu->stop(event, PERF_EF_UPDATE);
2751
Peter Zijlstrae7850592010-05-21 14:43:08 +02002752 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002753
2754 if (disable)
2755 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002756 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002757}
2758
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002759/*
2760 * combine freq adjustment with unthrottling to avoid two passes over the
2761 * events. At the same time, make sure, having freq events does not change
2762 * the rate of unthrottling as that would introduce bias.
2763 */
2764static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2765 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002766{
2767 struct perf_event *event;
2768 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002769 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002770 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002771
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002772 /*
2773 * only need to iterate over all events iff:
2774 * - context have events in frequency mode (needs freq adjust)
2775 * - there are events to unthrottle on this cpu
2776 */
2777 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002778 return;
2779
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002780 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002781 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002782
Paul Mackerras03541f82009-10-14 16:58:03 +11002783 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002784 if (event->state != PERF_EVENT_STATE_ACTIVE)
2785 continue;
2786
Stephane Eranian5632ab12011-01-03 18:20:01 +02002787 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002788 continue;
2789
Alexander Shishkin44377272013-12-16 14:17:36 +02002790 perf_pmu_disable(event->pmu);
2791
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792 hwc = &event->hw;
2793
Jiri Olsaae23bff2013-08-24 16:45:54 +02002794 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002795 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002796 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002797 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798 }
2799
2800 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002801 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002802
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002803 /*
2804 * stop the event and update event->count
2805 */
2806 event->pmu->stop(event, PERF_EF_UPDATE);
2807
Peter Zijlstrae7850592010-05-21 14:43:08 +02002808 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002809 delta = now - hwc->freq_count_stamp;
2810 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002811
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002812 /*
2813 * restart the event
2814 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002815 * we have stopped the event so tell that
2816 * to perf_adjust_period() to avoid stopping it
2817 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002818 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002819 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002820 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002821
2822 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02002823 next:
2824 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002825 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002826
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002827 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002828 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829}
2830
2831/*
2832 * Round-robin a context's events:
2833 */
2834static void rotate_ctx(struct perf_event_context *ctx)
2835{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002836 /*
2837 * Rotate the first entry last of non-pinned groups. Rotation might be
2838 * disabled by the inheritance code.
2839 */
2840 if (!ctx->rotate_disable)
2841 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842}
2843
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002844/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002845 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2846 * because they're strictly cpu affine and rotate_start is called with IRQs
2847 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002848 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002849static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002850{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002851 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002852 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002853
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002854 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002855 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002856 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2857 rotate = 1;
2858 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002859
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002860 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002861 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002862 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002863 if (ctx->nr_events != ctx->nr_active)
2864 rotate = 1;
2865 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002866
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002867 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002868 goto done;
2869
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002870 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002871 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002872
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002873 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2874 if (ctx)
2875 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002876
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002877 rotate_ctx(&cpuctx->ctx);
2878 if (ctx)
2879 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002880
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002881 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002882
2883 perf_pmu_enable(cpuctx->ctx.pmu);
2884 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002885done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002886 if (remove)
2887 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002888
2889 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002890}
2891
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002892#ifdef CONFIG_NO_HZ_FULL
2893bool perf_event_can_stop_tick(void)
2894{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002895 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002896 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002897 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002898 else
2899 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002900}
2901#endif
2902
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002903void perf_event_task_tick(void)
2904{
2905 struct list_head *head = &__get_cpu_var(rotation_list);
2906 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002907 struct perf_event_context *ctx;
2908 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002909
2910 WARN_ON(!irqs_disabled());
2911
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002912 __this_cpu_inc(perf_throttled_seq);
2913 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2914
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002915 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002916 ctx = &cpuctx->ctx;
2917 perf_adjust_freq_unthr_context(ctx, throttled);
2918
2919 ctx = cpuctx->task_ctx;
2920 if (ctx)
2921 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002922 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002923}
2924
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002925static int event_enable_on_exec(struct perf_event *event,
2926 struct perf_event_context *ctx)
2927{
2928 if (!event->attr.enable_on_exec)
2929 return 0;
2930
2931 event->attr.enable_on_exec = 0;
2932 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2933 return 0;
2934
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002935 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002936
2937 return 1;
2938}
2939
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002940/*
2941 * Enable all of a task's events that have been marked enable-on-exec.
2942 * This expects task == current.
2943 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002944static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002945{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002946 struct perf_event *event;
2947 unsigned long flags;
2948 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002949 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002950
2951 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952 if (!ctx || !ctx->nr_events)
2953 goto out;
2954
Stephane Eraniane566b762011-04-06 02:54:54 +02002955 /*
2956 * We must ctxsw out cgroup events to avoid conflict
2957 * when invoking perf_task_event_sched_in() later on
2958 * in this function. Otherwise we end up trying to
2959 * ctxswin cgroup events which are already scheduled
2960 * in.
2961 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002962 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002963
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002964 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002965 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002967 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002968 ret = event_enable_on_exec(event, ctx);
2969 if (ret)
2970 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002971 }
2972
2973 /*
2974 * Unclone this context if we enabled any event.
2975 */
2976 if (enabled)
2977 unclone_ctx(ctx);
2978
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002979 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002980
Stephane Eraniane566b762011-04-06 02:54:54 +02002981 /*
2982 * Also calls ctxswin for cgroup events, if any:
2983 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002984 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002985out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002986 local_irq_restore(flags);
2987}
2988
Peter Zijlstrae041e322014-05-21 17:32:19 +02002989void perf_event_exec(void)
2990{
2991 struct perf_event_context *ctx;
2992 int ctxn;
2993
2994 rcu_read_lock();
2995 for_each_task_context_nr(ctxn) {
2996 ctx = current->perf_event_ctxp[ctxn];
2997 if (!ctx)
2998 continue;
2999
3000 perf_event_enable_on_exec(ctx);
3001 }
3002 rcu_read_unlock();
3003}
3004
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003005/*
3006 * Cross CPU call to read the hardware event
3007 */
3008static void __perf_event_read(void *info)
3009{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003010 struct perf_event *event = info;
3011 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003012 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003013
3014 /*
3015 * If this is a task context, we need to check whether it is
3016 * the current task context of this cpu. If not it has been
3017 * scheduled out before the smp call arrived. In that case
3018 * event->count would have been updated to a recent sample
3019 * when the event was scheduled out.
3020 */
3021 if (ctx->task && cpuctx->task_ctx != ctx)
3022 return;
3023
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003024 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003025 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003026 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003027 update_cgrp_time_from_event(event);
3028 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003030 if (event->state == PERF_EVENT_STATE_ACTIVE)
3031 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003032 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003033}
3034
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003035static inline u64 perf_event_count(struct perf_event *event)
3036{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003037 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003038}
3039
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003040static u64 perf_event_read(struct perf_event *event)
3041{
3042 /*
3043 * If event is enabled and currently active on a CPU, update the
3044 * value in the event structure:
3045 */
3046 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3047 smp_call_function_single(event->oncpu,
3048 __perf_event_read, event, 1);
3049 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003050 struct perf_event_context *ctx = event->ctx;
3051 unsigned long flags;
3052
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003053 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003054 /*
3055 * may read while context is not active
3056 * (e.g., thread is blocked), in that case
3057 * we cannot update context time
3058 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003059 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003060 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003061 update_cgrp_time_from_event(event);
3062 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003064 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003065 }
3066
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003067 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003068}
3069
3070/*
3071 * Initialize the perf_event context in a task_struct:
3072 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003073static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003074{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003075 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003076 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003077 INIT_LIST_HEAD(&ctx->pinned_groups);
3078 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003079 INIT_LIST_HEAD(&ctx->event_list);
3080 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081}
3082
Peter Zijlstraeb184472010-09-07 15:55:13 +02003083static struct perf_event_context *
3084alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003085{
3086 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003087
3088 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3089 if (!ctx)
3090 return NULL;
3091
3092 __perf_event_init_context(ctx);
3093 if (task) {
3094 ctx->task = task;
3095 get_task_struct(task);
3096 }
3097 ctx->pmu = pmu;
3098
3099 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003100}
3101
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003102static struct task_struct *
3103find_lively_task_by_vpid(pid_t vpid)
3104{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106 int err;
3107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003109 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003110 task = current;
3111 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003112 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003113 if (task)
3114 get_task_struct(task);
3115 rcu_read_unlock();
3116
3117 if (!task)
3118 return ERR_PTR(-ESRCH);
3119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120 /* Reuse ptrace permission checks for now. */
3121 err = -EACCES;
3122 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3123 goto errout;
3124
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003125 return task;
3126errout:
3127 put_task_struct(task);
3128 return ERR_PTR(err);
3129
3130}
3131
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003132/*
3133 * Returns a matching context with refcount and pincount.
3134 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003135static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003136find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003137{
3138 struct perf_event_context *ctx;
3139 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003140 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003141 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003143 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003144 /* Must be root to operate on a CPU event: */
3145 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3146 return ERR_PTR(-EACCES);
3147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148 /*
3149 * We could be clever and allow to attach a event to an
3150 * offline CPU and activate it when the CPU comes up, but
3151 * that's for later.
3152 */
3153 if (!cpu_online(cpu))
3154 return ERR_PTR(-ENODEV);
3155
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003156 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157 ctx = &cpuctx->ctx;
3158 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003159 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003160
3161 return ctx;
3162 }
3163
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003164 err = -EINVAL;
3165 ctxn = pmu->task_ctx_nr;
3166 if (ctxn < 0)
3167 goto errout;
3168
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003169retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003170 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003171 if (ctx) {
3172 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003173 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003174 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003175 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003176 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003177 err = -ENOMEM;
3178 if (!ctx)
3179 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003180
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003181 err = 0;
3182 mutex_lock(&task->perf_event_mutex);
3183 /*
3184 * If it has already passed perf_event_exit_task().
3185 * we must see PF_EXITING, it takes this mutex too.
3186 */
3187 if (task->flags & PF_EXITING)
3188 err = -ESRCH;
3189 else if (task->perf_event_ctxp[ctxn])
3190 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003191 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003192 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003193 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003194 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003195 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003196 mutex_unlock(&task->perf_event_mutex);
3197
3198 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003199 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003200
3201 if (err == -EAGAIN)
3202 goto retry;
3203 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205 }
3206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 return ctx;
3208
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003209errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003210 return ERR_PTR(err);
3211}
3212
Li Zefan6fb29152009-10-15 11:21:42 +08003213static void perf_event_free_filter(struct perf_event *event);
3214
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003215static void free_event_rcu(struct rcu_head *head)
3216{
3217 struct perf_event *event;
3218
3219 event = container_of(head, struct perf_event, rcu_head);
3220 if (event->ns)
3221 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003222 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003223 kfree(event);
3224}
3225
Frederic Weisbecker76369132011-05-19 19:55:04 +02003226static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003227static void ring_buffer_attach(struct perf_event *event,
3228 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003229
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003230static void unaccount_event_cpu(struct perf_event *event, int cpu)
3231{
3232 if (event->parent)
3233 return;
3234
3235 if (has_branch_stack(event)) {
3236 if (!(event->attach_state & PERF_ATTACH_TASK))
3237 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3238 }
3239 if (is_cgroup_event(event))
3240 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3241}
3242
3243static void unaccount_event(struct perf_event *event)
3244{
3245 if (event->parent)
3246 return;
3247
3248 if (event->attach_state & PERF_ATTACH_TASK)
3249 static_key_slow_dec_deferred(&perf_sched_events);
3250 if (event->attr.mmap || event->attr.mmap_data)
3251 atomic_dec(&nr_mmap_events);
3252 if (event->attr.comm)
3253 atomic_dec(&nr_comm_events);
3254 if (event->attr.task)
3255 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003256 if (event->attr.freq)
3257 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003258 if (is_cgroup_event(event))
3259 static_key_slow_dec_deferred(&perf_sched_events);
3260 if (has_branch_stack(event))
3261 static_key_slow_dec_deferred(&perf_sched_events);
3262
3263 unaccount_event_cpu(event, event->cpu);
3264}
3265
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003266static void __free_event(struct perf_event *event)
3267{
3268 if (!event->parent) {
3269 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3270 put_callchain_buffers();
3271 }
3272
3273 if (event->destroy)
3274 event->destroy(event);
3275
3276 if (event->ctx)
3277 put_ctx(event->ctx);
3278
Yan, Zhengc464c762014-03-18 16:56:41 +08003279 if (event->pmu)
3280 module_put(event->pmu->module);
3281
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003282 call_rcu(&event->rcu_head, free_event_rcu);
3283}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003284
3285static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003287 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003288
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003289 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003290
Frederic Weisbecker76369132011-05-19 19:55:04 +02003291 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003292 /*
3293 * Can happen when we close an event with re-directed output.
3294 *
3295 * Since we have a 0 refcount, perf_mmap_close() will skip
3296 * over us; possibly making our ring_buffer_put() the last.
3297 */
3298 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003299 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003300 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301 }
3302
Stephane Eraniane5d13672011-02-14 11:20:01 +02003303 if (is_cgroup_event(event))
3304 perf_detach_cgroup(event);
3305
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003306 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003307}
3308
Peter Zijlstra683ede42014-05-05 12:11:24 +02003309/*
3310 * Used to free events which have a known refcount of 1, such as in error paths
3311 * where the event isn't exposed yet and inherited events.
3312 */
3313static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003314{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003315 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3316 "unexpected event refcount: %ld; ptr=%p\n",
3317 atomic_long_read(&event->refcount), event)) {
3318 /* leak to avoid use-after-free */
3319 return;
3320 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003321
Peter Zijlstra683ede42014-05-05 12:11:24 +02003322 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003323}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003324
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003325/*
3326 * Called when the last reference to the file is gone.
3327 */
Al Viroa6fa9412012-08-20 14:59:25 +01003328static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003329{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003330 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra88821352010-11-09 19:01:43 +01003331 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003332
Al Viroa6fa9412012-08-20 14:59:25 +01003333 if (!atomic_long_dec_and_test(&event->refcount))
3334 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003335
Peter Zijlstra88821352010-11-09 19:01:43 +01003336 rcu_read_lock();
3337 owner = ACCESS_ONCE(event->owner);
3338 /*
3339 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3340 * !owner it means the list deletion is complete and we can indeed
3341 * free this event, otherwise we need to serialize on
3342 * owner->perf_event_mutex.
3343 */
3344 smp_read_barrier_depends();
3345 if (owner) {
3346 /*
3347 * Since delayed_put_task_struct() also drops the last
3348 * task reference we can safely take a new reference
3349 * while holding the rcu_read_lock().
3350 */
3351 get_task_struct(owner);
3352 }
3353 rcu_read_unlock();
3354
3355 if (owner) {
3356 mutex_lock(&owner->perf_event_mutex);
3357 /*
3358 * We have to re-check the event->owner field, if it is cleared
3359 * we raced with perf_event_exit_task(), acquiring the mutex
3360 * ensured they're done, and we can proceed with freeing the
3361 * event.
3362 */
3363 if (event->owner)
3364 list_del_init(&event->owner_entry);
3365 mutex_unlock(&owner->perf_event_mutex);
3366 put_task_struct(owner);
3367 }
3368
Peter Zijlstra683ede42014-05-05 12:11:24 +02003369 WARN_ON_ONCE(ctx->parent_ctx);
3370 /*
3371 * There are two ways this annotation is useful:
3372 *
3373 * 1) there is a lock recursion from perf_event_exit_task
3374 * see the comment there.
3375 *
3376 * 2) there is a lock-inversion with mmap_sem through
3377 * perf_event_read_group(), which takes faults while
3378 * holding ctx->mutex, however this is called after
3379 * the last filedesc died, so there is no possibility
3380 * to trigger the AB-BA case.
3381 */
3382 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3383 perf_remove_from_context(event, true);
3384 mutex_unlock(&ctx->mutex);
3385
3386 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003387}
3388
Peter Zijlstra683ede42014-05-05 12:11:24 +02003389int perf_event_release_kernel(struct perf_event *event)
3390{
3391 put_event(event);
3392 return 0;
3393}
3394EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3395
Al Viroa6fa9412012-08-20 14:59:25 +01003396static int perf_release(struct inode *inode, struct file *file)
3397{
3398 put_event(file->private_data);
3399 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003400}
3401
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003402u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403{
3404 struct perf_event *child;
3405 u64 total = 0;
3406
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003407 *enabled = 0;
3408 *running = 0;
3409
Peter Zijlstra6f105812009-11-20 22:19:56 +01003410 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003412 *enabled += event->total_time_enabled +
3413 atomic64_read(&event->child_total_time_enabled);
3414 *running += event->total_time_running +
3415 atomic64_read(&event->child_total_time_running);
3416
3417 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003419 *enabled += child->total_time_enabled;
3420 *running += child->total_time_running;
3421 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003422 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003423
3424 return total;
3425}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003426EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003428static int perf_event_read_group(struct perf_event *event,
3429 u64 read_format, char __user *buf)
3430{
3431 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003432 int n = 0, size = 0, ret = -EFAULT;
3433 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003434 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003435 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003436
Peter Zijlstra6f105812009-11-20 22:19:56 +01003437 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003438 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003439
3440 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003441 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3442 values[n++] = enabled;
3443 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3444 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003445 values[n++] = count;
3446 if (read_format & PERF_FORMAT_ID)
3447 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003448
3449 size = n * sizeof(u64);
3450
3451 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003452 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453
Peter Zijlstra6f105812009-11-20 22:19:56 +01003454 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455
3456 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003457 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003459 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003460 if (read_format & PERF_FORMAT_ID)
3461 values[n++] = primary_event_id(sub);
3462
3463 size = n * sizeof(u64);
3464
Stephane Eranian184d3da2009-11-23 21:40:49 -08003465 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003466 ret = -EFAULT;
3467 goto unlock;
3468 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003469
3470 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003471 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003472unlock:
3473 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003474
Peter Zijlstraabf48682009-11-20 22:19:49 +01003475 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003476}
3477
3478static int perf_event_read_one(struct perf_event *event,
3479 u64 read_format, char __user *buf)
3480{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003481 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003482 u64 values[4];
3483 int n = 0;
3484
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003485 values[n++] = perf_event_read_value(event, &enabled, &running);
3486 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3487 values[n++] = enabled;
3488 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3489 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003490 if (read_format & PERF_FORMAT_ID)
3491 values[n++] = primary_event_id(event);
3492
3493 if (copy_to_user(buf, values, n * sizeof(u64)))
3494 return -EFAULT;
3495
3496 return n * sizeof(u64);
3497}
3498
3499/*
3500 * Read the performance event - simple non blocking version for now
3501 */
3502static ssize_t
3503perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3504{
3505 u64 read_format = event->attr.read_format;
3506 int ret;
3507
3508 /*
3509 * Return end-of-file for a read on a event that is in
3510 * error state (i.e. because it was pinned but it couldn't be
3511 * scheduled on to the CPU at some point).
3512 */
3513 if (event->state == PERF_EVENT_STATE_ERROR)
3514 return 0;
3515
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003516 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003517 return -ENOSPC;
3518
3519 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003520 if (read_format & PERF_FORMAT_GROUP)
3521 ret = perf_event_read_group(event, read_format, buf);
3522 else
3523 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003524
3525 return ret;
3526}
3527
3528static ssize_t
3529perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3530{
3531 struct perf_event *event = file->private_data;
3532
3533 return perf_read_hw(event, buf, count);
3534}
3535
3536static unsigned int perf_poll(struct file *file, poll_table *wait)
3537{
3538 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003539 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003540 unsigned int events = POLL_HUP;
3541
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003542 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003543 * Pin the event->rb by taking event->mmap_mutex; otherwise
3544 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003545 */
3546 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003547 rb = event->rb;
3548 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003549 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003550 mutex_unlock(&event->mmap_mutex);
3551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003552 poll_wait(file, &event->waitq, wait);
3553
3554 return events;
3555}
3556
3557static void perf_event_reset(struct perf_event *event)
3558{
3559 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003560 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003561 perf_event_update_userpage(event);
3562}
3563
3564/*
3565 * Holding the top-level event's child_mutex means that any
3566 * descendant process that has inherited this event will block
3567 * in sync_child_event if it goes to exit, thus satisfying the
3568 * task existence requirements of perf_event_enable/disable.
3569 */
3570static void perf_event_for_each_child(struct perf_event *event,
3571 void (*func)(struct perf_event *))
3572{
3573 struct perf_event *child;
3574
3575 WARN_ON_ONCE(event->ctx->parent_ctx);
3576 mutex_lock(&event->child_mutex);
3577 func(event);
3578 list_for_each_entry(child, &event->child_list, child_list)
3579 func(child);
3580 mutex_unlock(&event->child_mutex);
3581}
3582
3583static void perf_event_for_each(struct perf_event *event,
3584 void (*func)(struct perf_event *))
3585{
3586 struct perf_event_context *ctx = event->ctx;
3587 struct perf_event *sibling;
3588
3589 WARN_ON_ONCE(ctx->parent_ctx);
3590 mutex_lock(&ctx->mutex);
3591 event = event->group_leader;
3592
3593 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003594 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003595 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003596 mutex_unlock(&ctx->mutex);
3597}
3598
3599static int perf_event_period(struct perf_event *event, u64 __user *arg)
3600{
3601 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003602 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003603 u64 value;
3604
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003605 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003606 return -EINVAL;
3607
John Blackwoodad0cf342010-09-28 18:03:11 -04003608 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003609 return -EFAULT;
3610
3611 if (!value)
3612 return -EINVAL;
3613
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003614 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003615 if (event->attr.freq) {
3616 if (value > sysctl_perf_event_sample_rate) {
3617 ret = -EINVAL;
3618 goto unlock;
3619 }
3620
3621 event->attr.sample_freq = value;
3622 } else {
3623 event->attr.sample_period = value;
3624 event->hw.sample_period = value;
3625 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003626
3627 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3628 if (active) {
3629 perf_pmu_disable(ctx->pmu);
3630 event->pmu->stop(event, PERF_EF_UPDATE);
3631 }
3632
3633 local64_set(&event->hw.period_left, 0);
3634
3635 if (active) {
3636 event->pmu->start(event, PERF_EF_RELOAD);
3637 perf_pmu_enable(ctx->pmu);
3638 }
3639
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003640unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003641 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003642
3643 return ret;
3644}
3645
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003646static const struct file_operations perf_fops;
3647
Al Viro2903ff02012-08-28 12:52:22 -04003648static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003649{
Al Viro2903ff02012-08-28 12:52:22 -04003650 struct fd f = fdget(fd);
3651 if (!f.file)
3652 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003653
Al Viro2903ff02012-08-28 12:52:22 -04003654 if (f.file->f_op != &perf_fops) {
3655 fdput(f);
3656 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003657 }
Al Viro2903ff02012-08-28 12:52:22 -04003658 *p = f;
3659 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003660}
3661
3662static int perf_event_set_output(struct perf_event *event,
3663 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003664static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003665
3666static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3667{
3668 struct perf_event *event = file->private_data;
3669 void (*func)(struct perf_event *);
3670 u32 flags = arg;
3671
3672 switch (cmd) {
3673 case PERF_EVENT_IOC_ENABLE:
3674 func = perf_event_enable;
3675 break;
3676 case PERF_EVENT_IOC_DISABLE:
3677 func = perf_event_disable;
3678 break;
3679 case PERF_EVENT_IOC_RESET:
3680 func = perf_event_reset;
3681 break;
3682
3683 case PERF_EVENT_IOC_REFRESH:
3684 return perf_event_refresh(event, arg);
3685
3686 case PERF_EVENT_IOC_PERIOD:
3687 return perf_event_period(event, (u64 __user *)arg);
3688
Jiri Olsacf4957f2012-10-24 13:37:58 +02003689 case PERF_EVENT_IOC_ID:
3690 {
3691 u64 id = primary_event_id(event);
3692
3693 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3694 return -EFAULT;
3695 return 0;
3696 }
3697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003698 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003699 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003700 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003701 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003702 struct perf_event *output_event;
3703 struct fd output;
3704 ret = perf_fget_light(arg, &output);
3705 if (ret)
3706 return ret;
3707 output_event = output.file->private_data;
3708 ret = perf_event_set_output(event, output_event);
3709 fdput(output);
3710 } else {
3711 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003712 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003713 return ret;
3714 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003715
Li Zefan6fb29152009-10-15 11:21:42 +08003716 case PERF_EVENT_IOC_SET_FILTER:
3717 return perf_event_set_filter(event, (void __user *)arg);
3718
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003719 default:
3720 return -ENOTTY;
3721 }
3722
3723 if (flags & PERF_IOC_FLAG_GROUP)
3724 perf_event_for_each(event, func);
3725 else
3726 perf_event_for_each_child(event, func);
3727
3728 return 0;
3729}
3730
Pawel Mollb3f20782014-06-13 16:03:32 +01003731#ifdef CONFIG_COMPAT
3732static long perf_compat_ioctl(struct file *file, unsigned int cmd,
3733 unsigned long arg)
3734{
3735 switch (_IOC_NR(cmd)) {
3736 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
3737 case _IOC_NR(PERF_EVENT_IOC_ID):
3738 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
3739 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
3740 cmd &= ~IOCSIZE_MASK;
3741 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
3742 }
3743 break;
3744 }
3745 return perf_ioctl(file, cmd, arg);
3746}
3747#else
3748# define perf_compat_ioctl NULL
3749#endif
3750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003751int perf_event_task_enable(void)
3752{
3753 struct perf_event *event;
3754
3755 mutex_lock(&current->perf_event_mutex);
3756 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3757 perf_event_for_each_child(event, perf_event_enable);
3758 mutex_unlock(&current->perf_event_mutex);
3759
3760 return 0;
3761}
3762
3763int perf_event_task_disable(void)
3764{
3765 struct perf_event *event;
3766
3767 mutex_lock(&current->perf_event_mutex);
3768 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3769 perf_event_for_each_child(event, perf_event_disable);
3770 mutex_unlock(&current->perf_event_mutex);
3771
3772 return 0;
3773}
3774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003775static int perf_event_index(struct perf_event *event)
3776{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003777 if (event->hw.state & PERF_HES_STOPPED)
3778 return 0;
3779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003780 if (event->state != PERF_EVENT_STATE_ACTIVE)
3781 return 0;
3782
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003783 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003784}
3785
Eric B Munsonc4794292011-06-23 16:34:38 -04003786static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003787 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003788 u64 *enabled,
3789 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003790{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003791 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003792
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003793 *now = perf_clock();
3794 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003795 *enabled = ctx_time - event->tstamp_enabled;
3796 *running = ctx_time - event->tstamp_running;
3797}
3798
Peter Zijlstrafa731582013-09-19 10:16:42 +02003799static void perf_event_init_userpage(struct perf_event *event)
3800{
3801 struct perf_event_mmap_page *userpg;
3802 struct ring_buffer *rb;
3803
3804 rcu_read_lock();
3805 rb = rcu_dereference(event->rb);
3806 if (!rb)
3807 goto unlock;
3808
3809 userpg = rb->user_page;
3810
3811 /* Allow new userspace to detect that bit 0 is deprecated */
3812 userpg->cap_bit0_is_deprecated = 1;
3813 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3814
3815unlock:
3816 rcu_read_unlock();
3817}
3818
Peter Zijlstrac7206202012-03-22 17:26:36 +01003819void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003820{
3821}
3822
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003823/*
3824 * Callers need to ensure there can be no nesting of this function, otherwise
3825 * the seqlock logic goes bad. We can not serialize this because the arch
3826 * code calls this from NMI context.
3827 */
3828void perf_event_update_userpage(struct perf_event *event)
3829{
3830 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003831 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003832 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003833
3834 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003835 rb = rcu_dereference(event->rb);
3836 if (!rb)
3837 goto unlock;
3838
Eric B Munson0d641202011-06-24 12:26:26 -04003839 /*
3840 * compute total_time_enabled, total_time_running
3841 * based on snapshot values taken when the event
3842 * was last scheduled in.
3843 *
3844 * we cannot simply called update_context_time()
3845 * because of locking issue as we can be called in
3846 * NMI context
3847 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003848 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003849
Frederic Weisbecker76369132011-05-19 19:55:04 +02003850 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003851 /*
3852 * Disable preemption so as to not let the corresponding user-space
3853 * spin too long if we get preempted.
3854 */
3855 preempt_disable();
3856 ++userpg->lock;
3857 barrier();
3858 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003859 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003860 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003861 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003862
Eric B Munson0d641202011-06-24 12:26:26 -04003863 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003864 atomic64_read(&event->child_total_time_enabled);
3865
Eric B Munson0d641202011-06-24 12:26:26 -04003866 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003867 atomic64_read(&event->child_total_time_running);
3868
Peter Zijlstrac7206202012-03-22 17:26:36 +01003869 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871 barrier();
3872 ++userpg->lock;
3873 preempt_enable();
3874unlock:
3875 rcu_read_unlock();
3876}
3877
Peter Zijlstra906010b2009-09-21 16:08:49 +02003878static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3879{
3880 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003881 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003882 int ret = VM_FAULT_SIGBUS;
3883
3884 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3885 if (vmf->pgoff == 0)
3886 ret = 0;
3887 return ret;
3888 }
3889
3890 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003891 rb = rcu_dereference(event->rb);
3892 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003893 goto unlock;
3894
3895 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3896 goto unlock;
3897
Frederic Weisbecker76369132011-05-19 19:55:04 +02003898 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003899 if (!vmf->page)
3900 goto unlock;
3901
3902 get_page(vmf->page);
3903 vmf->page->mapping = vma->vm_file->f_mapping;
3904 vmf->page->index = vmf->pgoff;
3905
3906 ret = 0;
3907unlock:
3908 rcu_read_unlock();
3909
3910 return ret;
3911}
3912
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003913static void ring_buffer_attach(struct perf_event *event,
3914 struct ring_buffer *rb)
3915{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003916 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003917 unsigned long flags;
3918
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003919 if (event->rb) {
3920 /*
3921 * Should be impossible, we set this when removing
3922 * event->rb_entry and wait/clear when adding event->rb_entry.
3923 */
3924 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003925
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003926 old_rb = event->rb;
3927 event->rcu_batches = get_state_synchronize_rcu();
3928 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003929
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003930 spin_lock_irqsave(&old_rb->event_lock, flags);
3931 list_del_rcu(&event->rb_entry);
3932 spin_unlock_irqrestore(&old_rb->event_lock, flags);
3933 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003934
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003935 if (event->rcu_pending && rb) {
3936 cond_synchronize_rcu(event->rcu_batches);
3937 event->rcu_pending = 0;
3938 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003939
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003940 if (rb) {
3941 spin_lock_irqsave(&rb->event_lock, flags);
3942 list_add_rcu(&event->rb_entry, &rb->event_list);
3943 spin_unlock_irqrestore(&rb->event_lock, flags);
3944 }
3945
3946 rcu_assign_pointer(event->rb, rb);
3947
3948 if (old_rb) {
3949 ring_buffer_put(old_rb);
3950 /*
3951 * Since we detached before setting the new rb, so that we
3952 * could attach the new rb, we could have missed a wakeup.
3953 * Provide it now.
3954 */
3955 wake_up_all(&event->waitq);
3956 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003957}
3958
3959static void ring_buffer_wakeup(struct perf_event *event)
3960{
3961 struct ring_buffer *rb;
3962
3963 rcu_read_lock();
3964 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003965 if (rb) {
3966 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3967 wake_up_all(&event->waitq);
3968 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003969 rcu_read_unlock();
3970}
3971
Frederic Weisbecker76369132011-05-19 19:55:04 +02003972static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003973{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003974 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003975
Frederic Weisbecker76369132011-05-19 19:55:04 +02003976 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3977 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003978}
3979
Frederic Weisbecker76369132011-05-19 19:55:04 +02003980static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003981{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003982 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003983
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003984 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003985 rb = rcu_dereference(event->rb);
3986 if (rb) {
3987 if (!atomic_inc_not_zero(&rb->refcount))
3988 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003989 }
3990 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003991
Frederic Weisbecker76369132011-05-19 19:55:04 +02003992 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003993}
3994
Frederic Weisbecker76369132011-05-19 19:55:04 +02003995static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003996{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003997 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003998 return;
3999
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004000 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004001
Frederic Weisbecker76369132011-05-19 19:55:04 +02004002 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004003}
4004
4005static void perf_mmap_open(struct vm_area_struct *vma)
4006{
4007 struct perf_event *event = vma->vm_file->private_data;
4008
4009 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004010 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004011}
4012
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004013/*
4014 * A buffer can be mmap()ed multiple times; either directly through the same
4015 * event, or through other events by use of perf_event_set_output().
4016 *
4017 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4018 * the buffer here, where we still have a VM context. This means we need
4019 * to detach all events redirecting to us.
4020 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004021static void perf_mmap_close(struct vm_area_struct *vma)
4022{
4023 struct perf_event *event = vma->vm_file->private_data;
4024
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004025 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004026 struct user_struct *mmap_user = rb->mmap_user;
4027 int mmap_locked = rb->mmap_locked;
4028 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004029
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004030 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004031
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004032 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004033 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004034
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004035 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004036 mutex_unlock(&event->mmap_mutex);
4037
4038 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004039 if (atomic_read(&rb->mmap_count))
4040 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004041
4042 /*
4043 * No other mmap()s, detach from all other events that might redirect
4044 * into the now unreachable buffer. Somewhat complicated by the
4045 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4046 */
4047again:
4048 rcu_read_lock();
4049 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4050 if (!atomic_long_inc_not_zero(&event->refcount)) {
4051 /*
4052 * This event is en-route to free_event() which will
4053 * detach it and remove it from the list.
4054 */
4055 continue;
4056 }
4057 rcu_read_unlock();
4058
4059 mutex_lock(&event->mmap_mutex);
4060 /*
4061 * Check we didn't race with perf_event_set_output() which can
4062 * swizzle the rb from under us while we were waiting to
4063 * acquire mmap_mutex.
4064 *
4065 * If we find a different rb; ignore this event, a next
4066 * iteration will no longer find it on the list. We have to
4067 * still restart the iteration to make sure we're not now
4068 * iterating the wrong list.
4069 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004070 if (event->rb == rb)
4071 ring_buffer_attach(event, NULL);
4072
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004073 mutex_unlock(&event->mmap_mutex);
4074 put_event(event);
4075
4076 /*
4077 * Restart the iteration; either we're on the wrong list or
4078 * destroyed its integrity by doing a deletion.
4079 */
4080 goto again;
4081 }
4082 rcu_read_unlock();
4083
4084 /*
4085 * It could be there's still a few 0-ref events on the list; they'll
4086 * get cleaned up by free_event() -- they'll also still have their
4087 * ref on the rb and will free it whenever they are done with it.
4088 *
4089 * Aside from that, this buffer is 'fully' detached and unmapped,
4090 * undo the VM accounting.
4091 */
4092
4093 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4094 vma->vm_mm->pinned_vm -= mmap_locked;
4095 free_uid(mmap_user);
4096
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004097out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004098 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004099}
4100
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004101static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004102 .open = perf_mmap_open,
4103 .close = perf_mmap_close,
4104 .fault = perf_mmap_fault,
4105 .page_mkwrite = perf_mmap_fault,
4106};
4107
4108static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4109{
4110 struct perf_event *event = file->private_data;
4111 unsigned long user_locked, user_lock_limit;
4112 struct user_struct *user = current_user();
4113 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004114 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115 unsigned long vma_size;
4116 unsigned long nr_pages;
4117 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004118 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004119
Peter Zijlstrac7920612010-05-18 10:33:24 +02004120 /*
4121 * Don't allow mmap() of inherited per-task counters. This would
4122 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004123 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004124 */
4125 if (event->cpu == -1 && event->attr.inherit)
4126 return -EINVAL;
4127
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004128 if (!(vma->vm_flags & VM_SHARED))
4129 return -EINVAL;
4130
4131 vma_size = vma->vm_end - vma->vm_start;
4132 nr_pages = (vma_size / PAGE_SIZE) - 1;
4133
4134 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004135 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136 * can do bitmasks instead of modulo.
4137 */
4138 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4139 return -EINVAL;
4140
4141 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4142 return -EINVAL;
4143
4144 if (vma->vm_pgoff != 0)
4145 return -EINVAL;
4146
4147 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004148again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004149 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004150 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004151 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004153 goto unlock;
4154 }
4155
4156 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4157 /*
4158 * Raced against perf_mmap_close() through
4159 * perf_event_set_output(). Try again, hope for better
4160 * luck.
4161 */
4162 mutex_unlock(&event->mmap_mutex);
4163 goto again;
4164 }
4165
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166 goto unlock;
4167 }
4168
4169 user_extra = nr_pages + 1;
4170 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4171
4172 /*
4173 * Increase the limit linearly with more CPUs:
4174 */
4175 user_lock_limit *= num_online_cpus();
4176
4177 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4178
4179 extra = 0;
4180 if (user_locked > user_lock_limit)
4181 extra = user_locked - user_lock_limit;
4182
Jiri Slaby78d7d402010-03-05 13:42:54 -08004183 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004184 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004185 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004186
4187 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4188 !capable(CAP_IPC_LOCK)) {
4189 ret = -EPERM;
4190 goto unlock;
4191 }
4192
Frederic Weisbecker76369132011-05-19 19:55:04 +02004193 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004194
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004195 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004196 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004197
Vince Weaver4ec83632011-06-01 15:15:36 -04004198 rb = rb_alloc(nr_pages,
4199 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4200 event->cpu, flags);
4201
Frederic Weisbecker76369132011-05-19 19:55:04 +02004202 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004203 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004204 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004205 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004206
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004207 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004208 rb->mmap_locked = extra;
4209 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004211 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004212 vma->vm_mm->pinned_vm += extra;
4213
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004214 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004215
Peter Zijlstrafa731582013-09-19 10:16:42 +02004216 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004217 perf_event_update_userpage(event);
4218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004220 if (!ret)
4221 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004222 mutex_unlock(&event->mmap_mutex);
4223
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004224 /*
4225 * Since pinned accounting is per vm we cannot allow fork() to copy our
4226 * vma.
4227 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004228 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004229 vma->vm_ops = &perf_mmap_vmops;
4230
4231 return ret;
4232}
4233
4234static int perf_fasync(int fd, struct file *filp, int on)
4235{
Al Viro496ad9a2013-01-23 17:07:38 -05004236 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004237 struct perf_event *event = filp->private_data;
4238 int retval;
4239
4240 mutex_lock(&inode->i_mutex);
4241 retval = fasync_helper(fd, filp, on, &event->fasync);
4242 mutex_unlock(&inode->i_mutex);
4243
4244 if (retval < 0)
4245 return retval;
4246
4247 return 0;
4248}
4249
4250static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004251 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004252 .release = perf_release,
4253 .read = perf_read,
4254 .poll = perf_poll,
4255 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004256 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004257 .mmap = perf_mmap,
4258 .fasync = perf_fasync,
4259};
4260
4261/*
4262 * Perf event wakeup
4263 *
4264 * If there's data, ensure we set the poll() state and publish everything
4265 * to user-space before waking everybody up.
4266 */
4267
4268void perf_event_wakeup(struct perf_event *event)
4269{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004270 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004271
4272 if (event->pending_kill) {
4273 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4274 event->pending_kill = 0;
4275 }
4276}
4277
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004278static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004279{
4280 struct perf_event *event = container_of(entry,
4281 struct perf_event, pending);
4282
4283 if (event->pending_disable) {
4284 event->pending_disable = 0;
4285 __perf_event_disable(event);
4286 }
4287
4288 if (event->pending_wakeup) {
4289 event->pending_wakeup = 0;
4290 perf_event_wakeup(event);
4291 }
4292}
4293
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004294/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004295 * We assume there is only KVM supporting the callbacks.
4296 * Later on, we might change it to a list if there is
4297 * another virtualization implementation supporting the callbacks.
4298 */
4299struct perf_guest_info_callbacks *perf_guest_cbs;
4300
4301int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4302{
4303 perf_guest_cbs = cbs;
4304 return 0;
4305}
4306EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4307
4308int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4309{
4310 perf_guest_cbs = NULL;
4311 return 0;
4312}
4313EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4314
Jiri Olsa40189942012-08-07 15:20:37 +02004315static void
4316perf_output_sample_regs(struct perf_output_handle *handle,
4317 struct pt_regs *regs, u64 mask)
4318{
4319 int bit;
4320
4321 for_each_set_bit(bit, (const unsigned long *) &mask,
4322 sizeof(mask) * BITS_PER_BYTE) {
4323 u64 val;
4324
4325 val = perf_reg_value(regs, bit);
4326 perf_output_put(handle, val);
4327 }
4328}
4329
4330static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4331 struct pt_regs *regs)
4332{
4333 if (!user_mode(regs)) {
4334 if (current->mm)
4335 regs = task_pt_regs(current);
4336 else
4337 regs = NULL;
4338 }
4339
4340 if (regs) {
4341 regs_user->regs = regs;
4342 regs_user->abi = perf_reg_abi(current);
4343 }
4344}
4345
Jiri Olsac5ebced2012-08-07 15:20:40 +02004346/*
4347 * Get remaining task size from user stack pointer.
4348 *
4349 * It'd be better to take stack vma map and limit this more
4350 * precisly, but there's no way to get it safely under interrupt,
4351 * so using TASK_SIZE as limit.
4352 */
4353static u64 perf_ustack_task_size(struct pt_regs *regs)
4354{
4355 unsigned long addr = perf_user_stack_pointer(regs);
4356
4357 if (!addr || addr >= TASK_SIZE)
4358 return 0;
4359
4360 return TASK_SIZE - addr;
4361}
4362
4363static u16
4364perf_sample_ustack_size(u16 stack_size, u16 header_size,
4365 struct pt_regs *regs)
4366{
4367 u64 task_size;
4368
4369 /* No regs, no stack pointer, no dump. */
4370 if (!regs)
4371 return 0;
4372
4373 /*
4374 * Check if we fit in with the requested stack size into the:
4375 * - TASK_SIZE
4376 * If we don't, we limit the size to the TASK_SIZE.
4377 *
4378 * - remaining sample size
4379 * If we don't, we customize the stack size to
4380 * fit in to the remaining sample size.
4381 */
4382
4383 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4384 stack_size = min(stack_size, (u16) task_size);
4385
4386 /* Current header size plus static size and dynamic size. */
4387 header_size += 2 * sizeof(u64);
4388
4389 /* Do we fit in with the current stack dump size? */
4390 if ((u16) (header_size + stack_size) < header_size) {
4391 /*
4392 * If we overflow the maximum size for the sample,
4393 * we customize the stack dump size to fit in.
4394 */
4395 stack_size = USHRT_MAX - header_size - sizeof(u64);
4396 stack_size = round_up(stack_size, sizeof(u64));
4397 }
4398
4399 return stack_size;
4400}
4401
4402static void
4403perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4404 struct pt_regs *regs)
4405{
4406 /* Case of a kernel thread, nothing to dump */
4407 if (!regs) {
4408 u64 size = 0;
4409 perf_output_put(handle, size);
4410 } else {
4411 unsigned long sp;
4412 unsigned int rem;
4413 u64 dyn_size;
4414
4415 /*
4416 * We dump:
4417 * static size
4418 * - the size requested by user or the best one we can fit
4419 * in to the sample max size
4420 * data
4421 * - user stack dump data
4422 * dynamic size
4423 * - the actual dumped size
4424 */
4425
4426 /* Static size. */
4427 perf_output_put(handle, dump_size);
4428
4429 /* Data. */
4430 sp = perf_user_stack_pointer(regs);
4431 rem = __output_copy_user(handle, (void *) sp, dump_size);
4432 dyn_size = dump_size - rem;
4433
4434 perf_output_skip(handle, rem);
4435
4436 /* Dynamic size. */
4437 perf_output_put(handle, dyn_size);
4438 }
4439}
4440
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004441static void __perf_event_header__init_id(struct perf_event_header *header,
4442 struct perf_sample_data *data,
4443 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004444{
4445 u64 sample_type = event->attr.sample_type;
4446
4447 data->type = sample_type;
4448 header->size += event->id_header_size;
4449
4450 if (sample_type & PERF_SAMPLE_TID) {
4451 /* namespace issues */
4452 data->tid_entry.pid = perf_event_pid(event, current);
4453 data->tid_entry.tid = perf_event_tid(event, current);
4454 }
4455
4456 if (sample_type & PERF_SAMPLE_TIME)
4457 data->time = perf_clock();
4458
Adrian Hunterff3d5272013-08-27 11:23:07 +03004459 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004460 data->id = primary_event_id(event);
4461
4462 if (sample_type & PERF_SAMPLE_STREAM_ID)
4463 data->stream_id = event->id;
4464
4465 if (sample_type & PERF_SAMPLE_CPU) {
4466 data->cpu_entry.cpu = raw_smp_processor_id();
4467 data->cpu_entry.reserved = 0;
4468 }
4469}
4470
Frederic Weisbecker76369132011-05-19 19:55:04 +02004471void perf_event_header__init_id(struct perf_event_header *header,
4472 struct perf_sample_data *data,
4473 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004474{
4475 if (event->attr.sample_id_all)
4476 __perf_event_header__init_id(header, data, event);
4477}
4478
4479static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4480 struct perf_sample_data *data)
4481{
4482 u64 sample_type = data->type;
4483
4484 if (sample_type & PERF_SAMPLE_TID)
4485 perf_output_put(handle, data->tid_entry);
4486
4487 if (sample_type & PERF_SAMPLE_TIME)
4488 perf_output_put(handle, data->time);
4489
4490 if (sample_type & PERF_SAMPLE_ID)
4491 perf_output_put(handle, data->id);
4492
4493 if (sample_type & PERF_SAMPLE_STREAM_ID)
4494 perf_output_put(handle, data->stream_id);
4495
4496 if (sample_type & PERF_SAMPLE_CPU)
4497 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004498
4499 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4500 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004501}
4502
Frederic Weisbecker76369132011-05-19 19:55:04 +02004503void perf_event__output_id_sample(struct perf_event *event,
4504 struct perf_output_handle *handle,
4505 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004506{
4507 if (event->attr.sample_id_all)
4508 __perf_event__output_id_sample(handle, sample);
4509}
4510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004512 struct perf_event *event,
4513 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004514{
4515 u64 read_format = event->attr.read_format;
4516 u64 values[4];
4517 int n = 0;
4518
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004519 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004520 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004521 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004522 atomic64_read(&event->child_total_time_enabled);
4523 }
4524 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004525 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 atomic64_read(&event->child_total_time_running);
4527 }
4528 if (read_format & PERF_FORMAT_ID)
4529 values[n++] = primary_event_id(event);
4530
Frederic Weisbecker76369132011-05-19 19:55:04 +02004531 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004532}
4533
4534/*
4535 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4536 */
4537static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004538 struct perf_event *event,
4539 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004540{
4541 struct perf_event *leader = event->group_leader, *sub;
4542 u64 read_format = event->attr.read_format;
4543 u64 values[5];
4544 int n = 0;
4545
4546 values[n++] = 1 + leader->nr_siblings;
4547
4548 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004549 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550
4551 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004552 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004553
4554 if (leader != event)
4555 leader->pmu->read(leader);
4556
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004557 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004558 if (read_format & PERF_FORMAT_ID)
4559 values[n++] = primary_event_id(leader);
4560
Frederic Weisbecker76369132011-05-19 19:55:04 +02004561 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004562
4563 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4564 n = 0;
4565
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004566 if ((sub != event) &&
4567 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004568 sub->pmu->read(sub);
4569
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004570 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004571 if (read_format & PERF_FORMAT_ID)
4572 values[n++] = primary_event_id(sub);
4573
Frederic Weisbecker76369132011-05-19 19:55:04 +02004574 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004575 }
4576}
4577
Stephane Eranianeed01522010-10-26 16:08:01 +02004578#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4579 PERF_FORMAT_TOTAL_TIME_RUNNING)
4580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004581static void perf_output_read(struct perf_output_handle *handle,
4582 struct perf_event *event)
4583{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004584 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004585 u64 read_format = event->attr.read_format;
4586
4587 /*
4588 * compute total_time_enabled, total_time_running
4589 * based on snapshot values taken when the event
4590 * was last scheduled in.
4591 *
4592 * we cannot simply called update_context_time()
4593 * because of locking issue as we are called in
4594 * NMI context
4595 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004596 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004597 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004598
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004599 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004600 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004601 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004602 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004603}
4604
4605void perf_output_sample(struct perf_output_handle *handle,
4606 struct perf_event_header *header,
4607 struct perf_sample_data *data,
4608 struct perf_event *event)
4609{
4610 u64 sample_type = data->type;
4611
4612 perf_output_put(handle, *header);
4613
Adrian Hunterff3d5272013-08-27 11:23:07 +03004614 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4615 perf_output_put(handle, data->id);
4616
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004617 if (sample_type & PERF_SAMPLE_IP)
4618 perf_output_put(handle, data->ip);
4619
4620 if (sample_type & PERF_SAMPLE_TID)
4621 perf_output_put(handle, data->tid_entry);
4622
4623 if (sample_type & PERF_SAMPLE_TIME)
4624 perf_output_put(handle, data->time);
4625
4626 if (sample_type & PERF_SAMPLE_ADDR)
4627 perf_output_put(handle, data->addr);
4628
4629 if (sample_type & PERF_SAMPLE_ID)
4630 perf_output_put(handle, data->id);
4631
4632 if (sample_type & PERF_SAMPLE_STREAM_ID)
4633 perf_output_put(handle, data->stream_id);
4634
4635 if (sample_type & PERF_SAMPLE_CPU)
4636 perf_output_put(handle, data->cpu_entry);
4637
4638 if (sample_type & PERF_SAMPLE_PERIOD)
4639 perf_output_put(handle, data->period);
4640
4641 if (sample_type & PERF_SAMPLE_READ)
4642 perf_output_read(handle, event);
4643
4644 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4645 if (data->callchain) {
4646 int size = 1;
4647
4648 if (data->callchain)
4649 size += data->callchain->nr;
4650
4651 size *= sizeof(u64);
4652
Frederic Weisbecker76369132011-05-19 19:55:04 +02004653 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654 } else {
4655 u64 nr = 0;
4656 perf_output_put(handle, nr);
4657 }
4658 }
4659
4660 if (sample_type & PERF_SAMPLE_RAW) {
4661 if (data->raw) {
4662 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004663 __output_copy(handle, data->raw->data,
4664 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004665 } else {
4666 struct {
4667 u32 size;
4668 u32 data;
4669 } raw = {
4670 .size = sizeof(u32),
4671 .data = 0,
4672 };
4673 perf_output_put(handle, raw);
4674 }
4675 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004676
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004677 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4678 if (data->br_stack) {
4679 size_t size;
4680
4681 size = data->br_stack->nr
4682 * sizeof(struct perf_branch_entry);
4683
4684 perf_output_put(handle, data->br_stack->nr);
4685 perf_output_copy(handle, data->br_stack->entries, size);
4686 } else {
4687 /*
4688 * we always store at least the value of nr
4689 */
4690 u64 nr = 0;
4691 perf_output_put(handle, nr);
4692 }
4693 }
Jiri Olsa40189942012-08-07 15:20:37 +02004694
4695 if (sample_type & PERF_SAMPLE_REGS_USER) {
4696 u64 abi = data->regs_user.abi;
4697
4698 /*
4699 * If there are no regs to dump, notice it through
4700 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4701 */
4702 perf_output_put(handle, abi);
4703
4704 if (abi) {
4705 u64 mask = event->attr.sample_regs_user;
4706 perf_output_sample_regs(handle,
4707 data->regs_user.regs,
4708 mask);
4709 }
4710 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004711
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004712 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004713 perf_output_sample_ustack(handle,
4714 data->stack_user_size,
4715 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004716 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004717
4718 if (sample_type & PERF_SAMPLE_WEIGHT)
4719 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004720
4721 if (sample_type & PERF_SAMPLE_DATA_SRC)
4722 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004723
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004724 if (sample_type & PERF_SAMPLE_TRANSACTION)
4725 perf_output_put(handle, data->txn);
4726
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004727 if (!event->attr.watermark) {
4728 int wakeup_events = event->attr.wakeup_events;
4729
4730 if (wakeup_events) {
4731 struct ring_buffer *rb = handle->rb;
4732 int events = local_inc_return(&rb->events);
4733
4734 if (events >= wakeup_events) {
4735 local_sub(wakeup_events, &rb->events);
4736 local_inc(&rb->wakeup);
4737 }
4738 }
4739 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004740}
4741
4742void perf_prepare_sample(struct perf_event_header *header,
4743 struct perf_sample_data *data,
4744 struct perf_event *event,
4745 struct pt_regs *regs)
4746{
4747 u64 sample_type = event->attr.sample_type;
4748
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004749 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004750 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004751
4752 header->misc = 0;
4753 header->misc |= perf_misc_flags(regs);
4754
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004755 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004756
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004757 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004758 data->ip = perf_instruction_pointer(regs);
4759
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4761 int size = 1;
4762
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004763 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764
4765 if (data->callchain)
4766 size += data->callchain->nr;
4767
4768 header->size += size * sizeof(u64);
4769 }
4770
4771 if (sample_type & PERF_SAMPLE_RAW) {
4772 int size = sizeof(u32);
4773
4774 if (data->raw)
4775 size += data->raw->size;
4776 else
4777 size += sizeof(u32);
4778
4779 WARN_ON_ONCE(size & (sizeof(u64)-1));
4780 header->size += size;
4781 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004782
4783 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4784 int size = sizeof(u64); /* nr */
4785 if (data->br_stack) {
4786 size += data->br_stack->nr
4787 * sizeof(struct perf_branch_entry);
4788 }
4789 header->size += size;
4790 }
Jiri Olsa40189942012-08-07 15:20:37 +02004791
4792 if (sample_type & PERF_SAMPLE_REGS_USER) {
4793 /* regs dump ABI info */
4794 int size = sizeof(u64);
4795
4796 perf_sample_regs_user(&data->regs_user, regs);
4797
4798 if (data->regs_user.regs) {
4799 u64 mask = event->attr.sample_regs_user;
4800 size += hweight64(mask) * sizeof(u64);
4801 }
4802
4803 header->size += size;
4804 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004805
4806 if (sample_type & PERF_SAMPLE_STACK_USER) {
4807 /*
4808 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4809 * processed as the last one or have additional check added
4810 * in case new sample type is added, because we could eat
4811 * up the rest of the sample size.
4812 */
4813 struct perf_regs_user *uregs = &data->regs_user;
4814 u16 stack_size = event->attr.sample_stack_user;
4815 u16 size = sizeof(u64);
4816
4817 if (!uregs->abi)
4818 perf_sample_regs_user(uregs, regs);
4819
4820 stack_size = perf_sample_ustack_size(stack_size, header->size,
4821 uregs->regs);
4822
4823 /*
4824 * If there is something to dump, add space for the dump
4825 * itself and for the field that tells the dynamic size,
4826 * which is how many have been actually dumped.
4827 */
4828 if (stack_size)
4829 size += sizeof(u64) + stack_size;
4830
4831 data->stack_user_size = stack_size;
4832 header->size += size;
4833 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004834}
4835
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004836static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004837 struct perf_sample_data *data,
4838 struct pt_regs *regs)
4839{
4840 struct perf_output_handle handle;
4841 struct perf_event_header header;
4842
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004843 /* protect the callchain buffers */
4844 rcu_read_lock();
4845
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846 perf_prepare_sample(&header, data, event, regs);
4847
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004848 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004849 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004850
4851 perf_output_sample(&handle, &header, data, event);
4852
4853 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004854
4855exit:
4856 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857}
4858
4859/*
4860 * read event_id
4861 */
4862
4863struct perf_read_event {
4864 struct perf_event_header header;
4865
4866 u32 pid;
4867 u32 tid;
4868};
4869
4870static void
4871perf_event_read_event(struct perf_event *event,
4872 struct task_struct *task)
4873{
4874 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004875 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004876 struct perf_read_event read_event = {
4877 .header = {
4878 .type = PERF_RECORD_READ,
4879 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004880 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004881 },
4882 .pid = perf_event_pid(event, task),
4883 .tid = perf_event_tid(event, task),
4884 };
4885 int ret;
4886
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004887 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004888 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 if (ret)
4890 return;
4891
4892 perf_output_put(&handle, read_event);
4893 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004894 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004895
4896 perf_output_end(&handle);
4897}
4898
Jiri Olsa52d857a2013-05-06 18:27:18 +02004899typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4900
4901static void
4902perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004903 perf_event_aux_output_cb output,
4904 void *data)
4905{
4906 struct perf_event *event;
4907
4908 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4909 if (event->state < PERF_EVENT_STATE_INACTIVE)
4910 continue;
4911 if (!event_filter_match(event))
4912 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02004913 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004914 }
4915}
4916
4917static void
Jiri Olsa67516842013-07-09 18:56:31 +02004918perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004919 struct perf_event_context *task_ctx)
4920{
4921 struct perf_cpu_context *cpuctx;
4922 struct perf_event_context *ctx;
4923 struct pmu *pmu;
4924 int ctxn;
4925
4926 rcu_read_lock();
4927 list_for_each_entry_rcu(pmu, &pmus, entry) {
4928 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4929 if (cpuctx->unique_pmu != pmu)
4930 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02004931 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004932 if (task_ctx)
4933 goto next;
4934 ctxn = pmu->task_ctx_nr;
4935 if (ctxn < 0)
4936 goto next;
4937 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4938 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02004939 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004940next:
4941 put_cpu_ptr(pmu->pmu_cpu_context);
4942 }
4943
4944 if (task_ctx) {
4945 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02004946 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004947 preempt_enable();
4948 }
4949 rcu_read_unlock();
4950}
4951
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952/*
4953 * task tracking -- fork/exit
4954 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02004955 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004956 */
4957
4958struct perf_task_event {
4959 struct task_struct *task;
4960 struct perf_event_context *task_ctx;
4961
4962 struct {
4963 struct perf_event_header header;
4964
4965 u32 pid;
4966 u32 ppid;
4967 u32 tid;
4968 u32 ptid;
4969 u64 time;
4970 } event_id;
4971};
4972
Jiri Olsa67516842013-07-09 18:56:31 +02004973static int perf_event_task_match(struct perf_event *event)
4974{
Stephane Eranian13d7a242013-08-21 12:10:24 +02004975 return event->attr.comm || event->attr.mmap ||
4976 event->attr.mmap2 || event->attr.mmap_data ||
4977 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02004978}
4979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004980static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004981 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004983 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004984 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004985 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004986 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004987 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004988
Jiri Olsa67516842013-07-09 18:56:31 +02004989 if (!perf_event_task_match(event))
4990 return;
4991
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004992 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004993
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004994 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004995 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004996 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004997 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998
4999 task_event->event_id.pid = perf_event_pid(event, task);
5000 task_event->event_id.ppid = perf_event_pid(event, current);
5001
5002 task_event->event_id.tid = perf_event_tid(event, task);
5003 task_event->event_id.ptid = perf_event_tid(event, current);
5004
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005005 perf_output_put(&handle, task_event->event_id);
5006
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005007 perf_event__output_id_sample(event, &handle, &sample);
5008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005010out:
5011 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012}
5013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005014static void perf_event_task(struct task_struct *task,
5015 struct perf_event_context *task_ctx,
5016 int new)
5017{
5018 struct perf_task_event task_event;
5019
5020 if (!atomic_read(&nr_comm_events) &&
5021 !atomic_read(&nr_mmap_events) &&
5022 !atomic_read(&nr_task_events))
5023 return;
5024
5025 task_event = (struct perf_task_event){
5026 .task = task,
5027 .task_ctx = task_ctx,
5028 .event_id = {
5029 .header = {
5030 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5031 .misc = 0,
5032 .size = sizeof(task_event.event_id),
5033 },
5034 /* .pid */
5035 /* .ppid */
5036 /* .tid */
5037 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005038 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005039 },
5040 };
5041
Jiri Olsa67516842013-07-09 18:56:31 +02005042 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005043 &task_event,
5044 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005045}
5046
5047void perf_event_fork(struct task_struct *task)
5048{
5049 perf_event_task(task, NULL, 1);
5050}
5051
5052/*
5053 * comm tracking
5054 */
5055
5056struct perf_comm_event {
5057 struct task_struct *task;
5058 char *comm;
5059 int comm_size;
5060
5061 struct {
5062 struct perf_event_header header;
5063
5064 u32 pid;
5065 u32 tid;
5066 } event_id;
5067};
5068
Jiri Olsa67516842013-07-09 18:56:31 +02005069static int perf_event_comm_match(struct perf_event *event)
5070{
5071 return event->attr.comm;
5072}
5073
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005074static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005075 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005077 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005079 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005080 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005081 int ret;
5082
Jiri Olsa67516842013-07-09 18:56:31 +02005083 if (!perf_event_comm_match(event))
5084 return;
5085
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005086 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5087 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005088 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005089
5090 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005091 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005092
5093 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5094 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5095
5096 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005097 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005098 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005099
5100 perf_event__output_id_sample(event, &handle, &sample);
5101
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005102 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005103out:
5104 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005105}
5106
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005107static void perf_event_comm_event(struct perf_comm_event *comm_event)
5108{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005110 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111
5112 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005113 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005114 size = ALIGN(strlen(comm)+1, sizeof(u64));
5115
5116 comm_event->comm = comm;
5117 comm_event->comm_size = size;
5118
5119 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005120
Jiri Olsa67516842013-07-09 18:56:31 +02005121 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005122 comm_event,
5123 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005124}
5125
Adrian Hunter82b89772014-05-28 11:45:04 +03005126void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005127{
5128 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005129
5130 if (!atomic_read(&nr_comm_events))
5131 return;
5132
5133 comm_event = (struct perf_comm_event){
5134 .task = task,
5135 /* .comm */
5136 /* .comm_size */
5137 .event_id = {
5138 .header = {
5139 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005140 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005141 /* .size */
5142 },
5143 /* .pid */
5144 /* .tid */
5145 },
5146 };
5147
5148 perf_event_comm_event(&comm_event);
5149}
5150
5151/*
5152 * mmap tracking
5153 */
5154
5155struct perf_mmap_event {
5156 struct vm_area_struct *vma;
5157
5158 const char *file_name;
5159 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005160 int maj, min;
5161 u64 ino;
5162 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005163 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005164
5165 struct {
5166 struct perf_event_header header;
5167
5168 u32 pid;
5169 u32 tid;
5170 u64 start;
5171 u64 len;
5172 u64 pgoff;
5173 } event_id;
5174};
5175
Jiri Olsa67516842013-07-09 18:56:31 +02005176static int perf_event_mmap_match(struct perf_event *event,
5177 void *data)
5178{
5179 struct perf_mmap_event *mmap_event = data;
5180 struct vm_area_struct *vma = mmap_event->vma;
5181 int executable = vma->vm_flags & VM_EXEC;
5182
5183 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005184 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005185}
5186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005187static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005188 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005189{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005190 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005191 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005192 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005193 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005194 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005195
Jiri Olsa67516842013-07-09 18:56:31 +02005196 if (!perf_event_mmap_match(event, data))
5197 return;
5198
Stephane Eranian13d7a242013-08-21 12:10:24 +02005199 if (event->attr.mmap2) {
5200 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5201 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5202 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5203 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005204 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005205 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5206 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005207 }
5208
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005209 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5210 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005211 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005212 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005213 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214
5215 mmap_event->event_id.pid = perf_event_pid(event, current);
5216 mmap_event->event_id.tid = perf_event_tid(event, current);
5217
5218 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005219
5220 if (event->attr.mmap2) {
5221 perf_output_put(&handle, mmap_event->maj);
5222 perf_output_put(&handle, mmap_event->min);
5223 perf_output_put(&handle, mmap_event->ino);
5224 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005225 perf_output_put(&handle, mmap_event->prot);
5226 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005227 }
5228
Frederic Weisbecker76369132011-05-19 19:55:04 +02005229 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005230 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005231
5232 perf_event__output_id_sample(event, &handle, &sample);
5233
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005234 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005235out:
5236 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005237}
5238
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5240{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241 struct vm_area_struct *vma = mmap_event->vma;
5242 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005243 int maj = 0, min = 0;
5244 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005245 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246 unsigned int size;
5247 char tmp[16];
5248 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005249 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250
5251 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005252 struct inode *inode;
5253 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005254
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005255 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005256 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005257 name = "//enomem";
5258 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005261 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005262 * need to add enough zero bytes after the string to handle
5263 * the 64bit alignment we do later.
5264 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005265 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005267 name = "//toolong";
5268 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005270 inode = file_inode(vma->vm_file);
5271 dev = inode->i_sb->s_dev;
5272 ino = inode->i_ino;
5273 gen = inode->i_generation;
5274 maj = MAJOR(dev);
5275 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005276
5277 if (vma->vm_flags & VM_READ)
5278 prot |= PROT_READ;
5279 if (vma->vm_flags & VM_WRITE)
5280 prot |= PROT_WRITE;
5281 if (vma->vm_flags & VM_EXEC)
5282 prot |= PROT_EXEC;
5283
5284 if (vma->vm_flags & VM_MAYSHARE)
5285 flags = MAP_SHARED;
5286 else
5287 flags = MAP_PRIVATE;
5288
5289 if (vma->vm_flags & VM_DENYWRITE)
5290 flags |= MAP_DENYWRITE;
5291 if (vma->vm_flags & VM_MAYEXEC)
5292 flags |= MAP_EXECUTABLE;
5293 if (vma->vm_flags & VM_LOCKED)
5294 flags |= MAP_LOCKED;
5295 if (vma->vm_flags & VM_HUGETLB)
5296 flags |= MAP_HUGETLB;
5297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005298 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005300 if (vma->vm_ops && vma->vm_ops->name) {
5301 name = (char *) vma->vm_ops->name(vma);
5302 if (name)
5303 goto cpy_name;
5304 }
5305
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005306 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005307 if (name)
5308 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005309
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005310 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005311 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005312 name = "[heap]";
5313 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005314 }
5315 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005316 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005317 name = "[stack]";
5318 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005319 }
5320
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005321 name = "//anon";
5322 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323 }
5324
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005325cpy_name:
5326 strlcpy(tmp, name, sizeof(tmp));
5327 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005328got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005329 /*
5330 * Since our buffer works in 8 byte units we need to align our string
5331 * size to a multiple of 8. However, we must guarantee the tail end is
5332 * zero'd out to avoid leaking random bits to userspace.
5333 */
5334 size = strlen(name)+1;
5335 while (!IS_ALIGNED(size, sizeof(u64)))
5336 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005337
5338 mmap_event->file_name = name;
5339 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005340 mmap_event->maj = maj;
5341 mmap_event->min = min;
5342 mmap_event->ino = ino;
5343 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005344 mmap_event->prot = prot;
5345 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005346
Stephane Eranian2fe85422013-01-24 16:10:39 +01005347 if (!(vma->vm_flags & VM_EXEC))
5348 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005350 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5351
Jiri Olsa67516842013-07-09 18:56:31 +02005352 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005353 mmap_event,
5354 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005355
5356 kfree(buf);
5357}
5358
Eric B Munson3af9e852010-05-18 15:30:49 +01005359void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005360{
5361 struct perf_mmap_event mmap_event;
5362
5363 if (!atomic_read(&nr_mmap_events))
5364 return;
5365
5366 mmap_event = (struct perf_mmap_event){
5367 .vma = vma,
5368 /* .file_name */
5369 /* .file_size */
5370 .event_id = {
5371 .header = {
5372 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005373 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005374 /* .size */
5375 },
5376 /* .pid */
5377 /* .tid */
5378 .start = vma->vm_start,
5379 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005380 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005382 /* .maj (attr_mmap2 only) */
5383 /* .min (attr_mmap2 only) */
5384 /* .ino (attr_mmap2 only) */
5385 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005386 /* .prot (attr_mmap2 only) */
5387 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 };
5389
5390 perf_event_mmap_event(&mmap_event);
5391}
5392
5393/*
5394 * IRQ throttle logging
5395 */
5396
5397static void perf_log_throttle(struct perf_event *event, int enable)
5398{
5399 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005400 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005401 int ret;
5402
5403 struct {
5404 struct perf_event_header header;
5405 u64 time;
5406 u64 id;
5407 u64 stream_id;
5408 } throttle_event = {
5409 .header = {
5410 .type = PERF_RECORD_THROTTLE,
5411 .misc = 0,
5412 .size = sizeof(throttle_event),
5413 },
5414 .time = perf_clock(),
5415 .id = primary_event_id(event),
5416 .stream_id = event->id,
5417 };
5418
5419 if (enable)
5420 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5421
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005422 perf_event_header__init_id(&throttle_event.header, &sample, event);
5423
5424 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005425 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005426 if (ret)
5427 return;
5428
5429 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005430 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431 perf_output_end(&handle);
5432}
5433
5434/*
5435 * Generic event overflow handling, sampling.
5436 */
5437
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005438static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005439 int throttle, struct perf_sample_data *data,
5440 struct pt_regs *regs)
5441{
5442 int events = atomic_read(&event->event_limit);
5443 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005444 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 int ret = 0;
5446
Peter Zijlstra96398822010-11-24 18:55:29 +01005447 /*
5448 * Non-sampling counters might still use the PMI to fold short
5449 * hardware counters, ignore those.
5450 */
5451 if (unlikely(!is_sampling_event(event)))
5452 return 0;
5453
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005454 seq = __this_cpu_read(perf_throttled_seq);
5455 if (seq != hwc->interrupts_seq) {
5456 hwc->interrupts_seq = seq;
5457 hwc->interrupts = 1;
5458 } else {
5459 hwc->interrupts++;
5460 if (unlikely(throttle
5461 && hwc->interrupts >= max_samples_per_tick)) {
5462 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005463 hwc->interrupts = MAX_INTERRUPTS;
5464 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005465 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005466 ret = 1;
5467 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005468 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469
5470 if (event->attr.freq) {
5471 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005472 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005473
Peter Zijlstraabd50712010-01-26 18:50:16 +01005474 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005475
Peter Zijlstraabd50712010-01-26 18:50:16 +01005476 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005477 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005478 }
5479
5480 /*
5481 * XXX event_limit might not quite work as expected on inherited
5482 * events
5483 */
5484
5485 event->pending_kill = POLL_IN;
5486 if (events && atomic_dec_and_test(&event->event_limit)) {
5487 ret = 1;
5488 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005489 event->pending_disable = 1;
5490 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005491 }
5492
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005493 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005494 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005495 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005496 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005497
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005498 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005499 event->pending_wakeup = 1;
5500 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005501 }
5502
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005503 return ret;
5504}
5505
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005506int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507 struct perf_sample_data *data,
5508 struct pt_regs *regs)
5509{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005510 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005511}
5512
5513/*
5514 * Generic software event infrastructure
5515 */
5516
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005517struct swevent_htable {
5518 struct swevent_hlist *swevent_hlist;
5519 struct mutex hlist_mutex;
5520 int hlist_refcount;
5521
5522 /* Recursion avoidance in each contexts */
5523 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005524
5525 /* Keeps track of cpu being initialized/exited */
5526 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005527};
5528
5529static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5530
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005531/*
5532 * We directly increment event->count and keep a second value in
5533 * event->hw.period_left to count intervals. This period event
5534 * is kept in the range [-sample_period, 0] so that we can use the
5535 * sign as trigger.
5536 */
5537
Jiri Olsaab573842013-05-01 17:25:44 +02005538u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005539{
5540 struct hw_perf_event *hwc = &event->hw;
5541 u64 period = hwc->last_period;
5542 u64 nr, offset;
5543 s64 old, val;
5544
5545 hwc->last_period = hwc->sample_period;
5546
5547again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005548 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005549 if (val < 0)
5550 return 0;
5551
5552 nr = div64_u64(period + val, period);
5553 offset = nr * period;
5554 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005555 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005556 goto again;
5557
5558 return nr;
5559}
5560
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005561static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005562 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005563 struct pt_regs *regs)
5564{
5565 struct hw_perf_event *hwc = &event->hw;
5566 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005567
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005568 if (!overflow)
5569 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005570
5571 if (hwc->interrupts == MAX_INTERRUPTS)
5572 return;
5573
5574 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005575 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576 data, regs)) {
5577 /*
5578 * We inhibit the overflow from happening when
5579 * hwc->interrupts == MAX_INTERRUPTS.
5580 */
5581 break;
5582 }
5583 throttle = 1;
5584 }
5585}
5586
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005587static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005588 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005589 struct pt_regs *regs)
5590{
5591 struct hw_perf_event *hwc = &event->hw;
5592
Peter Zijlstrae7850592010-05-21 14:43:08 +02005593 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005595 if (!regs)
5596 return;
5597
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005598 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005599 return;
5600
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005601 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5602 data->period = nr;
5603 return perf_swevent_overflow(event, 1, data, regs);
5604 } else
5605 data->period = event->hw.last_period;
5606
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005607 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005608 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005609
Peter Zijlstrae7850592010-05-21 14:43:08 +02005610 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005611 return;
5612
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005613 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005614}
5615
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005616static int perf_exclude_event(struct perf_event *event,
5617 struct pt_regs *regs)
5618{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005619 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005620 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005621
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005622 if (regs) {
5623 if (event->attr.exclude_user && user_mode(regs))
5624 return 1;
5625
5626 if (event->attr.exclude_kernel && !user_mode(regs))
5627 return 1;
5628 }
5629
5630 return 0;
5631}
5632
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005633static int perf_swevent_match(struct perf_event *event,
5634 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005635 u32 event_id,
5636 struct perf_sample_data *data,
5637 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005638{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005639 if (event->attr.type != type)
5640 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005642 if (event->attr.config != event_id)
5643 return 0;
5644
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005645 if (perf_exclude_event(event, regs))
5646 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647
5648 return 1;
5649}
5650
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005651static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005652{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005653 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005654
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005655 return hash_64(val, SWEVENT_HLIST_BITS);
5656}
5657
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005658static inline struct hlist_head *
5659__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005660{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005661 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005662
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005663 return &hlist->heads[hash];
5664}
5665
5666/* For the read side: events when they trigger */
5667static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005668find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005669{
5670 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005671
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005672 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005673 if (!hlist)
5674 return NULL;
5675
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005676 return __find_swevent_head(hlist, type, event_id);
5677}
5678
5679/* For the event head insertion and removal in the hlist */
5680static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005681find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005682{
5683 struct swevent_hlist *hlist;
5684 u32 event_id = event->attr.config;
5685 u64 type = event->attr.type;
5686
5687 /*
5688 * Event scheduling is always serialized against hlist allocation
5689 * and release. Which makes the protected version suitable here.
5690 * The context lock guarantees that.
5691 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005692 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005693 lockdep_is_held(&event->ctx->lock));
5694 if (!hlist)
5695 return NULL;
5696
5697 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005698}
5699
5700static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005701 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005702 struct perf_sample_data *data,
5703 struct pt_regs *regs)
5704{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005705 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005706 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005707 struct hlist_head *head;
5708
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005709 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005710 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005711 if (!head)
5712 goto end;
5713
Sasha Levinb67bfe02013-02-27 17:06:00 -08005714 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005715 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005716 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005717 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005718end:
5719 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005720}
5721
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005722int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005723{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005724 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005725
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005726 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005727}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005728EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005729
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005730inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005731{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005732 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005733
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005734 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005735}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005736
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005737void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005738{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005739 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005740 int rctx;
5741
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005742 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005743 rctx = perf_swevent_get_recursion_context();
5744 if (rctx < 0)
5745 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005746
Robert Richterfd0d0002012-04-02 20:19:08 +02005747 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005748
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005749 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005750
5751 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005752 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753}
5754
5755static void perf_swevent_read(struct perf_event *event)
5756{
5757}
5758
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005759static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005761 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005762 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005763 struct hlist_head *head;
5764
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005765 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005766 hwc->last_period = hwc->sample_period;
5767 perf_swevent_set_period(event);
5768 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005769
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005770 hwc->state = !(flags & PERF_EF_START);
5771
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005772 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02005773 if (!head) {
5774 /*
5775 * We can race with cpu hotplug code. Do not
5776 * WARN if the cpu just got unplugged.
5777 */
5778 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005779 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02005780 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005781
5782 hlist_add_head_rcu(&event->hlist_entry, head);
5783
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005784 return 0;
5785}
5786
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005787static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005788{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005789 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005790}
5791
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005792static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005793{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005794 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005795}
5796
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005797static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005798{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005799 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005800}
5801
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005802/* Deref the hlist from the update side */
5803static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005804swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005805{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005806 return rcu_dereference_protected(swhash->swevent_hlist,
5807 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005808}
5809
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005810static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005811{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005812 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005813
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005814 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005815 return;
5816
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005817 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005818 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005819}
5820
5821static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5822{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005823 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005824
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005825 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005826
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005827 if (!--swhash->hlist_refcount)
5828 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005829
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005830 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005831}
5832
5833static void swevent_hlist_put(struct perf_event *event)
5834{
5835 int cpu;
5836
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005837 for_each_possible_cpu(cpu)
5838 swevent_hlist_put_cpu(event, cpu);
5839}
5840
5841static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5842{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005843 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005844 int err = 0;
5845
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005846 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005847
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005848 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005849 struct swevent_hlist *hlist;
5850
5851 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5852 if (!hlist) {
5853 err = -ENOMEM;
5854 goto exit;
5855 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005856 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005857 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005858 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005859exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005860 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005861
5862 return err;
5863}
5864
5865static int swevent_hlist_get(struct perf_event *event)
5866{
5867 int err;
5868 int cpu, failed_cpu;
5869
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005870 get_online_cpus();
5871 for_each_possible_cpu(cpu) {
5872 err = swevent_hlist_get_cpu(event, cpu);
5873 if (err) {
5874 failed_cpu = cpu;
5875 goto fail;
5876 }
5877 }
5878 put_online_cpus();
5879
5880 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005881fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005882 for_each_possible_cpu(cpu) {
5883 if (cpu == failed_cpu)
5884 break;
5885 swevent_hlist_put_cpu(event, cpu);
5886 }
5887
5888 put_online_cpus();
5889 return err;
5890}
5891
Ingo Molnarc5905af2012-02-24 08:31:31 +01005892struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005893
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005894static void sw_perf_event_destroy(struct perf_event *event)
5895{
5896 u64 event_id = event->attr.config;
5897
5898 WARN_ON(event->parent);
5899
Ingo Molnarc5905af2012-02-24 08:31:31 +01005900 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005901 swevent_hlist_put(event);
5902}
5903
5904static int perf_swevent_init(struct perf_event *event)
5905{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005906 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005907
5908 if (event->attr.type != PERF_TYPE_SOFTWARE)
5909 return -ENOENT;
5910
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005911 /*
5912 * no branch sampling for software events
5913 */
5914 if (has_branch_stack(event))
5915 return -EOPNOTSUPP;
5916
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005917 switch (event_id) {
5918 case PERF_COUNT_SW_CPU_CLOCK:
5919 case PERF_COUNT_SW_TASK_CLOCK:
5920 return -ENOENT;
5921
5922 default:
5923 break;
5924 }
5925
Dan Carpenterce677832010-10-24 21:50:42 +02005926 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005927 return -ENOENT;
5928
5929 if (!event->parent) {
5930 int err;
5931
5932 err = swevent_hlist_get(event);
5933 if (err)
5934 return err;
5935
Ingo Molnarc5905af2012-02-24 08:31:31 +01005936 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005937 event->destroy = sw_perf_event_destroy;
5938 }
5939
5940 return 0;
5941}
5942
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005943static int perf_swevent_event_idx(struct perf_event *event)
5944{
5945 return 0;
5946}
5947
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005948static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005949 .task_ctx_nr = perf_sw_context,
5950
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005951 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005952 .add = perf_swevent_add,
5953 .del = perf_swevent_del,
5954 .start = perf_swevent_start,
5955 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005956 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005957
5958 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005959};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005960
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005961#ifdef CONFIG_EVENT_TRACING
5962
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005963static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005964 struct perf_sample_data *data)
5965{
5966 void *record = data->raw->data;
5967
5968 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5969 return 1;
5970 return 0;
5971}
5972
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005973static int perf_tp_event_match(struct perf_event *event,
5974 struct perf_sample_data *data,
5975 struct pt_regs *regs)
5976{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005977 if (event->hw.state & PERF_HES_STOPPED)
5978 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005979 /*
5980 * All tracepoints are from kernel-space.
5981 */
5982 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005983 return 0;
5984
5985 if (!perf_tp_filter_match(event, data))
5986 return 0;
5987
5988 return 1;
5989}
5990
5991void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005992 struct pt_regs *regs, struct hlist_head *head, int rctx,
5993 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005994{
5995 struct perf_sample_data data;
5996 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005997
5998 struct perf_raw_record raw = {
5999 .size = entry_size,
6000 .data = record,
6001 };
6002
Robert Richterfd0d0002012-04-02 20:19:08 +02006003 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006004 data.raw = &raw;
6005
Sasha Levinb67bfe02013-02-27 17:06:00 -08006006 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006007 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006008 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006009 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006010
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006011 /*
6012 * If we got specified a target task, also iterate its context and
6013 * deliver this event there too.
6014 */
6015 if (task && task != current) {
6016 struct perf_event_context *ctx;
6017 struct trace_entry *entry = record;
6018
6019 rcu_read_lock();
6020 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6021 if (!ctx)
6022 goto unlock;
6023
6024 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6025 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6026 continue;
6027 if (event->attr.config != entry->type)
6028 continue;
6029 if (perf_tp_event_match(event, &data, regs))
6030 perf_swevent_event(event, count, &data, regs);
6031 }
6032unlock:
6033 rcu_read_unlock();
6034 }
6035
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006036 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006037}
6038EXPORT_SYMBOL_GPL(perf_tp_event);
6039
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006040static void tp_perf_event_destroy(struct perf_event *event)
6041{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006042 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006043}
6044
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006045static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006046{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006047 int err;
6048
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006049 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6050 return -ENOENT;
6051
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006052 /*
6053 * no branch sampling for tracepoint events
6054 */
6055 if (has_branch_stack(event))
6056 return -EOPNOTSUPP;
6057
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006058 err = perf_trace_init(event);
6059 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006060 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006061
6062 event->destroy = tp_perf_event_destroy;
6063
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006064 return 0;
6065}
6066
6067static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006068 .task_ctx_nr = perf_sw_context,
6069
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006070 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006071 .add = perf_trace_add,
6072 .del = perf_trace_del,
6073 .start = perf_swevent_start,
6074 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006075 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006076
6077 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006078};
6079
6080static inline void perf_tp_register(void)
6081{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006082 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006083}
Li Zefan6fb29152009-10-15 11:21:42 +08006084
6085static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6086{
6087 char *filter_str;
6088 int ret;
6089
6090 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6091 return -EINVAL;
6092
6093 filter_str = strndup_user(arg, PAGE_SIZE);
6094 if (IS_ERR(filter_str))
6095 return PTR_ERR(filter_str);
6096
6097 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6098
6099 kfree(filter_str);
6100 return ret;
6101}
6102
6103static void perf_event_free_filter(struct perf_event *event)
6104{
6105 ftrace_profile_free_filter(event);
6106}
6107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006108#else
Li Zefan6fb29152009-10-15 11:21:42 +08006109
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006110static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006111{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006112}
Li Zefan6fb29152009-10-15 11:21:42 +08006113
6114static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6115{
6116 return -ENOENT;
6117}
6118
6119static void perf_event_free_filter(struct perf_event *event)
6120{
6121}
6122
Li Zefan07b139c2009-12-21 14:27:35 +08006123#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006124
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006125#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006126void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006127{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006128 struct perf_sample_data sample;
6129 struct pt_regs *regs = data;
6130
Robert Richterfd0d0002012-04-02 20:19:08 +02006131 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006132
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006133 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006134 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006135}
6136#endif
6137
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006138/*
6139 * hrtimer based swevent callback
6140 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006141
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006142static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006143{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006144 enum hrtimer_restart ret = HRTIMER_RESTART;
6145 struct perf_sample_data data;
6146 struct pt_regs *regs;
6147 struct perf_event *event;
6148 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006149
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006150 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006151
6152 if (event->state != PERF_EVENT_STATE_ACTIVE)
6153 return HRTIMER_NORESTART;
6154
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006155 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006156
Robert Richterfd0d0002012-04-02 20:19:08 +02006157 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006158 regs = get_irq_regs();
6159
6160 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006161 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006162 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006163 ret = HRTIMER_NORESTART;
6164 }
6165
6166 period = max_t(u64, 10000, event->hw.sample_period);
6167 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6168
6169 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006170}
6171
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006172static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006173{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006174 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006175 s64 period;
6176
6177 if (!is_sampling_event(event))
6178 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006179
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006180 period = local64_read(&hwc->period_left);
6181 if (period) {
6182 if (period < 0)
6183 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006184
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006185 local64_set(&hwc->period_left, 0);
6186 } else {
6187 period = max_t(u64, 10000, hwc->sample_period);
6188 }
6189 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006190 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006191 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006192}
6193
6194static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6195{
6196 struct hw_perf_event *hwc = &event->hw;
6197
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006198 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006199 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006200 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006201
6202 hrtimer_cancel(&hwc->hrtimer);
6203 }
6204}
6205
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006206static void perf_swevent_init_hrtimer(struct perf_event *event)
6207{
6208 struct hw_perf_event *hwc = &event->hw;
6209
6210 if (!is_sampling_event(event))
6211 return;
6212
6213 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6214 hwc->hrtimer.function = perf_swevent_hrtimer;
6215
6216 /*
6217 * Since hrtimers have a fixed rate, we can do a static freq->period
6218 * mapping and avoid the whole period adjust feedback stuff.
6219 */
6220 if (event->attr.freq) {
6221 long freq = event->attr.sample_freq;
6222
6223 event->attr.sample_period = NSEC_PER_SEC / freq;
6224 hwc->sample_period = event->attr.sample_period;
6225 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006226 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006227 event->attr.freq = 0;
6228 }
6229}
6230
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006231/*
6232 * Software event: cpu wall time clock
6233 */
6234
6235static void cpu_clock_event_update(struct perf_event *event)
6236{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006237 s64 prev;
6238 u64 now;
6239
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006240 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006241 prev = local64_xchg(&event->hw.prev_count, now);
6242 local64_add(now - prev, &event->count);
6243}
6244
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006245static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006246{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006247 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006248 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006249}
6250
6251static void cpu_clock_event_stop(struct perf_event *event, int flags)
6252{
6253 perf_swevent_cancel_hrtimer(event);
6254 cpu_clock_event_update(event);
6255}
6256
6257static int cpu_clock_event_add(struct perf_event *event, int flags)
6258{
6259 if (flags & PERF_EF_START)
6260 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006261
6262 return 0;
6263}
6264
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006265static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006266{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006267 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006268}
6269
6270static void cpu_clock_event_read(struct perf_event *event)
6271{
6272 cpu_clock_event_update(event);
6273}
6274
6275static int cpu_clock_event_init(struct perf_event *event)
6276{
6277 if (event->attr.type != PERF_TYPE_SOFTWARE)
6278 return -ENOENT;
6279
6280 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6281 return -ENOENT;
6282
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006283 /*
6284 * no branch sampling for software events
6285 */
6286 if (has_branch_stack(event))
6287 return -EOPNOTSUPP;
6288
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006289 perf_swevent_init_hrtimer(event);
6290
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006291 return 0;
6292}
6293
6294static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006295 .task_ctx_nr = perf_sw_context,
6296
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006297 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006298 .add = cpu_clock_event_add,
6299 .del = cpu_clock_event_del,
6300 .start = cpu_clock_event_start,
6301 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006302 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006303
6304 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006305};
6306
6307/*
6308 * Software event: task time clock
6309 */
6310
6311static void task_clock_event_update(struct perf_event *event, u64 now)
6312{
6313 u64 prev;
6314 s64 delta;
6315
6316 prev = local64_xchg(&event->hw.prev_count, now);
6317 delta = now - prev;
6318 local64_add(delta, &event->count);
6319}
6320
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006321static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006322{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006323 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006324 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006325}
6326
6327static void task_clock_event_stop(struct perf_event *event, int flags)
6328{
6329 perf_swevent_cancel_hrtimer(event);
6330 task_clock_event_update(event, event->ctx->time);
6331}
6332
6333static int task_clock_event_add(struct perf_event *event, int flags)
6334{
6335 if (flags & PERF_EF_START)
6336 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006337
6338 return 0;
6339}
6340
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006341static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006342{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006343 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006344}
6345
6346static void task_clock_event_read(struct perf_event *event)
6347{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006348 u64 now = perf_clock();
6349 u64 delta = now - event->ctx->timestamp;
6350 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006351
6352 task_clock_event_update(event, time);
6353}
6354
6355static int task_clock_event_init(struct perf_event *event)
6356{
6357 if (event->attr.type != PERF_TYPE_SOFTWARE)
6358 return -ENOENT;
6359
6360 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6361 return -ENOENT;
6362
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006363 /*
6364 * no branch sampling for software events
6365 */
6366 if (has_branch_stack(event))
6367 return -EOPNOTSUPP;
6368
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006369 perf_swevent_init_hrtimer(event);
6370
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006371 return 0;
6372}
6373
6374static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006375 .task_ctx_nr = perf_sw_context,
6376
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006377 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006378 .add = task_clock_event_add,
6379 .del = task_clock_event_del,
6380 .start = task_clock_event_start,
6381 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006382 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006383
6384 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006385};
6386
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006387static void perf_pmu_nop_void(struct pmu *pmu)
6388{
6389}
6390
6391static int perf_pmu_nop_int(struct pmu *pmu)
6392{
6393 return 0;
6394}
6395
6396static void perf_pmu_start_txn(struct pmu *pmu)
6397{
6398 perf_pmu_disable(pmu);
6399}
6400
6401static int perf_pmu_commit_txn(struct pmu *pmu)
6402{
6403 perf_pmu_enable(pmu);
6404 return 0;
6405}
6406
6407static void perf_pmu_cancel_txn(struct pmu *pmu)
6408{
6409 perf_pmu_enable(pmu);
6410}
6411
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006412static int perf_event_idx_default(struct perf_event *event)
6413{
6414 return event->hw.idx + 1;
6415}
6416
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006417/*
6418 * Ensures all contexts with the same task_ctx_nr have the same
6419 * pmu_cpu_context too.
6420 */
Mark Rutland9e317042014-02-10 17:44:18 +00006421static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006422{
6423 struct pmu *pmu;
6424
6425 if (ctxn < 0)
6426 return NULL;
6427
6428 list_for_each_entry(pmu, &pmus, entry) {
6429 if (pmu->task_ctx_nr == ctxn)
6430 return pmu->pmu_cpu_context;
6431 }
6432
6433 return NULL;
6434}
6435
Peter Zijlstra51676952010-12-07 14:18:20 +01006436static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006437{
Peter Zijlstra51676952010-12-07 14:18:20 +01006438 int cpu;
6439
6440 for_each_possible_cpu(cpu) {
6441 struct perf_cpu_context *cpuctx;
6442
6443 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6444
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006445 if (cpuctx->unique_pmu == old_pmu)
6446 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006447 }
6448}
6449
6450static void free_pmu_context(struct pmu *pmu)
6451{
6452 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006453
6454 mutex_lock(&pmus_lock);
6455 /*
6456 * Like a real lame refcount.
6457 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006458 list_for_each_entry(i, &pmus, entry) {
6459 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6460 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006461 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006462 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006463 }
6464
Peter Zijlstra51676952010-12-07 14:18:20 +01006465 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006466out:
6467 mutex_unlock(&pmus_lock);
6468}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006469static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006470
Peter Zijlstraabe43402010-11-17 23:17:37 +01006471static ssize_t
6472type_show(struct device *dev, struct device_attribute *attr, char *page)
6473{
6474 struct pmu *pmu = dev_get_drvdata(dev);
6475
6476 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6477}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006478static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006479
Stephane Eranian62b85632013-04-03 14:21:34 +02006480static ssize_t
6481perf_event_mux_interval_ms_show(struct device *dev,
6482 struct device_attribute *attr,
6483 char *page)
6484{
6485 struct pmu *pmu = dev_get_drvdata(dev);
6486
6487 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6488}
6489
6490static ssize_t
6491perf_event_mux_interval_ms_store(struct device *dev,
6492 struct device_attribute *attr,
6493 const char *buf, size_t count)
6494{
6495 struct pmu *pmu = dev_get_drvdata(dev);
6496 int timer, cpu, ret;
6497
6498 ret = kstrtoint(buf, 0, &timer);
6499 if (ret)
6500 return ret;
6501
6502 if (timer < 1)
6503 return -EINVAL;
6504
6505 /* same value, noting to do */
6506 if (timer == pmu->hrtimer_interval_ms)
6507 return count;
6508
6509 pmu->hrtimer_interval_ms = timer;
6510
6511 /* update all cpuctx for this PMU */
6512 for_each_possible_cpu(cpu) {
6513 struct perf_cpu_context *cpuctx;
6514 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6515 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6516
6517 if (hrtimer_active(&cpuctx->hrtimer))
6518 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6519 }
6520
6521 return count;
6522}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006523static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006524
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006525static struct attribute *pmu_dev_attrs[] = {
6526 &dev_attr_type.attr,
6527 &dev_attr_perf_event_mux_interval_ms.attr,
6528 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006529};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006530ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006531
6532static int pmu_bus_running;
6533static struct bus_type pmu_bus = {
6534 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006535 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006536};
6537
6538static void pmu_dev_release(struct device *dev)
6539{
6540 kfree(dev);
6541}
6542
6543static int pmu_dev_alloc(struct pmu *pmu)
6544{
6545 int ret = -ENOMEM;
6546
6547 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6548 if (!pmu->dev)
6549 goto out;
6550
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006551 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006552 device_initialize(pmu->dev);
6553 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6554 if (ret)
6555 goto free_dev;
6556
6557 dev_set_drvdata(pmu->dev, pmu);
6558 pmu->dev->bus = &pmu_bus;
6559 pmu->dev->release = pmu_dev_release;
6560 ret = device_add(pmu->dev);
6561 if (ret)
6562 goto free_dev;
6563
6564out:
6565 return ret;
6566
6567free_dev:
6568 put_device(pmu->dev);
6569 goto out;
6570}
6571
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006572static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006573static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006574
Mischa Jonker03d8e802013-06-04 11:45:48 +02006575int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006576{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006577 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006578
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006579 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006580 ret = -ENOMEM;
6581 pmu->pmu_disable_count = alloc_percpu(int);
6582 if (!pmu->pmu_disable_count)
6583 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006584
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006585 pmu->type = -1;
6586 if (!name)
6587 goto skip_type;
6588 pmu->name = name;
6589
6590 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006591 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6592 if (type < 0) {
6593 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006594 goto free_pdc;
6595 }
6596 }
6597 pmu->type = type;
6598
Peter Zijlstraabe43402010-11-17 23:17:37 +01006599 if (pmu_bus_running) {
6600 ret = pmu_dev_alloc(pmu);
6601 if (ret)
6602 goto free_idr;
6603 }
6604
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006605skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006606 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6607 if (pmu->pmu_cpu_context)
6608 goto got_cpu_context;
6609
Wei Yongjunc4814202013-04-12 11:05:54 +08006610 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006611 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6612 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006613 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006614
6615 for_each_possible_cpu(cpu) {
6616 struct perf_cpu_context *cpuctx;
6617
6618 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006619 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006620 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006621 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006622 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006623 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006624
6625 __perf_cpu_hrtimer_init(cpuctx, cpu);
6626
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006627 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006628 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006629 }
6630
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006631got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006632 if (!pmu->start_txn) {
6633 if (pmu->pmu_enable) {
6634 /*
6635 * If we have pmu_enable/pmu_disable calls, install
6636 * transaction stubs that use that to try and batch
6637 * hardware accesses.
6638 */
6639 pmu->start_txn = perf_pmu_start_txn;
6640 pmu->commit_txn = perf_pmu_commit_txn;
6641 pmu->cancel_txn = perf_pmu_cancel_txn;
6642 } else {
6643 pmu->start_txn = perf_pmu_nop_void;
6644 pmu->commit_txn = perf_pmu_nop_int;
6645 pmu->cancel_txn = perf_pmu_nop_void;
6646 }
6647 }
6648
6649 if (!pmu->pmu_enable) {
6650 pmu->pmu_enable = perf_pmu_nop_void;
6651 pmu->pmu_disable = perf_pmu_nop_void;
6652 }
6653
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006654 if (!pmu->event_idx)
6655 pmu->event_idx = perf_event_idx_default;
6656
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006657 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006658 ret = 0;
6659unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006660 mutex_unlock(&pmus_lock);
6661
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006662 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006663
Peter Zijlstraabe43402010-11-17 23:17:37 +01006664free_dev:
6665 device_del(pmu->dev);
6666 put_device(pmu->dev);
6667
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006668free_idr:
6669 if (pmu->type >= PERF_TYPE_MAX)
6670 idr_remove(&pmu_idr, pmu->type);
6671
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006672free_pdc:
6673 free_percpu(pmu->pmu_disable_count);
6674 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006675}
Yan, Zhengc464c762014-03-18 16:56:41 +08006676EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006677
6678void perf_pmu_unregister(struct pmu *pmu)
6679{
6680 mutex_lock(&pmus_lock);
6681 list_del_rcu(&pmu->entry);
6682 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006683
6684 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006685 * We dereference the pmu list under both SRCU and regular RCU, so
6686 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006688 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006689 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006690
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006691 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006692 if (pmu->type >= PERF_TYPE_MAX)
6693 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006694 device_del(pmu->dev);
6695 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006696 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006697}
Yan, Zhengc464c762014-03-18 16:56:41 +08006698EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006699
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006700struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006701{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006702 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006703 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006704 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006705
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006706 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006707
6708 rcu_read_lock();
6709 pmu = idr_find(&pmu_idr, event->attr.type);
6710 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006711 if (pmu) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006712 if (!try_module_get(pmu->module)) {
6713 pmu = ERR_PTR(-ENODEV);
6714 goto unlock;
6715 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006716 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006717 ret = pmu->event_init(event);
6718 if (ret)
6719 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006720 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006721 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006722
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006723 list_for_each_entry_rcu(pmu, &pmus, entry) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006724 if (!try_module_get(pmu->module)) {
6725 pmu = ERR_PTR(-ENODEV);
6726 goto unlock;
6727 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006728 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006729 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006730 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006731 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006732
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006733 if (ret != -ENOENT) {
6734 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006735 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006736 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006737 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006738 pmu = ERR_PTR(-ENOENT);
6739unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006740 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006741
6742 return pmu;
6743}
6744
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006745static void account_event_cpu(struct perf_event *event, int cpu)
6746{
6747 if (event->parent)
6748 return;
6749
6750 if (has_branch_stack(event)) {
6751 if (!(event->attach_state & PERF_ATTACH_TASK))
6752 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6753 }
6754 if (is_cgroup_event(event))
6755 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6756}
6757
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006758static void account_event(struct perf_event *event)
6759{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006760 if (event->parent)
6761 return;
6762
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006763 if (event->attach_state & PERF_ATTACH_TASK)
6764 static_key_slow_inc(&perf_sched_events.key);
6765 if (event->attr.mmap || event->attr.mmap_data)
6766 atomic_inc(&nr_mmap_events);
6767 if (event->attr.comm)
6768 atomic_inc(&nr_comm_events);
6769 if (event->attr.task)
6770 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006771 if (event->attr.freq) {
6772 if (atomic_inc_return(&nr_freq_events) == 1)
6773 tick_nohz_full_kick_all();
6774 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006775 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006776 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006777 if (is_cgroup_event(event))
6778 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006779
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006780 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006781}
6782
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006783/*
6784 * Allocate and initialize a event structure
6785 */
6786static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006787perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006788 struct task_struct *task,
6789 struct perf_event *group_leader,
6790 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006791 perf_overflow_handler_t overflow_handler,
6792 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006793{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006794 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006795 struct perf_event *event;
6796 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006797 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006798
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006799 if ((unsigned)cpu >= nr_cpu_ids) {
6800 if (!task || cpu != -1)
6801 return ERR_PTR(-EINVAL);
6802 }
6803
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006804 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006805 if (!event)
6806 return ERR_PTR(-ENOMEM);
6807
6808 /*
6809 * Single events are their own group leaders, with an
6810 * empty sibling list:
6811 */
6812 if (!group_leader)
6813 group_leader = event;
6814
6815 mutex_init(&event->child_mutex);
6816 INIT_LIST_HEAD(&event->child_list);
6817
6818 INIT_LIST_HEAD(&event->group_entry);
6819 INIT_LIST_HEAD(&event->event_entry);
6820 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006821 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006822 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006823 INIT_HLIST_NODE(&event->hlist_entry);
6824
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006826 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006827 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006828
6829 mutex_init(&event->mmap_mutex);
6830
Al Viroa6fa9412012-08-20 14:59:25 +01006831 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006832 event->cpu = cpu;
6833 event->attr = *attr;
6834 event->group_leader = group_leader;
6835 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006836 event->oncpu = -1;
6837
6838 event->parent = parent_event;
6839
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006840 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006841 event->id = atomic64_inc_return(&perf_event_id);
6842
6843 event->state = PERF_EVENT_STATE_INACTIVE;
6844
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006845 if (task) {
6846 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006847
6848 if (attr->type == PERF_TYPE_TRACEPOINT)
6849 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006850#ifdef CONFIG_HAVE_HW_BREAKPOINT
6851 /*
6852 * hw_breakpoint is a bit difficult here..
6853 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006854 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006855 event->hw.bp_target = task;
6856#endif
6857 }
6858
Avi Kivity4dc0da82011-06-29 18:42:35 +03006859 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006860 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006861 context = parent_event->overflow_handler_context;
6862 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006863
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006864 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006865 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006866
Jiri Olsa0231bb52013-02-01 11:23:45 +01006867 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006868
6869 pmu = NULL;
6870
6871 hwc = &event->hw;
6872 hwc->sample_period = attr->sample_period;
6873 if (attr->freq && attr->sample_freq)
6874 hwc->sample_period = 1;
6875 hwc->last_period = hwc->sample_period;
6876
Peter Zijlstrae7850592010-05-21 14:43:08 +02006877 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006878
6879 /*
6880 * we currently do not support PERF_FORMAT_GROUP on inherited events
6881 */
6882 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006883 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006884
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006885 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006886 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006887 goto err_ns;
6888 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006889 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006890 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006891 }
6892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006893 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006894 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6895 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006896 if (err)
6897 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01006898 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899 }
6900
6901 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006902
6903err_pmu:
6904 if (event->destroy)
6905 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08006906 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006907err_ns:
6908 if (event->ns)
6909 put_pid_ns(event->ns);
6910 kfree(event);
6911
6912 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006913}
6914
6915static int perf_copy_attr(struct perf_event_attr __user *uattr,
6916 struct perf_event_attr *attr)
6917{
6918 u32 size;
6919 int ret;
6920
6921 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6922 return -EFAULT;
6923
6924 /*
6925 * zero the full structure, so that a short copy will be nice.
6926 */
6927 memset(attr, 0, sizeof(*attr));
6928
6929 ret = get_user(size, &uattr->size);
6930 if (ret)
6931 return ret;
6932
6933 if (size > PAGE_SIZE) /* silly large */
6934 goto err_size;
6935
6936 if (!size) /* abi compat */
6937 size = PERF_ATTR_SIZE_VER0;
6938
6939 if (size < PERF_ATTR_SIZE_VER0)
6940 goto err_size;
6941
6942 /*
6943 * If we're handed a bigger struct than we know of,
6944 * ensure all the unknown bits are 0 - i.e. new
6945 * user-space does not rely on any kernel feature
6946 * extensions we dont know about yet.
6947 */
6948 if (size > sizeof(*attr)) {
6949 unsigned char __user *addr;
6950 unsigned char __user *end;
6951 unsigned char val;
6952
6953 addr = (void __user *)uattr + sizeof(*attr);
6954 end = (void __user *)uattr + size;
6955
6956 for (; addr < end; addr++) {
6957 ret = get_user(val, addr);
6958 if (ret)
6959 return ret;
6960 if (val)
6961 goto err_size;
6962 }
6963 size = sizeof(*attr);
6964 }
6965
6966 ret = copy_from_user(attr, uattr, size);
6967 if (ret)
6968 return -EFAULT;
6969
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306970 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006971 return -EINVAL;
6972
6973 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6974 return -EINVAL;
6975
6976 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6977 return -EINVAL;
6978
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006979 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6980 u64 mask = attr->branch_sample_type;
6981
6982 /* only using defined bits */
6983 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6984 return -EINVAL;
6985
6986 /* at least one branch bit must be set */
6987 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6988 return -EINVAL;
6989
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006990 /* propagate priv level, when not set for branch */
6991 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6992
6993 /* exclude_kernel checked on syscall entry */
6994 if (!attr->exclude_kernel)
6995 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6996
6997 if (!attr->exclude_user)
6998 mask |= PERF_SAMPLE_BRANCH_USER;
6999
7000 if (!attr->exclude_hv)
7001 mask |= PERF_SAMPLE_BRANCH_HV;
7002 /*
7003 * adjust user setting (for HW filter setup)
7004 */
7005 attr->branch_sample_type = mask;
7006 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007007 /* privileged levels capture (kernel, hv): check permissions */
7008 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007009 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7010 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007011 }
Jiri Olsa40189942012-08-07 15:20:37 +02007012
Jiri Olsac5ebced2012-08-07 15:20:40 +02007013 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007014 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007015 if (ret)
7016 return ret;
7017 }
7018
7019 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7020 if (!arch_perf_have_user_stack_dump())
7021 return -ENOSYS;
7022
7023 /*
7024 * We have __u32 type for the size, but so far
7025 * we can only use __u16 as maximum due to the
7026 * __u16 sample size limit.
7027 */
7028 if (attr->sample_stack_user >= USHRT_MAX)
7029 ret = -EINVAL;
7030 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7031 ret = -EINVAL;
7032 }
Jiri Olsa40189942012-08-07 15:20:37 +02007033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007034out:
7035 return ret;
7036
7037err_size:
7038 put_user(sizeof(*attr), &uattr->size);
7039 ret = -E2BIG;
7040 goto out;
7041}
7042
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007043static int
7044perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007046 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007047 int ret = -EINVAL;
7048
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007049 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007050 goto set;
7051
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007052 /* don't allow circular references */
7053 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007054 goto out;
7055
Peter Zijlstra0f139302010-05-20 14:35:15 +02007056 /*
7057 * Don't allow cross-cpu buffers
7058 */
7059 if (output_event->cpu != event->cpu)
7060 goto out;
7061
7062 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007063 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007064 */
7065 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7066 goto out;
7067
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007068set:
7069 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007070 /* Can't redirect output if we've got an active mmap() */
7071 if (atomic_read(&event->mmap_count))
7072 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007073
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007074 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007075 /* get the rb we want to redirect to */
7076 rb = ring_buffer_get(output_event);
7077 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007078 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007079 }
7080
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007081 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007083 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007084unlock:
7085 mutex_unlock(&event->mmap_mutex);
7086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007087out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007088 return ret;
7089}
7090
7091/**
7092 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7093 *
7094 * @attr_uptr: event_id type attributes for monitoring/sampling
7095 * @pid: target pid
7096 * @cpu: target cpu
7097 * @group_fd: group leader event fd
7098 */
7099SYSCALL_DEFINE5(perf_event_open,
7100 struct perf_event_attr __user *, attr_uptr,
7101 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7102{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007103 struct perf_event *group_leader = NULL, *output_event = NULL;
7104 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007105 struct perf_event_attr attr;
7106 struct perf_event_context *ctx;
7107 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007108 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007109 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007110 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007111 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007112 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007113 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007114 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007115
7116 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007117 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007118 return -EINVAL;
7119
7120 err = perf_copy_attr(attr_uptr, &attr);
7121 if (err)
7122 return err;
7123
7124 if (!attr.exclude_kernel) {
7125 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7126 return -EACCES;
7127 }
7128
7129 if (attr.freq) {
7130 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7131 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007132 } else {
7133 if (attr.sample_period & (1ULL << 63))
7134 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007135 }
7136
Stephane Eraniane5d13672011-02-14 11:20:01 +02007137 /*
7138 * In cgroup mode, the pid argument is used to pass the fd
7139 * opened to the cgroup directory in cgroupfs. The cpu argument
7140 * designates the cpu on which to monitor threads from that
7141 * cgroup.
7142 */
7143 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7144 return -EINVAL;
7145
Yann Droneauda21b0b32014-01-05 21:36:33 +01007146 if (flags & PERF_FLAG_FD_CLOEXEC)
7147 f_flags |= O_CLOEXEC;
7148
7149 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007150 if (event_fd < 0)
7151 return event_fd;
7152
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007153 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007154 err = perf_fget_light(group_fd, &group);
7155 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007156 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007157 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007158 if (flags & PERF_FLAG_FD_OUTPUT)
7159 output_event = group_leader;
7160 if (flags & PERF_FLAG_FD_NO_GROUP)
7161 group_leader = NULL;
7162 }
7163
Stephane Eraniane5d13672011-02-14 11:20:01 +02007164 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007165 task = find_lively_task_by_vpid(pid);
7166 if (IS_ERR(task)) {
7167 err = PTR_ERR(task);
7168 goto err_group_fd;
7169 }
7170 }
7171
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007172 if (task && group_leader &&
7173 group_leader->attr.inherit != attr.inherit) {
7174 err = -EINVAL;
7175 goto err_task;
7176 }
7177
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007178 get_online_cpus();
7179
Avi Kivity4dc0da82011-06-29 18:42:35 +03007180 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7181 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007182 if (IS_ERR(event)) {
7183 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007184 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007185 }
7186
Stephane Eraniane5d13672011-02-14 11:20:01 +02007187 if (flags & PERF_FLAG_PID_CGROUP) {
7188 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007189 if (err) {
7190 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007191 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007192 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007193 }
7194
Vince Weaver53b25332014-05-16 17:12:12 -04007195 if (is_sampling_event(event)) {
7196 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7197 err = -ENOTSUPP;
7198 goto err_alloc;
7199 }
7200 }
7201
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007202 account_event(event);
7203
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007204 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007205 * Special case software events and allow them to be part of
7206 * any hardware group.
7207 */
7208 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007209
7210 if (group_leader &&
7211 (is_software_event(event) != is_software_event(group_leader))) {
7212 if (is_software_event(event)) {
7213 /*
7214 * If event and group_leader are not both a software
7215 * event, and event is, then group leader is not.
7216 *
7217 * Allow the addition of software events to !software
7218 * groups, this is safe because software events never
7219 * fail to schedule.
7220 */
7221 pmu = group_leader->pmu;
7222 } else if (is_software_event(group_leader) &&
7223 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7224 /*
7225 * In case the group is a pure software group, and we
7226 * try to add a hardware event, move the whole group to
7227 * the hardware context.
7228 */
7229 move_group = 1;
7230 }
7231 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007232
7233 /*
7234 * Get the target context (task or percpu):
7235 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007236 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007237 if (IS_ERR(ctx)) {
7238 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007239 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007240 }
7241
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007242 if (task) {
7243 put_task_struct(task);
7244 task = NULL;
7245 }
7246
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007247 /*
7248 * Look up the group leader (we will attach this event to it):
7249 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007250 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007251 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007252
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007253 /*
7254 * Do not allow a recursive hierarchy (this new sibling
7255 * becoming part of another group-sibling):
7256 */
7257 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007258 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007259 /*
7260 * Do not allow to attach to a group in a different
7261 * task or CPU context:
7262 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007263 if (move_group) {
7264 if (group_leader->ctx->type != ctx->type)
7265 goto err_context;
7266 } else {
7267 if (group_leader->ctx != ctx)
7268 goto err_context;
7269 }
7270
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007271 /*
7272 * Only a group leader can be exclusive or pinned
7273 */
7274 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007275 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007276 }
7277
7278 if (output_event) {
7279 err = perf_event_set_output(event, output_event);
7280 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007281 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007282 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007283
Yann Droneauda21b0b32014-01-05 21:36:33 +01007284 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7285 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007286 if (IS_ERR(event_file)) {
7287 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007288 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007289 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007290
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007291 if (move_group) {
7292 struct perf_event_context *gctx = group_leader->ctx;
7293
7294 mutex_lock(&gctx->mutex);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007295 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007296
7297 /*
7298 * Removing from the context ends up with disabled
7299 * event. What we want here is event in the initial
7300 * startup state, ready to be add into new context.
7301 */
7302 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007303 list_for_each_entry(sibling, &group_leader->sibling_list,
7304 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007305 perf_remove_from_context(sibling, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007306 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007307 put_ctx(gctx);
7308 }
7309 mutex_unlock(&gctx->mutex);
7310 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007311 }
7312
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007313 WARN_ON_ONCE(ctx->parent_ctx);
7314 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007315
7316 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007317 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007318 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007319 get_ctx(ctx);
7320 list_for_each_entry(sibling, &group_leader->sibling_list,
7321 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007322 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007323 get_ctx(ctx);
7324 }
7325 }
7326
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007327 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007328 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007329 mutex_unlock(&ctx->mutex);
7330
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007331 put_online_cpus();
7332
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007333 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007335 mutex_lock(&current->perf_event_mutex);
7336 list_add_tail(&event->owner_entry, &current->perf_event_list);
7337 mutex_unlock(&current->perf_event_mutex);
7338
Peter Zijlstra8a495422010-05-27 15:47:49 +02007339 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007340 * Precalculate sample_data sizes
7341 */
7342 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007343 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007344
7345 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007346 * Drop the reference on the group_event after placing the
7347 * new event on the sibling_list. This ensures destruction
7348 * of the group leader will find the pointer to itself in
7349 * perf_group_detach().
7350 */
Al Viro2903ff02012-08-28 12:52:22 -04007351 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007352 fd_install(event_fd, event_file);
7353 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007354
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007355err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007356 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007357 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007358err_alloc:
7359 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007360err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007361 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007362err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007363 if (task)
7364 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007365err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007366 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007367err_fd:
7368 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007369 return err;
7370}
7371
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007372/**
7373 * perf_event_create_kernel_counter
7374 *
7375 * @attr: attributes of the counter to create
7376 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007377 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007378 */
7379struct perf_event *
7380perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007381 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007382 perf_overflow_handler_t overflow_handler,
7383 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007384{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007385 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007386 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007387 int err;
7388
7389 /*
7390 * Get the target context (task or percpu):
7391 */
7392
Avi Kivity4dc0da82011-06-29 18:42:35 +03007393 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7394 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007395 if (IS_ERR(event)) {
7396 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007397 goto err;
7398 }
7399
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007400 account_event(event);
7401
Matt Helsley38a81da2010-09-13 13:01:20 -07007402 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007403 if (IS_ERR(ctx)) {
7404 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007405 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007406 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007407
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007408 WARN_ON_ONCE(ctx->parent_ctx);
7409 mutex_lock(&ctx->mutex);
7410 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007411 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007412 mutex_unlock(&ctx->mutex);
7413
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007414 return event;
7415
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007416err_free:
7417 free_event(event);
7418err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007419 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007420}
7421EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7422
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007423void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7424{
7425 struct perf_event_context *src_ctx;
7426 struct perf_event_context *dst_ctx;
7427 struct perf_event *event, *tmp;
7428 LIST_HEAD(events);
7429
7430 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7431 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7432
7433 mutex_lock(&src_ctx->mutex);
7434 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7435 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007436 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007437 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007438 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007439 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007440 }
7441 mutex_unlock(&src_ctx->mutex);
7442
7443 synchronize_rcu();
7444
7445 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007446 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7447 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007448 if (event->state >= PERF_EVENT_STATE_OFF)
7449 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007450 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007451 perf_install_in_context(dst_ctx, event, dst_cpu);
7452 get_ctx(dst_ctx);
7453 }
7454 mutex_unlock(&dst_ctx->mutex);
7455}
7456EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007458static void sync_child_event(struct perf_event *child_event,
7459 struct task_struct *child)
7460{
7461 struct perf_event *parent_event = child_event->parent;
7462 u64 child_val;
7463
7464 if (child_event->attr.inherit_stat)
7465 perf_event_read_event(child_event, child);
7466
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007467 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007468
7469 /*
7470 * Add back the child's count to the parent's count:
7471 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007472 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007473 atomic64_add(child_event->total_time_enabled,
7474 &parent_event->child_total_time_enabled);
7475 atomic64_add(child_event->total_time_running,
7476 &parent_event->child_total_time_running);
7477
7478 /*
7479 * Remove this event from the parent's list
7480 */
7481 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7482 mutex_lock(&parent_event->child_mutex);
7483 list_del_init(&child_event->child_list);
7484 mutex_unlock(&parent_event->child_mutex);
7485
7486 /*
7487 * Release the parent event, if this was the last
7488 * reference to it.
7489 */
Al Viroa6fa9412012-08-20 14:59:25 +01007490 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007491}
7492
7493static void
7494__perf_event_exit_task(struct perf_event *child_event,
7495 struct perf_event_context *child_ctx,
7496 struct task_struct *child)
7497{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007498 /*
7499 * Do not destroy the 'original' grouping; because of the context
7500 * switch optimization the original events could've ended up in a
7501 * random child task.
7502 *
7503 * If we were to destroy the original group, all group related
7504 * operations would cease to function properly after this random
7505 * child dies.
7506 *
7507 * Do destroy all inherited groups, we don't care about those
7508 * and being thorough is better.
7509 */
7510 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007512 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007513 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007514 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007515 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007516 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007517 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007518 sync_child_event(child_event, child);
7519 free_event(child_event);
7520 }
7521}
7522
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007523static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007524{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007525 struct perf_event *child_event, *next;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007526 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007527 unsigned long flags;
7528
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007529 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007530 perf_event_task(child, NULL, 0);
7531 return;
7532 }
7533
7534 local_irq_save(flags);
7535 /*
7536 * We can't reschedule here because interrupts are disabled,
7537 * and either child is current or it is a task that can't be
7538 * scheduled, so we are now safe from rescheduling changing
7539 * our context.
7540 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007541 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007542
7543 /*
7544 * Take the context lock here so that if find_get_context is
7545 * reading child->perf_event_ctxp, we wait until it has
7546 * incremented the context's refcount before we do put_ctx below.
7547 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007548 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007549 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007550 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007551
7552 /*
7553 * In order to avoid freeing: child_ctx->parent_ctx->task
7554 * under perf_event_context::lock, grab another reference.
7555 */
7556 parent_ctx = child_ctx->parent_ctx;
7557 if (parent_ctx)
7558 get_ctx(parent_ctx);
7559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007560 /*
7561 * If this context is a clone; unclone it so it can't get
7562 * swapped to another process while we're removing all
7563 * the events from it.
7564 */
7565 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007566 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007567 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007568
7569 /*
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007570 * Now that we no longer hold perf_event_context::lock, drop
7571 * our extra child_ctx->parent_ctx reference.
7572 */
7573 if (parent_ctx)
7574 put_ctx(parent_ctx);
7575
7576 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007577 * Report the task dead after unscheduling the events so that we
7578 * won't get any samples after PERF_RECORD_EXIT. We can however still
7579 * get a few PERF_RECORD_READ events.
7580 */
7581 perf_event_task(child, child_ctx, 0);
7582
7583 /*
7584 * We can recurse on the same lock type through:
7585 *
7586 * __perf_event_exit_task()
7587 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007588 * put_event()
7589 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007590 *
7591 * But since its the parent context it won't be the same instance.
7592 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007593 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007594
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007595 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007596 __perf_event_exit_task(child_event, child_ctx, child);
7597
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007598 mutex_unlock(&child_ctx->mutex);
7599
7600 put_ctx(child_ctx);
7601}
7602
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007603/*
7604 * When a child task exits, feed back event values to parent events.
7605 */
7606void perf_event_exit_task(struct task_struct *child)
7607{
Peter Zijlstra88821352010-11-09 19:01:43 +01007608 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007609 int ctxn;
7610
Peter Zijlstra88821352010-11-09 19:01:43 +01007611 mutex_lock(&child->perf_event_mutex);
7612 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7613 owner_entry) {
7614 list_del_init(&event->owner_entry);
7615
7616 /*
7617 * Ensure the list deletion is visible before we clear
7618 * the owner, closes a race against perf_release() where
7619 * we need to serialize on the owner->perf_event_mutex.
7620 */
7621 smp_wmb();
7622 event->owner = NULL;
7623 }
7624 mutex_unlock(&child->perf_event_mutex);
7625
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007626 for_each_task_context_nr(ctxn)
7627 perf_event_exit_task_context(child, ctxn);
7628}
7629
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007630static void perf_free_event(struct perf_event *event,
7631 struct perf_event_context *ctx)
7632{
7633 struct perf_event *parent = event->parent;
7634
7635 if (WARN_ON_ONCE(!parent))
7636 return;
7637
7638 mutex_lock(&parent->child_mutex);
7639 list_del_init(&event->child_list);
7640 mutex_unlock(&parent->child_mutex);
7641
Al Viroa6fa9412012-08-20 14:59:25 +01007642 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007643
Peter Zijlstra8a495422010-05-27 15:47:49 +02007644 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007645 list_del_event(event, ctx);
7646 free_event(event);
7647}
7648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007649/*
7650 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007651 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007652 */
7653void perf_event_free_task(struct task_struct *task)
7654{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007655 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007656 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007657 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007658
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007659 for_each_task_context_nr(ctxn) {
7660 ctx = task->perf_event_ctxp[ctxn];
7661 if (!ctx)
7662 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007663
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007664 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007665again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007666 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7667 group_entry)
7668 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007669
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007670 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7671 group_entry)
7672 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007673
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007674 if (!list_empty(&ctx->pinned_groups) ||
7675 !list_empty(&ctx->flexible_groups))
7676 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007677
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007678 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007679
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007680 put_ctx(ctx);
7681 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007682}
7683
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007684void perf_event_delayed_put(struct task_struct *task)
7685{
7686 int ctxn;
7687
7688 for_each_task_context_nr(ctxn)
7689 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7690}
7691
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007692/*
7693 * inherit a event from parent task to child task:
7694 */
7695static struct perf_event *
7696inherit_event(struct perf_event *parent_event,
7697 struct task_struct *parent,
7698 struct perf_event_context *parent_ctx,
7699 struct task_struct *child,
7700 struct perf_event *group_leader,
7701 struct perf_event_context *child_ctx)
7702{
7703 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007704 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007705
7706 /*
7707 * Instead of creating recursive hierarchies of events,
7708 * we link inherited events back to the original parent,
7709 * which has a filp for sure, which we use as the reference
7710 * count:
7711 */
7712 if (parent_event->parent)
7713 parent_event = parent_event->parent;
7714
7715 child_event = perf_event_alloc(&parent_event->attr,
7716 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007717 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007718 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007719 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007720 if (IS_ERR(child_event))
7721 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007722
7723 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7724 free_event(child_event);
7725 return NULL;
7726 }
7727
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007728 get_ctx(child_ctx);
7729
7730 /*
7731 * Make the child state follow the state of the parent event,
7732 * not its attr.disabled bit. We hold the parent's mutex,
7733 * so we won't race with perf_event_{en, dis}able_family.
7734 */
7735 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7736 child_event->state = PERF_EVENT_STATE_INACTIVE;
7737 else
7738 child_event->state = PERF_EVENT_STATE_OFF;
7739
7740 if (parent_event->attr.freq) {
7741 u64 sample_period = parent_event->hw.sample_period;
7742 struct hw_perf_event *hwc = &child_event->hw;
7743
7744 hwc->sample_period = sample_period;
7745 hwc->last_period = sample_period;
7746
7747 local64_set(&hwc->period_left, sample_period);
7748 }
7749
7750 child_event->ctx = child_ctx;
7751 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007752 child_event->overflow_handler_context
7753 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007754
7755 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007756 * Precalculate sample_data sizes
7757 */
7758 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007759 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007760
7761 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007762 * Link it up in the child's context:
7763 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007764 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007765 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007766 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007767
7768 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007769 * Link this into the parent event's child list
7770 */
7771 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7772 mutex_lock(&parent_event->child_mutex);
7773 list_add_tail(&child_event->child_list, &parent_event->child_list);
7774 mutex_unlock(&parent_event->child_mutex);
7775
7776 return child_event;
7777}
7778
7779static int inherit_group(struct perf_event *parent_event,
7780 struct task_struct *parent,
7781 struct perf_event_context *parent_ctx,
7782 struct task_struct *child,
7783 struct perf_event_context *child_ctx)
7784{
7785 struct perf_event *leader;
7786 struct perf_event *sub;
7787 struct perf_event *child_ctr;
7788
7789 leader = inherit_event(parent_event, parent, parent_ctx,
7790 child, NULL, child_ctx);
7791 if (IS_ERR(leader))
7792 return PTR_ERR(leader);
7793 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7794 child_ctr = inherit_event(sub, parent, parent_ctx,
7795 child, leader, child_ctx);
7796 if (IS_ERR(child_ctr))
7797 return PTR_ERR(child_ctr);
7798 }
7799 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007800}
7801
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007802static int
7803inherit_task_group(struct perf_event *event, struct task_struct *parent,
7804 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007805 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007806 int *inherited_all)
7807{
7808 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007809 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007810
7811 if (!event->attr.inherit) {
7812 *inherited_all = 0;
7813 return 0;
7814 }
7815
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007816 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007817 if (!child_ctx) {
7818 /*
7819 * This is executed from the parent task context, so
7820 * inherit events that have been marked for cloning.
7821 * First allocate and initialize a context for the
7822 * child.
7823 */
7824
Jiri Olsa734df5a2013-07-09 17:44:10 +02007825 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007826 if (!child_ctx)
7827 return -ENOMEM;
7828
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007829 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007830 }
7831
7832 ret = inherit_group(event, parent, parent_ctx,
7833 child, child_ctx);
7834
7835 if (ret)
7836 *inherited_all = 0;
7837
7838 return ret;
7839}
7840
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007841/*
7842 * Initialize the perf_event context in task_struct
7843 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02007844static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007845{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007846 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007847 struct perf_event_context *cloned_ctx;
7848 struct perf_event *event;
7849 struct task_struct *parent = current;
7850 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007851 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007852 int ret = 0;
7853
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007854 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007855 return 0;
7856
7857 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007858 * If the parent's context is a clone, pin it so it won't get
7859 * swapped under us.
7860 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007861 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02007862 if (!parent_ctx)
7863 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007864
7865 /*
7866 * No need to check if parent_ctx != NULL here; since we saw
7867 * it non-NULL earlier, the only reason for it to become NULL
7868 * is if we exit, and since we're currently in the middle of
7869 * a fork we can't be exiting at the same time.
7870 */
7871
7872 /*
7873 * Lock the parent list. No need to lock the child - not PID
7874 * hashed yet and not running, so nobody can access it.
7875 */
7876 mutex_lock(&parent_ctx->mutex);
7877
7878 /*
7879 * We dont have to disable NMIs - we are only looking at
7880 * the list, not manipulating it:
7881 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007882 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007883 ret = inherit_task_group(event, parent, parent_ctx,
7884 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007885 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007886 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007887 }
7888
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007889 /*
7890 * We can't hold ctx->lock when iterating the ->flexible_group list due
7891 * to allocations, but we need to prevent rotation because
7892 * rotate_ctx() will change the list from interrupt context.
7893 */
7894 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7895 parent_ctx->rotate_disable = 1;
7896 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7897
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007898 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007899 ret = inherit_task_group(event, parent, parent_ctx,
7900 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007901 if (ret)
7902 break;
7903 }
7904
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007905 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7906 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007907
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007908 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007909
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007910 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007911 /*
7912 * Mark the child context as a clone of the parent
7913 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007914 *
7915 * Note that if the parent is a clone, the holding of
7916 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007917 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007918 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007919 if (cloned_ctx) {
7920 child_ctx->parent_ctx = cloned_ctx;
7921 child_ctx->parent_gen = parent_ctx->parent_gen;
7922 } else {
7923 child_ctx->parent_ctx = parent_ctx;
7924 child_ctx->parent_gen = parent_ctx->generation;
7925 }
7926 get_ctx(child_ctx->parent_ctx);
7927 }
7928
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007929 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007930 mutex_unlock(&parent_ctx->mutex);
7931
7932 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007933 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007934
7935 return ret;
7936}
7937
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007938/*
7939 * Initialize the perf_event context in task_struct
7940 */
7941int perf_event_init_task(struct task_struct *child)
7942{
7943 int ctxn, ret;
7944
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007945 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7946 mutex_init(&child->perf_event_mutex);
7947 INIT_LIST_HEAD(&child->perf_event_list);
7948
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007949 for_each_task_context_nr(ctxn) {
7950 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07007951 if (ret) {
7952 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007953 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07007954 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007955 }
7956
7957 return 0;
7958}
7959
Paul Mackerras220b1402010-03-10 20:45:52 +11007960static void __init perf_event_init_all_cpus(void)
7961{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007962 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007963 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007964
7965 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007966 swhash = &per_cpu(swevent_htable, cpu);
7967 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007968 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007969 }
7970}
7971
Paul Gortmaker0db06282013-06-19 14:53:51 -04007972static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007973{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007974 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007975
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007976 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02007977 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007978 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007979 struct swevent_hlist *hlist;
7980
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007981 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7982 WARN_ON(!hlist);
7983 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007984 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007985 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007986}
7987
Peter Zijlstrac2774432010-12-08 15:29:02 +01007988#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007989static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007990{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007991 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7992
7993 WARN_ON(!irqs_disabled());
7994
7995 list_del_init(&cpuctx->rotation_list);
7996}
7997
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007998static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007999{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008000 struct remove_event re = { .detach_group = false };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008001 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008002
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008003 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02008004
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008005 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008006 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8007 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008008 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008009}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008010
8011static void perf_event_exit_cpu_context(int cpu)
8012{
8013 struct perf_event_context *ctx;
8014 struct pmu *pmu;
8015 int idx;
8016
8017 idx = srcu_read_lock(&pmus_srcu);
8018 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008019 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008020
8021 mutex_lock(&ctx->mutex);
8022 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8023 mutex_unlock(&ctx->mutex);
8024 }
8025 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008026}
8027
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008028static void perf_event_exit_cpu(int cpu)
8029{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008030 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008031
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008032 perf_event_exit_cpu_context(cpu);
8033
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008034 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008035 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008036 swevent_hlist_release(swhash);
8037 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008038}
8039#else
8040static inline void perf_event_exit_cpu(int cpu) { }
8041#endif
8042
Peter Zijlstrac2774432010-12-08 15:29:02 +01008043static int
8044perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8045{
8046 int cpu;
8047
8048 for_each_online_cpu(cpu)
8049 perf_event_exit_cpu(cpu);
8050
8051 return NOTIFY_OK;
8052}
8053
8054/*
8055 * Run the perf reboot notifier at the very last possible moment so that
8056 * the generic watchdog code runs as long as possible.
8057 */
8058static struct notifier_block perf_reboot_notifier = {
8059 .notifier_call = perf_reboot,
8060 .priority = INT_MIN,
8061};
8062
Paul Gortmaker0db06282013-06-19 14:53:51 -04008063static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008064perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8065{
8066 unsigned int cpu = (long)hcpu;
8067
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008068 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008069
8070 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008071 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008072 perf_event_init_cpu(cpu);
8073 break;
8074
Peter Zijlstra5e116372010-06-11 13:35:08 +02008075 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008076 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008077 perf_event_exit_cpu(cpu);
8078 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008079 default:
8080 break;
8081 }
8082
8083 return NOTIFY_OK;
8084}
8085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008086void __init perf_event_init(void)
8087{
Jason Wessel3c502e72010-11-04 17:33:01 -05008088 int ret;
8089
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008090 idr_init(&pmu_idr);
8091
Paul Mackerras220b1402010-03-10 20:45:52 +11008092 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008093 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008094 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8095 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8096 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008097 perf_tp_register();
8098 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008099 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008100
8101 ret = init_hw_breakpoint();
8102 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008103
8104 /* do not patch jump label more than once per second */
8105 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008106
8107 /*
8108 * Build time assertion that we keep the data_head at the intended
8109 * location. IOW, validation we got the __reserved[] size right.
8110 */
8111 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8112 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008113}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008114
8115static int __init perf_event_sysfs_init(void)
8116{
8117 struct pmu *pmu;
8118 int ret;
8119
8120 mutex_lock(&pmus_lock);
8121
8122 ret = bus_register(&pmu_bus);
8123 if (ret)
8124 goto unlock;
8125
8126 list_for_each_entry(pmu, &pmus, entry) {
8127 if (!pmu->name || pmu->type < 0)
8128 continue;
8129
8130 ret = pmu_dev_alloc(pmu);
8131 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8132 }
8133 pmu_bus_running = 1;
8134 ret = 0;
8135
8136unlock:
8137 mutex_unlock(&pmus_lock);
8138
8139 return ret;
8140}
8141device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008142
8143#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008144static struct cgroup_subsys_state *
8145perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008146{
8147 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008148
Li Zefan1b15d052011-03-03 14:26:06 +08008149 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008150 if (!jc)
8151 return ERR_PTR(-ENOMEM);
8152
Stephane Eraniane5d13672011-02-14 11:20:01 +02008153 jc->info = alloc_percpu(struct perf_cgroup_info);
8154 if (!jc->info) {
8155 kfree(jc);
8156 return ERR_PTR(-ENOMEM);
8157 }
8158
Stephane Eraniane5d13672011-02-14 11:20:01 +02008159 return &jc->css;
8160}
8161
Tejun Heoeb954192013-08-08 20:11:23 -04008162static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008163{
Tejun Heoeb954192013-08-08 20:11:23 -04008164 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8165
Stephane Eraniane5d13672011-02-14 11:20:01 +02008166 free_percpu(jc->info);
8167 kfree(jc);
8168}
8169
8170static int __perf_cgroup_move(void *info)
8171{
8172 struct task_struct *task = info;
8173 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8174 return 0;
8175}
8176
Tejun Heoeb954192013-08-08 20:11:23 -04008177static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8178 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008179{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008180 struct task_struct *task;
8181
Tejun Heo924f0d92014-02-13 06:58:41 -05008182 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008183 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008184}
8185
Tejun Heoeb954192013-08-08 20:11:23 -04008186static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8187 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008188 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008189{
8190 /*
8191 * cgroup_exit() is called in the copy_process() failure path.
8192 * Ignore this case since the task hasn't ran yet, this avoids
8193 * trying to poke a half freed task state from generic code.
8194 */
8195 if (!(task->flags & PF_EXITING))
8196 return;
8197
Tejun Heobb9d97b2011-12-12 18:12:21 -08008198 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008199}
8200
Tejun Heo073219e2014-02-08 10:36:58 -05008201struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008202 .css_alloc = perf_cgroup_css_alloc,
8203 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008204 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008205 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008206};
8207#endif /* CONFIG_CGROUP_PERF */