blob: 05b7d8c72c6c73c2efc0f0591a5f7fd220222408 [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>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
8 *
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>
16#include <linux/file.h>
17#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020019#include <linux/hash.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020020#include <linux/sysfs.h>
21#include <linux/dcache.h>
22#include <linux/percpu.h>
23#include <linux/ptrace.h>
24#include <linux/vmstat.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020025#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/hardirq.h>
27#include <linux/rculist.h>
28#include <linux/uaccess.h>
29#include <linux/syscalls.h>
30#include <linux/anon_inodes.h>
31#include <linux/kernel_stat.h>
32#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080033#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050034#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020035
36#include <asm/irq_regs.h>
37
Peter Zijlstra82cd6de2010-10-14 17:57:23 +020038atomic_t perf_task_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020039static atomic_t nr_mmap_events __read_mostly;
40static atomic_t nr_comm_events __read_mostly;
41static atomic_t nr_task_events __read_mostly;
42
Peter Zijlstra108b02c2010-09-06 14:32:03 +020043static LIST_HEAD(pmus);
44static DEFINE_MUTEX(pmus_lock);
45static struct srcu_struct pmus_srcu;
46
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047/*
48 * perf event paranoia level:
49 * -1 - not paranoid at all
50 * 0 - disallow raw tracepoint access for unpriv
51 * 1 - disallow cpu events for unpriv
52 * 2 - disallow kernel profiling for unpriv
53 */
54int sysctl_perf_event_paranoid __read_mostly = 1;
55
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
57
58/*
59 * max perf event sample rate
60 */
61int sysctl_perf_event_sample_rate __read_mostly = 100000;
62
63static atomic64_t perf_event_id;
64
Ingo Molnarcdd6c482009-09-21 12:02:48 +020065void __weak perf_event_print_debug(void) { }
66
Matt Fleming84c79912010-10-03 21:41:13 +010067extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020068{
Matt Fleming84c79912010-10-03 21:41:13 +010069 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +020070}
71
Peter Zijlstra33696fc2010-06-14 08:49:00 +020072void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020073{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020074 int *count = this_cpu_ptr(pmu->pmu_disable_count);
75 if (!(*count)++)
76 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020077}
78
Peter Zijlstra33696fc2010-06-14 08:49:00 +020079void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020080{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020081 int *count = this_cpu_ptr(pmu->pmu_disable_count);
82 if (!--(*count))
83 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020084}
85
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020086static DEFINE_PER_CPU(struct list_head, rotation_list);
87
88/*
89 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
90 * because they're strictly cpu affine and rotate_start is called with IRQs
91 * disabled, while rotate_context is called from IRQ context.
92 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +020093static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020094{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020095 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020096 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020097
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020098 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020099
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200100 if (list_empty(&cpuctx->rotation_list))
101 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200102}
103
104static void get_ctx(struct perf_event_context *ctx)
105{
106 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
107}
108
109static void free_ctx(struct rcu_head *head)
110{
111 struct perf_event_context *ctx;
112
113 ctx = container_of(head, struct perf_event_context, rcu_head);
114 kfree(ctx);
115}
116
117static void put_ctx(struct perf_event_context *ctx)
118{
119 if (atomic_dec_and_test(&ctx->refcount)) {
120 if (ctx->parent_ctx)
121 put_ctx(ctx->parent_ctx);
122 if (ctx->task)
123 put_task_struct(ctx->task);
124 call_rcu(&ctx->rcu_head, free_ctx);
125 }
126}
127
128static void unclone_ctx(struct perf_event_context *ctx)
129{
130 if (ctx->parent_ctx) {
131 put_ctx(ctx->parent_ctx);
132 ctx->parent_ctx = NULL;
133 }
134}
135
136/*
137 * If we inherit events we want to return the parent event id
138 * to userspace.
139 */
140static u64 primary_event_id(struct perf_event *event)
141{
142 u64 id = event->id;
143
144 if (event->parent)
145 id = event->parent->id;
146
147 return id;
148}
149
150/*
151 * Get the perf_event_context for a task and lock it.
152 * This has to cope with with the fact that until it is locked,
153 * the context could get moved to another task.
154 */
155static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200156perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200157{
158 struct perf_event_context *ctx;
159
160 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200161retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200162 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200163 if (ctx) {
164 /*
165 * If this context is a clone of another, it might
166 * get swapped for another underneath us by
167 * perf_event_task_sched_out, though the
168 * rcu_read_lock() protects us from any context
169 * getting freed. Lock the context and check if it
170 * got swapped before we could get the lock, and retry
171 * if so. If we locked the right context, then it
172 * can't get swapped on us any more.
173 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100174 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200175 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100176 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200177 goto retry;
178 }
179
180 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100181 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200182 ctx = NULL;
183 }
184 }
185 rcu_read_unlock();
186 return ctx;
187}
188
189/*
190 * Get the context for a task and increment its pin_count so it
191 * can't get swapped to another task. This also increments its
192 * reference count so that the context can't get freed.
193 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200194static struct perf_event_context *
195perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200196{
197 struct perf_event_context *ctx;
198 unsigned long flags;
199
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200200 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201 if (ctx) {
202 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100203 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200204 }
205 return ctx;
206}
207
208static void perf_unpin_context(struct perf_event_context *ctx)
209{
210 unsigned long flags;
211
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100212 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200213 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100214 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200215 put_ctx(ctx);
216}
217
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100218static inline u64 perf_clock(void)
219{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200220 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100221}
222
223/*
224 * Update the record of the current time in a context.
225 */
226static void update_context_time(struct perf_event_context *ctx)
227{
228 u64 now = perf_clock();
229
230 ctx->time += now - ctx->timestamp;
231 ctx->timestamp = now;
232}
233
234/*
235 * Update the total_time_enabled and total_time_running fields for a event.
236 */
237static void update_event_times(struct perf_event *event)
238{
239 struct perf_event_context *ctx = event->ctx;
240 u64 run_end;
241
242 if (event->state < PERF_EVENT_STATE_INACTIVE ||
243 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
244 return;
245
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100246 if (ctx->is_active)
247 run_end = ctx->time;
248 else
249 run_end = event->tstamp_stopped;
250
251 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100252
253 if (event->state == PERF_EVENT_STATE_INACTIVE)
254 run_end = event->tstamp_stopped;
255 else
256 run_end = ctx->time;
257
258 event->total_time_running = run_end - event->tstamp_running;
259}
260
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200261/*
262 * Update total_time_enabled and total_time_running for all events in a group.
263 */
264static void update_group_times(struct perf_event *leader)
265{
266 struct perf_event *event;
267
268 update_event_times(leader);
269 list_for_each_entry(event, &leader->sibling_list, group_entry)
270 update_event_times(event);
271}
272
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100273static struct list_head *
274ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
275{
276 if (event->attr.pinned)
277 return &ctx->pinned_groups;
278 else
279 return &ctx->flexible_groups;
280}
281
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200282/*
283 * Add a event from the lists for its context.
284 * Must be called with ctx->mutex and ctx->lock held.
285 */
286static void
287list_add_event(struct perf_event *event, struct perf_event_context *ctx)
288{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200289 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
290 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200291
292 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200293 * If we're a stand alone event or group leader, we go to the context
294 * list, group events are kept attached to the group so that
295 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200296 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200297 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100298 struct list_head *list;
299
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100300 if (is_software_event(event))
301 event->group_flags |= PERF_GROUP_SOFTWARE;
302
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100303 list = ctx_group_list(event, ctx);
304 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200305 }
306
307 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200308 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200309 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200310 ctx->nr_events++;
311 if (event->attr.inherit_stat)
312 ctx->nr_stat++;
313}
314
Peter Zijlstra8a495422010-05-27 15:47:49 +0200315static void perf_group_attach(struct perf_event *event)
316{
317 struct perf_event *group_leader = event->group_leader;
318
Peter Zijlstra74c33372010-10-15 11:40:29 +0200319 /*
320 * We can have double attach due to group movement in perf_event_open.
321 */
322 if (event->attach_state & PERF_ATTACH_GROUP)
323 return;
324
Peter Zijlstra8a495422010-05-27 15:47:49 +0200325 event->attach_state |= PERF_ATTACH_GROUP;
326
327 if (group_leader == event)
328 return;
329
330 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
331 !is_software_event(event))
332 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
333
334 list_add_tail(&event->group_entry, &group_leader->sibling_list);
335 group_leader->nr_siblings++;
336}
337
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338/*
339 * Remove a event from the lists for its context.
340 * Must be called with ctx->mutex and ctx->lock held.
341 */
342static void
343list_del_event(struct perf_event *event, struct perf_event_context *ctx)
344{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200345 /*
346 * We can have double detach due to exit/hot-unplug + close.
347 */
348 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200349 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200350
351 event->attach_state &= ~PERF_ATTACH_CONTEXT;
352
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200353 ctx->nr_events--;
354 if (event->attr.inherit_stat)
355 ctx->nr_stat--;
356
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200357 list_del_rcu(&event->event_entry);
358
Peter Zijlstra8a495422010-05-27 15:47:49 +0200359 if (event->group_leader == event)
360 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200361
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200362 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800363
364 /*
365 * If event was in error state, then keep it
366 * that way, otherwise bogus counts will be
367 * returned on read(). The only way to get out
368 * of error state is by explicit re-enabling
369 * of the event
370 */
371 if (event->state > PERF_EVENT_STATE_OFF)
372 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200373}
374
Peter Zijlstra8a495422010-05-27 15:47:49 +0200375static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200376{
377 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200378 struct list_head *list = NULL;
379
380 /*
381 * We can have double detach due to exit/hot-unplug + close.
382 */
383 if (!(event->attach_state & PERF_ATTACH_GROUP))
384 return;
385
386 event->attach_state &= ~PERF_ATTACH_GROUP;
387
388 /*
389 * If this is a sibling, remove it from its group.
390 */
391 if (event->group_leader != event) {
392 list_del_init(&event->group_entry);
393 event->group_leader->nr_siblings--;
394 return;
395 }
396
397 if (!list_empty(&event->group_entry))
398 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100399
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200400 /*
401 * If this was a group event with sibling events then
402 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200403 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200404 */
405 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200406 if (list)
407 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200408 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100409
410 /* Inherit group flags from the previous leader */
411 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200412 }
413}
414
Stephane Eranianfa66f072010-08-26 16:40:01 +0200415static inline int
416event_filter_match(struct perf_event *event)
417{
418 return event->cpu == -1 || event->cpu == smp_processor_id();
419}
420
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200421static void
422event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200423 struct perf_cpu_context *cpuctx,
424 struct perf_event_context *ctx)
425{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200426 u64 delta;
427 /*
428 * An event which could not be activated because of
429 * filter mismatch still needs to have its timings
430 * maintained, otherwise bogus information is return
431 * via read() for time_enabled, time_running:
432 */
433 if (event->state == PERF_EVENT_STATE_INACTIVE
434 && !event_filter_match(event)) {
435 delta = ctx->time - event->tstamp_stopped;
436 event->tstamp_running += delta;
437 event->tstamp_stopped = ctx->time;
438 }
439
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200440 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200441 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200442
443 event->state = PERF_EVENT_STATE_INACTIVE;
444 if (event->pending_disable) {
445 event->pending_disable = 0;
446 event->state = PERF_EVENT_STATE_OFF;
447 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200448 event->tstamp_stopped = ctx->time;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200449 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200450 event->oncpu = -1;
451
452 if (!is_software_event(event))
453 cpuctx->active_oncpu--;
454 ctx->nr_active--;
455 if (event->attr.exclusive || !cpuctx->active_oncpu)
456 cpuctx->exclusive = 0;
457}
458
459static void
460group_sched_out(struct perf_event *group_event,
461 struct perf_cpu_context *cpuctx,
462 struct perf_event_context *ctx)
463{
464 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200465 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200466
467 event_sched_out(group_event, cpuctx, ctx);
468
469 /*
470 * Schedule out siblings (if any):
471 */
472 list_for_each_entry(event, &group_event->sibling_list, group_entry)
473 event_sched_out(event, cpuctx, ctx);
474
Stephane Eranianfa66f072010-08-26 16:40:01 +0200475 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200476 cpuctx->exclusive = 0;
477}
478
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200479static inline struct perf_cpu_context *
480__get_cpu_context(struct perf_event_context *ctx)
481{
482 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
483}
484
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200485/*
486 * Cross CPU call to remove a performance event
487 *
488 * We disable the event on the hardware level first. After that we
489 * remove it from the context list.
490 */
491static void __perf_event_remove_from_context(void *info)
492{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200493 struct perf_event *event = info;
494 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200495 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200496
497 /*
498 * If this is a task context, we need to check whether it is
499 * the current task context of this cpu. If not it has been
500 * scheduled out before the smp call arrived.
501 */
502 if (ctx->task && cpuctx->task_ctx != ctx)
503 return;
504
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100505 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200506
507 event_sched_out(event, cpuctx, ctx);
508
509 list_del_event(event, ctx);
510
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100511 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200512}
513
514
515/*
516 * Remove the event from a task's (or a CPU's) list of events.
517 *
518 * Must be called with ctx->mutex held.
519 *
520 * CPU events are removed with a smp call. For task events we only
521 * call when the task is on a CPU.
522 *
523 * If event->ctx is a cloned context, callers must make sure that
524 * every task struct that event->ctx->task could possibly point to
525 * remains valid. This is OK when called from perf_release since
526 * that only calls us on the top-level context, which can't be a clone.
527 * When called from perf_event_exit_task, it's OK because the
528 * context has been detached from its task.
529 */
530static void perf_event_remove_from_context(struct perf_event *event)
531{
532 struct perf_event_context *ctx = event->ctx;
533 struct task_struct *task = ctx->task;
534
535 if (!task) {
536 /*
537 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200538 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200539 */
540 smp_call_function_single(event->cpu,
541 __perf_event_remove_from_context,
542 event, 1);
543 return;
544 }
545
546retry:
547 task_oncpu_function_call(task, __perf_event_remove_from_context,
548 event);
549
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100550 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200551 /*
552 * If the context is active we need to retry the smp call.
553 */
554 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100555 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556 goto retry;
557 }
558
559 /*
560 * The lock prevents that this context is scheduled in so we
561 * can remove the event safely, if the call above did not
562 * succeed.
563 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100564 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100566 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200567}
568
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200569/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570 * Cross CPU call to disable a performance event
571 */
572static void __perf_event_disable(void *info)
573{
574 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200576 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200577
578 /*
579 * If this is a per-task event, need to check whether this
580 * event's task is the current task on this cpu.
581 */
582 if (ctx->task && cpuctx->task_ctx != ctx)
583 return;
584
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100585 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200586
587 /*
588 * If the event is on, turn it off.
589 * If it is in error state, leave it in error state.
590 */
591 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
592 update_context_time(ctx);
593 update_group_times(event);
594 if (event == event->group_leader)
595 group_sched_out(event, cpuctx, ctx);
596 else
597 event_sched_out(event, cpuctx, ctx);
598 event->state = PERF_EVENT_STATE_OFF;
599 }
600
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100601 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200602}
603
604/*
605 * Disable a event.
606 *
607 * If event->ctx is a cloned context, callers must make sure that
608 * every task struct that event->ctx->task could possibly point to
609 * remains valid. This condition is satisifed when called through
610 * perf_event_for_each_child or perf_event_for_each because they
611 * hold the top-level event's child_mutex, so any descendant that
612 * goes to exit will block in sync_child_event.
613 * When called from perf_pending_event it's OK because event->ctx
614 * is the current context on this CPU and preemption is disabled,
615 * hence we can't get into perf_event_task_sched_out for this context.
616 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100617void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200618{
619 struct perf_event_context *ctx = event->ctx;
620 struct task_struct *task = ctx->task;
621
622 if (!task) {
623 /*
624 * Disable the event on the cpu that it's on
625 */
626 smp_call_function_single(event->cpu, __perf_event_disable,
627 event, 1);
628 return;
629 }
630
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200631retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200632 task_oncpu_function_call(task, __perf_event_disable, event);
633
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100634 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200635 /*
636 * If the event is still active, we need to retry the cross-call.
637 */
638 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100639 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200640 goto retry;
641 }
642
643 /*
644 * Since we have the lock this context can't be scheduled
645 * in, so we can change the state safely.
646 */
647 if (event->state == PERF_EVENT_STATE_INACTIVE) {
648 update_group_times(event);
649 event->state = PERF_EVENT_STATE_OFF;
650 }
651
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100652 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200653}
654
655static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200656event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200657 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100658 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200659{
660 if (event->state <= PERF_EVENT_STATE_OFF)
661 return 0;
662
663 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100664 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200665 /*
666 * The new state must be visible before we turn it on in the hardware:
667 */
668 smp_wmb();
669
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200670 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200671 event->state = PERF_EVENT_STATE_INACTIVE;
672 event->oncpu = -1;
673 return -EAGAIN;
674 }
675
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200676 event->tstamp_running += ctx->time - event->tstamp_stopped;
677
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200678 if (!is_software_event(event))
679 cpuctx->active_oncpu++;
680 ctx->nr_active++;
681
682 if (event->attr.exclusive)
683 cpuctx->exclusive = 1;
684
685 return 0;
686}
687
688static int
689group_sched_in(struct perf_event *group_event,
690 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100691 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200692{
Lin Ming6bde9b62010-04-23 13:56:00 +0800693 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200694 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +0200695 u64 now = ctx->time;
696 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697
698 if (group_event->state == PERF_EVENT_STATE_OFF)
699 return 0;
700
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200701 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +0800702
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200703 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200704 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200705 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200706 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200707
708 /*
709 * Schedule in siblings as one group (if any):
710 */
711 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200712 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200713 partial_group = event;
714 goto group_error;
715 }
716 }
717
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200718 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000719 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200720
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200721group_error:
722 /*
723 * Groups can be scheduled in as one unit only, so undo any
724 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +0200725 * The events up to the failed event are scheduled out normally,
726 * tstamp_stopped will be updated.
727 *
728 * The failed events and the remaining siblings need to have
729 * their timings updated as if they had gone thru event_sched_in()
730 * and event_sched_out(). This is required to get consistent timings
731 * across the group. This also takes care of the case where the group
732 * could never be scheduled by ensuring tstamp_stopped is set to mark
733 * the time the event was actually stopped, such that time delta
734 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200735 */
736 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
737 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +0200738 simulate = true;
739
740 if (simulate) {
741 event->tstamp_running += now - event->tstamp_stopped;
742 event->tstamp_stopped = now;
743 } else {
744 event_sched_out(event, cpuctx, ctx);
745 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200746 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200747 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200748
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200749 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200750
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200751 return -EAGAIN;
752}
753
754/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200755 * Work out whether we can put this event group on the CPU now.
756 */
757static int group_can_go_on(struct perf_event *event,
758 struct perf_cpu_context *cpuctx,
759 int can_add_hw)
760{
761 /*
762 * Groups consisting entirely of software events can always go on.
763 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100764 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200765 return 1;
766 /*
767 * If an exclusive group is already on, no other hardware
768 * events can go on.
769 */
770 if (cpuctx->exclusive)
771 return 0;
772 /*
773 * If this group is exclusive and there are already
774 * events on the CPU, it can't go on.
775 */
776 if (event->attr.exclusive && cpuctx->active_oncpu)
777 return 0;
778 /*
779 * Otherwise, try to add it if all previous groups were able
780 * to go on.
781 */
782 return can_add_hw;
783}
784
785static void add_event_to_ctx(struct perf_event *event,
786 struct perf_event_context *ctx)
787{
788 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200789 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200790 event->tstamp_enabled = ctx->time;
791 event->tstamp_running = ctx->time;
792 event->tstamp_stopped = ctx->time;
793}
794
795/*
796 * Cross CPU call to install and enable a performance event
797 *
798 * Must be called with ctx->mutex held
799 */
800static void __perf_install_in_context(void *info)
801{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200802 struct perf_event *event = info;
803 struct perf_event_context *ctx = event->ctx;
804 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200805 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200806 int err;
807
808 /*
809 * If this is a task context, we need to check whether it is
810 * the current task context of this cpu. If not it has been
811 * scheduled out before the smp call arrived.
812 * Or possibly this is the right context but it isn't
813 * on this cpu because it had no events.
814 */
815 if (ctx->task && cpuctx->task_ctx != ctx) {
816 if (cpuctx->task_ctx || ctx->task != current)
817 return;
818 cpuctx->task_ctx = ctx;
819 }
820
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100821 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200822 ctx->is_active = 1;
823 update_context_time(ctx);
824
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200825 add_event_to_ctx(event, ctx);
826
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100827 if (event->cpu != -1 && event->cpu != smp_processor_id())
828 goto unlock;
829
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200830 /*
831 * Don't put the event on if it is disabled or if
832 * it is in a group and the group isn't on.
833 */
834 if (event->state != PERF_EVENT_STATE_INACTIVE ||
835 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
836 goto unlock;
837
838 /*
839 * An exclusive event can't go on if there are already active
840 * hardware events, and no hardware event can go on if there
841 * is already an exclusive event on.
842 */
843 if (!group_can_go_on(event, cpuctx, 1))
844 err = -EEXIST;
845 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100846 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200847
848 if (err) {
849 /*
850 * This event couldn't go on. If it is in a group
851 * then we have to pull the whole group off.
852 * If the event group is pinned then put it in error state.
853 */
854 if (leader != event)
855 group_sched_out(leader, cpuctx, ctx);
856 if (leader->attr.pinned) {
857 update_group_times(leader);
858 leader->state = PERF_EVENT_STATE_ERROR;
859 }
860 }
861
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200862unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100863 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200864}
865
866/*
867 * Attach a performance event to a context
868 *
869 * First we add the event to the list with the hardware enable bit
870 * in event->hw_config cleared.
871 *
872 * If the event is attached to a task which is on a CPU we use a smp
873 * call to enable it in the task context. The task might have been
874 * scheduled away, but we check this in the smp call again.
875 *
876 * Must be called with ctx->mutex held.
877 */
878static void
879perf_install_in_context(struct perf_event_context *ctx,
880 struct perf_event *event,
881 int cpu)
882{
883 struct task_struct *task = ctx->task;
884
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200885 event->ctx = ctx;
886
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200887 if (!task) {
888 /*
889 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200890 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200891 */
892 smp_call_function_single(cpu, __perf_install_in_context,
893 event, 1);
894 return;
895 }
896
897retry:
898 task_oncpu_function_call(task, __perf_install_in_context,
899 event);
900
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100901 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200902 /*
903 * we need to retry the smp call.
904 */
905 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100906 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200907 goto retry;
908 }
909
910 /*
911 * The lock prevents that this context is scheduled in so we
912 * can add the event safely, if it the call above did not
913 * succeed.
914 */
915 if (list_empty(&event->group_entry))
916 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100917 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200918}
919
920/*
921 * Put a event into inactive state and update time fields.
922 * Enabling the leader of a group effectively enables all
923 * the group members that aren't explicitly disabled, so we
924 * have to update their ->tstamp_enabled also.
925 * Note: this works for group members as well as group leaders
926 * since the non-leader members' sibling_lists will be empty.
927 */
928static void __perf_event_mark_enabled(struct perf_event *event,
929 struct perf_event_context *ctx)
930{
931 struct perf_event *sub;
932
933 event->state = PERF_EVENT_STATE_INACTIVE;
934 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200935 list_for_each_entry(sub, &event->sibling_list, group_entry) {
936 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200937 sub->tstamp_enabled =
938 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200939 }
940 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200941}
942
943/*
944 * Cross CPU call to enable a performance event
945 */
946static void __perf_event_enable(void *info)
947{
948 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200949 struct perf_event_context *ctx = event->ctx;
950 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200951 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200952 int err;
953
954 /*
955 * If this is a per-task event, need to check whether this
956 * event's task is the current task on this cpu.
957 */
958 if (ctx->task && cpuctx->task_ctx != ctx) {
959 if (cpuctx->task_ctx || ctx->task != current)
960 return;
961 cpuctx->task_ctx = ctx;
962 }
963
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100964 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965 ctx->is_active = 1;
966 update_context_time(ctx);
967
968 if (event->state >= PERF_EVENT_STATE_INACTIVE)
969 goto unlock;
970 __perf_event_mark_enabled(event, ctx);
971
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100972 if (event->cpu != -1 && event->cpu != smp_processor_id())
973 goto unlock;
974
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200975 /*
976 * If the event is in a group and isn't the group leader,
977 * then don't put it on unless the group is on.
978 */
979 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
980 goto unlock;
981
982 if (!group_can_go_on(event, cpuctx, 1)) {
983 err = -EEXIST;
984 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200985 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100986 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100988 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200989 }
990
991 if (err) {
992 /*
993 * If this event can't go on and it's part of a
994 * group, then the whole group has to come off.
995 */
996 if (leader != event)
997 group_sched_out(leader, cpuctx, ctx);
998 if (leader->attr.pinned) {
999 update_group_times(leader);
1000 leader->state = PERF_EVENT_STATE_ERROR;
1001 }
1002 }
1003
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001004unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001005 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001006}
1007
1008/*
1009 * Enable a event.
1010 *
1011 * If event->ctx is a cloned context, callers must make sure that
1012 * every task struct that event->ctx->task could possibly point to
1013 * remains valid. This condition is satisfied when called through
1014 * perf_event_for_each_child or perf_event_for_each as described
1015 * for perf_event_disable.
1016 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001017void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018{
1019 struct perf_event_context *ctx = event->ctx;
1020 struct task_struct *task = ctx->task;
1021
1022 if (!task) {
1023 /*
1024 * Enable the event on the cpu that it's on
1025 */
1026 smp_call_function_single(event->cpu, __perf_event_enable,
1027 event, 1);
1028 return;
1029 }
1030
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001031 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001032 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1033 goto out;
1034
1035 /*
1036 * If the event is in error state, clear that first.
1037 * That way, if we see the event in error state below, we
1038 * know that it has gone back into error state, as distinct
1039 * from the task having been scheduled away before the
1040 * cross-call arrived.
1041 */
1042 if (event->state == PERF_EVENT_STATE_ERROR)
1043 event->state = PERF_EVENT_STATE_OFF;
1044
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001045retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001046 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001047 task_oncpu_function_call(task, __perf_event_enable, event);
1048
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001049 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001050
1051 /*
1052 * If the context is active and the event is still off,
1053 * we need to retry the cross-call.
1054 */
1055 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1056 goto retry;
1057
1058 /*
1059 * Since we have the lock this context can't be scheduled
1060 * in, so we can change the state safely.
1061 */
1062 if (event->state == PERF_EVENT_STATE_OFF)
1063 __perf_event_mark_enabled(event, ctx);
1064
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001065out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001066 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001067}
1068
1069static int perf_event_refresh(struct perf_event *event, int refresh)
1070{
1071 /*
1072 * not supported on inherited events
1073 */
1074 if (event->attr.inherit)
1075 return -EINVAL;
1076
1077 atomic_add(refresh, &event->event_limit);
1078 perf_event_enable(event);
1079
1080 return 0;
1081}
1082
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001083enum event_type_t {
1084 EVENT_FLEXIBLE = 0x1,
1085 EVENT_PINNED = 0x2,
1086 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1087};
1088
1089static void ctx_sched_out(struct perf_event_context *ctx,
1090 struct perf_cpu_context *cpuctx,
1091 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001092{
1093 struct perf_event *event;
1094
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001095 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001096 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001097 ctx->is_active = 0;
1098 if (likely(!ctx->nr_events))
1099 goto out;
1100 update_context_time(ctx);
1101
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001102 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001103 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001104
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001105 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001106 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1107 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001108 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001109
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001110 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001111 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001112 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001113 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001114out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001115 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001116 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117}
1118
1119/*
1120 * Test whether two contexts are equivalent, i.e. whether they
1121 * have both been cloned from the same version of the same context
1122 * and they both have the same number of enabled events.
1123 * If the number of enabled events is the same, then the set
1124 * of enabled events should be the same, because these are both
1125 * inherited contexts, therefore we can't access individual events
1126 * in them directly with an fd; we can only enable/disable all
1127 * events via prctl, or enable/disable all events in a family
1128 * via ioctl, which will have the same effect on both contexts.
1129 */
1130static int context_equiv(struct perf_event_context *ctx1,
1131 struct perf_event_context *ctx2)
1132{
1133 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1134 && ctx1->parent_gen == ctx2->parent_gen
1135 && !ctx1->pin_count && !ctx2->pin_count;
1136}
1137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001138static void __perf_event_sync_stat(struct perf_event *event,
1139 struct perf_event *next_event)
1140{
1141 u64 value;
1142
1143 if (!event->attr.inherit_stat)
1144 return;
1145
1146 /*
1147 * Update the event value, we cannot use perf_event_read()
1148 * because we're in the middle of a context switch and have IRQs
1149 * disabled, which upsets smp_call_function_single(), however
1150 * we know the event must be on the current CPU, therefore we
1151 * don't need to use it.
1152 */
1153 switch (event->state) {
1154 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001155 event->pmu->read(event);
1156 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001157
1158 case PERF_EVENT_STATE_INACTIVE:
1159 update_event_times(event);
1160 break;
1161
1162 default:
1163 break;
1164 }
1165
1166 /*
1167 * In order to keep per-task stats reliable we need to flip the event
1168 * values when we flip the contexts.
1169 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001170 value = local64_read(&next_event->count);
1171 value = local64_xchg(&event->count, value);
1172 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173
1174 swap(event->total_time_enabled, next_event->total_time_enabled);
1175 swap(event->total_time_running, next_event->total_time_running);
1176
1177 /*
1178 * Since we swizzled the values, update the user visible data too.
1179 */
1180 perf_event_update_userpage(event);
1181 perf_event_update_userpage(next_event);
1182}
1183
1184#define list_next_entry(pos, member) \
1185 list_entry(pos->member.next, typeof(*pos), member)
1186
1187static void perf_event_sync_stat(struct perf_event_context *ctx,
1188 struct perf_event_context *next_ctx)
1189{
1190 struct perf_event *event, *next_event;
1191
1192 if (!ctx->nr_stat)
1193 return;
1194
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001195 update_context_time(ctx);
1196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197 event = list_first_entry(&ctx->event_list,
1198 struct perf_event, event_entry);
1199
1200 next_event = list_first_entry(&next_ctx->event_list,
1201 struct perf_event, event_entry);
1202
1203 while (&event->event_entry != &ctx->event_list &&
1204 &next_event->event_entry != &next_ctx->event_list) {
1205
1206 __perf_event_sync_stat(event, next_event);
1207
1208 event = list_next_entry(event, event_entry);
1209 next_event = list_next_entry(next_event, event_entry);
1210 }
1211}
1212
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001213void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1214 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001216 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001217 struct perf_event_context *next_ctx;
1218 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001219 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001220 int do_switch = 1;
1221
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001222 if (likely(!ctx))
1223 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001224
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001225 cpuctx = __get_cpu_context(ctx);
1226 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227 return;
1228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001229 rcu_read_lock();
1230 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001231 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 if (parent && next_ctx &&
1233 rcu_dereference(next_ctx->parent_ctx) == parent) {
1234 /*
1235 * Looks like the two contexts are clones, so we might be
1236 * able to optimize the context switch. We lock both
1237 * contexts and check that they are clones under the
1238 * lock (including re-checking that neither has been
1239 * uncloned in the meantime). It doesn't matter which
1240 * order we take the locks because no other cpu could
1241 * be trying to lock both of these tasks.
1242 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001243 raw_spin_lock(&ctx->lock);
1244 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001245 if (context_equiv(ctx, next_ctx)) {
1246 /*
1247 * XXX do we need a memory barrier of sorts
1248 * wrt to rcu_dereference() of perf_event_ctxp
1249 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001250 task->perf_event_ctxp[ctxn] = next_ctx;
1251 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001252 ctx->task = next;
1253 next_ctx->task = task;
1254 do_switch = 0;
1255
1256 perf_event_sync_stat(ctx, next_ctx);
1257 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001258 raw_spin_unlock(&next_ctx->lock);
1259 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001260 }
1261 rcu_read_unlock();
1262
1263 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001264 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265 cpuctx->task_ctx = NULL;
1266 }
1267}
1268
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001269#define for_each_task_context_nr(ctxn) \
1270 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1271
1272/*
1273 * Called from scheduler to remove the events of the current task,
1274 * with interrupts disabled.
1275 *
1276 * We stop each event and update the event value in event->count.
1277 *
1278 * This does not protect us against NMI, but disable()
1279 * sets the disabled bit in the control field of event _before_
1280 * accessing the event control register. If a NMI hits, then it will
1281 * not restart the event.
1282 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001283void __perf_event_task_sched_out(struct task_struct *task,
1284 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001285{
1286 int ctxn;
1287
1288 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1289
1290 for_each_task_context_nr(ctxn)
1291 perf_event_context_sched_out(task, ctxn, next);
1292}
1293
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001294static void task_ctx_sched_out(struct perf_event_context *ctx,
1295 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001296{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001297 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001298
1299 if (!cpuctx->task_ctx)
1300 return;
1301
1302 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1303 return;
1304
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001305 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 cpuctx->task_ctx = NULL;
1307}
1308
1309/*
1310 * Called with IRQs disabled
1311 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001312static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1313 enum event_type_t event_type)
1314{
1315 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316}
1317
1318static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001319ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001320 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001321{
1322 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001323
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001324 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1325 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001327 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328 continue;
1329
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001330 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001331 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001332
1333 /*
1334 * If this pinned group hasn't been scheduled,
1335 * put it in error state.
1336 */
1337 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1338 update_group_times(event);
1339 event->state = PERF_EVENT_STATE_ERROR;
1340 }
1341 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001342}
1343
1344static void
1345ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001346 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001347{
1348 struct perf_event *event;
1349 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001351 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1352 /* Ignore events in OFF or ERROR state */
1353 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001354 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001355 /*
1356 * Listen to the 'cpu' scheduling filter constraint
1357 * of events:
1358 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001359 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 continue;
1361
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001362 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001363 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001364 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001365 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001366 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001367}
1368
1369static void
1370ctx_sched_in(struct perf_event_context *ctx,
1371 struct perf_cpu_context *cpuctx,
1372 enum event_type_t event_type)
1373{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001374 raw_spin_lock(&ctx->lock);
1375 ctx->is_active = 1;
1376 if (likely(!ctx->nr_events))
1377 goto out;
1378
1379 ctx->timestamp = perf_clock();
1380
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001381 /*
1382 * First go through the list and put on any pinned groups
1383 * in order to give them the best chance of going on.
1384 */
1385 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001386 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001387
1388 /* Then walk through the lower prio flexible groups */
1389 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001390 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001391
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001392out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001393 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394}
1395
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001396static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1397 enum event_type_t event_type)
1398{
1399 struct perf_event_context *ctx = &cpuctx->ctx;
1400
1401 ctx_sched_in(ctx, cpuctx, event_type);
1402}
1403
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001404static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001405 enum event_type_t event_type)
1406{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001407 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001408
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001409 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001410 if (cpuctx->task_ctx == ctx)
1411 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001412
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001413 ctx_sched_in(ctx, cpuctx, event_type);
1414 cpuctx->task_ctx = ctx;
1415}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001417void perf_event_context_sched_in(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001418{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001419 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001420
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001421 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001422 if (cpuctx->task_ctx == ctx)
1423 return;
1424
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001425 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001426 /*
1427 * We want to keep the following priority order:
1428 * cpu pinned (that don't need to move), task pinned,
1429 * cpu flexible, task flexible.
1430 */
1431 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1432
1433 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1434 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1435 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1436
1437 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001438
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001439 /*
1440 * Since these rotations are per-cpu, we need to ensure the
1441 * cpu-context we got scheduled on is actually rotating.
1442 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001443 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001444 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445}
1446
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001447/*
1448 * Called from scheduler to add the events of the current task
1449 * with interrupts disabled.
1450 *
1451 * We restore the event value and then enable it.
1452 *
1453 * This does not protect us against NMI, but enable()
1454 * sets the enabled bit in the control field of event _before_
1455 * accessing the event control register. If a NMI hits, then it will
1456 * keep the event running.
1457 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001458void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001459{
1460 struct perf_event_context *ctx;
1461 int ctxn;
1462
1463 for_each_task_context_nr(ctxn) {
1464 ctx = task->perf_event_ctxp[ctxn];
1465 if (likely(!ctx))
1466 continue;
1467
1468 perf_event_context_sched_in(ctx);
1469 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470}
1471
1472#define MAX_INTERRUPTS (~0ULL)
1473
1474static void perf_log_throttle(struct perf_event *event, int enable);
1475
Peter Zijlstraabd50712010-01-26 18:50:16 +01001476static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1477{
1478 u64 frequency = event->attr.sample_freq;
1479 u64 sec = NSEC_PER_SEC;
1480 u64 divisor, dividend;
1481
1482 int count_fls, nsec_fls, frequency_fls, sec_fls;
1483
1484 count_fls = fls64(count);
1485 nsec_fls = fls64(nsec);
1486 frequency_fls = fls64(frequency);
1487 sec_fls = 30;
1488
1489 /*
1490 * We got @count in @nsec, with a target of sample_freq HZ
1491 * the target period becomes:
1492 *
1493 * @count * 10^9
1494 * period = -------------------
1495 * @nsec * sample_freq
1496 *
1497 */
1498
1499 /*
1500 * Reduce accuracy by one bit such that @a and @b converge
1501 * to a similar magnitude.
1502 */
1503#define REDUCE_FLS(a, b) \
1504do { \
1505 if (a##_fls > b##_fls) { \
1506 a >>= 1; \
1507 a##_fls--; \
1508 } else { \
1509 b >>= 1; \
1510 b##_fls--; \
1511 } \
1512} while (0)
1513
1514 /*
1515 * Reduce accuracy until either term fits in a u64, then proceed with
1516 * the other, so that finally we can do a u64/u64 division.
1517 */
1518 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1519 REDUCE_FLS(nsec, frequency);
1520 REDUCE_FLS(sec, count);
1521 }
1522
1523 if (count_fls + sec_fls > 64) {
1524 divisor = nsec * frequency;
1525
1526 while (count_fls + sec_fls > 64) {
1527 REDUCE_FLS(count, sec);
1528 divisor >>= 1;
1529 }
1530
1531 dividend = count * sec;
1532 } else {
1533 dividend = count * sec;
1534
1535 while (nsec_fls + frequency_fls > 64) {
1536 REDUCE_FLS(nsec, frequency);
1537 dividend >>= 1;
1538 }
1539
1540 divisor = nsec * frequency;
1541 }
1542
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001543 if (!divisor)
1544 return dividend;
1545
Peter Zijlstraabd50712010-01-26 18:50:16 +01001546 return div64_u64(dividend, divisor);
1547}
1548
1549static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550{
1551 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001552 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553 s64 delta;
1554
Peter Zijlstraabd50712010-01-26 18:50:16 +01001555 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001556
1557 delta = (s64)(period - hwc->sample_period);
1558 delta = (delta + 7) / 8; /* low pass filter */
1559
1560 sample_period = hwc->sample_period + delta;
1561
1562 if (!sample_period)
1563 sample_period = 1;
1564
1565 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001566
Peter Zijlstrae7850592010-05-21 14:43:08 +02001567 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001568 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001569 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001570 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001571 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572}
1573
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001574static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001575{
1576 struct perf_event *event;
1577 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001578 u64 interrupts, now;
1579 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001581 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001582 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001583 if (event->state != PERF_EVENT_STATE_ACTIVE)
1584 continue;
1585
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001586 if (event->cpu != -1 && event->cpu != smp_processor_id())
1587 continue;
1588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589 hwc = &event->hw;
1590
1591 interrupts = hwc->interrupts;
1592 hwc->interrupts = 0;
1593
1594 /*
1595 * unthrottle events on the tick
1596 */
1597 if (interrupts == MAX_INTERRUPTS) {
1598 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001599 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001600 }
1601
1602 if (!event->attr.freq || !event->attr.sample_freq)
1603 continue;
1604
Peter Zijlstraabd50712010-01-26 18:50:16 +01001605 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001606 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001607 delta = now - hwc->freq_count_stamp;
1608 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609
Peter Zijlstraabd50712010-01-26 18:50:16 +01001610 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001611 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001613 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614}
1615
1616/*
1617 * Round-robin a context's events:
1618 */
1619static void rotate_ctx(struct perf_event_context *ctx)
1620{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001621 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001622
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001623 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001624 list_rotate_left(&ctx->flexible_groups);
1625
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001626 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627}
1628
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001629/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001630 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1631 * because they're strictly cpu affine and rotate_start is called with IRQs
1632 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001633 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001634static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001635{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001636 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001637 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001638 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001640 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001641 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001642 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1643 rotate = 1;
1644 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001646 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001647 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001648 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001649 if (ctx->nr_events != ctx->nr_active)
1650 rotate = 1;
1651 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001653 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001654 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001656 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001658 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001659 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001660
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001661 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001663 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664
1665 rotate_ctx(&cpuctx->ctx);
1666 if (ctx)
1667 rotate_ctx(ctx);
1668
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001669 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001670 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001671 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001672
1673done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001674 if (remove)
1675 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001676
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001677 perf_pmu_enable(cpuctx->ctx.pmu);
1678}
1679
1680void perf_event_task_tick(void)
1681{
1682 struct list_head *head = &__get_cpu_var(rotation_list);
1683 struct perf_cpu_context *cpuctx, *tmp;
1684
1685 WARN_ON(!irqs_disabled());
1686
1687 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1688 if (cpuctx->jiffies_interval == 1 ||
1689 !(jiffies % cpuctx->jiffies_interval))
1690 perf_rotate_context(cpuctx);
1691 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692}
1693
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001694static int event_enable_on_exec(struct perf_event *event,
1695 struct perf_event_context *ctx)
1696{
1697 if (!event->attr.enable_on_exec)
1698 return 0;
1699
1700 event->attr.enable_on_exec = 0;
1701 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1702 return 0;
1703
1704 __perf_event_mark_enabled(event, ctx);
1705
1706 return 1;
1707}
1708
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709/*
1710 * Enable all of a task's events that have been marked enable-on-exec.
1711 * This expects task == current.
1712 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001713static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001714{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 struct perf_event *event;
1716 unsigned long flags;
1717 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001718 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719
1720 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721 if (!ctx || !ctx->nr_events)
1722 goto out;
1723
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001724 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001725
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001726 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001728 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1729 ret = event_enable_on_exec(event, ctx);
1730 if (ret)
1731 enabled = 1;
1732 }
1733
1734 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1735 ret = event_enable_on_exec(event, ctx);
1736 if (ret)
1737 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 }
1739
1740 /*
1741 * Unclone this context if we enabled any event.
1742 */
1743 if (enabled)
1744 unclone_ctx(ctx);
1745
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001746 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001748 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001749out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750 local_irq_restore(flags);
1751}
1752
1753/*
1754 * Cross CPU call to read the hardware event
1755 */
1756static void __perf_event_read(void *info)
1757{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 struct perf_event *event = info;
1759 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001760 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761
1762 /*
1763 * If this is a task context, we need to check whether it is
1764 * the current task context of this cpu. If not it has been
1765 * scheduled out before the smp call arrived. In that case
1766 * event->count would have been updated to a recent sample
1767 * when the event was scheduled out.
1768 */
1769 if (ctx->task && cpuctx->task_ctx != ctx)
1770 return;
1771
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001772 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001773 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001775 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001776
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001777 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001778}
1779
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001780static inline u64 perf_event_count(struct perf_event *event)
1781{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001782 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001783}
1784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785static u64 perf_event_read(struct perf_event *event)
1786{
1787 /*
1788 * If event is enabled and currently active on a CPU, update the
1789 * value in the event structure:
1790 */
1791 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1792 smp_call_function_single(event->oncpu,
1793 __perf_event_read, event, 1);
1794 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001795 struct perf_event_context *ctx = event->ctx;
1796 unsigned long flags;
1797
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001798 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02001799 /*
1800 * may read while context is not active
1801 * (e.g., thread is blocked), in that case
1802 * we cannot update context time
1803 */
1804 if (ctx->is_active)
1805 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001807 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001808 }
1809
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001810 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811}
1812
1813/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001814 * Callchain support
1815 */
1816
1817struct callchain_cpus_entries {
1818 struct rcu_head rcu_head;
1819 struct perf_callchain_entry *cpu_entries[0];
1820};
1821
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001822static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001823static atomic_t nr_callchain_events;
1824static DEFINE_MUTEX(callchain_mutex);
1825struct callchain_cpus_entries *callchain_cpus_entries;
1826
1827
1828__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1829 struct pt_regs *regs)
1830{
1831}
1832
1833__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1834 struct pt_regs *regs)
1835{
1836}
1837
1838static void release_callchain_buffers_rcu(struct rcu_head *head)
1839{
1840 struct callchain_cpus_entries *entries;
1841 int cpu;
1842
1843 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1844
1845 for_each_possible_cpu(cpu)
1846 kfree(entries->cpu_entries[cpu]);
1847
1848 kfree(entries);
1849}
1850
1851static void release_callchain_buffers(void)
1852{
1853 struct callchain_cpus_entries *entries;
1854
1855 entries = callchain_cpus_entries;
1856 rcu_assign_pointer(callchain_cpus_entries, NULL);
1857 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1858}
1859
1860static int alloc_callchain_buffers(void)
1861{
1862 int cpu;
1863 int size;
1864 struct callchain_cpus_entries *entries;
1865
1866 /*
1867 * We can't use the percpu allocation API for data that can be
1868 * accessed from NMI. Use a temporary manual per cpu allocation
1869 * until that gets sorted out.
1870 */
1871 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1872 num_possible_cpus();
1873
1874 entries = kzalloc(size, GFP_KERNEL);
1875 if (!entries)
1876 return -ENOMEM;
1877
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001878 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001879
1880 for_each_possible_cpu(cpu) {
1881 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1882 cpu_to_node(cpu));
1883 if (!entries->cpu_entries[cpu])
1884 goto fail;
1885 }
1886
1887 rcu_assign_pointer(callchain_cpus_entries, entries);
1888
1889 return 0;
1890
1891fail:
1892 for_each_possible_cpu(cpu)
1893 kfree(entries->cpu_entries[cpu]);
1894 kfree(entries);
1895
1896 return -ENOMEM;
1897}
1898
1899static int get_callchain_buffers(void)
1900{
1901 int err = 0;
1902 int count;
1903
1904 mutex_lock(&callchain_mutex);
1905
1906 count = atomic_inc_return(&nr_callchain_events);
1907 if (WARN_ON_ONCE(count < 1)) {
1908 err = -EINVAL;
1909 goto exit;
1910 }
1911
1912 if (count > 1) {
1913 /* If the allocation failed, give up */
1914 if (!callchain_cpus_entries)
1915 err = -ENOMEM;
1916 goto exit;
1917 }
1918
1919 err = alloc_callchain_buffers();
1920 if (err)
1921 release_callchain_buffers();
1922exit:
1923 mutex_unlock(&callchain_mutex);
1924
1925 return err;
1926}
1927
1928static void put_callchain_buffers(void)
1929{
1930 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1931 release_callchain_buffers();
1932 mutex_unlock(&callchain_mutex);
1933 }
1934}
1935
1936static int get_recursion_context(int *recursion)
1937{
1938 int rctx;
1939
1940 if (in_nmi())
1941 rctx = 3;
1942 else if (in_irq())
1943 rctx = 2;
1944 else if (in_softirq())
1945 rctx = 1;
1946 else
1947 rctx = 0;
1948
1949 if (recursion[rctx])
1950 return -1;
1951
1952 recursion[rctx]++;
1953 barrier();
1954
1955 return rctx;
1956}
1957
1958static inline void put_recursion_context(int *recursion, int rctx)
1959{
1960 barrier();
1961 recursion[rctx]--;
1962}
1963
1964static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1965{
1966 int cpu;
1967 struct callchain_cpus_entries *entries;
1968
1969 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1970 if (*rctx == -1)
1971 return NULL;
1972
1973 entries = rcu_dereference(callchain_cpus_entries);
1974 if (!entries)
1975 return NULL;
1976
1977 cpu = smp_processor_id();
1978
1979 return &entries->cpu_entries[cpu][*rctx];
1980}
1981
1982static void
1983put_callchain_entry(int rctx)
1984{
1985 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1986}
1987
1988static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1989{
1990 int rctx;
1991 struct perf_callchain_entry *entry;
1992
1993
1994 entry = get_callchain_entry(&rctx);
1995 if (rctx == -1)
1996 return NULL;
1997
1998 if (!entry)
1999 goto exit_put;
2000
2001 entry->nr = 0;
2002
2003 if (!user_mode(regs)) {
2004 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2005 perf_callchain_kernel(entry, regs);
2006 if (current->mm)
2007 regs = task_pt_regs(current);
2008 else
2009 regs = NULL;
2010 }
2011
2012 if (regs) {
2013 perf_callchain_store(entry, PERF_CONTEXT_USER);
2014 perf_callchain_user(entry, regs);
2015 }
2016
2017exit_put:
2018 put_callchain_entry(rctx);
2019
2020 return entry;
2021}
2022
2023/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 * Initialize the perf_event context in a task_struct:
2025 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002026static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002027{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002028 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002029 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002030 INIT_LIST_HEAD(&ctx->pinned_groups);
2031 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 INIT_LIST_HEAD(&ctx->event_list);
2033 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034}
2035
Peter Zijlstraeb184472010-09-07 15:55:13 +02002036static struct perf_event_context *
2037alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002038{
2039 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002040
2041 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2042 if (!ctx)
2043 return NULL;
2044
2045 __perf_event_init_context(ctx);
2046 if (task) {
2047 ctx->task = task;
2048 get_task_struct(task);
2049 }
2050 ctx->pmu = pmu;
2051
2052 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053}
2054
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002055static struct task_struct *
2056find_lively_task_by_vpid(pid_t vpid)
2057{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002059 int err;
2060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002062 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063 task = current;
2064 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002065 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066 if (task)
2067 get_task_struct(task);
2068 rcu_read_unlock();
2069
2070 if (!task)
2071 return ERR_PTR(-ESRCH);
2072
2073 /*
2074 * Can't attach events to a dying task.
2075 */
2076 err = -ESRCH;
2077 if (task->flags & PF_EXITING)
2078 goto errout;
2079
2080 /* Reuse ptrace permission checks for now. */
2081 err = -EACCES;
2082 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2083 goto errout;
2084
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002085 return task;
2086errout:
2087 put_task_struct(task);
2088 return ERR_PTR(err);
2089
2090}
2091
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002092static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002093find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094{
2095 struct perf_event_context *ctx;
2096 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002098 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099
Matt Helsley38a81da2010-09-13 13:01:20 -07002100 if (!task && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101 /* Must be root to operate on a CPU event: */
2102 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2103 return ERR_PTR(-EACCES);
2104
2105 if (cpu < 0 || cpu >= nr_cpumask_bits)
2106 return ERR_PTR(-EINVAL);
2107
2108 /*
2109 * We could be clever and allow to attach a event to an
2110 * offline CPU and activate it when the CPU comes up, but
2111 * that's for later.
2112 */
2113 if (!cpu_online(cpu))
2114 return ERR_PTR(-ENODEV);
2115
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002116 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117 ctx = &cpuctx->ctx;
2118 get_ctx(ctx);
2119
2120 return ctx;
2121 }
2122
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002123 err = -EINVAL;
2124 ctxn = pmu->task_ctx_nr;
2125 if (ctxn < 0)
2126 goto errout;
2127
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002128retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002129 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002130 if (ctx) {
2131 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002132 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 }
2134
2135 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002136 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137 err = -ENOMEM;
2138 if (!ctx)
2139 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002140
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002141 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002142
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002143 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002144 /*
2145 * We raced with some other task; use
2146 * the context they set.
2147 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002148 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002149 kfree(ctx);
2150 goto retry;
2151 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002152 }
2153
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002154 return ctx;
2155
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002156errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002157 return ERR_PTR(err);
2158}
2159
Li Zefan6fb29152009-10-15 11:21:42 +08002160static void perf_event_free_filter(struct perf_event *event);
2161
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002162static void free_event_rcu(struct rcu_head *head)
2163{
2164 struct perf_event *event;
2165
2166 event = container_of(head, struct perf_event, rcu_head);
2167 if (event->ns)
2168 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002169 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002170 kfree(event);
2171}
2172
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002173static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174
2175static void free_event(struct perf_event *event)
2176{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002177 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002178
2179 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002180 if (event->attach_state & PERF_ATTACH_TASK)
2181 jump_label_dec(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002182 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002183 atomic_dec(&nr_mmap_events);
2184 if (event->attr.comm)
2185 atomic_dec(&nr_comm_events);
2186 if (event->attr.task)
2187 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002188 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2189 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190 }
2191
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002192 if (event->buffer) {
2193 perf_buffer_put(event->buffer);
2194 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002195 }
2196
2197 if (event->destroy)
2198 event->destroy(event);
2199
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002200 if (event->ctx)
2201 put_ctx(event->ctx);
2202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203 call_rcu(&event->rcu_head, free_event_rcu);
2204}
2205
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002206int perf_event_release_kernel(struct perf_event *event)
2207{
2208 struct perf_event_context *ctx = event->ctx;
2209
Peter Zijlstra050735b2010-05-11 11:51:53 +02002210 /*
2211 * Remove from the PMU, can't get re-enabled since we got
2212 * here because the last ref went.
2213 */
2214 perf_event_disable(event);
2215
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002216 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002217 /*
2218 * There are two ways this annotation is useful:
2219 *
2220 * 1) there is a lock recursion from perf_event_exit_task
2221 * see the comment there.
2222 *
2223 * 2) there is a lock-inversion with mmap_sem through
2224 * perf_event_read_group(), which takes faults while
2225 * holding ctx->mutex, however this is called after
2226 * the last filedesc died, so there is no possibility
2227 * to trigger the AB-BA case.
2228 */
2229 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002230 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002231 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002232 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002233 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002234 mutex_unlock(&ctx->mutex);
2235
2236 mutex_lock(&event->owner->perf_event_mutex);
2237 list_del_init(&event->owner_entry);
2238 mutex_unlock(&event->owner->perf_event_mutex);
2239 put_task_struct(event->owner);
2240
2241 free_event(event);
2242
2243 return 0;
2244}
2245EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2246
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002247/*
2248 * Called when the last reference to the file is gone.
2249 */
2250static int perf_release(struct inode *inode, struct file *file)
2251{
2252 struct perf_event *event = file->private_data;
2253
2254 file->private_data = NULL;
2255
2256 return perf_event_release_kernel(event);
2257}
2258
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259static int perf_event_read_size(struct perf_event *event)
2260{
2261 int entry = sizeof(u64); /* value */
2262 int size = 0;
2263 int nr = 1;
2264
2265 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2266 size += sizeof(u64);
2267
2268 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2269 size += sizeof(u64);
2270
2271 if (event->attr.read_format & PERF_FORMAT_ID)
2272 entry += sizeof(u64);
2273
2274 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2275 nr += event->group_leader->nr_siblings;
2276 size += sizeof(u64);
2277 }
2278
2279 size += entry * nr;
2280
2281 return size;
2282}
2283
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002284u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002285{
2286 struct perf_event *child;
2287 u64 total = 0;
2288
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002289 *enabled = 0;
2290 *running = 0;
2291
Peter Zijlstra6f105812009-11-20 22:19:56 +01002292 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002293 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002294 *enabled += event->total_time_enabled +
2295 atomic64_read(&event->child_total_time_enabled);
2296 *running += event->total_time_running +
2297 atomic64_read(&event->child_total_time_running);
2298
2299 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002300 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002301 *enabled += child->total_time_enabled;
2302 *running += child->total_time_running;
2303 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002304 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002305
2306 return total;
2307}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002308EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002310static int perf_event_read_group(struct perf_event *event,
2311 u64 read_format, char __user *buf)
2312{
2313 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002314 int n = 0, size = 0, ret = -EFAULT;
2315 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002316 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002317 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002318
Peter Zijlstra6f105812009-11-20 22:19:56 +01002319 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002320 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321
2322 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002323 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2324 values[n++] = enabled;
2325 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2326 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002327 values[n++] = count;
2328 if (read_format & PERF_FORMAT_ID)
2329 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002330
2331 size = n * sizeof(u64);
2332
2333 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002334 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335
Peter Zijlstra6f105812009-11-20 22:19:56 +01002336 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337
2338 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002339 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002341 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002342 if (read_format & PERF_FORMAT_ID)
2343 values[n++] = primary_event_id(sub);
2344
2345 size = n * sizeof(u64);
2346
Stephane Eranian184d3da2009-11-23 21:40:49 -08002347 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002348 ret = -EFAULT;
2349 goto unlock;
2350 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002351
2352 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002353 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002354unlock:
2355 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002356
Peter Zijlstraabf48682009-11-20 22:19:49 +01002357 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002358}
2359
2360static int perf_event_read_one(struct perf_event *event,
2361 u64 read_format, char __user *buf)
2362{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002363 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364 u64 values[4];
2365 int n = 0;
2366
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002367 values[n++] = perf_event_read_value(event, &enabled, &running);
2368 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2369 values[n++] = enabled;
2370 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2371 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372 if (read_format & PERF_FORMAT_ID)
2373 values[n++] = primary_event_id(event);
2374
2375 if (copy_to_user(buf, values, n * sizeof(u64)))
2376 return -EFAULT;
2377
2378 return n * sizeof(u64);
2379}
2380
2381/*
2382 * Read the performance event - simple non blocking version for now
2383 */
2384static ssize_t
2385perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2386{
2387 u64 read_format = event->attr.read_format;
2388 int ret;
2389
2390 /*
2391 * Return end-of-file for a read on a event that is in
2392 * error state (i.e. because it was pinned but it couldn't be
2393 * scheduled on to the CPU at some point).
2394 */
2395 if (event->state == PERF_EVENT_STATE_ERROR)
2396 return 0;
2397
2398 if (count < perf_event_read_size(event))
2399 return -ENOSPC;
2400
2401 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002402 if (read_format & PERF_FORMAT_GROUP)
2403 ret = perf_event_read_group(event, read_format, buf);
2404 else
2405 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002406
2407 return ret;
2408}
2409
2410static ssize_t
2411perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2412{
2413 struct perf_event *event = file->private_data;
2414
2415 return perf_read_hw(event, buf, count);
2416}
2417
2418static unsigned int perf_poll(struct file *file, poll_table *wait)
2419{
2420 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002421 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422 unsigned int events = POLL_HUP;
2423
2424 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002425 buffer = rcu_dereference(event->buffer);
2426 if (buffer)
2427 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 rcu_read_unlock();
2429
2430 poll_wait(file, &event->waitq, wait);
2431
2432 return events;
2433}
2434
2435static void perf_event_reset(struct perf_event *event)
2436{
2437 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002438 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002439 perf_event_update_userpage(event);
2440}
2441
2442/*
2443 * Holding the top-level event's child_mutex means that any
2444 * descendant process that has inherited this event will block
2445 * in sync_child_event if it goes to exit, thus satisfying the
2446 * task existence requirements of perf_event_enable/disable.
2447 */
2448static void perf_event_for_each_child(struct perf_event *event,
2449 void (*func)(struct perf_event *))
2450{
2451 struct perf_event *child;
2452
2453 WARN_ON_ONCE(event->ctx->parent_ctx);
2454 mutex_lock(&event->child_mutex);
2455 func(event);
2456 list_for_each_entry(child, &event->child_list, child_list)
2457 func(child);
2458 mutex_unlock(&event->child_mutex);
2459}
2460
2461static void perf_event_for_each(struct perf_event *event,
2462 void (*func)(struct perf_event *))
2463{
2464 struct perf_event_context *ctx = event->ctx;
2465 struct perf_event *sibling;
2466
2467 WARN_ON_ONCE(ctx->parent_ctx);
2468 mutex_lock(&ctx->mutex);
2469 event = event->group_leader;
2470
2471 perf_event_for_each_child(event, func);
2472 func(event);
2473 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2474 perf_event_for_each_child(event, func);
2475 mutex_unlock(&ctx->mutex);
2476}
2477
2478static int perf_event_period(struct perf_event *event, u64 __user *arg)
2479{
2480 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002481 int ret = 0;
2482 u64 value;
2483
2484 if (!event->attr.sample_period)
2485 return -EINVAL;
2486
John Blackwoodad0cf342010-09-28 18:03:11 -04002487 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488 return -EFAULT;
2489
2490 if (!value)
2491 return -EINVAL;
2492
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002493 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002494 if (event->attr.freq) {
2495 if (value > sysctl_perf_event_sample_rate) {
2496 ret = -EINVAL;
2497 goto unlock;
2498 }
2499
2500 event->attr.sample_freq = value;
2501 } else {
2502 event->attr.sample_period = value;
2503 event->hw.sample_period = value;
2504 }
2505unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002506 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002507
2508 return ret;
2509}
2510
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002511static const struct file_operations perf_fops;
2512
2513static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2514{
2515 struct file *file;
2516
2517 file = fget_light(fd, fput_needed);
2518 if (!file)
2519 return ERR_PTR(-EBADF);
2520
2521 if (file->f_op != &perf_fops) {
2522 fput_light(file, *fput_needed);
2523 *fput_needed = 0;
2524 return ERR_PTR(-EBADF);
2525 }
2526
2527 return file->private_data;
2528}
2529
2530static int perf_event_set_output(struct perf_event *event,
2531 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002532static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002533
2534static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2535{
2536 struct perf_event *event = file->private_data;
2537 void (*func)(struct perf_event *);
2538 u32 flags = arg;
2539
2540 switch (cmd) {
2541 case PERF_EVENT_IOC_ENABLE:
2542 func = perf_event_enable;
2543 break;
2544 case PERF_EVENT_IOC_DISABLE:
2545 func = perf_event_disable;
2546 break;
2547 case PERF_EVENT_IOC_RESET:
2548 func = perf_event_reset;
2549 break;
2550
2551 case PERF_EVENT_IOC_REFRESH:
2552 return perf_event_refresh(event, arg);
2553
2554 case PERF_EVENT_IOC_PERIOD:
2555 return perf_event_period(event, (u64 __user *)arg);
2556
2557 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002558 {
2559 struct perf_event *output_event = NULL;
2560 int fput_needed = 0;
2561 int ret;
2562
2563 if (arg != -1) {
2564 output_event = perf_fget_light(arg, &fput_needed);
2565 if (IS_ERR(output_event))
2566 return PTR_ERR(output_event);
2567 }
2568
2569 ret = perf_event_set_output(event, output_event);
2570 if (output_event)
2571 fput_light(output_event->filp, fput_needed);
2572
2573 return ret;
2574 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002575
Li Zefan6fb29152009-10-15 11:21:42 +08002576 case PERF_EVENT_IOC_SET_FILTER:
2577 return perf_event_set_filter(event, (void __user *)arg);
2578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002579 default:
2580 return -ENOTTY;
2581 }
2582
2583 if (flags & PERF_IOC_FLAG_GROUP)
2584 perf_event_for_each(event, func);
2585 else
2586 perf_event_for_each_child(event, func);
2587
2588 return 0;
2589}
2590
2591int perf_event_task_enable(void)
2592{
2593 struct perf_event *event;
2594
2595 mutex_lock(&current->perf_event_mutex);
2596 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2597 perf_event_for_each_child(event, perf_event_enable);
2598 mutex_unlock(&current->perf_event_mutex);
2599
2600 return 0;
2601}
2602
2603int perf_event_task_disable(void)
2604{
2605 struct perf_event *event;
2606
2607 mutex_lock(&current->perf_event_mutex);
2608 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2609 perf_event_for_each_child(event, perf_event_disable);
2610 mutex_unlock(&current->perf_event_mutex);
2611
2612 return 0;
2613}
2614
2615#ifndef PERF_EVENT_INDEX_OFFSET
2616# define PERF_EVENT_INDEX_OFFSET 0
2617#endif
2618
2619static int perf_event_index(struct perf_event *event)
2620{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002621 if (event->hw.state & PERF_HES_STOPPED)
2622 return 0;
2623
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002624 if (event->state != PERF_EVENT_STATE_ACTIVE)
2625 return 0;
2626
2627 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2628}
2629
2630/*
2631 * Callers need to ensure there can be no nesting of this function, otherwise
2632 * the seqlock logic goes bad. We can not serialize this because the arch
2633 * code calls this from NMI context.
2634 */
2635void perf_event_update_userpage(struct perf_event *event)
2636{
2637 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002638 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002639
2640 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002641 buffer = rcu_dereference(event->buffer);
2642 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002643 goto unlock;
2644
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002645 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002646
2647 /*
2648 * Disable preemption so as to not let the corresponding user-space
2649 * spin too long if we get preempted.
2650 */
2651 preempt_disable();
2652 ++userpg->lock;
2653 barrier();
2654 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002655 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002657 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002658
2659 userpg->time_enabled = event->total_time_enabled +
2660 atomic64_read(&event->child_total_time_enabled);
2661
2662 userpg->time_running = event->total_time_running +
2663 atomic64_read(&event->child_total_time_running);
2664
2665 barrier();
2666 ++userpg->lock;
2667 preempt_enable();
2668unlock:
2669 rcu_read_unlock();
2670}
2671
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002672static unsigned long perf_data_size(struct perf_buffer *buffer);
2673
2674static void
2675perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2676{
2677 long max_size = perf_data_size(buffer);
2678
2679 if (watermark)
2680 buffer->watermark = min(max_size, watermark);
2681
2682 if (!buffer->watermark)
2683 buffer->watermark = max_size / 2;
2684
2685 if (flags & PERF_BUFFER_WRITABLE)
2686 buffer->writable = 1;
2687
2688 atomic_set(&buffer->refcount, 1);
2689}
2690
Peter Zijlstra906010b2009-09-21 16:08:49 +02002691#ifndef CONFIG_PERF_USE_VMALLOC
2692
2693/*
2694 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2695 */
2696
2697static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002698perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002699{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002700 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002701 return NULL;
2702
2703 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002704 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002705
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002706 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002707}
2708
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002709static void *perf_mmap_alloc_page(int cpu)
2710{
2711 struct page *page;
2712 int node;
2713
2714 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2715 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2716 if (!page)
2717 return NULL;
2718
2719 return page_address(page);
2720}
2721
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002722static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002723perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002725 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002726 unsigned long size;
2727 int i;
2728
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002729 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730 size += nr_pages * sizeof(void *);
2731
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002732 buffer = kzalloc(size, GFP_KERNEL);
2733 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734 goto fail;
2735
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002736 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002737 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002738 goto fail_user_page;
2739
2740 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002741 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002742 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743 goto fail_data_pages;
2744 }
2745
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002746 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002748 perf_buffer_init(buffer, watermark, flags);
2749
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002750 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751
2752fail_data_pages:
2753 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002754 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002756 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002757
2758fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002759 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760
2761fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002762 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763}
2764
2765static void perf_mmap_free_page(unsigned long addr)
2766{
2767 struct page *page = virt_to_page((void *)addr);
2768
2769 page->mapping = NULL;
2770 __free_page(page);
2771}
2772
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002773static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775 int i;
2776
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002777 perf_mmap_free_page((unsigned long)buffer->user_page);
2778 for (i = 0; i < buffer->nr_pages; i++)
2779 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2780 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002781}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002782
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002783static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002784{
2785 return 0;
2786}
2787
Peter Zijlstra906010b2009-09-21 16:08:49 +02002788#else
2789
2790/*
2791 * Back perf_mmap() with vmalloc memory.
2792 *
2793 * Required for architectures that have d-cache aliasing issues.
2794 */
2795
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002796static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002797{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002798 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002799}
2800
Peter Zijlstra906010b2009-09-21 16:08:49 +02002801static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002802perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002803{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002804 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002805 return NULL;
2806
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002807 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002808}
2809
2810static void perf_mmap_unmark_page(void *addr)
2811{
2812 struct page *page = vmalloc_to_page(addr);
2813
2814 page->mapping = NULL;
2815}
2816
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002817static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002818{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002819 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002820 void *base;
2821 int i, nr;
2822
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002823 buffer = container_of(work, struct perf_buffer, work);
2824 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002825
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002826 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002827 for (i = 0; i < nr + 1; i++)
2828 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2829
2830 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002831 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002832}
2833
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002834static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002835{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002836 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002837}
2838
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002839static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002840perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002841{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002842 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002843 unsigned long size;
2844 void *all_buf;
2845
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002846 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002847 size += sizeof(void *);
2848
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002849 buffer = kzalloc(size, GFP_KERNEL);
2850 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002851 goto fail;
2852
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002853 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002854
2855 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2856 if (!all_buf)
2857 goto fail_all_buf;
2858
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002859 buffer->user_page = all_buf;
2860 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2861 buffer->page_order = ilog2(nr_pages);
2862 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002863
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002864 perf_buffer_init(buffer, watermark, flags);
2865
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002866 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002867
2868fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002869 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002870
2871fail:
2872 return NULL;
2873}
2874
2875#endif
2876
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002877static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002878{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002879 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002880}
2881
Peter Zijlstra906010b2009-09-21 16:08:49 +02002882static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2883{
2884 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002885 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002886 int ret = VM_FAULT_SIGBUS;
2887
2888 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2889 if (vmf->pgoff == 0)
2890 ret = 0;
2891 return ret;
2892 }
2893
2894 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002895 buffer = rcu_dereference(event->buffer);
2896 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002897 goto unlock;
2898
2899 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2900 goto unlock;
2901
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002902 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002903 if (!vmf->page)
2904 goto unlock;
2905
2906 get_page(vmf->page);
2907 vmf->page->mapping = vma->vm_file->f_mapping;
2908 vmf->page->index = vmf->pgoff;
2909
2910 ret = 0;
2911unlock:
2912 rcu_read_unlock();
2913
2914 return ret;
2915}
2916
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002917static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002918{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002919 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002920
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002921 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2922 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002923}
2924
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002925static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002926{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002927 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002928
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002929 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002930 buffer = rcu_dereference(event->buffer);
2931 if (buffer) {
2932 if (!atomic_inc_not_zero(&buffer->refcount))
2933 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002934 }
2935 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002936
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002937 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002938}
2939
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002940static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002941{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002942 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002943 return;
2944
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002945 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002946}
2947
2948static void perf_mmap_open(struct vm_area_struct *vma)
2949{
2950 struct perf_event *event = vma->vm_file->private_data;
2951
2952 atomic_inc(&event->mmap_count);
2953}
2954
2955static void perf_mmap_close(struct vm_area_struct *vma)
2956{
2957 struct perf_event *event = vma->vm_file->private_data;
2958
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002960 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002961 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002962 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002963
Peter Zijlstra906010b2009-09-21 16:08:49 +02002964 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002965 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002966 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002967 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002968
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002969 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002970 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002971 }
2972}
2973
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002974static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002975 .open = perf_mmap_open,
2976 .close = perf_mmap_close,
2977 .fault = perf_mmap_fault,
2978 .page_mkwrite = perf_mmap_fault,
2979};
2980
2981static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2982{
2983 struct perf_event *event = file->private_data;
2984 unsigned long user_locked, user_lock_limit;
2985 struct user_struct *user = current_user();
2986 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002987 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002988 unsigned long vma_size;
2989 unsigned long nr_pages;
2990 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002991 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992
Peter Zijlstrac7920612010-05-18 10:33:24 +02002993 /*
2994 * Don't allow mmap() of inherited per-task counters. This would
2995 * create a performance issue due to all children writing to the
2996 * same buffer.
2997 */
2998 if (event->cpu == -1 && event->attr.inherit)
2999 return -EINVAL;
3000
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003001 if (!(vma->vm_flags & VM_SHARED))
3002 return -EINVAL;
3003
3004 vma_size = vma->vm_end - vma->vm_start;
3005 nr_pages = (vma_size / PAGE_SIZE) - 1;
3006
3007 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003008 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003009 * can do bitmasks instead of modulo.
3010 */
3011 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3012 return -EINVAL;
3013
3014 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3015 return -EINVAL;
3016
3017 if (vma->vm_pgoff != 0)
3018 return -EINVAL;
3019
3020 WARN_ON_ONCE(event->ctx->parent_ctx);
3021 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003022 if (event->buffer) {
3023 if (event->buffer->nr_pages == nr_pages)
3024 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003025 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026 ret = -EINVAL;
3027 goto unlock;
3028 }
3029
3030 user_extra = nr_pages + 1;
3031 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3032
3033 /*
3034 * Increase the limit linearly with more CPUs:
3035 */
3036 user_lock_limit *= num_online_cpus();
3037
3038 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3039
3040 extra = 0;
3041 if (user_locked > user_lock_limit)
3042 extra = user_locked - user_lock_limit;
3043
Jiri Slaby78d7d402010-03-05 13:42:54 -08003044 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045 lock_limit >>= PAGE_SHIFT;
3046 locked = vma->vm_mm->locked_vm + extra;
3047
3048 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3049 !capable(CAP_IPC_LOCK)) {
3050 ret = -EPERM;
3051 goto unlock;
3052 }
3053
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003054 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003055
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003056 if (vma->vm_flags & VM_WRITE)
3057 flags |= PERF_BUFFER_WRITABLE;
3058
3059 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3060 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003061 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003062 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003064 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003065 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003067 atomic_long_add(user_extra, &user->locked_vm);
3068 event->mmap_locked = extra;
3069 event->mmap_user = get_current_user();
3070 vma->vm_mm->locked_vm += event->mmap_locked;
3071
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003073 if (!ret)
3074 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003075 mutex_unlock(&event->mmap_mutex);
3076
3077 vma->vm_flags |= VM_RESERVED;
3078 vma->vm_ops = &perf_mmap_vmops;
3079
3080 return ret;
3081}
3082
3083static int perf_fasync(int fd, struct file *filp, int on)
3084{
3085 struct inode *inode = filp->f_path.dentry->d_inode;
3086 struct perf_event *event = filp->private_data;
3087 int retval;
3088
3089 mutex_lock(&inode->i_mutex);
3090 retval = fasync_helper(fd, filp, on, &event->fasync);
3091 mutex_unlock(&inode->i_mutex);
3092
3093 if (retval < 0)
3094 return retval;
3095
3096 return 0;
3097}
3098
3099static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003100 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003101 .release = perf_release,
3102 .read = perf_read,
3103 .poll = perf_poll,
3104 .unlocked_ioctl = perf_ioctl,
3105 .compat_ioctl = perf_ioctl,
3106 .mmap = perf_mmap,
3107 .fasync = perf_fasync,
3108};
3109
3110/*
3111 * Perf event wakeup
3112 *
3113 * If there's data, ensure we set the poll() state and publish everything
3114 * to user-space before waking everybody up.
3115 */
3116
3117void perf_event_wakeup(struct perf_event *event)
3118{
3119 wake_up_all(&event->waitq);
3120
3121 if (event->pending_kill) {
3122 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3123 event->pending_kill = 0;
3124 }
3125}
3126
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003127static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003128{
3129 struct perf_event *event = container_of(entry,
3130 struct perf_event, pending);
3131
3132 if (event->pending_disable) {
3133 event->pending_disable = 0;
3134 __perf_event_disable(event);
3135 }
3136
3137 if (event->pending_wakeup) {
3138 event->pending_wakeup = 0;
3139 perf_event_wakeup(event);
3140 }
3141}
3142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003144 * We assume there is only KVM supporting the callbacks.
3145 * Later on, we might change it to a list if there is
3146 * another virtualization implementation supporting the callbacks.
3147 */
3148struct perf_guest_info_callbacks *perf_guest_cbs;
3149
3150int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3151{
3152 perf_guest_cbs = cbs;
3153 return 0;
3154}
3155EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3156
3157int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3158{
3159 perf_guest_cbs = NULL;
3160 return 0;
3161}
3162EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3163
3164/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003165 * Output
3166 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003167static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003168 unsigned long offset, unsigned long head)
3169{
3170 unsigned long mask;
3171
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003172 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003173 return true;
3174
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003175 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003176
3177 offset = (offset - tail) & mask;
3178 head = (head - tail) & mask;
3179
3180 if ((int)(head - offset) < 0)
3181 return false;
3182
3183 return true;
3184}
3185
3186static void perf_output_wakeup(struct perf_output_handle *handle)
3187{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003188 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189
3190 if (handle->nmi) {
3191 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003192 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193 } else
3194 perf_event_wakeup(handle->event);
3195}
3196
3197/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003199 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003200 * cannot fully serialize things.
3201 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003203 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003205static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003207 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208
Peter Zijlstraef607772010-05-18 10:50:41 +02003209 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003210 local_inc(&buffer->nest);
3211 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212}
3213
Peter Zijlstraef607772010-05-18 10:50:41 +02003214static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003215{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003216 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218
3219again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003220 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221
3222 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003223 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 */
3225
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003226 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003227 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003228
3229 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003230 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003231 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003232 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003233 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003234 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003235
Peter Zijlstraef607772010-05-18 10:50:41 +02003236 /*
3237 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003238 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003239 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003240 if (unlikely(head != local_read(&buffer->head))) {
3241 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003242 goto again;
3243 }
3244
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003245 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003246 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003247
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003248out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003249 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003250}
3251
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003252__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253 const void *buf, unsigned int len)
3254{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003255 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003256 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003257
3258 memcpy(handle->addr, buf, size);
3259
3260 len -= size;
3261 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003262 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003263 handle->size -= size;
3264 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003265 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003266
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003267 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003268 handle->page &= buffer->nr_pages - 1;
3269 handle->addr = buffer->data_pages[handle->page];
3270 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003271 }
3272 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273}
3274
3275int perf_output_begin(struct perf_output_handle *handle,
3276 struct perf_event *event, unsigned int size,
3277 int nmi, int sample)
3278{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003279 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280 unsigned long tail, offset, head;
3281 int have_lost;
3282 struct {
3283 struct perf_event_header header;
3284 u64 id;
3285 u64 lost;
3286 } lost_event;
3287
3288 rcu_read_lock();
3289 /*
3290 * For inherited events we send all the output towards the parent.
3291 */
3292 if (event->parent)
3293 event = event->parent;
3294
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003295 buffer = rcu_dereference(event->buffer);
3296 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297 goto out;
3298
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003299 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003300 handle->event = event;
3301 handle->nmi = nmi;
3302 handle->sample = sample;
3303
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003304 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003305 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003306
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003307 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308 if (have_lost)
3309 size += sizeof(lost_event);
3310
Peter Zijlstraef607772010-05-18 10:50:41 +02003311 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003312
3313 do {
3314 /*
3315 * Userspace could choose to issue a mb() before updating the
3316 * tail pointer. So that all reads will be completed before the
3317 * write is issued.
3318 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003319 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003320 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003321 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003322 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003323 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003324 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003325 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003327 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3328 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003329
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003330 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3331 handle->page &= buffer->nr_pages - 1;
3332 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3333 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003334 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003335 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003337 if (have_lost) {
3338 lost_event.header.type = PERF_RECORD_LOST;
3339 lost_event.header.misc = 0;
3340 lost_event.header.size = sizeof(lost_event);
3341 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003342 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343
3344 perf_output_put(handle, lost_event);
3345 }
3346
3347 return 0;
3348
3349fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003350 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003351 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352out:
3353 rcu_read_unlock();
3354
3355 return -ENOSPC;
3356}
3357
3358void perf_output_end(struct perf_output_handle *handle)
3359{
3360 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003361 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003362
3363 int wakeup_events = event->attr.wakeup_events;
3364
3365 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003366 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003368 local_sub(wakeup_events, &buffer->events);
3369 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370 }
3371 }
3372
Peter Zijlstraef607772010-05-18 10:50:41 +02003373 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003374 rcu_read_unlock();
3375}
3376
3377static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3378{
3379 /*
3380 * only top level events have the pid namespace they were created in
3381 */
3382 if (event->parent)
3383 event = event->parent;
3384
3385 return task_tgid_nr_ns(p, event->ns);
3386}
3387
3388static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3389{
3390 /*
3391 * only top level events have the pid namespace they were created in
3392 */
3393 if (event->parent)
3394 event = event->parent;
3395
3396 return task_pid_nr_ns(p, event->ns);
3397}
3398
3399static void perf_output_read_one(struct perf_output_handle *handle,
3400 struct perf_event *event)
3401{
3402 u64 read_format = event->attr.read_format;
3403 u64 values[4];
3404 int n = 0;
3405
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003406 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3408 values[n++] = event->total_time_enabled +
3409 atomic64_read(&event->child_total_time_enabled);
3410 }
3411 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3412 values[n++] = event->total_time_running +
3413 atomic64_read(&event->child_total_time_running);
3414 }
3415 if (read_format & PERF_FORMAT_ID)
3416 values[n++] = primary_event_id(event);
3417
3418 perf_output_copy(handle, values, n * sizeof(u64));
3419}
3420
3421/*
3422 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3423 */
3424static void perf_output_read_group(struct perf_output_handle *handle,
3425 struct perf_event *event)
3426{
3427 struct perf_event *leader = event->group_leader, *sub;
3428 u64 read_format = event->attr.read_format;
3429 u64 values[5];
3430 int n = 0;
3431
3432 values[n++] = 1 + leader->nr_siblings;
3433
3434 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3435 values[n++] = leader->total_time_enabled;
3436
3437 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3438 values[n++] = leader->total_time_running;
3439
3440 if (leader != event)
3441 leader->pmu->read(leader);
3442
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003443 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444 if (read_format & PERF_FORMAT_ID)
3445 values[n++] = primary_event_id(leader);
3446
3447 perf_output_copy(handle, values, n * sizeof(u64));
3448
3449 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3450 n = 0;
3451
3452 if (sub != event)
3453 sub->pmu->read(sub);
3454
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003455 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003456 if (read_format & PERF_FORMAT_ID)
3457 values[n++] = primary_event_id(sub);
3458
3459 perf_output_copy(handle, values, n * sizeof(u64));
3460 }
3461}
3462
3463static void perf_output_read(struct perf_output_handle *handle,
3464 struct perf_event *event)
3465{
3466 if (event->attr.read_format & PERF_FORMAT_GROUP)
3467 perf_output_read_group(handle, event);
3468 else
3469 perf_output_read_one(handle, event);
3470}
3471
3472void perf_output_sample(struct perf_output_handle *handle,
3473 struct perf_event_header *header,
3474 struct perf_sample_data *data,
3475 struct perf_event *event)
3476{
3477 u64 sample_type = data->type;
3478
3479 perf_output_put(handle, *header);
3480
3481 if (sample_type & PERF_SAMPLE_IP)
3482 perf_output_put(handle, data->ip);
3483
3484 if (sample_type & PERF_SAMPLE_TID)
3485 perf_output_put(handle, data->tid_entry);
3486
3487 if (sample_type & PERF_SAMPLE_TIME)
3488 perf_output_put(handle, data->time);
3489
3490 if (sample_type & PERF_SAMPLE_ADDR)
3491 perf_output_put(handle, data->addr);
3492
3493 if (sample_type & PERF_SAMPLE_ID)
3494 perf_output_put(handle, data->id);
3495
3496 if (sample_type & PERF_SAMPLE_STREAM_ID)
3497 perf_output_put(handle, data->stream_id);
3498
3499 if (sample_type & PERF_SAMPLE_CPU)
3500 perf_output_put(handle, data->cpu_entry);
3501
3502 if (sample_type & PERF_SAMPLE_PERIOD)
3503 perf_output_put(handle, data->period);
3504
3505 if (sample_type & PERF_SAMPLE_READ)
3506 perf_output_read(handle, event);
3507
3508 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3509 if (data->callchain) {
3510 int size = 1;
3511
3512 if (data->callchain)
3513 size += data->callchain->nr;
3514
3515 size *= sizeof(u64);
3516
3517 perf_output_copy(handle, data->callchain, size);
3518 } else {
3519 u64 nr = 0;
3520 perf_output_put(handle, nr);
3521 }
3522 }
3523
3524 if (sample_type & PERF_SAMPLE_RAW) {
3525 if (data->raw) {
3526 perf_output_put(handle, data->raw->size);
3527 perf_output_copy(handle, data->raw->data,
3528 data->raw->size);
3529 } else {
3530 struct {
3531 u32 size;
3532 u32 data;
3533 } raw = {
3534 .size = sizeof(u32),
3535 .data = 0,
3536 };
3537 perf_output_put(handle, raw);
3538 }
3539 }
3540}
3541
3542void perf_prepare_sample(struct perf_event_header *header,
3543 struct perf_sample_data *data,
3544 struct perf_event *event,
3545 struct pt_regs *regs)
3546{
3547 u64 sample_type = event->attr.sample_type;
3548
3549 data->type = sample_type;
3550
3551 header->type = PERF_RECORD_SAMPLE;
3552 header->size = sizeof(*header);
3553
3554 header->misc = 0;
3555 header->misc |= perf_misc_flags(regs);
3556
3557 if (sample_type & PERF_SAMPLE_IP) {
3558 data->ip = perf_instruction_pointer(regs);
3559
3560 header->size += sizeof(data->ip);
3561 }
3562
3563 if (sample_type & PERF_SAMPLE_TID) {
3564 /* namespace issues */
3565 data->tid_entry.pid = perf_event_pid(event, current);
3566 data->tid_entry.tid = perf_event_tid(event, current);
3567
3568 header->size += sizeof(data->tid_entry);
3569 }
3570
3571 if (sample_type & PERF_SAMPLE_TIME) {
3572 data->time = perf_clock();
3573
3574 header->size += sizeof(data->time);
3575 }
3576
3577 if (sample_type & PERF_SAMPLE_ADDR)
3578 header->size += sizeof(data->addr);
3579
3580 if (sample_type & PERF_SAMPLE_ID) {
3581 data->id = primary_event_id(event);
3582
3583 header->size += sizeof(data->id);
3584 }
3585
3586 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3587 data->stream_id = event->id;
3588
3589 header->size += sizeof(data->stream_id);
3590 }
3591
3592 if (sample_type & PERF_SAMPLE_CPU) {
3593 data->cpu_entry.cpu = raw_smp_processor_id();
3594 data->cpu_entry.reserved = 0;
3595
3596 header->size += sizeof(data->cpu_entry);
3597 }
3598
3599 if (sample_type & PERF_SAMPLE_PERIOD)
3600 header->size += sizeof(data->period);
3601
3602 if (sample_type & PERF_SAMPLE_READ)
3603 header->size += perf_event_read_size(event);
3604
3605 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3606 int size = 1;
3607
3608 data->callchain = perf_callchain(regs);
3609
3610 if (data->callchain)
3611 size += data->callchain->nr;
3612
3613 header->size += size * sizeof(u64);
3614 }
3615
3616 if (sample_type & PERF_SAMPLE_RAW) {
3617 int size = sizeof(u32);
3618
3619 if (data->raw)
3620 size += data->raw->size;
3621 else
3622 size += sizeof(u32);
3623
3624 WARN_ON_ONCE(size & (sizeof(u64)-1));
3625 header->size += size;
3626 }
3627}
3628
3629static void perf_event_output(struct perf_event *event, int nmi,
3630 struct perf_sample_data *data,
3631 struct pt_regs *regs)
3632{
3633 struct perf_output_handle handle;
3634 struct perf_event_header header;
3635
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003636 /* protect the callchain buffers */
3637 rcu_read_lock();
3638
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003639 perf_prepare_sample(&header, data, event, regs);
3640
3641 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003642 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003643
3644 perf_output_sample(&handle, &header, data, event);
3645
3646 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003647
3648exit:
3649 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003650}
3651
3652/*
3653 * read event_id
3654 */
3655
3656struct perf_read_event {
3657 struct perf_event_header header;
3658
3659 u32 pid;
3660 u32 tid;
3661};
3662
3663static void
3664perf_event_read_event(struct perf_event *event,
3665 struct task_struct *task)
3666{
3667 struct perf_output_handle handle;
3668 struct perf_read_event read_event = {
3669 .header = {
3670 .type = PERF_RECORD_READ,
3671 .misc = 0,
3672 .size = sizeof(read_event) + perf_event_read_size(event),
3673 },
3674 .pid = perf_event_pid(event, task),
3675 .tid = perf_event_tid(event, task),
3676 };
3677 int ret;
3678
3679 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3680 if (ret)
3681 return;
3682
3683 perf_output_put(&handle, read_event);
3684 perf_output_read(&handle, event);
3685
3686 perf_output_end(&handle);
3687}
3688
3689/*
3690 * task tracking -- fork/exit
3691 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003692 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003693 */
3694
3695struct perf_task_event {
3696 struct task_struct *task;
3697 struct perf_event_context *task_ctx;
3698
3699 struct {
3700 struct perf_event_header header;
3701
3702 u32 pid;
3703 u32 ppid;
3704 u32 tid;
3705 u32 ptid;
3706 u64 time;
3707 } event_id;
3708};
3709
3710static void perf_event_task_output(struct perf_event *event,
3711 struct perf_task_event *task_event)
3712{
3713 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003714 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003715 int size, ret;
3716
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717 size = task_event->event_id.header.size;
3718 ret = perf_output_begin(&handle, event, size, 0, 0);
3719
Peter Zijlstraef607772010-05-18 10:50:41 +02003720 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 return;
3722
3723 task_event->event_id.pid = perf_event_pid(event, task);
3724 task_event->event_id.ppid = perf_event_pid(event, current);
3725
3726 task_event->event_id.tid = perf_event_tid(event, task);
3727 task_event->event_id.ptid = perf_event_tid(event, current);
3728
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003729 perf_output_put(&handle, task_event->event_id);
3730
3731 perf_output_end(&handle);
3732}
3733
3734static int perf_event_task_match(struct perf_event *event)
3735{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003736 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003737 return 0;
3738
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003739 if (event->cpu != -1 && event->cpu != smp_processor_id())
3740 return 0;
3741
Eric B Munson3af9e852010-05-18 15:30:49 +01003742 if (event->attr.comm || event->attr.mmap ||
3743 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003744 return 1;
3745
3746 return 0;
3747}
3748
3749static void perf_event_task_ctx(struct perf_event_context *ctx,
3750 struct perf_task_event *task_event)
3751{
3752 struct perf_event *event;
3753
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003754 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3755 if (perf_event_task_match(event))
3756 perf_event_task_output(event, task_event);
3757 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003758}
3759
3760static void perf_event_task_event(struct perf_task_event *task_event)
3761{
3762 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003763 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003764 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003765 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003766
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003767 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003768 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003769 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003770 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003771
3772 ctx = task_event->task_ctx;
3773 if (!ctx) {
3774 ctxn = pmu->task_ctx_nr;
3775 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003776 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003777 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3778 }
3779 if (ctx)
3780 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003781next:
3782 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003783 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003784 rcu_read_unlock();
3785}
3786
3787static void perf_event_task(struct task_struct *task,
3788 struct perf_event_context *task_ctx,
3789 int new)
3790{
3791 struct perf_task_event task_event;
3792
3793 if (!atomic_read(&nr_comm_events) &&
3794 !atomic_read(&nr_mmap_events) &&
3795 !atomic_read(&nr_task_events))
3796 return;
3797
3798 task_event = (struct perf_task_event){
3799 .task = task,
3800 .task_ctx = task_ctx,
3801 .event_id = {
3802 .header = {
3803 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3804 .misc = 0,
3805 .size = sizeof(task_event.event_id),
3806 },
3807 /* .pid */
3808 /* .ppid */
3809 /* .tid */
3810 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003811 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003812 },
3813 };
3814
3815 perf_event_task_event(&task_event);
3816}
3817
3818void perf_event_fork(struct task_struct *task)
3819{
3820 perf_event_task(task, NULL, 1);
3821}
3822
3823/*
3824 * comm tracking
3825 */
3826
3827struct perf_comm_event {
3828 struct task_struct *task;
3829 char *comm;
3830 int comm_size;
3831
3832 struct {
3833 struct perf_event_header header;
3834
3835 u32 pid;
3836 u32 tid;
3837 } event_id;
3838};
3839
3840static void perf_event_comm_output(struct perf_event *event,
3841 struct perf_comm_event *comm_event)
3842{
3843 struct perf_output_handle handle;
3844 int size = comm_event->event_id.header.size;
3845 int ret = perf_output_begin(&handle, event, size, 0, 0);
3846
3847 if (ret)
3848 return;
3849
3850 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3851 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3852
3853 perf_output_put(&handle, comm_event->event_id);
3854 perf_output_copy(&handle, comm_event->comm,
3855 comm_event->comm_size);
3856 perf_output_end(&handle);
3857}
3858
3859static int perf_event_comm_match(struct perf_event *event)
3860{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003861 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003862 return 0;
3863
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003864 if (event->cpu != -1 && event->cpu != smp_processor_id())
3865 return 0;
3866
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003867 if (event->attr.comm)
3868 return 1;
3869
3870 return 0;
3871}
3872
3873static void perf_event_comm_ctx(struct perf_event_context *ctx,
3874 struct perf_comm_event *comm_event)
3875{
3876 struct perf_event *event;
3877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003878 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3879 if (perf_event_comm_match(event))
3880 perf_event_comm_output(event, comm_event);
3881 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882}
3883
3884static void perf_event_comm_event(struct perf_comm_event *comm_event)
3885{
3886 struct perf_cpu_context *cpuctx;
3887 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003889 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003890 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003891 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003892
3893 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003894 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003895 size = ALIGN(strlen(comm)+1, sizeof(u64));
3896
3897 comm_event->comm = comm;
3898 comm_event->comm_size = size;
3899
3900 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3901
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003902 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003903 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003904 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003905 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003906
3907 ctxn = pmu->task_ctx_nr;
3908 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003909 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003910
3911 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3912 if (ctx)
3913 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003914next:
3915 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003916 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003917 rcu_read_unlock();
3918}
3919
3920void perf_event_comm(struct task_struct *task)
3921{
3922 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003923 struct perf_event_context *ctx;
3924 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003926 for_each_task_context_nr(ctxn) {
3927 ctx = task->perf_event_ctxp[ctxn];
3928 if (!ctx)
3929 continue;
3930
3931 perf_event_enable_on_exec(ctx);
3932 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003933
3934 if (!atomic_read(&nr_comm_events))
3935 return;
3936
3937 comm_event = (struct perf_comm_event){
3938 .task = task,
3939 /* .comm */
3940 /* .comm_size */
3941 .event_id = {
3942 .header = {
3943 .type = PERF_RECORD_COMM,
3944 .misc = 0,
3945 /* .size */
3946 },
3947 /* .pid */
3948 /* .tid */
3949 },
3950 };
3951
3952 perf_event_comm_event(&comm_event);
3953}
3954
3955/*
3956 * mmap tracking
3957 */
3958
3959struct perf_mmap_event {
3960 struct vm_area_struct *vma;
3961
3962 const char *file_name;
3963 int file_size;
3964
3965 struct {
3966 struct perf_event_header header;
3967
3968 u32 pid;
3969 u32 tid;
3970 u64 start;
3971 u64 len;
3972 u64 pgoff;
3973 } event_id;
3974};
3975
3976static void perf_event_mmap_output(struct perf_event *event,
3977 struct perf_mmap_event *mmap_event)
3978{
3979 struct perf_output_handle handle;
3980 int size = mmap_event->event_id.header.size;
3981 int ret = perf_output_begin(&handle, event, size, 0, 0);
3982
3983 if (ret)
3984 return;
3985
3986 mmap_event->event_id.pid = perf_event_pid(event, current);
3987 mmap_event->event_id.tid = perf_event_tid(event, current);
3988
3989 perf_output_put(&handle, mmap_event->event_id);
3990 perf_output_copy(&handle, mmap_event->file_name,
3991 mmap_event->file_size);
3992 perf_output_end(&handle);
3993}
3994
3995static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003996 struct perf_mmap_event *mmap_event,
3997 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003999 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004000 return 0;
4001
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004002 if (event->cpu != -1 && event->cpu != smp_processor_id())
4003 return 0;
4004
Eric B Munson3af9e852010-05-18 15:30:49 +01004005 if ((!executable && event->attr.mmap_data) ||
4006 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004007 return 1;
4008
4009 return 0;
4010}
4011
4012static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004013 struct perf_mmap_event *mmap_event,
4014 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004015{
4016 struct perf_event *event;
4017
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004018 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004019 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004020 perf_event_mmap_output(event, mmap_event);
4021 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004022}
4023
4024static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4025{
4026 struct perf_cpu_context *cpuctx;
4027 struct perf_event_context *ctx;
4028 struct vm_area_struct *vma = mmap_event->vma;
4029 struct file *file = vma->vm_file;
4030 unsigned int size;
4031 char tmp[16];
4032 char *buf = NULL;
4033 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004034 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004035 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004036
4037 memset(tmp, 0, sizeof(tmp));
4038
4039 if (file) {
4040 /*
4041 * d_path works from the end of the buffer backwards, so we
4042 * need to add enough zero bytes after the string to handle
4043 * the 64bit alignment we do later.
4044 */
4045 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4046 if (!buf) {
4047 name = strncpy(tmp, "//enomem", sizeof(tmp));
4048 goto got_name;
4049 }
4050 name = d_path(&file->f_path, buf, PATH_MAX);
4051 if (IS_ERR(name)) {
4052 name = strncpy(tmp, "//toolong", sizeof(tmp));
4053 goto got_name;
4054 }
4055 } else {
4056 if (arch_vma_name(mmap_event->vma)) {
4057 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4058 sizeof(tmp));
4059 goto got_name;
4060 }
4061
4062 if (!vma->vm_mm) {
4063 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4064 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004065 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4066 vma->vm_end >= vma->vm_mm->brk) {
4067 name = strncpy(tmp, "[heap]", sizeof(tmp));
4068 goto got_name;
4069 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4070 vma->vm_end >= vma->vm_mm->start_stack) {
4071 name = strncpy(tmp, "[stack]", sizeof(tmp));
4072 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004073 }
4074
4075 name = strncpy(tmp, "//anon", sizeof(tmp));
4076 goto got_name;
4077 }
4078
4079got_name:
4080 size = ALIGN(strlen(name)+1, sizeof(u64));
4081
4082 mmap_event->file_name = name;
4083 mmap_event->file_size = size;
4084
4085 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4086
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004087 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004088 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004089 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004090 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4091 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004092
4093 ctxn = pmu->task_ctx_nr;
4094 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004095 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004096
4097 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4098 if (ctx) {
4099 perf_event_mmap_ctx(ctx, mmap_event,
4100 vma->vm_flags & VM_EXEC);
4101 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004102next:
4103 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004104 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004105 rcu_read_unlock();
4106
4107 kfree(buf);
4108}
4109
Eric B Munson3af9e852010-05-18 15:30:49 +01004110void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004111{
4112 struct perf_mmap_event mmap_event;
4113
4114 if (!atomic_read(&nr_mmap_events))
4115 return;
4116
4117 mmap_event = (struct perf_mmap_event){
4118 .vma = vma,
4119 /* .file_name */
4120 /* .file_size */
4121 .event_id = {
4122 .header = {
4123 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004124 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125 /* .size */
4126 },
4127 /* .pid */
4128 /* .tid */
4129 .start = vma->vm_start,
4130 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004131 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004132 },
4133 };
4134
4135 perf_event_mmap_event(&mmap_event);
4136}
4137
4138/*
4139 * IRQ throttle logging
4140 */
4141
4142static void perf_log_throttle(struct perf_event *event, int enable)
4143{
4144 struct perf_output_handle handle;
4145 int ret;
4146
4147 struct {
4148 struct perf_event_header header;
4149 u64 time;
4150 u64 id;
4151 u64 stream_id;
4152 } throttle_event = {
4153 .header = {
4154 .type = PERF_RECORD_THROTTLE,
4155 .misc = 0,
4156 .size = sizeof(throttle_event),
4157 },
4158 .time = perf_clock(),
4159 .id = primary_event_id(event),
4160 .stream_id = event->id,
4161 };
4162
4163 if (enable)
4164 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4165
4166 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4167 if (ret)
4168 return;
4169
4170 perf_output_put(&handle, throttle_event);
4171 perf_output_end(&handle);
4172}
4173
4174/*
4175 * Generic event overflow handling, sampling.
4176 */
4177
4178static int __perf_event_overflow(struct perf_event *event, int nmi,
4179 int throttle, struct perf_sample_data *data,
4180 struct pt_regs *regs)
4181{
4182 int events = atomic_read(&event->event_limit);
4183 struct hw_perf_event *hwc = &event->hw;
4184 int ret = 0;
4185
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004186 if (!throttle) {
4187 hwc->interrupts++;
4188 } else {
4189 if (hwc->interrupts != MAX_INTERRUPTS) {
4190 hwc->interrupts++;
4191 if (HZ * hwc->interrupts >
4192 (u64)sysctl_perf_event_sample_rate) {
4193 hwc->interrupts = MAX_INTERRUPTS;
4194 perf_log_throttle(event, 0);
4195 ret = 1;
4196 }
4197 } else {
4198 /*
4199 * Keep re-disabling events even though on the previous
4200 * pass we disabled it - just in case we raced with a
4201 * sched-in and the event got enabled again:
4202 */
4203 ret = 1;
4204 }
4205 }
4206
4207 if (event->attr.freq) {
4208 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004209 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210
Peter Zijlstraabd50712010-01-26 18:50:16 +01004211 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004212
Peter Zijlstraabd50712010-01-26 18:50:16 +01004213 if (delta > 0 && delta < 2*TICK_NSEC)
4214 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215 }
4216
4217 /*
4218 * XXX event_limit might not quite work as expected on inherited
4219 * events
4220 */
4221
4222 event->pending_kill = POLL_IN;
4223 if (events && atomic_dec_and_test(&event->event_limit)) {
4224 ret = 1;
4225 event->pending_kill = POLL_HUP;
4226 if (nmi) {
4227 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004228 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004229 } else
4230 perf_event_disable(event);
4231 }
4232
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004233 if (event->overflow_handler)
4234 event->overflow_handler(event, nmi, data, regs);
4235 else
4236 perf_event_output(event, nmi, data, regs);
4237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004238 return ret;
4239}
4240
4241int perf_event_overflow(struct perf_event *event, int nmi,
4242 struct perf_sample_data *data,
4243 struct pt_regs *regs)
4244{
4245 return __perf_event_overflow(event, nmi, 1, data, regs);
4246}
4247
4248/*
4249 * Generic software event infrastructure
4250 */
4251
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004252struct swevent_htable {
4253 struct swevent_hlist *swevent_hlist;
4254 struct mutex hlist_mutex;
4255 int hlist_refcount;
4256
4257 /* Recursion avoidance in each contexts */
4258 int recursion[PERF_NR_CONTEXTS];
4259};
4260
4261static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263/*
4264 * We directly increment event->count and keep a second value in
4265 * event->hw.period_left to count intervals. This period event
4266 * is kept in the range [-sample_period, 0] so that we can use the
4267 * sign as trigger.
4268 */
4269
4270static u64 perf_swevent_set_period(struct perf_event *event)
4271{
4272 struct hw_perf_event *hwc = &event->hw;
4273 u64 period = hwc->last_period;
4274 u64 nr, offset;
4275 s64 old, val;
4276
4277 hwc->last_period = hwc->sample_period;
4278
4279again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004280 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004281 if (val < 0)
4282 return 0;
4283
4284 nr = div64_u64(period + val, period);
4285 offset = nr * period;
4286 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004287 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004288 goto again;
4289
4290 return nr;
4291}
4292
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004293static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004294 int nmi, struct perf_sample_data *data,
4295 struct pt_regs *regs)
4296{
4297 struct hw_perf_event *hwc = &event->hw;
4298 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004299
4300 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004301 if (!overflow)
4302 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004303
4304 if (hwc->interrupts == MAX_INTERRUPTS)
4305 return;
4306
4307 for (; overflow; overflow--) {
4308 if (__perf_event_overflow(event, nmi, throttle,
4309 data, regs)) {
4310 /*
4311 * We inhibit the overflow from happening when
4312 * hwc->interrupts == MAX_INTERRUPTS.
4313 */
4314 break;
4315 }
4316 throttle = 1;
4317 }
4318}
4319
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004320static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004321 int nmi, struct perf_sample_data *data,
4322 struct pt_regs *regs)
4323{
4324 struct hw_perf_event *hwc = &event->hw;
4325
Peter Zijlstrae7850592010-05-21 14:43:08 +02004326 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004327
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328 if (!regs)
4329 return;
4330
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004331 if (!hwc->sample_period)
4332 return;
4333
4334 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4335 return perf_swevent_overflow(event, 1, nmi, data, regs);
4336
Peter Zijlstrae7850592010-05-21 14:43:08 +02004337 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004338 return;
4339
4340 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341}
4342
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004343static int perf_exclude_event(struct perf_event *event,
4344 struct pt_regs *regs)
4345{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004346 if (event->hw.state & PERF_HES_STOPPED)
4347 return 0;
4348
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004349 if (regs) {
4350 if (event->attr.exclude_user && user_mode(regs))
4351 return 1;
4352
4353 if (event->attr.exclude_kernel && !user_mode(regs))
4354 return 1;
4355 }
4356
4357 return 0;
4358}
4359
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360static int perf_swevent_match(struct perf_event *event,
4361 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004362 u32 event_id,
4363 struct perf_sample_data *data,
4364 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004365{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004366 if (event->attr.type != type)
4367 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004369 if (event->attr.config != event_id)
4370 return 0;
4371
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004372 if (perf_exclude_event(event, regs))
4373 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004374
4375 return 1;
4376}
4377
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004378static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004379{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004380 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004381
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004382 return hash_64(val, SWEVENT_HLIST_BITS);
4383}
4384
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004385static inline struct hlist_head *
4386__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004387{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004388 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004389
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004390 return &hlist->heads[hash];
4391}
4392
4393/* For the read side: events when they trigger */
4394static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004395find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004396{
4397 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004398
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004399 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004400 if (!hlist)
4401 return NULL;
4402
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004403 return __find_swevent_head(hlist, type, event_id);
4404}
4405
4406/* For the event head insertion and removal in the hlist */
4407static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004408find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004409{
4410 struct swevent_hlist *hlist;
4411 u32 event_id = event->attr.config;
4412 u64 type = event->attr.type;
4413
4414 /*
4415 * Event scheduling is always serialized against hlist allocation
4416 * and release. Which makes the protected version suitable here.
4417 * The context lock guarantees that.
4418 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004419 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004420 lockdep_is_held(&event->ctx->lock));
4421 if (!hlist)
4422 return NULL;
4423
4424 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004425}
4426
4427static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4428 u64 nr, int nmi,
4429 struct perf_sample_data *data,
4430 struct pt_regs *regs)
4431{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004432 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004433 struct perf_event *event;
4434 struct hlist_node *node;
4435 struct hlist_head *head;
4436
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004437 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004438 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004439 if (!head)
4440 goto end;
4441
4442 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004443 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004444 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004446end:
4447 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448}
4449
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004450int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004452 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004453
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004454 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004455}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004456EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004457
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004458void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004459{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004460 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004461
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004462 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004463}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004465void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4466 struct pt_regs *regs, u64 addr)
4467{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004468 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004469 int rctx;
4470
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004471 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004472 rctx = perf_swevent_get_recursion_context();
4473 if (rctx < 0)
4474 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004476 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004477
4478 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004479
4480 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004481 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482}
4483
4484static void perf_swevent_read(struct perf_event *event)
4485{
4486}
4487
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004488static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004489{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004490 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004491 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004492 struct hlist_head *head;
4493
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494 if (hwc->sample_period) {
4495 hwc->last_period = hwc->sample_period;
4496 perf_swevent_set_period(event);
4497 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004498
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004499 hwc->state = !(flags & PERF_EF_START);
4500
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004501 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004502 if (WARN_ON_ONCE(!head))
4503 return -EINVAL;
4504
4505 hlist_add_head_rcu(&event->hlist_entry, head);
4506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004507 return 0;
4508}
4509
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004510static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004512 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513}
4514
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004515static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004516{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004517 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004518}
4519
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004520static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004521{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004522 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004523}
4524
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004525/* Deref the hlist from the update side */
4526static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004527swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004528{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004529 return rcu_dereference_protected(swhash->swevent_hlist,
4530 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004531}
4532
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004533static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4534{
4535 struct swevent_hlist *hlist;
4536
4537 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4538 kfree(hlist);
4539}
4540
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004541static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004542{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004543 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004544
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004545 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004546 return;
4547
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004548 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004549 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4550}
4551
4552static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4553{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004554 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004555
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004556 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004557
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004558 if (!--swhash->hlist_refcount)
4559 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004560
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004561 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004562}
4563
4564static void swevent_hlist_put(struct perf_event *event)
4565{
4566 int cpu;
4567
4568 if (event->cpu != -1) {
4569 swevent_hlist_put_cpu(event, event->cpu);
4570 return;
4571 }
4572
4573 for_each_possible_cpu(cpu)
4574 swevent_hlist_put_cpu(event, cpu);
4575}
4576
4577static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4578{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004579 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004580 int err = 0;
4581
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004582 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004583
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004584 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004585 struct swevent_hlist *hlist;
4586
4587 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4588 if (!hlist) {
4589 err = -ENOMEM;
4590 goto exit;
4591 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004592 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004593 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004594 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004595exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004596 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004597
4598 return err;
4599}
4600
4601static int swevent_hlist_get(struct perf_event *event)
4602{
4603 int err;
4604 int cpu, failed_cpu;
4605
4606 if (event->cpu != -1)
4607 return swevent_hlist_get_cpu(event, event->cpu);
4608
4609 get_online_cpus();
4610 for_each_possible_cpu(cpu) {
4611 err = swevent_hlist_get_cpu(event, cpu);
4612 if (err) {
4613 failed_cpu = cpu;
4614 goto fail;
4615 }
4616 }
4617 put_online_cpus();
4618
4619 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004620fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004621 for_each_possible_cpu(cpu) {
4622 if (cpu == failed_cpu)
4623 break;
4624 swevent_hlist_put_cpu(event, cpu);
4625 }
4626
4627 put_online_cpus();
4628 return err;
4629}
4630
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004631atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004632
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004633static void sw_perf_event_destroy(struct perf_event *event)
4634{
4635 u64 event_id = event->attr.config;
4636
4637 WARN_ON(event->parent);
4638
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004639 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004640 swevent_hlist_put(event);
4641}
4642
4643static int perf_swevent_init(struct perf_event *event)
4644{
4645 int event_id = event->attr.config;
4646
4647 if (event->attr.type != PERF_TYPE_SOFTWARE)
4648 return -ENOENT;
4649
4650 switch (event_id) {
4651 case PERF_COUNT_SW_CPU_CLOCK:
4652 case PERF_COUNT_SW_TASK_CLOCK:
4653 return -ENOENT;
4654
4655 default:
4656 break;
4657 }
4658
4659 if (event_id > PERF_COUNT_SW_MAX)
4660 return -ENOENT;
4661
4662 if (!event->parent) {
4663 int err;
4664
4665 err = swevent_hlist_get(event);
4666 if (err)
4667 return err;
4668
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004669 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004670 event->destroy = sw_perf_event_destroy;
4671 }
4672
4673 return 0;
4674}
4675
4676static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004677 .task_ctx_nr = perf_sw_context,
4678
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004679 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004680 .add = perf_swevent_add,
4681 .del = perf_swevent_del,
4682 .start = perf_swevent_start,
4683 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004684 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004685};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004686
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004687#ifdef CONFIG_EVENT_TRACING
4688
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004689static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004690 struct perf_sample_data *data)
4691{
4692 void *record = data->raw->data;
4693
4694 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4695 return 1;
4696 return 0;
4697}
4698
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004699static int perf_tp_event_match(struct perf_event *event,
4700 struct perf_sample_data *data,
4701 struct pt_regs *regs)
4702{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004703 /*
4704 * All tracepoints are from kernel-space.
4705 */
4706 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004707 return 0;
4708
4709 if (!perf_tp_filter_match(event, data))
4710 return 0;
4711
4712 return 1;
4713}
4714
4715void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004716 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004717{
4718 struct perf_sample_data data;
4719 struct perf_event *event;
4720 struct hlist_node *node;
4721
4722 struct perf_raw_record raw = {
4723 .size = entry_size,
4724 .data = record,
4725 };
4726
4727 perf_sample_data_init(&data, addr);
4728 data.raw = &raw;
4729
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004730 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4731 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004732 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004733 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004734
4735 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004736}
4737EXPORT_SYMBOL_GPL(perf_tp_event);
4738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004739static void tp_perf_event_destroy(struct perf_event *event)
4740{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004741 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004742}
4743
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004744static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004745{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004746 int err;
4747
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004748 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4749 return -ENOENT;
4750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004751 /*
4752 * Raw tracepoint data is a severe data leak, only allow root to
4753 * have these.
4754 */
4755 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4756 perf_paranoid_tracepoint_raw() &&
4757 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004758 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004759
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004760 err = perf_trace_init(event);
4761 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004762 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004763
4764 event->destroy = tp_perf_event_destroy;
4765
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004766 return 0;
4767}
4768
4769static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004770 .task_ctx_nr = perf_sw_context,
4771
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004772 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004773 .add = perf_trace_add,
4774 .del = perf_trace_del,
4775 .start = perf_swevent_start,
4776 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004777 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004778};
4779
4780static inline void perf_tp_register(void)
4781{
4782 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004783}
Li Zefan6fb29152009-10-15 11:21:42 +08004784
4785static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4786{
4787 char *filter_str;
4788 int ret;
4789
4790 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4791 return -EINVAL;
4792
4793 filter_str = strndup_user(arg, PAGE_SIZE);
4794 if (IS_ERR(filter_str))
4795 return PTR_ERR(filter_str);
4796
4797 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4798
4799 kfree(filter_str);
4800 return ret;
4801}
4802
4803static void perf_event_free_filter(struct perf_event *event)
4804{
4805 ftrace_profile_free_filter(event);
4806}
4807
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004808#else
Li Zefan6fb29152009-10-15 11:21:42 +08004809
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004810static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004811{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004812}
Li Zefan6fb29152009-10-15 11:21:42 +08004813
4814static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4815{
4816 return -ENOENT;
4817}
4818
4819static void perf_event_free_filter(struct perf_event *event)
4820{
4821}
4822
Li Zefan07b139c2009-12-21 14:27:35 +08004823#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004824
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004825#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004826void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004827{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004828 struct perf_sample_data sample;
4829 struct pt_regs *regs = data;
4830
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004831 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004832
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004833 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4834 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004835}
4836#endif
4837
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004838/*
4839 * hrtimer based swevent callback
4840 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004841
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004842static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004844 enum hrtimer_restart ret = HRTIMER_RESTART;
4845 struct perf_sample_data data;
4846 struct pt_regs *regs;
4847 struct perf_event *event;
4848 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004849
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004850 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4851 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004852
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004853 perf_sample_data_init(&data, 0);
4854 data.period = event->hw.last_period;
4855 regs = get_irq_regs();
4856
4857 if (regs && !perf_exclude_event(event, regs)) {
4858 if (!(event->attr.exclude_idle && current->pid == 0))
4859 if (perf_event_overflow(event, 0, &data, regs))
4860 ret = HRTIMER_NORESTART;
4861 }
4862
4863 period = max_t(u64, 10000, event->hw.sample_period);
4864 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4865
4866 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867}
4868
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004869static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004871 struct hw_perf_event *hwc = &event->hw;
4872
4873 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4874 hwc->hrtimer.function = perf_swevent_hrtimer;
4875 if (hwc->sample_period) {
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004876 s64 period = local64_read(&hwc->period_left);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004877
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004878 if (period) {
4879 if (period < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004880 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004881
4882 local64_set(&hwc->period_left, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004883 } else {
4884 period = max_t(u64, 10000, hwc->sample_period);
4885 }
4886 __hrtimer_start_range_ns(&hwc->hrtimer,
4887 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02004888 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004889 }
4890}
4891
4892static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4893{
4894 struct hw_perf_event *hwc = &event->hw;
4895
4896 if (hwc->sample_period) {
4897 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004898 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004899
4900 hrtimer_cancel(&hwc->hrtimer);
4901 }
4902}
4903
4904/*
4905 * Software event: cpu wall time clock
4906 */
4907
4908static void cpu_clock_event_update(struct perf_event *event)
4909{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004910 s64 prev;
4911 u64 now;
4912
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004913 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004914 prev = local64_xchg(&event->hw.prev_count, now);
4915 local64_add(now - prev, &event->count);
4916}
4917
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004918static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004919{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004920 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004921 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004922}
4923
4924static void cpu_clock_event_stop(struct perf_event *event, int flags)
4925{
4926 perf_swevent_cancel_hrtimer(event);
4927 cpu_clock_event_update(event);
4928}
4929
4930static int cpu_clock_event_add(struct perf_event *event, int flags)
4931{
4932 if (flags & PERF_EF_START)
4933 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004934
4935 return 0;
4936}
4937
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004938static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004939{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004940 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004941}
4942
4943static void cpu_clock_event_read(struct perf_event *event)
4944{
4945 cpu_clock_event_update(event);
4946}
4947
4948static int cpu_clock_event_init(struct perf_event *event)
4949{
4950 if (event->attr.type != PERF_TYPE_SOFTWARE)
4951 return -ENOENT;
4952
4953 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4954 return -ENOENT;
4955
4956 return 0;
4957}
4958
4959static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004960 .task_ctx_nr = perf_sw_context,
4961
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004962 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004963 .add = cpu_clock_event_add,
4964 .del = cpu_clock_event_del,
4965 .start = cpu_clock_event_start,
4966 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004967 .read = cpu_clock_event_read,
4968};
4969
4970/*
4971 * Software event: task time clock
4972 */
4973
4974static void task_clock_event_update(struct perf_event *event, u64 now)
4975{
4976 u64 prev;
4977 s64 delta;
4978
4979 prev = local64_xchg(&event->hw.prev_count, now);
4980 delta = now - prev;
4981 local64_add(delta, &event->count);
4982}
4983
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004984static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004985{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004986 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004987 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004988}
4989
4990static void task_clock_event_stop(struct perf_event *event, int flags)
4991{
4992 perf_swevent_cancel_hrtimer(event);
4993 task_clock_event_update(event, event->ctx->time);
4994}
4995
4996static int task_clock_event_add(struct perf_event *event, int flags)
4997{
4998 if (flags & PERF_EF_START)
4999 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005000
5001 return 0;
5002}
5003
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005004static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005005{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005006 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005007}
5008
5009static void task_clock_event_read(struct perf_event *event)
5010{
5011 u64 time;
5012
5013 if (!in_nmi()) {
5014 update_context_time(event->ctx);
5015 time = event->ctx->time;
5016 } else {
5017 u64 now = perf_clock();
5018 u64 delta = now - event->ctx->timestamp;
5019 time = event->ctx->time + delta;
5020 }
5021
5022 task_clock_event_update(event, time);
5023}
5024
5025static int task_clock_event_init(struct perf_event *event)
5026{
5027 if (event->attr.type != PERF_TYPE_SOFTWARE)
5028 return -ENOENT;
5029
5030 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5031 return -ENOENT;
5032
5033 return 0;
5034}
5035
5036static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005037 .task_ctx_nr = perf_sw_context,
5038
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005039 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005040 .add = task_clock_event_add,
5041 .del = task_clock_event_del,
5042 .start = task_clock_event_start,
5043 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005044 .read = task_clock_event_read,
5045};
5046
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005047static void perf_pmu_nop_void(struct pmu *pmu)
5048{
5049}
5050
5051static int perf_pmu_nop_int(struct pmu *pmu)
5052{
5053 return 0;
5054}
5055
5056static void perf_pmu_start_txn(struct pmu *pmu)
5057{
5058 perf_pmu_disable(pmu);
5059}
5060
5061static int perf_pmu_commit_txn(struct pmu *pmu)
5062{
5063 perf_pmu_enable(pmu);
5064 return 0;
5065}
5066
5067static void perf_pmu_cancel_txn(struct pmu *pmu)
5068{
5069 perf_pmu_enable(pmu);
5070}
5071
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005072/*
5073 * Ensures all contexts with the same task_ctx_nr have the same
5074 * pmu_cpu_context too.
5075 */
5076static void *find_pmu_context(int ctxn)
5077{
5078 struct pmu *pmu;
5079
5080 if (ctxn < 0)
5081 return NULL;
5082
5083 list_for_each_entry(pmu, &pmus, entry) {
5084 if (pmu->task_ctx_nr == ctxn)
5085 return pmu->pmu_cpu_context;
5086 }
5087
5088 return NULL;
5089}
5090
5091static void free_pmu_context(void * __percpu cpu_context)
5092{
5093 struct pmu *pmu;
5094
5095 mutex_lock(&pmus_lock);
5096 /*
5097 * Like a real lame refcount.
5098 */
5099 list_for_each_entry(pmu, &pmus, entry) {
5100 if (pmu->pmu_cpu_context == cpu_context)
5101 goto out;
5102 }
5103
5104 free_percpu(cpu_context);
5105out:
5106 mutex_unlock(&pmus_lock);
5107}
5108
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005109int perf_pmu_register(struct pmu *pmu)
5110{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005111 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005112
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005113 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005114 ret = -ENOMEM;
5115 pmu->pmu_disable_count = alloc_percpu(int);
5116 if (!pmu->pmu_disable_count)
5117 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005118
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005119 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5120 if (pmu->pmu_cpu_context)
5121 goto got_cpu_context;
5122
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005123 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5124 if (!pmu->pmu_cpu_context)
5125 goto free_pdc;
5126
5127 for_each_possible_cpu(cpu) {
5128 struct perf_cpu_context *cpuctx;
5129
5130 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005131 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005132 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005133 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005134 cpuctx->jiffies_interval = 1;
5135 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005136 }
5137
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005138got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005139 if (!pmu->start_txn) {
5140 if (pmu->pmu_enable) {
5141 /*
5142 * If we have pmu_enable/pmu_disable calls, install
5143 * transaction stubs that use that to try and batch
5144 * hardware accesses.
5145 */
5146 pmu->start_txn = perf_pmu_start_txn;
5147 pmu->commit_txn = perf_pmu_commit_txn;
5148 pmu->cancel_txn = perf_pmu_cancel_txn;
5149 } else {
5150 pmu->start_txn = perf_pmu_nop_void;
5151 pmu->commit_txn = perf_pmu_nop_int;
5152 pmu->cancel_txn = perf_pmu_nop_void;
5153 }
5154 }
5155
5156 if (!pmu->pmu_enable) {
5157 pmu->pmu_enable = perf_pmu_nop_void;
5158 pmu->pmu_disable = perf_pmu_nop_void;
5159 }
5160
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005161 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005162 ret = 0;
5163unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005164 mutex_unlock(&pmus_lock);
5165
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005166 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005167
5168free_pdc:
5169 free_percpu(pmu->pmu_disable_count);
5170 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005171}
5172
5173void perf_pmu_unregister(struct pmu *pmu)
5174{
5175 mutex_lock(&pmus_lock);
5176 list_del_rcu(&pmu->entry);
5177 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005178
5179 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005180 * We dereference the pmu list under both SRCU and regular RCU, so
5181 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005183 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005184 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005185
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005186 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005187 free_pmu_context(pmu->pmu_cpu_context);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005188}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005189
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005190struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005191{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005192 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005193 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005194
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005195 idx = srcu_read_lock(&pmus_srcu);
5196 list_for_each_entry_rcu(pmu, &pmus, entry) {
5197 int ret = pmu->event_init(event);
5198 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005199 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005200
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005201 if (ret != -ENOENT) {
5202 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005203 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005204 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005206 pmu = ERR_PTR(-ENOENT);
5207unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005208 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209
5210 return pmu;
5211}
5212
5213/*
5214 * Allocate and initialize a event structure
5215 */
5216static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005217perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005218 struct task_struct *task,
5219 struct perf_event *group_leader,
5220 struct perf_event *parent_event,
5221 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005223 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005224 struct perf_event *event;
5225 struct hw_perf_event *hwc;
5226 long err;
5227
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005228 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229 if (!event)
5230 return ERR_PTR(-ENOMEM);
5231
5232 /*
5233 * Single events are their own group leaders, with an
5234 * empty sibling list:
5235 */
5236 if (!group_leader)
5237 group_leader = event;
5238
5239 mutex_init(&event->child_mutex);
5240 INIT_LIST_HEAD(&event->child_list);
5241
5242 INIT_LIST_HEAD(&event->group_entry);
5243 INIT_LIST_HEAD(&event->event_entry);
5244 INIT_LIST_HEAD(&event->sibling_list);
5245 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005246 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247
5248 mutex_init(&event->mmap_mutex);
5249
5250 event->cpu = cpu;
5251 event->attr = *attr;
5252 event->group_leader = group_leader;
5253 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254 event->oncpu = -1;
5255
5256 event->parent = parent_event;
5257
5258 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5259 event->id = atomic64_inc_return(&perf_event_id);
5260
5261 event->state = PERF_EVENT_STATE_INACTIVE;
5262
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005263 if (task) {
5264 event->attach_state = PERF_ATTACH_TASK;
5265#ifdef CONFIG_HAVE_HW_BREAKPOINT
5266 /*
5267 * hw_breakpoint is a bit difficult here..
5268 */
5269 if (attr->type == PERF_TYPE_BREAKPOINT)
5270 event->hw.bp_target = task;
5271#endif
5272 }
5273
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005274 if (!overflow_handler && parent_event)
5275 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005276
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005277 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005279 if (attr->disabled)
5280 event->state = PERF_EVENT_STATE_OFF;
5281
5282 pmu = NULL;
5283
5284 hwc = &event->hw;
5285 hwc->sample_period = attr->sample_period;
5286 if (attr->freq && attr->sample_freq)
5287 hwc->sample_period = 1;
5288 hwc->last_period = hwc->sample_period;
5289
Peter Zijlstrae7850592010-05-21 14:43:08 +02005290 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291
5292 /*
5293 * we currently do not support PERF_FORMAT_GROUP on inherited events
5294 */
5295 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5296 goto done;
5297
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005298 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005300done:
5301 err = 0;
5302 if (!pmu)
5303 err = -EINVAL;
5304 else if (IS_ERR(pmu))
5305 err = PTR_ERR(pmu);
5306
5307 if (err) {
5308 if (event->ns)
5309 put_pid_ns(event->ns);
5310 kfree(event);
5311 return ERR_PTR(err);
5312 }
5313
5314 event->pmu = pmu;
5315
5316 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005317 if (event->attach_state & PERF_ATTACH_TASK)
5318 jump_label_inc(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005319 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005320 atomic_inc(&nr_mmap_events);
5321 if (event->attr.comm)
5322 atomic_inc(&nr_comm_events);
5323 if (event->attr.task)
5324 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005325 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5326 err = get_callchain_buffers();
5327 if (err) {
5328 free_event(event);
5329 return ERR_PTR(err);
5330 }
5331 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005332 }
5333
5334 return event;
5335}
5336
5337static int perf_copy_attr(struct perf_event_attr __user *uattr,
5338 struct perf_event_attr *attr)
5339{
5340 u32 size;
5341 int ret;
5342
5343 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5344 return -EFAULT;
5345
5346 /*
5347 * zero the full structure, so that a short copy will be nice.
5348 */
5349 memset(attr, 0, sizeof(*attr));
5350
5351 ret = get_user(size, &uattr->size);
5352 if (ret)
5353 return ret;
5354
5355 if (size > PAGE_SIZE) /* silly large */
5356 goto err_size;
5357
5358 if (!size) /* abi compat */
5359 size = PERF_ATTR_SIZE_VER0;
5360
5361 if (size < PERF_ATTR_SIZE_VER0)
5362 goto err_size;
5363
5364 /*
5365 * If we're handed a bigger struct than we know of,
5366 * ensure all the unknown bits are 0 - i.e. new
5367 * user-space does not rely on any kernel feature
5368 * extensions we dont know about yet.
5369 */
5370 if (size > sizeof(*attr)) {
5371 unsigned char __user *addr;
5372 unsigned char __user *end;
5373 unsigned char val;
5374
5375 addr = (void __user *)uattr + sizeof(*attr);
5376 end = (void __user *)uattr + size;
5377
5378 for (; addr < end; addr++) {
5379 ret = get_user(val, addr);
5380 if (ret)
5381 return ret;
5382 if (val)
5383 goto err_size;
5384 }
5385 size = sizeof(*attr);
5386 }
5387
5388 ret = copy_from_user(attr, uattr, size);
5389 if (ret)
5390 return -EFAULT;
5391
5392 /*
5393 * If the type exists, the corresponding creation will verify
5394 * the attr->config.
5395 */
5396 if (attr->type >= PERF_TYPE_MAX)
5397 return -EINVAL;
5398
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305399 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005400 return -EINVAL;
5401
5402 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5403 return -EINVAL;
5404
5405 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5406 return -EINVAL;
5407
5408out:
5409 return ret;
5410
5411err_size:
5412 put_user(sizeof(*attr), &uattr->size);
5413 ret = -E2BIG;
5414 goto out;
5415}
5416
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005417static int
5418perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005419{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005420 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005421 int ret = -EINVAL;
5422
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005423 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005424 goto set;
5425
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005426 /* don't allow circular references */
5427 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428 goto out;
5429
Peter Zijlstra0f139302010-05-20 14:35:15 +02005430 /*
5431 * Don't allow cross-cpu buffers
5432 */
5433 if (output_event->cpu != event->cpu)
5434 goto out;
5435
5436 /*
5437 * If its not a per-cpu buffer, it must be the same task.
5438 */
5439 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5440 goto out;
5441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442set:
5443 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005444 /* Can't redirect output if we've got an active mmap() */
5445 if (atomic_read(&event->mmap_count))
5446 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005447
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005448 if (output_event) {
5449 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005450 buffer = perf_buffer_get(output_event);
5451 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005452 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005453 }
5454
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005455 old_buffer = event->buffer;
5456 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005457 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005458unlock:
5459 mutex_unlock(&event->mmap_mutex);
5460
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005461 if (old_buffer)
5462 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005463out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005464 return ret;
5465}
5466
5467/**
5468 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5469 *
5470 * @attr_uptr: event_id type attributes for monitoring/sampling
5471 * @pid: target pid
5472 * @cpu: target cpu
5473 * @group_fd: group leader event fd
5474 */
5475SYSCALL_DEFINE5(perf_event_open,
5476 struct perf_event_attr __user *, attr_uptr,
5477 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5478{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005479 struct perf_event *group_leader = NULL, *output_event = NULL;
5480 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481 struct perf_event_attr attr;
5482 struct perf_event_context *ctx;
5483 struct file *event_file = NULL;
5484 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07005485 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005486 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005487 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005488 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005489 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005490 int err;
5491
5492 /* for future expandability... */
5493 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5494 return -EINVAL;
5495
5496 err = perf_copy_attr(attr_uptr, &attr);
5497 if (err)
5498 return err;
5499
5500 if (!attr.exclude_kernel) {
5501 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5502 return -EACCES;
5503 }
5504
5505 if (attr.freq) {
5506 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5507 return -EINVAL;
5508 }
5509
Al Viroea635c62010-05-26 17:40:29 -04005510 event_fd = get_unused_fd_flags(O_RDWR);
5511 if (event_fd < 0)
5512 return event_fd;
5513
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005514 if (group_fd != -1) {
5515 group_leader = perf_fget_light(group_fd, &fput_needed);
5516 if (IS_ERR(group_leader)) {
5517 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005518 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005519 }
5520 group_file = group_leader->filp;
5521 if (flags & PERF_FLAG_FD_OUTPUT)
5522 output_event = group_leader;
5523 if (flags & PERF_FLAG_FD_NO_GROUP)
5524 group_leader = NULL;
5525 }
5526
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005527 if (pid != -1) {
5528 task = find_lively_task_by_vpid(pid);
5529 if (IS_ERR(task)) {
5530 err = PTR_ERR(task);
5531 goto err_group_fd;
5532 }
5533 }
5534
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005535 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005536 if (IS_ERR(event)) {
5537 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005538 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005539 }
5540
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005541 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005542 * Special case software events and allow them to be part of
5543 * any hardware group.
5544 */
5545 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005546
5547 if (group_leader &&
5548 (is_software_event(event) != is_software_event(group_leader))) {
5549 if (is_software_event(event)) {
5550 /*
5551 * If event and group_leader are not both a software
5552 * event, and event is, then group leader is not.
5553 *
5554 * Allow the addition of software events to !software
5555 * groups, this is safe because software events never
5556 * fail to schedule.
5557 */
5558 pmu = group_leader->pmu;
5559 } else if (is_software_event(group_leader) &&
5560 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5561 /*
5562 * In case the group is a pure software group, and we
5563 * try to add a hardware event, move the whole group to
5564 * the hardware context.
5565 */
5566 move_group = 1;
5567 }
5568 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005569
5570 /*
5571 * Get the target context (task or percpu):
5572 */
Matt Helsley38a81da2010-09-13 13:01:20 -07005573 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005574 if (IS_ERR(ctx)) {
5575 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005576 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005577 }
5578
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005579 /*
5580 * Look up the group leader (we will attach this event to it):
5581 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005582 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005583 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005585 /*
5586 * Do not allow a recursive hierarchy (this new sibling
5587 * becoming part of another group-sibling):
5588 */
5589 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005590 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005591 /*
5592 * Do not allow to attach to a group in a different
5593 * task or CPU context:
5594 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005595 if (move_group) {
5596 if (group_leader->ctx->type != ctx->type)
5597 goto err_context;
5598 } else {
5599 if (group_leader->ctx != ctx)
5600 goto err_context;
5601 }
5602
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005603 /*
5604 * Only a group leader can be exclusive or pinned
5605 */
5606 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005607 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005608 }
5609
5610 if (output_event) {
5611 err = perf_event_set_output(event, output_event);
5612 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005613 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005614 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005615
Al Viroea635c62010-05-26 17:40:29 -04005616 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5617 if (IS_ERR(event_file)) {
5618 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005619 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005620 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005621
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005622 if (move_group) {
5623 struct perf_event_context *gctx = group_leader->ctx;
5624
5625 mutex_lock(&gctx->mutex);
5626 perf_event_remove_from_context(group_leader);
5627 list_for_each_entry(sibling, &group_leader->sibling_list,
5628 group_entry) {
5629 perf_event_remove_from_context(sibling);
5630 put_ctx(gctx);
5631 }
5632 mutex_unlock(&gctx->mutex);
5633 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005634 }
5635
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005636 event->filp = event_file;
5637 WARN_ON_ONCE(ctx->parent_ctx);
5638 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005639
5640 if (move_group) {
5641 perf_install_in_context(ctx, group_leader, cpu);
5642 get_ctx(ctx);
5643 list_for_each_entry(sibling, &group_leader->sibling_list,
5644 group_entry) {
5645 perf_install_in_context(ctx, sibling, cpu);
5646 get_ctx(ctx);
5647 }
5648 }
5649
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005650 perf_install_in_context(ctx, event, cpu);
5651 ++ctx->generation;
5652 mutex_unlock(&ctx->mutex);
5653
5654 event->owner = current;
5655 get_task_struct(current);
5656 mutex_lock(&current->perf_event_mutex);
5657 list_add_tail(&event->owner_entry, &current->perf_event_list);
5658 mutex_unlock(&current->perf_event_mutex);
5659
Peter Zijlstra8a495422010-05-27 15:47:49 +02005660 /*
5661 * Drop the reference on the group_event after placing the
5662 * new event on the sibling_list. This ensures destruction
5663 * of the group leader will find the pointer to itself in
5664 * perf_group_detach().
5665 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005666 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005667 fd_install(event_fd, event_file);
5668 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005669
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005670err_context:
Al Viroea635c62010-05-26 17:40:29 -04005671 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005672err_alloc:
5673 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02005674err_task:
5675 if (task)
5676 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005677err_group_fd:
5678 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005679err_fd:
5680 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005681 return err;
5682}
5683
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005684/**
5685 * perf_event_create_kernel_counter
5686 *
5687 * @attr: attributes of the counter to create
5688 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07005689 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005690 */
5691struct perf_event *
5692perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07005693 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005694 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005695{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005696 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005697 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005698 int err;
5699
5700 /*
5701 * Get the target context (task or percpu):
5702 */
5703
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005704 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005705 if (IS_ERR(event)) {
5706 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005707 goto err;
5708 }
5709
Matt Helsley38a81da2010-09-13 13:01:20 -07005710 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005711 if (IS_ERR(ctx)) {
5712 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005713 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005714 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005715
5716 event->filp = NULL;
5717 WARN_ON_ONCE(ctx->parent_ctx);
5718 mutex_lock(&ctx->mutex);
5719 perf_install_in_context(ctx, event, cpu);
5720 ++ctx->generation;
5721 mutex_unlock(&ctx->mutex);
5722
5723 event->owner = current;
5724 get_task_struct(current);
5725 mutex_lock(&current->perf_event_mutex);
5726 list_add_tail(&event->owner_entry, &current->perf_event_list);
5727 mutex_unlock(&current->perf_event_mutex);
5728
5729 return event;
5730
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005731err_free:
5732 free_event(event);
5733err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005734 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005735}
5736EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5737
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005738static void sync_child_event(struct perf_event *child_event,
5739 struct task_struct *child)
5740{
5741 struct perf_event *parent_event = child_event->parent;
5742 u64 child_val;
5743
5744 if (child_event->attr.inherit_stat)
5745 perf_event_read_event(child_event, child);
5746
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005747 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005748
5749 /*
5750 * Add back the child's count to the parent's count:
5751 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005752 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753 atomic64_add(child_event->total_time_enabled,
5754 &parent_event->child_total_time_enabled);
5755 atomic64_add(child_event->total_time_running,
5756 &parent_event->child_total_time_running);
5757
5758 /*
5759 * Remove this event from the parent's list
5760 */
5761 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5762 mutex_lock(&parent_event->child_mutex);
5763 list_del_init(&child_event->child_list);
5764 mutex_unlock(&parent_event->child_mutex);
5765
5766 /*
5767 * Release the parent event, if this was the last
5768 * reference to it.
5769 */
5770 fput(parent_event->filp);
5771}
5772
5773static void
5774__perf_event_exit_task(struct perf_event *child_event,
5775 struct perf_event_context *child_ctx,
5776 struct task_struct *child)
5777{
5778 struct perf_event *parent_event;
5779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005780 perf_event_remove_from_context(child_event);
5781
5782 parent_event = child_event->parent;
5783 /*
5784 * It can happen that parent exits first, and has events
5785 * that are still around due to the child reference. These
5786 * events need to be zapped - but otherwise linger.
5787 */
5788 if (parent_event) {
5789 sync_child_event(child_event, child);
5790 free_event(child_event);
5791 }
5792}
5793
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005794static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795{
5796 struct perf_event *child_event, *tmp;
5797 struct perf_event_context *child_ctx;
5798 unsigned long flags;
5799
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005800 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005801 perf_event_task(child, NULL, 0);
5802 return;
5803 }
5804
5805 local_irq_save(flags);
5806 /*
5807 * We can't reschedule here because interrupts are disabled,
5808 * and either child is current or it is a task that can't be
5809 * scheduled, so we are now safe from rescheduling changing
5810 * our context.
5811 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005812 child_ctx = child->perf_event_ctxp[ctxn];
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005813 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005814
5815 /*
5816 * Take the context lock here so that if find_get_context is
5817 * reading child->perf_event_ctxp, we wait until it has
5818 * incremented the context's refcount before we do put_ctx below.
5819 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005820 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005821 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822 /*
5823 * If this context is a clone; unclone it so it can't get
5824 * swapped to another process while we're removing all
5825 * the events from it.
5826 */
5827 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005828 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005829 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005830
5831 /*
5832 * Report the task dead after unscheduling the events so that we
5833 * won't get any samples after PERF_RECORD_EXIT. We can however still
5834 * get a few PERF_RECORD_READ events.
5835 */
5836 perf_event_task(child, child_ctx, 0);
5837
5838 /*
5839 * We can recurse on the same lock type through:
5840 *
5841 * __perf_event_exit_task()
5842 * sync_child_event()
5843 * fput(parent_event->filp)
5844 * perf_release()
5845 * mutex_lock(&ctx->mutex)
5846 *
5847 * But since its the parent context it won't be the same instance.
5848 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005849 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005850
5851again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005852 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5853 group_entry)
5854 __perf_event_exit_task(child_event, child_ctx, child);
5855
5856 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005857 group_entry)
5858 __perf_event_exit_task(child_event, child_ctx, child);
5859
5860 /*
5861 * If the last event was a group event, it will have appended all
5862 * its siblings to the list, but we obtained 'tmp' before that which
5863 * will still point to the list head terminating the iteration.
5864 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005865 if (!list_empty(&child_ctx->pinned_groups) ||
5866 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005867 goto again;
5868
5869 mutex_unlock(&child_ctx->mutex);
5870
5871 put_ctx(child_ctx);
5872}
5873
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005874/*
5875 * When a child task exits, feed back event values to parent events.
5876 */
5877void perf_event_exit_task(struct task_struct *child)
5878{
5879 int ctxn;
5880
5881 for_each_task_context_nr(ctxn)
5882 perf_event_exit_task_context(child, ctxn);
5883}
5884
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005885static void perf_free_event(struct perf_event *event,
5886 struct perf_event_context *ctx)
5887{
5888 struct perf_event *parent = event->parent;
5889
5890 if (WARN_ON_ONCE(!parent))
5891 return;
5892
5893 mutex_lock(&parent->child_mutex);
5894 list_del_init(&event->child_list);
5895 mutex_unlock(&parent->child_mutex);
5896
5897 fput(parent->filp);
5898
Peter Zijlstra8a495422010-05-27 15:47:49 +02005899 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005900 list_del_event(event, ctx);
5901 free_event(event);
5902}
5903
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904/*
5905 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005906 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005907 */
5908void perf_event_free_task(struct task_struct *task)
5909{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005910 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005911 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005912 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005913
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005914 for_each_task_context_nr(ctxn) {
5915 ctx = task->perf_event_ctxp[ctxn];
5916 if (!ctx)
5917 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005918
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005919 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005920again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005921 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5922 group_entry)
5923 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005924
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005925 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5926 group_entry)
5927 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005929 if (!list_empty(&ctx->pinned_groups) ||
5930 !list_empty(&ctx->flexible_groups))
5931 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005932
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005933 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005934
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005935 put_ctx(ctx);
5936 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005937}
5938
Peter Zijlstra4e231c72010-09-09 21:01:59 +02005939void perf_event_delayed_put(struct task_struct *task)
5940{
5941 int ctxn;
5942
5943 for_each_task_context_nr(ctxn)
5944 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5945}
5946
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005947/*
5948 * inherit a event from parent task to child task:
5949 */
5950static struct perf_event *
5951inherit_event(struct perf_event *parent_event,
5952 struct task_struct *parent,
5953 struct perf_event_context *parent_ctx,
5954 struct task_struct *child,
5955 struct perf_event *group_leader,
5956 struct perf_event_context *child_ctx)
5957{
5958 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02005959 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005960
5961 /*
5962 * Instead of creating recursive hierarchies of events,
5963 * we link inherited events back to the original parent,
5964 * which has a filp for sure, which we use as the reference
5965 * count:
5966 */
5967 if (parent_event->parent)
5968 parent_event = parent_event->parent;
5969
5970 child_event = perf_event_alloc(&parent_event->attr,
5971 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005972 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005973 group_leader, parent_event,
5974 NULL);
5975 if (IS_ERR(child_event))
5976 return child_event;
5977 get_ctx(child_ctx);
5978
5979 /*
5980 * Make the child state follow the state of the parent event,
5981 * not its attr.disabled bit. We hold the parent's mutex,
5982 * so we won't race with perf_event_{en, dis}able_family.
5983 */
5984 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5985 child_event->state = PERF_EVENT_STATE_INACTIVE;
5986 else
5987 child_event->state = PERF_EVENT_STATE_OFF;
5988
5989 if (parent_event->attr.freq) {
5990 u64 sample_period = parent_event->hw.sample_period;
5991 struct hw_perf_event *hwc = &child_event->hw;
5992
5993 hwc->sample_period = sample_period;
5994 hwc->last_period = sample_period;
5995
5996 local64_set(&hwc->period_left, sample_period);
5997 }
5998
5999 child_event->ctx = child_ctx;
6000 child_event->overflow_handler = parent_event->overflow_handler;
6001
6002 /*
6003 * Link it up in the child's context:
6004 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006005 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006006 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006007 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006008
6009 /*
6010 * Get a reference to the parent filp - we will fput it
6011 * when the child event exits. This is safe to do because
6012 * we are in the parent and we know that the filp still
6013 * exists and has a nonzero count:
6014 */
6015 atomic_long_inc(&parent_event->filp->f_count);
6016
6017 /*
6018 * Link this into the parent event's child list
6019 */
6020 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6021 mutex_lock(&parent_event->child_mutex);
6022 list_add_tail(&child_event->child_list, &parent_event->child_list);
6023 mutex_unlock(&parent_event->child_mutex);
6024
6025 return child_event;
6026}
6027
6028static int inherit_group(struct perf_event *parent_event,
6029 struct task_struct *parent,
6030 struct perf_event_context *parent_ctx,
6031 struct task_struct *child,
6032 struct perf_event_context *child_ctx)
6033{
6034 struct perf_event *leader;
6035 struct perf_event *sub;
6036 struct perf_event *child_ctr;
6037
6038 leader = inherit_event(parent_event, parent, parent_ctx,
6039 child, NULL, child_ctx);
6040 if (IS_ERR(leader))
6041 return PTR_ERR(leader);
6042 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6043 child_ctr = inherit_event(sub, parent, parent_ctx,
6044 child, leader, child_ctx);
6045 if (IS_ERR(child_ctr))
6046 return PTR_ERR(child_ctr);
6047 }
6048 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006049}
6050
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006051static int
6052inherit_task_group(struct perf_event *event, struct task_struct *parent,
6053 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006054 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006055 int *inherited_all)
6056{
6057 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006058 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006059
6060 if (!event->attr.inherit) {
6061 *inherited_all = 0;
6062 return 0;
6063 }
6064
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006065 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006066 if (!child_ctx) {
6067 /*
6068 * This is executed from the parent task context, so
6069 * inherit events that have been marked for cloning.
6070 * First allocate and initialize a context for the
6071 * child.
6072 */
6073
Peter Zijlstraeb184472010-09-07 15:55:13 +02006074 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006075 if (!child_ctx)
6076 return -ENOMEM;
6077
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006078 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006079 }
6080
6081 ret = inherit_group(event, parent, parent_ctx,
6082 child, child_ctx);
6083
6084 if (ret)
6085 *inherited_all = 0;
6086
6087 return ret;
6088}
6089
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006090/*
6091 * Initialize the perf_event context in task_struct
6092 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006093int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006094{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006095 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096 struct perf_event_context *cloned_ctx;
6097 struct perf_event *event;
6098 struct task_struct *parent = current;
6099 int inherited_all = 1;
6100 int ret = 0;
6101
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006102 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103
6104 mutex_init(&child->perf_event_mutex);
6105 INIT_LIST_HEAD(&child->perf_event_list);
6106
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006107 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006108 return 0;
6109
6110 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006111 * If the parent's context is a clone, pin it so it won't get
6112 * swapped under us.
6113 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006114 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006115
6116 /*
6117 * No need to check if parent_ctx != NULL here; since we saw
6118 * it non-NULL earlier, the only reason for it to become NULL
6119 * is if we exit, and since we're currently in the middle of
6120 * a fork we can't be exiting at the same time.
6121 */
6122
6123 /*
6124 * Lock the parent list. No need to lock the child - not PID
6125 * hashed yet and not running, so nobody can access it.
6126 */
6127 mutex_lock(&parent_ctx->mutex);
6128
6129 /*
6130 * We dont have to disable NMIs - we are only looking at
6131 * the list, not manipulating it:
6132 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006133 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006134 ret = inherit_task_group(event, parent, parent_ctx,
6135 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006136 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006138 }
6139
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006140 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006141 ret = inherit_task_group(event, parent, parent_ctx,
6142 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006143 if (ret)
6144 break;
6145 }
6146
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006147 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006148
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006149 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150 /*
6151 * Mark the child context as a clone of the parent
6152 * context, or of whatever the parent is a clone of.
6153 * Note that if the parent is a clone, it could get
6154 * uncloned at any point, but that doesn't matter
6155 * because the list of events and the generation
6156 * count can't have changed since we took the mutex.
6157 */
6158 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6159 if (cloned_ctx) {
6160 child_ctx->parent_ctx = cloned_ctx;
6161 child_ctx->parent_gen = parent_ctx->parent_gen;
6162 } else {
6163 child_ctx->parent_ctx = parent_ctx;
6164 child_ctx->parent_gen = parent_ctx->generation;
6165 }
6166 get_ctx(child_ctx->parent_ctx);
6167 }
6168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006169 mutex_unlock(&parent_ctx->mutex);
6170
6171 perf_unpin_context(parent_ctx);
6172
6173 return ret;
6174}
6175
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006176/*
6177 * Initialize the perf_event context in task_struct
6178 */
6179int perf_event_init_task(struct task_struct *child)
6180{
6181 int ctxn, ret;
6182
6183 for_each_task_context_nr(ctxn) {
6184 ret = perf_event_init_context(child, ctxn);
6185 if (ret)
6186 return ret;
6187 }
6188
6189 return 0;
6190}
6191
Paul Mackerras220b1402010-03-10 20:45:52 +11006192static void __init perf_event_init_all_cpus(void)
6193{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006194 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006195 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006196
6197 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006198 swhash = &per_cpu(swevent_htable, cpu);
6199 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006200 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006201 }
6202}
6203
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006204static void __cpuinit perf_event_init_cpu(int cpu)
6205{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006206 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006207
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006208 mutex_lock(&swhash->hlist_mutex);
6209 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006210 struct swevent_hlist *hlist;
6211
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006212 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6213 WARN_ON(!hlist);
6214 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006215 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006216 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006217}
6218
6219#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006220static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006221{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006222 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6223
6224 WARN_ON(!irqs_disabled());
6225
6226 list_del_init(&cpuctx->rotation_list);
6227}
6228
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006229static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006230{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006231 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006232 struct perf_event *event, *tmp;
6233
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006234 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006235
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006236 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6237 __perf_event_remove_from_context(event);
6238 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006239 __perf_event_remove_from_context(event);
6240}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006241
6242static void perf_event_exit_cpu_context(int cpu)
6243{
6244 struct perf_event_context *ctx;
6245 struct pmu *pmu;
6246 int idx;
6247
6248 idx = srcu_read_lock(&pmus_srcu);
6249 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006250 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006251
6252 mutex_lock(&ctx->mutex);
6253 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6254 mutex_unlock(&ctx->mutex);
6255 }
6256 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006257}
6258
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006259static void perf_event_exit_cpu(int cpu)
6260{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006261 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006262
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006263 mutex_lock(&swhash->hlist_mutex);
6264 swevent_hlist_release(swhash);
6265 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006266
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006267 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006268}
6269#else
6270static inline void perf_event_exit_cpu(int cpu) { }
6271#endif
6272
6273static int __cpuinit
6274perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6275{
6276 unsigned int cpu = (long)hcpu;
6277
Peter Zijlstra5e116372010-06-11 13:35:08 +02006278 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006279
6280 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006281 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006282 perf_event_init_cpu(cpu);
6283 break;
6284
Peter Zijlstra5e116372010-06-11 13:35:08 +02006285 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006286 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006287 perf_event_exit_cpu(cpu);
6288 break;
6289
6290 default:
6291 break;
6292 }
6293
6294 return NOTIFY_OK;
6295}
6296
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006297void __init perf_event_init(void)
6298{
Jason Wessel3c502e72010-11-04 17:33:01 -05006299 int ret;
6300
Paul Mackerras220b1402010-03-10 20:45:52 +11006301 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006302 init_srcu_struct(&pmus_srcu);
6303 perf_pmu_register(&perf_swevent);
6304 perf_pmu_register(&perf_cpu_clock);
6305 perf_pmu_register(&perf_task_clock);
6306 perf_tp_register();
6307 perf_cpu_notifier(perf_cpu_notify);
Jason Wessel3c502e72010-11-04 17:33:01 -05006308
6309 ret = init_hw_breakpoint();
6310 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006311}