blob: 9a98ce95356106d689c9fba843dbb5a67a67c4c0 [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020034
35#include <asm/irq_regs.h>
36
37/*
38 * Each CPU has a list of per CPU events:
39 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080040static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041
42int perf_max_events __read_mostly = 1;
43static int perf_reserved_percpu __read_mostly;
44static int perf_overcommit __read_mostly = 1;
45
46static atomic_t nr_events __read_mostly;
47static atomic_t nr_mmap_events __read_mostly;
48static atomic_t nr_comm_events __read_mostly;
49static atomic_t nr_task_events __read_mostly;
50
51/*
52 * perf event paranoia level:
53 * -1 - not paranoid at all
54 * 0 - disallow raw tracepoint access for unpriv
55 * 1 - disallow cpu events for unpriv
56 * 2 - disallow kernel profiling for unpriv
57 */
58int sysctl_perf_event_paranoid __read_mostly = 1;
59
Ingo Molnarcdd6c482009-09-21 12:02:48 +020060int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
61
62/*
63 * max perf event sample rate
64 */
65int sysctl_perf_event_sample_rate __read_mostly = 100000;
66
67static atomic64_t perf_event_id;
68
69/*
70 * Lock for (sysadmin-configurable) event reservations:
71 */
72static DEFINE_SPINLOCK(perf_resource_lock);
73
Ingo Molnarcdd6c482009-09-21 12:02:48 +020074void __weak hw_perf_disable(void) { barrier(); }
75void __weak hw_perf_enable(void) { barrier(); }
76
Ingo Molnarcdd6c482009-09-21 12:02:48 +020077void __weak perf_event_print_debug(void) { }
78
79static DEFINE_PER_CPU(int, perf_disable_count);
80
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081void perf_disable(void)
82{
Peter Zijlstra32975a42010-03-06 19:49:19 +010083 if (!__get_cpu_var(perf_disable_count)++)
84 hw_perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020085}
86
87void perf_enable(void)
88{
Peter Zijlstra32975a42010-03-06 19:49:19 +010089 if (!--__get_cpu_var(perf_disable_count))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090 hw_perf_enable();
91}
92
93static void get_ctx(struct perf_event_context *ctx)
94{
95 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
96}
97
98static void free_ctx(struct rcu_head *head)
99{
100 struct perf_event_context *ctx;
101
102 ctx = container_of(head, struct perf_event_context, rcu_head);
103 kfree(ctx);
104}
105
106static void put_ctx(struct perf_event_context *ctx)
107{
108 if (atomic_dec_and_test(&ctx->refcount)) {
109 if (ctx->parent_ctx)
110 put_ctx(ctx->parent_ctx);
111 if (ctx->task)
112 put_task_struct(ctx->task);
113 call_rcu(&ctx->rcu_head, free_ctx);
114 }
115}
116
117static void unclone_ctx(struct perf_event_context *ctx)
118{
119 if (ctx->parent_ctx) {
120 put_ctx(ctx->parent_ctx);
121 ctx->parent_ctx = NULL;
122 }
123}
124
125/*
126 * If we inherit events we want to return the parent event id
127 * to userspace.
128 */
129static u64 primary_event_id(struct perf_event *event)
130{
131 u64 id = event->id;
132
133 if (event->parent)
134 id = event->parent->id;
135
136 return id;
137}
138
139/*
140 * Get the perf_event_context for a task and lock it.
141 * This has to cope with with the fact that until it is locked,
142 * the context could get moved to another task.
143 */
144static struct perf_event_context *
145perf_lock_task_context(struct task_struct *task, unsigned long *flags)
146{
147 struct perf_event_context *ctx;
148
149 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200150retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200151 ctx = rcu_dereference(task->perf_event_ctxp);
152 if (ctx) {
153 /*
154 * If this context is a clone of another, it might
155 * get swapped for another underneath us by
156 * perf_event_task_sched_out, though the
157 * rcu_read_lock() protects us from any context
158 * getting freed. Lock the context and check if it
159 * got swapped before we could get the lock, and retry
160 * if so. If we locked the right context, then it
161 * can't get swapped on us any more.
162 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100163 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100165 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200166 goto retry;
167 }
168
169 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100170 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200171 ctx = NULL;
172 }
173 }
174 rcu_read_unlock();
175 return ctx;
176}
177
178/*
179 * Get the context for a task and increment its pin_count so it
180 * can't get swapped to another task. This also increments its
181 * reference count so that the context can't get freed.
182 */
183static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
184{
185 struct perf_event_context *ctx;
186 unsigned long flags;
187
188 ctx = perf_lock_task_context(task, &flags);
189 if (ctx) {
190 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100191 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200192 }
193 return ctx;
194}
195
196static void perf_unpin_context(struct perf_event_context *ctx)
197{
198 unsigned long flags;
199
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100200 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203 put_ctx(ctx);
204}
205
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100206static inline u64 perf_clock(void)
207{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200208 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100209}
210
211/*
212 * Update the record of the current time in a context.
213 */
214static void update_context_time(struct perf_event_context *ctx)
215{
216 u64 now = perf_clock();
217
218 ctx->time += now - ctx->timestamp;
219 ctx->timestamp = now;
220}
221
222/*
223 * Update the total_time_enabled and total_time_running fields for a event.
224 */
225static void update_event_times(struct perf_event *event)
226{
227 struct perf_event_context *ctx = event->ctx;
228 u64 run_end;
229
230 if (event->state < PERF_EVENT_STATE_INACTIVE ||
231 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
232 return;
233
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100234 if (ctx->is_active)
235 run_end = ctx->time;
236 else
237 run_end = event->tstamp_stopped;
238
239 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100240
241 if (event->state == PERF_EVENT_STATE_INACTIVE)
242 run_end = event->tstamp_stopped;
243 else
244 run_end = ctx->time;
245
246 event->total_time_running = run_end - event->tstamp_running;
247}
248
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200249/*
250 * Update total_time_enabled and total_time_running for all events in a group.
251 */
252static void update_group_times(struct perf_event *leader)
253{
254 struct perf_event *event;
255
256 update_event_times(leader);
257 list_for_each_entry(event, &leader->sibling_list, group_entry)
258 update_event_times(event);
259}
260
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100261static struct list_head *
262ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
263{
264 if (event->attr.pinned)
265 return &ctx->pinned_groups;
266 else
267 return &ctx->flexible_groups;
268}
269
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200270/*
271 * Add a event from the lists for its context.
272 * Must be called with ctx->mutex and ctx->lock held.
273 */
274static void
275list_add_event(struct perf_event *event, struct perf_event_context *ctx)
276{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200277 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
278 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279
280 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200281 * If we're a stand alone event or group leader, we go to the context
282 * list, group events are kept attached to the group so that
283 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200284 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200285 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100286 struct list_head *list;
287
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100288 if (is_software_event(event))
289 event->group_flags |= PERF_GROUP_SOFTWARE;
290
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100291 list = ctx_group_list(event, ctx);
292 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293 }
294
295 list_add_rcu(&event->event_entry, &ctx->event_list);
296 ctx->nr_events++;
297 if (event->attr.inherit_stat)
298 ctx->nr_stat++;
299}
300
Peter Zijlstra8a495422010-05-27 15:47:49 +0200301static void perf_group_attach(struct perf_event *event)
302{
303 struct perf_event *group_leader = event->group_leader;
304
305 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
306 event->attach_state |= PERF_ATTACH_GROUP;
307
308 if (group_leader == event)
309 return;
310
311 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
312 !is_software_event(event))
313 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
314
315 list_add_tail(&event->group_entry, &group_leader->sibling_list);
316 group_leader->nr_siblings++;
317}
318
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200319/*
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
322 */
323static void
324list_del_event(struct perf_event *event, struct perf_event_context *ctx)
325{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200326 /*
327 * We can have double detach due to exit/hot-unplug + close.
328 */
329 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200330 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200331
332 event->attach_state &= ~PERF_ATTACH_CONTEXT;
333
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200334 ctx->nr_events--;
335 if (event->attr.inherit_stat)
336 ctx->nr_stat--;
337
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338 list_del_rcu(&event->event_entry);
339
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340 if (event->group_leader == event)
341 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200343 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800344
345 /*
346 * If event was in error state, then keep it
347 * that way, otherwise bogus counts will be
348 * returned on read(). The only way to get out
349 * of error state is by explicit re-enabling
350 * of the event
351 */
352 if (event->state > PERF_EVENT_STATE_OFF)
353 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200354}
355
Peter Zijlstra8a495422010-05-27 15:47:49 +0200356static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200357{
358 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200359 struct list_head *list = NULL;
360
361 /*
362 * We can have double detach due to exit/hot-unplug + close.
363 */
364 if (!(event->attach_state & PERF_ATTACH_GROUP))
365 return;
366
367 event->attach_state &= ~PERF_ATTACH_GROUP;
368
369 /*
370 * If this is a sibling, remove it from its group.
371 */
372 if (event->group_leader != event) {
373 list_del_init(&event->group_entry);
374 event->group_leader->nr_siblings--;
375 return;
376 }
377
378 if (!list_empty(&event->group_entry))
379 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100380
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381 /*
382 * If this was a group event with sibling events then
383 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200384 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200385 */
386 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200387 if (list)
388 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200389 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100390
391 /* Inherit group flags from the previous leader */
392 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393 }
394}
395
Stephane Eranianfa66f072010-08-26 16:40:01 +0200396static inline int
397event_filter_match(struct perf_event *event)
398{
399 return event->cpu == -1 || event->cpu == smp_processor_id();
400}
401
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402static void
403event_sched_out(struct perf_event *event,
404 struct perf_cpu_context *cpuctx,
405 struct perf_event_context *ctx)
406{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200407 u64 delta;
408 /*
409 * An event which could not be activated because of
410 * filter mismatch still needs to have its timings
411 * maintained, otherwise bogus information is return
412 * via read() for time_enabled, time_running:
413 */
414 if (event->state == PERF_EVENT_STATE_INACTIVE
415 && !event_filter_match(event)) {
416 delta = ctx->time - event->tstamp_stopped;
417 event->tstamp_running += delta;
418 event->tstamp_stopped = ctx->time;
419 }
420
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200421 if (event->state != PERF_EVENT_STATE_ACTIVE)
422 return;
423
424 event->state = PERF_EVENT_STATE_INACTIVE;
425 if (event->pending_disable) {
426 event->pending_disable = 0;
427 event->state = PERF_EVENT_STATE_OFF;
428 }
429 event->tstamp_stopped = ctx->time;
430 event->pmu->disable(event);
431 event->oncpu = -1;
432
433 if (!is_software_event(event))
434 cpuctx->active_oncpu--;
435 ctx->nr_active--;
436 if (event->attr.exclusive || !cpuctx->active_oncpu)
437 cpuctx->exclusive = 0;
438}
439
440static void
441group_sched_out(struct perf_event *group_event,
442 struct perf_cpu_context *cpuctx,
443 struct perf_event_context *ctx)
444{
445 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200446 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200447
448 event_sched_out(group_event, cpuctx, ctx);
449
450 /*
451 * Schedule out siblings (if any):
452 */
453 list_for_each_entry(event, &group_event->sibling_list, group_entry)
454 event_sched_out(event, cpuctx, ctx);
455
Stephane Eranianfa66f072010-08-26 16:40:01 +0200456 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200457 cpuctx->exclusive = 0;
458}
459
460/*
461 * Cross CPU call to remove a performance event
462 *
463 * We disable the event on the hardware level first. After that we
464 * remove it from the context list.
465 */
466static void __perf_event_remove_from_context(void *info)
467{
468 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
469 struct perf_event *event = info;
470 struct perf_event_context *ctx = event->ctx;
471
472 /*
473 * If this is a task context, we need to check whether it is
474 * the current task context of this cpu. If not it has been
475 * scheduled out before the smp call arrived.
476 */
477 if (ctx->task && cpuctx->task_ctx != ctx)
478 return;
479
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100480 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200481
482 event_sched_out(event, cpuctx, ctx);
483
484 list_del_event(event, ctx);
485
486 if (!ctx->task) {
487 /*
488 * Allow more per task events with respect to the
489 * reservation:
490 */
491 cpuctx->max_pertask =
492 min(perf_max_events - ctx->nr_events,
493 perf_max_events - perf_reserved_percpu);
494 }
495
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100496 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200497}
498
499
500/*
501 * Remove the event from a task's (or a CPU's) list of events.
502 *
503 * Must be called with ctx->mutex held.
504 *
505 * CPU events are removed with a smp call. For task events we only
506 * call when the task is on a CPU.
507 *
508 * If event->ctx is a cloned context, callers must make sure that
509 * every task struct that event->ctx->task could possibly point to
510 * remains valid. This is OK when called from perf_release since
511 * that only calls us on the top-level context, which can't be a clone.
512 * When called from perf_event_exit_task, it's OK because the
513 * context has been detached from its task.
514 */
515static void perf_event_remove_from_context(struct perf_event *event)
516{
517 struct perf_event_context *ctx = event->ctx;
518 struct task_struct *task = ctx->task;
519
520 if (!task) {
521 /*
522 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200523 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200524 */
525 smp_call_function_single(event->cpu,
526 __perf_event_remove_from_context,
527 event, 1);
528 return;
529 }
530
531retry:
532 task_oncpu_function_call(task, __perf_event_remove_from_context,
533 event);
534
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100535 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200536 /*
537 * If the context is active we need to retry the smp call.
538 */
539 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100540 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200541 goto retry;
542 }
543
544 /*
545 * The lock prevents that this context is scheduled in so we
546 * can remove the event safely, if the call above did not
547 * succeed.
548 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100549 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200550 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100551 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200552}
553
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200554/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555 * Cross CPU call to disable a performance event
556 */
557static void __perf_event_disable(void *info)
558{
559 struct perf_event *event = info;
560 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
561 struct perf_event_context *ctx = event->ctx;
562
563 /*
564 * If this is a per-task event, need to check whether this
565 * event's task is the current task on this cpu.
566 */
567 if (ctx->task && cpuctx->task_ctx != ctx)
568 return;
569
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100570 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200571
572 /*
573 * If the event is on, turn it off.
574 * If it is in error state, leave it in error state.
575 */
576 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
577 update_context_time(ctx);
578 update_group_times(event);
579 if (event == event->group_leader)
580 group_sched_out(event, cpuctx, ctx);
581 else
582 event_sched_out(event, cpuctx, ctx);
583 event->state = PERF_EVENT_STATE_OFF;
584 }
585
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100586 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200587}
588
589/*
590 * Disable a event.
591 *
592 * If event->ctx is a cloned context, callers must make sure that
593 * every task struct that event->ctx->task could possibly point to
594 * remains valid. This condition is satisifed when called through
595 * perf_event_for_each_child or perf_event_for_each because they
596 * hold the top-level event's child_mutex, so any descendant that
597 * goes to exit will block in sync_child_event.
598 * When called from perf_pending_event it's OK because event->ctx
599 * is the current context on this CPU and preemption is disabled,
600 * hence we can't get into perf_event_task_sched_out for this context.
601 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100602void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200603{
604 struct perf_event_context *ctx = event->ctx;
605 struct task_struct *task = ctx->task;
606
607 if (!task) {
608 /*
609 * Disable the event on the cpu that it's on
610 */
611 smp_call_function_single(event->cpu, __perf_event_disable,
612 event, 1);
613 return;
614 }
615
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200616retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200617 task_oncpu_function_call(task, __perf_event_disable, event);
618
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100619 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200620 /*
621 * If the event is still active, we need to retry the cross-call.
622 */
623 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100624 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200625 goto retry;
626 }
627
628 /*
629 * Since we have the lock this context can't be scheduled
630 * in, so we can change the state safely.
631 */
632 if (event->state == PERF_EVENT_STATE_INACTIVE) {
633 update_group_times(event);
634 event->state = PERF_EVENT_STATE_OFF;
635 }
636
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100637 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200638}
639
640static int
641event_sched_in(struct perf_event *event,
642 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100643 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200644{
645 if (event->state <= PERF_EVENT_STATE_OFF)
646 return 0;
647
648 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100649 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200650 /*
651 * The new state must be visible before we turn it on in the hardware:
652 */
653 smp_wmb();
654
655 if (event->pmu->enable(event)) {
656 event->state = PERF_EVENT_STATE_INACTIVE;
657 event->oncpu = -1;
658 return -EAGAIN;
659 }
660
661 event->tstamp_running += ctx->time - event->tstamp_stopped;
662
663 if (!is_software_event(event))
664 cpuctx->active_oncpu++;
665 ctx->nr_active++;
666
667 if (event->attr.exclusive)
668 cpuctx->exclusive = 1;
669
670 return 0;
671}
672
673static int
674group_sched_in(struct perf_event *group_event,
675 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100676 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200677{
Lin Ming6bde9b62010-04-23 13:56:00 +0800678 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200679 struct pmu *pmu = group_event->pmu;
Lin Ming6bde9b62010-04-23 13:56:00 +0800680 bool txn = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200681
682 if (group_event->state == PERF_EVENT_STATE_OFF)
683 return 0;
684
Lin Ming6bde9b62010-04-23 13:56:00 +0800685 /* Check if group transaction availabe */
686 if (pmu->start_txn)
687 txn = true;
688
689 if (txn)
690 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200691
Stephane Eranian90151c352010-05-25 16:23:10 +0200692 if (event_sched_in(group_event, cpuctx, ctx)) {
693 if (txn)
694 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200695 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +0200696 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697
698 /*
699 * Schedule in siblings as one group (if any):
700 */
701 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100702 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200703 partial_group = event;
704 goto group_error;
705 }
706 }
707
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200708 if (!txn || !pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000709 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800710
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200711group_error:
712 /*
713 * Groups can be scheduled in as one unit only, so undo any
714 * partial group before returning:
715 */
716 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
717 if (event == partial_group)
718 break;
719 event_sched_out(event, cpuctx, ctx);
720 }
721 event_sched_out(group_event, cpuctx, ctx);
722
Stephane Eranian90151c352010-05-25 16:23:10 +0200723 if (txn)
724 pmu->cancel_txn(pmu);
725
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200726 return -EAGAIN;
727}
728
729/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200730 * Work out whether we can put this event group on the CPU now.
731 */
732static int group_can_go_on(struct perf_event *event,
733 struct perf_cpu_context *cpuctx,
734 int can_add_hw)
735{
736 /*
737 * Groups consisting entirely of software events can always go on.
738 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100739 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200740 return 1;
741 /*
742 * If an exclusive group is already on, no other hardware
743 * events can go on.
744 */
745 if (cpuctx->exclusive)
746 return 0;
747 /*
748 * If this group is exclusive and there are already
749 * events on the CPU, it can't go on.
750 */
751 if (event->attr.exclusive && cpuctx->active_oncpu)
752 return 0;
753 /*
754 * Otherwise, try to add it if all previous groups were able
755 * to go on.
756 */
757 return can_add_hw;
758}
759
760static void add_event_to_ctx(struct perf_event *event,
761 struct perf_event_context *ctx)
762{
763 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200764 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200765 event->tstamp_enabled = ctx->time;
766 event->tstamp_running = ctx->time;
767 event->tstamp_stopped = ctx->time;
768}
769
770/*
771 * Cross CPU call to install and enable a performance event
772 *
773 * Must be called with ctx->mutex held
774 */
775static void __perf_install_in_context(void *info)
776{
777 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
778 struct perf_event *event = info;
779 struct perf_event_context *ctx = event->ctx;
780 struct perf_event *leader = event->group_leader;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200781 int err;
782
783 /*
784 * If this is a task context, we need to check whether it is
785 * the current task context of this cpu. If not it has been
786 * scheduled out before the smp call arrived.
787 * Or possibly this is the right context but it isn't
788 * on this cpu because it had no events.
789 */
790 if (ctx->task && cpuctx->task_ctx != ctx) {
791 if (cpuctx->task_ctx || ctx->task != current)
792 return;
793 cpuctx->task_ctx = ctx;
794 }
795
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100796 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200797 ctx->is_active = 1;
798 update_context_time(ctx);
799
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200800 add_event_to_ctx(event, ctx);
801
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100802 if (event->cpu != -1 && event->cpu != smp_processor_id())
803 goto unlock;
804
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200805 /*
806 * Don't put the event on if it is disabled or if
807 * it is in a group and the group isn't on.
808 */
809 if (event->state != PERF_EVENT_STATE_INACTIVE ||
810 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
811 goto unlock;
812
813 /*
814 * An exclusive event can't go on if there are already active
815 * hardware events, and no hardware event can go on if there
816 * is already an exclusive event on.
817 */
818 if (!group_can_go_on(event, cpuctx, 1))
819 err = -EEXIST;
820 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100821 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200822
823 if (err) {
824 /*
825 * This event couldn't go on. If it is in a group
826 * then we have to pull the whole group off.
827 * If the event group is pinned then put it in error state.
828 */
829 if (leader != event)
830 group_sched_out(leader, cpuctx, ctx);
831 if (leader->attr.pinned) {
832 update_group_times(leader);
833 leader->state = PERF_EVENT_STATE_ERROR;
834 }
835 }
836
837 if (!err && !ctx->task && cpuctx->max_pertask)
838 cpuctx->max_pertask--;
839
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200840unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100841 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200842}
843
844/*
845 * Attach a performance event to a context
846 *
847 * First we add the event to the list with the hardware enable bit
848 * in event->hw_config cleared.
849 *
850 * If the event is attached to a task which is on a CPU we use a smp
851 * call to enable it in the task context. The task might have been
852 * scheduled away, but we check this in the smp call again.
853 *
854 * Must be called with ctx->mutex held.
855 */
856static void
857perf_install_in_context(struct perf_event_context *ctx,
858 struct perf_event *event,
859 int cpu)
860{
861 struct task_struct *task = ctx->task;
862
863 if (!task) {
864 /*
865 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200866 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867 */
868 smp_call_function_single(cpu, __perf_install_in_context,
869 event, 1);
870 return;
871 }
872
873retry:
874 task_oncpu_function_call(task, __perf_install_in_context,
875 event);
876
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100877 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878 /*
879 * we need to retry the smp call.
880 */
881 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100882 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200883 goto retry;
884 }
885
886 /*
887 * The lock prevents that this context is scheduled in so we
888 * can add the event safely, if it the call above did not
889 * succeed.
890 */
891 if (list_empty(&event->group_entry))
892 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100893 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200894}
895
896/*
897 * Put a event into inactive state and update time fields.
898 * Enabling the leader of a group effectively enables all
899 * the group members that aren't explicitly disabled, so we
900 * have to update their ->tstamp_enabled also.
901 * Note: this works for group members as well as group leaders
902 * since the non-leader members' sibling_lists will be empty.
903 */
904static void __perf_event_mark_enabled(struct perf_event *event,
905 struct perf_event_context *ctx)
906{
907 struct perf_event *sub;
908
909 event->state = PERF_EVENT_STATE_INACTIVE;
910 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200911 list_for_each_entry(sub, &event->sibling_list, group_entry) {
912 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200913 sub->tstamp_enabled =
914 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200915 }
916 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917}
918
919/*
920 * Cross CPU call to enable a performance event
921 */
922static void __perf_event_enable(void *info)
923{
924 struct perf_event *event = info;
925 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
926 struct perf_event_context *ctx = event->ctx;
927 struct perf_event *leader = event->group_leader;
928 int err;
929
930 /*
931 * If this is a per-task event, need to check whether this
932 * event's task is the current task on this cpu.
933 */
934 if (ctx->task && cpuctx->task_ctx != ctx) {
935 if (cpuctx->task_ctx || ctx->task != current)
936 return;
937 cpuctx->task_ctx = ctx;
938 }
939
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100940 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200941 ctx->is_active = 1;
942 update_context_time(ctx);
943
944 if (event->state >= PERF_EVENT_STATE_INACTIVE)
945 goto unlock;
946 __perf_event_mark_enabled(event, ctx);
947
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100948 if (event->cpu != -1 && event->cpu != smp_processor_id())
949 goto unlock;
950
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200951 /*
952 * If the event is in a group and isn't the group leader,
953 * then don't put it on unless the group is on.
954 */
955 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
956 goto unlock;
957
958 if (!group_can_go_on(event, cpuctx, 1)) {
959 err = -EEXIST;
960 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200961 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100962 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200963 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100964 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965 }
966
967 if (err) {
968 /*
969 * If this event can't go on and it's part of a
970 * group, then the whole group has to come off.
971 */
972 if (leader != event)
973 group_sched_out(leader, cpuctx, ctx);
974 if (leader->attr.pinned) {
975 update_group_times(leader);
976 leader->state = PERF_EVENT_STATE_ERROR;
977 }
978 }
979
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200980unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100981 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200982}
983
984/*
985 * Enable a event.
986 *
987 * If event->ctx is a cloned context, callers must make sure that
988 * every task struct that event->ctx->task could possibly point to
989 * remains valid. This condition is satisfied when called through
990 * perf_event_for_each_child or perf_event_for_each as described
991 * for perf_event_disable.
992 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100993void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200994{
995 struct perf_event_context *ctx = event->ctx;
996 struct task_struct *task = ctx->task;
997
998 if (!task) {
999 /*
1000 * Enable the event on the cpu that it's on
1001 */
1002 smp_call_function_single(event->cpu, __perf_event_enable,
1003 event, 1);
1004 return;
1005 }
1006
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001007 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1009 goto out;
1010
1011 /*
1012 * If the event is in error state, clear that first.
1013 * That way, if we see the event in error state below, we
1014 * know that it has gone back into error state, as distinct
1015 * from the task having been scheduled away before the
1016 * cross-call arrived.
1017 */
1018 if (event->state == PERF_EVENT_STATE_ERROR)
1019 event->state = PERF_EVENT_STATE_OFF;
1020
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001021retry:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001022 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001023 task_oncpu_function_call(task, __perf_event_enable, event);
1024
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001025 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026
1027 /*
1028 * If the context is active and the event is still off,
1029 * we need to retry the cross-call.
1030 */
1031 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1032 goto retry;
1033
1034 /*
1035 * Since we have the lock this context can't be scheduled
1036 * in, so we can change the state safely.
1037 */
1038 if (event->state == PERF_EVENT_STATE_OFF)
1039 __perf_event_mark_enabled(event, ctx);
1040
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001041out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001042 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001043}
1044
1045static int perf_event_refresh(struct perf_event *event, int refresh)
1046{
1047 /*
1048 * not supported on inherited events
1049 */
1050 if (event->attr.inherit)
1051 return -EINVAL;
1052
1053 atomic_add(refresh, &event->event_limit);
1054 perf_event_enable(event);
1055
1056 return 0;
1057}
1058
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001059enum event_type_t {
1060 EVENT_FLEXIBLE = 0x1,
1061 EVENT_PINNED = 0x2,
1062 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1063};
1064
1065static void ctx_sched_out(struct perf_event_context *ctx,
1066 struct perf_cpu_context *cpuctx,
1067 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001068{
1069 struct perf_event *event;
1070
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001071 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001072 ctx->is_active = 0;
1073 if (likely(!ctx->nr_events))
1074 goto out;
1075 update_context_time(ctx);
1076
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001077 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001078 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001079
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001080 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001081 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1082 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001083 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001084
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001085 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001086 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001087 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001088 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001089out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001090 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091}
1092
1093/*
1094 * Test whether two contexts are equivalent, i.e. whether they
1095 * have both been cloned from the same version of the same context
1096 * and they both have the same number of enabled events.
1097 * If the number of enabled events is the same, then the set
1098 * of enabled events should be the same, because these are both
1099 * inherited contexts, therefore we can't access individual events
1100 * in them directly with an fd; we can only enable/disable all
1101 * events via prctl, or enable/disable all events in a family
1102 * via ioctl, which will have the same effect on both contexts.
1103 */
1104static int context_equiv(struct perf_event_context *ctx1,
1105 struct perf_event_context *ctx2)
1106{
1107 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1108 && ctx1->parent_gen == ctx2->parent_gen
1109 && !ctx1->pin_count && !ctx2->pin_count;
1110}
1111
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001112static void __perf_event_sync_stat(struct perf_event *event,
1113 struct perf_event *next_event)
1114{
1115 u64 value;
1116
1117 if (!event->attr.inherit_stat)
1118 return;
1119
1120 /*
1121 * Update the event value, we cannot use perf_event_read()
1122 * because we're in the middle of a context switch and have IRQs
1123 * disabled, which upsets smp_call_function_single(), however
1124 * we know the event must be on the current CPU, therefore we
1125 * don't need to use it.
1126 */
1127 switch (event->state) {
1128 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001129 event->pmu->read(event);
1130 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001131
1132 case PERF_EVENT_STATE_INACTIVE:
1133 update_event_times(event);
1134 break;
1135
1136 default:
1137 break;
1138 }
1139
1140 /*
1141 * In order to keep per-task stats reliable we need to flip the event
1142 * values when we flip the contexts.
1143 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001144 value = local64_read(&next_event->count);
1145 value = local64_xchg(&event->count, value);
1146 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001147
1148 swap(event->total_time_enabled, next_event->total_time_enabled);
1149 swap(event->total_time_running, next_event->total_time_running);
1150
1151 /*
1152 * Since we swizzled the values, update the user visible data too.
1153 */
1154 perf_event_update_userpage(event);
1155 perf_event_update_userpage(next_event);
1156}
1157
1158#define list_next_entry(pos, member) \
1159 list_entry(pos->member.next, typeof(*pos), member)
1160
1161static void perf_event_sync_stat(struct perf_event_context *ctx,
1162 struct perf_event_context *next_ctx)
1163{
1164 struct perf_event *event, *next_event;
1165
1166 if (!ctx->nr_stat)
1167 return;
1168
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001169 update_context_time(ctx);
1170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001171 event = list_first_entry(&ctx->event_list,
1172 struct perf_event, event_entry);
1173
1174 next_event = list_first_entry(&next_ctx->event_list,
1175 struct perf_event, event_entry);
1176
1177 while (&event->event_entry != &ctx->event_list &&
1178 &next_event->event_entry != &next_ctx->event_list) {
1179
1180 __perf_event_sync_stat(event, next_event);
1181
1182 event = list_next_entry(event, event_entry);
1183 next_event = list_next_entry(next_event, event_entry);
1184 }
1185}
1186
1187/*
1188 * Called from scheduler to remove the events of the current task,
1189 * with interrupts disabled.
1190 *
1191 * We stop each event and update the event value in event->count.
1192 *
1193 * This does not protect us against NMI, but disable()
1194 * sets the disabled bit in the control field of event _before_
1195 * accessing the event control register. If a NMI hits, then it will
1196 * not restart the event.
1197 */
1198void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001199 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001201 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001202 struct perf_event_context *ctx = task->perf_event_ctxp;
1203 struct perf_event_context *next_ctx;
1204 struct perf_event_context *parent;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 int do_switch = 1;
1206
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001207 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208
1209 if (likely(!ctx || !cpuctx->task_ctx))
1210 return;
1211
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001212 rcu_read_lock();
1213 parent = rcu_dereference(ctx->parent_ctx);
1214 next_ctx = next->perf_event_ctxp;
1215 if (parent && next_ctx &&
1216 rcu_dereference(next_ctx->parent_ctx) == parent) {
1217 /*
1218 * Looks like the two contexts are clones, so we might be
1219 * able to optimize the context switch. We lock both
1220 * contexts and check that they are clones under the
1221 * lock (including re-checking that neither has been
1222 * uncloned in the meantime). It doesn't matter which
1223 * order we take the locks because no other cpu could
1224 * be trying to lock both of these tasks.
1225 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001226 raw_spin_lock(&ctx->lock);
1227 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 if (context_equiv(ctx, next_ctx)) {
1229 /*
1230 * XXX do we need a memory barrier of sorts
1231 * wrt to rcu_dereference() of perf_event_ctxp
1232 */
1233 task->perf_event_ctxp = next_ctx;
1234 next->perf_event_ctxp = ctx;
1235 ctx->task = next;
1236 next_ctx->task = task;
1237 do_switch = 0;
1238
1239 perf_event_sync_stat(ctx, next_ctx);
1240 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001241 raw_spin_unlock(&next_ctx->lock);
1242 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001243 }
1244 rcu_read_unlock();
1245
1246 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001247 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001248 cpuctx->task_ctx = NULL;
1249 }
1250}
1251
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001252static void task_ctx_sched_out(struct perf_event_context *ctx,
1253 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001254{
1255 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1256
1257 if (!cpuctx->task_ctx)
1258 return;
1259
1260 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1261 return;
1262
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001263 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 cpuctx->task_ctx = NULL;
1265}
1266
1267/*
1268 * Called with IRQs disabled
1269 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001270static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001271{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001272 task_ctx_sched_out(ctx, EVENT_ALL);
1273}
1274
1275/*
1276 * Called with IRQs disabled
1277 */
1278static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1279 enum event_type_t event_type)
1280{
1281 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001282}
1283
1284static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001285ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001286 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001287{
1288 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001289
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001290 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1291 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001292 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001293 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001294 continue;
1295
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001296 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001297 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001298
1299 /*
1300 * If this pinned group hasn't been scheduled,
1301 * put it in error state.
1302 */
1303 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1304 update_group_times(event);
1305 event->state = PERF_EVENT_STATE_ERROR;
1306 }
1307 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001308}
1309
1310static void
1311ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001312 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001313{
1314 struct perf_event *event;
1315 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001317 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1318 /* Ignore events in OFF or ERROR state */
1319 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001321 /*
1322 * Listen to the 'cpu' scheduling filter constraint
1323 * of events:
1324 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001325 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326 continue;
1327
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001328 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001329 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001330 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001331 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001332 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001333}
1334
1335static void
1336ctx_sched_in(struct perf_event_context *ctx,
1337 struct perf_cpu_context *cpuctx,
1338 enum event_type_t event_type)
1339{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001340 raw_spin_lock(&ctx->lock);
1341 ctx->is_active = 1;
1342 if (likely(!ctx->nr_events))
1343 goto out;
1344
1345 ctx->timestamp = perf_clock();
1346
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001347 /*
1348 * First go through the list and put on any pinned groups
1349 * in order to give them the best chance of going on.
1350 */
1351 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001352 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001353
1354 /* Then walk through the lower prio flexible groups */
1355 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001356 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001357
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001358out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001359 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360}
1361
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001362static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1363 enum event_type_t event_type)
1364{
1365 struct perf_event_context *ctx = &cpuctx->ctx;
1366
1367 ctx_sched_in(ctx, cpuctx, event_type);
1368}
1369
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001370static void task_ctx_sched_in(struct task_struct *task,
1371 enum event_type_t event_type)
1372{
1373 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1374 struct perf_event_context *ctx = task->perf_event_ctxp;
1375
1376 if (likely(!ctx))
1377 return;
1378 if (cpuctx->task_ctx == ctx)
1379 return;
1380 ctx_sched_in(ctx, cpuctx, event_type);
1381 cpuctx->task_ctx = ctx;
1382}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001383/*
1384 * Called from scheduler to add the events of the current task
1385 * with interrupts disabled.
1386 *
1387 * We restore the event value and then enable it.
1388 *
1389 * This does not protect us against NMI, but enable()
1390 * sets the enabled bit in the control field of event _before_
1391 * accessing the event control register. If a NMI hits, then it will
1392 * keep the event running.
1393 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001394void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001396 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1397 struct perf_event_context *ctx = task->perf_event_ctxp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001398
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001399 if (likely(!ctx))
1400 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001402 if (cpuctx->task_ctx == ctx)
1403 return;
1404
1405 /*
1406 * We want to keep the following priority order:
1407 * cpu pinned (that don't need to move), task pinned,
1408 * cpu flexible, task flexible.
1409 */
1410 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1411
1412 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1413 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1414 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1415
1416 cpuctx->task_ctx = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417}
1418
1419#define MAX_INTERRUPTS (~0ULL)
1420
1421static void perf_log_throttle(struct perf_event *event, int enable);
1422
Peter Zijlstraabd50712010-01-26 18:50:16 +01001423static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1424{
1425 u64 frequency = event->attr.sample_freq;
1426 u64 sec = NSEC_PER_SEC;
1427 u64 divisor, dividend;
1428
1429 int count_fls, nsec_fls, frequency_fls, sec_fls;
1430
1431 count_fls = fls64(count);
1432 nsec_fls = fls64(nsec);
1433 frequency_fls = fls64(frequency);
1434 sec_fls = 30;
1435
1436 /*
1437 * We got @count in @nsec, with a target of sample_freq HZ
1438 * the target period becomes:
1439 *
1440 * @count * 10^9
1441 * period = -------------------
1442 * @nsec * sample_freq
1443 *
1444 */
1445
1446 /*
1447 * Reduce accuracy by one bit such that @a and @b converge
1448 * to a similar magnitude.
1449 */
1450#define REDUCE_FLS(a, b) \
1451do { \
1452 if (a##_fls > b##_fls) { \
1453 a >>= 1; \
1454 a##_fls--; \
1455 } else { \
1456 b >>= 1; \
1457 b##_fls--; \
1458 } \
1459} while (0)
1460
1461 /*
1462 * Reduce accuracy until either term fits in a u64, then proceed with
1463 * the other, so that finally we can do a u64/u64 division.
1464 */
1465 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1466 REDUCE_FLS(nsec, frequency);
1467 REDUCE_FLS(sec, count);
1468 }
1469
1470 if (count_fls + sec_fls > 64) {
1471 divisor = nsec * frequency;
1472
1473 while (count_fls + sec_fls > 64) {
1474 REDUCE_FLS(count, sec);
1475 divisor >>= 1;
1476 }
1477
1478 dividend = count * sec;
1479 } else {
1480 dividend = count * sec;
1481
1482 while (nsec_fls + frequency_fls > 64) {
1483 REDUCE_FLS(nsec, frequency);
1484 dividend >>= 1;
1485 }
1486
1487 divisor = nsec * frequency;
1488 }
1489
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001490 if (!divisor)
1491 return dividend;
1492
Peter Zijlstraabd50712010-01-26 18:50:16 +01001493 return div64_u64(dividend, divisor);
1494}
1495
Stephane Eraniand76a0812010-02-08 17:06:01 +02001496static void perf_event_stop(struct perf_event *event)
1497{
1498 if (!event->pmu->stop)
1499 return event->pmu->disable(event);
1500
1501 return event->pmu->stop(event);
1502}
1503
1504static int perf_event_start(struct perf_event *event)
1505{
1506 if (!event->pmu->start)
1507 return event->pmu->enable(event);
1508
1509 return event->pmu->start(event);
1510}
1511
Peter Zijlstraabd50712010-01-26 18:50:16 +01001512static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513{
1514 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001515 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516 s64 delta;
1517
Peter Zijlstraabd50712010-01-26 18:50:16 +01001518 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519
1520 delta = (s64)(period - hwc->sample_period);
1521 delta = (delta + 7) / 8; /* low pass filter */
1522
1523 sample_period = hwc->sample_period + delta;
1524
1525 if (!sample_period)
1526 sample_period = 1;
1527
1528 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001529
Peter Zijlstrae7850592010-05-21 14:43:08 +02001530 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eraniand76a0812010-02-08 17:06:01 +02001531 perf_event_stop(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001532 local64_set(&hwc->period_left, 0);
Stephane Eraniand76a0812010-02-08 17:06:01 +02001533 perf_event_start(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001534 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001535}
1536
1537static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1538{
1539 struct perf_event *event;
1540 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001541 u64 interrupts, now;
1542 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001543
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001544 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001545 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546 if (event->state != PERF_EVENT_STATE_ACTIVE)
1547 continue;
1548
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001549 if (event->cpu != -1 && event->cpu != smp_processor_id())
1550 continue;
1551
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552 hwc = &event->hw;
1553
1554 interrupts = hwc->interrupts;
1555 hwc->interrupts = 0;
1556
1557 /*
1558 * unthrottle events on the tick
1559 */
1560 if (interrupts == MAX_INTERRUPTS) {
1561 perf_log_throttle(event, 1);
1562 event->pmu->unthrottle(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563 }
1564
1565 if (!event->attr.freq || !event->attr.sample_freq)
1566 continue;
1567
Peter Zijlstraabd50712010-01-26 18:50:16 +01001568 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001569 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001570 delta = now - hwc->freq_count_stamp;
1571 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572
Peter Zijlstraabd50712010-01-26 18:50:16 +01001573 if (delta > 0)
1574 perf_adjust_period(event, TICK_NSEC, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001575 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001576 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577}
1578
1579/*
1580 * Round-robin a context's events:
1581 */
1582static void rotate_ctx(struct perf_event_context *ctx)
1583{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001584 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001585
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001586 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001587 list_rotate_left(&ctx->flexible_groups);
1588
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001589 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590}
1591
Peter Zijlstra49f47432009-12-27 11:51:52 +01001592void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593{
1594 struct perf_cpu_context *cpuctx;
1595 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001596 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001597
1598 if (!atomic_read(&nr_events))
1599 return;
1600
Peter Zijlstra49f47432009-12-27 11:51:52 +01001601 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001602 if (cpuctx->ctx.nr_events &&
1603 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1604 rotate = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001605
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001606 ctx = curr->perf_event_ctxp;
1607 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1608 rotate = 1;
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610 perf_ctx_adjust_freq(&cpuctx->ctx);
1611 if (ctx)
1612 perf_ctx_adjust_freq(ctx);
1613
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001614 if (!rotate)
1615 return;
1616
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001617 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001619 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620
1621 rotate_ctx(&cpuctx->ctx);
1622 if (ctx)
1623 rotate_ctx(ctx);
1624
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001625 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001627 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628}
1629
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001630static int event_enable_on_exec(struct perf_event *event,
1631 struct perf_event_context *ctx)
1632{
1633 if (!event->attr.enable_on_exec)
1634 return 0;
1635
1636 event->attr.enable_on_exec = 0;
1637 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1638 return 0;
1639
1640 __perf_event_mark_enabled(event, ctx);
1641
1642 return 1;
1643}
1644
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645/*
1646 * Enable all of a task's events that have been marked enable-on-exec.
1647 * This expects task == current.
1648 */
1649static void perf_event_enable_on_exec(struct task_struct *task)
1650{
1651 struct perf_event_context *ctx;
1652 struct perf_event *event;
1653 unsigned long flags;
1654 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001655 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656
1657 local_irq_save(flags);
1658 ctx = task->perf_event_ctxp;
1659 if (!ctx || !ctx->nr_events)
1660 goto out;
1661
1662 __perf_event_task_sched_out(ctx);
1663
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001664 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001665
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001666 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1667 ret = event_enable_on_exec(event, ctx);
1668 if (ret)
1669 enabled = 1;
1670 }
1671
1672 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1673 ret = event_enable_on_exec(event, ctx);
1674 if (ret)
1675 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676 }
1677
1678 /*
1679 * Unclone this context if we enabled any event.
1680 */
1681 if (enabled)
1682 unclone_ctx(ctx);
1683
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001684 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001685
Peter Zijlstra49f47432009-12-27 11:51:52 +01001686 perf_event_task_sched_in(task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001687out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688 local_irq_restore(flags);
1689}
1690
1691/*
1692 * Cross CPU call to read the hardware event
1693 */
1694static void __perf_event_read(void *info)
1695{
1696 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1697 struct perf_event *event = info;
1698 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699
1700 /*
1701 * If this is a task context, we need to check whether it is
1702 * the current task context of this cpu. If not it has been
1703 * scheduled out before the smp call arrived. In that case
1704 * event->count would have been updated to a recent sample
1705 * when the event was scheduled out.
1706 */
1707 if (ctx->task && cpuctx->task_ctx != ctx)
1708 return;
1709
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001710 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001711 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001712 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001713 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001714
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001715 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716}
1717
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001718static inline u64 perf_event_count(struct perf_event *event)
1719{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001720 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001721}
1722
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723static u64 perf_event_read(struct perf_event *event)
1724{
1725 /*
1726 * If event is enabled and currently active on a CPU, update the
1727 * value in the event structure:
1728 */
1729 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1730 smp_call_function_single(event->oncpu,
1731 __perf_event_read, event, 1);
1732 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001733 struct perf_event_context *ctx = event->ctx;
1734 unsigned long flags;
1735
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001736 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001737 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001739 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001740 }
1741
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001742 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743}
1744
1745/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001746 * Callchain support
1747 */
1748
1749struct callchain_cpus_entries {
1750 struct rcu_head rcu_head;
1751 struct perf_callchain_entry *cpu_entries[0];
1752};
1753
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001754static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001755static atomic_t nr_callchain_events;
1756static DEFINE_MUTEX(callchain_mutex);
1757struct callchain_cpus_entries *callchain_cpus_entries;
1758
1759
1760__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1761 struct pt_regs *regs)
1762{
1763}
1764
1765__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1766 struct pt_regs *regs)
1767{
1768}
1769
1770static void release_callchain_buffers_rcu(struct rcu_head *head)
1771{
1772 struct callchain_cpus_entries *entries;
1773 int cpu;
1774
1775 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1776
1777 for_each_possible_cpu(cpu)
1778 kfree(entries->cpu_entries[cpu]);
1779
1780 kfree(entries);
1781}
1782
1783static void release_callchain_buffers(void)
1784{
1785 struct callchain_cpus_entries *entries;
1786
1787 entries = callchain_cpus_entries;
1788 rcu_assign_pointer(callchain_cpus_entries, NULL);
1789 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1790}
1791
1792static int alloc_callchain_buffers(void)
1793{
1794 int cpu;
1795 int size;
1796 struct callchain_cpus_entries *entries;
1797
1798 /*
1799 * We can't use the percpu allocation API for data that can be
1800 * accessed from NMI. Use a temporary manual per cpu allocation
1801 * until that gets sorted out.
1802 */
1803 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1804 num_possible_cpus();
1805
1806 entries = kzalloc(size, GFP_KERNEL);
1807 if (!entries)
1808 return -ENOMEM;
1809
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001810 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001811
1812 for_each_possible_cpu(cpu) {
1813 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1814 cpu_to_node(cpu));
1815 if (!entries->cpu_entries[cpu])
1816 goto fail;
1817 }
1818
1819 rcu_assign_pointer(callchain_cpus_entries, entries);
1820
1821 return 0;
1822
1823fail:
1824 for_each_possible_cpu(cpu)
1825 kfree(entries->cpu_entries[cpu]);
1826 kfree(entries);
1827
1828 return -ENOMEM;
1829}
1830
1831static int get_callchain_buffers(void)
1832{
1833 int err = 0;
1834 int count;
1835
1836 mutex_lock(&callchain_mutex);
1837
1838 count = atomic_inc_return(&nr_callchain_events);
1839 if (WARN_ON_ONCE(count < 1)) {
1840 err = -EINVAL;
1841 goto exit;
1842 }
1843
1844 if (count > 1) {
1845 /* If the allocation failed, give up */
1846 if (!callchain_cpus_entries)
1847 err = -ENOMEM;
1848 goto exit;
1849 }
1850
1851 err = alloc_callchain_buffers();
1852 if (err)
1853 release_callchain_buffers();
1854exit:
1855 mutex_unlock(&callchain_mutex);
1856
1857 return err;
1858}
1859
1860static void put_callchain_buffers(void)
1861{
1862 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1863 release_callchain_buffers();
1864 mutex_unlock(&callchain_mutex);
1865 }
1866}
1867
1868static int get_recursion_context(int *recursion)
1869{
1870 int rctx;
1871
1872 if (in_nmi())
1873 rctx = 3;
1874 else if (in_irq())
1875 rctx = 2;
1876 else if (in_softirq())
1877 rctx = 1;
1878 else
1879 rctx = 0;
1880
1881 if (recursion[rctx])
1882 return -1;
1883
1884 recursion[rctx]++;
1885 barrier();
1886
1887 return rctx;
1888}
1889
1890static inline void put_recursion_context(int *recursion, int rctx)
1891{
1892 barrier();
1893 recursion[rctx]--;
1894}
1895
1896static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1897{
1898 int cpu;
1899 struct callchain_cpus_entries *entries;
1900
1901 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1902 if (*rctx == -1)
1903 return NULL;
1904
1905 entries = rcu_dereference(callchain_cpus_entries);
1906 if (!entries)
1907 return NULL;
1908
1909 cpu = smp_processor_id();
1910
1911 return &entries->cpu_entries[cpu][*rctx];
1912}
1913
1914static void
1915put_callchain_entry(int rctx)
1916{
1917 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1918}
1919
1920static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1921{
1922 int rctx;
1923 struct perf_callchain_entry *entry;
1924
1925
1926 entry = get_callchain_entry(&rctx);
1927 if (rctx == -1)
1928 return NULL;
1929
1930 if (!entry)
1931 goto exit_put;
1932
1933 entry->nr = 0;
1934
1935 if (!user_mode(regs)) {
1936 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1937 perf_callchain_kernel(entry, regs);
1938 if (current->mm)
1939 regs = task_pt_regs(current);
1940 else
1941 regs = NULL;
1942 }
1943
1944 if (regs) {
1945 perf_callchain_store(entry, PERF_CONTEXT_USER);
1946 perf_callchain_user(entry, regs);
1947 }
1948
1949exit_put:
1950 put_callchain_entry(rctx);
1951
1952 return entry;
1953}
1954
1955/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001956 * Initialize the perf_event context in a task_struct:
1957 */
1958static void
1959__perf_event_init_context(struct perf_event_context *ctx,
1960 struct task_struct *task)
1961{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001962 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001964 INIT_LIST_HEAD(&ctx->pinned_groups);
1965 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001966 INIT_LIST_HEAD(&ctx->event_list);
1967 atomic_set(&ctx->refcount, 1);
1968 ctx->task = task;
1969}
1970
1971static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1972{
1973 struct perf_event_context *ctx;
1974 struct perf_cpu_context *cpuctx;
1975 struct task_struct *task;
1976 unsigned long flags;
1977 int err;
1978
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001979 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 /* Must be root to operate on a CPU event: */
1981 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1982 return ERR_PTR(-EACCES);
1983
Paul Mackerras0f624e72009-12-15 19:40:32 +11001984 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001985 return ERR_PTR(-EINVAL);
1986
1987 /*
1988 * We could be clever and allow to attach a event to an
1989 * offline CPU and activate it when the CPU comes up, but
1990 * that's for later.
1991 */
Rusty Russellf6325e32009-12-17 11:43:08 -06001992 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001993 return ERR_PTR(-ENODEV);
1994
1995 cpuctx = &per_cpu(perf_cpu_context, cpu);
1996 ctx = &cpuctx->ctx;
1997 get_ctx(ctx);
1998
1999 return ctx;
2000 }
2001
2002 rcu_read_lock();
2003 if (!pid)
2004 task = current;
2005 else
2006 task = find_task_by_vpid(pid);
2007 if (task)
2008 get_task_struct(task);
2009 rcu_read_unlock();
2010
2011 if (!task)
2012 return ERR_PTR(-ESRCH);
2013
2014 /*
2015 * Can't attach events to a dying task.
2016 */
2017 err = -ESRCH;
2018 if (task->flags & PF_EXITING)
2019 goto errout;
2020
2021 /* Reuse ptrace permission checks for now. */
2022 err = -EACCES;
2023 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2024 goto errout;
2025
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002026retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002027 ctx = perf_lock_task_context(task, &flags);
2028 if (ctx) {
2029 unclone_ctx(ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002030 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002031 }
2032
2033 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08002034 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002035 err = -ENOMEM;
2036 if (!ctx)
2037 goto errout;
2038 __perf_event_init_context(ctx, task);
2039 get_ctx(ctx);
2040 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
2041 /*
2042 * We raced with some other task; use
2043 * the context they set.
2044 */
2045 kfree(ctx);
2046 goto retry;
2047 }
2048 get_task_struct(task);
2049 }
2050
2051 put_task_struct(task);
2052 return ctx;
2053
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002054errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055 put_task_struct(task);
2056 return ERR_PTR(err);
2057}
2058
Li Zefan6fb29152009-10-15 11:21:42 +08002059static void perf_event_free_filter(struct perf_event *event);
2060
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002061static void free_event_rcu(struct rcu_head *head)
2062{
2063 struct perf_event *event;
2064
2065 event = container_of(head, struct perf_event, rcu_head);
2066 if (event->ns)
2067 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002068 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002069 kfree(event);
2070}
2071
2072static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002073static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074
2075static void free_event(struct perf_event *event)
2076{
2077 perf_pending_sync(event);
2078
2079 if (!event->parent) {
2080 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002081 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 atomic_dec(&nr_mmap_events);
2083 if (event->attr.comm)
2084 atomic_dec(&nr_comm_events);
2085 if (event->attr.task)
2086 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002087 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2088 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002089 }
2090
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002091 if (event->buffer) {
2092 perf_buffer_put(event->buffer);
2093 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094 }
2095
2096 if (event->destroy)
2097 event->destroy(event);
2098
2099 put_ctx(event->ctx);
2100 call_rcu(&event->rcu_head, free_event_rcu);
2101}
2102
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002103int perf_event_release_kernel(struct perf_event *event)
2104{
2105 struct perf_event_context *ctx = event->ctx;
2106
Peter Zijlstra050735b2010-05-11 11:51:53 +02002107 /*
2108 * Remove from the PMU, can't get re-enabled since we got
2109 * here because the last ref went.
2110 */
2111 perf_event_disable(event);
2112
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002113 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002114 /*
2115 * There are two ways this annotation is useful:
2116 *
2117 * 1) there is a lock recursion from perf_event_exit_task
2118 * see the comment there.
2119 *
2120 * 2) there is a lock-inversion with mmap_sem through
2121 * perf_event_read_group(), which takes faults while
2122 * holding ctx->mutex, however this is called after
2123 * the last filedesc died, so there is no possibility
2124 * to trigger the AB-BA case.
2125 */
2126 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002127 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002128 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002129 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002130 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002131 mutex_unlock(&ctx->mutex);
2132
2133 mutex_lock(&event->owner->perf_event_mutex);
2134 list_del_init(&event->owner_entry);
2135 mutex_unlock(&event->owner->perf_event_mutex);
2136 put_task_struct(event->owner);
2137
2138 free_event(event);
2139
2140 return 0;
2141}
2142EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2143
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002144/*
2145 * Called when the last reference to the file is gone.
2146 */
2147static int perf_release(struct inode *inode, struct file *file)
2148{
2149 struct perf_event *event = file->private_data;
2150
2151 file->private_data = NULL;
2152
2153 return perf_event_release_kernel(event);
2154}
2155
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002156static int perf_event_read_size(struct perf_event *event)
2157{
2158 int entry = sizeof(u64); /* value */
2159 int size = 0;
2160 int nr = 1;
2161
2162 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2163 size += sizeof(u64);
2164
2165 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2166 size += sizeof(u64);
2167
2168 if (event->attr.read_format & PERF_FORMAT_ID)
2169 entry += sizeof(u64);
2170
2171 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2172 nr += event->group_leader->nr_siblings;
2173 size += sizeof(u64);
2174 }
2175
2176 size += entry * nr;
2177
2178 return size;
2179}
2180
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002181u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182{
2183 struct perf_event *child;
2184 u64 total = 0;
2185
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002186 *enabled = 0;
2187 *running = 0;
2188
Peter Zijlstra6f105812009-11-20 22:19:56 +01002189 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002191 *enabled += event->total_time_enabled +
2192 atomic64_read(&event->child_total_time_enabled);
2193 *running += event->total_time_running +
2194 atomic64_read(&event->child_total_time_running);
2195
2196 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002197 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002198 *enabled += child->total_time_enabled;
2199 *running += child->total_time_running;
2200 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002201 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002202
2203 return total;
2204}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002205EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002207static int perf_event_read_group(struct perf_event *event,
2208 u64 read_format, char __user *buf)
2209{
2210 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002211 int n = 0, size = 0, ret = -EFAULT;
2212 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002213 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002214 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002215
Peter Zijlstra6f105812009-11-20 22:19:56 +01002216 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002217 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002218
2219 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002220 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2221 values[n++] = enabled;
2222 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2223 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002224 values[n++] = count;
2225 if (read_format & PERF_FORMAT_ID)
2226 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002227
2228 size = n * sizeof(u64);
2229
2230 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002231 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232
Peter Zijlstra6f105812009-11-20 22:19:56 +01002233 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002234
2235 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002236 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002238 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002239 if (read_format & PERF_FORMAT_ID)
2240 values[n++] = primary_event_id(sub);
2241
2242 size = n * sizeof(u64);
2243
Stephane Eranian184d3da2009-11-23 21:40:49 -08002244 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002245 ret = -EFAULT;
2246 goto unlock;
2247 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002248
2249 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002251unlock:
2252 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253
Peter Zijlstraabf48682009-11-20 22:19:49 +01002254 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002255}
2256
2257static int perf_event_read_one(struct perf_event *event,
2258 u64 read_format, char __user *buf)
2259{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002260 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002261 u64 values[4];
2262 int n = 0;
2263
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002264 values[n++] = perf_event_read_value(event, &enabled, &running);
2265 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2266 values[n++] = enabled;
2267 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2268 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269 if (read_format & PERF_FORMAT_ID)
2270 values[n++] = primary_event_id(event);
2271
2272 if (copy_to_user(buf, values, n * sizeof(u64)))
2273 return -EFAULT;
2274
2275 return n * sizeof(u64);
2276}
2277
2278/*
2279 * Read the performance event - simple non blocking version for now
2280 */
2281static ssize_t
2282perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2283{
2284 u64 read_format = event->attr.read_format;
2285 int ret;
2286
2287 /*
2288 * Return end-of-file for a read on a event that is in
2289 * error state (i.e. because it was pinned but it couldn't be
2290 * scheduled on to the CPU at some point).
2291 */
2292 if (event->state == PERF_EVENT_STATE_ERROR)
2293 return 0;
2294
2295 if (count < perf_event_read_size(event))
2296 return -ENOSPC;
2297
2298 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299 if (read_format & PERF_FORMAT_GROUP)
2300 ret = perf_event_read_group(event, read_format, buf);
2301 else
2302 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303
2304 return ret;
2305}
2306
2307static ssize_t
2308perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2309{
2310 struct perf_event *event = file->private_data;
2311
2312 return perf_read_hw(event, buf, count);
2313}
2314
2315static unsigned int perf_poll(struct file *file, poll_table *wait)
2316{
2317 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002318 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319 unsigned int events = POLL_HUP;
2320
2321 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002322 buffer = rcu_dereference(event->buffer);
2323 if (buffer)
2324 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 rcu_read_unlock();
2326
2327 poll_wait(file, &event->waitq, wait);
2328
2329 return events;
2330}
2331
2332static void perf_event_reset(struct perf_event *event)
2333{
2334 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002335 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336 perf_event_update_userpage(event);
2337}
2338
2339/*
2340 * Holding the top-level event's child_mutex means that any
2341 * descendant process that has inherited this event will block
2342 * in sync_child_event if it goes to exit, thus satisfying the
2343 * task existence requirements of perf_event_enable/disable.
2344 */
2345static void perf_event_for_each_child(struct perf_event *event,
2346 void (*func)(struct perf_event *))
2347{
2348 struct perf_event *child;
2349
2350 WARN_ON_ONCE(event->ctx->parent_ctx);
2351 mutex_lock(&event->child_mutex);
2352 func(event);
2353 list_for_each_entry(child, &event->child_list, child_list)
2354 func(child);
2355 mutex_unlock(&event->child_mutex);
2356}
2357
2358static void perf_event_for_each(struct perf_event *event,
2359 void (*func)(struct perf_event *))
2360{
2361 struct perf_event_context *ctx = event->ctx;
2362 struct perf_event *sibling;
2363
2364 WARN_ON_ONCE(ctx->parent_ctx);
2365 mutex_lock(&ctx->mutex);
2366 event = event->group_leader;
2367
2368 perf_event_for_each_child(event, func);
2369 func(event);
2370 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2371 perf_event_for_each_child(event, func);
2372 mutex_unlock(&ctx->mutex);
2373}
2374
2375static int perf_event_period(struct perf_event *event, u64 __user *arg)
2376{
2377 struct perf_event_context *ctx = event->ctx;
2378 unsigned long size;
2379 int ret = 0;
2380 u64 value;
2381
2382 if (!event->attr.sample_period)
2383 return -EINVAL;
2384
2385 size = copy_from_user(&value, arg, sizeof(value));
2386 if (size != sizeof(value))
2387 return -EFAULT;
2388
2389 if (!value)
2390 return -EINVAL;
2391
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002392 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393 if (event->attr.freq) {
2394 if (value > sysctl_perf_event_sample_rate) {
2395 ret = -EINVAL;
2396 goto unlock;
2397 }
2398
2399 event->attr.sample_freq = value;
2400 } else {
2401 event->attr.sample_period = value;
2402 event->hw.sample_period = value;
2403 }
2404unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002405 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002406
2407 return ret;
2408}
2409
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002410static const struct file_operations perf_fops;
2411
2412static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2413{
2414 struct file *file;
2415
2416 file = fget_light(fd, fput_needed);
2417 if (!file)
2418 return ERR_PTR(-EBADF);
2419
2420 if (file->f_op != &perf_fops) {
2421 fput_light(file, *fput_needed);
2422 *fput_needed = 0;
2423 return ERR_PTR(-EBADF);
2424 }
2425
2426 return file->private_data;
2427}
2428
2429static int perf_event_set_output(struct perf_event *event,
2430 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002431static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002432
2433static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2434{
2435 struct perf_event *event = file->private_data;
2436 void (*func)(struct perf_event *);
2437 u32 flags = arg;
2438
2439 switch (cmd) {
2440 case PERF_EVENT_IOC_ENABLE:
2441 func = perf_event_enable;
2442 break;
2443 case PERF_EVENT_IOC_DISABLE:
2444 func = perf_event_disable;
2445 break;
2446 case PERF_EVENT_IOC_RESET:
2447 func = perf_event_reset;
2448 break;
2449
2450 case PERF_EVENT_IOC_REFRESH:
2451 return perf_event_refresh(event, arg);
2452
2453 case PERF_EVENT_IOC_PERIOD:
2454 return perf_event_period(event, (u64 __user *)arg);
2455
2456 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002457 {
2458 struct perf_event *output_event = NULL;
2459 int fput_needed = 0;
2460 int ret;
2461
2462 if (arg != -1) {
2463 output_event = perf_fget_light(arg, &fput_needed);
2464 if (IS_ERR(output_event))
2465 return PTR_ERR(output_event);
2466 }
2467
2468 ret = perf_event_set_output(event, output_event);
2469 if (output_event)
2470 fput_light(output_event->filp, fput_needed);
2471
2472 return ret;
2473 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002474
Li Zefan6fb29152009-10-15 11:21:42 +08002475 case PERF_EVENT_IOC_SET_FILTER:
2476 return perf_event_set_filter(event, (void __user *)arg);
2477
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002478 default:
2479 return -ENOTTY;
2480 }
2481
2482 if (flags & PERF_IOC_FLAG_GROUP)
2483 perf_event_for_each(event, func);
2484 else
2485 perf_event_for_each_child(event, func);
2486
2487 return 0;
2488}
2489
2490int perf_event_task_enable(void)
2491{
2492 struct perf_event *event;
2493
2494 mutex_lock(&current->perf_event_mutex);
2495 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2496 perf_event_for_each_child(event, perf_event_enable);
2497 mutex_unlock(&current->perf_event_mutex);
2498
2499 return 0;
2500}
2501
2502int perf_event_task_disable(void)
2503{
2504 struct perf_event *event;
2505
2506 mutex_lock(&current->perf_event_mutex);
2507 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2508 perf_event_for_each_child(event, perf_event_disable);
2509 mutex_unlock(&current->perf_event_mutex);
2510
2511 return 0;
2512}
2513
2514#ifndef PERF_EVENT_INDEX_OFFSET
2515# define PERF_EVENT_INDEX_OFFSET 0
2516#endif
2517
2518static int perf_event_index(struct perf_event *event)
2519{
2520 if (event->state != PERF_EVENT_STATE_ACTIVE)
2521 return 0;
2522
2523 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2524}
2525
2526/*
2527 * Callers need to ensure there can be no nesting of this function, otherwise
2528 * the seqlock logic goes bad. We can not serialize this because the arch
2529 * code calls this from NMI context.
2530 */
2531void perf_event_update_userpage(struct perf_event *event)
2532{
2533 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002534 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002535
2536 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002537 buffer = rcu_dereference(event->buffer);
2538 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002539 goto unlock;
2540
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002541 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002542
2543 /*
2544 * Disable preemption so as to not let the corresponding user-space
2545 * spin too long if we get preempted.
2546 */
2547 preempt_disable();
2548 ++userpg->lock;
2549 barrier();
2550 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002551 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002552 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002553 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002554
2555 userpg->time_enabled = event->total_time_enabled +
2556 atomic64_read(&event->child_total_time_enabled);
2557
2558 userpg->time_running = event->total_time_running +
2559 atomic64_read(&event->child_total_time_running);
2560
2561 barrier();
2562 ++userpg->lock;
2563 preempt_enable();
2564unlock:
2565 rcu_read_unlock();
2566}
2567
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002568static unsigned long perf_data_size(struct perf_buffer *buffer);
2569
2570static void
2571perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2572{
2573 long max_size = perf_data_size(buffer);
2574
2575 if (watermark)
2576 buffer->watermark = min(max_size, watermark);
2577
2578 if (!buffer->watermark)
2579 buffer->watermark = max_size / 2;
2580
2581 if (flags & PERF_BUFFER_WRITABLE)
2582 buffer->writable = 1;
2583
2584 atomic_set(&buffer->refcount, 1);
2585}
2586
Peter Zijlstra906010b2009-09-21 16:08:49 +02002587#ifndef CONFIG_PERF_USE_VMALLOC
2588
2589/*
2590 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2591 */
2592
2593static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002594perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002595{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002596 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002597 return NULL;
2598
2599 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002600 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002601
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002602 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002603}
2604
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002605static void *perf_mmap_alloc_page(int cpu)
2606{
2607 struct page *page;
2608 int node;
2609
2610 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2611 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2612 if (!page)
2613 return NULL;
2614
2615 return page_address(page);
2616}
2617
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002618static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002619perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002620{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002621 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002622 unsigned long size;
2623 int i;
2624
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002625 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002626 size += nr_pages * sizeof(void *);
2627
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002628 buffer = kzalloc(size, GFP_KERNEL);
2629 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002630 goto fail;
2631
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002632 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002633 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002634 goto fail_user_page;
2635
2636 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002637 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002638 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002639 goto fail_data_pages;
2640 }
2641
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002642 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002643
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002644 perf_buffer_init(buffer, watermark, flags);
2645
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002646 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002647
2648fail_data_pages:
2649 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002650 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002651
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002652 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002653
2654fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002655 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656
2657fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002658 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002659}
2660
2661static void perf_mmap_free_page(unsigned long addr)
2662{
2663 struct page *page = virt_to_page((void *)addr);
2664
2665 page->mapping = NULL;
2666 __free_page(page);
2667}
2668
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002669static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002671 int i;
2672
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002673 perf_mmap_free_page((unsigned long)buffer->user_page);
2674 for (i = 0; i < buffer->nr_pages; i++)
2675 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2676 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002677}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002678
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002679static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002680{
2681 return 0;
2682}
2683
Peter Zijlstra906010b2009-09-21 16:08:49 +02002684#else
2685
2686/*
2687 * Back perf_mmap() with vmalloc memory.
2688 *
2689 * Required for architectures that have d-cache aliasing issues.
2690 */
2691
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002692static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002693{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002694 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002695}
2696
Peter Zijlstra906010b2009-09-21 16:08:49 +02002697static 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 > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002701 return NULL;
2702
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002703 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002704}
2705
2706static void perf_mmap_unmark_page(void *addr)
2707{
2708 struct page *page = vmalloc_to_page(addr);
2709
2710 page->mapping = NULL;
2711}
2712
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002713static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002714{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002715 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002716 void *base;
2717 int i, nr;
2718
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002719 buffer = container_of(work, struct perf_buffer, work);
2720 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002721
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002722 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002723 for (i = 0; i < nr + 1; i++)
2724 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2725
2726 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002727 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002728}
2729
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002730static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002731{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002732 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002733}
2734
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002735static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002736perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002737{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002738 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002739 unsigned long size;
2740 void *all_buf;
2741
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002742 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002743 size += sizeof(void *);
2744
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002745 buffer = kzalloc(size, GFP_KERNEL);
2746 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002747 goto fail;
2748
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002749 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002750
2751 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2752 if (!all_buf)
2753 goto fail_all_buf;
2754
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002755 buffer->user_page = all_buf;
2756 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2757 buffer->page_order = ilog2(nr_pages);
2758 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002759
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002760 perf_buffer_init(buffer, watermark, flags);
2761
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002762 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002763
2764fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002765 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002766
2767fail:
2768 return NULL;
2769}
2770
2771#endif
2772
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002773static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002774{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002775 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002776}
2777
Peter Zijlstra906010b2009-09-21 16:08:49 +02002778static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2779{
2780 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002781 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002782 int ret = VM_FAULT_SIGBUS;
2783
2784 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2785 if (vmf->pgoff == 0)
2786 ret = 0;
2787 return ret;
2788 }
2789
2790 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002791 buffer = rcu_dereference(event->buffer);
2792 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002793 goto unlock;
2794
2795 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2796 goto unlock;
2797
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002798 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002799 if (!vmf->page)
2800 goto unlock;
2801
2802 get_page(vmf->page);
2803 vmf->page->mapping = vma->vm_file->f_mapping;
2804 vmf->page->index = vmf->pgoff;
2805
2806 ret = 0;
2807unlock:
2808 rcu_read_unlock();
2809
2810 return ret;
2811}
2812
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002813static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002814{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002815 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002816
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002817 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2818 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002819}
2820
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002821static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002822{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002823 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002824
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002825 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002826 buffer = rcu_dereference(event->buffer);
2827 if (buffer) {
2828 if (!atomic_inc_not_zero(&buffer->refcount))
2829 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002830 }
2831 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002832
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002833 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002834}
2835
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002836static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002837{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002838 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002839 return;
2840
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002841 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842}
2843
2844static void perf_mmap_open(struct vm_area_struct *vma)
2845{
2846 struct perf_event *event = vma->vm_file->private_data;
2847
2848 atomic_inc(&event->mmap_count);
2849}
2850
2851static void perf_mmap_close(struct vm_area_struct *vma)
2852{
2853 struct perf_event *event = vma->vm_file->private_data;
2854
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002855 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002856 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002857 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002858 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002859
Peter Zijlstra906010b2009-09-21 16:08:49 +02002860 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002861 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002862 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002863 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002864
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002865 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002866 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867 }
2868}
2869
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002870static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002871 .open = perf_mmap_open,
2872 .close = perf_mmap_close,
2873 .fault = perf_mmap_fault,
2874 .page_mkwrite = perf_mmap_fault,
2875};
2876
2877static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2878{
2879 struct perf_event *event = file->private_data;
2880 unsigned long user_locked, user_lock_limit;
2881 struct user_struct *user = current_user();
2882 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002883 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002884 unsigned long vma_size;
2885 unsigned long nr_pages;
2886 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002887 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002888
Peter Zijlstrac7920612010-05-18 10:33:24 +02002889 /*
2890 * Don't allow mmap() of inherited per-task counters. This would
2891 * create a performance issue due to all children writing to the
2892 * same buffer.
2893 */
2894 if (event->cpu == -1 && event->attr.inherit)
2895 return -EINVAL;
2896
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897 if (!(vma->vm_flags & VM_SHARED))
2898 return -EINVAL;
2899
2900 vma_size = vma->vm_end - vma->vm_start;
2901 nr_pages = (vma_size / PAGE_SIZE) - 1;
2902
2903 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002904 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905 * can do bitmasks instead of modulo.
2906 */
2907 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2908 return -EINVAL;
2909
2910 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2911 return -EINVAL;
2912
2913 if (vma->vm_pgoff != 0)
2914 return -EINVAL;
2915
2916 WARN_ON_ONCE(event->ctx->parent_ctx);
2917 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002918 if (event->buffer) {
2919 if (event->buffer->nr_pages == nr_pages)
2920 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002921 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002922 ret = -EINVAL;
2923 goto unlock;
2924 }
2925
2926 user_extra = nr_pages + 1;
2927 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2928
2929 /*
2930 * Increase the limit linearly with more CPUs:
2931 */
2932 user_lock_limit *= num_online_cpus();
2933
2934 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2935
2936 extra = 0;
2937 if (user_locked > user_lock_limit)
2938 extra = user_locked - user_lock_limit;
2939
Jiri Slaby78d7d402010-03-05 13:42:54 -08002940 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002941 lock_limit >>= PAGE_SHIFT;
2942 locked = vma->vm_mm->locked_vm + extra;
2943
2944 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2945 !capable(CAP_IPC_LOCK)) {
2946 ret = -EPERM;
2947 goto unlock;
2948 }
2949
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002950 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002951
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002952 if (vma->vm_flags & VM_WRITE)
2953 flags |= PERF_BUFFER_WRITABLE;
2954
2955 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2956 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002957 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002958 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002960 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002961 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002962
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002963 atomic_long_add(user_extra, &user->locked_vm);
2964 event->mmap_locked = extra;
2965 event->mmap_user = get_current_user();
2966 vma->vm_mm->locked_vm += event->mmap_locked;
2967
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002969 if (!ret)
2970 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002971 mutex_unlock(&event->mmap_mutex);
2972
2973 vma->vm_flags |= VM_RESERVED;
2974 vma->vm_ops = &perf_mmap_vmops;
2975
2976 return ret;
2977}
2978
2979static int perf_fasync(int fd, struct file *filp, int on)
2980{
2981 struct inode *inode = filp->f_path.dentry->d_inode;
2982 struct perf_event *event = filp->private_data;
2983 int retval;
2984
2985 mutex_lock(&inode->i_mutex);
2986 retval = fasync_helper(fd, filp, on, &event->fasync);
2987 mutex_unlock(&inode->i_mutex);
2988
2989 if (retval < 0)
2990 return retval;
2991
2992 return 0;
2993}
2994
2995static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01002996 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002997 .release = perf_release,
2998 .read = perf_read,
2999 .poll = perf_poll,
3000 .unlocked_ioctl = perf_ioctl,
3001 .compat_ioctl = perf_ioctl,
3002 .mmap = perf_mmap,
3003 .fasync = perf_fasync,
3004};
3005
3006/*
3007 * Perf event wakeup
3008 *
3009 * If there's data, ensure we set the poll() state and publish everything
3010 * to user-space before waking everybody up.
3011 */
3012
3013void perf_event_wakeup(struct perf_event *event)
3014{
3015 wake_up_all(&event->waitq);
3016
3017 if (event->pending_kill) {
3018 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3019 event->pending_kill = 0;
3020 }
3021}
3022
3023/*
3024 * Pending wakeups
3025 *
3026 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3027 *
3028 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3029 * single linked list and use cmpxchg() to add entries lockless.
3030 */
3031
3032static void perf_pending_event(struct perf_pending_entry *entry)
3033{
3034 struct perf_event *event = container_of(entry,
3035 struct perf_event, pending);
3036
3037 if (event->pending_disable) {
3038 event->pending_disable = 0;
3039 __perf_event_disable(event);
3040 }
3041
3042 if (event->pending_wakeup) {
3043 event->pending_wakeup = 0;
3044 perf_event_wakeup(event);
3045 }
3046}
3047
3048#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3049
3050static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3051 PENDING_TAIL,
3052};
3053
3054static void perf_pending_queue(struct perf_pending_entry *entry,
3055 void (*func)(struct perf_pending_entry *))
3056{
3057 struct perf_pending_entry **head;
3058
3059 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3060 return;
3061
3062 entry->func = func;
3063
3064 head = &get_cpu_var(perf_pending_head);
3065
3066 do {
3067 entry->next = *head;
3068 } while (cmpxchg(head, entry->next, entry) != entry->next);
3069
3070 set_perf_event_pending();
3071
3072 put_cpu_var(perf_pending_head);
3073}
3074
3075static int __perf_pending_run(void)
3076{
3077 struct perf_pending_entry *list;
3078 int nr = 0;
3079
3080 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3081 while (list != PENDING_TAIL) {
3082 void (*func)(struct perf_pending_entry *);
3083 struct perf_pending_entry *entry = list;
3084
3085 list = list->next;
3086
3087 func = entry->func;
3088 entry->next = NULL;
3089 /*
3090 * Ensure we observe the unqueue before we issue the wakeup,
3091 * so that we won't be waiting forever.
3092 * -- see perf_not_pending().
3093 */
3094 smp_wmb();
3095
3096 func(entry);
3097 nr++;
3098 }
3099
3100 return nr;
3101}
3102
3103static inline int perf_not_pending(struct perf_event *event)
3104{
3105 /*
3106 * If we flush on whatever cpu we run, there is a chance we don't
3107 * need to wait.
3108 */
3109 get_cpu();
3110 __perf_pending_run();
3111 put_cpu();
3112
3113 /*
3114 * Ensure we see the proper queue state before going to sleep
3115 * so that we do not miss the wakeup. -- see perf_pending_handle()
3116 */
3117 smp_rmb();
3118 return event->pending.next == NULL;
3119}
3120
3121static void perf_pending_sync(struct perf_event *event)
3122{
3123 wait_event(event->waitq, perf_not_pending(event));
3124}
3125
3126void perf_event_do_pending(void)
3127{
3128 __perf_pending_run();
3129}
3130
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003131/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003132 * We assume there is only KVM supporting the callbacks.
3133 * Later on, we might change it to a list if there is
3134 * another virtualization implementation supporting the callbacks.
3135 */
3136struct perf_guest_info_callbacks *perf_guest_cbs;
3137
3138int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3139{
3140 perf_guest_cbs = cbs;
3141 return 0;
3142}
3143EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3144
3145int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3146{
3147 perf_guest_cbs = NULL;
3148 return 0;
3149}
3150EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3151
3152/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 * Output
3154 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003155static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156 unsigned long offset, unsigned long head)
3157{
3158 unsigned long mask;
3159
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003160 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003161 return true;
3162
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003163 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003164
3165 offset = (offset - tail) & mask;
3166 head = (head - tail) & mask;
3167
3168 if ((int)(head - offset) < 0)
3169 return false;
3170
3171 return true;
3172}
3173
3174static void perf_output_wakeup(struct perf_output_handle *handle)
3175{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003176 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003177
3178 if (handle->nmi) {
3179 handle->event->pending_wakeup = 1;
3180 perf_pending_queue(&handle->event->pending,
3181 perf_pending_event);
3182 } else
3183 perf_event_wakeup(handle->event);
3184}
3185
3186/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003188 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189 * cannot fully serialize things.
3190 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003191 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003192 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003194static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003195{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003196 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197
Peter Zijlstraef607772010-05-18 10:50:41 +02003198 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003199 local_inc(&buffer->nest);
3200 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201}
3202
Peter Zijlstraef607772010-05-18 10:50:41 +02003203static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003205 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207
3208again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003209 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003210
3211 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003212 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003213 */
3214
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003215 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003216 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217
3218 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003219 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003220 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003221 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003222 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003223 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224
Peter Zijlstraef607772010-05-18 10:50:41 +02003225 /*
3226 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003227 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003228 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003229 if (unlikely(head != local_read(&buffer->head))) {
3230 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231 goto again;
3232 }
3233
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003234 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003235 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003236
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003237out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003238 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003239}
3240
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003241__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003242 const void *buf, unsigned int len)
3243{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003244 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003245 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003246
3247 memcpy(handle->addr, buf, size);
3248
3249 len -= size;
3250 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003251 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003252 handle->size -= size;
3253 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003254 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003255
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003256 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003257 handle->page &= buffer->nr_pages - 1;
3258 handle->addr = buffer->data_pages[handle->page];
3259 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003260 }
3261 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003262}
3263
3264int perf_output_begin(struct perf_output_handle *handle,
3265 struct perf_event *event, unsigned int size,
3266 int nmi, int sample)
3267{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003268 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003269 unsigned long tail, offset, head;
3270 int have_lost;
3271 struct {
3272 struct perf_event_header header;
3273 u64 id;
3274 u64 lost;
3275 } lost_event;
3276
3277 rcu_read_lock();
3278 /*
3279 * For inherited events we send all the output towards the parent.
3280 */
3281 if (event->parent)
3282 event = event->parent;
3283
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003284 buffer = rcu_dereference(event->buffer);
3285 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286 goto out;
3287
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003288 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289 handle->event = event;
3290 handle->nmi = nmi;
3291 handle->sample = sample;
3292
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003293 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003294 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003295
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003296 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297 if (have_lost)
3298 size += sizeof(lost_event);
3299
Peter Zijlstraef607772010-05-18 10:50:41 +02003300 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301
3302 do {
3303 /*
3304 * Userspace could choose to issue a mb() before updating the
3305 * tail pointer. So that all reads will be completed before the
3306 * write is issued.
3307 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003308 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003309 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003310 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003311 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003312 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003313 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003314 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003316 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3317 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003319 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3320 handle->page &= buffer->nr_pages - 1;
3321 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3322 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003323 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003324 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326 if (have_lost) {
3327 lost_event.header.type = PERF_RECORD_LOST;
3328 lost_event.header.misc = 0;
3329 lost_event.header.size = sizeof(lost_event);
3330 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003331 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332
3333 perf_output_put(handle, lost_event);
3334 }
3335
3336 return 0;
3337
3338fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003339 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003340 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341out:
3342 rcu_read_unlock();
3343
3344 return -ENOSPC;
3345}
3346
3347void perf_output_end(struct perf_output_handle *handle)
3348{
3349 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003350 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351
3352 int wakeup_events = event->attr.wakeup_events;
3353
3354 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003355 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003356 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003357 local_sub(wakeup_events, &buffer->events);
3358 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359 }
3360 }
3361
Peter Zijlstraef607772010-05-18 10:50:41 +02003362 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 rcu_read_unlock();
3364}
3365
3366static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3367{
3368 /*
3369 * only top level events have the pid namespace they were created in
3370 */
3371 if (event->parent)
3372 event = event->parent;
3373
3374 return task_tgid_nr_ns(p, event->ns);
3375}
3376
3377static u32 perf_event_tid(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_pid_nr_ns(p, event->ns);
3386}
3387
3388static void perf_output_read_one(struct perf_output_handle *handle,
3389 struct perf_event *event)
3390{
3391 u64 read_format = event->attr.read_format;
3392 u64 values[4];
3393 int n = 0;
3394
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003395 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003396 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3397 values[n++] = event->total_time_enabled +
3398 atomic64_read(&event->child_total_time_enabled);
3399 }
3400 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3401 values[n++] = event->total_time_running +
3402 atomic64_read(&event->child_total_time_running);
3403 }
3404 if (read_format & PERF_FORMAT_ID)
3405 values[n++] = primary_event_id(event);
3406
3407 perf_output_copy(handle, values, n * sizeof(u64));
3408}
3409
3410/*
3411 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3412 */
3413static void perf_output_read_group(struct perf_output_handle *handle,
3414 struct perf_event *event)
3415{
3416 struct perf_event *leader = event->group_leader, *sub;
3417 u64 read_format = event->attr.read_format;
3418 u64 values[5];
3419 int n = 0;
3420
3421 values[n++] = 1 + leader->nr_siblings;
3422
3423 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3424 values[n++] = leader->total_time_enabled;
3425
3426 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3427 values[n++] = leader->total_time_running;
3428
3429 if (leader != event)
3430 leader->pmu->read(leader);
3431
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003432 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003433 if (read_format & PERF_FORMAT_ID)
3434 values[n++] = primary_event_id(leader);
3435
3436 perf_output_copy(handle, values, n * sizeof(u64));
3437
3438 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3439 n = 0;
3440
3441 if (sub != event)
3442 sub->pmu->read(sub);
3443
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003444 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003445 if (read_format & PERF_FORMAT_ID)
3446 values[n++] = primary_event_id(sub);
3447
3448 perf_output_copy(handle, values, n * sizeof(u64));
3449 }
3450}
3451
3452static void perf_output_read(struct perf_output_handle *handle,
3453 struct perf_event *event)
3454{
3455 if (event->attr.read_format & PERF_FORMAT_GROUP)
3456 perf_output_read_group(handle, event);
3457 else
3458 perf_output_read_one(handle, event);
3459}
3460
3461void perf_output_sample(struct perf_output_handle *handle,
3462 struct perf_event_header *header,
3463 struct perf_sample_data *data,
3464 struct perf_event *event)
3465{
3466 u64 sample_type = data->type;
3467
3468 perf_output_put(handle, *header);
3469
3470 if (sample_type & PERF_SAMPLE_IP)
3471 perf_output_put(handle, data->ip);
3472
3473 if (sample_type & PERF_SAMPLE_TID)
3474 perf_output_put(handle, data->tid_entry);
3475
3476 if (sample_type & PERF_SAMPLE_TIME)
3477 perf_output_put(handle, data->time);
3478
3479 if (sample_type & PERF_SAMPLE_ADDR)
3480 perf_output_put(handle, data->addr);
3481
3482 if (sample_type & PERF_SAMPLE_ID)
3483 perf_output_put(handle, data->id);
3484
3485 if (sample_type & PERF_SAMPLE_STREAM_ID)
3486 perf_output_put(handle, data->stream_id);
3487
3488 if (sample_type & PERF_SAMPLE_CPU)
3489 perf_output_put(handle, data->cpu_entry);
3490
3491 if (sample_type & PERF_SAMPLE_PERIOD)
3492 perf_output_put(handle, data->period);
3493
3494 if (sample_type & PERF_SAMPLE_READ)
3495 perf_output_read(handle, event);
3496
3497 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3498 if (data->callchain) {
3499 int size = 1;
3500
3501 if (data->callchain)
3502 size += data->callchain->nr;
3503
3504 size *= sizeof(u64);
3505
3506 perf_output_copy(handle, data->callchain, size);
3507 } else {
3508 u64 nr = 0;
3509 perf_output_put(handle, nr);
3510 }
3511 }
3512
3513 if (sample_type & PERF_SAMPLE_RAW) {
3514 if (data->raw) {
3515 perf_output_put(handle, data->raw->size);
3516 perf_output_copy(handle, data->raw->data,
3517 data->raw->size);
3518 } else {
3519 struct {
3520 u32 size;
3521 u32 data;
3522 } raw = {
3523 .size = sizeof(u32),
3524 .data = 0,
3525 };
3526 perf_output_put(handle, raw);
3527 }
3528 }
3529}
3530
3531void perf_prepare_sample(struct perf_event_header *header,
3532 struct perf_sample_data *data,
3533 struct perf_event *event,
3534 struct pt_regs *regs)
3535{
3536 u64 sample_type = event->attr.sample_type;
3537
3538 data->type = sample_type;
3539
3540 header->type = PERF_RECORD_SAMPLE;
3541 header->size = sizeof(*header);
3542
3543 header->misc = 0;
3544 header->misc |= perf_misc_flags(regs);
3545
3546 if (sample_type & PERF_SAMPLE_IP) {
3547 data->ip = perf_instruction_pointer(regs);
3548
3549 header->size += sizeof(data->ip);
3550 }
3551
3552 if (sample_type & PERF_SAMPLE_TID) {
3553 /* namespace issues */
3554 data->tid_entry.pid = perf_event_pid(event, current);
3555 data->tid_entry.tid = perf_event_tid(event, current);
3556
3557 header->size += sizeof(data->tid_entry);
3558 }
3559
3560 if (sample_type & PERF_SAMPLE_TIME) {
3561 data->time = perf_clock();
3562
3563 header->size += sizeof(data->time);
3564 }
3565
3566 if (sample_type & PERF_SAMPLE_ADDR)
3567 header->size += sizeof(data->addr);
3568
3569 if (sample_type & PERF_SAMPLE_ID) {
3570 data->id = primary_event_id(event);
3571
3572 header->size += sizeof(data->id);
3573 }
3574
3575 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3576 data->stream_id = event->id;
3577
3578 header->size += sizeof(data->stream_id);
3579 }
3580
3581 if (sample_type & PERF_SAMPLE_CPU) {
3582 data->cpu_entry.cpu = raw_smp_processor_id();
3583 data->cpu_entry.reserved = 0;
3584
3585 header->size += sizeof(data->cpu_entry);
3586 }
3587
3588 if (sample_type & PERF_SAMPLE_PERIOD)
3589 header->size += sizeof(data->period);
3590
3591 if (sample_type & PERF_SAMPLE_READ)
3592 header->size += perf_event_read_size(event);
3593
3594 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3595 int size = 1;
3596
3597 data->callchain = perf_callchain(regs);
3598
3599 if (data->callchain)
3600 size += data->callchain->nr;
3601
3602 header->size += size * sizeof(u64);
3603 }
3604
3605 if (sample_type & PERF_SAMPLE_RAW) {
3606 int size = sizeof(u32);
3607
3608 if (data->raw)
3609 size += data->raw->size;
3610 else
3611 size += sizeof(u32);
3612
3613 WARN_ON_ONCE(size & (sizeof(u64)-1));
3614 header->size += size;
3615 }
3616}
3617
3618static void perf_event_output(struct perf_event *event, int nmi,
3619 struct perf_sample_data *data,
3620 struct pt_regs *regs)
3621{
3622 struct perf_output_handle handle;
3623 struct perf_event_header header;
3624
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003625 /* protect the callchain buffers */
3626 rcu_read_lock();
3627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003628 perf_prepare_sample(&header, data, event, regs);
3629
3630 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003631 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003632
3633 perf_output_sample(&handle, &header, data, event);
3634
3635 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003636
3637exit:
3638 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003639}
3640
3641/*
3642 * read event_id
3643 */
3644
3645struct perf_read_event {
3646 struct perf_event_header header;
3647
3648 u32 pid;
3649 u32 tid;
3650};
3651
3652static void
3653perf_event_read_event(struct perf_event *event,
3654 struct task_struct *task)
3655{
3656 struct perf_output_handle handle;
3657 struct perf_read_event read_event = {
3658 .header = {
3659 .type = PERF_RECORD_READ,
3660 .misc = 0,
3661 .size = sizeof(read_event) + perf_event_read_size(event),
3662 },
3663 .pid = perf_event_pid(event, task),
3664 .tid = perf_event_tid(event, task),
3665 };
3666 int ret;
3667
3668 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3669 if (ret)
3670 return;
3671
3672 perf_output_put(&handle, read_event);
3673 perf_output_read(&handle, event);
3674
3675 perf_output_end(&handle);
3676}
3677
3678/*
3679 * task tracking -- fork/exit
3680 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003681 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003682 */
3683
3684struct perf_task_event {
3685 struct task_struct *task;
3686 struct perf_event_context *task_ctx;
3687
3688 struct {
3689 struct perf_event_header header;
3690
3691 u32 pid;
3692 u32 ppid;
3693 u32 tid;
3694 u32 ptid;
3695 u64 time;
3696 } event_id;
3697};
3698
3699static void perf_event_task_output(struct perf_event *event,
3700 struct perf_task_event *task_event)
3701{
3702 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003703 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003704 int size, ret;
3705
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706 size = task_event->event_id.header.size;
3707 ret = perf_output_begin(&handle, event, size, 0, 0);
3708
Peter Zijlstraef607772010-05-18 10:50:41 +02003709 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003710 return;
3711
3712 task_event->event_id.pid = perf_event_pid(event, task);
3713 task_event->event_id.ppid = perf_event_pid(event, current);
3714
3715 task_event->event_id.tid = perf_event_tid(event, task);
3716 task_event->event_id.ptid = perf_event_tid(event, current);
3717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003718 perf_output_put(&handle, task_event->event_id);
3719
3720 perf_output_end(&handle);
3721}
3722
3723static int perf_event_task_match(struct perf_event *event)
3724{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003725 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003726 return 0;
3727
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003728 if (event->cpu != -1 && event->cpu != smp_processor_id())
3729 return 0;
3730
Eric B Munson3af9e852010-05-18 15:30:49 +01003731 if (event->attr.comm || event->attr.mmap ||
3732 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003733 return 1;
3734
3735 return 0;
3736}
3737
3738static void perf_event_task_ctx(struct perf_event_context *ctx,
3739 struct perf_task_event *task_event)
3740{
3741 struct perf_event *event;
3742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003743 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3744 if (perf_event_task_match(event))
3745 perf_event_task_output(event, task_event);
3746 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003747}
3748
3749static void perf_event_task_event(struct perf_task_event *task_event)
3750{
3751 struct perf_cpu_context *cpuctx;
3752 struct perf_event_context *ctx = task_event->task_ctx;
3753
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003754 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003755 cpuctx = &get_cpu_var(perf_cpu_context);
3756 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003758 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 if (ctx)
3760 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003761 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003762 rcu_read_unlock();
3763}
3764
3765static void perf_event_task(struct task_struct *task,
3766 struct perf_event_context *task_ctx,
3767 int new)
3768{
3769 struct perf_task_event task_event;
3770
3771 if (!atomic_read(&nr_comm_events) &&
3772 !atomic_read(&nr_mmap_events) &&
3773 !atomic_read(&nr_task_events))
3774 return;
3775
3776 task_event = (struct perf_task_event){
3777 .task = task,
3778 .task_ctx = task_ctx,
3779 .event_id = {
3780 .header = {
3781 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3782 .misc = 0,
3783 .size = sizeof(task_event.event_id),
3784 },
3785 /* .pid */
3786 /* .ppid */
3787 /* .tid */
3788 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003789 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003790 },
3791 };
3792
3793 perf_event_task_event(&task_event);
3794}
3795
3796void perf_event_fork(struct task_struct *task)
3797{
3798 perf_event_task(task, NULL, 1);
3799}
3800
3801/*
3802 * comm tracking
3803 */
3804
3805struct perf_comm_event {
3806 struct task_struct *task;
3807 char *comm;
3808 int comm_size;
3809
3810 struct {
3811 struct perf_event_header header;
3812
3813 u32 pid;
3814 u32 tid;
3815 } event_id;
3816};
3817
3818static void perf_event_comm_output(struct perf_event *event,
3819 struct perf_comm_event *comm_event)
3820{
3821 struct perf_output_handle handle;
3822 int size = comm_event->event_id.header.size;
3823 int ret = perf_output_begin(&handle, event, size, 0, 0);
3824
3825 if (ret)
3826 return;
3827
3828 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3829 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3830
3831 perf_output_put(&handle, comm_event->event_id);
3832 perf_output_copy(&handle, comm_event->comm,
3833 comm_event->comm_size);
3834 perf_output_end(&handle);
3835}
3836
3837static int perf_event_comm_match(struct perf_event *event)
3838{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003839 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003840 return 0;
3841
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003842 if (event->cpu != -1 && event->cpu != smp_processor_id())
3843 return 0;
3844
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003845 if (event->attr.comm)
3846 return 1;
3847
3848 return 0;
3849}
3850
3851static void perf_event_comm_ctx(struct perf_event_context *ctx,
3852 struct perf_comm_event *comm_event)
3853{
3854 struct perf_event *event;
3855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003856 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3857 if (perf_event_comm_match(event))
3858 perf_event_comm_output(event, comm_event);
3859 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003860}
3861
3862static void perf_event_comm_event(struct perf_comm_event *comm_event)
3863{
3864 struct perf_cpu_context *cpuctx;
3865 struct perf_event_context *ctx;
3866 unsigned int size;
3867 char comm[TASK_COMM_LEN];
3868
3869 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003870 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871 size = ALIGN(strlen(comm)+1, sizeof(u64));
3872
3873 comm_event->comm = comm;
3874 comm_event->comm_size = size;
3875
3876 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3877
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003878 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003879 cpuctx = &get_cpu_var(perf_cpu_context);
3880 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881 ctx = rcu_dereference(current->perf_event_ctxp);
3882 if (ctx)
3883 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003884 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885 rcu_read_unlock();
3886}
3887
3888void perf_event_comm(struct task_struct *task)
3889{
3890 struct perf_comm_event comm_event;
3891
3892 if (task->perf_event_ctxp)
3893 perf_event_enable_on_exec(task);
3894
3895 if (!atomic_read(&nr_comm_events))
3896 return;
3897
3898 comm_event = (struct perf_comm_event){
3899 .task = task,
3900 /* .comm */
3901 /* .comm_size */
3902 .event_id = {
3903 .header = {
3904 .type = PERF_RECORD_COMM,
3905 .misc = 0,
3906 /* .size */
3907 },
3908 /* .pid */
3909 /* .tid */
3910 },
3911 };
3912
3913 perf_event_comm_event(&comm_event);
3914}
3915
3916/*
3917 * mmap tracking
3918 */
3919
3920struct perf_mmap_event {
3921 struct vm_area_struct *vma;
3922
3923 const char *file_name;
3924 int file_size;
3925
3926 struct {
3927 struct perf_event_header header;
3928
3929 u32 pid;
3930 u32 tid;
3931 u64 start;
3932 u64 len;
3933 u64 pgoff;
3934 } event_id;
3935};
3936
3937static void perf_event_mmap_output(struct perf_event *event,
3938 struct perf_mmap_event *mmap_event)
3939{
3940 struct perf_output_handle handle;
3941 int size = mmap_event->event_id.header.size;
3942 int ret = perf_output_begin(&handle, event, size, 0, 0);
3943
3944 if (ret)
3945 return;
3946
3947 mmap_event->event_id.pid = perf_event_pid(event, current);
3948 mmap_event->event_id.tid = perf_event_tid(event, current);
3949
3950 perf_output_put(&handle, mmap_event->event_id);
3951 perf_output_copy(&handle, mmap_event->file_name,
3952 mmap_event->file_size);
3953 perf_output_end(&handle);
3954}
3955
3956static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003957 struct perf_mmap_event *mmap_event,
3958 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003960 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003961 return 0;
3962
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003963 if (event->cpu != -1 && event->cpu != smp_processor_id())
3964 return 0;
3965
Eric B Munson3af9e852010-05-18 15:30:49 +01003966 if ((!executable && event->attr.mmap_data) ||
3967 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003968 return 1;
3969
3970 return 0;
3971}
3972
3973static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01003974 struct perf_mmap_event *mmap_event,
3975 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003976{
3977 struct perf_event *event;
3978
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01003980 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003981 perf_event_mmap_output(event, mmap_event);
3982 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003983}
3984
3985static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3986{
3987 struct perf_cpu_context *cpuctx;
3988 struct perf_event_context *ctx;
3989 struct vm_area_struct *vma = mmap_event->vma;
3990 struct file *file = vma->vm_file;
3991 unsigned int size;
3992 char tmp[16];
3993 char *buf = NULL;
3994 const char *name;
3995
3996 memset(tmp, 0, sizeof(tmp));
3997
3998 if (file) {
3999 /*
4000 * d_path works from the end of the buffer backwards, so we
4001 * need to add enough zero bytes after the string to handle
4002 * the 64bit alignment we do later.
4003 */
4004 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4005 if (!buf) {
4006 name = strncpy(tmp, "//enomem", sizeof(tmp));
4007 goto got_name;
4008 }
4009 name = d_path(&file->f_path, buf, PATH_MAX);
4010 if (IS_ERR(name)) {
4011 name = strncpy(tmp, "//toolong", sizeof(tmp));
4012 goto got_name;
4013 }
4014 } else {
4015 if (arch_vma_name(mmap_event->vma)) {
4016 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4017 sizeof(tmp));
4018 goto got_name;
4019 }
4020
4021 if (!vma->vm_mm) {
4022 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4023 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004024 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4025 vma->vm_end >= vma->vm_mm->brk) {
4026 name = strncpy(tmp, "[heap]", sizeof(tmp));
4027 goto got_name;
4028 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4029 vma->vm_end >= vma->vm_mm->start_stack) {
4030 name = strncpy(tmp, "[stack]", sizeof(tmp));
4031 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004032 }
4033
4034 name = strncpy(tmp, "//anon", sizeof(tmp));
4035 goto got_name;
4036 }
4037
4038got_name:
4039 size = ALIGN(strlen(name)+1, sizeof(u64));
4040
4041 mmap_event->file_name = name;
4042 mmap_event->file_size = size;
4043
4044 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4045
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004046 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004047 cpuctx = &get_cpu_var(perf_cpu_context);
Eric B Munson3af9e852010-05-18 15:30:49 +01004048 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049 ctx = rcu_dereference(current->perf_event_ctxp);
4050 if (ctx)
Eric B Munson3af9e852010-05-18 15:30:49 +01004051 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004052 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004053 rcu_read_unlock();
4054
4055 kfree(buf);
4056}
4057
Eric B Munson3af9e852010-05-18 15:30:49 +01004058void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004059{
4060 struct perf_mmap_event mmap_event;
4061
4062 if (!atomic_read(&nr_mmap_events))
4063 return;
4064
4065 mmap_event = (struct perf_mmap_event){
4066 .vma = vma,
4067 /* .file_name */
4068 /* .file_size */
4069 .event_id = {
4070 .header = {
4071 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004072 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004073 /* .size */
4074 },
4075 /* .pid */
4076 /* .tid */
4077 .start = vma->vm_start,
4078 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004079 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004080 },
4081 };
4082
4083 perf_event_mmap_event(&mmap_event);
4084}
4085
4086/*
4087 * IRQ throttle logging
4088 */
4089
4090static void perf_log_throttle(struct perf_event *event, int enable)
4091{
4092 struct perf_output_handle handle;
4093 int ret;
4094
4095 struct {
4096 struct perf_event_header header;
4097 u64 time;
4098 u64 id;
4099 u64 stream_id;
4100 } throttle_event = {
4101 .header = {
4102 .type = PERF_RECORD_THROTTLE,
4103 .misc = 0,
4104 .size = sizeof(throttle_event),
4105 },
4106 .time = perf_clock(),
4107 .id = primary_event_id(event),
4108 .stream_id = event->id,
4109 };
4110
4111 if (enable)
4112 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4113
4114 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4115 if (ret)
4116 return;
4117
4118 perf_output_put(&handle, throttle_event);
4119 perf_output_end(&handle);
4120}
4121
4122/*
4123 * Generic event overflow handling, sampling.
4124 */
4125
4126static int __perf_event_overflow(struct perf_event *event, int nmi,
4127 int throttle, struct perf_sample_data *data,
4128 struct pt_regs *regs)
4129{
4130 int events = atomic_read(&event->event_limit);
4131 struct hw_perf_event *hwc = &event->hw;
4132 int ret = 0;
4133
4134 throttle = (throttle && event->pmu->unthrottle != NULL);
4135
4136 if (!throttle) {
4137 hwc->interrupts++;
4138 } else {
4139 if (hwc->interrupts != MAX_INTERRUPTS) {
4140 hwc->interrupts++;
4141 if (HZ * hwc->interrupts >
4142 (u64)sysctl_perf_event_sample_rate) {
4143 hwc->interrupts = MAX_INTERRUPTS;
4144 perf_log_throttle(event, 0);
4145 ret = 1;
4146 }
4147 } else {
4148 /*
4149 * Keep re-disabling events even though on the previous
4150 * pass we disabled it - just in case we raced with a
4151 * sched-in and the event got enabled again:
4152 */
4153 ret = 1;
4154 }
4155 }
4156
4157 if (event->attr.freq) {
4158 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004159 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160
Peter Zijlstraabd50712010-01-26 18:50:16 +01004161 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004162
Peter Zijlstraabd50712010-01-26 18:50:16 +01004163 if (delta > 0 && delta < 2*TICK_NSEC)
4164 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004165 }
4166
4167 /*
4168 * XXX event_limit might not quite work as expected on inherited
4169 * events
4170 */
4171
4172 event->pending_kill = POLL_IN;
4173 if (events && atomic_dec_and_test(&event->event_limit)) {
4174 ret = 1;
4175 event->pending_kill = POLL_HUP;
4176 if (nmi) {
4177 event->pending_disable = 1;
4178 perf_pending_queue(&event->pending,
4179 perf_pending_event);
4180 } else
4181 perf_event_disable(event);
4182 }
4183
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004184 if (event->overflow_handler)
4185 event->overflow_handler(event, nmi, data, regs);
4186 else
4187 perf_event_output(event, nmi, data, regs);
4188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004189 return ret;
4190}
4191
4192int perf_event_overflow(struct perf_event *event, int nmi,
4193 struct perf_sample_data *data,
4194 struct pt_regs *regs)
4195{
4196 return __perf_event_overflow(event, nmi, 1, data, regs);
4197}
4198
4199/*
4200 * Generic software event infrastructure
4201 */
4202
4203/*
4204 * We directly increment event->count and keep a second value in
4205 * event->hw.period_left to count intervals. This period event
4206 * is kept in the range [-sample_period, 0] so that we can use the
4207 * sign as trigger.
4208 */
4209
4210static u64 perf_swevent_set_period(struct perf_event *event)
4211{
4212 struct hw_perf_event *hwc = &event->hw;
4213 u64 period = hwc->last_period;
4214 u64 nr, offset;
4215 s64 old, val;
4216
4217 hwc->last_period = hwc->sample_period;
4218
4219again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004220 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004221 if (val < 0)
4222 return 0;
4223
4224 nr = div64_u64(period + val, period);
4225 offset = nr * period;
4226 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004227 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228 goto again;
4229
4230 return nr;
4231}
4232
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004233static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004234 int nmi, struct perf_sample_data *data,
4235 struct pt_regs *regs)
4236{
4237 struct hw_perf_event *hwc = &event->hw;
4238 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004239
4240 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004241 if (!overflow)
4242 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004243
4244 if (hwc->interrupts == MAX_INTERRUPTS)
4245 return;
4246
4247 for (; overflow; overflow--) {
4248 if (__perf_event_overflow(event, nmi, throttle,
4249 data, regs)) {
4250 /*
4251 * We inhibit the overflow from happening when
4252 * hwc->interrupts == MAX_INTERRUPTS.
4253 */
4254 break;
4255 }
4256 throttle = 1;
4257 }
4258}
4259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004260static void perf_swevent_add(struct perf_event *event, u64 nr,
4261 int nmi, struct perf_sample_data *data,
4262 struct pt_regs *regs)
4263{
4264 struct hw_perf_event *hwc = &event->hw;
4265
Peter Zijlstrae7850592010-05-21 14:43:08 +02004266 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268 if (!regs)
4269 return;
4270
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004271 if (!hwc->sample_period)
4272 return;
4273
4274 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4275 return perf_swevent_overflow(event, 1, nmi, data, regs);
4276
Peter Zijlstrae7850592010-05-21 14:43:08 +02004277 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004278 return;
4279
4280 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004281}
4282
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004283static int perf_exclude_event(struct perf_event *event,
4284 struct pt_regs *regs)
4285{
4286 if (regs) {
4287 if (event->attr.exclude_user && user_mode(regs))
4288 return 1;
4289
4290 if (event->attr.exclude_kernel && !user_mode(regs))
4291 return 1;
4292 }
4293
4294 return 0;
4295}
4296
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004297static int perf_swevent_match(struct perf_event *event,
4298 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004299 u32 event_id,
4300 struct perf_sample_data *data,
4301 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004303 if (event->attr.type != type)
4304 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306 if (event->attr.config != event_id)
4307 return 0;
4308
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004309 if (perf_exclude_event(event, regs))
4310 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311
4312 return 1;
4313}
4314
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004315static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004316{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004317 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004318
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004319 return hash_64(val, SWEVENT_HLIST_BITS);
4320}
4321
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004322static inline struct hlist_head *
4323__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004324{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004325 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004326
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004327 return &hlist->heads[hash];
4328}
4329
4330/* For the read side: events when they trigger */
4331static inline struct hlist_head *
4332find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4333{
4334 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004335
4336 hlist = rcu_dereference(ctx->swevent_hlist);
4337 if (!hlist)
4338 return NULL;
4339
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004340 return __find_swevent_head(hlist, type, event_id);
4341}
4342
4343/* For the event head insertion and removal in the hlist */
4344static inline struct hlist_head *
4345find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4346{
4347 struct swevent_hlist *hlist;
4348 u32 event_id = event->attr.config;
4349 u64 type = event->attr.type;
4350
4351 /*
4352 * Event scheduling is always serialized against hlist allocation
4353 * and release. Which makes the protected version suitable here.
4354 * The context lock guarantees that.
4355 */
4356 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4357 lockdep_is_held(&event->ctx->lock));
4358 if (!hlist)
4359 return NULL;
4360
4361 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004362}
4363
4364static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4365 u64 nr, int nmi,
4366 struct perf_sample_data *data,
4367 struct pt_regs *regs)
4368{
4369 struct perf_cpu_context *cpuctx;
4370 struct perf_event *event;
4371 struct hlist_node *node;
4372 struct hlist_head *head;
4373
4374 cpuctx = &__get_cpu_var(perf_cpu_context);
4375
4376 rcu_read_lock();
4377
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004378 head = find_swevent_head_rcu(cpuctx, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004379
4380 if (!head)
4381 goto end;
4382
4383 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004384 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004385 perf_swevent_add(event, nr, nmi, data, regs);
4386 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004387end:
4388 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004389}
4390
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004391int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004392{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004393 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004394
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004395 return get_recursion_context(cpuctx->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004396}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004397EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004398
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004399void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004400{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004401 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004402
4403 put_recursion_context(cpuctx->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004404}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4407 struct pt_regs *regs, u64 addr)
4408{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004409 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004410 int rctx;
4411
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004412 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004413 rctx = perf_swevent_get_recursion_context();
4414 if (rctx < 0)
4415 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004416
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004417 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004418
4419 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004420
4421 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004422 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423}
4424
4425static void perf_swevent_read(struct perf_event *event)
4426{
4427}
4428
4429static int perf_swevent_enable(struct perf_event *event)
4430{
4431 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004432 struct perf_cpu_context *cpuctx;
4433 struct hlist_head *head;
4434
4435 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436
4437 if (hwc->sample_period) {
4438 hwc->last_period = hwc->sample_period;
4439 perf_swevent_set_period(event);
4440 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004441
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004442 head = find_swevent_head(cpuctx, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004443 if (WARN_ON_ONCE(!head))
4444 return -EINVAL;
4445
4446 hlist_add_head_rcu(&event->hlist_entry, head);
4447
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448 return 0;
4449}
4450
4451static void perf_swevent_disable(struct perf_event *event)
4452{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004453 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454}
4455
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004456static void perf_swevent_void(struct perf_event *event)
4457{
4458}
4459
4460static int perf_swevent_int(struct perf_event *event)
4461{
4462 return 0;
4463}
4464
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004465/* Deref the hlist from the update side */
4466static inline struct swevent_hlist *
4467swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4468{
4469 return rcu_dereference_protected(cpuctx->swevent_hlist,
4470 lockdep_is_held(&cpuctx->hlist_mutex));
4471}
4472
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004473static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4474{
4475 struct swevent_hlist *hlist;
4476
4477 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4478 kfree(hlist);
4479}
4480
4481static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4482{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004483 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004484
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004485 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004486 return;
4487
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004488 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4489 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4490}
4491
4492static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4493{
4494 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4495
4496 mutex_lock(&cpuctx->hlist_mutex);
4497
4498 if (!--cpuctx->hlist_refcount)
4499 swevent_hlist_release(cpuctx);
4500
4501 mutex_unlock(&cpuctx->hlist_mutex);
4502}
4503
4504static void swevent_hlist_put(struct perf_event *event)
4505{
4506 int cpu;
4507
4508 if (event->cpu != -1) {
4509 swevent_hlist_put_cpu(event, event->cpu);
4510 return;
4511 }
4512
4513 for_each_possible_cpu(cpu)
4514 swevent_hlist_put_cpu(event, cpu);
4515}
4516
4517static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4518{
4519 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4520 int err = 0;
4521
4522 mutex_lock(&cpuctx->hlist_mutex);
4523
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004524 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004525 struct swevent_hlist *hlist;
4526
4527 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4528 if (!hlist) {
4529 err = -ENOMEM;
4530 goto exit;
4531 }
4532 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4533 }
4534 cpuctx->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004535exit:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004536 mutex_unlock(&cpuctx->hlist_mutex);
4537
4538 return err;
4539}
4540
4541static int swevent_hlist_get(struct perf_event *event)
4542{
4543 int err;
4544 int cpu, failed_cpu;
4545
4546 if (event->cpu != -1)
4547 return swevent_hlist_get_cpu(event, event->cpu);
4548
4549 get_online_cpus();
4550 for_each_possible_cpu(cpu) {
4551 err = swevent_hlist_get_cpu(event, cpu);
4552 if (err) {
4553 failed_cpu = cpu;
4554 goto fail;
4555 }
4556 }
4557 put_online_cpus();
4558
4559 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004560fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004561 for_each_possible_cpu(cpu) {
4562 if (cpu == failed_cpu)
4563 break;
4564 swevent_hlist_put_cpu(event, cpu);
4565 }
4566
4567 put_online_cpus();
4568 return err;
4569}
4570
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004571atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004572
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004573static void sw_perf_event_destroy(struct perf_event *event)
4574{
4575 u64 event_id = event->attr.config;
4576
4577 WARN_ON(event->parent);
4578
4579 atomic_dec(&perf_swevent_enabled[event_id]);
4580 swevent_hlist_put(event);
4581}
4582
4583static int perf_swevent_init(struct perf_event *event)
4584{
4585 int event_id = event->attr.config;
4586
4587 if (event->attr.type != PERF_TYPE_SOFTWARE)
4588 return -ENOENT;
4589
4590 switch (event_id) {
4591 case PERF_COUNT_SW_CPU_CLOCK:
4592 case PERF_COUNT_SW_TASK_CLOCK:
4593 return -ENOENT;
4594
4595 default:
4596 break;
4597 }
4598
4599 if (event_id > PERF_COUNT_SW_MAX)
4600 return -ENOENT;
4601
4602 if (!event->parent) {
4603 int err;
4604
4605 err = swevent_hlist_get(event);
4606 if (err)
4607 return err;
4608
4609 atomic_inc(&perf_swevent_enabled[event_id]);
4610 event->destroy = sw_perf_event_destroy;
4611 }
4612
4613 return 0;
4614}
4615
4616static struct pmu perf_swevent = {
4617 .event_init = perf_swevent_init,
4618 .enable = perf_swevent_enable,
4619 .disable = perf_swevent_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004620 .start = perf_swevent_int,
4621 .stop = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004622 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004623 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004624};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004625
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004626#ifdef CONFIG_EVENT_TRACING
4627
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004628static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004629 struct perf_sample_data *data)
4630{
4631 void *record = data->raw->data;
4632
4633 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4634 return 1;
4635 return 0;
4636}
4637
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004638static int perf_tp_event_match(struct perf_event *event,
4639 struct perf_sample_data *data,
4640 struct pt_regs *regs)
4641{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004642 /*
4643 * All tracepoints are from kernel-space.
4644 */
4645 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004646 return 0;
4647
4648 if (!perf_tp_filter_match(event, data))
4649 return 0;
4650
4651 return 1;
4652}
4653
4654void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004655 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004656{
4657 struct perf_sample_data data;
4658 struct perf_event *event;
4659 struct hlist_node *node;
4660
4661 struct perf_raw_record raw = {
4662 .size = entry_size,
4663 .data = record,
4664 };
4665
4666 perf_sample_data_init(&data, addr);
4667 data.raw = &raw;
4668
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004669 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4670 if (perf_tp_event_match(event, &data, regs))
4671 perf_swevent_add(event, count, 1, &data, regs);
4672 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004673
4674 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004675}
4676EXPORT_SYMBOL_GPL(perf_tp_event);
4677
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004678static void tp_perf_event_destroy(struct perf_event *event)
4679{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004680 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004681}
4682
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004683static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004684{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004685 int err;
4686
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004687 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4688 return -ENOENT;
4689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004690 /*
4691 * Raw tracepoint data is a severe data leak, only allow root to
4692 * have these.
4693 */
4694 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4695 perf_paranoid_tracepoint_raw() &&
4696 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004697 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004699 err = perf_trace_init(event);
4700 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004701 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004702
4703 event->destroy = tp_perf_event_destroy;
4704
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004705 return 0;
4706}
4707
4708static struct pmu perf_tracepoint = {
4709 .event_init = perf_tp_event_init,
4710 .enable = perf_trace_enable,
4711 .disable = perf_trace_disable,
4712 .start = perf_swevent_int,
4713 .stop = perf_swevent_void,
4714 .read = perf_swevent_read,
4715 .unthrottle = perf_swevent_void,
4716};
4717
4718static inline void perf_tp_register(void)
4719{
4720 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004721}
Li Zefan6fb29152009-10-15 11:21:42 +08004722
4723static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4724{
4725 char *filter_str;
4726 int ret;
4727
4728 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4729 return -EINVAL;
4730
4731 filter_str = strndup_user(arg, PAGE_SIZE);
4732 if (IS_ERR(filter_str))
4733 return PTR_ERR(filter_str);
4734
4735 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4736
4737 kfree(filter_str);
4738 return ret;
4739}
4740
4741static void perf_event_free_filter(struct perf_event *event)
4742{
4743 ftrace_profile_free_filter(event);
4744}
4745
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004746#else
Li Zefan6fb29152009-10-15 11:21:42 +08004747
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004748static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004749{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004750}
Li Zefan6fb29152009-10-15 11:21:42 +08004751
4752static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4753{
4754 return -ENOENT;
4755}
4756
4757static void perf_event_free_filter(struct perf_event *event)
4758{
4759}
4760
Li Zefan07b139c2009-12-21 14:27:35 +08004761#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004762
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004763#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004764void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004765{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004766 struct perf_sample_data sample;
4767 struct pt_regs *regs = data;
4768
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004769 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004770
4771 if (!perf_exclude_event(bp, regs))
4772 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004773}
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004774#endif
4775
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004776/*
4777 * hrtimer based swevent callback
4778 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004779
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004780static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004781{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004782 enum hrtimer_restart ret = HRTIMER_RESTART;
4783 struct perf_sample_data data;
4784 struct pt_regs *regs;
4785 struct perf_event *event;
4786 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004787
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004788 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4789 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004790
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004791 perf_sample_data_init(&data, 0);
4792 data.period = event->hw.last_period;
4793 regs = get_irq_regs();
4794
4795 if (regs && !perf_exclude_event(event, regs)) {
4796 if (!(event->attr.exclude_idle && current->pid == 0))
4797 if (perf_event_overflow(event, 0, &data, regs))
4798 ret = HRTIMER_NORESTART;
4799 }
4800
4801 period = max_t(u64, 10000, event->hw.sample_period);
4802 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4803
4804 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004805}
4806
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004807static void perf_swevent_start_hrtimer(struct perf_event *event)
4808{
4809 struct hw_perf_event *hwc = &event->hw;
4810
4811 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4812 hwc->hrtimer.function = perf_swevent_hrtimer;
4813 if (hwc->sample_period) {
4814 u64 period;
4815
4816 if (hwc->remaining) {
4817 if (hwc->remaining < 0)
4818 period = 10000;
4819 else
4820 period = hwc->remaining;
4821 hwc->remaining = 0;
4822 } else {
4823 period = max_t(u64, 10000, hwc->sample_period);
4824 }
4825 __hrtimer_start_range_ns(&hwc->hrtimer,
4826 ns_to_ktime(period), 0,
4827 HRTIMER_MODE_REL, 0);
4828 }
4829}
4830
4831static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4832{
4833 struct hw_perf_event *hwc = &event->hw;
4834
4835 if (hwc->sample_period) {
4836 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4837 hwc->remaining = ktime_to_ns(remaining);
4838
4839 hrtimer_cancel(&hwc->hrtimer);
4840 }
4841}
4842
4843/*
4844 * Software event: cpu wall time clock
4845 */
4846
4847static void cpu_clock_event_update(struct perf_event *event)
4848{
4849 int cpu = raw_smp_processor_id();
4850 s64 prev;
4851 u64 now;
4852
4853 now = cpu_clock(cpu);
4854 prev = local64_xchg(&event->hw.prev_count, now);
4855 local64_add(now - prev, &event->count);
4856}
4857
4858static int cpu_clock_event_enable(struct perf_event *event)
4859{
4860 struct hw_perf_event *hwc = &event->hw;
4861 int cpu = raw_smp_processor_id();
4862
4863 local64_set(&hwc->prev_count, cpu_clock(cpu));
4864 perf_swevent_start_hrtimer(event);
4865
4866 return 0;
4867}
4868
4869static void cpu_clock_event_disable(struct perf_event *event)
4870{
4871 perf_swevent_cancel_hrtimer(event);
4872 cpu_clock_event_update(event);
4873}
4874
4875static void cpu_clock_event_read(struct perf_event *event)
4876{
4877 cpu_clock_event_update(event);
4878}
4879
4880static int cpu_clock_event_init(struct perf_event *event)
4881{
4882 if (event->attr.type != PERF_TYPE_SOFTWARE)
4883 return -ENOENT;
4884
4885 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4886 return -ENOENT;
4887
4888 return 0;
4889}
4890
4891static struct pmu perf_cpu_clock = {
4892 .event_init = cpu_clock_event_init,
4893 .enable = cpu_clock_event_enable,
4894 .disable = cpu_clock_event_disable,
4895 .read = cpu_clock_event_read,
4896};
4897
4898/*
4899 * Software event: task time clock
4900 */
4901
4902static void task_clock_event_update(struct perf_event *event, u64 now)
4903{
4904 u64 prev;
4905 s64 delta;
4906
4907 prev = local64_xchg(&event->hw.prev_count, now);
4908 delta = now - prev;
4909 local64_add(delta, &event->count);
4910}
4911
4912static int task_clock_event_enable(struct perf_event *event)
4913{
4914 struct hw_perf_event *hwc = &event->hw;
4915 u64 now;
4916
4917 now = event->ctx->time;
4918
4919 local64_set(&hwc->prev_count, now);
4920
4921 perf_swevent_start_hrtimer(event);
4922
4923 return 0;
4924}
4925
4926static void task_clock_event_disable(struct perf_event *event)
4927{
4928 perf_swevent_cancel_hrtimer(event);
4929 task_clock_event_update(event, event->ctx->time);
4930
4931}
4932
4933static void task_clock_event_read(struct perf_event *event)
4934{
4935 u64 time;
4936
4937 if (!in_nmi()) {
4938 update_context_time(event->ctx);
4939 time = event->ctx->time;
4940 } else {
4941 u64 now = perf_clock();
4942 u64 delta = now - event->ctx->timestamp;
4943 time = event->ctx->time + delta;
4944 }
4945
4946 task_clock_event_update(event, time);
4947}
4948
4949static int task_clock_event_init(struct perf_event *event)
4950{
4951 if (event->attr.type != PERF_TYPE_SOFTWARE)
4952 return -ENOENT;
4953
4954 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
4955 return -ENOENT;
4956
4957 return 0;
4958}
4959
4960static struct pmu perf_task_clock = {
4961 .event_init = task_clock_event_init,
4962 .enable = task_clock_event_enable,
4963 .disable = task_clock_event_disable,
4964 .read = task_clock_event_read,
4965};
4966
4967static LIST_HEAD(pmus);
4968static DEFINE_MUTEX(pmus_lock);
4969static struct srcu_struct pmus_srcu;
4970
4971int perf_pmu_register(struct pmu *pmu)
4972{
4973 mutex_lock(&pmus_lock);
4974 list_add_rcu(&pmu->entry, &pmus);
4975 mutex_unlock(&pmus_lock);
4976
4977 return 0;
4978}
4979
4980void perf_pmu_unregister(struct pmu *pmu)
4981{
4982 mutex_lock(&pmus_lock);
4983 list_del_rcu(&pmu->entry);
4984 mutex_unlock(&pmus_lock);
4985
4986 synchronize_srcu(&pmus_srcu);
4987}
4988
4989struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004990{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004991 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004992 int idx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004993
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004994 idx = srcu_read_lock(&pmus_srcu);
4995 list_for_each_entry_rcu(pmu, &pmus, entry) {
4996 int ret = pmu->event_init(event);
4997 if (!ret)
4998 break;
4999 if (ret != -ENOENT) {
5000 pmu = ERR_PTR(ret);
5001 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005002 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005003 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005004 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005005
5006 return pmu;
5007}
5008
5009/*
5010 * Allocate and initialize a event structure
5011 */
5012static struct perf_event *
5013perf_event_alloc(struct perf_event_attr *attr,
5014 int cpu,
5015 struct perf_event_context *ctx,
5016 struct perf_event *group_leader,
5017 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005018 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005019 gfp_t gfpflags)
5020{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005021 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022 struct perf_event *event;
5023 struct hw_perf_event *hwc;
5024 long err;
5025
5026 event = kzalloc(sizeof(*event), gfpflags);
5027 if (!event)
5028 return ERR_PTR(-ENOMEM);
5029
5030 /*
5031 * Single events are their own group leaders, with an
5032 * empty sibling list:
5033 */
5034 if (!group_leader)
5035 group_leader = event;
5036
5037 mutex_init(&event->child_mutex);
5038 INIT_LIST_HEAD(&event->child_list);
5039
5040 INIT_LIST_HEAD(&event->group_entry);
5041 INIT_LIST_HEAD(&event->event_entry);
5042 INIT_LIST_HEAD(&event->sibling_list);
5043 init_waitqueue_head(&event->waitq);
5044
5045 mutex_init(&event->mmap_mutex);
5046
5047 event->cpu = cpu;
5048 event->attr = *attr;
5049 event->group_leader = group_leader;
5050 event->pmu = NULL;
5051 event->ctx = ctx;
5052 event->oncpu = -1;
5053
5054 event->parent = parent_event;
5055
5056 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5057 event->id = atomic64_inc_return(&perf_event_id);
5058
5059 event->state = PERF_EVENT_STATE_INACTIVE;
5060
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005061 if (!overflow_handler && parent_event)
5062 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005063
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005064 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005066 if (attr->disabled)
5067 event->state = PERF_EVENT_STATE_OFF;
5068
5069 pmu = NULL;
5070
5071 hwc = &event->hw;
5072 hwc->sample_period = attr->sample_period;
5073 if (attr->freq && attr->sample_freq)
5074 hwc->sample_period = 1;
5075 hwc->last_period = hwc->sample_period;
5076
Peter Zijlstrae7850592010-05-21 14:43:08 +02005077 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078
5079 /*
5080 * we currently do not support PERF_FORMAT_GROUP on inherited events
5081 */
5082 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5083 goto done;
5084
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005085 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087done:
5088 err = 0;
5089 if (!pmu)
5090 err = -EINVAL;
5091 else if (IS_ERR(pmu))
5092 err = PTR_ERR(pmu);
5093
5094 if (err) {
5095 if (event->ns)
5096 put_pid_ns(event->ns);
5097 kfree(event);
5098 return ERR_PTR(err);
5099 }
5100
5101 event->pmu = pmu;
5102
5103 if (!event->parent) {
5104 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005105 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005106 atomic_inc(&nr_mmap_events);
5107 if (event->attr.comm)
5108 atomic_inc(&nr_comm_events);
5109 if (event->attr.task)
5110 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005111 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5112 err = get_callchain_buffers();
5113 if (err) {
5114 free_event(event);
5115 return ERR_PTR(err);
5116 }
5117 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005118 }
5119
5120 return event;
5121}
5122
5123static int perf_copy_attr(struct perf_event_attr __user *uattr,
5124 struct perf_event_attr *attr)
5125{
5126 u32 size;
5127 int ret;
5128
5129 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5130 return -EFAULT;
5131
5132 /*
5133 * zero the full structure, so that a short copy will be nice.
5134 */
5135 memset(attr, 0, sizeof(*attr));
5136
5137 ret = get_user(size, &uattr->size);
5138 if (ret)
5139 return ret;
5140
5141 if (size > PAGE_SIZE) /* silly large */
5142 goto err_size;
5143
5144 if (!size) /* abi compat */
5145 size = PERF_ATTR_SIZE_VER0;
5146
5147 if (size < PERF_ATTR_SIZE_VER0)
5148 goto err_size;
5149
5150 /*
5151 * If we're handed a bigger struct than we know of,
5152 * ensure all the unknown bits are 0 - i.e. new
5153 * user-space does not rely on any kernel feature
5154 * extensions we dont know about yet.
5155 */
5156 if (size > sizeof(*attr)) {
5157 unsigned char __user *addr;
5158 unsigned char __user *end;
5159 unsigned char val;
5160
5161 addr = (void __user *)uattr + sizeof(*attr);
5162 end = (void __user *)uattr + size;
5163
5164 for (; addr < end; addr++) {
5165 ret = get_user(val, addr);
5166 if (ret)
5167 return ret;
5168 if (val)
5169 goto err_size;
5170 }
5171 size = sizeof(*attr);
5172 }
5173
5174 ret = copy_from_user(attr, uattr, size);
5175 if (ret)
5176 return -EFAULT;
5177
5178 /*
5179 * If the type exists, the corresponding creation will verify
5180 * the attr->config.
5181 */
5182 if (attr->type >= PERF_TYPE_MAX)
5183 return -EINVAL;
5184
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305185 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 return -EINVAL;
5187
5188 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5189 return -EINVAL;
5190
5191 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5192 return -EINVAL;
5193
5194out:
5195 return ret;
5196
5197err_size:
5198 put_user(sizeof(*attr), &uattr->size);
5199 ret = -E2BIG;
5200 goto out;
5201}
5202
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005203static int
5204perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005206 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005207 int ret = -EINVAL;
5208
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005209 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005210 goto set;
5211
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005212 /* don't allow circular references */
5213 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214 goto out;
5215
Peter Zijlstra0f139302010-05-20 14:35:15 +02005216 /*
5217 * Don't allow cross-cpu buffers
5218 */
5219 if (output_event->cpu != event->cpu)
5220 goto out;
5221
5222 /*
5223 * If its not a per-cpu buffer, it must be the same task.
5224 */
5225 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5226 goto out;
5227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228set:
5229 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005230 /* Can't redirect output if we've got an active mmap() */
5231 if (atomic_read(&event->mmap_count))
5232 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005234 if (output_event) {
5235 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005236 buffer = perf_buffer_get(output_event);
5237 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005238 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239 }
5240
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005241 old_buffer = event->buffer;
5242 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005243 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005244unlock:
5245 mutex_unlock(&event->mmap_mutex);
5246
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005247 if (old_buffer)
5248 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005249out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005250 return ret;
5251}
5252
5253/**
5254 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5255 *
5256 * @attr_uptr: event_id type attributes for monitoring/sampling
5257 * @pid: target pid
5258 * @cpu: target cpu
5259 * @group_fd: group leader event fd
5260 */
5261SYSCALL_DEFINE5(perf_event_open,
5262 struct perf_event_attr __user *, attr_uptr,
5263 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5264{
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005265 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266 struct perf_event_attr attr;
5267 struct perf_event_context *ctx;
5268 struct file *event_file = NULL;
5269 struct file *group_file = NULL;
Al Viroea635c62010-05-26 17:40:29 -04005270 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005272 int err;
5273
5274 /* for future expandability... */
5275 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5276 return -EINVAL;
5277
5278 err = perf_copy_attr(attr_uptr, &attr);
5279 if (err)
5280 return err;
5281
5282 if (!attr.exclude_kernel) {
5283 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5284 return -EACCES;
5285 }
5286
5287 if (attr.freq) {
5288 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5289 return -EINVAL;
5290 }
5291
Al Viroea635c62010-05-26 17:40:29 -04005292 event_fd = get_unused_fd_flags(O_RDWR);
5293 if (event_fd < 0)
5294 return event_fd;
5295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005296 /*
5297 * Get the target context (task or percpu):
5298 */
5299 ctx = find_get_context(pid, cpu);
Al Viroea635c62010-05-26 17:40:29 -04005300 if (IS_ERR(ctx)) {
5301 err = PTR_ERR(ctx);
5302 goto err_fd;
5303 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005304
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005305 if (group_fd != -1) {
5306 group_leader = perf_fget_light(group_fd, &fput_needed);
5307 if (IS_ERR(group_leader)) {
5308 err = PTR_ERR(group_leader);
5309 goto err_put_context;
5310 }
5311 group_file = group_leader->filp;
5312 if (flags & PERF_FLAG_FD_OUTPUT)
5313 output_event = group_leader;
5314 if (flags & PERF_FLAG_FD_NO_GROUP)
5315 group_leader = NULL;
5316 }
5317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005318 /*
5319 * Look up the group leader (we will attach this event to it):
5320 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005321 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005322 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324 /*
5325 * Do not allow a recursive hierarchy (this new sibling
5326 * becoming part of another group-sibling):
5327 */
5328 if (group_leader->group_leader != group_leader)
5329 goto err_put_context;
5330 /*
5331 * Do not allow to attach to a group in a different
5332 * task or CPU context:
5333 */
5334 if (group_leader->ctx != ctx)
5335 goto err_put_context;
5336 /*
5337 * Only a group leader can be exclusive or pinned
5338 */
5339 if (attr.exclusive || attr.pinned)
5340 goto err_put_context;
5341 }
5342
5343 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005344 NULL, NULL, GFP_KERNEL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005345 if (IS_ERR(event)) {
5346 err = PTR_ERR(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005347 goto err_put_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005348 }
5349
5350 if (output_event) {
5351 err = perf_event_set_output(event, output_event);
5352 if (err)
5353 goto err_free_put_context;
5354 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005355
Al Viroea635c62010-05-26 17:40:29 -04005356 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5357 if (IS_ERR(event_file)) {
5358 err = PTR_ERR(event_file);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005359 goto err_free_put_context;
Al Viroea635c62010-05-26 17:40:29 -04005360 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005361
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005362 event->filp = event_file;
5363 WARN_ON_ONCE(ctx->parent_ctx);
5364 mutex_lock(&ctx->mutex);
5365 perf_install_in_context(ctx, event, cpu);
5366 ++ctx->generation;
5367 mutex_unlock(&ctx->mutex);
5368
5369 event->owner = current;
5370 get_task_struct(current);
5371 mutex_lock(&current->perf_event_mutex);
5372 list_add_tail(&event->owner_entry, &current->perf_event_list);
5373 mutex_unlock(&current->perf_event_mutex);
5374
Peter Zijlstra8a495422010-05-27 15:47:49 +02005375 /*
5376 * Drop the reference on the group_event after placing the
5377 * new event on the sibling_list. This ensures destruction
5378 * of the group leader will find the pointer to itself in
5379 * perf_group_detach().
5380 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005382 fd_install(event_fd, event_file);
5383 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005384
Al Viroea635c62010-05-26 17:40:29 -04005385err_free_put_context:
5386 free_event(event);
5387err_put_context:
5388 fput_light(group_file, fput_needed);
5389 put_ctx(ctx);
5390err_fd:
5391 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005392 return err;
5393}
5394
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005395/**
5396 * perf_event_create_kernel_counter
5397 *
5398 * @attr: attributes of the counter to create
5399 * @cpu: cpu in which the counter is bound
5400 * @pid: task to profile
5401 */
5402struct perf_event *
5403perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005404 pid_t pid,
5405 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005406{
5407 struct perf_event *event;
5408 struct perf_event_context *ctx;
5409 int err;
5410
5411 /*
5412 * Get the target context (task or percpu):
5413 */
5414
5415 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005416 if (IS_ERR(ctx)) {
5417 err = PTR_ERR(ctx);
5418 goto err_exit;
5419 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005420
5421 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005422 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005423 if (IS_ERR(event)) {
5424 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005425 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005426 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005427
5428 event->filp = NULL;
5429 WARN_ON_ONCE(ctx->parent_ctx);
5430 mutex_lock(&ctx->mutex);
5431 perf_install_in_context(ctx, event, cpu);
5432 ++ctx->generation;
5433 mutex_unlock(&ctx->mutex);
5434
5435 event->owner = current;
5436 get_task_struct(current);
5437 mutex_lock(&current->perf_event_mutex);
5438 list_add_tail(&event->owner_entry, &current->perf_event_list);
5439 mutex_unlock(&current->perf_event_mutex);
5440
5441 return event;
5442
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005443 err_put_context:
5444 put_ctx(ctx);
5445 err_exit:
5446 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005447}
5448EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5449
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005450/*
5451 * inherit a event from parent task to child task:
5452 */
5453static struct perf_event *
5454inherit_event(struct perf_event *parent_event,
5455 struct task_struct *parent,
5456 struct perf_event_context *parent_ctx,
5457 struct task_struct *child,
5458 struct perf_event *group_leader,
5459 struct perf_event_context *child_ctx)
5460{
5461 struct perf_event *child_event;
5462
5463 /*
5464 * Instead of creating recursive hierarchies of events,
5465 * we link inherited events back to the original parent,
5466 * which has a filp for sure, which we use as the reference
5467 * count:
5468 */
5469 if (parent_event->parent)
5470 parent_event = parent_event->parent;
5471
5472 child_event = perf_event_alloc(&parent_event->attr,
5473 parent_event->cpu, child_ctx,
5474 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005475 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005476 if (IS_ERR(child_event))
5477 return child_event;
5478 get_ctx(child_ctx);
5479
5480 /*
5481 * Make the child state follow the state of the parent event,
5482 * not its attr.disabled bit. We hold the parent's mutex,
5483 * so we won't race with perf_event_{en, dis}able_family.
5484 */
5485 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5486 child_event->state = PERF_EVENT_STATE_INACTIVE;
5487 else
5488 child_event->state = PERF_EVENT_STATE_OFF;
5489
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005490 if (parent_event->attr.freq) {
5491 u64 sample_period = parent_event->hw.sample_period;
5492 struct hw_perf_event *hwc = &child_event->hw;
5493
5494 hwc->sample_period = sample_period;
5495 hwc->last_period = sample_period;
5496
Peter Zijlstrae7850592010-05-21 14:43:08 +02005497 local64_set(&hwc->period_left, sample_period);
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005498 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005499
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005500 child_event->overflow_handler = parent_event->overflow_handler;
5501
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005502 /*
5503 * Link it up in the child's context:
5504 */
5505 add_event_to_ctx(child_event, child_ctx);
5506
5507 /*
5508 * Get a reference to the parent filp - we will fput it
5509 * when the child event exits. This is safe to do because
5510 * we are in the parent and we know that the filp still
5511 * exists and has a nonzero count:
5512 */
5513 atomic_long_inc(&parent_event->filp->f_count);
5514
5515 /*
5516 * Link this into the parent event's child list
5517 */
5518 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5519 mutex_lock(&parent_event->child_mutex);
5520 list_add_tail(&child_event->child_list, &parent_event->child_list);
5521 mutex_unlock(&parent_event->child_mutex);
5522
5523 return child_event;
5524}
5525
5526static int inherit_group(struct perf_event *parent_event,
5527 struct task_struct *parent,
5528 struct perf_event_context *parent_ctx,
5529 struct task_struct *child,
5530 struct perf_event_context *child_ctx)
5531{
5532 struct perf_event *leader;
5533 struct perf_event *sub;
5534 struct perf_event *child_ctr;
5535
5536 leader = inherit_event(parent_event, parent, parent_ctx,
5537 child, NULL, child_ctx);
5538 if (IS_ERR(leader))
5539 return PTR_ERR(leader);
5540 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5541 child_ctr = inherit_event(sub, parent, parent_ctx,
5542 child, leader, child_ctx);
5543 if (IS_ERR(child_ctr))
5544 return PTR_ERR(child_ctr);
5545 }
5546 return 0;
5547}
5548
5549static void sync_child_event(struct perf_event *child_event,
5550 struct task_struct *child)
5551{
5552 struct perf_event *parent_event = child_event->parent;
5553 u64 child_val;
5554
5555 if (child_event->attr.inherit_stat)
5556 perf_event_read_event(child_event, child);
5557
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005558 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005559
5560 /*
5561 * Add back the child's count to the parent's count:
5562 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005563 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005564 atomic64_add(child_event->total_time_enabled,
5565 &parent_event->child_total_time_enabled);
5566 atomic64_add(child_event->total_time_running,
5567 &parent_event->child_total_time_running);
5568
5569 /*
5570 * Remove this event from the parent's list
5571 */
5572 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5573 mutex_lock(&parent_event->child_mutex);
5574 list_del_init(&child_event->child_list);
5575 mutex_unlock(&parent_event->child_mutex);
5576
5577 /*
5578 * Release the parent event, if this was the last
5579 * reference to it.
5580 */
5581 fput(parent_event->filp);
5582}
5583
5584static void
5585__perf_event_exit_task(struct perf_event *child_event,
5586 struct perf_event_context *child_ctx,
5587 struct task_struct *child)
5588{
5589 struct perf_event *parent_event;
5590
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005591 perf_event_remove_from_context(child_event);
5592
5593 parent_event = child_event->parent;
5594 /*
5595 * It can happen that parent exits first, and has events
5596 * that are still around due to the child reference. These
5597 * events need to be zapped - but otherwise linger.
5598 */
5599 if (parent_event) {
5600 sync_child_event(child_event, child);
5601 free_event(child_event);
5602 }
5603}
5604
5605/*
5606 * When a child task exits, feed back event values to parent events.
5607 */
5608void perf_event_exit_task(struct task_struct *child)
5609{
5610 struct perf_event *child_event, *tmp;
5611 struct perf_event_context *child_ctx;
5612 unsigned long flags;
5613
5614 if (likely(!child->perf_event_ctxp)) {
5615 perf_event_task(child, NULL, 0);
5616 return;
5617 }
5618
5619 local_irq_save(flags);
5620 /*
5621 * We can't reschedule here because interrupts are disabled,
5622 * and either child is current or it is a task that can't be
5623 * scheduled, so we are now safe from rescheduling changing
5624 * our context.
5625 */
5626 child_ctx = child->perf_event_ctxp;
5627 __perf_event_task_sched_out(child_ctx);
5628
5629 /*
5630 * Take the context lock here so that if find_get_context is
5631 * reading child->perf_event_ctxp, we wait until it has
5632 * incremented the context's refcount before we do put_ctx below.
5633 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005634 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005635 child->perf_event_ctxp = NULL;
5636 /*
5637 * If this context is a clone; unclone it so it can't get
5638 * swapped to another process while we're removing all
5639 * the events from it.
5640 */
5641 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005642 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005643 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644
5645 /*
5646 * Report the task dead after unscheduling the events so that we
5647 * won't get any samples after PERF_RECORD_EXIT. We can however still
5648 * get a few PERF_RECORD_READ events.
5649 */
5650 perf_event_task(child, child_ctx, 0);
5651
5652 /*
5653 * We can recurse on the same lock type through:
5654 *
5655 * __perf_event_exit_task()
5656 * sync_child_event()
5657 * fput(parent_event->filp)
5658 * perf_release()
5659 * mutex_lock(&ctx->mutex)
5660 *
5661 * But since its the parent context it won't be the same instance.
5662 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005663 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005664
5665again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005666 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5667 group_entry)
5668 __perf_event_exit_task(child_event, child_ctx, child);
5669
5670 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005671 group_entry)
5672 __perf_event_exit_task(child_event, child_ctx, child);
5673
5674 /*
5675 * If the last event was a group event, it will have appended all
5676 * its siblings to the list, but we obtained 'tmp' before that which
5677 * will still point to the list head terminating the iteration.
5678 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005679 if (!list_empty(&child_ctx->pinned_groups) ||
5680 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005681 goto again;
5682
5683 mutex_unlock(&child_ctx->mutex);
5684
5685 put_ctx(child_ctx);
5686}
5687
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005688static void perf_free_event(struct perf_event *event,
5689 struct perf_event_context *ctx)
5690{
5691 struct perf_event *parent = event->parent;
5692
5693 if (WARN_ON_ONCE(!parent))
5694 return;
5695
5696 mutex_lock(&parent->child_mutex);
5697 list_del_init(&event->child_list);
5698 mutex_unlock(&parent->child_mutex);
5699
5700 fput(parent->filp);
5701
Peter Zijlstra8a495422010-05-27 15:47:49 +02005702 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005703 list_del_event(event, ctx);
5704 free_event(event);
5705}
5706
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005707/*
5708 * free an unexposed, unused context as created by inheritance by
5709 * init_task below, used by fork() in case of fail.
5710 */
5711void perf_event_free_task(struct task_struct *task)
5712{
5713 struct perf_event_context *ctx = task->perf_event_ctxp;
5714 struct perf_event *event, *tmp;
5715
5716 if (!ctx)
5717 return;
5718
5719 mutex_lock(&ctx->mutex);
5720again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005721 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5722 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005723
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005724 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5725 group_entry)
5726 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005727
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005728 if (!list_empty(&ctx->pinned_groups) ||
5729 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005730 goto again;
5731
5732 mutex_unlock(&ctx->mutex);
5733
5734 put_ctx(ctx);
5735}
5736
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005737static int
5738inherit_task_group(struct perf_event *event, struct task_struct *parent,
5739 struct perf_event_context *parent_ctx,
5740 struct task_struct *child,
5741 int *inherited_all)
5742{
5743 int ret;
5744 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5745
5746 if (!event->attr.inherit) {
5747 *inherited_all = 0;
5748 return 0;
5749 }
5750
5751 if (!child_ctx) {
5752 /*
5753 * This is executed from the parent task context, so
5754 * inherit events that have been marked for cloning.
5755 * First allocate and initialize a context for the
5756 * child.
5757 */
5758
5759 child_ctx = kzalloc(sizeof(struct perf_event_context),
5760 GFP_KERNEL);
5761 if (!child_ctx)
5762 return -ENOMEM;
5763
5764 __perf_event_init_context(child_ctx, child);
5765 child->perf_event_ctxp = child_ctx;
5766 get_task_struct(child);
5767 }
5768
5769 ret = inherit_group(event, parent, parent_ctx,
5770 child, child_ctx);
5771
5772 if (ret)
5773 *inherited_all = 0;
5774
5775 return ret;
5776}
5777
5778
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005779/*
5780 * Initialize the perf_event context in task_struct
5781 */
5782int perf_event_init_task(struct task_struct *child)
5783{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005784 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005785 struct perf_event_context *cloned_ctx;
5786 struct perf_event *event;
5787 struct task_struct *parent = current;
5788 int inherited_all = 1;
5789 int ret = 0;
5790
5791 child->perf_event_ctxp = NULL;
5792
5793 mutex_init(&child->perf_event_mutex);
5794 INIT_LIST_HEAD(&child->perf_event_list);
5795
5796 if (likely(!parent->perf_event_ctxp))
5797 return 0;
5798
5799 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800 * If the parent's context is a clone, pin it so it won't get
5801 * swapped under us.
5802 */
5803 parent_ctx = perf_pin_task_context(parent);
5804
5805 /*
5806 * No need to check if parent_ctx != NULL here; since we saw
5807 * it non-NULL earlier, the only reason for it to become NULL
5808 * is if we exit, and since we're currently in the middle of
5809 * a fork we can't be exiting at the same time.
5810 */
5811
5812 /*
5813 * Lock the parent list. No need to lock the child - not PID
5814 * hashed yet and not running, so nobody can access it.
5815 */
5816 mutex_lock(&parent_ctx->mutex);
5817
5818 /*
5819 * We dont have to disable NMIs - we are only looking at
5820 * the list, not manipulating it:
5821 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005822 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5823 ret = inherit_task_group(event, parent, parent_ctx, child,
5824 &inherited_all);
5825 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005826 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005827 }
5828
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005829 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5830 ret = inherit_task_group(event, parent, parent_ctx, child,
5831 &inherited_all);
5832 if (ret)
5833 break;
5834 }
5835
5836 child_ctx = child->perf_event_ctxp;
5837
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005838 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005839 /*
5840 * Mark the child context as a clone of the parent
5841 * context, or of whatever the parent is a clone of.
5842 * Note that if the parent is a clone, it could get
5843 * uncloned at any point, but that doesn't matter
5844 * because the list of events and the generation
5845 * count can't have changed since we took the mutex.
5846 */
5847 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5848 if (cloned_ctx) {
5849 child_ctx->parent_ctx = cloned_ctx;
5850 child_ctx->parent_gen = parent_ctx->parent_gen;
5851 } else {
5852 child_ctx->parent_ctx = parent_ctx;
5853 child_ctx->parent_gen = parent_ctx->generation;
5854 }
5855 get_ctx(child_ctx->parent_ctx);
5856 }
5857
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005858 mutex_unlock(&parent_ctx->mutex);
5859
5860 perf_unpin_context(parent_ctx);
5861
5862 return ret;
5863}
5864
Paul Mackerras220b1402010-03-10 20:45:52 +11005865static void __init perf_event_init_all_cpus(void)
5866{
5867 int cpu;
5868 struct perf_cpu_context *cpuctx;
5869
5870 for_each_possible_cpu(cpu) {
5871 cpuctx = &per_cpu(perf_cpu_context, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005872 mutex_init(&cpuctx->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005873 __perf_event_init_context(&cpuctx->ctx, NULL);
5874 }
5875}
5876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005877static void __cpuinit perf_event_init_cpu(int cpu)
5878{
5879 struct perf_cpu_context *cpuctx;
5880
5881 cpuctx = &per_cpu(perf_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005882
5883 spin_lock(&perf_resource_lock);
5884 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5885 spin_unlock(&perf_resource_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005886
5887 mutex_lock(&cpuctx->hlist_mutex);
5888 if (cpuctx->hlist_refcount > 0) {
5889 struct swevent_hlist *hlist;
5890
5891 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5892 WARN_ON_ONCE(!hlist);
5893 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5894 }
5895 mutex_unlock(&cpuctx->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896}
5897
5898#ifdef CONFIG_HOTPLUG_CPU
5899static void __perf_event_exit_cpu(void *info)
5900{
5901 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5902 struct perf_event_context *ctx = &cpuctx->ctx;
5903 struct perf_event *event, *tmp;
5904
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005905 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5906 __perf_event_remove_from_context(event);
5907 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908 __perf_event_remove_from_context(event);
5909}
5910static void perf_event_exit_cpu(int cpu)
5911{
5912 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5913 struct perf_event_context *ctx = &cpuctx->ctx;
5914
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005915 mutex_lock(&cpuctx->hlist_mutex);
5916 swevent_hlist_release(cpuctx);
5917 mutex_unlock(&cpuctx->hlist_mutex);
5918
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005919 mutex_lock(&ctx->mutex);
5920 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5921 mutex_unlock(&ctx->mutex);
5922}
5923#else
5924static inline void perf_event_exit_cpu(int cpu) { }
5925#endif
5926
5927static int __cpuinit
5928perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5929{
5930 unsigned int cpu = (long)hcpu;
5931
Peter Zijlstra5e116372010-06-11 13:35:08 +02005932 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005933
5934 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02005935 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005936 perf_event_init_cpu(cpu);
5937 break;
5938
Peter Zijlstra5e116372010-06-11 13:35:08 +02005939 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005940 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005941 perf_event_exit_cpu(cpu);
5942 break;
5943
5944 default:
5945 break;
5946 }
5947
5948 return NOTIFY_OK;
5949}
5950
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005951void __init perf_event_init(void)
5952{
Paul Mackerras220b1402010-03-10 20:45:52 +11005953 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005954 init_srcu_struct(&pmus_srcu);
5955 perf_pmu_register(&perf_swevent);
5956 perf_pmu_register(&perf_cpu_clock);
5957 perf_pmu_register(&perf_task_clock);
5958 perf_tp_register();
5959 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005960}
5961
Andi Kleenc9be0a32010-01-05 12:47:58 +01005962static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5963 struct sysdev_class_attribute *attr,
5964 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005965{
5966 return sprintf(buf, "%d\n", perf_reserved_percpu);
5967}
5968
5969static ssize_t
5970perf_set_reserve_percpu(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01005971 struct sysdev_class_attribute *attr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972 const char *buf,
5973 size_t count)
5974{
5975 struct perf_cpu_context *cpuctx;
5976 unsigned long val;
5977 int err, cpu, mpt;
5978
5979 err = strict_strtoul(buf, 10, &val);
5980 if (err)
5981 return err;
5982 if (val > perf_max_events)
5983 return -EINVAL;
5984
5985 spin_lock(&perf_resource_lock);
5986 perf_reserved_percpu = val;
5987 for_each_online_cpu(cpu) {
5988 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005989 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005990 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5991 perf_max_events - perf_reserved_percpu);
5992 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005993 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005994 }
5995 spin_unlock(&perf_resource_lock);
5996
5997 return count;
5998}
5999
Andi Kleenc9be0a32010-01-05 12:47:58 +01006000static ssize_t perf_show_overcommit(struct sysdev_class *class,
6001 struct sysdev_class_attribute *attr,
6002 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006003{
6004 return sprintf(buf, "%d\n", perf_overcommit);
6005}
6006
6007static ssize_t
Andi Kleenc9be0a32010-01-05 12:47:58 +01006008perf_set_overcommit(struct sysdev_class *class,
6009 struct sysdev_class_attribute *attr,
6010 const char *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006011{
6012 unsigned long val;
6013 int err;
6014
6015 err = strict_strtoul(buf, 10, &val);
6016 if (err)
6017 return err;
6018 if (val > 1)
6019 return -EINVAL;
6020
6021 spin_lock(&perf_resource_lock);
6022 perf_overcommit = val;
6023 spin_unlock(&perf_resource_lock);
6024
6025 return count;
6026}
6027
6028static SYSDEV_CLASS_ATTR(
6029 reserve_percpu,
6030 0644,
6031 perf_show_reserve_percpu,
6032 perf_set_reserve_percpu
6033 );
6034
6035static SYSDEV_CLASS_ATTR(
6036 overcommit,
6037 0644,
6038 perf_show_overcommit,
6039 perf_set_overcommit
6040 );
6041
6042static struct attribute *perfclass_attrs[] = {
6043 &attr_reserve_percpu.attr,
6044 &attr_overcommit.attr,
6045 NULL
6046};
6047
6048static struct attribute_group perfclass_attr_group = {
6049 .attrs = perfclass_attrs,
6050 .name = "perf_events",
6051};
6052
6053static int __init perf_event_sysfs_init(void)
6054{
6055 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
6056 &perfclass_attr_group);
6057}
6058device_initcall(perf_event_sysfs_init);