blob: a0780b3a3d50560812aeb48deb23a1e0697197dd [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042
Frederic Weisbecker76369132011-05-19 19:55:04 +020043#include "internal.h"
44
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045#include <asm/irq_regs.h>
46
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010047struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020048 struct task_struct *p;
49 int (*func)(void *info);
50 void *info;
51 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052};
53
54static void remote_function(void *data)
55{
56 struct remote_function_call *tfc = data;
57 struct task_struct *p = tfc->p;
58
59 if (p) {
60 tfc->ret = -EAGAIN;
61 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 return;
63 }
64
65 tfc->ret = tfc->func(tfc->info);
66}
67
68/**
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
73 *
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
76 *
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
80 */
81static int
82task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
83{
84 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020085 .p = p,
86 .func = func,
87 .info = info,
88 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010089 };
90
91 if (task_curr(p))
92 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
93
94 return data.ret;
95}
96
97/**
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
101 *
102 * Calls the function @func on the remote cpu.
103 *
104 * returns: @func return value or -ENXIO when the cpu is offline
105 */
106static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
107{
108 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200109 .p = NULL,
110 .func = func,
111 .info = info,
112 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100113 };
114
115 smp_call_function_single(cpu, remote_function, &data, 1);
116
117 return data.ret;
118}
119
Stephane Eraniane5d13672011-02-14 11:20:01 +0200120#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP)
123
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100124/*
125 * branch priv levels that need permission checks
126 */
127#define PERF_SAMPLE_BRANCH_PERM_PLM \
128 (PERF_SAMPLE_BRANCH_KERNEL |\
129 PERF_SAMPLE_BRANCH_HV)
130
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200131enum event_type_t {
132 EVENT_FLEXIBLE = 0x1,
133 EVENT_PINNED = 0x2,
134 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
135};
136
Stephane Eraniane5d13672011-02-14 11:20:01 +0200137/*
138 * perf_sched_events : >0 events exist
139 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
140 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100141struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200142static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100143static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200144
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145static atomic_t nr_mmap_events __read_mostly;
146static atomic_t nr_comm_events __read_mostly;
147static atomic_t nr_task_events __read_mostly;
148
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200149static LIST_HEAD(pmus);
150static DEFINE_MUTEX(pmus_lock);
151static struct srcu_struct pmus_srcu;
152
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200153/*
154 * perf event paranoia level:
155 * -1 - not paranoid at all
156 * 0 - disallow raw tracepoint access for unpriv
157 * 1 - disallow cpu events for unpriv
158 * 2 - disallow kernel profiling for unpriv
159 */
160int sysctl_perf_event_paranoid __read_mostly = 1;
161
Frederic Weisbecker20443382011-03-31 03:33:29 +0200162/* Minimum for 512 kiB + 1 user control page */
163int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164
165/*
166 * max perf event sample rate
167 */
Peter Zijlstra163ec432011-02-16 11:22:34 +0100168#define DEFAULT_MAX_SAMPLE_RATE 100000
169int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
170static int max_samples_per_tick __read_mostly =
171 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
172
Stephane Eranian9e630202013-04-03 14:21:33 +0200173static int perf_rotate_context(struct perf_cpu_context *cpuctx);
174
Peter Zijlstra163ec432011-02-16 11:22:34 +0100175int perf_proc_update_handler(struct ctl_table *table, int write,
176 void __user *buffer, size_t *lenp,
177 loff_t *ppos)
178{
179 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
180
181 if (ret || !write)
182 return ret;
183
184 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
185
186 return 0;
187}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200188
189static atomic64_t perf_event_id;
190
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200191static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
192 enum event_type_t event_type);
193
194static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200195 enum event_type_t event_type,
196 struct task_struct *task);
197
198static void update_context_time(struct perf_event_context *ctx);
199static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200200
Peter Zijlstra10c6db12011-11-26 02:47:31 +0100201static void ring_buffer_attach(struct perf_event *event,
202 struct ring_buffer *rb);
203
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200204void __weak perf_event_print_debug(void) { }
205
Matt Fleming84c79912010-10-03 21:41:13 +0100206extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200207{
Matt Fleming84c79912010-10-03 21:41:13 +0100208 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209}
210
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200211static inline u64 perf_clock(void)
212{
213 return local_clock();
214}
215
Stephane Eraniane5d13672011-02-14 11:20:01 +0200216static inline struct perf_cpu_context *
217__get_cpu_context(struct perf_event_context *ctx)
218{
219 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
220}
221
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200222static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
223 struct perf_event_context *ctx)
224{
225 raw_spin_lock(&cpuctx->ctx.lock);
226 if (ctx)
227 raw_spin_lock(&ctx->lock);
228}
229
230static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
231 struct perf_event_context *ctx)
232{
233 if (ctx)
234 raw_spin_unlock(&ctx->lock);
235 raw_spin_unlock(&cpuctx->ctx.lock);
236}
237
Stephane Eraniane5d13672011-02-14 11:20:01 +0200238#ifdef CONFIG_CGROUP_PERF
239
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200240/*
Li Zefan877c6852013-03-05 11:38:08 +0800241 * perf_cgroup_info keeps track of time_enabled for a cgroup.
242 * This is a per-cpu dynamically allocated data structure.
243 */
244struct perf_cgroup_info {
245 u64 time;
246 u64 timestamp;
247};
248
249struct perf_cgroup {
250 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900251 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800252};
253
254/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200255 * Must ensure cgroup is pinned (css_get) before calling
256 * this function. In other words, we cannot call this function
257 * if there is no cgroup event for the current CPU context.
258 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200259static inline struct perf_cgroup *
260perf_cgroup_from_task(struct task_struct *task)
261{
262 return container_of(task_subsys_state(task, perf_subsys_id),
263 struct perf_cgroup, css);
264}
265
266static inline bool
267perf_cgroup_match(struct perf_event *event)
268{
269 struct perf_event_context *ctx = event->ctx;
270 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
271
Tejun Heoef824fa2013-04-08 19:00:38 -0700272 /* @event doesn't care about cgroup */
273 if (!event->cgrp)
274 return true;
275
276 /* wants specific cgroup scope but @cpuctx isn't associated with any */
277 if (!cpuctx->cgrp)
278 return false;
279
280 /*
281 * Cgroup scoping is recursive. An event enabled for a cgroup is
282 * also enabled for all its descendant cgroups. If @cpuctx's
283 * cgroup is a descendant of @event's (the test covers identity
284 * case), it's a match.
285 */
286 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
287 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200288}
289
Salman Qazi9c5da092012-06-14 15:31:09 -0700290static inline bool perf_tryget_cgroup(struct perf_event *event)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200291{
Salman Qazi9c5da092012-06-14 15:31:09 -0700292 return css_tryget(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200293}
294
295static inline void perf_put_cgroup(struct perf_event *event)
296{
297 css_put(&event->cgrp->css);
298}
299
300static inline void perf_detach_cgroup(struct perf_event *event)
301{
302 perf_put_cgroup(event);
303 event->cgrp = NULL;
304}
305
306static inline int is_cgroup_event(struct perf_event *event)
307{
308 return event->cgrp != NULL;
309}
310
311static inline u64 perf_cgroup_event_time(struct perf_event *event)
312{
313 struct perf_cgroup_info *t;
314
315 t = per_cpu_ptr(event->cgrp->info, event->cpu);
316 return t->time;
317}
318
319static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
320{
321 struct perf_cgroup_info *info;
322 u64 now;
323
324 now = perf_clock();
325
326 info = this_cpu_ptr(cgrp->info);
327
328 info->time += now - info->timestamp;
329 info->timestamp = now;
330}
331
332static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
333{
334 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
335 if (cgrp_out)
336 __update_cgrp_time(cgrp_out);
337}
338
339static inline void update_cgrp_time_from_event(struct perf_event *event)
340{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200341 struct perf_cgroup *cgrp;
342
Stephane Eraniane5d13672011-02-14 11:20:01 +0200343 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200344 * ensure we access cgroup data only when needed and
345 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200346 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200347 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200348 return;
349
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200350 cgrp = perf_cgroup_from_task(current);
351 /*
352 * Do not update time when cgroup is not active
353 */
354 if (cgrp == event->cgrp)
355 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200356}
357
358static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200359perf_cgroup_set_timestamp(struct task_struct *task,
360 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200361{
362 struct perf_cgroup *cgrp;
363 struct perf_cgroup_info *info;
364
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200365 /*
366 * ctx->lock held by caller
367 * ensure we do not access cgroup data
368 * unless we have the cgroup pinned (css_get)
369 */
370 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200371 return;
372
373 cgrp = perf_cgroup_from_task(task);
374 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200375 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200376}
377
378#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
379#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
380
381/*
382 * reschedule events based on the cgroup constraint of task.
383 *
384 * mode SWOUT : schedule out everything
385 * mode SWIN : schedule in based on cgroup for next
386 */
387void perf_cgroup_switch(struct task_struct *task, int mode)
388{
389 struct perf_cpu_context *cpuctx;
390 struct pmu *pmu;
391 unsigned long flags;
392
393 /*
394 * disable interrupts to avoid geting nr_cgroup
395 * changes via __perf_event_disable(). Also
396 * avoids preemption.
397 */
398 local_irq_save(flags);
399
400 /*
401 * we reschedule only in the presence of cgroup
402 * constrained events.
403 */
404 rcu_read_lock();
405
406 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200407 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200408 if (cpuctx->unique_pmu != pmu)
409 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200410
Stephane Eraniane5d13672011-02-14 11:20:01 +0200411 /*
412 * perf_cgroup_events says at least one
413 * context on this CPU has cgroup events.
414 *
415 * ctx->nr_cgroups reports the number of cgroup
416 * events for a context.
417 */
418 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200419 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
420 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200421
422 if (mode & PERF_CGROUP_SWOUT) {
423 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
424 /*
425 * must not be done before ctxswout due
426 * to event_filter_match() in event_sched_out()
427 */
428 cpuctx->cgrp = NULL;
429 }
430
431 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200432 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200433 /*
434 * set cgrp before ctxsw in to allow
435 * event_filter_match() to not have to pass
436 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200437 */
438 cpuctx->cgrp = perf_cgroup_from_task(task);
439 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
440 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200441 perf_pmu_enable(cpuctx->ctx.pmu);
442 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200443 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200444 }
445
446 rcu_read_unlock();
447
448 local_irq_restore(flags);
449}
450
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200451static inline void perf_cgroup_sched_out(struct task_struct *task,
452 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200453{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200454 struct perf_cgroup *cgrp1;
455 struct perf_cgroup *cgrp2 = NULL;
456
457 /*
458 * we come here when we know perf_cgroup_events > 0
459 */
460 cgrp1 = perf_cgroup_from_task(task);
461
462 /*
463 * next is NULL when called from perf_event_enable_on_exec()
464 * that will systematically cause a cgroup_switch()
465 */
466 if (next)
467 cgrp2 = perf_cgroup_from_task(next);
468
469 /*
470 * only schedule out current cgroup events if we know
471 * that we are switching to a different cgroup. Otherwise,
472 * do no touch the cgroup events.
473 */
474 if (cgrp1 != cgrp2)
475 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200476}
477
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200478static inline void perf_cgroup_sched_in(struct task_struct *prev,
479 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200480{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200481 struct perf_cgroup *cgrp1;
482 struct perf_cgroup *cgrp2 = NULL;
483
484 /*
485 * we come here when we know perf_cgroup_events > 0
486 */
487 cgrp1 = perf_cgroup_from_task(task);
488
489 /* prev can never be NULL */
490 cgrp2 = perf_cgroup_from_task(prev);
491
492 /*
493 * only need to schedule in cgroup events if we are changing
494 * cgroup during ctxsw. Cgroup events were not scheduled
495 * out of ctxsw out if that was not the case.
496 */
497 if (cgrp1 != cgrp2)
498 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200499}
500
501static inline int perf_cgroup_connect(int fd, struct perf_event *event,
502 struct perf_event_attr *attr,
503 struct perf_event *group_leader)
504{
505 struct perf_cgroup *cgrp;
506 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400507 struct fd f = fdget(fd);
508 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200509
Al Viro2903ff02012-08-28 12:52:22 -0400510 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200511 return -EBADF;
512
Al Viro2903ff02012-08-28 12:52:22 -0400513 css = cgroup_css_from_dir(f.file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800514 if (IS_ERR(css)) {
515 ret = PTR_ERR(css);
516 goto out;
517 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200518
519 cgrp = container_of(css, struct perf_cgroup, css);
520 event->cgrp = cgrp;
521
Li Zefanf75e18c2011-03-03 14:25:50 +0800522 /* must be done before we fput() the file */
Salman Qazi9c5da092012-06-14 15:31:09 -0700523 if (!perf_tryget_cgroup(event)) {
524 event->cgrp = NULL;
525 ret = -ENOENT;
526 goto out;
527 }
Li Zefanf75e18c2011-03-03 14:25:50 +0800528
Stephane Eraniane5d13672011-02-14 11:20:01 +0200529 /*
530 * all events in a group must monitor
531 * the same cgroup because a task belongs
532 * to only one perf cgroup at a time
533 */
534 if (group_leader && group_leader->cgrp != cgrp) {
535 perf_detach_cgroup(event);
536 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200537 }
Li Zefan3db272c2011-03-03 14:25:37 +0800538out:
Al Viro2903ff02012-08-28 12:52:22 -0400539 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200540 return ret;
541}
542
543static inline void
544perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
545{
546 struct perf_cgroup_info *t;
547 t = per_cpu_ptr(event->cgrp->info, event->cpu);
548 event->shadow_ctx_time = now - t->timestamp;
549}
550
551static inline void
552perf_cgroup_defer_enabled(struct perf_event *event)
553{
554 /*
555 * when the current task's perf cgroup does not match
556 * the event's, we need to remember to call the
557 * perf_mark_enable() function the first time a task with
558 * a matching perf cgroup is scheduled in.
559 */
560 if (is_cgroup_event(event) && !perf_cgroup_match(event))
561 event->cgrp_defer_enabled = 1;
562}
563
564static inline void
565perf_cgroup_mark_enabled(struct perf_event *event,
566 struct perf_event_context *ctx)
567{
568 struct perf_event *sub;
569 u64 tstamp = perf_event_time(event);
570
571 if (!event->cgrp_defer_enabled)
572 return;
573
574 event->cgrp_defer_enabled = 0;
575
576 event->tstamp_enabled = tstamp - event->total_time_enabled;
577 list_for_each_entry(sub, &event->sibling_list, group_entry) {
578 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
579 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
580 sub->cgrp_defer_enabled = 0;
581 }
582 }
583}
584#else /* !CONFIG_CGROUP_PERF */
585
586static inline bool
587perf_cgroup_match(struct perf_event *event)
588{
589 return true;
590}
591
592static inline void perf_detach_cgroup(struct perf_event *event)
593{}
594
595static inline int is_cgroup_event(struct perf_event *event)
596{
597 return 0;
598}
599
600static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
601{
602 return 0;
603}
604
605static inline void update_cgrp_time_from_event(struct perf_event *event)
606{
607}
608
609static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
610{
611}
612
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200613static inline void perf_cgroup_sched_out(struct task_struct *task,
614 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615{
616}
617
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200618static inline void perf_cgroup_sched_in(struct task_struct *prev,
619 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200620{
621}
622
623static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
624 struct perf_event_attr *attr,
625 struct perf_event *group_leader)
626{
627 return -EINVAL;
628}
629
630static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200631perf_cgroup_set_timestamp(struct task_struct *task,
632 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200633{
634}
635
636void
637perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
638{
639}
640
641static inline void
642perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
643{
644}
645
646static inline u64 perf_cgroup_event_time(struct perf_event *event)
647{
648 return 0;
649}
650
651static inline void
652perf_cgroup_defer_enabled(struct perf_event *event)
653{
654}
655
656static inline void
657perf_cgroup_mark_enabled(struct perf_event *event,
658 struct perf_event_context *ctx)
659{
660}
661#endif
662
Stephane Eranian9e630202013-04-03 14:21:33 +0200663/*
664 * set default to be dependent on timer tick just
665 * like original code
666 */
667#define PERF_CPU_HRTIMER (1000 / HZ)
668/*
669 * function must be called with interrupts disbled
670 */
671static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
672{
673 struct perf_cpu_context *cpuctx;
674 enum hrtimer_restart ret = HRTIMER_NORESTART;
675 int rotations = 0;
676
677 WARN_ON(!irqs_disabled());
678
679 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
680
681 rotations = perf_rotate_context(cpuctx);
682
683 /*
684 * arm timer if needed
685 */
686 if (rotations) {
687 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
688 ret = HRTIMER_RESTART;
689 }
690
691 return ret;
692}
693
694/* CPU is going down */
695void perf_cpu_hrtimer_cancel(int cpu)
696{
697 struct perf_cpu_context *cpuctx;
698 struct pmu *pmu;
699 unsigned long flags;
700
701 if (WARN_ON(cpu != smp_processor_id()))
702 return;
703
704 local_irq_save(flags);
705
706 rcu_read_lock();
707
708 list_for_each_entry_rcu(pmu, &pmus, entry) {
709 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
710
711 if (pmu->task_ctx_nr == perf_sw_context)
712 continue;
713
714 hrtimer_cancel(&cpuctx->hrtimer);
715 }
716
717 rcu_read_unlock();
718
719 local_irq_restore(flags);
720}
721
722static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
723{
724 struct hrtimer *hr = &cpuctx->hrtimer;
725 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200726 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200727
728 /* no multiplexing needed for SW PMU */
729 if (pmu->task_ctx_nr == perf_sw_context)
730 return;
731
Stephane Eranian62b85632013-04-03 14:21:34 +0200732 /*
733 * check default is sane, if not set then force to
734 * default interval (1/tick)
735 */
736 timer = pmu->hrtimer_interval_ms;
737 if (timer < 1)
738 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
739
740 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200741
742 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
743 hr->function = perf_cpu_hrtimer_handler;
744}
745
746static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
747{
748 struct hrtimer *hr = &cpuctx->hrtimer;
749 struct pmu *pmu = cpuctx->ctx.pmu;
750
751 /* not for SW PMU */
752 if (pmu->task_ctx_nr == perf_sw_context)
753 return;
754
755 if (hrtimer_active(hr))
756 return;
757
758 if (!hrtimer_callback_running(hr))
759 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
760 0, HRTIMER_MODE_REL_PINNED, 0);
761}
762
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200763void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200764{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200765 int *count = this_cpu_ptr(pmu->pmu_disable_count);
766 if (!(*count)++)
767 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200768}
769
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200770void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200771{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200772 int *count = this_cpu_ptr(pmu->pmu_disable_count);
773 if (!--(*count))
774 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200775}
776
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200777static DEFINE_PER_CPU(struct list_head, rotation_list);
778
779/*
780 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
781 * because they're strictly cpu affine and rotate_start is called with IRQs
782 * disabled, while rotate_context is called from IRQ context.
783 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200784static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200785{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200786 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200787 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200788
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200789 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200790
Frederic Weisbecker12351ef2013-04-20 15:48:22 +0200791 if (list_empty(&cpuctx->rotation_list)) {
792 int was_empty = list_empty(head);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200793 list_add(&cpuctx->rotation_list, head);
Frederic Weisbecker12351ef2013-04-20 15:48:22 +0200794 if (was_empty)
795 tick_nohz_full_kick();
796 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200797}
798
799static void get_ctx(struct perf_event_context *ctx)
800{
801 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
802}
803
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200804static void put_ctx(struct perf_event_context *ctx)
805{
806 if (atomic_dec_and_test(&ctx->refcount)) {
807 if (ctx->parent_ctx)
808 put_ctx(ctx->parent_ctx);
809 if (ctx->task)
810 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800811 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200812 }
813}
814
815static void unclone_ctx(struct perf_event_context *ctx)
816{
817 if (ctx->parent_ctx) {
818 put_ctx(ctx->parent_ctx);
819 ctx->parent_ctx = NULL;
820 }
821}
822
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200823static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
824{
825 /*
826 * only top level events have the pid namespace they were created in
827 */
828 if (event->parent)
829 event = event->parent;
830
831 return task_tgid_nr_ns(p, event->ns);
832}
833
834static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
835{
836 /*
837 * only top level events have the pid namespace they were created in
838 */
839 if (event->parent)
840 event = event->parent;
841
842 return task_pid_nr_ns(p, event->ns);
843}
844
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200845/*
846 * If we inherit events we want to return the parent event id
847 * to userspace.
848 */
849static u64 primary_event_id(struct perf_event *event)
850{
851 u64 id = event->id;
852
853 if (event->parent)
854 id = event->parent->id;
855
856 return id;
857}
858
859/*
860 * Get the perf_event_context for a task and lock it.
861 * This has to cope with with the fact that until it is locked,
862 * the context could get moved to another task.
863 */
864static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200865perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200866{
867 struct perf_event_context *ctx;
868
869 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200870retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200871 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200872 if (ctx) {
873 /*
874 * If this context is a clone of another, it might
875 * get swapped for another underneath us by
876 * perf_event_task_sched_out, though the
877 * rcu_read_lock() protects us from any context
878 * getting freed. Lock the context and check if it
879 * got swapped before we could get the lock, and retry
880 * if so. If we locked the right context, then it
881 * can't get swapped on us any more.
882 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100883 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200884 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100885 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200886 goto retry;
887 }
888
889 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100890 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200891 ctx = NULL;
892 }
893 }
894 rcu_read_unlock();
895 return ctx;
896}
897
898/*
899 * Get the context for a task and increment its pin_count so it
900 * can't get swapped to another task. This also increments its
901 * reference count so that the context can't get freed.
902 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200903static struct perf_event_context *
904perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200905{
906 struct perf_event_context *ctx;
907 unsigned long flags;
908
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200909 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200910 if (ctx) {
911 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100912 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200913 }
914 return ctx;
915}
916
917static void perf_unpin_context(struct perf_event_context *ctx)
918{
919 unsigned long flags;
920
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100921 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200922 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100923 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200924}
925
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100926/*
927 * Update the record of the current time in a context.
928 */
929static void update_context_time(struct perf_event_context *ctx)
930{
931 u64 now = perf_clock();
932
933 ctx->time += now - ctx->timestamp;
934 ctx->timestamp = now;
935}
936
Stephane Eranian41587552011-01-03 18:20:01 +0200937static u64 perf_event_time(struct perf_event *event)
938{
939 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200940
941 if (is_cgroup_event(event))
942 return perf_cgroup_event_time(event);
943
Stephane Eranian41587552011-01-03 18:20:01 +0200944 return ctx ? ctx->time : 0;
945}
946
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100947/*
948 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -0400949 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100950 */
951static void update_event_times(struct perf_event *event)
952{
953 struct perf_event_context *ctx = event->ctx;
954 u64 run_end;
955
956 if (event->state < PERF_EVENT_STATE_INACTIVE ||
957 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
958 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200959 /*
960 * in cgroup mode, time_enabled represents
961 * the time the event was enabled AND active
962 * tasks were in the monitored cgroup. This is
963 * independent of the activity of the context as
964 * there may be a mix of cgroup and non-cgroup events.
965 *
966 * That is why we treat cgroup events differently
967 * here.
968 */
969 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +0900970 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200971 else if (ctx->is_active)
972 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100973 else
974 run_end = event->tstamp_stopped;
975
976 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100977
978 if (event->state == PERF_EVENT_STATE_INACTIVE)
979 run_end = event->tstamp_stopped;
980 else
Stephane Eranian41587552011-01-03 18:20:01 +0200981 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100982
983 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200984
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100985}
986
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200987/*
988 * Update total_time_enabled and total_time_running for all events in a group.
989 */
990static void update_group_times(struct perf_event *leader)
991{
992 struct perf_event *event;
993
994 update_event_times(leader);
995 list_for_each_entry(event, &leader->sibling_list, group_entry)
996 update_event_times(event);
997}
998
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100999static struct list_head *
1000ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1001{
1002 if (event->attr.pinned)
1003 return &ctx->pinned_groups;
1004 else
1005 return &ctx->flexible_groups;
1006}
1007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008/*
1009 * Add a event from the lists for its context.
1010 * Must be called with ctx->mutex and ctx->lock held.
1011 */
1012static void
1013list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1014{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001015 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1016 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001017
1018 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001019 * If we're a stand alone event or group leader, we go to the context
1020 * list, group events are kept attached to the group so that
1021 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001023 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001024 struct list_head *list;
1025
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001026 if (is_software_event(event))
1027 event->group_flags |= PERF_GROUP_SOFTWARE;
1028
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001029 list = ctx_group_list(event, ctx);
1030 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031 }
1032
Peter Zijlstra08309372011-03-03 11:31:20 +01001033 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001034 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001035
Stephane Eraniand010b332012-02-09 23:21:00 +01001036 if (has_branch_stack(event))
1037 ctx->nr_branch_stack++;
1038
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001039 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001040 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001041 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001042 ctx->nr_events++;
1043 if (event->attr.inherit_stat)
1044 ctx->nr_stat++;
1045}
1046
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001047/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001048 * Initialize event state based on the perf_event_attr::disabled.
1049 */
1050static inline void perf_event__state_init(struct perf_event *event)
1051{
1052 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1053 PERF_EVENT_STATE_INACTIVE;
1054}
1055
1056/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001057 * Called at perf_event creation and when events are attached/detached from a
1058 * group.
1059 */
1060static void perf_event__read_size(struct perf_event *event)
1061{
1062 int entry = sizeof(u64); /* value */
1063 int size = 0;
1064 int nr = 1;
1065
1066 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1067 size += sizeof(u64);
1068
1069 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1070 size += sizeof(u64);
1071
1072 if (event->attr.read_format & PERF_FORMAT_ID)
1073 entry += sizeof(u64);
1074
1075 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1076 nr += event->group_leader->nr_siblings;
1077 size += sizeof(u64);
1078 }
1079
1080 size += entry * nr;
1081 event->read_size = size;
1082}
1083
1084static void perf_event__header_size(struct perf_event *event)
1085{
1086 struct perf_sample_data *data;
1087 u64 sample_type = event->attr.sample_type;
1088 u16 size = 0;
1089
1090 perf_event__read_size(event);
1091
1092 if (sample_type & PERF_SAMPLE_IP)
1093 size += sizeof(data->ip);
1094
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001095 if (sample_type & PERF_SAMPLE_ADDR)
1096 size += sizeof(data->addr);
1097
1098 if (sample_type & PERF_SAMPLE_PERIOD)
1099 size += sizeof(data->period);
1100
Andi Kleenc3feedf2013-01-24 16:10:28 +01001101 if (sample_type & PERF_SAMPLE_WEIGHT)
1102 size += sizeof(data->weight);
1103
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001104 if (sample_type & PERF_SAMPLE_READ)
1105 size += event->read_size;
1106
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001107 if (sample_type & PERF_SAMPLE_DATA_SRC)
1108 size += sizeof(data->data_src.val);
1109
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001110 event->header_size = size;
1111}
1112
1113static void perf_event__id_header_size(struct perf_event *event)
1114{
1115 struct perf_sample_data *data;
1116 u64 sample_type = event->attr.sample_type;
1117 u16 size = 0;
1118
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001119 if (sample_type & PERF_SAMPLE_TID)
1120 size += sizeof(data->tid_entry);
1121
1122 if (sample_type & PERF_SAMPLE_TIME)
1123 size += sizeof(data->time);
1124
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001125 if (sample_type & PERF_SAMPLE_ID)
1126 size += sizeof(data->id);
1127
1128 if (sample_type & PERF_SAMPLE_STREAM_ID)
1129 size += sizeof(data->stream_id);
1130
1131 if (sample_type & PERF_SAMPLE_CPU)
1132 size += sizeof(data->cpu_entry);
1133
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001134 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001135}
1136
Peter Zijlstra8a495422010-05-27 15:47:49 +02001137static void perf_group_attach(struct perf_event *event)
1138{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001139 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001140
Peter Zijlstra74c33372010-10-15 11:40:29 +02001141 /*
1142 * We can have double attach due to group movement in perf_event_open.
1143 */
1144 if (event->attach_state & PERF_ATTACH_GROUP)
1145 return;
1146
Peter Zijlstra8a495422010-05-27 15:47:49 +02001147 event->attach_state |= PERF_ATTACH_GROUP;
1148
1149 if (group_leader == event)
1150 return;
1151
1152 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1153 !is_software_event(event))
1154 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1155
1156 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1157 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001158
1159 perf_event__header_size(group_leader);
1160
1161 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1162 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001163}
1164
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001165/*
1166 * Remove a event from the lists for its context.
1167 * Must be called with ctx->mutex and ctx->lock held.
1168 */
1169static void
1170list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1171{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001172 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001173 /*
1174 * We can have double detach due to exit/hot-unplug + close.
1175 */
1176 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001177 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001178
1179 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1180
Stephane Eranian68cacd22011-03-23 16:03:06 +01001181 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001182 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001183 cpuctx = __get_cpu_context(ctx);
1184 /*
1185 * if there are no more cgroup events
1186 * then cler cgrp to avoid stale pointer
1187 * in update_cgrp_time_from_cpuctx()
1188 */
1189 if (!ctx->nr_cgroups)
1190 cpuctx->cgrp = NULL;
1191 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001192
Stephane Eraniand010b332012-02-09 23:21:00 +01001193 if (has_branch_stack(event))
1194 ctx->nr_branch_stack--;
1195
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001196 ctx->nr_events--;
1197 if (event->attr.inherit_stat)
1198 ctx->nr_stat--;
1199
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200 list_del_rcu(&event->event_entry);
1201
Peter Zijlstra8a495422010-05-27 15:47:49 +02001202 if (event->group_leader == event)
1203 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001205 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001206
1207 /*
1208 * If event was in error state, then keep it
1209 * that way, otherwise bogus counts will be
1210 * returned on read(). The only way to get out
1211 * of error state is by explicit re-enabling
1212 * of the event
1213 */
1214 if (event->state > PERF_EVENT_STATE_OFF)
1215 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001216}
1217
Peter Zijlstra8a495422010-05-27 15:47:49 +02001218static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001219{
1220 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001221 struct list_head *list = NULL;
1222
1223 /*
1224 * We can have double detach due to exit/hot-unplug + close.
1225 */
1226 if (!(event->attach_state & PERF_ATTACH_GROUP))
1227 return;
1228
1229 event->attach_state &= ~PERF_ATTACH_GROUP;
1230
1231 /*
1232 * If this is a sibling, remove it from its group.
1233 */
1234 if (event->group_leader != event) {
1235 list_del_init(&event->group_entry);
1236 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001237 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001238 }
1239
1240 if (!list_empty(&event->group_entry))
1241 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001243 /*
1244 * If this was a group event with sibling events then
1245 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001246 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247 */
1248 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001249 if (list)
1250 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001252
1253 /* Inherit group flags from the previous leader */
1254 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001256
1257out:
1258 perf_event__header_size(event->group_leader);
1259
1260 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1261 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001262}
1263
Stephane Eranianfa66f072010-08-26 16:40:01 +02001264static inline int
1265event_filter_match(struct perf_event *event)
1266{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001267 return (event->cpu == -1 || event->cpu == smp_processor_id())
1268 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001269}
1270
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001271static void
1272event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001273 struct perf_cpu_context *cpuctx,
1274 struct perf_event_context *ctx)
1275{
Stephane Eranian41587552011-01-03 18:20:01 +02001276 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001277 u64 delta;
1278 /*
1279 * An event which could not be activated because of
1280 * filter mismatch still needs to have its timings
1281 * maintained, otherwise bogus information is return
1282 * via read() for time_enabled, time_running:
1283 */
1284 if (event->state == PERF_EVENT_STATE_INACTIVE
1285 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001286 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001287 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001288 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001289 }
1290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001292 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001293
1294 event->state = PERF_EVENT_STATE_INACTIVE;
1295 if (event->pending_disable) {
1296 event->pending_disable = 0;
1297 event->state = PERF_EVENT_STATE_OFF;
1298 }
Stephane Eranian41587552011-01-03 18:20:01 +02001299 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001300 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301 event->oncpu = -1;
1302
1303 if (!is_software_event(event))
1304 cpuctx->active_oncpu--;
1305 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001306 if (event->attr.freq && event->attr.sample_freq)
1307 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 if (event->attr.exclusive || !cpuctx->active_oncpu)
1309 cpuctx->exclusive = 0;
1310}
1311
1312static void
1313group_sched_out(struct perf_event *group_event,
1314 struct perf_cpu_context *cpuctx,
1315 struct perf_event_context *ctx)
1316{
1317 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001318 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001319
1320 event_sched_out(group_event, cpuctx, ctx);
1321
1322 /*
1323 * Schedule out siblings (if any):
1324 */
1325 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1326 event_sched_out(event, cpuctx, ctx);
1327
Stephane Eranianfa66f072010-08-26 16:40:01 +02001328 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001329 cpuctx->exclusive = 0;
1330}
1331
1332/*
1333 * Cross CPU call to remove a performance event
1334 *
1335 * We disable the event on the hardware level first. After that we
1336 * remove it from the context list.
1337 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001338static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001339{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001340 struct perf_event *event = info;
1341 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001342 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001343
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001344 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001347 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1348 ctx->is_active = 0;
1349 cpuctx->task_ctx = NULL;
1350 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001351 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001352
1353 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001354}
1355
1356
1357/*
1358 * Remove the event from a task's (or a CPU's) list of events.
1359 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 * CPU events are removed with a smp call. For task events we only
1361 * call when the task is on a CPU.
1362 *
1363 * If event->ctx is a cloned context, callers must make sure that
1364 * every task struct that event->ctx->task could possibly point to
1365 * remains valid. This is OK when called from perf_release since
1366 * that only calls us on the top-level context, which can't be a clone.
1367 * When called from perf_event_exit_task, it's OK because the
1368 * context has been detached from its task.
1369 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001370static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371{
1372 struct perf_event_context *ctx = event->ctx;
1373 struct task_struct *task = ctx->task;
1374
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001375 lockdep_assert_held(&ctx->mutex);
1376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001377 if (!task) {
1378 /*
1379 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001380 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001381 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001382 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001383 return;
1384 }
1385
1386retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001387 if (!task_function_call(task, __perf_remove_from_context, event))
1388 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001390 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001391 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001392 * If we failed to find a running task, but find the context active now
1393 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001395 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001396 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001397 goto retry;
1398 }
1399
1400 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001401 * Since the task isn't running, its safe to remove the event, us
1402 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001403 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001404 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001405 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406}
1407
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001408/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409 * Cross CPU call to disable a performance event
1410 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301411int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001412{
1413 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001415 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416
1417 /*
1418 * If this is a per-task event, need to check whether this
1419 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001420 *
1421 * Can trigger due to concurrent perf_event_context_sched_out()
1422 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423 */
1424 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001425 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001427 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428
1429 /*
1430 * If the event is on, turn it off.
1431 * If it is in error state, leave it in error state.
1432 */
1433 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1434 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001435 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001436 update_group_times(event);
1437 if (event == event->group_leader)
1438 group_sched_out(event, cpuctx, ctx);
1439 else
1440 event_sched_out(event, cpuctx, ctx);
1441 event->state = PERF_EVENT_STATE_OFF;
1442 }
1443
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001444 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001445
1446 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001447}
1448
1449/*
1450 * Disable a event.
1451 *
1452 * If event->ctx is a cloned context, callers must make sure that
1453 * every task struct that event->ctx->task could possibly point to
1454 * remains valid. This condition is satisifed when called through
1455 * perf_event_for_each_child or perf_event_for_each because they
1456 * hold the top-level event's child_mutex, so any descendant that
1457 * goes to exit will block in sync_child_event.
1458 * When called from perf_pending_event it's OK because event->ctx
1459 * is the current context on this CPU and preemption is disabled,
1460 * hence we can't get into perf_event_task_sched_out for this context.
1461 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001462void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463{
1464 struct perf_event_context *ctx = event->ctx;
1465 struct task_struct *task = ctx->task;
1466
1467 if (!task) {
1468 /*
1469 * Disable the event on the cpu that it's on
1470 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001471 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 return;
1473 }
1474
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001475retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001476 if (!task_function_call(task, __perf_event_disable, event))
1477 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001478
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001479 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001480 /*
1481 * If the event is still active, we need to retry the cross-call.
1482 */
1483 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001484 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001485 /*
1486 * Reload the task pointer, it might have been changed by
1487 * a concurrent perf_event_context_sched_out().
1488 */
1489 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001490 goto retry;
1491 }
1492
1493 /*
1494 * Since we have the lock this context can't be scheduled
1495 * in, so we can change the state safely.
1496 */
1497 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1498 update_group_times(event);
1499 event->state = PERF_EVENT_STATE_OFF;
1500 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001501 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001503EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504
Stephane Eraniane5d13672011-02-14 11:20:01 +02001505static void perf_set_shadow_time(struct perf_event *event,
1506 struct perf_event_context *ctx,
1507 u64 tstamp)
1508{
1509 /*
1510 * use the correct time source for the time snapshot
1511 *
1512 * We could get by without this by leveraging the
1513 * fact that to get to this function, the caller
1514 * has most likely already called update_context_time()
1515 * and update_cgrp_time_xx() and thus both timestamp
1516 * are identical (or very close). Given that tstamp is,
1517 * already adjusted for cgroup, we could say that:
1518 * tstamp - ctx->timestamp
1519 * is equivalent to
1520 * tstamp - cgrp->timestamp.
1521 *
1522 * Then, in perf_output_read(), the calculation would
1523 * work with no changes because:
1524 * - event is guaranteed scheduled in
1525 * - no scheduled out in between
1526 * - thus the timestamp would be the same
1527 *
1528 * But this is a bit hairy.
1529 *
1530 * So instead, we have an explicit cgroup call to remain
1531 * within the time time source all along. We believe it
1532 * is cleaner and simpler to understand.
1533 */
1534 if (is_cgroup_event(event))
1535 perf_cgroup_set_shadow_time(event, tstamp);
1536 else
1537 event->shadow_ctx_time = tstamp - ctx->timestamp;
1538}
1539
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001540#define MAX_INTERRUPTS (~0ULL)
1541
1542static void perf_log_throttle(struct perf_event *event, int enable);
1543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001545event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001547 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001548{
Stephane Eranian41587552011-01-03 18:20:01 +02001549 u64 tstamp = perf_event_time(event);
1550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001551 if (event->state <= PERF_EVENT_STATE_OFF)
1552 return 0;
1553
1554 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001555 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001556
1557 /*
1558 * Unthrottle events, since we scheduled we might have missed several
1559 * ticks already, also for a heavily scheduling task there is little
1560 * guarantee it'll get a tick in a timely manner.
1561 */
1562 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1563 perf_log_throttle(event, 1);
1564 event->hw.interrupts = 0;
1565 }
1566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567 /*
1568 * The new state must be visible before we turn it on in the hardware:
1569 */
1570 smp_wmb();
1571
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001572 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573 event->state = PERF_EVENT_STATE_INACTIVE;
1574 event->oncpu = -1;
1575 return -EAGAIN;
1576 }
1577
Stephane Eranian41587552011-01-03 18:20:01 +02001578 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001579
Stephane Eraniane5d13672011-02-14 11:20:01 +02001580 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001581
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582 if (!is_software_event(event))
1583 cpuctx->active_oncpu++;
1584 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001585 if (event->attr.freq && event->attr.sample_freq)
1586 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001587
1588 if (event->attr.exclusive)
1589 cpuctx->exclusive = 1;
1590
1591 return 0;
1592}
1593
1594static int
1595group_sched_in(struct perf_event *group_event,
1596 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001597 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001598{
Lin Ming6bde9b62010-04-23 13:56:00 +08001599 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001600 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001601 u64 now = ctx->time;
1602 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001603
1604 if (group_event->state == PERF_EVENT_STATE_OFF)
1605 return 0;
1606
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001607 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001608
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001609 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001610 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001611 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001613 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614
1615 /*
1616 * Schedule in siblings as one group (if any):
1617 */
1618 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001619 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620 partial_group = event;
1621 goto group_error;
1622 }
1623 }
1624
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001625 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001626 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628group_error:
1629 /*
1630 * Groups can be scheduled in as one unit only, so undo any
1631 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001632 * The events up to the failed event are scheduled out normally,
1633 * tstamp_stopped will be updated.
1634 *
1635 * The failed events and the remaining siblings need to have
1636 * their timings updated as if they had gone thru event_sched_in()
1637 * and event_sched_out(). This is required to get consistent timings
1638 * across the group. This also takes care of the case where the group
1639 * could never be scheduled by ensuring tstamp_stopped is set to mark
1640 * the time the event was actually stopped, such that time delta
1641 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642 */
1643 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1644 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001645 simulate = true;
1646
1647 if (simulate) {
1648 event->tstamp_running += now - event->tstamp_stopped;
1649 event->tstamp_stopped = now;
1650 } else {
1651 event_sched_out(event, cpuctx, ctx);
1652 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001654 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001656 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001657
Stephane Eranian9e630202013-04-03 14:21:33 +02001658 perf_cpu_hrtimer_restart(cpuctx);
1659
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 return -EAGAIN;
1661}
1662
1663/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664 * Work out whether we can put this event group on the CPU now.
1665 */
1666static int group_can_go_on(struct perf_event *event,
1667 struct perf_cpu_context *cpuctx,
1668 int can_add_hw)
1669{
1670 /*
1671 * Groups consisting entirely of software events can always go on.
1672 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001673 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001674 return 1;
1675 /*
1676 * If an exclusive group is already on, no other hardware
1677 * events can go on.
1678 */
1679 if (cpuctx->exclusive)
1680 return 0;
1681 /*
1682 * If this group is exclusive and there are already
1683 * events on the CPU, it can't go on.
1684 */
1685 if (event->attr.exclusive && cpuctx->active_oncpu)
1686 return 0;
1687 /*
1688 * Otherwise, try to add it if all previous groups were able
1689 * to go on.
1690 */
1691 return can_add_hw;
1692}
1693
1694static void add_event_to_ctx(struct perf_event *event,
1695 struct perf_event_context *ctx)
1696{
Stephane Eranian41587552011-01-03 18:20:01 +02001697 u64 tstamp = perf_event_time(event);
1698
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001700 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001701 event->tstamp_enabled = tstamp;
1702 event->tstamp_running = tstamp;
1703 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704}
1705
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001706static void task_ctx_sched_out(struct perf_event_context *ctx);
1707static void
1708ctx_sched_in(struct perf_event_context *ctx,
1709 struct perf_cpu_context *cpuctx,
1710 enum event_type_t event_type,
1711 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001712
Peter Zijlstradce58552011-04-09 21:17:46 +02001713static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1714 struct perf_event_context *ctx,
1715 struct task_struct *task)
1716{
1717 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1718 if (ctx)
1719 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1720 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1721 if (ctx)
1722 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1723}
1724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001725/*
1726 * Cross CPU call to install and enable a performance event
1727 *
1728 * Must be called with ctx->mutex held
1729 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001730static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001732 struct perf_event *event = info;
1733 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001734 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001735 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1736 struct task_struct *task = current;
1737
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001738 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001739 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001740
1741 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001742 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001744 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001745 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001746
1747 /*
1748 * If the context we're installing events in is not the
1749 * active task_ctx, flip them.
1750 */
1751 if (ctx->task && task_ctx != ctx) {
1752 if (task_ctx)
1753 raw_spin_unlock(&task_ctx->lock);
1754 raw_spin_lock(&ctx->lock);
1755 task_ctx = ctx;
1756 }
1757
1758 if (task_ctx) {
1759 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001760 task = task_ctx->task;
1761 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001762
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001763 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001764
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001766 /*
1767 * update cgrp time only if current cgrp
1768 * matches event->cgrp. Must be done before
1769 * calling add_event_to_ctx()
1770 */
1771 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773 add_event_to_ctx(event, ctx);
1774
1775 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001776 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001778 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001780 perf_pmu_enable(cpuctx->ctx.pmu);
1781 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001782
1783 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784}
1785
1786/*
1787 * Attach a performance event to a context
1788 *
1789 * First we add the event to the list with the hardware enable bit
1790 * in event->hw_config cleared.
1791 *
1792 * If the event is attached to a task which is on a CPU we use a smp
1793 * call to enable it in the task context. The task might have been
1794 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001795 */
1796static void
1797perf_install_in_context(struct perf_event_context *ctx,
1798 struct perf_event *event,
1799 int cpu)
1800{
1801 struct task_struct *task = ctx->task;
1802
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001803 lockdep_assert_held(&ctx->mutex);
1804
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001805 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001806 if (event->cpu != -1)
1807 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001808
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809 if (!task) {
1810 /*
1811 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001812 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001814 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001815 return;
1816 }
1817
1818retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001819 if (!task_function_call(task, __perf_install_in_context, event))
1820 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001822 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001823 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001824 * If we failed to find a running task, but find the context active now
1825 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001826 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001827 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001828 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829 goto retry;
1830 }
1831
1832 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001833 * Since the task isn't running, its safe to add the event, us holding
1834 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001835 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001836 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001837 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838}
1839
1840/*
1841 * Put a event into inactive state and update time fields.
1842 * Enabling the leader of a group effectively enables all
1843 * the group members that aren't explicitly disabled, so we
1844 * have to update their ->tstamp_enabled also.
1845 * Note: this works for group members as well as group leaders
1846 * since the non-leader members' sibling_lists will be empty.
1847 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001848static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001849{
1850 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001851 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001852
1853 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001854 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001855 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001856 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1857 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001858 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859}
1860
1861/*
1862 * Cross CPU call to enable a performance event
1863 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001864static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001865{
1866 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867 struct perf_event_context *ctx = event->ctx;
1868 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001869 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001870 int err;
1871
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001872 if (WARN_ON_ONCE(!ctx->is_active))
1873 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001875 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876 update_context_time(ctx);
1877
1878 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1879 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001880
1881 /*
1882 * set current task's cgroup time reference point
1883 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001884 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001885
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001886 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001887
Stephane Eraniane5d13672011-02-14 11:20:01 +02001888 if (!event_filter_match(event)) {
1889 if (is_cgroup_event(event))
1890 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001891 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001892 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001893
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 /*
1895 * If the event is in a group and isn't the group leader,
1896 * then don't put it on unless the group is on.
1897 */
1898 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1899 goto unlock;
1900
1901 if (!group_can_go_on(event, cpuctx, 1)) {
1902 err = -EEXIST;
1903 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001905 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001906 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001907 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908 }
1909
1910 if (err) {
1911 /*
1912 * If this event can't go on and it's part of a
1913 * group, then the whole group has to come off.
1914 */
Stephane Eranian9e630202013-04-03 14:21:33 +02001915 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02001917 perf_cpu_hrtimer_restart(cpuctx);
1918 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919 if (leader->attr.pinned) {
1920 update_group_times(leader);
1921 leader->state = PERF_EVENT_STATE_ERROR;
1922 }
1923 }
1924
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001925unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001926 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001927
1928 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001929}
1930
1931/*
1932 * Enable a event.
1933 *
1934 * If event->ctx is a cloned context, callers must make sure that
1935 * every task struct that event->ctx->task could possibly point to
1936 * remains valid. This condition is satisfied when called through
1937 * perf_event_for_each_child or perf_event_for_each as described
1938 * for perf_event_disable.
1939 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001940void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001941{
1942 struct perf_event_context *ctx = event->ctx;
1943 struct task_struct *task = ctx->task;
1944
1945 if (!task) {
1946 /*
1947 * Enable the event on the cpu that it's on
1948 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001949 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001950 return;
1951 }
1952
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001953 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1955 goto out;
1956
1957 /*
1958 * If the event is in error state, clear that first.
1959 * That way, if we see the event in error state below, we
1960 * know that it has gone back into error state, as distinct
1961 * from the task having been scheduled away before the
1962 * cross-call arrived.
1963 */
1964 if (event->state == PERF_EVENT_STATE_ERROR)
1965 event->state = PERF_EVENT_STATE_OFF;
1966
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001967retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001968 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001969 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001970 goto out;
1971 }
1972
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001973 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001974
1975 if (!task_function_call(task, __perf_event_enable, event))
1976 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001978 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001979
1980 /*
1981 * If the context is active and the event is still off,
1982 * we need to retry the cross-call.
1983 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001984 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1985 /*
1986 * task could have been flipped by a concurrent
1987 * perf_event_context_sched_out()
1988 */
1989 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001991 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001992
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001993out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001994 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001996EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001997
Avi Kivity26ca5c12011-06-29 18:42:37 +03001998int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001999{
2000 /*
2001 * not supported on inherited events
2002 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002003 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 return -EINVAL;
2005
2006 atomic_add(refresh, &event->event_limit);
2007 perf_event_enable(event);
2008
2009 return 0;
2010}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002011EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002012
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002013static void ctx_sched_out(struct perf_event_context *ctx,
2014 struct perf_cpu_context *cpuctx,
2015 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016{
2017 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002018 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019
Peter Zijlstradb24d332011-04-09 21:17:45 +02002020 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002022 return;
2023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002025 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002026 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002027 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002028
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002029 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002030 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002031 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2032 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002033 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002034
Peter Zijlstradb24d332011-04-09 21:17:45 +02002035 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002036 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002037 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002038 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002039 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002040}
2041
2042/*
2043 * Test whether two contexts are equivalent, i.e. whether they
2044 * have both been cloned from the same version of the same context
2045 * and they both have the same number of enabled events.
2046 * If the number of enabled events is the same, then the set
2047 * of enabled events should be the same, because these are both
2048 * inherited contexts, therefore we can't access individual events
2049 * in them directly with an fd; we can only enable/disable all
2050 * events via prctl, or enable/disable all events in a family
2051 * via ioctl, which will have the same effect on both contexts.
2052 */
2053static int context_equiv(struct perf_event_context *ctx1,
2054 struct perf_event_context *ctx2)
2055{
2056 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
2057 && ctx1->parent_gen == ctx2->parent_gen
2058 && !ctx1->pin_count && !ctx2->pin_count;
2059}
2060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061static void __perf_event_sync_stat(struct perf_event *event,
2062 struct perf_event *next_event)
2063{
2064 u64 value;
2065
2066 if (!event->attr.inherit_stat)
2067 return;
2068
2069 /*
2070 * Update the event value, we cannot use perf_event_read()
2071 * because we're in the middle of a context switch and have IRQs
2072 * disabled, which upsets smp_call_function_single(), however
2073 * we know the event must be on the current CPU, therefore we
2074 * don't need to use it.
2075 */
2076 switch (event->state) {
2077 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002078 event->pmu->read(event);
2079 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080
2081 case PERF_EVENT_STATE_INACTIVE:
2082 update_event_times(event);
2083 break;
2084
2085 default:
2086 break;
2087 }
2088
2089 /*
2090 * In order to keep per-task stats reliable we need to flip the event
2091 * values when we flip the contexts.
2092 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002093 value = local64_read(&next_event->count);
2094 value = local64_xchg(&event->count, value);
2095 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096
2097 swap(event->total_time_enabled, next_event->total_time_enabled);
2098 swap(event->total_time_running, next_event->total_time_running);
2099
2100 /*
2101 * Since we swizzled the values, update the user visible data too.
2102 */
2103 perf_event_update_userpage(event);
2104 perf_event_update_userpage(next_event);
2105}
2106
2107#define list_next_entry(pos, member) \
2108 list_entry(pos->member.next, typeof(*pos), member)
2109
2110static void perf_event_sync_stat(struct perf_event_context *ctx,
2111 struct perf_event_context *next_ctx)
2112{
2113 struct perf_event *event, *next_event;
2114
2115 if (!ctx->nr_stat)
2116 return;
2117
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002118 update_context_time(ctx);
2119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002120 event = list_first_entry(&ctx->event_list,
2121 struct perf_event, event_entry);
2122
2123 next_event = list_first_entry(&next_ctx->event_list,
2124 struct perf_event, event_entry);
2125
2126 while (&event->event_entry != &ctx->event_list &&
2127 &next_event->event_entry != &next_ctx->event_list) {
2128
2129 __perf_event_sync_stat(event, next_event);
2130
2131 event = list_next_entry(event, event_entry);
2132 next_event = list_next_entry(next_event, event_entry);
2133 }
2134}
2135
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002136static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2137 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002139 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140 struct perf_event_context *next_ctx;
2141 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002142 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143 int do_switch = 1;
2144
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002145 if (likely(!ctx))
2146 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002148 cpuctx = __get_cpu_context(ctx);
2149 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002150 return;
2151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002152 rcu_read_lock();
2153 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002154 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155 if (parent && next_ctx &&
2156 rcu_dereference(next_ctx->parent_ctx) == parent) {
2157 /*
2158 * Looks like the two contexts are clones, so we might be
2159 * able to optimize the context switch. We lock both
2160 * contexts and check that they are clones under the
2161 * lock (including re-checking that neither has been
2162 * uncloned in the meantime). It doesn't matter which
2163 * order we take the locks because no other cpu could
2164 * be trying to lock both of these tasks.
2165 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002166 raw_spin_lock(&ctx->lock);
2167 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002168 if (context_equiv(ctx, next_ctx)) {
2169 /*
2170 * XXX do we need a memory barrier of sorts
2171 * wrt to rcu_dereference() of perf_event_ctxp
2172 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002173 task->perf_event_ctxp[ctxn] = next_ctx;
2174 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175 ctx->task = next;
2176 next_ctx->task = task;
2177 do_switch = 0;
2178
2179 perf_event_sync_stat(ctx, next_ctx);
2180 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002181 raw_spin_unlock(&next_ctx->lock);
2182 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002183 }
2184 rcu_read_unlock();
2185
2186 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002187 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002188 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002190 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191 }
2192}
2193
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002194#define for_each_task_context_nr(ctxn) \
2195 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2196
2197/*
2198 * Called from scheduler to remove the events of the current task,
2199 * with interrupts disabled.
2200 *
2201 * We stop each event and update the event value in event->count.
2202 *
2203 * This does not protect us against NMI, but disable()
2204 * sets the disabled bit in the control field of event _before_
2205 * accessing the event control register. If a NMI hits, then it will
2206 * not restart the event.
2207 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002208void __perf_event_task_sched_out(struct task_struct *task,
2209 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002210{
2211 int ctxn;
2212
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002213 for_each_task_context_nr(ctxn)
2214 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002215
2216 /*
2217 * if cgroup events exist on this CPU, then we need
2218 * to check if we have to switch out PMU state.
2219 * cgroup event are system-wide mode only
2220 */
2221 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002222 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002223}
2224
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002225static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002226{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002227 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228
2229 if (!cpuctx->task_ctx)
2230 return;
2231
2232 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2233 return;
2234
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002235 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002236 cpuctx->task_ctx = NULL;
2237}
2238
2239/*
2240 * Called with IRQs disabled
2241 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002242static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2243 enum event_type_t event_type)
2244{
2245 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002246}
2247
2248static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002249ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002250 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251{
2252 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002254 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2255 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002257 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002258 continue;
2259
Stephane Eraniane5d13672011-02-14 11:20:01 +02002260 /* may need to reset tstamp_enabled */
2261 if (is_cgroup_event(event))
2262 perf_cgroup_mark_enabled(event, ctx);
2263
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002264 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002265 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266
2267 /*
2268 * If this pinned group hasn't been scheduled,
2269 * put it in error state.
2270 */
2271 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2272 update_group_times(event);
2273 event->state = PERF_EVENT_STATE_ERROR;
2274 }
2275 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002276}
2277
2278static void
2279ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002280 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002281{
2282 struct perf_event *event;
2283 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002285 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2286 /* Ignore events in OFF or ERROR state */
2287 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002289 /*
2290 * Listen to the 'cpu' scheduling filter constraint
2291 * of events:
2292 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002293 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002294 continue;
2295
Stephane Eraniane5d13672011-02-14 11:20:01 +02002296 /* may need to reset tstamp_enabled */
2297 if (is_cgroup_event(event))
2298 perf_cgroup_mark_enabled(event, ctx);
2299
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002300 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002301 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002302 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002303 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002305}
2306
2307static void
2308ctx_sched_in(struct perf_event_context *ctx,
2309 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002310 enum event_type_t event_type,
2311 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002312{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002313 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002314 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002315
Peter Zijlstradb24d332011-04-09 21:17:45 +02002316 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002317 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002318 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002319
Stephane Eraniane5d13672011-02-14 11:20:01 +02002320 now = perf_clock();
2321 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002322 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002323 /*
2324 * First go through the list and put on any pinned groups
2325 * in order to give them the best chance of going on.
2326 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002327 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002328 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002329
2330 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002331 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002332 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333}
2334
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002335static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002336 enum event_type_t event_type,
2337 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002338{
2339 struct perf_event_context *ctx = &cpuctx->ctx;
2340
Stephane Eraniane5d13672011-02-14 11:20:01 +02002341 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002342}
2343
Stephane Eraniane5d13672011-02-14 11:20:01 +02002344static void perf_event_context_sched_in(struct perf_event_context *ctx,
2345 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002346{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002347 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002348
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002349 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002350 if (cpuctx->task_ctx == ctx)
2351 return;
2352
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002353 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002354 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002355 /*
2356 * We want to keep the following priority order:
2357 * cpu pinned (that don't need to move), task pinned,
2358 * cpu flexible, task flexible.
2359 */
2360 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2361
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002362 if (ctx->nr_events)
2363 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002364
Gleb Natapov86b47c22011-11-22 16:08:21 +02002365 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2366
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002367 perf_pmu_enable(ctx->pmu);
2368 perf_ctx_unlock(cpuctx, ctx);
2369
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002370 /*
2371 * Since these rotations are per-cpu, we need to ensure the
2372 * cpu-context we got scheduled on is actually rotating.
2373 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002374 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375}
2376
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002377/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002378 * When sampling the branck stack in system-wide, it may be necessary
2379 * to flush the stack on context switch. This happens when the branch
2380 * stack does not tag its entries with the pid of the current task.
2381 * Otherwise it becomes impossible to associate a branch entry with a
2382 * task. This ambiguity is more likely to appear when the branch stack
2383 * supports priv level filtering and the user sets it to monitor only
2384 * at the user level (which could be a useful measurement in system-wide
2385 * mode). In that case, the risk is high of having a branch stack with
2386 * branch from multiple tasks. Flushing may mean dropping the existing
2387 * entries or stashing them somewhere in the PMU specific code layer.
2388 *
2389 * This function provides the context switch callback to the lower code
2390 * layer. It is invoked ONLY when there is at least one system-wide context
2391 * with at least one active event using taken branch sampling.
2392 */
2393static void perf_branch_stack_sched_in(struct task_struct *prev,
2394 struct task_struct *task)
2395{
2396 struct perf_cpu_context *cpuctx;
2397 struct pmu *pmu;
2398 unsigned long flags;
2399
2400 /* no need to flush branch stack if not changing task */
2401 if (prev == task)
2402 return;
2403
2404 local_irq_save(flags);
2405
2406 rcu_read_lock();
2407
2408 list_for_each_entry_rcu(pmu, &pmus, entry) {
2409 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2410
2411 /*
2412 * check if the context has at least one
2413 * event using PERF_SAMPLE_BRANCH_STACK
2414 */
2415 if (cpuctx->ctx.nr_branch_stack > 0
2416 && pmu->flush_branch_stack) {
2417
2418 pmu = cpuctx->ctx.pmu;
2419
2420 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2421
2422 perf_pmu_disable(pmu);
2423
2424 pmu->flush_branch_stack();
2425
2426 perf_pmu_enable(pmu);
2427
2428 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2429 }
2430 }
2431
2432 rcu_read_unlock();
2433
2434 local_irq_restore(flags);
2435}
2436
2437/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002438 * Called from scheduler to add the events of the current task
2439 * with interrupts disabled.
2440 *
2441 * We restore the event value and then enable it.
2442 *
2443 * This does not protect us against NMI, but enable()
2444 * sets the enabled bit in the control field of event _before_
2445 * accessing the event control register. If a NMI hits, then it will
2446 * keep the event running.
2447 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002448void __perf_event_task_sched_in(struct task_struct *prev,
2449 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002450{
2451 struct perf_event_context *ctx;
2452 int ctxn;
2453
2454 for_each_task_context_nr(ctxn) {
2455 ctx = task->perf_event_ctxp[ctxn];
2456 if (likely(!ctx))
2457 continue;
2458
Stephane Eraniane5d13672011-02-14 11:20:01 +02002459 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002460 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002461 /*
2462 * if cgroup events exist on this CPU, then we need
2463 * to check if we have to switch in PMU state.
2464 * cgroup event are system-wide mode only
2465 */
2466 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002467 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002468
2469 /* check for system-wide branch_stack events */
2470 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2471 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472}
2473
Peter Zijlstraabd50712010-01-26 18:50:16 +01002474static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2475{
2476 u64 frequency = event->attr.sample_freq;
2477 u64 sec = NSEC_PER_SEC;
2478 u64 divisor, dividend;
2479
2480 int count_fls, nsec_fls, frequency_fls, sec_fls;
2481
2482 count_fls = fls64(count);
2483 nsec_fls = fls64(nsec);
2484 frequency_fls = fls64(frequency);
2485 sec_fls = 30;
2486
2487 /*
2488 * We got @count in @nsec, with a target of sample_freq HZ
2489 * the target period becomes:
2490 *
2491 * @count * 10^9
2492 * period = -------------------
2493 * @nsec * sample_freq
2494 *
2495 */
2496
2497 /*
2498 * Reduce accuracy by one bit such that @a and @b converge
2499 * to a similar magnitude.
2500 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002501#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002502do { \
2503 if (a##_fls > b##_fls) { \
2504 a >>= 1; \
2505 a##_fls--; \
2506 } else { \
2507 b >>= 1; \
2508 b##_fls--; \
2509 } \
2510} while (0)
2511
2512 /*
2513 * Reduce accuracy until either term fits in a u64, then proceed with
2514 * the other, so that finally we can do a u64/u64 division.
2515 */
2516 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2517 REDUCE_FLS(nsec, frequency);
2518 REDUCE_FLS(sec, count);
2519 }
2520
2521 if (count_fls + sec_fls > 64) {
2522 divisor = nsec * frequency;
2523
2524 while (count_fls + sec_fls > 64) {
2525 REDUCE_FLS(count, sec);
2526 divisor >>= 1;
2527 }
2528
2529 dividend = count * sec;
2530 } else {
2531 dividend = count * sec;
2532
2533 while (nsec_fls + frequency_fls > 64) {
2534 REDUCE_FLS(nsec, frequency);
2535 dividend >>= 1;
2536 }
2537
2538 divisor = nsec * frequency;
2539 }
2540
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002541 if (!divisor)
2542 return dividend;
2543
Peter Zijlstraabd50712010-01-26 18:50:16 +01002544 return div64_u64(dividend, divisor);
2545}
2546
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002547static DEFINE_PER_CPU(int, perf_throttled_count);
2548static DEFINE_PER_CPU(u64, perf_throttled_seq);
2549
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002550static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002551{
2552 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002553 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002554 s64 delta;
2555
Peter Zijlstraabd50712010-01-26 18:50:16 +01002556 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002557
2558 delta = (s64)(period - hwc->sample_period);
2559 delta = (delta + 7) / 8; /* low pass filter */
2560
2561 sample_period = hwc->sample_period + delta;
2562
2563 if (!sample_period)
2564 sample_period = 1;
2565
2566 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002567
Peter Zijlstrae7850592010-05-21 14:43:08 +02002568 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002569 if (disable)
2570 event->pmu->stop(event, PERF_EF_UPDATE);
2571
Peter Zijlstrae7850592010-05-21 14:43:08 +02002572 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002573
2574 if (disable)
2575 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002576 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577}
2578
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002579/*
2580 * combine freq adjustment with unthrottling to avoid two passes over the
2581 * events. At the same time, make sure, having freq events does not change
2582 * the rate of unthrottling as that would introduce bias.
2583 */
2584static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2585 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002586{
2587 struct perf_event *event;
2588 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002589 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002590 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002592 /*
2593 * only need to iterate over all events iff:
2594 * - context have events in frequency mode (needs freq adjust)
2595 * - there are events to unthrottle on this cpu
2596 */
2597 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002598 return;
2599
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002600 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002601 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002602
Paul Mackerras03541f82009-10-14 16:58:03 +11002603 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002604 if (event->state != PERF_EVENT_STATE_ACTIVE)
2605 continue;
2606
Stephane Eranian5632ab12011-01-03 18:20:01 +02002607 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002608 continue;
2609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002610 hwc = &event->hw;
2611
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002612 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2613 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002614 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002615 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002616 }
2617
2618 if (!event->attr.freq || !event->attr.sample_freq)
2619 continue;
2620
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002621 /*
2622 * stop the event and update event->count
2623 */
2624 event->pmu->stop(event, PERF_EF_UPDATE);
2625
Peter Zijlstrae7850592010-05-21 14:43:08 +02002626 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002627 delta = now - hwc->freq_count_stamp;
2628 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002629
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002630 /*
2631 * restart the event
2632 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002633 * we have stopped the event so tell that
2634 * to perf_adjust_period() to avoid stopping it
2635 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002636 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002637 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002638 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002639
2640 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002641 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002642
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002643 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002644 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645}
2646
2647/*
2648 * Round-robin a context's events:
2649 */
2650static void rotate_ctx(struct perf_event_context *ctx)
2651{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002652 /*
2653 * Rotate the first entry last of non-pinned groups. Rotation might be
2654 * disabled by the inheritance code.
2655 */
2656 if (!ctx->rotate_disable)
2657 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002658}
2659
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002660/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002661 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2662 * because they're strictly cpu affine and rotate_start is called with IRQs
2663 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002664 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002665static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002666{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002667 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002668 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002669
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002670 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002671 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002672 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2673 rotate = 1;
2674 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002675
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002676 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002677 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002678 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002679 if (ctx->nr_events != ctx->nr_active)
2680 rotate = 1;
2681 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002683 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002684 goto done;
2685
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002686 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002687 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002688
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002689 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2690 if (ctx)
2691 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002692
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002693 rotate_ctx(&cpuctx->ctx);
2694 if (ctx)
2695 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002697 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002698
2699 perf_pmu_enable(cpuctx->ctx.pmu);
2700 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002701done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002702 if (remove)
2703 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002704
2705 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002706}
2707
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002708#ifdef CONFIG_NO_HZ_FULL
2709bool perf_event_can_stop_tick(void)
2710{
2711 if (list_empty(&__get_cpu_var(rotation_list)))
2712 return true;
2713 else
2714 return false;
2715}
2716#endif
2717
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002718void perf_event_task_tick(void)
2719{
2720 struct list_head *head = &__get_cpu_var(rotation_list);
2721 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002722 struct perf_event_context *ctx;
2723 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002724
2725 WARN_ON(!irqs_disabled());
2726
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002727 __this_cpu_inc(perf_throttled_seq);
2728 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2729
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002730 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002731 ctx = &cpuctx->ctx;
2732 perf_adjust_freq_unthr_context(ctx, throttled);
2733
2734 ctx = cpuctx->task_ctx;
2735 if (ctx)
2736 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002737 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002738}
2739
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002740static int event_enable_on_exec(struct perf_event *event,
2741 struct perf_event_context *ctx)
2742{
2743 if (!event->attr.enable_on_exec)
2744 return 0;
2745
2746 event->attr.enable_on_exec = 0;
2747 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2748 return 0;
2749
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002750 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002751
2752 return 1;
2753}
2754
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755/*
2756 * Enable all of a task's events that have been marked enable-on-exec.
2757 * This expects task == current.
2758 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002759static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002761 struct perf_event *event;
2762 unsigned long flags;
2763 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002764 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002765
2766 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767 if (!ctx || !ctx->nr_events)
2768 goto out;
2769
Stephane Eraniane566b762011-04-06 02:54:54 +02002770 /*
2771 * We must ctxsw out cgroup events to avoid conflict
2772 * when invoking perf_task_event_sched_in() later on
2773 * in this function. Otherwise we end up trying to
2774 * ctxswin cgroup events which are already scheduled
2775 * in.
2776 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002777 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002779 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002780 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002781
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002782 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002783 ret = event_enable_on_exec(event, ctx);
2784 if (ret)
2785 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002786 }
2787
2788 /*
2789 * Unclone this context if we enabled any event.
2790 */
2791 if (enabled)
2792 unclone_ctx(ctx);
2793
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002794 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002795
Stephane Eraniane566b762011-04-06 02:54:54 +02002796 /*
2797 * Also calls ctxswin for cgroup events, if any:
2798 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002799 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002800out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002801 local_irq_restore(flags);
2802}
2803
2804/*
2805 * Cross CPU call to read the hardware event
2806 */
2807static void __perf_event_read(void *info)
2808{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002809 struct perf_event *event = info;
2810 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002811 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002812
2813 /*
2814 * If this is a task context, we need to check whether it is
2815 * the current task context of this cpu. If not it has been
2816 * scheduled out before the smp call arrived. In that case
2817 * event->count would have been updated to a recent sample
2818 * when the event was scheduled out.
2819 */
2820 if (ctx->task && cpuctx->task_ctx != ctx)
2821 return;
2822
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002823 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002824 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002825 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002826 update_cgrp_time_from_event(event);
2827 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002828 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002829 if (event->state == PERF_EVENT_STATE_ACTIVE)
2830 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002831 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002832}
2833
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002834static inline u64 perf_event_count(struct perf_event *event)
2835{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002836 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002837}
2838
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002839static u64 perf_event_read(struct perf_event *event)
2840{
2841 /*
2842 * If event is enabled and currently active on a CPU, update the
2843 * value in the event structure:
2844 */
2845 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2846 smp_call_function_single(event->oncpu,
2847 __perf_event_read, event, 1);
2848 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002849 struct perf_event_context *ctx = event->ctx;
2850 unsigned long flags;
2851
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002852 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002853 /*
2854 * may read while context is not active
2855 * (e.g., thread is blocked), in that case
2856 * we cannot update context time
2857 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002858 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002859 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002860 update_cgrp_time_from_event(event);
2861 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002862 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002863 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002864 }
2865
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002866 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867}
2868
2869/*
2870 * Initialize the perf_event context in a task_struct:
2871 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002872static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002873{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002874 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002875 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002876 INIT_LIST_HEAD(&ctx->pinned_groups);
2877 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002878 INIT_LIST_HEAD(&ctx->event_list);
2879 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002880}
2881
Peter Zijlstraeb184472010-09-07 15:55:13 +02002882static struct perf_event_context *
2883alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002884{
2885 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002886
2887 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2888 if (!ctx)
2889 return NULL;
2890
2891 __perf_event_init_context(ctx);
2892 if (task) {
2893 ctx->task = task;
2894 get_task_struct(task);
2895 }
2896 ctx->pmu = pmu;
2897
2898 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002899}
2900
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002901static struct task_struct *
2902find_lively_task_by_vpid(pid_t vpid)
2903{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905 int err;
2906
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002908 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909 task = current;
2910 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002911 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912 if (task)
2913 get_task_struct(task);
2914 rcu_read_unlock();
2915
2916 if (!task)
2917 return ERR_PTR(-ESRCH);
2918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919 /* Reuse ptrace permission checks for now. */
2920 err = -EACCES;
2921 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2922 goto errout;
2923
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002924 return task;
2925errout:
2926 put_task_struct(task);
2927 return ERR_PTR(err);
2928
2929}
2930
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002931/*
2932 * Returns a matching context with refcount and pincount.
2933 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002934static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002935find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002936{
2937 struct perf_event_context *ctx;
2938 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002939 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002940 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002941
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002942 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002943 /* Must be root to operate on a CPU event: */
2944 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2945 return ERR_PTR(-EACCES);
2946
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947 /*
2948 * We could be clever and allow to attach a event to an
2949 * offline CPU and activate it when the CPU comes up, but
2950 * that's for later.
2951 */
2952 if (!cpu_online(cpu))
2953 return ERR_PTR(-ENODEV);
2954
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002955 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002956 ctx = &cpuctx->ctx;
2957 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002958 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959
2960 return ctx;
2961 }
2962
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002963 err = -EINVAL;
2964 ctxn = pmu->task_ctx_nr;
2965 if (ctxn < 0)
2966 goto errout;
2967
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002968retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002969 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970 if (ctx) {
2971 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002972 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002973 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002974 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002975 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976 err = -ENOMEM;
2977 if (!ctx)
2978 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002979
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002980 err = 0;
2981 mutex_lock(&task->perf_event_mutex);
2982 /*
2983 * If it has already passed perf_event_exit_task().
2984 * we must see PF_EXITING, it takes this mutex too.
2985 */
2986 if (task->flags & PF_EXITING)
2987 err = -ESRCH;
2988 else if (task->perf_event_ctxp[ctxn])
2989 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002990 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002991 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002992 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002993 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002994 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002995 mutex_unlock(&task->perf_event_mutex);
2996
2997 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02002998 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002999
3000 if (err == -EAGAIN)
3001 goto retry;
3002 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003003 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003004 }
3005
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006 return ctx;
3007
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003008errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003009 return ERR_PTR(err);
3010}
3011
Li Zefan6fb29152009-10-15 11:21:42 +08003012static void perf_event_free_filter(struct perf_event *event);
3013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014static void free_event_rcu(struct rcu_head *head)
3015{
3016 struct perf_event *event;
3017
3018 event = container_of(head, struct perf_event, rcu_head);
3019 if (event->ns)
3020 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003021 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022 kfree(event);
3023}
3024
Frederic Weisbecker76369132011-05-19 19:55:04 +02003025static void ring_buffer_put(struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026
3027static void free_event(struct perf_event *event)
3028{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003029 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030
3031 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02003032 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01003033 static_key_slow_dec_deferred(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01003034 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003035 atomic_dec(&nr_mmap_events);
3036 if (event->attr.comm)
3037 atomic_dec(&nr_comm_events);
3038 if (event->attr.task)
3039 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003040 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3041 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01003042 if (is_cgroup_event(event)) {
3043 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01003044 static_key_slow_dec_deferred(&perf_sched_events);
Peter Zijlstra08309372011-03-03 11:31:20 +01003045 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003046
3047 if (has_branch_stack(event)) {
3048 static_key_slow_dec_deferred(&perf_sched_events);
3049 /* is system-wide event */
3050 if (!(event->attach_state & PERF_ATTACH_TASK))
3051 atomic_dec(&per_cpu(perf_branch_stack_events,
3052 event->cpu));
3053 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003054 }
3055
Frederic Weisbecker76369132011-05-19 19:55:04 +02003056 if (event->rb) {
3057 ring_buffer_put(event->rb);
3058 event->rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059 }
3060
Stephane Eraniane5d13672011-02-14 11:20:01 +02003061 if (is_cgroup_event(event))
3062 perf_detach_cgroup(event);
3063
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064 if (event->destroy)
3065 event->destroy(event);
3066
Peter Zijlstra0c67b402010-09-13 11:15:58 +02003067 if (event->ctx)
3068 put_ctx(event->ctx);
3069
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003070 call_rcu(&event->rcu_head, free_event_rcu);
3071}
3072
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003073int perf_event_release_kernel(struct perf_event *event)
3074{
3075 struct perf_event_context *ctx = event->ctx;
3076
3077 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02003078 /*
3079 * There are two ways this annotation is useful:
3080 *
3081 * 1) there is a lock recursion from perf_event_exit_task
3082 * see the comment there.
3083 *
3084 * 2) there is a lock-inversion with mmap_sem through
3085 * perf_event_read_group(), which takes faults while
3086 * holding ctx->mutex, however this is called after
3087 * the last filedesc died, so there is no possibility
3088 * to trigger the AB-BA case.
3089 */
3090 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003091 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02003092 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003093 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02003094 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003095 mutex_unlock(&ctx->mutex);
3096
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003097 free_event(event);
3098
3099 return 0;
3100}
3101EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3102
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003103/*
3104 * Called when the last reference to the file is gone.
3105 */
Al Viroa6fa9412012-08-20 14:59:25 +01003106static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003107{
Peter Zijlstra88821352010-11-09 19:01:43 +01003108 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003109
Al Viroa6fa9412012-08-20 14:59:25 +01003110 if (!atomic_long_dec_and_test(&event->refcount))
3111 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003112
Peter Zijlstra88821352010-11-09 19:01:43 +01003113 rcu_read_lock();
3114 owner = ACCESS_ONCE(event->owner);
3115 /*
3116 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3117 * !owner it means the list deletion is complete and we can indeed
3118 * free this event, otherwise we need to serialize on
3119 * owner->perf_event_mutex.
3120 */
3121 smp_read_barrier_depends();
3122 if (owner) {
3123 /*
3124 * Since delayed_put_task_struct() also drops the last
3125 * task reference we can safely take a new reference
3126 * while holding the rcu_read_lock().
3127 */
3128 get_task_struct(owner);
3129 }
3130 rcu_read_unlock();
3131
3132 if (owner) {
3133 mutex_lock(&owner->perf_event_mutex);
3134 /*
3135 * We have to re-check the event->owner field, if it is cleared
3136 * we raced with perf_event_exit_task(), acquiring the mutex
3137 * ensured they're done, and we can proceed with freeing the
3138 * event.
3139 */
3140 if (event->owner)
3141 list_del_init(&event->owner_entry);
3142 mutex_unlock(&owner->perf_event_mutex);
3143 put_task_struct(owner);
3144 }
3145
Al Viroa6fa9412012-08-20 14:59:25 +01003146 perf_event_release_kernel(event);
3147}
3148
3149static int perf_release(struct inode *inode, struct file *file)
3150{
3151 put_event(file->private_data);
3152 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003153}
3154
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003155u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156{
3157 struct perf_event *child;
3158 u64 total = 0;
3159
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003160 *enabled = 0;
3161 *running = 0;
3162
Peter Zijlstra6f105812009-11-20 22:19:56 +01003163 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003164 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003165 *enabled += event->total_time_enabled +
3166 atomic64_read(&event->child_total_time_enabled);
3167 *running += event->total_time_running +
3168 atomic64_read(&event->child_total_time_running);
3169
3170 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003171 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003172 *enabled += child->total_time_enabled;
3173 *running += child->total_time_running;
3174 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003175 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003176
3177 return total;
3178}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003179EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003180
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003181static int perf_event_read_group(struct perf_event *event,
3182 u64 read_format, char __user *buf)
3183{
3184 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003185 int n = 0, size = 0, ret = -EFAULT;
3186 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003187 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003188 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003189
Peter Zijlstra6f105812009-11-20 22:19:56 +01003190 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003191 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192
3193 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003194 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3195 values[n++] = enabled;
3196 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3197 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003198 values[n++] = count;
3199 if (read_format & PERF_FORMAT_ID)
3200 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201
3202 size = n * sizeof(u64);
3203
3204 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003205 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206
Peter Zijlstra6f105812009-11-20 22:19:56 +01003207 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208
3209 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003210 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003212 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003213 if (read_format & PERF_FORMAT_ID)
3214 values[n++] = primary_event_id(sub);
3215
3216 size = n * sizeof(u64);
3217
Stephane Eranian184d3da2009-11-23 21:40:49 -08003218 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003219 ret = -EFAULT;
3220 goto unlock;
3221 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003222
3223 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003225unlock:
3226 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227
Peter Zijlstraabf48682009-11-20 22:19:49 +01003228 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003229}
3230
3231static int perf_event_read_one(struct perf_event *event,
3232 u64 read_format, char __user *buf)
3233{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003234 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003235 u64 values[4];
3236 int n = 0;
3237
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003238 values[n++] = perf_event_read_value(event, &enabled, &running);
3239 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3240 values[n++] = enabled;
3241 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3242 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003243 if (read_format & PERF_FORMAT_ID)
3244 values[n++] = primary_event_id(event);
3245
3246 if (copy_to_user(buf, values, n * sizeof(u64)))
3247 return -EFAULT;
3248
3249 return n * sizeof(u64);
3250}
3251
3252/*
3253 * Read the performance event - simple non blocking version for now
3254 */
3255static ssize_t
3256perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3257{
3258 u64 read_format = event->attr.read_format;
3259 int ret;
3260
3261 /*
3262 * Return end-of-file for a read on a event that is in
3263 * error state (i.e. because it was pinned but it couldn't be
3264 * scheduled on to the CPU at some point).
3265 */
3266 if (event->state == PERF_EVENT_STATE_ERROR)
3267 return 0;
3268
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003269 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003270 return -ENOSPC;
3271
3272 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273 if (read_format & PERF_FORMAT_GROUP)
3274 ret = perf_event_read_group(event, read_format, buf);
3275 else
3276 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277
3278 return ret;
3279}
3280
3281static ssize_t
3282perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3283{
3284 struct perf_event *event = file->private_data;
3285
3286 return perf_read_hw(event, buf, count);
3287}
3288
3289static unsigned int perf_poll(struct file *file, poll_table *wait)
3290{
3291 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003292 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 unsigned int events = POLL_HUP;
3294
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003295 /*
3296 * Race between perf_event_set_output() and perf_poll(): perf_poll()
3297 * grabs the rb reference but perf_event_set_output() overrides it.
3298 * Here is the timeline for two threads T1, T2:
3299 * t0: T1, rb = rcu_dereference(event->rb)
3300 * t1: T2, old_rb = event->rb
3301 * t2: T2, event->rb = new rb
3302 * t3: T2, ring_buffer_detach(old_rb)
3303 * t4: T1, ring_buffer_attach(rb1)
3304 * t5: T1, poll_wait(event->waitq)
3305 *
3306 * To avoid this problem, we grab mmap_mutex in perf_poll()
3307 * thereby ensuring that the assignment of the new ring buffer
3308 * and the detachment of the old buffer appear atomic to perf_poll()
3309 */
3310 mutex_lock(&event->mmap_mutex);
3311
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003312 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003313 rb = rcu_dereference(event->rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003314 if (rb) {
3315 ring_buffer_attach(event, rb);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003316 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003317 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318 rcu_read_unlock();
3319
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003320 mutex_unlock(&event->mmap_mutex);
3321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003322 poll_wait(file, &event->waitq, wait);
3323
3324 return events;
3325}
3326
3327static void perf_event_reset(struct perf_event *event)
3328{
3329 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003330 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331 perf_event_update_userpage(event);
3332}
3333
3334/*
3335 * Holding the top-level event's child_mutex means that any
3336 * descendant process that has inherited this event will block
3337 * in sync_child_event if it goes to exit, thus satisfying the
3338 * task existence requirements of perf_event_enable/disable.
3339 */
3340static void perf_event_for_each_child(struct perf_event *event,
3341 void (*func)(struct perf_event *))
3342{
3343 struct perf_event *child;
3344
3345 WARN_ON_ONCE(event->ctx->parent_ctx);
3346 mutex_lock(&event->child_mutex);
3347 func(event);
3348 list_for_each_entry(child, &event->child_list, child_list)
3349 func(child);
3350 mutex_unlock(&event->child_mutex);
3351}
3352
3353static void perf_event_for_each(struct perf_event *event,
3354 void (*func)(struct perf_event *))
3355{
3356 struct perf_event_context *ctx = event->ctx;
3357 struct perf_event *sibling;
3358
3359 WARN_ON_ONCE(ctx->parent_ctx);
3360 mutex_lock(&ctx->mutex);
3361 event = event->group_leader;
3362
3363 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003364 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003365 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366 mutex_unlock(&ctx->mutex);
3367}
3368
3369static int perf_event_period(struct perf_event *event, u64 __user *arg)
3370{
3371 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003372 int ret = 0;
3373 u64 value;
3374
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003375 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376 return -EINVAL;
3377
John Blackwoodad0cf342010-09-28 18:03:11 -04003378 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003379 return -EFAULT;
3380
3381 if (!value)
3382 return -EINVAL;
3383
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003384 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003385 if (event->attr.freq) {
3386 if (value > sysctl_perf_event_sample_rate) {
3387 ret = -EINVAL;
3388 goto unlock;
3389 }
3390
3391 event->attr.sample_freq = value;
3392 } else {
3393 event->attr.sample_period = value;
3394 event->hw.sample_period = value;
3395 }
3396unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003397 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398
3399 return ret;
3400}
3401
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003402static const struct file_operations perf_fops;
3403
Al Viro2903ff02012-08-28 12:52:22 -04003404static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003405{
Al Viro2903ff02012-08-28 12:52:22 -04003406 struct fd f = fdget(fd);
3407 if (!f.file)
3408 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003409
Al Viro2903ff02012-08-28 12:52:22 -04003410 if (f.file->f_op != &perf_fops) {
3411 fdput(f);
3412 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003413 }
Al Viro2903ff02012-08-28 12:52:22 -04003414 *p = f;
3415 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003416}
3417
3418static int perf_event_set_output(struct perf_event *event,
3419 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003420static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003421
3422static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3423{
3424 struct perf_event *event = file->private_data;
3425 void (*func)(struct perf_event *);
3426 u32 flags = arg;
3427
3428 switch (cmd) {
3429 case PERF_EVENT_IOC_ENABLE:
3430 func = perf_event_enable;
3431 break;
3432 case PERF_EVENT_IOC_DISABLE:
3433 func = perf_event_disable;
3434 break;
3435 case PERF_EVENT_IOC_RESET:
3436 func = perf_event_reset;
3437 break;
3438
3439 case PERF_EVENT_IOC_REFRESH:
3440 return perf_event_refresh(event, arg);
3441
3442 case PERF_EVENT_IOC_PERIOD:
3443 return perf_event_period(event, (u64 __user *)arg);
3444
3445 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003446 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003447 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003448 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003449 struct perf_event *output_event;
3450 struct fd output;
3451 ret = perf_fget_light(arg, &output);
3452 if (ret)
3453 return ret;
3454 output_event = output.file->private_data;
3455 ret = perf_event_set_output(event, output_event);
3456 fdput(output);
3457 } else {
3458 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003459 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003460 return ret;
3461 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003462
Li Zefan6fb29152009-10-15 11:21:42 +08003463 case PERF_EVENT_IOC_SET_FILTER:
3464 return perf_event_set_filter(event, (void __user *)arg);
3465
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003466 default:
3467 return -ENOTTY;
3468 }
3469
3470 if (flags & PERF_IOC_FLAG_GROUP)
3471 perf_event_for_each(event, func);
3472 else
3473 perf_event_for_each_child(event, func);
3474
3475 return 0;
3476}
3477
3478int perf_event_task_enable(void)
3479{
3480 struct perf_event *event;
3481
3482 mutex_lock(&current->perf_event_mutex);
3483 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3484 perf_event_for_each_child(event, perf_event_enable);
3485 mutex_unlock(&current->perf_event_mutex);
3486
3487 return 0;
3488}
3489
3490int perf_event_task_disable(void)
3491{
3492 struct perf_event *event;
3493
3494 mutex_lock(&current->perf_event_mutex);
3495 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3496 perf_event_for_each_child(event, perf_event_disable);
3497 mutex_unlock(&current->perf_event_mutex);
3498
3499 return 0;
3500}
3501
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003502static int perf_event_index(struct perf_event *event)
3503{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003504 if (event->hw.state & PERF_HES_STOPPED)
3505 return 0;
3506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003507 if (event->state != PERF_EVENT_STATE_ACTIVE)
3508 return 0;
3509
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003510 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003511}
3512
Eric B Munsonc4794292011-06-23 16:34:38 -04003513static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003514 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003515 u64 *enabled,
3516 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003517{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003518 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003519
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003520 *now = perf_clock();
3521 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003522 *enabled = ctx_time - event->tstamp_enabled;
3523 *running = ctx_time - event->tstamp_running;
3524}
3525
Peter Zijlstrac7206202012-03-22 17:26:36 +01003526void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003527{
3528}
3529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530/*
3531 * Callers need to ensure there can be no nesting of this function, otherwise
3532 * the seqlock logic goes bad. We can not serialize this because the arch
3533 * code calls this from NMI context.
3534 */
3535void perf_event_update_userpage(struct perf_event *event)
3536{
3537 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003538 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003539 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003540
3541 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003542 /*
3543 * compute total_time_enabled, total_time_running
3544 * based on snapshot values taken when the event
3545 * was last scheduled in.
3546 *
3547 * we cannot simply called update_context_time()
3548 * because of locking issue as we can be called in
3549 * NMI context
3550 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003551 calc_timer_values(event, &now, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003552 rb = rcu_dereference(event->rb);
3553 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003554 goto unlock;
3555
Frederic Weisbecker76369132011-05-19 19:55:04 +02003556 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003557
3558 /*
3559 * Disable preemption so as to not let the corresponding user-space
3560 * spin too long if we get preempted.
3561 */
3562 preempt_disable();
3563 ++userpg->lock;
3564 barrier();
3565 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003566 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003567 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003568 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003569
Eric B Munson0d641202011-06-24 12:26:26 -04003570 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003571 atomic64_read(&event->child_total_time_enabled);
3572
Eric B Munson0d641202011-06-24 12:26:26 -04003573 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003574 atomic64_read(&event->child_total_time_running);
3575
Peter Zijlstrac7206202012-03-22 17:26:36 +01003576 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003577
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003578 barrier();
3579 ++userpg->lock;
3580 preempt_enable();
3581unlock:
3582 rcu_read_unlock();
3583}
3584
Peter Zijlstra906010b2009-09-21 16:08:49 +02003585static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3586{
3587 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003588 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003589 int ret = VM_FAULT_SIGBUS;
3590
3591 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3592 if (vmf->pgoff == 0)
3593 ret = 0;
3594 return ret;
3595 }
3596
3597 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003598 rb = rcu_dereference(event->rb);
3599 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003600 goto unlock;
3601
3602 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3603 goto unlock;
3604
Frederic Weisbecker76369132011-05-19 19:55:04 +02003605 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003606 if (!vmf->page)
3607 goto unlock;
3608
3609 get_page(vmf->page);
3610 vmf->page->mapping = vma->vm_file->f_mapping;
3611 vmf->page->index = vmf->pgoff;
3612
3613 ret = 0;
3614unlock:
3615 rcu_read_unlock();
3616
3617 return ret;
3618}
3619
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003620static void ring_buffer_attach(struct perf_event *event,
3621 struct ring_buffer *rb)
3622{
3623 unsigned long flags;
3624
3625 if (!list_empty(&event->rb_entry))
3626 return;
3627
3628 spin_lock_irqsave(&rb->event_lock, flags);
3629 if (!list_empty(&event->rb_entry))
3630 goto unlock;
3631
3632 list_add(&event->rb_entry, &rb->event_list);
3633unlock:
3634 spin_unlock_irqrestore(&rb->event_lock, flags);
3635}
3636
3637static void ring_buffer_detach(struct perf_event *event,
3638 struct ring_buffer *rb)
3639{
3640 unsigned long flags;
3641
3642 if (list_empty(&event->rb_entry))
3643 return;
3644
3645 spin_lock_irqsave(&rb->event_lock, flags);
3646 list_del_init(&event->rb_entry);
3647 wake_up_all(&event->waitq);
3648 spin_unlock_irqrestore(&rb->event_lock, flags);
3649}
3650
3651static void ring_buffer_wakeup(struct perf_event *event)
3652{
3653 struct ring_buffer *rb;
3654
3655 rcu_read_lock();
3656 rb = rcu_dereference(event->rb);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003657 if (!rb)
3658 goto unlock;
3659
3660 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003661 wake_up_all(&event->waitq);
Will Deacon44b7f4b2011-12-13 20:40:45 +01003662
3663unlock:
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003664 rcu_read_unlock();
3665}
3666
Frederic Weisbecker76369132011-05-19 19:55:04 +02003667static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003668{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003669 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003670
Frederic Weisbecker76369132011-05-19 19:55:04 +02003671 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3672 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003673}
3674
Frederic Weisbecker76369132011-05-19 19:55:04 +02003675static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003676{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003677 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003678
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003679 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003680 rb = rcu_dereference(event->rb);
3681 if (rb) {
3682 if (!atomic_inc_not_zero(&rb->refcount))
3683 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003684 }
3685 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003686
Frederic Weisbecker76369132011-05-19 19:55:04 +02003687 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003688}
3689
Frederic Weisbecker76369132011-05-19 19:55:04 +02003690static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003691{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003692 struct perf_event *event, *n;
3693 unsigned long flags;
3694
Frederic Weisbecker76369132011-05-19 19:55:04 +02003695 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003696 return;
3697
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003698 spin_lock_irqsave(&rb->event_lock, flags);
3699 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) {
3700 list_del_init(&event->rb_entry);
3701 wake_up_all(&event->waitq);
3702 }
3703 spin_unlock_irqrestore(&rb->event_lock, flags);
3704
Frederic Weisbecker76369132011-05-19 19:55:04 +02003705 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706}
3707
3708static void perf_mmap_open(struct vm_area_struct *vma)
3709{
3710 struct perf_event *event = vma->vm_file->private_data;
3711
3712 atomic_inc(&event->mmap_count);
3713}
3714
3715static void perf_mmap_close(struct vm_area_struct *vma)
3716{
3717 struct perf_event *event = vma->vm_file->private_data;
3718
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003719 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02003720 unsigned long size = perf_data_size(event->rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003721 struct user_struct *user = event->mmap_user;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003722 struct ring_buffer *rb = event->rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003723
Peter Zijlstra906010b2009-09-21 16:08:49 +02003724 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003725 vma->vm_mm->pinned_vm -= event->mmap_locked;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003726 rcu_assign_pointer(event->rb, NULL);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003727 ring_buffer_detach(event, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003728 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003729
Frederic Weisbecker76369132011-05-19 19:55:04 +02003730 ring_buffer_put(rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003731 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732 }
3733}
3734
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003735static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003736 .open = perf_mmap_open,
3737 .close = perf_mmap_close,
3738 .fault = perf_mmap_fault,
3739 .page_mkwrite = perf_mmap_fault,
3740};
3741
3742static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3743{
3744 struct perf_event *event = file->private_data;
3745 unsigned long user_locked, user_lock_limit;
3746 struct user_struct *user = current_user();
3747 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003748 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003749 unsigned long vma_size;
3750 unsigned long nr_pages;
3751 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003752 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003753
Peter Zijlstrac7920612010-05-18 10:33:24 +02003754 /*
3755 * Don't allow mmap() of inherited per-task counters. This would
3756 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003757 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003758 */
3759 if (event->cpu == -1 && event->attr.inherit)
3760 return -EINVAL;
3761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762 if (!(vma->vm_flags & VM_SHARED))
3763 return -EINVAL;
3764
3765 vma_size = vma->vm_end - vma->vm_start;
3766 nr_pages = (vma_size / PAGE_SIZE) - 1;
3767
3768 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003769 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003770 * can do bitmasks instead of modulo.
3771 */
3772 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3773 return -EINVAL;
3774
3775 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3776 return -EINVAL;
3777
3778 if (vma->vm_pgoff != 0)
3779 return -EINVAL;
3780
3781 WARN_ON_ONCE(event->ctx->parent_ctx);
3782 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003783 if (event->rb) {
3784 if (event->rb->nr_pages == nr_pages)
3785 atomic_inc(&event->rb->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003786 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003787 ret = -EINVAL;
3788 goto unlock;
3789 }
3790
3791 user_extra = nr_pages + 1;
3792 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3793
3794 /*
3795 * Increase the limit linearly with more CPUs:
3796 */
3797 user_lock_limit *= num_online_cpus();
3798
3799 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3800
3801 extra = 0;
3802 if (user_locked > user_lock_limit)
3803 extra = user_locked - user_lock_limit;
3804
Jiri Slaby78d7d402010-03-05 13:42:54 -08003805 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003806 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003807 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003808
3809 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3810 !capable(CAP_IPC_LOCK)) {
3811 ret = -EPERM;
3812 goto unlock;
3813 }
3814
Frederic Weisbecker76369132011-05-19 19:55:04 +02003815 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003816
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003817 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003818 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003819
Vince Weaver4ec83632011-06-01 15:15:36 -04003820 rb = rb_alloc(nr_pages,
3821 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3822 event->cpu, flags);
3823
Frederic Weisbecker76369132011-05-19 19:55:04 +02003824 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003825 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003826 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003827 }
Frederic Weisbecker76369132011-05-19 19:55:04 +02003828 rcu_assign_pointer(event->rb, rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003829
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003830 atomic_long_add(user_extra, &user->locked_vm);
3831 event->mmap_locked = extra;
3832 event->mmap_user = get_current_user();
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003833 vma->vm_mm->pinned_vm += event->mmap_locked;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003834
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01003835 perf_event_update_userpage(event);
3836
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003837unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003838 if (!ret)
3839 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003840 mutex_unlock(&event->mmap_mutex);
3841
Konstantin Khlebnikov314e51b2012-10-08 16:29:02 -07003842 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003843 vma->vm_ops = &perf_mmap_vmops;
3844
3845 return ret;
3846}
3847
3848static int perf_fasync(int fd, struct file *filp, int on)
3849{
Al Viro496ad9a2013-01-23 17:07:38 -05003850 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003851 struct perf_event *event = filp->private_data;
3852 int retval;
3853
3854 mutex_lock(&inode->i_mutex);
3855 retval = fasync_helper(fd, filp, on, &event->fasync);
3856 mutex_unlock(&inode->i_mutex);
3857
3858 if (retval < 0)
3859 return retval;
3860
3861 return 0;
3862}
3863
3864static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003865 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003866 .release = perf_release,
3867 .read = perf_read,
3868 .poll = perf_poll,
3869 .unlocked_ioctl = perf_ioctl,
3870 .compat_ioctl = perf_ioctl,
3871 .mmap = perf_mmap,
3872 .fasync = perf_fasync,
3873};
3874
3875/*
3876 * Perf event wakeup
3877 *
3878 * If there's data, ensure we set the poll() state and publish everything
3879 * to user-space before waking everybody up.
3880 */
3881
3882void perf_event_wakeup(struct perf_event *event)
3883{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003884 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885
3886 if (event->pending_kill) {
3887 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3888 event->pending_kill = 0;
3889 }
3890}
3891
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003892static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003893{
3894 struct perf_event *event = container_of(entry,
3895 struct perf_event, pending);
3896
3897 if (event->pending_disable) {
3898 event->pending_disable = 0;
3899 __perf_event_disable(event);
3900 }
3901
3902 if (event->pending_wakeup) {
3903 event->pending_wakeup = 0;
3904 perf_event_wakeup(event);
3905 }
3906}
3907
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003908/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003909 * We assume there is only KVM supporting the callbacks.
3910 * Later on, we might change it to a list if there is
3911 * another virtualization implementation supporting the callbacks.
3912 */
3913struct perf_guest_info_callbacks *perf_guest_cbs;
3914
3915int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3916{
3917 perf_guest_cbs = cbs;
3918 return 0;
3919}
3920EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3921
3922int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3923{
3924 perf_guest_cbs = NULL;
3925 return 0;
3926}
3927EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3928
Jiri Olsa40189942012-08-07 15:20:37 +02003929static void
3930perf_output_sample_regs(struct perf_output_handle *handle,
3931 struct pt_regs *regs, u64 mask)
3932{
3933 int bit;
3934
3935 for_each_set_bit(bit, (const unsigned long *) &mask,
3936 sizeof(mask) * BITS_PER_BYTE) {
3937 u64 val;
3938
3939 val = perf_reg_value(regs, bit);
3940 perf_output_put(handle, val);
3941 }
3942}
3943
3944static void perf_sample_regs_user(struct perf_regs_user *regs_user,
3945 struct pt_regs *regs)
3946{
3947 if (!user_mode(regs)) {
3948 if (current->mm)
3949 regs = task_pt_regs(current);
3950 else
3951 regs = NULL;
3952 }
3953
3954 if (regs) {
3955 regs_user->regs = regs;
3956 regs_user->abi = perf_reg_abi(current);
3957 }
3958}
3959
Jiri Olsac5ebced2012-08-07 15:20:40 +02003960/*
3961 * Get remaining task size from user stack pointer.
3962 *
3963 * It'd be better to take stack vma map and limit this more
3964 * precisly, but there's no way to get it safely under interrupt,
3965 * so using TASK_SIZE as limit.
3966 */
3967static u64 perf_ustack_task_size(struct pt_regs *regs)
3968{
3969 unsigned long addr = perf_user_stack_pointer(regs);
3970
3971 if (!addr || addr >= TASK_SIZE)
3972 return 0;
3973
3974 return TASK_SIZE - addr;
3975}
3976
3977static u16
3978perf_sample_ustack_size(u16 stack_size, u16 header_size,
3979 struct pt_regs *regs)
3980{
3981 u64 task_size;
3982
3983 /* No regs, no stack pointer, no dump. */
3984 if (!regs)
3985 return 0;
3986
3987 /*
3988 * Check if we fit in with the requested stack size into the:
3989 * - TASK_SIZE
3990 * If we don't, we limit the size to the TASK_SIZE.
3991 *
3992 * - remaining sample size
3993 * If we don't, we customize the stack size to
3994 * fit in to the remaining sample size.
3995 */
3996
3997 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
3998 stack_size = min(stack_size, (u16) task_size);
3999
4000 /* Current header size plus static size and dynamic size. */
4001 header_size += 2 * sizeof(u64);
4002
4003 /* Do we fit in with the current stack dump size? */
4004 if ((u16) (header_size + stack_size) < header_size) {
4005 /*
4006 * If we overflow the maximum size for the sample,
4007 * we customize the stack dump size to fit in.
4008 */
4009 stack_size = USHRT_MAX - header_size - sizeof(u64);
4010 stack_size = round_up(stack_size, sizeof(u64));
4011 }
4012
4013 return stack_size;
4014}
4015
4016static void
4017perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4018 struct pt_regs *regs)
4019{
4020 /* Case of a kernel thread, nothing to dump */
4021 if (!regs) {
4022 u64 size = 0;
4023 perf_output_put(handle, size);
4024 } else {
4025 unsigned long sp;
4026 unsigned int rem;
4027 u64 dyn_size;
4028
4029 /*
4030 * We dump:
4031 * static size
4032 * - the size requested by user or the best one we can fit
4033 * in to the sample max size
4034 * data
4035 * - user stack dump data
4036 * dynamic size
4037 * - the actual dumped size
4038 */
4039
4040 /* Static size. */
4041 perf_output_put(handle, dump_size);
4042
4043 /* Data. */
4044 sp = perf_user_stack_pointer(regs);
4045 rem = __output_copy_user(handle, (void *) sp, dump_size);
4046 dyn_size = dump_size - rem;
4047
4048 perf_output_skip(handle, rem);
4049
4050 /* Dynamic size. */
4051 perf_output_put(handle, dyn_size);
4052 }
4053}
4054
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004055static void __perf_event_header__init_id(struct perf_event_header *header,
4056 struct perf_sample_data *data,
4057 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004058{
4059 u64 sample_type = event->attr.sample_type;
4060
4061 data->type = sample_type;
4062 header->size += event->id_header_size;
4063
4064 if (sample_type & PERF_SAMPLE_TID) {
4065 /* namespace issues */
4066 data->tid_entry.pid = perf_event_pid(event, current);
4067 data->tid_entry.tid = perf_event_tid(event, current);
4068 }
4069
4070 if (sample_type & PERF_SAMPLE_TIME)
4071 data->time = perf_clock();
4072
4073 if (sample_type & PERF_SAMPLE_ID)
4074 data->id = primary_event_id(event);
4075
4076 if (sample_type & PERF_SAMPLE_STREAM_ID)
4077 data->stream_id = event->id;
4078
4079 if (sample_type & PERF_SAMPLE_CPU) {
4080 data->cpu_entry.cpu = raw_smp_processor_id();
4081 data->cpu_entry.reserved = 0;
4082 }
4083}
4084
Frederic Weisbecker76369132011-05-19 19:55:04 +02004085void perf_event_header__init_id(struct perf_event_header *header,
4086 struct perf_sample_data *data,
4087 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004088{
4089 if (event->attr.sample_id_all)
4090 __perf_event_header__init_id(header, data, event);
4091}
4092
4093static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4094 struct perf_sample_data *data)
4095{
4096 u64 sample_type = data->type;
4097
4098 if (sample_type & PERF_SAMPLE_TID)
4099 perf_output_put(handle, data->tid_entry);
4100
4101 if (sample_type & PERF_SAMPLE_TIME)
4102 perf_output_put(handle, data->time);
4103
4104 if (sample_type & PERF_SAMPLE_ID)
4105 perf_output_put(handle, data->id);
4106
4107 if (sample_type & PERF_SAMPLE_STREAM_ID)
4108 perf_output_put(handle, data->stream_id);
4109
4110 if (sample_type & PERF_SAMPLE_CPU)
4111 perf_output_put(handle, data->cpu_entry);
4112}
4113
Frederic Weisbecker76369132011-05-19 19:55:04 +02004114void perf_event__output_id_sample(struct perf_event *event,
4115 struct perf_output_handle *handle,
4116 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004117{
4118 if (event->attr.sample_id_all)
4119 __perf_event__output_id_sample(handle, sample);
4120}
4121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004122static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004123 struct perf_event *event,
4124 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125{
4126 u64 read_format = event->attr.read_format;
4127 u64 values[4];
4128 int n = 0;
4129
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004130 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004131 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004132 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133 atomic64_read(&event->child_total_time_enabled);
4134 }
4135 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004136 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004137 atomic64_read(&event->child_total_time_running);
4138 }
4139 if (read_format & PERF_FORMAT_ID)
4140 values[n++] = primary_event_id(event);
4141
Frederic Weisbecker76369132011-05-19 19:55:04 +02004142 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004143}
4144
4145/*
4146 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4147 */
4148static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004149 struct perf_event *event,
4150 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004151{
4152 struct perf_event *leader = event->group_leader, *sub;
4153 u64 read_format = event->attr.read_format;
4154 u64 values[5];
4155 int n = 0;
4156
4157 values[n++] = 1 + leader->nr_siblings;
4158
4159 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004160 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004161
4162 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004163 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004164
4165 if (leader != event)
4166 leader->pmu->read(leader);
4167
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004168 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004169 if (read_format & PERF_FORMAT_ID)
4170 values[n++] = primary_event_id(leader);
4171
Frederic Weisbecker76369132011-05-19 19:55:04 +02004172 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004173
4174 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4175 n = 0;
4176
4177 if (sub != event)
4178 sub->pmu->read(sub);
4179
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004180 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004181 if (read_format & PERF_FORMAT_ID)
4182 values[n++] = primary_event_id(sub);
4183
Frederic Weisbecker76369132011-05-19 19:55:04 +02004184 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185 }
4186}
4187
Stephane Eranianeed01522010-10-26 16:08:01 +02004188#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4189 PERF_FORMAT_TOTAL_TIME_RUNNING)
4190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004191static void perf_output_read(struct perf_output_handle *handle,
4192 struct perf_event *event)
4193{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004194 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004195 u64 read_format = event->attr.read_format;
4196
4197 /*
4198 * compute total_time_enabled, total_time_running
4199 * based on snapshot values taken when the event
4200 * was last scheduled in.
4201 *
4202 * we cannot simply called update_context_time()
4203 * because of locking issue as we are called in
4204 * NMI context
4205 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004206 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004207 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004208
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004209 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004210 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004211 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004212 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004213}
4214
4215void perf_output_sample(struct perf_output_handle *handle,
4216 struct perf_event_header *header,
4217 struct perf_sample_data *data,
4218 struct perf_event *event)
4219{
4220 u64 sample_type = data->type;
4221
4222 perf_output_put(handle, *header);
4223
4224 if (sample_type & PERF_SAMPLE_IP)
4225 perf_output_put(handle, data->ip);
4226
4227 if (sample_type & PERF_SAMPLE_TID)
4228 perf_output_put(handle, data->tid_entry);
4229
4230 if (sample_type & PERF_SAMPLE_TIME)
4231 perf_output_put(handle, data->time);
4232
4233 if (sample_type & PERF_SAMPLE_ADDR)
4234 perf_output_put(handle, data->addr);
4235
4236 if (sample_type & PERF_SAMPLE_ID)
4237 perf_output_put(handle, data->id);
4238
4239 if (sample_type & PERF_SAMPLE_STREAM_ID)
4240 perf_output_put(handle, data->stream_id);
4241
4242 if (sample_type & PERF_SAMPLE_CPU)
4243 perf_output_put(handle, data->cpu_entry);
4244
4245 if (sample_type & PERF_SAMPLE_PERIOD)
4246 perf_output_put(handle, data->period);
4247
4248 if (sample_type & PERF_SAMPLE_READ)
4249 perf_output_read(handle, event);
4250
4251 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4252 if (data->callchain) {
4253 int size = 1;
4254
4255 if (data->callchain)
4256 size += data->callchain->nr;
4257
4258 size *= sizeof(u64);
4259
Frederic Weisbecker76369132011-05-19 19:55:04 +02004260 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004261 } else {
4262 u64 nr = 0;
4263 perf_output_put(handle, nr);
4264 }
4265 }
4266
4267 if (sample_type & PERF_SAMPLE_RAW) {
4268 if (data->raw) {
4269 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004270 __output_copy(handle, data->raw->data,
4271 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004272 } else {
4273 struct {
4274 u32 size;
4275 u32 data;
4276 } raw = {
4277 .size = sizeof(u32),
4278 .data = 0,
4279 };
4280 perf_output_put(handle, raw);
4281 }
4282 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004283
4284 if (!event->attr.watermark) {
4285 int wakeup_events = event->attr.wakeup_events;
4286
4287 if (wakeup_events) {
4288 struct ring_buffer *rb = handle->rb;
4289 int events = local_inc_return(&rb->events);
4290
4291 if (events >= wakeup_events) {
4292 local_sub(wakeup_events, &rb->events);
4293 local_inc(&rb->wakeup);
4294 }
4295 }
4296 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004297
4298 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4299 if (data->br_stack) {
4300 size_t size;
4301
4302 size = data->br_stack->nr
4303 * sizeof(struct perf_branch_entry);
4304
4305 perf_output_put(handle, data->br_stack->nr);
4306 perf_output_copy(handle, data->br_stack->entries, size);
4307 } else {
4308 /*
4309 * we always store at least the value of nr
4310 */
4311 u64 nr = 0;
4312 perf_output_put(handle, nr);
4313 }
4314 }
Jiri Olsa40189942012-08-07 15:20:37 +02004315
4316 if (sample_type & PERF_SAMPLE_REGS_USER) {
4317 u64 abi = data->regs_user.abi;
4318
4319 /*
4320 * If there are no regs to dump, notice it through
4321 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4322 */
4323 perf_output_put(handle, abi);
4324
4325 if (abi) {
4326 u64 mask = event->attr.sample_regs_user;
4327 perf_output_sample_regs(handle,
4328 data->regs_user.regs,
4329 mask);
4330 }
4331 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004332
4333 if (sample_type & PERF_SAMPLE_STACK_USER)
4334 perf_output_sample_ustack(handle,
4335 data->stack_user_size,
4336 data->regs_user.regs);
Andi Kleenc3feedf2013-01-24 16:10:28 +01004337
4338 if (sample_type & PERF_SAMPLE_WEIGHT)
4339 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004340
4341 if (sample_type & PERF_SAMPLE_DATA_SRC)
4342 perf_output_put(handle, data->data_src.val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343}
4344
4345void perf_prepare_sample(struct perf_event_header *header,
4346 struct perf_sample_data *data,
4347 struct perf_event *event,
4348 struct pt_regs *regs)
4349{
4350 u64 sample_type = event->attr.sample_type;
4351
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004352 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004353 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354
4355 header->misc = 0;
4356 header->misc |= perf_misc_flags(regs);
4357
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004358 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004359
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004360 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004361 data->ip = perf_instruction_pointer(regs);
4362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4364 int size = 1;
4365
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004366 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004367
4368 if (data->callchain)
4369 size += data->callchain->nr;
4370
4371 header->size += size * sizeof(u64);
4372 }
4373
4374 if (sample_type & PERF_SAMPLE_RAW) {
4375 int size = sizeof(u32);
4376
4377 if (data->raw)
4378 size += data->raw->size;
4379 else
4380 size += sizeof(u32);
4381
4382 WARN_ON_ONCE(size & (sizeof(u64)-1));
4383 header->size += size;
4384 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004385
4386 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4387 int size = sizeof(u64); /* nr */
4388 if (data->br_stack) {
4389 size += data->br_stack->nr
4390 * sizeof(struct perf_branch_entry);
4391 }
4392 header->size += size;
4393 }
Jiri Olsa40189942012-08-07 15:20:37 +02004394
4395 if (sample_type & PERF_SAMPLE_REGS_USER) {
4396 /* regs dump ABI info */
4397 int size = sizeof(u64);
4398
4399 perf_sample_regs_user(&data->regs_user, regs);
4400
4401 if (data->regs_user.regs) {
4402 u64 mask = event->attr.sample_regs_user;
4403 size += hweight64(mask) * sizeof(u64);
4404 }
4405
4406 header->size += size;
4407 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004408
4409 if (sample_type & PERF_SAMPLE_STACK_USER) {
4410 /*
4411 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4412 * processed as the last one or have additional check added
4413 * in case new sample type is added, because we could eat
4414 * up the rest of the sample size.
4415 */
4416 struct perf_regs_user *uregs = &data->regs_user;
4417 u16 stack_size = event->attr.sample_stack_user;
4418 u16 size = sizeof(u64);
4419
4420 if (!uregs->abi)
4421 perf_sample_regs_user(uregs, regs);
4422
4423 stack_size = perf_sample_ustack_size(stack_size, header->size,
4424 uregs->regs);
4425
4426 /*
4427 * If there is something to dump, add space for the dump
4428 * itself and for the field that tells the dynamic size,
4429 * which is how many have been actually dumped.
4430 */
4431 if (stack_size)
4432 size += sizeof(u64) + stack_size;
4433
4434 data->stack_user_size = stack_size;
4435 header->size += size;
4436 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437}
4438
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004439static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440 struct perf_sample_data *data,
4441 struct pt_regs *regs)
4442{
4443 struct perf_output_handle handle;
4444 struct perf_event_header header;
4445
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004446 /* protect the callchain buffers */
4447 rcu_read_lock();
4448
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004449 perf_prepare_sample(&header, data, event, regs);
4450
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004451 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004452 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004453
4454 perf_output_sample(&handle, &header, data, event);
4455
4456 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004457
4458exit:
4459 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004460}
4461
4462/*
4463 * read event_id
4464 */
4465
4466struct perf_read_event {
4467 struct perf_event_header header;
4468
4469 u32 pid;
4470 u32 tid;
4471};
4472
4473static void
4474perf_event_read_event(struct perf_event *event,
4475 struct task_struct *task)
4476{
4477 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004478 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004479 struct perf_read_event read_event = {
4480 .header = {
4481 .type = PERF_RECORD_READ,
4482 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004483 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004484 },
4485 .pid = perf_event_pid(event, task),
4486 .tid = perf_event_tid(event, task),
4487 };
4488 int ret;
4489
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004490 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004491 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004492 if (ret)
4493 return;
4494
4495 perf_output_put(&handle, read_event);
4496 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004497 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004498
4499 perf_output_end(&handle);
4500}
4501
Jiri Olsa52d857a2013-05-06 18:27:18 +02004502typedef int (perf_event_aux_match_cb)(struct perf_event *event, void *data);
4503typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4504
4505static void
4506perf_event_aux_ctx(struct perf_event_context *ctx,
4507 perf_event_aux_match_cb match,
4508 perf_event_aux_output_cb output,
4509 void *data)
4510{
4511 struct perf_event *event;
4512
4513 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4514 if (event->state < PERF_EVENT_STATE_INACTIVE)
4515 continue;
4516 if (!event_filter_match(event))
4517 continue;
4518 if (match(event, data))
4519 output(event, data);
4520 }
4521}
4522
4523static void
4524perf_event_aux(perf_event_aux_match_cb match,
4525 perf_event_aux_output_cb output,
4526 void *data,
4527 struct perf_event_context *task_ctx)
4528{
4529 struct perf_cpu_context *cpuctx;
4530 struct perf_event_context *ctx;
4531 struct pmu *pmu;
4532 int ctxn;
4533
4534 rcu_read_lock();
4535 list_for_each_entry_rcu(pmu, &pmus, entry) {
4536 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4537 if (cpuctx->unique_pmu != pmu)
4538 goto next;
4539 perf_event_aux_ctx(&cpuctx->ctx, match, output, data);
4540 if (task_ctx)
4541 goto next;
4542 ctxn = pmu->task_ctx_nr;
4543 if (ctxn < 0)
4544 goto next;
4545 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4546 if (ctx)
4547 perf_event_aux_ctx(ctx, match, output, data);
4548next:
4549 put_cpu_ptr(pmu->pmu_cpu_context);
4550 }
4551
4552 if (task_ctx) {
4553 preempt_disable();
4554 perf_event_aux_ctx(task_ctx, match, output, data);
4555 preempt_enable();
4556 }
4557 rcu_read_unlock();
4558}
4559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004560/*
4561 * task tracking -- fork/exit
4562 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004563 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564 */
4565
4566struct perf_task_event {
4567 struct task_struct *task;
4568 struct perf_event_context *task_ctx;
4569
4570 struct {
4571 struct perf_event_header header;
4572
4573 u32 pid;
4574 u32 ppid;
4575 u32 tid;
4576 u32 ptid;
4577 u64 time;
4578 } event_id;
4579};
4580
4581static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004582 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004583{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004584 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004585 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004586 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004587 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004588 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004589
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004590 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004591
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004592 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004593 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004594 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004595 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004596
4597 task_event->event_id.pid = perf_event_pid(event, task);
4598 task_event->event_id.ppid = perf_event_pid(event, current);
4599
4600 task_event->event_id.tid = perf_event_tid(event, task);
4601 task_event->event_id.ptid = perf_event_tid(event, current);
4602
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004603 perf_output_put(&handle, task_event->event_id);
4604
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004605 perf_event__output_id_sample(event, &handle, &sample);
4606
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004607 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004608out:
4609 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004610}
4611
Jiri Olsa52d857a2013-05-06 18:27:18 +02004612static int perf_event_task_match(struct perf_event *event,
4613 void *data __maybe_unused)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004614{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004615 return event->attr.comm || event->attr.mmap ||
4616 event->attr.mmap_data || event->attr.task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004617}
4618
4619static void perf_event_task(struct task_struct *task,
4620 struct perf_event_context *task_ctx,
4621 int new)
4622{
4623 struct perf_task_event task_event;
4624
4625 if (!atomic_read(&nr_comm_events) &&
4626 !atomic_read(&nr_mmap_events) &&
4627 !atomic_read(&nr_task_events))
4628 return;
4629
4630 task_event = (struct perf_task_event){
4631 .task = task,
4632 .task_ctx = task_ctx,
4633 .event_id = {
4634 .header = {
4635 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4636 .misc = 0,
4637 .size = sizeof(task_event.event_id),
4638 },
4639 /* .pid */
4640 /* .ppid */
4641 /* .tid */
4642 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004643 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004644 },
4645 };
4646
Jiri Olsa52d857a2013-05-06 18:27:18 +02004647 perf_event_aux(perf_event_task_match,
4648 perf_event_task_output,
4649 &task_event,
4650 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651}
4652
4653void perf_event_fork(struct task_struct *task)
4654{
4655 perf_event_task(task, NULL, 1);
4656}
4657
4658/*
4659 * comm tracking
4660 */
4661
4662struct perf_comm_event {
4663 struct task_struct *task;
4664 char *comm;
4665 int comm_size;
4666
4667 struct {
4668 struct perf_event_header header;
4669
4670 u32 pid;
4671 u32 tid;
4672 } event_id;
4673};
4674
4675static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004676 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004677{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004678 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004679 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004680 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004681 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004682 int ret;
4683
4684 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4685 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004686 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004687
4688 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004689 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690
4691 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4692 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4693
4694 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004695 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004696 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004697
4698 perf_event__output_id_sample(event, &handle, &sample);
4699
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004700 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004701out:
4702 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004703}
4704
Jiri Olsa52d857a2013-05-06 18:27:18 +02004705static int perf_event_comm_match(struct perf_event *event,
4706 void *data __maybe_unused)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004707{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004708 return event->attr.comm;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004709}
4710
4711static void perf_event_comm_event(struct perf_comm_event *comm_event)
4712{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004713 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004714 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004715
4716 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004717 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004718 size = ALIGN(strlen(comm)+1, sizeof(u64));
4719
4720 comm_event->comm = comm;
4721 comm_event->comm_size = size;
4722
4723 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004724
Jiri Olsa52d857a2013-05-06 18:27:18 +02004725 perf_event_aux(perf_event_comm_match,
4726 perf_event_comm_output,
4727 comm_event,
4728 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004729}
4730
4731void perf_event_comm(struct task_struct *task)
4732{
4733 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004734 struct perf_event_context *ctx;
4735 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004736
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07004737 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004738 for_each_task_context_nr(ctxn) {
4739 ctx = task->perf_event_ctxp[ctxn];
4740 if (!ctx)
4741 continue;
4742
4743 perf_event_enable_on_exec(ctx);
4744 }
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07004745 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004746
4747 if (!atomic_read(&nr_comm_events))
4748 return;
4749
4750 comm_event = (struct perf_comm_event){
4751 .task = task,
4752 /* .comm */
4753 /* .comm_size */
4754 .event_id = {
4755 .header = {
4756 .type = PERF_RECORD_COMM,
4757 .misc = 0,
4758 /* .size */
4759 },
4760 /* .pid */
4761 /* .tid */
4762 },
4763 };
4764
4765 perf_event_comm_event(&comm_event);
4766}
4767
4768/*
4769 * mmap tracking
4770 */
4771
4772struct perf_mmap_event {
4773 struct vm_area_struct *vma;
4774
4775 const char *file_name;
4776 int file_size;
4777
4778 struct {
4779 struct perf_event_header header;
4780
4781 u32 pid;
4782 u32 tid;
4783 u64 start;
4784 u64 len;
4785 u64 pgoff;
4786 } event_id;
4787};
4788
4789static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004790 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004791{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004792 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004793 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004794 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004795 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004796 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004797
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004798 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4799 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004800 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004801 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004802 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004803
4804 mmap_event->event_id.pid = perf_event_pid(event, current);
4805 mmap_event->event_id.tid = perf_event_tid(event, current);
4806
4807 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004808 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004809 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004810
4811 perf_event__output_id_sample(event, &handle, &sample);
4812
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004813 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004814out:
4815 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004816}
4817
4818static int perf_event_mmap_match(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004819 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004820{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004821 struct perf_mmap_event *mmap_event = data;
4822 struct vm_area_struct *vma = mmap_event->vma;
4823 int executable = vma->vm_flags & VM_EXEC;
Peter Zijlstra22e19082010-01-18 09:12:32 +01004824
Jiri Olsa52d857a2013-05-06 18:27:18 +02004825 return (!executable && event->attr.mmap_data) ||
4826 (executable && event->attr.mmap);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004827}
4828
4829static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4830{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004831 struct vm_area_struct *vma = mmap_event->vma;
4832 struct file *file = vma->vm_file;
4833 unsigned int size;
4834 char tmp[16];
4835 char *buf = NULL;
4836 const char *name;
4837
4838 memset(tmp, 0, sizeof(tmp));
4839
4840 if (file) {
4841 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004842 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843 * need to add enough zero bytes after the string to handle
4844 * the 64bit alignment we do later.
4845 */
4846 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4847 if (!buf) {
4848 name = strncpy(tmp, "//enomem", sizeof(tmp));
4849 goto got_name;
4850 }
4851 name = d_path(&file->f_path, buf, PATH_MAX);
4852 if (IS_ERR(name)) {
4853 name = strncpy(tmp, "//toolong", sizeof(tmp));
4854 goto got_name;
4855 }
4856 } else {
4857 if (arch_vma_name(mmap_event->vma)) {
4858 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
Chen Gangc97847d2013-04-08 11:48:27 +08004859 sizeof(tmp) - 1);
4860 tmp[sizeof(tmp) - 1] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004861 goto got_name;
4862 }
4863
4864 if (!vma->vm_mm) {
4865 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4866 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004867 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4868 vma->vm_end >= vma->vm_mm->brk) {
4869 name = strncpy(tmp, "[heap]", sizeof(tmp));
4870 goto got_name;
4871 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4872 vma->vm_end >= vma->vm_mm->start_stack) {
4873 name = strncpy(tmp, "[stack]", sizeof(tmp));
4874 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004875 }
4876
4877 name = strncpy(tmp, "//anon", sizeof(tmp));
4878 goto got_name;
4879 }
4880
4881got_name:
4882 size = ALIGN(strlen(name)+1, sizeof(u64));
4883
4884 mmap_event->file_name = name;
4885 mmap_event->file_size = size;
4886
Stephane Eranian2fe85422013-01-24 16:10:39 +01004887 if (!(vma->vm_flags & VM_EXEC))
4888 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
4889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004890 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4891
Jiri Olsa52d857a2013-05-06 18:27:18 +02004892 perf_event_aux(perf_event_mmap_match,
4893 perf_event_mmap_output,
4894 mmap_event,
4895 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004896
4897 kfree(buf);
4898}
4899
Eric B Munson3af9e852010-05-18 15:30:49 +01004900void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901{
4902 struct perf_mmap_event mmap_event;
4903
4904 if (!atomic_read(&nr_mmap_events))
4905 return;
4906
4907 mmap_event = (struct perf_mmap_event){
4908 .vma = vma,
4909 /* .file_name */
4910 /* .file_size */
4911 .event_id = {
4912 .header = {
4913 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004914 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004915 /* .size */
4916 },
4917 /* .pid */
4918 /* .tid */
4919 .start = vma->vm_start,
4920 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004921 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004922 },
4923 };
4924
4925 perf_event_mmap_event(&mmap_event);
4926}
4927
4928/*
4929 * IRQ throttle logging
4930 */
4931
4932static void perf_log_throttle(struct perf_event *event, int enable)
4933{
4934 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004935 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004936 int ret;
4937
4938 struct {
4939 struct perf_event_header header;
4940 u64 time;
4941 u64 id;
4942 u64 stream_id;
4943 } throttle_event = {
4944 .header = {
4945 .type = PERF_RECORD_THROTTLE,
4946 .misc = 0,
4947 .size = sizeof(throttle_event),
4948 },
4949 .time = perf_clock(),
4950 .id = primary_event_id(event),
4951 .stream_id = event->id,
4952 };
4953
4954 if (enable)
4955 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4956
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004957 perf_event_header__init_id(&throttle_event.header, &sample, event);
4958
4959 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004960 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004961 if (ret)
4962 return;
4963
4964 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004965 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004966 perf_output_end(&handle);
4967}
4968
4969/*
4970 * Generic event overflow handling, sampling.
4971 */
4972
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004973static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004974 int throttle, struct perf_sample_data *data,
4975 struct pt_regs *regs)
4976{
4977 int events = atomic_read(&event->event_limit);
4978 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004979 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004980 int ret = 0;
4981
Peter Zijlstra96398822010-11-24 18:55:29 +01004982 /*
4983 * Non-sampling counters might still use the PMI to fold short
4984 * hardware counters, ignore those.
4985 */
4986 if (unlikely(!is_sampling_event(event)))
4987 return 0;
4988
Stephane Eraniane050e3f2012-01-26 17:03:19 +01004989 seq = __this_cpu_read(perf_throttled_seq);
4990 if (seq != hwc->interrupts_seq) {
4991 hwc->interrupts_seq = seq;
4992 hwc->interrupts = 1;
4993 } else {
4994 hwc->interrupts++;
4995 if (unlikely(throttle
4996 && hwc->interrupts >= max_samples_per_tick)) {
4997 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01004998 hwc->interrupts = MAX_INTERRUPTS;
4999 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005000 ret = 1;
5001 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005002 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003
5004 if (event->attr.freq) {
5005 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005006 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007
Peter Zijlstraabd50712010-01-26 18:50:16 +01005008 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009
Peter Zijlstraabd50712010-01-26 18:50:16 +01005010 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005011 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012 }
5013
5014 /*
5015 * XXX event_limit might not quite work as expected on inherited
5016 * events
5017 */
5018
5019 event->pending_kill = POLL_IN;
5020 if (events && atomic_dec_and_test(&event->event_limit)) {
5021 ret = 1;
5022 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005023 event->pending_disable = 1;
5024 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025 }
5026
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005027 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005028 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005029 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005030 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005031
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005032 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005033 event->pending_wakeup = 1;
5034 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005035 }
5036
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005037 return ret;
5038}
5039
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005040int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005041 struct perf_sample_data *data,
5042 struct pt_regs *regs)
5043{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005044 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005045}
5046
5047/*
5048 * Generic software event infrastructure
5049 */
5050
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005051struct swevent_htable {
5052 struct swevent_hlist *swevent_hlist;
5053 struct mutex hlist_mutex;
5054 int hlist_refcount;
5055
5056 /* Recursion avoidance in each contexts */
5057 int recursion[PERF_NR_CONTEXTS];
5058};
5059
5060static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5061
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005062/*
5063 * We directly increment event->count and keep a second value in
5064 * event->hw.period_left to count intervals. This period event
5065 * is kept in the range [-sample_period, 0] so that we can use the
5066 * sign as trigger.
5067 */
5068
Jiri Olsaab573842013-05-01 17:25:44 +02005069u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005070{
5071 struct hw_perf_event *hwc = &event->hw;
5072 u64 period = hwc->last_period;
5073 u64 nr, offset;
5074 s64 old, val;
5075
5076 hwc->last_period = hwc->sample_period;
5077
5078again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005079 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005080 if (val < 0)
5081 return 0;
5082
5083 nr = div64_u64(period + val, period);
5084 offset = nr * period;
5085 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005086 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087 goto again;
5088
5089 return nr;
5090}
5091
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005092static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005093 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005094 struct pt_regs *regs)
5095{
5096 struct hw_perf_event *hwc = &event->hw;
5097 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005098
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005099 if (!overflow)
5100 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005101
5102 if (hwc->interrupts == MAX_INTERRUPTS)
5103 return;
5104
5105 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005106 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005107 data, regs)) {
5108 /*
5109 * We inhibit the overflow from happening when
5110 * hwc->interrupts == MAX_INTERRUPTS.
5111 */
5112 break;
5113 }
5114 throttle = 1;
5115 }
5116}
5117
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005118static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005119 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005120 struct pt_regs *regs)
5121{
5122 struct hw_perf_event *hwc = &event->hw;
5123
Peter Zijlstrae7850592010-05-21 14:43:08 +02005124 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005125
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005126 if (!regs)
5127 return;
5128
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005129 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005130 return;
5131
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005132 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5133 data->period = nr;
5134 return perf_swevent_overflow(event, 1, data, regs);
5135 } else
5136 data->period = event->hw.last_period;
5137
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005138 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005139 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005140
Peter Zijlstrae7850592010-05-21 14:43:08 +02005141 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005142 return;
5143
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005144 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005145}
5146
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005147static int perf_exclude_event(struct perf_event *event,
5148 struct pt_regs *regs)
5149{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005150 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005151 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005152
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005153 if (regs) {
5154 if (event->attr.exclude_user && user_mode(regs))
5155 return 1;
5156
5157 if (event->attr.exclude_kernel && !user_mode(regs))
5158 return 1;
5159 }
5160
5161 return 0;
5162}
5163
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005164static int perf_swevent_match(struct perf_event *event,
5165 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005166 u32 event_id,
5167 struct perf_sample_data *data,
5168 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005169{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005170 if (event->attr.type != type)
5171 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005172
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005173 if (event->attr.config != event_id)
5174 return 0;
5175
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005176 if (perf_exclude_event(event, regs))
5177 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005178
5179 return 1;
5180}
5181
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005182static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005183{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005184 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005185
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005186 return hash_64(val, SWEVENT_HLIST_BITS);
5187}
5188
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005189static inline struct hlist_head *
5190__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005191{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005192 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005193
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005194 return &hlist->heads[hash];
5195}
5196
5197/* For the read side: events when they trigger */
5198static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005199find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005200{
5201 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005202
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005203 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005204 if (!hlist)
5205 return NULL;
5206
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005207 return __find_swevent_head(hlist, type, event_id);
5208}
5209
5210/* For the event head insertion and removal in the hlist */
5211static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005212find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005213{
5214 struct swevent_hlist *hlist;
5215 u32 event_id = event->attr.config;
5216 u64 type = event->attr.type;
5217
5218 /*
5219 * Event scheduling is always serialized against hlist allocation
5220 * and release. Which makes the protected version suitable here.
5221 * The context lock guarantees that.
5222 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005223 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005224 lockdep_is_held(&event->ctx->lock));
5225 if (!hlist)
5226 return NULL;
5227
5228 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005229}
5230
5231static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005232 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005233 struct perf_sample_data *data,
5234 struct pt_regs *regs)
5235{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005236 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005237 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005238 struct hlist_head *head;
5239
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005240 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005241 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005242 if (!head)
5243 goto end;
5244
Sasha Levinb67bfe02013-02-27 17:06:00 -08005245 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005246 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005247 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005248 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005249end:
5250 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005251}
5252
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005253int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005255 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005256
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005257 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005259EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005261inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005262{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005263 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005264
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005265 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005266}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005267
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005268void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005270 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005271 int rctx;
5272
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005273 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005274 rctx = perf_swevent_get_recursion_context();
5275 if (rctx < 0)
5276 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005277
Robert Richterfd0d0002012-04-02 20:19:08 +02005278 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005279
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005280 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005281
5282 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005283 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284}
5285
5286static void perf_swevent_read(struct perf_event *event)
5287{
5288}
5289
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005290static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005292 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005293 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005294 struct hlist_head *head;
5295
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005296 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 hwc->last_period = hwc->sample_period;
5298 perf_swevent_set_period(event);
5299 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005300
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005301 hwc->state = !(flags & PERF_EF_START);
5302
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005303 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005304 if (WARN_ON_ONCE(!head))
5305 return -EINVAL;
5306
5307 hlist_add_head_rcu(&event->hlist_entry, head);
5308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005309 return 0;
5310}
5311
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005312static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005313{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005314 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005315}
5316
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005317static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005318{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005319 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005320}
5321
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005322static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005323{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005324 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005325}
5326
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005327/* Deref the hlist from the update side */
5328static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005329swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005330{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005331 return rcu_dereference_protected(swhash->swevent_hlist,
5332 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005333}
5334
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005335static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005336{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005337 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005338
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005339 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005340 return;
5341
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005342 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005343 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005344}
5345
5346static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5347{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005348 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005349
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005350 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005351
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005352 if (!--swhash->hlist_refcount)
5353 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005354
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005355 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005356}
5357
5358static void swevent_hlist_put(struct perf_event *event)
5359{
5360 int cpu;
5361
5362 if (event->cpu != -1) {
5363 swevent_hlist_put_cpu(event, event->cpu);
5364 return;
5365 }
5366
5367 for_each_possible_cpu(cpu)
5368 swevent_hlist_put_cpu(event, cpu);
5369}
5370
5371static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5372{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005373 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005374 int err = 0;
5375
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005376 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005377
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005378 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005379 struct swevent_hlist *hlist;
5380
5381 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5382 if (!hlist) {
5383 err = -ENOMEM;
5384 goto exit;
5385 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005386 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005387 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005388 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005389exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005390 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005391
5392 return err;
5393}
5394
5395static int swevent_hlist_get(struct perf_event *event)
5396{
5397 int err;
5398 int cpu, failed_cpu;
5399
5400 if (event->cpu != -1)
5401 return swevent_hlist_get_cpu(event, event->cpu);
5402
5403 get_online_cpus();
5404 for_each_possible_cpu(cpu) {
5405 err = swevent_hlist_get_cpu(event, cpu);
5406 if (err) {
5407 failed_cpu = cpu;
5408 goto fail;
5409 }
5410 }
5411 put_online_cpus();
5412
5413 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005414fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005415 for_each_possible_cpu(cpu) {
5416 if (cpu == failed_cpu)
5417 break;
5418 swevent_hlist_put_cpu(event, cpu);
5419 }
5420
5421 put_online_cpus();
5422 return err;
5423}
5424
Ingo Molnarc5905af2012-02-24 08:31:31 +01005425struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005426
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005427static void sw_perf_event_destroy(struct perf_event *event)
5428{
5429 u64 event_id = event->attr.config;
5430
5431 WARN_ON(event->parent);
5432
Ingo Molnarc5905af2012-02-24 08:31:31 +01005433 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005434 swevent_hlist_put(event);
5435}
5436
5437static int perf_swevent_init(struct perf_event *event)
5438{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005439 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005440
5441 if (event->attr.type != PERF_TYPE_SOFTWARE)
5442 return -ENOENT;
5443
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005444 /*
5445 * no branch sampling for software events
5446 */
5447 if (has_branch_stack(event))
5448 return -EOPNOTSUPP;
5449
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005450 switch (event_id) {
5451 case PERF_COUNT_SW_CPU_CLOCK:
5452 case PERF_COUNT_SW_TASK_CLOCK:
5453 return -ENOENT;
5454
5455 default:
5456 break;
5457 }
5458
Dan Carpenterce677832010-10-24 21:50:42 +02005459 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005460 return -ENOENT;
5461
5462 if (!event->parent) {
5463 int err;
5464
5465 err = swevent_hlist_get(event);
5466 if (err)
5467 return err;
5468
Ingo Molnarc5905af2012-02-24 08:31:31 +01005469 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005470 event->destroy = sw_perf_event_destroy;
5471 }
5472
5473 return 0;
5474}
5475
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005476static int perf_swevent_event_idx(struct perf_event *event)
5477{
5478 return 0;
5479}
5480
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005481static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005482 .task_ctx_nr = perf_sw_context,
5483
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005484 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005485 .add = perf_swevent_add,
5486 .del = perf_swevent_del,
5487 .start = perf_swevent_start,
5488 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005489 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005490
5491 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005492};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005493
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005494#ifdef CONFIG_EVENT_TRACING
5495
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005496static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005497 struct perf_sample_data *data)
5498{
5499 void *record = data->raw->data;
5500
5501 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5502 return 1;
5503 return 0;
5504}
5505
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005506static int perf_tp_event_match(struct perf_event *event,
5507 struct perf_sample_data *data,
5508 struct pt_regs *regs)
5509{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005510 if (event->hw.state & PERF_HES_STOPPED)
5511 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005512 /*
5513 * All tracepoints are from kernel-space.
5514 */
5515 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005516 return 0;
5517
5518 if (!perf_tp_filter_match(event, data))
5519 return 0;
5520
5521 return 1;
5522}
5523
5524void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005525 struct pt_regs *regs, struct hlist_head *head, int rctx,
5526 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005527{
5528 struct perf_sample_data data;
5529 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005530
5531 struct perf_raw_record raw = {
5532 .size = entry_size,
5533 .data = record,
5534 };
5535
Robert Richterfd0d0002012-04-02 20:19:08 +02005536 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005537 data.raw = &raw;
5538
Sasha Levinb67bfe02013-02-27 17:06:00 -08005539 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005540 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005541 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005542 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005543
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005544 /*
5545 * If we got specified a target task, also iterate its context and
5546 * deliver this event there too.
5547 */
5548 if (task && task != current) {
5549 struct perf_event_context *ctx;
5550 struct trace_entry *entry = record;
5551
5552 rcu_read_lock();
5553 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5554 if (!ctx)
5555 goto unlock;
5556
5557 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5558 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5559 continue;
5560 if (event->attr.config != entry->type)
5561 continue;
5562 if (perf_tp_event_match(event, &data, regs))
5563 perf_swevent_event(event, count, &data, regs);
5564 }
5565unlock:
5566 rcu_read_unlock();
5567 }
5568
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005569 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005570}
5571EXPORT_SYMBOL_GPL(perf_tp_event);
5572
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005573static void tp_perf_event_destroy(struct perf_event *event)
5574{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005575 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576}
5577
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005578static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005579{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005580 int err;
5581
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005582 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5583 return -ENOENT;
5584
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005585 /*
5586 * no branch sampling for tracepoint events
5587 */
5588 if (has_branch_stack(event))
5589 return -EOPNOTSUPP;
5590
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005591 err = perf_trace_init(event);
5592 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005593 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594
5595 event->destroy = tp_perf_event_destroy;
5596
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005597 return 0;
5598}
5599
5600static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005601 .task_ctx_nr = perf_sw_context,
5602
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005603 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005604 .add = perf_trace_add,
5605 .del = perf_trace_del,
5606 .start = perf_swevent_start,
5607 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005608 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005609
5610 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005611};
5612
5613static inline void perf_tp_register(void)
5614{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005615 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005616}
Li Zefan6fb29152009-10-15 11:21:42 +08005617
5618static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5619{
5620 char *filter_str;
5621 int ret;
5622
5623 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5624 return -EINVAL;
5625
5626 filter_str = strndup_user(arg, PAGE_SIZE);
5627 if (IS_ERR(filter_str))
5628 return PTR_ERR(filter_str);
5629
5630 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5631
5632 kfree(filter_str);
5633 return ret;
5634}
5635
5636static void perf_event_free_filter(struct perf_event *event)
5637{
5638 ftrace_profile_free_filter(event);
5639}
5640
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005641#else
Li Zefan6fb29152009-10-15 11:21:42 +08005642
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005643static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645}
Li Zefan6fb29152009-10-15 11:21:42 +08005646
5647static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5648{
5649 return -ENOENT;
5650}
5651
5652static void perf_event_free_filter(struct perf_event *event)
5653{
5654}
5655
Li Zefan07b139c2009-12-21 14:27:35 +08005656#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005657
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005658#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005659void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005660{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005661 struct perf_sample_data sample;
5662 struct pt_regs *regs = data;
5663
Robert Richterfd0d0002012-04-02 20:19:08 +02005664 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005665
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005666 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005667 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005668}
5669#endif
5670
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005671/*
5672 * hrtimer based swevent callback
5673 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005674
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005675static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005676{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005677 enum hrtimer_restart ret = HRTIMER_RESTART;
5678 struct perf_sample_data data;
5679 struct pt_regs *regs;
5680 struct perf_event *event;
5681 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005683 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005684
5685 if (event->state != PERF_EVENT_STATE_ACTIVE)
5686 return HRTIMER_NORESTART;
5687
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005688 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005689
Robert Richterfd0d0002012-04-02 20:19:08 +02005690 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005691 regs = get_irq_regs();
5692
5693 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08005694 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02005695 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005696 ret = HRTIMER_NORESTART;
5697 }
5698
5699 period = max_t(u64, 10000, event->hw.sample_period);
5700 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5701
5702 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005703}
5704
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005705static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005707 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005708 s64 period;
5709
5710 if (!is_sampling_event(event))
5711 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005712
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005713 period = local64_read(&hwc->period_left);
5714 if (period) {
5715 if (period < 0)
5716 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005717
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005718 local64_set(&hwc->period_left, 0);
5719 } else {
5720 period = max_t(u64, 10000, hwc->sample_period);
5721 }
5722 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005723 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005724 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005725}
5726
5727static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5728{
5729 struct hw_perf_event *hwc = &event->hw;
5730
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005731 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005732 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005733 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005734
5735 hrtimer_cancel(&hwc->hrtimer);
5736 }
5737}
5738
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005739static void perf_swevent_init_hrtimer(struct perf_event *event)
5740{
5741 struct hw_perf_event *hwc = &event->hw;
5742
5743 if (!is_sampling_event(event))
5744 return;
5745
5746 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5747 hwc->hrtimer.function = perf_swevent_hrtimer;
5748
5749 /*
5750 * Since hrtimers have a fixed rate, we can do a static freq->period
5751 * mapping and avoid the whole period adjust feedback stuff.
5752 */
5753 if (event->attr.freq) {
5754 long freq = event->attr.sample_freq;
5755
5756 event->attr.sample_period = NSEC_PER_SEC / freq;
5757 hwc->sample_period = event->attr.sample_period;
5758 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09005759 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005760 event->attr.freq = 0;
5761 }
5762}
5763
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005764/*
5765 * Software event: cpu wall time clock
5766 */
5767
5768static void cpu_clock_event_update(struct perf_event *event)
5769{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005770 s64 prev;
5771 u64 now;
5772
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005773 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005774 prev = local64_xchg(&event->hw.prev_count, now);
5775 local64_add(now - prev, &event->count);
5776}
5777
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005778static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005779{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005780 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005781 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005782}
5783
5784static void cpu_clock_event_stop(struct perf_event *event, int flags)
5785{
5786 perf_swevent_cancel_hrtimer(event);
5787 cpu_clock_event_update(event);
5788}
5789
5790static int cpu_clock_event_add(struct perf_event *event, int flags)
5791{
5792 if (flags & PERF_EF_START)
5793 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005794
5795 return 0;
5796}
5797
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005798static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005799{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005800 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005801}
5802
5803static void cpu_clock_event_read(struct perf_event *event)
5804{
5805 cpu_clock_event_update(event);
5806}
5807
5808static int cpu_clock_event_init(struct perf_event *event)
5809{
5810 if (event->attr.type != PERF_TYPE_SOFTWARE)
5811 return -ENOENT;
5812
5813 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5814 return -ENOENT;
5815
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005816 /*
5817 * no branch sampling for software events
5818 */
5819 if (has_branch_stack(event))
5820 return -EOPNOTSUPP;
5821
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005822 perf_swevent_init_hrtimer(event);
5823
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005824 return 0;
5825}
5826
5827static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005828 .task_ctx_nr = perf_sw_context,
5829
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005830 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005831 .add = cpu_clock_event_add,
5832 .del = cpu_clock_event_del,
5833 .start = cpu_clock_event_start,
5834 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005835 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005836
5837 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005838};
5839
5840/*
5841 * Software event: task time clock
5842 */
5843
5844static void task_clock_event_update(struct perf_event *event, u64 now)
5845{
5846 u64 prev;
5847 s64 delta;
5848
5849 prev = local64_xchg(&event->hw.prev_count, now);
5850 delta = now - prev;
5851 local64_add(delta, &event->count);
5852}
5853
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005854static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005855{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005856 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005857 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005858}
5859
5860static void task_clock_event_stop(struct perf_event *event, int flags)
5861{
5862 perf_swevent_cancel_hrtimer(event);
5863 task_clock_event_update(event, event->ctx->time);
5864}
5865
5866static int task_clock_event_add(struct perf_event *event, int flags)
5867{
5868 if (flags & PERF_EF_START)
5869 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005870
5871 return 0;
5872}
5873
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005874static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005875{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005876 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005877}
5878
5879static void task_clock_event_read(struct perf_event *event)
5880{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01005881 u64 now = perf_clock();
5882 u64 delta = now - event->ctx->timestamp;
5883 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005884
5885 task_clock_event_update(event, time);
5886}
5887
5888static int task_clock_event_init(struct perf_event *event)
5889{
5890 if (event->attr.type != PERF_TYPE_SOFTWARE)
5891 return -ENOENT;
5892
5893 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5894 return -ENOENT;
5895
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005896 /*
5897 * no branch sampling for software events
5898 */
5899 if (has_branch_stack(event))
5900 return -EOPNOTSUPP;
5901
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005902 perf_swevent_init_hrtimer(event);
5903
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005904 return 0;
5905}
5906
5907static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005908 .task_ctx_nr = perf_sw_context,
5909
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005910 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005911 .add = task_clock_event_add,
5912 .del = task_clock_event_del,
5913 .start = task_clock_event_start,
5914 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005915 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005916
5917 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005918};
5919
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005920static void perf_pmu_nop_void(struct pmu *pmu)
5921{
5922}
5923
5924static int perf_pmu_nop_int(struct pmu *pmu)
5925{
5926 return 0;
5927}
5928
5929static void perf_pmu_start_txn(struct pmu *pmu)
5930{
5931 perf_pmu_disable(pmu);
5932}
5933
5934static int perf_pmu_commit_txn(struct pmu *pmu)
5935{
5936 perf_pmu_enable(pmu);
5937 return 0;
5938}
5939
5940static void perf_pmu_cancel_txn(struct pmu *pmu)
5941{
5942 perf_pmu_enable(pmu);
5943}
5944
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005945static int perf_event_idx_default(struct perf_event *event)
5946{
5947 return event->hw.idx + 1;
5948}
5949
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005950/*
5951 * Ensures all contexts with the same task_ctx_nr have the same
5952 * pmu_cpu_context too.
5953 */
5954static void *find_pmu_context(int ctxn)
5955{
5956 struct pmu *pmu;
5957
5958 if (ctxn < 0)
5959 return NULL;
5960
5961 list_for_each_entry(pmu, &pmus, entry) {
5962 if (pmu->task_ctx_nr == ctxn)
5963 return pmu->pmu_cpu_context;
5964 }
5965
5966 return NULL;
5967}
5968
Peter Zijlstra51676952010-12-07 14:18:20 +01005969static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005970{
Peter Zijlstra51676952010-12-07 14:18:20 +01005971 int cpu;
5972
5973 for_each_possible_cpu(cpu) {
5974 struct perf_cpu_context *cpuctx;
5975
5976 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5977
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02005978 if (cpuctx->unique_pmu == old_pmu)
5979 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01005980 }
5981}
5982
5983static void free_pmu_context(struct pmu *pmu)
5984{
5985 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005986
5987 mutex_lock(&pmus_lock);
5988 /*
5989 * Like a real lame refcount.
5990 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005991 list_for_each_entry(i, &pmus, entry) {
5992 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5993 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005994 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005995 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005996 }
5997
Peter Zijlstra51676952010-12-07 14:18:20 +01005998 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005999out:
6000 mutex_unlock(&pmus_lock);
6001}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006002static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006003
Peter Zijlstraabe43402010-11-17 23:17:37 +01006004static ssize_t
6005type_show(struct device *dev, struct device_attribute *attr, char *page)
6006{
6007 struct pmu *pmu = dev_get_drvdata(dev);
6008
6009 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6010}
6011
Stephane Eranian62b85632013-04-03 14:21:34 +02006012static ssize_t
6013perf_event_mux_interval_ms_show(struct device *dev,
6014 struct device_attribute *attr,
6015 char *page)
6016{
6017 struct pmu *pmu = dev_get_drvdata(dev);
6018
6019 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6020}
6021
6022static ssize_t
6023perf_event_mux_interval_ms_store(struct device *dev,
6024 struct device_attribute *attr,
6025 const char *buf, size_t count)
6026{
6027 struct pmu *pmu = dev_get_drvdata(dev);
6028 int timer, cpu, ret;
6029
6030 ret = kstrtoint(buf, 0, &timer);
6031 if (ret)
6032 return ret;
6033
6034 if (timer < 1)
6035 return -EINVAL;
6036
6037 /* same value, noting to do */
6038 if (timer == pmu->hrtimer_interval_ms)
6039 return count;
6040
6041 pmu->hrtimer_interval_ms = timer;
6042
6043 /* update all cpuctx for this PMU */
6044 for_each_possible_cpu(cpu) {
6045 struct perf_cpu_context *cpuctx;
6046 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6047 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6048
6049 if (hrtimer_active(&cpuctx->hrtimer))
6050 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6051 }
6052
6053 return count;
6054}
6055
6056#define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
6057
Peter Zijlstraabe43402010-11-17 23:17:37 +01006058static struct device_attribute pmu_dev_attrs[] = {
Stephane Eranian62b85632013-04-03 14:21:34 +02006059 __ATTR_RO(type),
6060 __ATTR_RW(perf_event_mux_interval_ms),
6061 __ATTR_NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006062};
6063
6064static int pmu_bus_running;
6065static struct bus_type pmu_bus = {
6066 .name = "event_source",
6067 .dev_attrs = pmu_dev_attrs,
6068};
6069
6070static void pmu_dev_release(struct device *dev)
6071{
6072 kfree(dev);
6073}
6074
6075static int pmu_dev_alloc(struct pmu *pmu)
6076{
6077 int ret = -ENOMEM;
6078
6079 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6080 if (!pmu->dev)
6081 goto out;
6082
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006083 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006084 device_initialize(pmu->dev);
6085 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6086 if (ret)
6087 goto free_dev;
6088
6089 dev_set_drvdata(pmu->dev, pmu);
6090 pmu->dev->bus = &pmu_bus;
6091 pmu->dev->release = pmu_dev_release;
6092 ret = device_add(pmu->dev);
6093 if (ret)
6094 goto free_dev;
6095
6096out:
6097 return ret;
6098
6099free_dev:
6100 put_device(pmu->dev);
6101 goto out;
6102}
6103
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006104static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006105static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006106
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006107int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006108{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006109 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006110
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006111 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006112 ret = -ENOMEM;
6113 pmu->pmu_disable_count = alloc_percpu(int);
6114 if (!pmu->pmu_disable_count)
6115 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006116
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006117 pmu->type = -1;
6118 if (!name)
6119 goto skip_type;
6120 pmu->name = name;
6121
6122 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006123 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6124 if (type < 0) {
6125 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006126 goto free_pdc;
6127 }
6128 }
6129 pmu->type = type;
6130
Peter Zijlstraabe43402010-11-17 23:17:37 +01006131 if (pmu_bus_running) {
6132 ret = pmu_dev_alloc(pmu);
6133 if (ret)
6134 goto free_idr;
6135 }
6136
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006137skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006138 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6139 if (pmu->pmu_cpu_context)
6140 goto got_cpu_context;
6141
Wei Yongjunc4814202013-04-12 11:05:54 +08006142 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006143 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6144 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006145 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006146
6147 for_each_possible_cpu(cpu) {
6148 struct perf_cpu_context *cpuctx;
6149
6150 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006151 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006152 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006153 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006154 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006155 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006156
6157 __perf_cpu_hrtimer_init(cpuctx, cpu);
6158
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006159 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006160 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006161 }
6162
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006163got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006164 if (!pmu->start_txn) {
6165 if (pmu->pmu_enable) {
6166 /*
6167 * If we have pmu_enable/pmu_disable calls, install
6168 * transaction stubs that use that to try and batch
6169 * hardware accesses.
6170 */
6171 pmu->start_txn = perf_pmu_start_txn;
6172 pmu->commit_txn = perf_pmu_commit_txn;
6173 pmu->cancel_txn = perf_pmu_cancel_txn;
6174 } else {
6175 pmu->start_txn = perf_pmu_nop_void;
6176 pmu->commit_txn = perf_pmu_nop_int;
6177 pmu->cancel_txn = perf_pmu_nop_void;
6178 }
6179 }
6180
6181 if (!pmu->pmu_enable) {
6182 pmu->pmu_enable = perf_pmu_nop_void;
6183 pmu->pmu_disable = perf_pmu_nop_void;
6184 }
6185
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006186 if (!pmu->event_idx)
6187 pmu->event_idx = perf_event_idx_default;
6188
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006189 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006190 ret = 0;
6191unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006192 mutex_unlock(&pmus_lock);
6193
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006194 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006195
Peter Zijlstraabe43402010-11-17 23:17:37 +01006196free_dev:
6197 device_del(pmu->dev);
6198 put_device(pmu->dev);
6199
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006200free_idr:
6201 if (pmu->type >= PERF_TYPE_MAX)
6202 idr_remove(&pmu_idr, pmu->type);
6203
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006204free_pdc:
6205 free_percpu(pmu->pmu_disable_count);
6206 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006207}
6208
6209void perf_pmu_unregister(struct pmu *pmu)
6210{
6211 mutex_lock(&pmus_lock);
6212 list_del_rcu(&pmu->entry);
6213 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006214
6215 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006216 * We dereference the pmu list under both SRCU and regular RCU, so
6217 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006218 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006219 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006220 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006221
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006222 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006223 if (pmu->type >= PERF_TYPE_MAX)
6224 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006225 device_del(pmu->dev);
6226 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006227 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006228}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006229
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006230struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006232 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006233 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006234 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006235
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006236 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006237
6238 rcu_read_lock();
6239 pmu = idr_find(&pmu_idr, event->attr.type);
6240 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006241 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006242 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006243 ret = pmu->event_init(event);
6244 if (ret)
6245 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006246 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006247 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006248
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006249 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006250 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006251 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006252 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006253 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006254
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006255 if (ret != -ENOENT) {
6256 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006257 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006258 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006259 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006260 pmu = ERR_PTR(-ENOENT);
6261unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006262 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006263
6264 return pmu;
6265}
6266
6267/*
6268 * Allocate and initialize a event structure
6269 */
6270static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006271perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006272 struct task_struct *task,
6273 struct perf_event *group_leader,
6274 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006275 perf_overflow_handler_t overflow_handler,
6276 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006277{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006278 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006279 struct perf_event *event;
6280 struct hw_perf_event *hwc;
6281 long err;
6282
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006283 if ((unsigned)cpu >= nr_cpu_ids) {
6284 if (!task || cpu != -1)
6285 return ERR_PTR(-EINVAL);
6286 }
6287
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006288 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006289 if (!event)
6290 return ERR_PTR(-ENOMEM);
6291
6292 /*
6293 * Single events are their own group leaders, with an
6294 * empty sibling list:
6295 */
6296 if (!group_leader)
6297 group_leader = event;
6298
6299 mutex_init(&event->child_mutex);
6300 INIT_LIST_HEAD(&event->child_list);
6301
6302 INIT_LIST_HEAD(&event->group_entry);
6303 INIT_LIST_HEAD(&event->event_entry);
6304 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006305 INIT_LIST_HEAD(&event->rb_entry);
6306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006307 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006308 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006309
6310 mutex_init(&event->mmap_mutex);
6311
Al Viroa6fa9412012-08-20 14:59:25 +01006312 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006313 event->cpu = cpu;
6314 event->attr = *attr;
6315 event->group_leader = group_leader;
6316 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006317 event->oncpu = -1;
6318
6319 event->parent = parent_event;
6320
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006321 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006322 event->id = atomic64_inc_return(&perf_event_id);
6323
6324 event->state = PERF_EVENT_STATE_INACTIVE;
6325
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006326 if (task) {
6327 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006328
6329 if (attr->type == PERF_TYPE_TRACEPOINT)
6330 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006331#ifdef CONFIG_HAVE_HW_BREAKPOINT
6332 /*
6333 * hw_breakpoint is a bit difficult here..
6334 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006335 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006336 event->hw.bp_target = task;
6337#endif
6338 }
6339
Avi Kivity4dc0da82011-06-29 18:42:35 +03006340 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006341 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006342 context = parent_event->overflow_handler_context;
6343 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006344
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006345 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006346 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006347
Jiri Olsa0231bb52013-02-01 11:23:45 +01006348 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349
6350 pmu = NULL;
6351
6352 hwc = &event->hw;
6353 hwc->sample_period = attr->sample_period;
6354 if (attr->freq && attr->sample_freq)
6355 hwc->sample_period = 1;
6356 hwc->last_period = hwc->sample_period;
6357
Peter Zijlstrae7850592010-05-21 14:43:08 +02006358 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006359
6360 /*
6361 * we currently do not support PERF_FORMAT_GROUP on inherited events
6362 */
6363 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6364 goto done;
6365
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006366 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006368done:
6369 err = 0;
6370 if (!pmu)
6371 err = -EINVAL;
6372 else if (IS_ERR(pmu))
6373 err = PTR_ERR(pmu);
6374
6375 if (err) {
6376 if (event->ns)
6377 put_pid_ns(event->ns);
6378 kfree(event);
6379 return ERR_PTR(err);
6380 }
6381
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006382 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006383 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01006384 static_key_slow_inc(&perf_sched_events.key);
Eric B Munson3af9e852010-05-18 15:30:49 +01006385 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006386 atomic_inc(&nr_mmap_events);
6387 if (event->attr.comm)
6388 atomic_inc(&nr_comm_events);
6389 if (event->attr.task)
6390 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006391 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6392 err = get_callchain_buffers();
6393 if (err) {
6394 free_event(event);
6395 return ERR_PTR(err);
6396 }
6397 }
Stephane Eraniand010b332012-02-09 23:21:00 +01006398 if (has_branch_stack(event)) {
6399 static_key_slow_inc(&perf_sched_events.key);
6400 if (!(event->attach_state & PERF_ATTACH_TASK))
6401 atomic_inc(&per_cpu(perf_branch_stack_events,
6402 event->cpu));
6403 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006404 }
6405
6406 return event;
6407}
6408
6409static int perf_copy_attr(struct perf_event_attr __user *uattr,
6410 struct perf_event_attr *attr)
6411{
6412 u32 size;
6413 int ret;
6414
6415 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6416 return -EFAULT;
6417
6418 /*
6419 * zero the full structure, so that a short copy will be nice.
6420 */
6421 memset(attr, 0, sizeof(*attr));
6422
6423 ret = get_user(size, &uattr->size);
6424 if (ret)
6425 return ret;
6426
6427 if (size > PAGE_SIZE) /* silly large */
6428 goto err_size;
6429
6430 if (!size) /* abi compat */
6431 size = PERF_ATTR_SIZE_VER0;
6432
6433 if (size < PERF_ATTR_SIZE_VER0)
6434 goto err_size;
6435
6436 /*
6437 * If we're handed a bigger struct than we know of,
6438 * ensure all the unknown bits are 0 - i.e. new
6439 * user-space does not rely on any kernel feature
6440 * extensions we dont know about yet.
6441 */
6442 if (size > sizeof(*attr)) {
6443 unsigned char __user *addr;
6444 unsigned char __user *end;
6445 unsigned char val;
6446
6447 addr = (void __user *)uattr + sizeof(*attr);
6448 end = (void __user *)uattr + size;
6449
6450 for (; addr < end; addr++) {
6451 ret = get_user(val, addr);
6452 if (ret)
6453 return ret;
6454 if (val)
6455 goto err_size;
6456 }
6457 size = sizeof(*attr);
6458 }
6459
6460 ret = copy_from_user(attr, uattr, size);
6461 if (ret)
6462 return -EFAULT;
6463
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306464 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006465 return -EINVAL;
6466
6467 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6468 return -EINVAL;
6469
6470 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6471 return -EINVAL;
6472
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006473 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6474 u64 mask = attr->branch_sample_type;
6475
6476 /* only using defined bits */
6477 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6478 return -EINVAL;
6479
6480 /* at least one branch bit must be set */
6481 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6482 return -EINVAL;
6483
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006484 /* propagate priv level, when not set for branch */
6485 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6486
6487 /* exclude_kernel checked on syscall entry */
6488 if (!attr->exclude_kernel)
6489 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6490
6491 if (!attr->exclude_user)
6492 mask |= PERF_SAMPLE_BRANCH_USER;
6493
6494 if (!attr->exclude_hv)
6495 mask |= PERF_SAMPLE_BRANCH_HV;
6496 /*
6497 * adjust user setting (for HW filter setup)
6498 */
6499 attr->branch_sample_type = mask;
6500 }
Stephane Eranian2b923c82013-05-21 12:53:37 +02006501 /* kernel level capture: check permissions */
6502 if ((mask & PERF_SAMPLE_BRANCH_KERNEL)
6503 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6504 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006505 }
Jiri Olsa40189942012-08-07 15:20:37 +02006506
Jiri Olsac5ebced2012-08-07 15:20:40 +02006507 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006508 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006509 if (ret)
6510 return ret;
6511 }
6512
6513 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6514 if (!arch_perf_have_user_stack_dump())
6515 return -ENOSYS;
6516
6517 /*
6518 * We have __u32 type for the size, but so far
6519 * we can only use __u16 as maximum due to the
6520 * __u16 sample size limit.
6521 */
6522 if (attr->sample_stack_user >= USHRT_MAX)
6523 ret = -EINVAL;
6524 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6525 ret = -EINVAL;
6526 }
Jiri Olsa40189942012-08-07 15:20:37 +02006527
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006528out:
6529 return ret;
6530
6531err_size:
6532 put_user(sizeof(*attr), &uattr->size);
6533 ret = -E2BIG;
6534 goto out;
6535}
6536
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006537static int
6538perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006540 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541 int ret = -EINVAL;
6542
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006543 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006544 goto set;
6545
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006546 /* don't allow circular references */
6547 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006548 goto out;
6549
Peter Zijlstra0f139302010-05-20 14:35:15 +02006550 /*
6551 * Don't allow cross-cpu buffers
6552 */
6553 if (output_event->cpu != event->cpu)
6554 goto out;
6555
6556 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006557 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006558 */
6559 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6560 goto out;
6561
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006562set:
6563 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006564 /* Can't redirect output if we've got an active mmap() */
6565 if (atomic_read(&event->mmap_count))
6566 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006567
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006568 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006569 /* get the rb we want to redirect to */
6570 rb = ring_buffer_get(output_event);
6571 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006572 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006573 }
6574
Frederic Weisbecker76369132011-05-19 19:55:04 +02006575 old_rb = event->rb;
6576 rcu_assign_pointer(event->rb, rb);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006577 if (old_rb)
6578 ring_buffer_detach(event, old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006579 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006580unlock:
6581 mutex_unlock(&event->mmap_mutex);
6582
Frederic Weisbecker76369132011-05-19 19:55:04 +02006583 if (old_rb)
6584 ring_buffer_put(old_rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006585out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006586 return ret;
6587}
6588
6589/**
6590 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6591 *
6592 * @attr_uptr: event_id type attributes for monitoring/sampling
6593 * @pid: target pid
6594 * @cpu: target cpu
6595 * @group_fd: group leader event fd
6596 */
6597SYSCALL_DEFINE5(perf_event_open,
6598 struct perf_event_attr __user *, attr_uptr,
6599 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6600{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006601 struct perf_event *group_leader = NULL, *output_event = NULL;
6602 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006603 struct perf_event_attr attr;
6604 struct perf_event_context *ctx;
6605 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006606 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006607 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006608 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006609 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006610 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006611 int err;
6612
6613 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006614 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006615 return -EINVAL;
6616
6617 err = perf_copy_attr(attr_uptr, &attr);
6618 if (err)
6619 return err;
6620
6621 if (!attr.exclude_kernel) {
6622 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6623 return -EACCES;
6624 }
6625
6626 if (attr.freq) {
6627 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6628 return -EINVAL;
6629 }
6630
Stephane Eraniane5d13672011-02-14 11:20:01 +02006631 /*
6632 * In cgroup mode, the pid argument is used to pass the fd
6633 * opened to the cgroup directory in cgroupfs. The cpu argument
6634 * designates the cpu on which to monitor threads from that
6635 * cgroup.
6636 */
6637 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6638 return -EINVAL;
6639
Al Viroab72a702012-08-21 09:40:46 -04006640 event_fd = get_unused_fd();
Al Viroea635c62010-05-26 17:40:29 -04006641 if (event_fd < 0)
6642 return event_fd;
6643
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006644 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04006645 err = perf_fget_light(group_fd, &group);
6646 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006647 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04006648 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006649 if (flags & PERF_FLAG_FD_OUTPUT)
6650 output_event = group_leader;
6651 if (flags & PERF_FLAG_FD_NO_GROUP)
6652 group_leader = NULL;
6653 }
6654
Stephane Eraniane5d13672011-02-14 11:20:01 +02006655 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006656 task = find_lively_task_by_vpid(pid);
6657 if (IS_ERR(task)) {
6658 err = PTR_ERR(task);
6659 goto err_group_fd;
6660 }
6661 }
6662
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006663 get_online_cpus();
6664
Avi Kivity4dc0da82011-06-29 18:42:35 +03006665 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6666 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006667 if (IS_ERR(event)) {
6668 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006669 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006670 }
6671
Stephane Eraniane5d13672011-02-14 11:20:01 +02006672 if (flags & PERF_FLAG_PID_CGROUP) {
6673 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6674 if (err)
6675 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006676 /*
6677 * one more event:
6678 * - that has cgroup constraint on event->cpu
6679 * - that may need work on context switch
6680 */
6681 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01006682 static_key_slow_inc(&perf_sched_events.key);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006683 }
6684
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006685 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006686 * Special case software events and allow them to be part of
6687 * any hardware group.
6688 */
6689 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006690
6691 if (group_leader &&
6692 (is_software_event(event) != is_software_event(group_leader))) {
6693 if (is_software_event(event)) {
6694 /*
6695 * If event and group_leader are not both a software
6696 * event, and event is, then group leader is not.
6697 *
6698 * Allow the addition of software events to !software
6699 * groups, this is safe because software events never
6700 * fail to schedule.
6701 */
6702 pmu = group_leader->pmu;
6703 } else if (is_software_event(group_leader) &&
6704 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6705 /*
6706 * In case the group is a pure software group, and we
6707 * try to add a hardware event, move the whole group to
6708 * the hardware context.
6709 */
6710 move_group = 1;
6711 }
6712 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006713
6714 /*
6715 * Get the target context (task or percpu):
6716 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006717 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006718 if (IS_ERR(ctx)) {
6719 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006720 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006721 }
6722
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006723 if (task) {
6724 put_task_struct(task);
6725 task = NULL;
6726 }
6727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006728 /*
6729 * Look up the group leader (we will attach this event to it):
6730 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006731 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006732 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006734 /*
6735 * Do not allow a recursive hierarchy (this new sibling
6736 * becoming part of another group-sibling):
6737 */
6738 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006739 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740 /*
6741 * Do not allow to attach to a group in a different
6742 * task or CPU context:
6743 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006744 if (move_group) {
6745 if (group_leader->ctx->type != ctx->type)
6746 goto err_context;
6747 } else {
6748 if (group_leader->ctx != ctx)
6749 goto err_context;
6750 }
6751
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006752 /*
6753 * Only a group leader can be exclusive or pinned
6754 */
6755 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006756 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006757 }
6758
6759 if (output_event) {
6760 err = perf_event_set_output(event, output_event);
6761 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006762 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006763 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006764
Al Viroea635c62010-05-26 17:40:29 -04006765 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6766 if (IS_ERR(event_file)) {
6767 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006768 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006769 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006770
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006771 if (move_group) {
6772 struct perf_event_context *gctx = group_leader->ctx;
6773
6774 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006775 perf_remove_from_context(group_leader);
Jiri Olsa0231bb52013-02-01 11:23:45 +01006776
6777 /*
6778 * Removing from the context ends up with disabled
6779 * event. What we want here is event in the initial
6780 * startup state, ready to be add into new context.
6781 */
6782 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006783 list_for_each_entry(sibling, &group_leader->sibling_list,
6784 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006785 perf_remove_from_context(sibling);
Jiri Olsa0231bb52013-02-01 11:23:45 +01006786 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006787 put_ctx(gctx);
6788 }
6789 mutex_unlock(&gctx->mutex);
6790 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006791 }
6792
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006793 WARN_ON_ONCE(ctx->parent_ctx);
6794 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006795
6796 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006797 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006798 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006799 get_ctx(ctx);
6800 list_for_each_entry(sibling, &group_leader->sibling_list,
6801 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006802 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006803 get_ctx(ctx);
6804 }
6805 }
6806
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006807 perf_install_in_context(ctx, event, event->cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006808 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006809 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006810 mutex_unlock(&ctx->mutex);
6811
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006812 put_online_cpus();
6813
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006814 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006815
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006816 mutex_lock(&current->perf_event_mutex);
6817 list_add_tail(&event->owner_entry, &current->perf_event_list);
6818 mutex_unlock(&current->perf_event_mutex);
6819
Peter Zijlstra8a495422010-05-27 15:47:49 +02006820 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006821 * Precalculate sample_data sizes
6822 */
6823 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006824 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006825
6826 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006827 * Drop the reference on the group_event after placing the
6828 * new event on the sibling_list. This ensures destruction
6829 * of the group leader will find the pointer to itself in
6830 * perf_group_detach().
6831 */
Al Viro2903ff02012-08-28 12:52:22 -04006832 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04006833 fd_install(event_fd, event_file);
6834 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006835
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006836err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006837 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006838 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006839err_alloc:
6840 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006841err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006842 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006843 if (task)
6844 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006845err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04006846 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04006847err_fd:
6848 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006849 return err;
6850}
6851
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006852/**
6853 * perf_event_create_kernel_counter
6854 *
6855 * @attr: attributes of the counter to create
6856 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006857 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006858 */
6859struct perf_event *
6860perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006861 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006862 perf_overflow_handler_t overflow_handler,
6863 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006864{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006865 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006866 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006867 int err;
6868
6869 /*
6870 * Get the target context (task or percpu):
6871 */
6872
Avi Kivity4dc0da82011-06-29 18:42:35 +03006873 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
6874 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006875 if (IS_ERR(event)) {
6876 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006877 goto err;
6878 }
6879
Matt Helsley38a81da2010-09-13 13:01:20 -07006880 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006881 if (IS_ERR(ctx)) {
6882 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006883 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006884 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006885
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006886 WARN_ON_ONCE(ctx->parent_ctx);
6887 mutex_lock(&ctx->mutex);
6888 perf_install_in_context(ctx, event, cpu);
6889 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006890 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006891 mutex_unlock(&ctx->mutex);
6892
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006893 return event;
6894
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006895err_free:
6896 free_event(event);
6897err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006898 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006899}
6900EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6901
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006902void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
6903{
6904 struct perf_event_context *src_ctx;
6905 struct perf_event_context *dst_ctx;
6906 struct perf_event *event, *tmp;
6907 LIST_HEAD(events);
6908
6909 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
6910 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
6911
6912 mutex_lock(&src_ctx->mutex);
6913 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
6914 event_entry) {
6915 perf_remove_from_context(event);
6916 put_ctx(src_ctx);
6917 list_add(&event->event_entry, &events);
6918 }
6919 mutex_unlock(&src_ctx->mutex);
6920
6921 synchronize_rcu();
6922
6923 mutex_lock(&dst_ctx->mutex);
6924 list_for_each_entry_safe(event, tmp, &events, event_entry) {
6925 list_del(&event->event_entry);
6926 if (event->state >= PERF_EVENT_STATE_OFF)
6927 event->state = PERF_EVENT_STATE_INACTIVE;
6928 perf_install_in_context(dst_ctx, event, dst_cpu);
6929 get_ctx(dst_ctx);
6930 }
6931 mutex_unlock(&dst_ctx->mutex);
6932}
6933EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
6934
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006935static void sync_child_event(struct perf_event *child_event,
6936 struct task_struct *child)
6937{
6938 struct perf_event *parent_event = child_event->parent;
6939 u64 child_val;
6940
6941 if (child_event->attr.inherit_stat)
6942 perf_event_read_event(child_event, child);
6943
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006944 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006945
6946 /*
6947 * Add back the child's count to the parent's count:
6948 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006949 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950 atomic64_add(child_event->total_time_enabled,
6951 &parent_event->child_total_time_enabled);
6952 atomic64_add(child_event->total_time_running,
6953 &parent_event->child_total_time_running);
6954
6955 /*
6956 * Remove this event from the parent's list
6957 */
6958 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6959 mutex_lock(&parent_event->child_mutex);
6960 list_del_init(&child_event->child_list);
6961 mutex_unlock(&parent_event->child_mutex);
6962
6963 /*
6964 * Release the parent event, if this was the last
6965 * reference to it.
6966 */
Al Viroa6fa9412012-08-20 14:59:25 +01006967 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006968}
6969
6970static void
6971__perf_event_exit_task(struct perf_event *child_event,
6972 struct perf_event_context *child_ctx,
6973 struct task_struct *child)
6974{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006975 if (child_event->parent) {
6976 raw_spin_lock_irq(&child_ctx->lock);
6977 perf_group_detach(child_event);
6978 raw_spin_unlock_irq(&child_ctx->lock);
6979 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006980
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006981 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006982
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006984 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006985 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006986 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006987 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01006988 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006989 sync_child_event(child_event, child);
6990 free_event(child_event);
6991 }
6992}
6993
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006994static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006995{
6996 struct perf_event *child_event, *tmp;
6997 struct perf_event_context *child_ctx;
6998 unsigned long flags;
6999
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007000 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007001 perf_event_task(child, NULL, 0);
7002 return;
7003 }
7004
7005 local_irq_save(flags);
7006 /*
7007 * We can't reschedule here because interrupts are disabled,
7008 * and either child is current or it is a task that can't be
7009 * scheduled, so we are now safe from rescheduling changing
7010 * our context.
7011 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007012 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007013
7014 /*
7015 * Take the context lock here so that if find_get_context is
7016 * reading child->perf_event_ctxp, we wait until it has
7017 * incremented the context's refcount before we do put_ctx below.
7018 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007019 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007020 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007021 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007022 /*
7023 * If this context is a clone; unclone it so it can't get
7024 * swapped to another process while we're removing all
7025 * the events from it.
7026 */
7027 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007028 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007029 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007030
7031 /*
7032 * Report the task dead after unscheduling the events so that we
7033 * won't get any samples after PERF_RECORD_EXIT. We can however still
7034 * get a few PERF_RECORD_READ events.
7035 */
7036 perf_event_task(child, child_ctx, 0);
7037
7038 /*
7039 * We can recurse on the same lock type through:
7040 *
7041 * __perf_event_exit_task()
7042 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007043 * put_event()
7044 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045 *
7046 * But since its the parent context it won't be the same instance.
7047 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007048 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007049
7050again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007051 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7052 group_entry)
7053 __perf_event_exit_task(child_event, child_ctx, child);
7054
7055 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007056 group_entry)
7057 __perf_event_exit_task(child_event, child_ctx, child);
7058
7059 /*
7060 * If the last event was a group event, it will have appended all
7061 * its siblings to the list, but we obtained 'tmp' before that which
7062 * will still point to the list head terminating the iteration.
7063 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007064 if (!list_empty(&child_ctx->pinned_groups) ||
7065 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007066 goto again;
7067
7068 mutex_unlock(&child_ctx->mutex);
7069
7070 put_ctx(child_ctx);
7071}
7072
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007073/*
7074 * When a child task exits, feed back event values to parent events.
7075 */
7076void perf_event_exit_task(struct task_struct *child)
7077{
Peter Zijlstra88821352010-11-09 19:01:43 +01007078 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007079 int ctxn;
7080
Peter Zijlstra88821352010-11-09 19:01:43 +01007081 mutex_lock(&child->perf_event_mutex);
7082 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7083 owner_entry) {
7084 list_del_init(&event->owner_entry);
7085
7086 /*
7087 * Ensure the list deletion is visible before we clear
7088 * the owner, closes a race against perf_release() where
7089 * we need to serialize on the owner->perf_event_mutex.
7090 */
7091 smp_wmb();
7092 event->owner = NULL;
7093 }
7094 mutex_unlock(&child->perf_event_mutex);
7095
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007096 for_each_task_context_nr(ctxn)
7097 perf_event_exit_task_context(child, ctxn);
7098}
7099
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007100static void perf_free_event(struct perf_event *event,
7101 struct perf_event_context *ctx)
7102{
7103 struct perf_event *parent = event->parent;
7104
7105 if (WARN_ON_ONCE(!parent))
7106 return;
7107
7108 mutex_lock(&parent->child_mutex);
7109 list_del_init(&event->child_list);
7110 mutex_unlock(&parent->child_mutex);
7111
Al Viroa6fa9412012-08-20 14:59:25 +01007112 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007113
Peter Zijlstra8a495422010-05-27 15:47:49 +02007114 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007115 list_del_event(event, ctx);
7116 free_event(event);
7117}
7118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007119/*
7120 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007121 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007122 */
7123void perf_event_free_task(struct task_struct *task)
7124{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007125 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007126 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007127 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007128
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007129 for_each_task_context_nr(ctxn) {
7130 ctx = task->perf_event_ctxp[ctxn];
7131 if (!ctx)
7132 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007133
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007134 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007135again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007136 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7137 group_entry)
7138 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007139
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007140 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7141 group_entry)
7142 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007143
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007144 if (!list_empty(&ctx->pinned_groups) ||
7145 !list_empty(&ctx->flexible_groups))
7146 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007147
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007148 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007149
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007150 put_ctx(ctx);
7151 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007152}
7153
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007154void perf_event_delayed_put(struct task_struct *task)
7155{
7156 int ctxn;
7157
7158 for_each_task_context_nr(ctxn)
7159 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7160}
7161
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007162/*
7163 * inherit a event from parent task to child task:
7164 */
7165static struct perf_event *
7166inherit_event(struct perf_event *parent_event,
7167 struct task_struct *parent,
7168 struct perf_event_context *parent_ctx,
7169 struct task_struct *child,
7170 struct perf_event *group_leader,
7171 struct perf_event_context *child_ctx)
7172{
7173 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007174 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007175
7176 /*
7177 * Instead of creating recursive hierarchies of events,
7178 * we link inherited events back to the original parent,
7179 * which has a filp for sure, which we use as the reference
7180 * count:
7181 */
7182 if (parent_event->parent)
7183 parent_event = parent_event->parent;
7184
7185 child_event = perf_event_alloc(&parent_event->attr,
7186 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007187 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007188 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007189 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007190 if (IS_ERR(child_event))
7191 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007192
7193 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7194 free_event(child_event);
7195 return NULL;
7196 }
7197
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007198 get_ctx(child_ctx);
7199
7200 /*
7201 * Make the child state follow the state of the parent event,
7202 * not its attr.disabled bit. We hold the parent's mutex,
7203 * so we won't race with perf_event_{en, dis}able_family.
7204 */
7205 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7206 child_event->state = PERF_EVENT_STATE_INACTIVE;
7207 else
7208 child_event->state = PERF_EVENT_STATE_OFF;
7209
7210 if (parent_event->attr.freq) {
7211 u64 sample_period = parent_event->hw.sample_period;
7212 struct hw_perf_event *hwc = &child_event->hw;
7213
7214 hwc->sample_period = sample_period;
7215 hwc->last_period = sample_period;
7216
7217 local64_set(&hwc->period_left, sample_period);
7218 }
7219
7220 child_event->ctx = child_ctx;
7221 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007222 child_event->overflow_handler_context
7223 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007224
7225 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007226 * Precalculate sample_data sizes
7227 */
7228 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007229 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007230
7231 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007232 * Link it up in the child's context:
7233 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007234 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007235 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007236 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007237
7238 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007239 * Link this into the parent event's child list
7240 */
7241 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7242 mutex_lock(&parent_event->child_mutex);
7243 list_add_tail(&child_event->child_list, &parent_event->child_list);
7244 mutex_unlock(&parent_event->child_mutex);
7245
7246 return child_event;
7247}
7248
7249static int inherit_group(struct perf_event *parent_event,
7250 struct task_struct *parent,
7251 struct perf_event_context *parent_ctx,
7252 struct task_struct *child,
7253 struct perf_event_context *child_ctx)
7254{
7255 struct perf_event *leader;
7256 struct perf_event *sub;
7257 struct perf_event *child_ctr;
7258
7259 leader = inherit_event(parent_event, parent, parent_ctx,
7260 child, NULL, child_ctx);
7261 if (IS_ERR(leader))
7262 return PTR_ERR(leader);
7263 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7264 child_ctr = inherit_event(sub, parent, parent_ctx,
7265 child, leader, child_ctx);
7266 if (IS_ERR(child_ctr))
7267 return PTR_ERR(child_ctr);
7268 }
7269 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007270}
7271
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007272static int
7273inherit_task_group(struct perf_event *event, struct task_struct *parent,
7274 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007275 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007276 int *inherited_all)
7277{
7278 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007279 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007280
7281 if (!event->attr.inherit) {
7282 *inherited_all = 0;
7283 return 0;
7284 }
7285
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007286 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007287 if (!child_ctx) {
7288 /*
7289 * This is executed from the parent task context, so
7290 * inherit events that have been marked for cloning.
7291 * First allocate and initialize a context for the
7292 * child.
7293 */
7294
Peter Zijlstraeb184472010-09-07 15:55:13 +02007295 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007296 if (!child_ctx)
7297 return -ENOMEM;
7298
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007299 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007300 }
7301
7302 ret = inherit_group(event, parent, parent_ctx,
7303 child, child_ctx);
7304
7305 if (ret)
7306 *inherited_all = 0;
7307
7308 return ret;
7309}
7310
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007311/*
7312 * Initialize the perf_event context in task_struct
7313 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007314int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007315{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007316 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007317 struct perf_event_context *cloned_ctx;
7318 struct perf_event *event;
7319 struct task_struct *parent = current;
7320 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007321 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007322 int ret = 0;
7323
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007324 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007325 return 0;
7326
7327 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007328 * If the parent's context is a clone, pin it so it won't get
7329 * swapped under us.
7330 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007331 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007332
7333 /*
7334 * No need to check if parent_ctx != NULL here; since we saw
7335 * it non-NULL earlier, the only reason for it to become NULL
7336 * is if we exit, and since we're currently in the middle of
7337 * a fork we can't be exiting at the same time.
7338 */
7339
7340 /*
7341 * Lock the parent list. No need to lock the child - not PID
7342 * hashed yet and not running, so nobody can access it.
7343 */
7344 mutex_lock(&parent_ctx->mutex);
7345
7346 /*
7347 * We dont have to disable NMIs - we are only looking at
7348 * the list, not manipulating it:
7349 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007350 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007351 ret = inherit_task_group(event, parent, parent_ctx,
7352 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007353 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007354 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007355 }
7356
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007357 /*
7358 * We can't hold ctx->lock when iterating the ->flexible_group list due
7359 * to allocations, but we need to prevent rotation because
7360 * rotate_ctx() will change the list from interrupt context.
7361 */
7362 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7363 parent_ctx->rotate_disable = 1;
7364 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7365
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007366 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007367 ret = inherit_task_group(event, parent, parent_ctx,
7368 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007369 if (ret)
7370 break;
7371 }
7372
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007373 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7374 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007375
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007376 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007377
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007378 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007379 /*
7380 * Mark the child context as a clone of the parent
7381 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007382 *
7383 * Note that if the parent is a clone, the holding of
7384 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007385 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007386 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007387 if (cloned_ctx) {
7388 child_ctx->parent_ctx = cloned_ctx;
7389 child_ctx->parent_gen = parent_ctx->parent_gen;
7390 } else {
7391 child_ctx->parent_ctx = parent_ctx;
7392 child_ctx->parent_gen = parent_ctx->generation;
7393 }
7394 get_ctx(child_ctx->parent_ctx);
7395 }
7396
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007397 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007398 mutex_unlock(&parent_ctx->mutex);
7399
7400 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007401 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007402
7403 return ret;
7404}
7405
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007406/*
7407 * Initialize the perf_event context in task_struct
7408 */
7409int perf_event_init_task(struct task_struct *child)
7410{
7411 int ctxn, ret;
7412
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007413 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7414 mutex_init(&child->perf_event_mutex);
7415 INIT_LIST_HEAD(&child->perf_event_list);
7416
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007417 for_each_task_context_nr(ctxn) {
7418 ret = perf_event_init_context(child, ctxn);
7419 if (ret)
7420 return ret;
7421 }
7422
7423 return 0;
7424}
7425
Paul Mackerras220b1402010-03-10 20:45:52 +11007426static void __init perf_event_init_all_cpus(void)
7427{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007428 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007429 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007430
7431 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007432 swhash = &per_cpu(swevent_htable, cpu);
7433 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007434 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007435 }
7436}
7437
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007438static void __cpuinit perf_event_init_cpu(int cpu)
7439{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007440 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007441
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007442 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007443 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007444 struct swevent_hlist *hlist;
7445
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007446 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7447 WARN_ON(!hlist);
7448 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007449 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007450 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007451}
7452
Peter Zijlstrac2774432010-12-08 15:29:02 +01007453#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007454static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007455{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007456 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7457
7458 WARN_ON(!irqs_disabled());
7459
7460 list_del_init(&cpuctx->rotation_list);
7461}
7462
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007463static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007464{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007465 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007466 struct perf_event *event, *tmp;
7467
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007468 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007469
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007470 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007471 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007472 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007473 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007474}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007475
7476static void perf_event_exit_cpu_context(int cpu)
7477{
7478 struct perf_event_context *ctx;
7479 struct pmu *pmu;
7480 int idx;
7481
7482 idx = srcu_read_lock(&pmus_srcu);
7483 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007484 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007485
7486 mutex_lock(&ctx->mutex);
7487 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7488 mutex_unlock(&ctx->mutex);
7489 }
7490 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007491}
7492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007493static void perf_event_exit_cpu(int cpu)
7494{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007495 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007496
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007497 mutex_lock(&swhash->hlist_mutex);
7498 swevent_hlist_release(swhash);
7499 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007500
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007501 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007502}
7503#else
7504static inline void perf_event_exit_cpu(int cpu) { }
7505#endif
7506
Peter Zijlstrac2774432010-12-08 15:29:02 +01007507static int
7508perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7509{
7510 int cpu;
7511
7512 for_each_online_cpu(cpu)
7513 perf_event_exit_cpu(cpu);
7514
7515 return NOTIFY_OK;
7516}
7517
7518/*
7519 * Run the perf reboot notifier at the very last possible moment so that
7520 * the generic watchdog code runs as long as possible.
7521 */
7522static struct notifier_block perf_reboot_notifier = {
7523 .notifier_call = perf_reboot,
7524 .priority = INT_MIN,
7525};
7526
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007527static int __cpuinit
7528perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7529{
7530 unsigned int cpu = (long)hcpu;
7531
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007532 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007533
7534 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007535 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007536 perf_event_init_cpu(cpu);
7537 break;
7538
Peter Zijlstra5e116372010-06-11 13:35:08 +02007539 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007540 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007541 perf_event_exit_cpu(cpu);
7542 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007543 default:
7544 break;
7545 }
7546
7547 return NOTIFY_OK;
7548}
7549
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007550void __init perf_event_init(void)
7551{
Jason Wessel3c502e72010-11-04 17:33:01 -05007552 int ret;
7553
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007554 idr_init(&pmu_idr);
7555
Paul Mackerras220b1402010-03-10 20:45:52 +11007556 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007557 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007558 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7559 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7560 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007561 perf_tp_register();
7562 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007563 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007564
7565 ret = init_hw_breakpoint();
7566 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007567
7568 /* do not patch jump label more than once per second */
7569 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007570
7571 /*
7572 * Build time assertion that we keep the data_head at the intended
7573 * location. IOW, validation we got the __reserved[] size right.
7574 */
7575 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7576 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007577}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007578
7579static int __init perf_event_sysfs_init(void)
7580{
7581 struct pmu *pmu;
7582 int ret;
7583
7584 mutex_lock(&pmus_lock);
7585
7586 ret = bus_register(&pmu_bus);
7587 if (ret)
7588 goto unlock;
7589
7590 list_for_each_entry(pmu, &pmus, entry) {
7591 if (!pmu->name || pmu->type < 0)
7592 continue;
7593
7594 ret = pmu_dev_alloc(pmu);
7595 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7596 }
7597 pmu_bus_running = 1;
7598 ret = 0;
7599
7600unlock:
7601 mutex_unlock(&pmus_lock);
7602
7603 return ret;
7604}
7605device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007606
7607#ifdef CONFIG_CGROUP_PERF
Tejun Heo92fb9742012-11-19 08:13:38 -08007608static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007609{
7610 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007611
Li Zefan1b15d052011-03-03 14:26:06 +08007612 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007613 if (!jc)
7614 return ERR_PTR(-ENOMEM);
7615
Stephane Eraniane5d13672011-02-14 11:20:01 +02007616 jc->info = alloc_percpu(struct perf_cgroup_info);
7617 if (!jc->info) {
7618 kfree(jc);
7619 return ERR_PTR(-ENOMEM);
7620 }
7621
Stephane Eraniane5d13672011-02-14 11:20:01 +02007622 return &jc->css;
7623}
7624
Tejun Heo92fb9742012-11-19 08:13:38 -08007625static void perf_cgroup_css_free(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007626{
7627 struct perf_cgroup *jc;
7628 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7629 struct perf_cgroup, css);
7630 free_percpu(jc->info);
7631 kfree(jc);
7632}
7633
7634static int __perf_cgroup_move(void *info)
7635{
7636 struct task_struct *task = info;
7637 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7638 return 0;
7639}
7640
Li Zefan761b3ef2012-01-31 13:47:36 +08007641static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007642{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007643 struct task_struct *task;
7644
7645 cgroup_taskset_for_each(task, cgrp, tset)
7646 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007647}
7648
Li Zefan761b3ef2012-01-31 13:47:36 +08007649static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7650 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007651{
7652 /*
7653 * cgroup_exit() is called in the copy_process() failure path.
7654 * Ignore this case since the task hasn't ran yet, this avoids
7655 * trying to poke a half freed task state from generic code.
7656 */
7657 if (!(task->flags & PF_EXITING))
7658 return;
7659
Tejun Heobb9d97b2011-12-12 18:12:21 -08007660 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007661}
7662
7663struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007664 .name = "perf_event",
7665 .subsys_id = perf_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08007666 .css_alloc = perf_cgroup_css_alloc,
7667 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007668 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007669 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007670};
7671#endif /* CONFIG_CGROUP_PERF */