blob: 13d98d756347319b532ef23ba0b01ef5e4a2bf25 [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
Ingo Molnarcdd6c482009-09-21 12:02:48 +020037static atomic_t nr_events __read_mostly;
38static atomic_t nr_mmap_events __read_mostly;
39static atomic_t nr_comm_events __read_mostly;
40static atomic_t nr_task_events __read_mostly;
41
Peter Zijlstra108b02c2010-09-06 14:32:03 +020042static LIST_HEAD(pmus);
43static DEFINE_MUTEX(pmus_lock);
44static struct srcu_struct pmus_srcu;
45
Ingo Molnarcdd6c482009-09-21 12:02:48 +020046/*
47 * perf event paranoia level:
48 * -1 - not paranoid at all
49 * 0 - disallow raw tracepoint access for unpriv
50 * 1 - disallow cpu events for unpriv
51 * 2 - disallow kernel profiling for unpriv
52 */
53int sysctl_perf_event_paranoid __read_mostly = 1;
54
Ingo Molnarcdd6c482009-09-21 12:02:48 +020055int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
56
57/*
58 * max perf event sample rate
59 */
60int sysctl_perf_event_sample_rate __read_mostly = 100000;
61
62static atomic64_t perf_event_id;
63
Ingo Molnarcdd6c482009-09-21 12:02:48 +020064void __weak perf_event_print_debug(void) { }
65
Peter Zijlstra33696fc2010-06-14 08:49:00 +020066void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020067{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020068 int *count = this_cpu_ptr(pmu->pmu_disable_count);
69 if (!(*count)++)
70 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020071}
72
Peter Zijlstra33696fc2010-06-14 08:49:00 +020073void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020074{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020075 int *count = this_cpu_ptr(pmu->pmu_disable_count);
76 if (!--(*count))
77 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020078}
79
Peter Zijlstra108b02c2010-09-06 14:32:03 +020080static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020081{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020082 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020083
84 if (hrtimer_active(&cpuctx->timer))
85 return;
86
87 __hrtimer_start_range_ns(&cpuctx->timer,
88 ns_to_ktime(cpuctx->timer_interval), 0,
89 HRTIMER_MODE_REL_PINNED, 0);
90}
91
Peter Zijlstra108b02c2010-09-06 14:32:03 +020092static void perf_pmu_rotate_stop(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020093{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020094 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020095
96 hrtimer_cancel(&cpuctx->timer);
97}
98
Ingo Molnarcdd6c482009-09-21 12:02:48 +020099static void get_ctx(struct perf_event_context *ctx)
100{
101 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
102}
103
104static void free_ctx(struct rcu_head *head)
105{
106 struct perf_event_context *ctx;
107
108 ctx = container_of(head, struct perf_event_context, rcu_head);
109 kfree(ctx);
110}
111
112static void put_ctx(struct perf_event_context *ctx)
113{
114 if (atomic_dec_and_test(&ctx->refcount)) {
115 if (ctx->parent_ctx)
116 put_ctx(ctx->parent_ctx);
117 if (ctx->task)
118 put_task_struct(ctx->task);
119 call_rcu(&ctx->rcu_head, free_ctx);
120 }
121}
122
123static void unclone_ctx(struct perf_event_context *ctx)
124{
125 if (ctx->parent_ctx) {
126 put_ctx(ctx->parent_ctx);
127 ctx->parent_ctx = NULL;
128 }
129}
130
131/*
132 * If we inherit events we want to return the parent event id
133 * to userspace.
134 */
135static u64 primary_event_id(struct perf_event *event)
136{
137 u64 id = event->id;
138
139 if (event->parent)
140 id = event->parent->id;
141
142 return id;
143}
144
145/*
146 * Get the perf_event_context for a task and lock it.
147 * This has to cope with with the fact that until it is locked,
148 * the context could get moved to another task.
149 */
150static struct perf_event_context *
151perf_lock_task_context(struct task_struct *task, unsigned long *flags)
152{
153 struct perf_event_context *ctx;
154
155 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200156retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200157 ctx = rcu_dereference(task->perf_event_ctxp);
158 if (ctx) {
159 /*
160 * If this context is a clone of another, it might
161 * get swapped for another underneath us by
162 * perf_event_task_sched_out, though the
163 * rcu_read_lock() protects us from any context
164 * getting freed. Lock the context and check if it
165 * got swapped before we could get the lock, and retry
166 * if so. If we locked the right context, then it
167 * can't get swapped on us any more.
168 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100169 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200170 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100171 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200172 goto retry;
173 }
174
175 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100176 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200177 ctx = NULL;
178 }
179 }
180 rcu_read_unlock();
181 return ctx;
182}
183
184/*
185 * Get the context for a task and increment its pin_count so it
186 * can't get swapped to another task. This also increments its
187 * reference count so that the context can't get freed.
188 */
189static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
190{
191 struct perf_event_context *ctx;
192 unsigned long flags;
193
194 ctx = perf_lock_task_context(task, &flags);
195 if (ctx) {
196 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100197 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200198 }
199 return ctx;
200}
201
202static void perf_unpin_context(struct perf_event_context *ctx)
203{
204 unsigned long flags;
205
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100206 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200207 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100208 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209 put_ctx(ctx);
210}
211
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100212static inline u64 perf_clock(void)
213{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200214 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100215}
216
217/*
218 * Update the record of the current time in a context.
219 */
220static void update_context_time(struct perf_event_context *ctx)
221{
222 u64 now = perf_clock();
223
224 ctx->time += now - ctx->timestamp;
225 ctx->timestamp = now;
226}
227
228/*
229 * Update the total_time_enabled and total_time_running fields for a event.
230 */
231static void update_event_times(struct perf_event *event)
232{
233 struct perf_event_context *ctx = event->ctx;
234 u64 run_end;
235
236 if (event->state < PERF_EVENT_STATE_INACTIVE ||
237 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
238 return;
239
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100240 if (ctx->is_active)
241 run_end = ctx->time;
242 else
243 run_end = event->tstamp_stopped;
244
245 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100246
247 if (event->state == PERF_EVENT_STATE_INACTIVE)
248 run_end = event->tstamp_stopped;
249 else
250 run_end = ctx->time;
251
252 event->total_time_running = run_end - event->tstamp_running;
253}
254
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200255/*
256 * Update total_time_enabled and total_time_running for all events in a group.
257 */
258static void update_group_times(struct perf_event *leader)
259{
260 struct perf_event *event;
261
262 update_event_times(leader);
263 list_for_each_entry(event, &leader->sibling_list, group_entry)
264 update_event_times(event);
265}
266
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100267static struct list_head *
268ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
269{
270 if (event->attr.pinned)
271 return &ctx->pinned_groups;
272 else
273 return &ctx->flexible_groups;
274}
275
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200276/*
277 * Add a event from the lists for its context.
278 * Must be called with ctx->mutex and ctx->lock held.
279 */
280static void
281list_add_event(struct perf_event *event, struct perf_event_context *ctx)
282{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200283 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
284 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200285
286 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200287 * If we're a stand alone event or group leader, we go to the context
288 * list, group events are kept attached to the group so that
289 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200290 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200291 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100292 struct list_head *list;
293
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100294 if (is_software_event(event))
295 event->group_flags |= PERF_GROUP_SOFTWARE;
296
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100297 list = ctx_group_list(event, ctx);
298 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200299 }
300
301 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200302 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200303 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200304 ctx->nr_events++;
305 if (event->attr.inherit_stat)
306 ctx->nr_stat++;
307}
308
Peter Zijlstra8a495422010-05-27 15:47:49 +0200309static void perf_group_attach(struct perf_event *event)
310{
311 struct perf_event *group_leader = event->group_leader;
312
313 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
314 event->attach_state |= PERF_ATTACH_GROUP;
315
316 if (group_leader == event)
317 return;
318
319 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
320 !is_software_event(event))
321 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
322
323 list_add_tail(&event->group_entry, &group_leader->sibling_list);
324 group_leader->nr_siblings++;
325}
326
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200327/*
328 * Remove a event from the lists for its context.
329 * Must be called with ctx->mutex and ctx->lock held.
330 */
331static void
332list_del_event(struct perf_event *event, struct perf_event_context *ctx)
333{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200334 /*
335 * We can have double detach due to exit/hot-unplug + close.
336 */
337 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200339
340 event->attach_state &= ~PERF_ATTACH_CONTEXT;
341
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342 ctx->nr_events--;
343 if (event->attr.inherit_stat)
344 ctx->nr_stat--;
345
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200346 list_del_rcu(&event->event_entry);
347
Peter Zijlstra8a495422010-05-27 15:47:49 +0200348 if (event->group_leader == event)
349 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200350
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200351 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800352
353 /*
354 * If event was in error state, then keep it
355 * that way, otherwise bogus counts will be
356 * returned on read(). The only way to get out
357 * of error state is by explicit re-enabling
358 * of the event
359 */
360 if (event->state > PERF_EVENT_STATE_OFF)
361 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200362}
363
Peter Zijlstra8a495422010-05-27 15:47:49 +0200364static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200365{
366 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200367 struct list_head *list = NULL;
368
369 /*
370 * We can have double detach due to exit/hot-unplug + close.
371 */
372 if (!(event->attach_state & PERF_ATTACH_GROUP))
373 return;
374
375 event->attach_state &= ~PERF_ATTACH_GROUP;
376
377 /*
378 * If this is a sibling, remove it from its group.
379 */
380 if (event->group_leader != event) {
381 list_del_init(&event->group_entry);
382 event->group_leader->nr_siblings--;
383 return;
384 }
385
386 if (!list_empty(&event->group_entry))
387 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100388
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200389 /*
390 * If this was a group event with sibling events then
391 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200392 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393 */
394 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200395 if (list)
396 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200397 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100398
399 /* Inherit group flags from the previous leader */
400 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200401 }
402}
403
Stephane Eranianfa66f072010-08-26 16:40:01 +0200404static inline int
405event_filter_match(struct perf_event *event)
406{
407 return event->cpu == -1 || event->cpu == smp_processor_id();
408}
409
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200410static void
411event_sched_out(struct perf_event *event,
412 struct perf_cpu_context *cpuctx,
413 struct perf_event_context *ctx)
414{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200415 u64 delta;
416 /*
417 * An event which could not be activated because of
418 * filter mismatch still needs to have its timings
419 * maintained, otherwise bogus information is return
420 * via read() for time_enabled, time_running:
421 */
422 if (event->state == PERF_EVENT_STATE_INACTIVE
423 && !event_filter_match(event)) {
424 delta = ctx->time - event->tstamp_stopped;
425 event->tstamp_running += delta;
426 event->tstamp_stopped = ctx->time;
427 }
428
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200429 if (event->state != PERF_EVENT_STATE_ACTIVE)
430 return;
431
432 event->state = PERF_EVENT_STATE_INACTIVE;
433 if (event->pending_disable) {
434 event->pending_disable = 0;
435 event->state = PERF_EVENT_STATE_OFF;
436 }
437 event->tstamp_stopped = ctx->time;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200438 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200439 event->oncpu = -1;
440
441 if (!is_software_event(event))
442 cpuctx->active_oncpu--;
443 ctx->nr_active--;
444 if (event->attr.exclusive || !cpuctx->active_oncpu)
445 cpuctx->exclusive = 0;
446}
447
448static void
449group_sched_out(struct perf_event *group_event,
450 struct perf_cpu_context *cpuctx,
451 struct perf_event_context *ctx)
452{
453 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200454 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200455
456 event_sched_out(group_event, cpuctx, ctx);
457
458 /*
459 * Schedule out siblings (if any):
460 */
461 list_for_each_entry(event, &group_event->sibling_list, group_entry)
462 event_sched_out(event, cpuctx, ctx);
463
Stephane Eranianfa66f072010-08-26 16:40:01 +0200464 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200465 cpuctx->exclusive = 0;
466}
467
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200468static inline struct perf_cpu_context *
469__get_cpu_context(struct perf_event_context *ctx)
470{
471 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
472}
473
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200474/*
475 * Cross CPU call to remove a performance event
476 *
477 * We disable the event on the hardware level first. After that we
478 * remove it from the context list.
479 */
480static void __perf_event_remove_from_context(void *info)
481{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200482 struct perf_event *event = info;
483 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200484 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200485
486 /*
487 * If this is a task context, we need to check whether it is
488 * the current task context of this cpu. If not it has been
489 * scheduled out before the smp call arrived.
490 */
491 if (ctx->task && cpuctx->task_ctx != ctx)
492 return;
493
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100494 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200495
496 event_sched_out(event, cpuctx, ctx);
497
498 list_del_event(event, ctx);
499
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100500 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200501}
502
503
504/*
505 * Remove the event from a task's (or a CPU's) list of events.
506 *
507 * Must be called with ctx->mutex held.
508 *
509 * CPU events are removed with a smp call. For task events we only
510 * call when the task is on a CPU.
511 *
512 * If event->ctx is a cloned context, callers must make sure that
513 * every task struct that event->ctx->task could possibly point to
514 * remains valid. This is OK when called from perf_release since
515 * that only calls us on the top-level context, which can't be a clone.
516 * When called from perf_event_exit_task, it's OK because the
517 * context has been detached from its task.
518 */
519static void perf_event_remove_from_context(struct perf_event *event)
520{
521 struct perf_event_context *ctx = event->ctx;
522 struct task_struct *task = ctx->task;
523
524 if (!task) {
525 /*
526 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200527 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200528 */
529 smp_call_function_single(event->cpu,
530 __perf_event_remove_from_context,
531 event, 1);
532 return;
533 }
534
535retry:
536 task_oncpu_function_call(task, __perf_event_remove_from_context,
537 event);
538
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100539 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200540 /*
541 * If the context is active we need to retry the smp call.
542 */
543 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100544 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200545 goto retry;
546 }
547
548 /*
549 * The lock prevents that this context is scheduled in so we
550 * can remove the event safely, if the call above did not
551 * succeed.
552 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100553 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200554 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100555 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556}
557
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200558/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200559 * Cross CPU call to disable a performance event
560 */
561static void __perf_event_disable(void *info)
562{
563 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200564 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200565 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200566
567 /*
568 * If this is a per-task event, need to check whether this
569 * event's task is the current task on this cpu.
570 */
571 if (ctx->task && cpuctx->task_ctx != ctx)
572 return;
573
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100574 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575
576 /*
577 * If the event is on, turn it off.
578 * If it is in error state, leave it in error state.
579 */
580 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
581 update_context_time(ctx);
582 update_group_times(event);
583 if (event == event->group_leader)
584 group_sched_out(event, cpuctx, ctx);
585 else
586 event_sched_out(event, cpuctx, ctx);
587 event->state = PERF_EVENT_STATE_OFF;
588 }
589
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100590 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200591}
592
593/*
594 * Disable a event.
595 *
596 * If event->ctx is a cloned context, callers must make sure that
597 * every task struct that event->ctx->task could possibly point to
598 * remains valid. This condition is satisifed when called through
599 * perf_event_for_each_child or perf_event_for_each because they
600 * hold the top-level event's child_mutex, so any descendant that
601 * goes to exit will block in sync_child_event.
602 * When called from perf_pending_event it's OK because event->ctx
603 * is the current context on this CPU and preemption is disabled,
604 * hence we can't get into perf_event_task_sched_out for this context.
605 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100606void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200607{
608 struct perf_event_context *ctx = event->ctx;
609 struct task_struct *task = ctx->task;
610
611 if (!task) {
612 /*
613 * Disable the event on the cpu that it's on
614 */
615 smp_call_function_single(event->cpu, __perf_event_disable,
616 event, 1);
617 return;
618 }
619
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200620retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200621 task_oncpu_function_call(task, __perf_event_disable, event);
622
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100623 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200624 /*
625 * If the event is still active, we need to retry the cross-call.
626 */
627 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100628 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200629 goto retry;
630 }
631
632 /*
633 * Since we have the lock this context can't be scheduled
634 * in, so we can change the state safely.
635 */
636 if (event->state == PERF_EVENT_STATE_INACTIVE) {
637 update_group_times(event);
638 event->state = PERF_EVENT_STATE_OFF;
639 }
640
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100641 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200642}
643
644static int
645event_sched_in(struct perf_event *event,
646 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100647 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200648{
649 if (event->state <= PERF_EVENT_STATE_OFF)
650 return 0;
651
652 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100653 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200654 /*
655 * The new state must be visible before we turn it on in the hardware:
656 */
657 smp_wmb();
658
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200659 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660 event->state = PERF_EVENT_STATE_INACTIVE;
661 event->oncpu = -1;
662 return -EAGAIN;
663 }
664
665 event->tstamp_running += ctx->time - event->tstamp_stopped;
666
667 if (!is_software_event(event))
668 cpuctx->active_oncpu++;
669 ctx->nr_active++;
670
671 if (event->attr.exclusive)
672 cpuctx->exclusive = 1;
673
674 return 0;
675}
676
677static int
678group_sched_in(struct perf_event *group_event,
679 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100680 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200681{
Lin Ming6bde9b62010-04-23 13:56:00 +0800682 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200683 struct pmu *pmu = group_event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200684
685 if (group_event->state == PERF_EVENT_STATE_OFF)
686 return 0;
687
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200688 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200689
Stephane Eranian90151c32010-05-25 16:23:10 +0200690 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200691 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200692 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200693 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200694
695 /*
696 * Schedule in siblings as one group (if any):
697 */
698 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100699 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200700 partial_group = event;
701 goto group_error;
702 }
703 }
704
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200705 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000706 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800707
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200708group_error:
709 /*
710 * Groups can be scheduled in as one unit only, so undo any
711 * partial group before returning:
712 */
713 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
714 if (event == partial_group)
715 break;
716 event_sched_out(event, cpuctx, ctx);
717 }
718 event_sched_out(group_event, cpuctx, ctx);
719
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200720 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200721
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200722 return -EAGAIN;
723}
724
725/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200726 * Work out whether we can put this event group on the CPU now.
727 */
728static int group_can_go_on(struct perf_event *event,
729 struct perf_cpu_context *cpuctx,
730 int can_add_hw)
731{
732 /*
733 * Groups consisting entirely of software events can always go on.
734 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100735 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200736 return 1;
737 /*
738 * If an exclusive group is already on, no other hardware
739 * events can go on.
740 */
741 if (cpuctx->exclusive)
742 return 0;
743 /*
744 * If this group is exclusive and there are already
745 * events on the CPU, it can't go on.
746 */
747 if (event->attr.exclusive && cpuctx->active_oncpu)
748 return 0;
749 /*
750 * Otherwise, try to add it if all previous groups were able
751 * to go on.
752 */
753 return can_add_hw;
754}
755
756static void add_event_to_ctx(struct perf_event *event,
757 struct perf_event_context *ctx)
758{
759 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200760 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200761 event->tstamp_enabled = ctx->time;
762 event->tstamp_running = ctx->time;
763 event->tstamp_stopped = ctx->time;
764}
765
766/*
767 * Cross CPU call to install and enable a performance event
768 *
769 * Must be called with ctx->mutex held
770 */
771static void __perf_install_in_context(void *info)
772{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200773 struct perf_event *event = info;
774 struct perf_event_context *ctx = event->ctx;
775 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200776 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200777 int err;
778
779 /*
780 * If this is a task context, we need to check whether it is
781 * the current task context of this cpu. If not it has been
782 * scheduled out before the smp call arrived.
783 * Or possibly this is the right context but it isn't
784 * on this cpu because it had no events.
785 */
786 if (ctx->task && cpuctx->task_ctx != ctx) {
787 if (cpuctx->task_ctx || ctx->task != current)
788 return;
789 cpuctx->task_ctx = ctx;
790 }
791
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100792 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200793 ctx->is_active = 1;
794 update_context_time(ctx);
795
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200796 add_event_to_ctx(event, ctx);
797
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100798 if (event->cpu != -1 && event->cpu != smp_processor_id())
799 goto unlock;
800
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200801 /*
802 * Don't put the event on if it is disabled or if
803 * it is in a group and the group isn't on.
804 */
805 if (event->state != PERF_EVENT_STATE_INACTIVE ||
806 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
807 goto unlock;
808
809 /*
810 * An exclusive event can't go on if there are already active
811 * hardware events, and no hardware event can go on if there
812 * is already an exclusive event on.
813 */
814 if (!group_can_go_on(event, cpuctx, 1))
815 err = -EEXIST;
816 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100817 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200818
819 if (err) {
820 /*
821 * This event couldn't go on. If it is in a group
822 * then we have to pull the whole group off.
823 * If the event group is pinned then put it in error state.
824 */
825 if (leader != event)
826 group_sched_out(leader, cpuctx, ctx);
827 if (leader->attr.pinned) {
828 update_group_times(leader);
829 leader->state = PERF_EVENT_STATE_ERROR;
830 }
831 }
832
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200833unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100834 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200835}
836
837/*
838 * Attach a performance event to a context
839 *
840 * First we add the event to the list with the hardware enable bit
841 * in event->hw_config cleared.
842 *
843 * If the event is attached to a task which is on a CPU we use a smp
844 * call to enable it in the task context. The task might have been
845 * scheduled away, but we check this in the smp call again.
846 *
847 * Must be called with ctx->mutex held.
848 */
849static void
850perf_install_in_context(struct perf_event_context *ctx,
851 struct perf_event *event,
852 int cpu)
853{
854 struct task_struct *task = ctx->task;
855
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200856 event->ctx = ctx;
857
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200858 if (!task) {
859 /*
860 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200861 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862 */
863 smp_call_function_single(cpu, __perf_install_in_context,
864 event, 1);
865 return;
866 }
867
868retry:
869 task_oncpu_function_call(task, __perf_install_in_context,
870 event);
871
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100872 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200873 /*
874 * we need to retry the smp call.
875 */
876 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100877 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878 goto retry;
879 }
880
881 /*
882 * The lock prevents that this context is scheduled in so we
883 * can add the event safely, if it the call above did not
884 * succeed.
885 */
886 if (list_empty(&event->group_entry))
887 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100888 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200889}
890
891/*
892 * Put a event into inactive state and update time fields.
893 * Enabling the leader of a group effectively enables all
894 * the group members that aren't explicitly disabled, so we
895 * have to update their ->tstamp_enabled also.
896 * Note: this works for group members as well as group leaders
897 * since the non-leader members' sibling_lists will be empty.
898 */
899static void __perf_event_mark_enabled(struct perf_event *event,
900 struct perf_event_context *ctx)
901{
902 struct perf_event *sub;
903
904 event->state = PERF_EVENT_STATE_INACTIVE;
905 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200906 list_for_each_entry(sub, &event->sibling_list, group_entry) {
907 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908 sub->tstamp_enabled =
909 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200910 }
911 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200912}
913
914/*
915 * Cross CPU call to enable a performance event
916 */
917static void __perf_event_enable(void *info)
918{
919 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200920 struct perf_event_context *ctx = event->ctx;
921 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200922 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200923 int err;
924
925 /*
926 * If this is a per-task event, need to check whether this
927 * event's task is the current task on this cpu.
928 */
929 if (ctx->task && cpuctx->task_ctx != ctx) {
930 if (cpuctx->task_ctx || ctx->task != current)
931 return;
932 cpuctx->task_ctx = ctx;
933 }
934
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100935 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200936 ctx->is_active = 1;
937 update_context_time(ctx);
938
939 if (event->state >= PERF_EVENT_STATE_INACTIVE)
940 goto unlock;
941 __perf_event_mark_enabled(event, ctx);
942
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100943 if (event->cpu != -1 && event->cpu != smp_processor_id())
944 goto unlock;
945
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200946 /*
947 * If the event is in a group and isn't the group leader,
948 * then don't put it on unless the group is on.
949 */
950 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
951 goto unlock;
952
953 if (!group_can_go_on(event, cpuctx, 1)) {
954 err = -EEXIST;
955 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200956 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100957 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200958 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100959 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200960 }
961
962 if (err) {
963 /*
964 * If this event can't go on and it's part of a
965 * group, then the whole group has to come off.
966 */
967 if (leader != event)
968 group_sched_out(leader, cpuctx, ctx);
969 if (leader->attr.pinned) {
970 update_group_times(leader);
971 leader->state = PERF_EVENT_STATE_ERROR;
972 }
973 }
974
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200975unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100976 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200977}
978
979/*
980 * Enable a event.
981 *
982 * If event->ctx is a cloned context, callers must make sure that
983 * every task struct that event->ctx->task could possibly point to
984 * remains valid. This condition is satisfied when called through
985 * perf_event_for_each_child or perf_event_for_each as described
986 * for perf_event_disable.
987 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100988void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200989{
990 struct perf_event_context *ctx = event->ctx;
991 struct task_struct *task = ctx->task;
992
993 if (!task) {
994 /*
995 * Enable the event on the cpu that it's on
996 */
997 smp_call_function_single(event->cpu, __perf_event_enable,
998 event, 1);
999 return;
1000 }
1001
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001002 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001003 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1004 goto out;
1005
1006 /*
1007 * If the event is in error state, clear that first.
1008 * That way, if we see the event in error state below, we
1009 * know that it has gone back into error state, as distinct
1010 * from the task having been scheduled away before the
1011 * cross-call arrived.
1012 */
1013 if (event->state == PERF_EVENT_STATE_ERROR)
1014 event->state = PERF_EVENT_STATE_OFF;
1015
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001016retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001017 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 task_oncpu_function_call(task, __perf_event_enable, event);
1019
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001020 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001021
1022 /*
1023 * If the context is active and the event is still off,
1024 * we need to retry the cross-call.
1025 */
1026 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1027 goto retry;
1028
1029 /*
1030 * Since we have the lock this context can't be scheduled
1031 * in, so we can change the state safely.
1032 */
1033 if (event->state == PERF_EVENT_STATE_OFF)
1034 __perf_event_mark_enabled(event, ctx);
1035
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001036out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001037 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038}
1039
1040static int perf_event_refresh(struct perf_event *event, int refresh)
1041{
1042 /*
1043 * not supported on inherited events
1044 */
1045 if (event->attr.inherit)
1046 return -EINVAL;
1047
1048 atomic_add(refresh, &event->event_limit);
1049 perf_event_enable(event);
1050
1051 return 0;
1052}
1053
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001054enum event_type_t {
1055 EVENT_FLEXIBLE = 0x1,
1056 EVENT_PINNED = 0x2,
1057 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1058};
1059
1060static void ctx_sched_out(struct perf_event_context *ctx,
1061 struct perf_cpu_context *cpuctx,
1062 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001063{
1064 struct perf_event *event;
1065
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001066 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001067 ctx->is_active = 0;
1068 if (likely(!ctx->nr_events))
1069 goto out;
1070 update_context_time(ctx);
1071
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001072 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001073 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001074
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001075 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001076 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1077 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001078 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001079
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001080 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001081 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001082 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001083 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001084out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001085 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086}
1087
1088/*
1089 * Test whether two contexts are equivalent, i.e. whether they
1090 * have both been cloned from the same version of the same context
1091 * and they both have the same number of enabled events.
1092 * If the number of enabled events is the same, then the set
1093 * of enabled events should be the same, because these are both
1094 * inherited contexts, therefore we can't access individual events
1095 * in them directly with an fd; we can only enable/disable all
1096 * events via prctl, or enable/disable all events in a family
1097 * via ioctl, which will have the same effect on both contexts.
1098 */
1099static int context_equiv(struct perf_event_context *ctx1,
1100 struct perf_event_context *ctx2)
1101{
1102 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1103 && ctx1->parent_gen == ctx2->parent_gen
1104 && !ctx1->pin_count && !ctx2->pin_count;
1105}
1106
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001107static void __perf_event_sync_stat(struct perf_event *event,
1108 struct perf_event *next_event)
1109{
1110 u64 value;
1111
1112 if (!event->attr.inherit_stat)
1113 return;
1114
1115 /*
1116 * Update the event value, we cannot use perf_event_read()
1117 * because we're in the middle of a context switch and have IRQs
1118 * disabled, which upsets smp_call_function_single(), however
1119 * we know the event must be on the current CPU, therefore we
1120 * don't need to use it.
1121 */
1122 switch (event->state) {
1123 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001124 event->pmu->read(event);
1125 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126
1127 case PERF_EVENT_STATE_INACTIVE:
1128 update_event_times(event);
1129 break;
1130
1131 default:
1132 break;
1133 }
1134
1135 /*
1136 * In order to keep per-task stats reliable we need to flip the event
1137 * values when we flip the contexts.
1138 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001139 value = local64_read(&next_event->count);
1140 value = local64_xchg(&event->count, value);
1141 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142
1143 swap(event->total_time_enabled, next_event->total_time_enabled);
1144 swap(event->total_time_running, next_event->total_time_running);
1145
1146 /*
1147 * Since we swizzled the values, update the user visible data too.
1148 */
1149 perf_event_update_userpage(event);
1150 perf_event_update_userpage(next_event);
1151}
1152
1153#define list_next_entry(pos, member) \
1154 list_entry(pos->member.next, typeof(*pos), member)
1155
1156static void perf_event_sync_stat(struct perf_event_context *ctx,
1157 struct perf_event_context *next_ctx)
1158{
1159 struct perf_event *event, *next_event;
1160
1161 if (!ctx->nr_stat)
1162 return;
1163
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001164 update_context_time(ctx);
1165
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001166 event = list_first_entry(&ctx->event_list,
1167 struct perf_event, event_entry);
1168
1169 next_event = list_first_entry(&next_ctx->event_list,
1170 struct perf_event, event_entry);
1171
1172 while (&event->event_entry != &ctx->event_list &&
1173 &next_event->event_entry != &next_ctx->event_list) {
1174
1175 __perf_event_sync_stat(event, next_event);
1176
1177 event = list_next_entry(event, event_entry);
1178 next_event = list_next_entry(next_event, event_entry);
1179 }
1180}
1181
1182/*
1183 * Called from scheduler to remove the events of the current task,
1184 * with interrupts disabled.
1185 *
1186 * We stop each event and update the event value in event->count.
1187 *
1188 * This does not protect us against NMI, but disable()
1189 * sets the disabled bit in the control field of event _before_
1190 * accessing the event control register. If a NMI hits, then it will
1191 * not restart the event.
1192 */
1193void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001194 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001195{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001196 struct perf_event_context *ctx = task->perf_event_ctxp;
1197 struct perf_event_context *next_ctx;
1198 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001199 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200 int do_switch = 1;
1201
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001202 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001203
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001204 if (likely(!ctx))
1205 return;
1206
1207 cpuctx = __get_cpu_context(ctx);
1208 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001209 return;
1210
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001211 rcu_read_lock();
1212 parent = rcu_dereference(ctx->parent_ctx);
1213 next_ctx = next->perf_event_ctxp;
1214 if (parent && next_ctx &&
1215 rcu_dereference(next_ctx->parent_ctx) == parent) {
1216 /*
1217 * Looks like the two contexts are clones, so we might be
1218 * able to optimize the context switch. We lock both
1219 * contexts and check that they are clones under the
1220 * lock (including re-checking that neither has been
1221 * uncloned in the meantime). It doesn't matter which
1222 * order we take the locks because no other cpu could
1223 * be trying to lock both of these tasks.
1224 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001225 raw_spin_lock(&ctx->lock);
1226 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227 if (context_equiv(ctx, next_ctx)) {
1228 /*
1229 * XXX do we need a memory barrier of sorts
1230 * wrt to rcu_dereference() of perf_event_ctxp
1231 */
1232 task->perf_event_ctxp = next_ctx;
1233 next->perf_event_ctxp = ctx;
1234 ctx->task = next;
1235 next_ctx->task = task;
1236 do_switch = 0;
1237
1238 perf_event_sync_stat(ctx, next_ctx);
1239 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001240 raw_spin_unlock(&next_ctx->lock);
1241 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242 }
1243 rcu_read_unlock();
1244
1245 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001246 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001247 cpuctx->task_ctx = NULL;
1248 }
1249}
1250
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001251static void task_ctx_sched_out(struct perf_event_context *ctx,
1252 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001254 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001255
1256 if (!cpuctx->task_ctx)
1257 return;
1258
1259 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1260 return;
1261
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001262 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001263 cpuctx->task_ctx = NULL;
1264}
1265
1266/*
1267 * Called with IRQs disabled
1268 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001269static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001271 task_ctx_sched_out(ctx, EVENT_ALL);
1272}
1273
1274/*
1275 * Called with IRQs disabled
1276 */
1277static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1278 enum event_type_t event_type)
1279{
1280 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001281}
1282
1283static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001284ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001285 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001286{
1287 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001288
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001289 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1290 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001292 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001293 continue;
1294
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001295 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001296 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297
1298 /*
1299 * If this pinned group hasn't been scheduled,
1300 * put it in error state.
1301 */
1302 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1303 update_group_times(event);
1304 event->state = PERF_EVENT_STATE_ERROR;
1305 }
1306 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001307}
1308
1309static void
1310ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001311 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001312{
1313 struct perf_event *event;
1314 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001316 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1317 /* Ignore events in OFF or ERROR state */
1318 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001319 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320 /*
1321 * Listen to the 'cpu' scheduling filter constraint
1322 * of events:
1323 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001324 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001325 continue;
1326
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001327 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001328 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001329 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001330 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001332}
1333
1334static void
1335ctx_sched_in(struct perf_event_context *ctx,
1336 struct perf_cpu_context *cpuctx,
1337 enum event_type_t event_type)
1338{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001339 raw_spin_lock(&ctx->lock);
1340 ctx->is_active = 1;
1341 if (likely(!ctx->nr_events))
1342 goto out;
1343
1344 ctx->timestamp = perf_clock();
1345
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001346 /*
1347 * First go through the list and put on any pinned groups
1348 * in order to give them the best chance of going on.
1349 */
1350 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001351 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001352
1353 /* Then walk through the lower prio flexible groups */
1354 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001355 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001356
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001357out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001358 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001359}
1360
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001361static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1362 enum event_type_t event_type)
1363{
1364 struct perf_event_context *ctx = &cpuctx->ctx;
1365
1366 ctx_sched_in(ctx, cpuctx, event_type);
1367}
1368
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001369static void task_ctx_sched_in(struct task_struct *task,
1370 enum event_type_t event_type)
1371{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001372 struct perf_event_context *ctx = task->perf_event_ctxp;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001373 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001374
1375 if (likely(!ctx))
1376 return;
1377 if (cpuctx->task_ctx == ctx)
1378 return;
1379 ctx_sched_in(ctx, cpuctx, event_type);
1380 cpuctx->task_ctx = ctx;
1381}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382/*
1383 * Called from scheduler to add the events of the current task
1384 * with interrupts disabled.
1385 *
1386 * We restore the event value and then enable it.
1387 *
1388 * This does not protect us against NMI, but enable()
1389 * sets the enabled bit in the control field of event _before_
1390 * accessing the event control register. If a NMI hits, then it will
1391 * keep the event running.
1392 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001393void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001394{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001395 struct perf_event_context *ctx = task->perf_event_ctxp;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001396 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001397
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001398 if (likely(!ctx))
1399 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001400
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001401 cpuctx = __get_cpu_context(ctx);
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;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001417
1418 /*
1419 * Since these rotations are per-cpu, we need to ensure the
1420 * cpu-context we got scheduled on is actually rotating.
1421 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001422 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423}
1424
1425#define MAX_INTERRUPTS (~0ULL)
1426
1427static void perf_log_throttle(struct perf_event *event, int enable);
1428
Peter Zijlstraabd50712010-01-26 18:50:16 +01001429static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1430{
1431 u64 frequency = event->attr.sample_freq;
1432 u64 sec = NSEC_PER_SEC;
1433 u64 divisor, dividend;
1434
1435 int count_fls, nsec_fls, frequency_fls, sec_fls;
1436
1437 count_fls = fls64(count);
1438 nsec_fls = fls64(nsec);
1439 frequency_fls = fls64(frequency);
1440 sec_fls = 30;
1441
1442 /*
1443 * We got @count in @nsec, with a target of sample_freq HZ
1444 * the target period becomes:
1445 *
1446 * @count * 10^9
1447 * period = -------------------
1448 * @nsec * sample_freq
1449 *
1450 */
1451
1452 /*
1453 * Reduce accuracy by one bit such that @a and @b converge
1454 * to a similar magnitude.
1455 */
1456#define REDUCE_FLS(a, b) \
1457do { \
1458 if (a##_fls > b##_fls) { \
1459 a >>= 1; \
1460 a##_fls--; \
1461 } else { \
1462 b >>= 1; \
1463 b##_fls--; \
1464 } \
1465} while (0)
1466
1467 /*
1468 * Reduce accuracy until either term fits in a u64, then proceed with
1469 * the other, so that finally we can do a u64/u64 division.
1470 */
1471 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1472 REDUCE_FLS(nsec, frequency);
1473 REDUCE_FLS(sec, count);
1474 }
1475
1476 if (count_fls + sec_fls > 64) {
1477 divisor = nsec * frequency;
1478
1479 while (count_fls + sec_fls > 64) {
1480 REDUCE_FLS(count, sec);
1481 divisor >>= 1;
1482 }
1483
1484 dividend = count * sec;
1485 } else {
1486 dividend = count * sec;
1487
1488 while (nsec_fls + frequency_fls > 64) {
1489 REDUCE_FLS(nsec, frequency);
1490 dividend >>= 1;
1491 }
1492
1493 divisor = nsec * frequency;
1494 }
1495
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001496 if (!divisor)
1497 return dividend;
1498
Peter Zijlstraabd50712010-01-26 18:50:16 +01001499 return div64_u64(dividend, divisor);
1500}
1501
1502static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001503{
1504 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001505 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001506 s64 delta;
1507
Peter Zijlstraabd50712010-01-26 18:50:16 +01001508 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509
1510 delta = (s64)(period - hwc->sample_period);
1511 delta = (delta + 7) / 8; /* low pass filter */
1512
1513 sample_period = hwc->sample_period + delta;
1514
1515 if (!sample_period)
1516 sample_period = 1;
1517
1518 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001519
Peter Zijlstrae7850592010-05-21 14:43:08 +02001520 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001521 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001522 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001523 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001524 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001525}
1526
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001527static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001528{
1529 struct perf_event *event;
1530 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001531 u64 interrupts, now;
1532 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001534 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001535 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536 if (event->state != PERF_EVENT_STATE_ACTIVE)
1537 continue;
1538
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001539 if (event->cpu != -1 && event->cpu != smp_processor_id())
1540 continue;
1541
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001542 hwc = &event->hw;
1543
1544 interrupts = hwc->interrupts;
1545 hwc->interrupts = 0;
1546
1547 /*
1548 * unthrottle events on the tick
1549 */
1550 if (interrupts == MAX_INTERRUPTS) {
1551 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001552 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553 }
1554
1555 if (!event->attr.freq || !event->attr.sample_freq)
1556 continue;
1557
Peter Zijlstraabd50712010-01-26 18:50:16 +01001558 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001559 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001560 delta = now - hwc->freq_count_stamp;
1561 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001562
Peter Zijlstraabd50712010-01-26 18:50:16 +01001563 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001564 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001565 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001566 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567}
1568
1569/*
1570 * Round-robin a context's events:
1571 */
1572static void rotate_ctx(struct perf_event_context *ctx)
1573{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001574 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001575
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001576 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001577 list_rotate_left(&ctx->flexible_groups);
1578
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001579 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580}
1581
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001582/*
1583 * Cannot race with ->pmu_rotate_start() because this is ran from hardirq
1584 * context, and ->pmu_rotate_start() is called with irqs disabled (both are
1585 * cpu affine, so there are no SMP races).
1586 */
1587static enum hrtimer_restart perf_event_context_tick(struct hrtimer *timer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001588{
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001589 enum hrtimer_restart restart = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590 struct perf_cpu_context *cpuctx;
1591 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001592 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001594 cpuctx = container_of(timer, struct perf_cpu_context, timer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001596 if (cpuctx->ctx.nr_events) {
1597 restart = HRTIMER_RESTART;
1598 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1599 rotate = 1;
1600 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001602 ctx = current->perf_event_ctxp;
1603 if (ctx && ctx->nr_events) {
1604 restart = HRTIMER_RESTART;
1605 if (ctx->nr_events != ctx->nr_active)
1606 rotate = 1;
1607 }
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001608
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001609 perf_ctx_adjust_freq(&cpuctx->ctx, cpuctx->timer_interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610 if (ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001611 perf_ctx_adjust_freq(ctx, cpuctx->timer_interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001613 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001614 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001615
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001616 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001618 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619
1620 rotate_ctx(&cpuctx->ctx);
1621 if (ctx)
1622 rotate_ctx(ctx);
1623
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001624 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625 if (ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001626 task_ctx_sched_in(current, EVENT_FLEXIBLE);
1627
1628done:
1629 hrtimer_forward_now(timer, ns_to_ktime(cpuctx->timer_interval));
1630
1631 return restart;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001632}
1633
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001634static int event_enable_on_exec(struct perf_event *event,
1635 struct perf_event_context *ctx)
1636{
1637 if (!event->attr.enable_on_exec)
1638 return 0;
1639
1640 event->attr.enable_on_exec = 0;
1641 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1642 return 0;
1643
1644 __perf_event_mark_enabled(event, ctx);
1645
1646 return 1;
1647}
1648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001649/*
1650 * Enable all of a task's events that have been marked enable-on-exec.
1651 * This expects task == current.
1652 */
1653static void perf_event_enable_on_exec(struct task_struct *task)
1654{
1655 struct perf_event_context *ctx;
1656 struct perf_event *event;
1657 unsigned long flags;
1658 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001659 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660
1661 local_irq_save(flags);
1662 ctx = task->perf_event_ctxp;
1663 if (!ctx || !ctx->nr_events)
1664 goto out;
1665
1666 __perf_event_task_sched_out(ctx);
1667
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001668 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001669
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001670 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1671 ret = event_enable_on_exec(event, ctx);
1672 if (ret)
1673 enabled = 1;
1674 }
1675
1676 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1677 ret = event_enable_on_exec(event, ctx);
1678 if (ret)
1679 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680 }
1681
1682 /*
1683 * Unclone this context if we enabled any event.
1684 */
1685 if (enabled)
1686 unclone_ctx(ctx);
1687
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001688 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001689
Peter Zijlstra49f47432009-12-27 11:51:52 +01001690 perf_event_task_sched_in(task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001691out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692 local_irq_restore(flags);
1693}
1694
1695/*
1696 * Cross CPU call to read the hardware event
1697 */
1698static void __perf_event_read(void *info)
1699{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700 struct perf_event *event = info;
1701 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001702 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703
1704 /*
1705 * If this is a task context, we need to check whether it is
1706 * the current task context of this cpu. If not it has been
1707 * scheduled out before the smp call arrived. In that case
1708 * event->count would have been updated to a recent sample
1709 * when the event was scheduled out.
1710 */
1711 if (ctx->task && cpuctx->task_ctx != ctx)
1712 return;
1713
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001714 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001715 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001717 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001718
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001719 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720}
1721
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001722static inline u64 perf_event_count(struct perf_event *event)
1723{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001724 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001725}
1726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727static u64 perf_event_read(struct perf_event *event)
1728{
1729 /*
1730 * If event is enabled and currently active on a CPU, update the
1731 * value in the event structure:
1732 */
1733 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1734 smp_call_function_single(event->oncpu,
1735 __perf_event_read, event, 1);
1736 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001737 struct perf_event_context *ctx = event->ctx;
1738 unsigned long flags;
1739
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001740 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001741 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001743 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001744 }
1745
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001746 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747}
1748
1749/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001750 * Callchain support
1751 */
1752
1753struct callchain_cpus_entries {
1754 struct rcu_head rcu_head;
1755 struct perf_callchain_entry *cpu_entries[0];
1756};
1757
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001758static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001759static atomic_t nr_callchain_events;
1760static DEFINE_MUTEX(callchain_mutex);
1761struct callchain_cpus_entries *callchain_cpus_entries;
1762
1763
1764__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1765 struct pt_regs *regs)
1766{
1767}
1768
1769__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1770 struct pt_regs *regs)
1771{
1772}
1773
1774static void release_callchain_buffers_rcu(struct rcu_head *head)
1775{
1776 struct callchain_cpus_entries *entries;
1777 int cpu;
1778
1779 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1780
1781 for_each_possible_cpu(cpu)
1782 kfree(entries->cpu_entries[cpu]);
1783
1784 kfree(entries);
1785}
1786
1787static void release_callchain_buffers(void)
1788{
1789 struct callchain_cpus_entries *entries;
1790
1791 entries = callchain_cpus_entries;
1792 rcu_assign_pointer(callchain_cpus_entries, NULL);
1793 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1794}
1795
1796static int alloc_callchain_buffers(void)
1797{
1798 int cpu;
1799 int size;
1800 struct callchain_cpus_entries *entries;
1801
1802 /*
1803 * We can't use the percpu allocation API for data that can be
1804 * accessed from NMI. Use a temporary manual per cpu allocation
1805 * until that gets sorted out.
1806 */
1807 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1808 num_possible_cpus();
1809
1810 entries = kzalloc(size, GFP_KERNEL);
1811 if (!entries)
1812 return -ENOMEM;
1813
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001814 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001815
1816 for_each_possible_cpu(cpu) {
1817 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1818 cpu_to_node(cpu));
1819 if (!entries->cpu_entries[cpu])
1820 goto fail;
1821 }
1822
1823 rcu_assign_pointer(callchain_cpus_entries, entries);
1824
1825 return 0;
1826
1827fail:
1828 for_each_possible_cpu(cpu)
1829 kfree(entries->cpu_entries[cpu]);
1830 kfree(entries);
1831
1832 return -ENOMEM;
1833}
1834
1835static int get_callchain_buffers(void)
1836{
1837 int err = 0;
1838 int count;
1839
1840 mutex_lock(&callchain_mutex);
1841
1842 count = atomic_inc_return(&nr_callchain_events);
1843 if (WARN_ON_ONCE(count < 1)) {
1844 err = -EINVAL;
1845 goto exit;
1846 }
1847
1848 if (count > 1) {
1849 /* If the allocation failed, give up */
1850 if (!callchain_cpus_entries)
1851 err = -ENOMEM;
1852 goto exit;
1853 }
1854
1855 err = alloc_callchain_buffers();
1856 if (err)
1857 release_callchain_buffers();
1858exit:
1859 mutex_unlock(&callchain_mutex);
1860
1861 return err;
1862}
1863
1864static void put_callchain_buffers(void)
1865{
1866 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1867 release_callchain_buffers();
1868 mutex_unlock(&callchain_mutex);
1869 }
1870}
1871
1872static int get_recursion_context(int *recursion)
1873{
1874 int rctx;
1875
1876 if (in_nmi())
1877 rctx = 3;
1878 else if (in_irq())
1879 rctx = 2;
1880 else if (in_softirq())
1881 rctx = 1;
1882 else
1883 rctx = 0;
1884
1885 if (recursion[rctx])
1886 return -1;
1887
1888 recursion[rctx]++;
1889 barrier();
1890
1891 return rctx;
1892}
1893
1894static inline void put_recursion_context(int *recursion, int rctx)
1895{
1896 barrier();
1897 recursion[rctx]--;
1898}
1899
1900static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1901{
1902 int cpu;
1903 struct callchain_cpus_entries *entries;
1904
1905 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1906 if (*rctx == -1)
1907 return NULL;
1908
1909 entries = rcu_dereference(callchain_cpus_entries);
1910 if (!entries)
1911 return NULL;
1912
1913 cpu = smp_processor_id();
1914
1915 return &entries->cpu_entries[cpu][*rctx];
1916}
1917
1918static void
1919put_callchain_entry(int rctx)
1920{
1921 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1922}
1923
1924static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1925{
1926 int rctx;
1927 struct perf_callchain_entry *entry;
1928
1929
1930 entry = get_callchain_entry(&rctx);
1931 if (rctx == -1)
1932 return NULL;
1933
1934 if (!entry)
1935 goto exit_put;
1936
1937 entry->nr = 0;
1938
1939 if (!user_mode(regs)) {
1940 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1941 perf_callchain_kernel(entry, regs);
1942 if (current->mm)
1943 regs = task_pt_regs(current);
1944 else
1945 regs = NULL;
1946 }
1947
1948 if (regs) {
1949 perf_callchain_store(entry, PERF_CONTEXT_USER);
1950 perf_callchain_user(entry, regs);
1951 }
1952
1953exit_put:
1954 put_callchain_entry(rctx);
1955
1956 return entry;
1957}
1958
1959/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001960 * Initialize the perf_event context in a task_struct:
1961 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02001962static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001964 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001965 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001966 INIT_LIST_HEAD(&ctx->pinned_groups);
1967 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 INIT_LIST_HEAD(&ctx->event_list);
1969 atomic_set(&ctx->refcount, 1);
Peter Zijlstraeb184472010-09-07 15:55:13 +02001970}
1971
1972static struct perf_event_context *
1973alloc_perf_context(struct pmu *pmu, struct task_struct *task)
1974{
1975 struct perf_event_context *ctx;
1976
1977 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
1978 if (!ctx)
1979 return NULL;
1980
1981 __perf_event_init_context(ctx);
1982 if (task) {
1983 ctx->task = task;
1984 get_task_struct(task);
1985 }
1986 ctx->pmu = pmu;
1987
1988 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989}
1990
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001991static struct perf_event_context *
1992find_get_context(struct pmu *pmu, pid_t pid, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001993{
1994 struct perf_event_context *ctx;
1995 struct perf_cpu_context *cpuctx;
1996 struct task_struct *task;
1997 unsigned long flags;
1998 int err;
1999
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002000 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001 /* Must be root to operate on a CPU event: */
2002 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2003 return ERR_PTR(-EACCES);
2004
Paul Mackerras0f624e72009-12-15 19:40:32 +11002005 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006 return ERR_PTR(-EINVAL);
2007
2008 /*
2009 * We could be clever and allow to attach a event to an
2010 * offline CPU and activate it when the CPU comes up, but
2011 * that's for later.
2012 */
Rusty Russellf6325e32009-12-17 11:43:08 -06002013 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014 return ERR_PTR(-ENODEV);
2015
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002016 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002017 ctx = &cpuctx->ctx;
2018 get_ctx(ctx);
2019
2020 return ctx;
2021 }
2022
2023 rcu_read_lock();
2024 if (!pid)
2025 task = current;
2026 else
2027 task = find_task_by_vpid(pid);
2028 if (task)
2029 get_task_struct(task);
2030 rcu_read_unlock();
2031
2032 if (!task)
2033 return ERR_PTR(-ESRCH);
2034
2035 /*
2036 * Can't attach events to a dying task.
2037 */
2038 err = -ESRCH;
2039 if (task->flags & PF_EXITING)
2040 goto errout;
2041
2042 /* Reuse ptrace permission checks for now. */
2043 err = -EACCES;
2044 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2045 goto errout;
2046
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002047retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002048 ctx = perf_lock_task_context(task, &flags);
2049 if (ctx) {
2050 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002051 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 }
2053
2054 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002055 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002056 err = -ENOMEM;
2057 if (!ctx)
2058 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002061
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
2063 /*
2064 * We raced with some other task; use
2065 * the context they set.
2066 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002067 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002068 kfree(ctx);
2069 goto retry;
2070 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002071 }
2072
2073 put_task_struct(task);
2074 return ctx;
2075
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002076errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002077 put_task_struct(task);
2078 return ERR_PTR(err);
2079}
2080
Li Zefan6fb29152009-10-15 11:21:42 +08002081static void perf_event_free_filter(struct perf_event *event);
2082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002083static void free_event_rcu(struct rcu_head *head)
2084{
2085 struct perf_event *event;
2086
2087 event = container_of(head, struct perf_event, rcu_head);
2088 if (event->ns)
2089 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002090 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002091 kfree(event);
2092}
2093
2094static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002095static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096
2097static void free_event(struct perf_event *event)
2098{
2099 perf_pending_sync(event);
2100
2101 if (!event->parent) {
2102 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002103 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002104 atomic_dec(&nr_mmap_events);
2105 if (event->attr.comm)
2106 atomic_dec(&nr_comm_events);
2107 if (event->attr.task)
2108 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002109 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2110 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111 }
2112
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002113 if (event->buffer) {
2114 perf_buffer_put(event->buffer);
2115 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116 }
2117
2118 if (event->destroy)
2119 event->destroy(event);
2120
2121 put_ctx(event->ctx);
2122 call_rcu(&event->rcu_head, free_event_rcu);
2123}
2124
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002125int perf_event_release_kernel(struct perf_event *event)
2126{
2127 struct perf_event_context *ctx = event->ctx;
2128
Peter Zijlstra050735b2010-05-11 11:51:53 +02002129 /*
2130 * Remove from the PMU, can't get re-enabled since we got
2131 * here because the last ref went.
2132 */
2133 perf_event_disable(event);
2134
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002135 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002136 /*
2137 * There are two ways this annotation is useful:
2138 *
2139 * 1) there is a lock recursion from perf_event_exit_task
2140 * see the comment there.
2141 *
2142 * 2) there is a lock-inversion with mmap_sem through
2143 * perf_event_read_group(), which takes faults while
2144 * holding ctx->mutex, however this is called after
2145 * the last filedesc died, so there is no possibility
2146 * to trigger the AB-BA case.
2147 */
2148 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002149 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002150 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002151 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002152 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002153 mutex_unlock(&ctx->mutex);
2154
2155 mutex_lock(&event->owner->perf_event_mutex);
2156 list_del_init(&event->owner_entry);
2157 mutex_unlock(&event->owner->perf_event_mutex);
2158 put_task_struct(event->owner);
2159
2160 free_event(event);
2161
2162 return 0;
2163}
2164EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2165
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002166/*
2167 * Called when the last reference to the file is gone.
2168 */
2169static int perf_release(struct inode *inode, struct file *file)
2170{
2171 struct perf_event *event = file->private_data;
2172
2173 file->private_data = NULL;
2174
2175 return perf_event_release_kernel(event);
2176}
2177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002178static int perf_event_read_size(struct perf_event *event)
2179{
2180 int entry = sizeof(u64); /* value */
2181 int size = 0;
2182 int nr = 1;
2183
2184 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2185 size += sizeof(u64);
2186
2187 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2188 size += sizeof(u64);
2189
2190 if (event->attr.read_format & PERF_FORMAT_ID)
2191 entry += sizeof(u64);
2192
2193 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2194 nr += event->group_leader->nr_siblings;
2195 size += sizeof(u64);
2196 }
2197
2198 size += entry * nr;
2199
2200 return size;
2201}
2202
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002203u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002204{
2205 struct perf_event *child;
2206 u64 total = 0;
2207
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002208 *enabled = 0;
2209 *running = 0;
2210
Peter Zijlstra6f105812009-11-20 22:19:56 +01002211 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002212 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002213 *enabled += event->total_time_enabled +
2214 atomic64_read(&event->child_total_time_enabled);
2215 *running += event->total_time_running +
2216 atomic64_read(&event->child_total_time_running);
2217
2218 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002219 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002220 *enabled += child->total_time_enabled;
2221 *running += child->total_time_running;
2222 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002223 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002224
2225 return total;
2226}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002227EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229static int perf_event_read_group(struct perf_event *event,
2230 u64 read_format, char __user *buf)
2231{
2232 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002233 int n = 0, size = 0, ret = -EFAULT;
2234 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002235 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002236 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002237
Peter Zijlstra6f105812009-11-20 22:19:56 +01002238 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002239 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002240
2241 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002242 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2243 values[n++] = enabled;
2244 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2245 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002246 values[n++] = count;
2247 if (read_format & PERF_FORMAT_ID)
2248 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002249
2250 size = n * sizeof(u64);
2251
2252 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002253 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254
Peter Zijlstra6f105812009-11-20 22:19:56 +01002255 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256
2257 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002258 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002260 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002261 if (read_format & PERF_FORMAT_ID)
2262 values[n++] = primary_event_id(sub);
2263
2264 size = n * sizeof(u64);
2265
Stephane Eranian184d3da2009-11-23 21:40:49 -08002266 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002267 ret = -EFAULT;
2268 goto unlock;
2269 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002270
2271 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002273unlock:
2274 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275
Peter Zijlstraabf48682009-11-20 22:19:49 +01002276 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002277}
2278
2279static int perf_event_read_one(struct perf_event *event,
2280 u64 read_format, char __user *buf)
2281{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002282 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283 u64 values[4];
2284 int n = 0;
2285
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002286 values[n++] = perf_event_read_value(event, &enabled, &running);
2287 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2288 values[n++] = enabled;
2289 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2290 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291 if (read_format & PERF_FORMAT_ID)
2292 values[n++] = primary_event_id(event);
2293
2294 if (copy_to_user(buf, values, n * sizeof(u64)))
2295 return -EFAULT;
2296
2297 return n * sizeof(u64);
2298}
2299
2300/*
2301 * Read the performance event - simple non blocking version for now
2302 */
2303static ssize_t
2304perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2305{
2306 u64 read_format = event->attr.read_format;
2307 int ret;
2308
2309 /*
2310 * Return end-of-file for a read on a event that is in
2311 * error state (i.e. because it was pinned but it couldn't be
2312 * scheduled on to the CPU at some point).
2313 */
2314 if (event->state == PERF_EVENT_STATE_ERROR)
2315 return 0;
2316
2317 if (count < perf_event_read_size(event))
2318 return -ENOSPC;
2319
2320 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321 if (read_format & PERF_FORMAT_GROUP)
2322 ret = perf_event_read_group(event, read_format, buf);
2323 else
2324 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325
2326 return ret;
2327}
2328
2329static ssize_t
2330perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2331{
2332 struct perf_event *event = file->private_data;
2333
2334 return perf_read_hw(event, buf, count);
2335}
2336
2337static unsigned int perf_poll(struct file *file, poll_table *wait)
2338{
2339 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002340 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002341 unsigned int events = POLL_HUP;
2342
2343 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002344 buffer = rcu_dereference(event->buffer);
2345 if (buffer)
2346 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347 rcu_read_unlock();
2348
2349 poll_wait(file, &event->waitq, wait);
2350
2351 return events;
2352}
2353
2354static void perf_event_reset(struct perf_event *event)
2355{
2356 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002357 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002358 perf_event_update_userpage(event);
2359}
2360
2361/*
2362 * Holding the top-level event's child_mutex means that any
2363 * descendant process that has inherited this event will block
2364 * in sync_child_event if it goes to exit, thus satisfying the
2365 * task existence requirements of perf_event_enable/disable.
2366 */
2367static void perf_event_for_each_child(struct perf_event *event,
2368 void (*func)(struct perf_event *))
2369{
2370 struct perf_event *child;
2371
2372 WARN_ON_ONCE(event->ctx->parent_ctx);
2373 mutex_lock(&event->child_mutex);
2374 func(event);
2375 list_for_each_entry(child, &event->child_list, child_list)
2376 func(child);
2377 mutex_unlock(&event->child_mutex);
2378}
2379
2380static void perf_event_for_each(struct perf_event *event,
2381 void (*func)(struct perf_event *))
2382{
2383 struct perf_event_context *ctx = event->ctx;
2384 struct perf_event *sibling;
2385
2386 WARN_ON_ONCE(ctx->parent_ctx);
2387 mutex_lock(&ctx->mutex);
2388 event = event->group_leader;
2389
2390 perf_event_for_each_child(event, func);
2391 func(event);
2392 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2393 perf_event_for_each_child(event, func);
2394 mutex_unlock(&ctx->mutex);
2395}
2396
2397static int perf_event_period(struct perf_event *event, u64 __user *arg)
2398{
2399 struct perf_event_context *ctx = event->ctx;
2400 unsigned long size;
2401 int ret = 0;
2402 u64 value;
2403
2404 if (!event->attr.sample_period)
2405 return -EINVAL;
2406
2407 size = copy_from_user(&value, arg, sizeof(value));
2408 if (size != sizeof(value))
2409 return -EFAULT;
2410
2411 if (!value)
2412 return -EINVAL;
2413
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002414 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415 if (event->attr.freq) {
2416 if (value > sysctl_perf_event_sample_rate) {
2417 ret = -EINVAL;
2418 goto unlock;
2419 }
2420
2421 event->attr.sample_freq = value;
2422 } else {
2423 event->attr.sample_period = value;
2424 event->hw.sample_period = value;
2425 }
2426unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002427 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428
2429 return ret;
2430}
2431
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002432static const struct file_operations perf_fops;
2433
2434static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2435{
2436 struct file *file;
2437
2438 file = fget_light(fd, fput_needed);
2439 if (!file)
2440 return ERR_PTR(-EBADF);
2441
2442 if (file->f_op != &perf_fops) {
2443 fput_light(file, *fput_needed);
2444 *fput_needed = 0;
2445 return ERR_PTR(-EBADF);
2446 }
2447
2448 return file->private_data;
2449}
2450
2451static int perf_event_set_output(struct perf_event *event,
2452 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002453static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
2455static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2456{
2457 struct perf_event *event = file->private_data;
2458 void (*func)(struct perf_event *);
2459 u32 flags = arg;
2460
2461 switch (cmd) {
2462 case PERF_EVENT_IOC_ENABLE:
2463 func = perf_event_enable;
2464 break;
2465 case PERF_EVENT_IOC_DISABLE:
2466 func = perf_event_disable;
2467 break;
2468 case PERF_EVENT_IOC_RESET:
2469 func = perf_event_reset;
2470 break;
2471
2472 case PERF_EVENT_IOC_REFRESH:
2473 return perf_event_refresh(event, arg);
2474
2475 case PERF_EVENT_IOC_PERIOD:
2476 return perf_event_period(event, (u64 __user *)arg);
2477
2478 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002479 {
2480 struct perf_event *output_event = NULL;
2481 int fput_needed = 0;
2482 int ret;
2483
2484 if (arg != -1) {
2485 output_event = perf_fget_light(arg, &fput_needed);
2486 if (IS_ERR(output_event))
2487 return PTR_ERR(output_event);
2488 }
2489
2490 ret = perf_event_set_output(event, output_event);
2491 if (output_event)
2492 fput_light(output_event->filp, fput_needed);
2493
2494 return ret;
2495 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002496
Li Zefan6fb29152009-10-15 11:21:42 +08002497 case PERF_EVENT_IOC_SET_FILTER:
2498 return perf_event_set_filter(event, (void __user *)arg);
2499
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002500 default:
2501 return -ENOTTY;
2502 }
2503
2504 if (flags & PERF_IOC_FLAG_GROUP)
2505 perf_event_for_each(event, func);
2506 else
2507 perf_event_for_each_child(event, func);
2508
2509 return 0;
2510}
2511
2512int perf_event_task_enable(void)
2513{
2514 struct perf_event *event;
2515
2516 mutex_lock(&current->perf_event_mutex);
2517 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2518 perf_event_for_each_child(event, perf_event_enable);
2519 mutex_unlock(&current->perf_event_mutex);
2520
2521 return 0;
2522}
2523
2524int perf_event_task_disable(void)
2525{
2526 struct perf_event *event;
2527
2528 mutex_lock(&current->perf_event_mutex);
2529 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2530 perf_event_for_each_child(event, perf_event_disable);
2531 mutex_unlock(&current->perf_event_mutex);
2532
2533 return 0;
2534}
2535
2536#ifndef PERF_EVENT_INDEX_OFFSET
2537# define PERF_EVENT_INDEX_OFFSET 0
2538#endif
2539
2540static int perf_event_index(struct perf_event *event)
2541{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002542 if (event->hw.state & PERF_HES_STOPPED)
2543 return 0;
2544
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002545 if (event->state != PERF_EVENT_STATE_ACTIVE)
2546 return 0;
2547
2548 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2549}
2550
2551/*
2552 * Callers need to ensure there can be no nesting of this function, otherwise
2553 * the seqlock logic goes bad. We can not serialize this because the arch
2554 * code calls this from NMI context.
2555 */
2556void perf_event_update_userpage(struct perf_event *event)
2557{
2558 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002559 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002560
2561 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002562 buffer = rcu_dereference(event->buffer);
2563 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002564 goto unlock;
2565
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002566 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002567
2568 /*
2569 * Disable preemption so as to not let the corresponding user-space
2570 * spin too long if we get preempted.
2571 */
2572 preempt_disable();
2573 ++userpg->lock;
2574 barrier();
2575 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002576 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002578 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002579
2580 userpg->time_enabled = event->total_time_enabled +
2581 atomic64_read(&event->child_total_time_enabled);
2582
2583 userpg->time_running = event->total_time_running +
2584 atomic64_read(&event->child_total_time_running);
2585
2586 barrier();
2587 ++userpg->lock;
2588 preempt_enable();
2589unlock:
2590 rcu_read_unlock();
2591}
2592
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002593static unsigned long perf_data_size(struct perf_buffer *buffer);
2594
2595static void
2596perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2597{
2598 long max_size = perf_data_size(buffer);
2599
2600 if (watermark)
2601 buffer->watermark = min(max_size, watermark);
2602
2603 if (!buffer->watermark)
2604 buffer->watermark = max_size / 2;
2605
2606 if (flags & PERF_BUFFER_WRITABLE)
2607 buffer->writable = 1;
2608
2609 atomic_set(&buffer->refcount, 1);
2610}
2611
Peter Zijlstra906010b2009-09-21 16:08:49 +02002612#ifndef CONFIG_PERF_USE_VMALLOC
2613
2614/*
2615 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2616 */
2617
2618static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002619perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002620{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002621 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002622 return NULL;
2623
2624 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002625 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002626
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002627 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002628}
2629
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002630static void *perf_mmap_alloc_page(int cpu)
2631{
2632 struct page *page;
2633 int node;
2634
2635 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2636 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2637 if (!page)
2638 return NULL;
2639
2640 return page_address(page);
2641}
2642
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002643static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002644perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002646 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002647 unsigned long size;
2648 int i;
2649
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002650 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002651 size += nr_pages * sizeof(void *);
2652
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002653 buffer = kzalloc(size, GFP_KERNEL);
2654 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002655 goto fail;
2656
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002657 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002658 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002659 goto fail_user_page;
2660
2661 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002662 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002663 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002664 goto fail_data_pages;
2665 }
2666
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002667 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002668
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002669 perf_buffer_init(buffer, watermark, flags);
2670
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002671 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002672
2673fail_data_pages:
2674 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002675 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002676
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002677 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002678
2679fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002680 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002681
2682fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002683 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002684}
2685
2686static void perf_mmap_free_page(unsigned long addr)
2687{
2688 struct page *page = virt_to_page((void *)addr);
2689
2690 page->mapping = NULL;
2691 __free_page(page);
2692}
2693
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002694static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002695{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696 int i;
2697
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002698 perf_mmap_free_page((unsigned long)buffer->user_page);
2699 for (i = 0; i < buffer->nr_pages; i++)
2700 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2701 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002702}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002703
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002704static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002705{
2706 return 0;
2707}
2708
Peter Zijlstra906010b2009-09-21 16:08:49 +02002709#else
2710
2711/*
2712 * Back perf_mmap() with vmalloc memory.
2713 *
2714 * Required for architectures that have d-cache aliasing issues.
2715 */
2716
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002717static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002718{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002719 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002720}
2721
Peter Zijlstra906010b2009-09-21 16:08:49 +02002722static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002723perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002724{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002725 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002726 return NULL;
2727
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002728 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002729}
2730
2731static void perf_mmap_unmark_page(void *addr)
2732{
2733 struct page *page = vmalloc_to_page(addr);
2734
2735 page->mapping = NULL;
2736}
2737
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002738static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002739{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002740 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002741 void *base;
2742 int i, nr;
2743
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002744 buffer = container_of(work, struct perf_buffer, work);
2745 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002746
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002747 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002748 for (i = 0; i < nr + 1; i++)
2749 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2750
2751 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002752 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002753}
2754
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002755static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002756{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002757 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002758}
2759
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002760static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002761perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002762{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002763 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002764 unsigned long size;
2765 void *all_buf;
2766
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002767 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002768 size += sizeof(void *);
2769
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002770 buffer = kzalloc(size, GFP_KERNEL);
2771 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002772 goto fail;
2773
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002774 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002775
2776 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2777 if (!all_buf)
2778 goto fail_all_buf;
2779
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002780 buffer->user_page = all_buf;
2781 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2782 buffer->page_order = ilog2(nr_pages);
2783 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002784
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002785 perf_buffer_init(buffer, watermark, flags);
2786
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002787 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002788
2789fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002790 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002791
2792fail:
2793 return NULL;
2794}
2795
2796#endif
2797
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002798static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002799{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002800 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002801}
2802
Peter Zijlstra906010b2009-09-21 16:08:49 +02002803static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2804{
2805 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002806 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002807 int ret = VM_FAULT_SIGBUS;
2808
2809 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2810 if (vmf->pgoff == 0)
2811 ret = 0;
2812 return ret;
2813 }
2814
2815 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002816 buffer = rcu_dereference(event->buffer);
2817 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002818 goto unlock;
2819
2820 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2821 goto unlock;
2822
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002823 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002824 if (!vmf->page)
2825 goto unlock;
2826
2827 get_page(vmf->page);
2828 vmf->page->mapping = vma->vm_file->f_mapping;
2829 vmf->page->index = vmf->pgoff;
2830
2831 ret = 0;
2832unlock:
2833 rcu_read_unlock();
2834
2835 return ret;
2836}
2837
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002838static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002839{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002840 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002841
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002842 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2843 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002844}
2845
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002846static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002847{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002848 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002850 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002851 buffer = rcu_dereference(event->buffer);
2852 if (buffer) {
2853 if (!atomic_inc_not_zero(&buffer->refcount))
2854 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002855 }
2856 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002857
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002858 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002859}
2860
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002861static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002862{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002863 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002864 return;
2865
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002866 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867}
2868
2869static void perf_mmap_open(struct vm_area_struct *vma)
2870{
2871 struct perf_event *event = vma->vm_file->private_data;
2872
2873 atomic_inc(&event->mmap_count);
2874}
2875
2876static void perf_mmap_close(struct vm_area_struct *vma)
2877{
2878 struct perf_event *event = vma->vm_file->private_data;
2879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002880 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002881 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002882 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002883 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002884
Peter Zijlstra906010b2009-09-21 16:08:49 +02002885 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002886 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002887 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002888 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002889
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002890 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002891 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002892 }
2893}
2894
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002895static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002896 .open = perf_mmap_open,
2897 .close = perf_mmap_close,
2898 .fault = perf_mmap_fault,
2899 .page_mkwrite = perf_mmap_fault,
2900};
2901
2902static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2903{
2904 struct perf_event *event = file->private_data;
2905 unsigned long user_locked, user_lock_limit;
2906 struct user_struct *user = current_user();
2907 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002908 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909 unsigned long vma_size;
2910 unsigned long nr_pages;
2911 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002912 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913
Peter Zijlstrac7920612010-05-18 10:33:24 +02002914 /*
2915 * Don't allow mmap() of inherited per-task counters. This would
2916 * create a performance issue due to all children writing to the
2917 * same buffer.
2918 */
2919 if (event->cpu == -1 && event->attr.inherit)
2920 return -EINVAL;
2921
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002922 if (!(vma->vm_flags & VM_SHARED))
2923 return -EINVAL;
2924
2925 vma_size = vma->vm_end - vma->vm_start;
2926 nr_pages = (vma_size / PAGE_SIZE) - 1;
2927
2928 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002929 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002930 * can do bitmasks instead of modulo.
2931 */
2932 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2933 return -EINVAL;
2934
2935 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2936 return -EINVAL;
2937
2938 if (vma->vm_pgoff != 0)
2939 return -EINVAL;
2940
2941 WARN_ON_ONCE(event->ctx->parent_ctx);
2942 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002943 if (event->buffer) {
2944 if (event->buffer->nr_pages == nr_pages)
2945 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002946 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947 ret = -EINVAL;
2948 goto unlock;
2949 }
2950
2951 user_extra = nr_pages + 1;
2952 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2953
2954 /*
2955 * Increase the limit linearly with more CPUs:
2956 */
2957 user_lock_limit *= num_online_cpus();
2958
2959 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2960
2961 extra = 0;
2962 if (user_locked > user_lock_limit)
2963 extra = user_locked - user_lock_limit;
2964
Jiri Slaby78d7d402010-03-05 13:42:54 -08002965 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966 lock_limit >>= PAGE_SHIFT;
2967 locked = vma->vm_mm->locked_vm + extra;
2968
2969 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2970 !capable(CAP_IPC_LOCK)) {
2971 ret = -EPERM;
2972 goto unlock;
2973 }
2974
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002975 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002976
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002977 if (vma->vm_flags & VM_WRITE)
2978 flags |= PERF_BUFFER_WRITABLE;
2979
2980 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2981 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002982 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002983 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002984 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002985 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002986 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002987
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002988 atomic_long_add(user_extra, &user->locked_vm);
2989 event->mmap_locked = extra;
2990 event->mmap_user = get_current_user();
2991 vma->vm_mm->locked_vm += event->mmap_locked;
2992
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002993unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002994 if (!ret)
2995 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002996 mutex_unlock(&event->mmap_mutex);
2997
2998 vma->vm_flags |= VM_RESERVED;
2999 vma->vm_ops = &perf_mmap_vmops;
3000
3001 return ret;
3002}
3003
3004static int perf_fasync(int fd, struct file *filp, int on)
3005{
3006 struct inode *inode = filp->f_path.dentry->d_inode;
3007 struct perf_event *event = filp->private_data;
3008 int retval;
3009
3010 mutex_lock(&inode->i_mutex);
3011 retval = fasync_helper(fd, filp, on, &event->fasync);
3012 mutex_unlock(&inode->i_mutex);
3013
3014 if (retval < 0)
3015 return retval;
3016
3017 return 0;
3018}
3019
3020static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003021 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022 .release = perf_release,
3023 .read = perf_read,
3024 .poll = perf_poll,
3025 .unlocked_ioctl = perf_ioctl,
3026 .compat_ioctl = perf_ioctl,
3027 .mmap = perf_mmap,
3028 .fasync = perf_fasync,
3029};
3030
3031/*
3032 * Perf event wakeup
3033 *
3034 * If there's data, ensure we set the poll() state and publish everything
3035 * to user-space before waking everybody up.
3036 */
3037
3038void perf_event_wakeup(struct perf_event *event)
3039{
3040 wake_up_all(&event->waitq);
3041
3042 if (event->pending_kill) {
3043 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3044 event->pending_kill = 0;
3045 }
3046}
3047
3048/*
3049 * Pending wakeups
3050 *
3051 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3052 *
3053 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3054 * single linked list and use cmpxchg() to add entries lockless.
3055 */
3056
3057static void perf_pending_event(struct perf_pending_entry *entry)
3058{
3059 struct perf_event *event = container_of(entry,
3060 struct perf_event, pending);
3061
3062 if (event->pending_disable) {
3063 event->pending_disable = 0;
3064 __perf_event_disable(event);
3065 }
3066
3067 if (event->pending_wakeup) {
3068 event->pending_wakeup = 0;
3069 perf_event_wakeup(event);
3070 }
3071}
3072
3073#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3074
3075static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3076 PENDING_TAIL,
3077};
3078
3079static void perf_pending_queue(struct perf_pending_entry *entry,
3080 void (*func)(struct perf_pending_entry *))
3081{
3082 struct perf_pending_entry **head;
3083
3084 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3085 return;
3086
3087 entry->func = func;
3088
3089 head = &get_cpu_var(perf_pending_head);
3090
3091 do {
3092 entry->next = *head;
3093 } while (cmpxchg(head, entry->next, entry) != entry->next);
3094
3095 set_perf_event_pending();
3096
3097 put_cpu_var(perf_pending_head);
3098}
3099
3100static int __perf_pending_run(void)
3101{
3102 struct perf_pending_entry *list;
3103 int nr = 0;
3104
3105 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3106 while (list != PENDING_TAIL) {
3107 void (*func)(struct perf_pending_entry *);
3108 struct perf_pending_entry *entry = list;
3109
3110 list = list->next;
3111
3112 func = entry->func;
3113 entry->next = NULL;
3114 /*
3115 * Ensure we observe the unqueue before we issue the wakeup,
3116 * so that we won't be waiting forever.
3117 * -- see perf_not_pending().
3118 */
3119 smp_wmb();
3120
3121 func(entry);
3122 nr++;
3123 }
3124
3125 return nr;
3126}
3127
3128static inline int perf_not_pending(struct perf_event *event)
3129{
3130 /*
3131 * If we flush on whatever cpu we run, there is a chance we don't
3132 * need to wait.
3133 */
3134 get_cpu();
3135 __perf_pending_run();
3136 put_cpu();
3137
3138 /*
3139 * Ensure we see the proper queue state before going to sleep
3140 * so that we do not miss the wakeup. -- see perf_pending_handle()
3141 */
3142 smp_rmb();
3143 return event->pending.next == NULL;
3144}
3145
3146static void perf_pending_sync(struct perf_event *event)
3147{
3148 wait_event(event->waitq, perf_not_pending(event));
3149}
3150
3151void perf_event_do_pending(void)
3152{
3153 __perf_pending_run();
3154}
3155
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003157 * We assume there is only KVM supporting the callbacks.
3158 * Later on, we might change it to a list if there is
3159 * another virtualization implementation supporting the callbacks.
3160 */
3161struct perf_guest_info_callbacks *perf_guest_cbs;
3162
3163int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3164{
3165 perf_guest_cbs = cbs;
3166 return 0;
3167}
3168EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3169
3170int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3171{
3172 perf_guest_cbs = NULL;
3173 return 0;
3174}
3175EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3176
3177/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003178 * Output
3179 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003180static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003181 unsigned long offset, unsigned long head)
3182{
3183 unsigned long mask;
3184
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003185 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003186 return true;
3187
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003188 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189
3190 offset = (offset - tail) & mask;
3191 head = (head - tail) & mask;
3192
3193 if ((int)(head - offset) < 0)
3194 return false;
3195
3196 return true;
3197}
3198
3199static void perf_output_wakeup(struct perf_output_handle *handle)
3200{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003201 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202
3203 if (handle->nmi) {
3204 handle->event->pending_wakeup = 1;
3205 perf_pending_queue(&handle->event->pending,
3206 perf_pending_event);
3207 } else
3208 perf_event_wakeup(handle->event);
3209}
3210
3211/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003213 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214 * cannot fully serialize things.
3215 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003217 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003219static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003221 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003222
Peter Zijlstraef607772010-05-18 10:50:41 +02003223 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003224 local_inc(&buffer->nest);
3225 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003226}
3227
Peter Zijlstraef607772010-05-18 10:50:41 +02003228static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003229{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003230 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232
3233again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003234 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003235
3236 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003237 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003238 */
3239
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003240 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003241 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003242
3243 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003244 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003245 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003246 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003248 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003249
Peter Zijlstraef607772010-05-18 10:50:41 +02003250 /*
3251 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003252 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003253 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003254 if (unlikely(head != local_read(&buffer->head))) {
3255 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003256 goto again;
3257 }
3258
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003259 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003261
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003262out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003263 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003264}
3265
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003266__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003267 const void *buf, unsigned int len)
3268{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003269 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003270 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003271
3272 memcpy(handle->addr, buf, size);
3273
3274 len -= size;
3275 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003276 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003277 handle->size -= size;
3278 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003279 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003280
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003281 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003282 handle->page &= buffer->nr_pages - 1;
3283 handle->addr = buffer->data_pages[handle->page];
3284 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003285 }
3286 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003287}
3288
3289int perf_output_begin(struct perf_output_handle *handle,
3290 struct perf_event *event, unsigned int size,
3291 int nmi, int sample)
3292{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003293 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003294 unsigned long tail, offset, head;
3295 int have_lost;
3296 struct {
3297 struct perf_event_header header;
3298 u64 id;
3299 u64 lost;
3300 } lost_event;
3301
3302 rcu_read_lock();
3303 /*
3304 * For inherited events we send all the output towards the parent.
3305 */
3306 if (event->parent)
3307 event = event->parent;
3308
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003309 buffer = rcu_dereference(event->buffer);
3310 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003311 goto out;
3312
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003313 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003314 handle->event = event;
3315 handle->nmi = nmi;
3316 handle->sample = sample;
3317
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003318 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003319 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003320
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003321 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003322 if (have_lost)
3323 size += sizeof(lost_event);
3324
Peter Zijlstraef607772010-05-18 10:50:41 +02003325 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326
3327 do {
3328 /*
3329 * Userspace could choose to issue a mb() before updating the
3330 * tail pointer. So that all reads will be completed before the
3331 * write is issued.
3332 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003333 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003335 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003337 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003339 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003341 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3342 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003344 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3345 handle->page &= buffer->nr_pages - 1;
3346 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3347 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003348 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003350
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351 if (have_lost) {
3352 lost_event.header.type = PERF_RECORD_LOST;
3353 lost_event.header.misc = 0;
3354 lost_event.header.size = sizeof(lost_event);
3355 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003356 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357
3358 perf_output_put(handle, lost_event);
3359 }
3360
3361 return 0;
3362
3363fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003364 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003365 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366out:
3367 rcu_read_unlock();
3368
3369 return -ENOSPC;
3370}
3371
3372void perf_output_end(struct perf_output_handle *handle)
3373{
3374 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003375 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376
3377 int wakeup_events = event->attr.wakeup_events;
3378
3379 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003380 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003382 local_sub(wakeup_events, &buffer->events);
3383 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384 }
3385 }
3386
Peter Zijlstraef607772010-05-18 10:50:41 +02003387 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003388 rcu_read_unlock();
3389}
3390
3391static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3392{
3393 /*
3394 * only top level events have the pid namespace they were created in
3395 */
3396 if (event->parent)
3397 event = event->parent;
3398
3399 return task_tgid_nr_ns(p, event->ns);
3400}
3401
3402static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3403{
3404 /*
3405 * only top level events have the pid namespace they were created in
3406 */
3407 if (event->parent)
3408 event = event->parent;
3409
3410 return task_pid_nr_ns(p, event->ns);
3411}
3412
3413static void perf_output_read_one(struct perf_output_handle *handle,
3414 struct perf_event *event)
3415{
3416 u64 read_format = event->attr.read_format;
3417 u64 values[4];
3418 int n = 0;
3419
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003420 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003421 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3422 values[n++] = event->total_time_enabled +
3423 atomic64_read(&event->child_total_time_enabled);
3424 }
3425 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3426 values[n++] = event->total_time_running +
3427 atomic64_read(&event->child_total_time_running);
3428 }
3429 if (read_format & PERF_FORMAT_ID)
3430 values[n++] = primary_event_id(event);
3431
3432 perf_output_copy(handle, values, n * sizeof(u64));
3433}
3434
3435/*
3436 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3437 */
3438static void perf_output_read_group(struct perf_output_handle *handle,
3439 struct perf_event *event)
3440{
3441 struct perf_event *leader = event->group_leader, *sub;
3442 u64 read_format = event->attr.read_format;
3443 u64 values[5];
3444 int n = 0;
3445
3446 values[n++] = 1 + leader->nr_siblings;
3447
3448 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3449 values[n++] = leader->total_time_enabled;
3450
3451 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3452 values[n++] = leader->total_time_running;
3453
3454 if (leader != event)
3455 leader->pmu->read(leader);
3456
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003457 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458 if (read_format & PERF_FORMAT_ID)
3459 values[n++] = primary_event_id(leader);
3460
3461 perf_output_copy(handle, values, n * sizeof(u64));
3462
3463 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3464 n = 0;
3465
3466 if (sub != event)
3467 sub->pmu->read(sub);
3468
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003469 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003470 if (read_format & PERF_FORMAT_ID)
3471 values[n++] = primary_event_id(sub);
3472
3473 perf_output_copy(handle, values, n * sizeof(u64));
3474 }
3475}
3476
3477static void perf_output_read(struct perf_output_handle *handle,
3478 struct perf_event *event)
3479{
3480 if (event->attr.read_format & PERF_FORMAT_GROUP)
3481 perf_output_read_group(handle, event);
3482 else
3483 perf_output_read_one(handle, event);
3484}
3485
3486void perf_output_sample(struct perf_output_handle *handle,
3487 struct perf_event_header *header,
3488 struct perf_sample_data *data,
3489 struct perf_event *event)
3490{
3491 u64 sample_type = data->type;
3492
3493 perf_output_put(handle, *header);
3494
3495 if (sample_type & PERF_SAMPLE_IP)
3496 perf_output_put(handle, data->ip);
3497
3498 if (sample_type & PERF_SAMPLE_TID)
3499 perf_output_put(handle, data->tid_entry);
3500
3501 if (sample_type & PERF_SAMPLE_TIME)
3502 perf_output_put(handle, data->time);
3503
3504 if (sample_type & PERF_SAMPLE_ADDR)
3505 perf_output_put(handle, data->addr);
3506
3507 if (sample_type & PERF_SAMPLE_ID)
3508 perf_output_put(handle, data->id);
3509
3510 if (sample_type & PERF_SAMPLE_STREAM_ID)
3511 perf_output_put(handle, data->stream_id);
3512
3513 if (sample_type & PERF_SAMPLE_CPU)
3514 perf_output_put(handle, data->cpu_entry);
3515
3516 if (sample_type & PERF_SAMPLE_PERIOD)
3517 perf_output_put(handle, data->period);
3518
3519 if (sample_type & PERF_SAMPLE_READ)
3520 perf_output_read(handle, event);
3521
3522 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3523 if (data->callchain) {
3524 int size = 1;
3525
3526 if (data->callchain)
3527 size += data->callchain->nr;
3528
3529 size *= sizeof(u64);
3530
3531 perf_output_copy(handle, data->callchain, size);
3532 } else {
3533 u64 nr = 0;
3534 perf_output_put(handle, nr);
3535 }
3536 }
3537
3538 if (sample_type & PERF_SAMPLE_RAW) {
3539 if (data->raw) {
3540 perf_output_put(handle, data->raw->size);
3541 perf_output_copy(handle, data->raw->data,
3542 data->raw->size);
3543 } else {
3544 struct {
3545 u32 size;
3546 u32 data;
3547 } raw = {
3548 .size = sizeof(u32),
3549 .data = 0,
3550 };
3551 perf_output_put(handle, raw);
3552 }
3553 }
3554}
3555
3556void perf_prepare_sample(struct perf_event_header *header,
3557 struct perf_sample_data *data,
3558 struct perf_event *event,
3559 struct pt_regs *regs)
3560{
3561 u64 sample_type = event->attr.sample_type;
3562
3563 data->type = sample_type;
3564
3565 header->type = PERF_RECORD_SAMPLE;
3566 header->size = sizeof(*header);
3567
3568 header->misc = 0;
3569 header->misc |= perf_misc_flags(regs);
3570
3571 if (sample_type & PERF_SAMPLE_IP) {
3572 data->ip = perf_instruction_pointer(regs);
3573
3574 header->size += sizeof(data->ip);
3575 }
3576
3577 if (sample_type & PERF_SAMPLE_TID) {
3578 /* namespace issues */
3579 data->tid_entry.pid = perf_event_pid(event, current);
3580 data->tid_entry.tid = perf_event_tid(event, current);
3581
3582 header->size += sizeof(data->tid_entry);
3583 }
3584
3585 if (sample_type & PERF_SAMPLE_TIME) {
3586 data->time = perf_clock();
3587
3588 header->size += sizeof(data->time);
3589 }
3590
3591 if (sample_type & PERF_SAMPLE_ADDR)
3592 header->size += sizeof(data->addr);
3593
3594 if (sample_type & PERF_SAMPLE_ID) {
3595 data->id = primary_event_id(event);
3596
3597 header->size += sizeof(data->id);
3598 }
3599
3600 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3601 data->stream_id = event->id;
3602
3603 header->size += sizeof(data->stream_id);
3604 }
3605
3606 if (sample_type & PERF_SAMPLE_CPU) {
3607 data->cpu_entry.cpu = raw_smp_processor_id();
3608 data->cpu_entry.reserved = 0;
3609
3610 header->size += sizeof(data->cpu_entry);
3611 }
3612
3613 if (sample_type & PERF_SAMPLE_PERIOD)
3614 header->size += sizeof(data->period);
3615
3616 if (sample_type & PERF_SAMPLE_READ)
3617 header->size += perf_event_read_size(event);
3618
3619 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3620 int size = 1;
3621
3622 data->callchain = perf_callchain(regs);
3623
3624 if (data->callchain)
3625 size += data->callchain->nr;
3626
3627 header->size += size * sizeof(u64);
3628 }
3629
3630 if (sample_type & PERF_SAMPLE_RAW) {
3631 int size = sizeof(u32);
3632
3633 if (data->raw)
3634 size += data->raw->size;
3635 else
3636 size += sizeof(u32);
3637
3638 WARN_ON_ONCE(size & (sizeof(u64)-1));
3639 header->size += size;
3640 }
3641}
3642
3643static void perf_event_output(struct perf_event *event, int nmi,
3644 struct perf_sample_data *data,
3645 struct pt_regs *regs)
3646{
3647 struct perf_output_handle handle;
3648 struct perf_event_header header;
3649
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003650 /* protect the callchain buffers */
3651 rcu_read_lock();
3652
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003653 perf_prepare_sample(&header, data, event, regs);
3654
3655 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003656 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003657
3658 perf_output_sample(&handle, &header, data, event);
3659
3660 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003661
3662exit:
3663 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003664}
3665
3666/*
3667 * read event_id
3668 */
3669
3670struct perf_read_event {
3671 struct perf_event_header header;
3672
3673 u32 pid;
3674 u32 tid;
3675};
3676
3677static void
3678perf_event_read_event(struct perf_event *event,
3679 struct task_struct *task)
3680{
3681 struct perf_output_handle handle;
3682 struct perf_read_event read_event = {
3683 .header = {
3684 .type = PERF_RECORD_READ,
3685 .misc = 0,
3686 .size = sizeof(read_event) + perf_event_read_size(event),
3687 },
3688 .pid = perf_event_pid(event, task),
3689 .tid = perf_event_tid(event, task),
3690 };
3691 int ret;
3692
3693 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3694 if (ret)
3695 return;
3696
3697 perf_output_put(&handle, read_event);
3698 perf_output_read(&handle, event);
3699
3700 perf_output_end(&handle);
3701}
3702
3703/*
3704 * task tracking -- fork/exit
3705 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003706 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003707 */
3708
3709struct perf_task_event {
3710 struct task_struct *task;
3711 struct perf_event_context *task_ctx;
3712
3713 struct {
3714 struct perf_event_header header;
3715
3716 u32 pid;
3717 u32 ppid;
3718 u32 tid;
3719 u32 ptid;
3720 u64 time;
3721 } event_id;
3722};
3723
3724static void perf_event_task_output(struct perf_event *event,
3725 struct perf_task_event *task_event)
3726{
3727 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003728 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003729 int size, ret;
3730
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003731 size = task_event->event_id.header.size;
3732 ret = perf_output_begin(&handle, event, size, 0, 0);
3733
Peter Zijlstraef607772010-05-18 10:50:41 +02003734 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003735 return;
3736
3737 task_event->event_id.pid = perf_event_pid(event, task);
3738 task_event->event_id.ppid = perf_event_pid(event, current);
3739
3740 task_event->event_id.tid = perf_event_tid(event, task);
3741 task_event->event_id.ptid = perf_event_tid(event, current);
3742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003743 perf_output_put(&handle, task_event->event_id);
3744
3745 perf_output_end(&handle);
3746}
3747
3748static int perf_event_task_match(struct perf_event *event)
3749{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003750 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003751 return 0;
3752
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003753 if (event->cpu != -1 && event->cpu != smp_processor_id())
3754 return 0;
3755
Eric B Munson3af9e852010-05-18 15:30:49 +01003756 if (event->attr.comm || event->attr.mmap ||
3757 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003758 return 1;
3759
3760 return 0;
3761}
3762
3763static void perf_event_task_ctx(struct perf_event_context *ctx,
3764 struct perf_task_event *task_event)
3765{
3766 struct perf_event *event;
3767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003768 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3769 if (perf_event_task_match(event))
3770 perf_event_task_output(event, task_event);
3771 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003772}
3773
3774static void perf_event_task_event(struct perf_task_event *task_event)
3775{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003776 struct perf_event_context *ctx = task_event->task_ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003777 struct perf_cpu_context *cpuctx;
3778 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003779
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003780 rcu_read_lock_sched();
3781 list_for_each_entry_rcu(pmu, &pmus, entry) {
3782 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3783 perf_event_task_ctx(&cpuctx->ctx, task_event);
3784 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003786 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003787 if (ctx)
3788 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003789 rcu_read_unlock_sched();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003790}
3791
3792static void perf_event_task(struct task_struct *task,
3793 struct perf_event_context *task_ctx,
3794 int new)
3795{
3796 struct perf_task_event task_event;
3797
3798 if (!atomic_read(&nr_comm_events) &&
3799 !atomic_read(&nr_mmap_events) &&
3800 !atomic_read(&nr_task_events))
3801 return;
3802
3803 task_event = (struct perf_task_event){
3804 .task = task,
3805 .task_ctx = task_ctx,
3806 .event_id = {
3807 .header = {
3808 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3809 .misc = 0,
3810 .size = sizeof(task_event.event_id),
3811 },
3812 /* .pid */
3813 /* .ppid */
3814 /* .tid */
3815 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003816 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003817 },
3818 };
3819
3820 perf_event_task_event(&task_event);
3821}
3822
3823void perf_event_fork(struct task_struct *task)
3824{
3825 perf_event_task(task, NULL, 1);
3826}
3827
3828/*
3829 * comm tracking
3830 */
3831
3832struct perf_comm_event {
3833 struct task_struct *task;
3834 char *comm;
3835 int comm_size;
3836
3837 struct {
3838 struct perf_event_header header;
3839
3840 u32 pid;
3841 u32 tid;
3842 } event_id;
3843};
3844
3845static void perf_event_comm_output(struct perf_event *event,
3846 struct perf_comm_event *comm_event)
3847{
3848 struct perf_output_handle handle;
3849 int size = comm_event->event_id.header.size;
3850 int ret = perf_output_begin(&handle, event, size, 0, 0);
3851
3852 if (ret)
3853 return;
3854
3855 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3856 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3857
3858 perf_output_put(&handle, comm_event->event_id);
3859 perf_output_copy(&handle, comm_event->comm,
3860 comm_event->comm_size);
3861 perf_output_end(&handle);
3862}
3863
3864static int perf_event_comm_match(struct perf_event *event)
3865{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003866 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003867 return 0;
3868
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003869 if (event->cpu != -1 && event->cpu != smp_processor_id())
3870 return 0;
3871
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872 if (event->attr.comm)
3873 return 1;
3874
3875 return 0;
3876}
3877
3878static void perf_event_comm_ctx(struct perf_event_context *ctx,
3879 struct perf_comm_event *comm_event)
3880{
3881 struct perf_event *event;
3882
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003883 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3884 if (perf_event_comm_match(event))
3885 perf_event_comm_output(event, comm_event);
3886 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003887}
3888
3889static void perf_event_comm_event(struct perf_comm_event *comm_event)
3890{
3891 struct perf_cpu_context *cpuctx;
3892 struct perf_event_context *ctx;
3893 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003894 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003895 char comm[TASK_COMM_LEN];
3896
3897 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003898 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899 size = ALIGN(strlen(comm)+1, sizeof(u64));
3900
3901 comm_event->comm = comm;
3902 comm_event->comm_size = size;
3903
3904 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3905
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003906 rcu_read_lock_sched();
3907 list_for_each_entry_rcu(pmu, &pmus, entry) {
3908 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3909 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
3910 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003911 ctx = rcu_dereference(current->perf_event_ctxp);
3912 if (ctx)
3913 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003914 rcu_read_unlock_sched();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003915}
3916
3917void perf_event_comm(struct task_struct *task)
3918{
3919 struct perf_comm_event comm_event;
3920
3921 if (task->perf_event_ctxp)
3922 perf_event_enable_on_exec(task);
3923
3924 if (!atomic_read(&nr_comm_events))
3925 return;
3926
3927 comm_event = (struct perf_comm_event){
3928 .task = task,
3929 /* .comm */
3930 /* .comm_size */
3931 .event_id = {
3932 .header = {
3933 .type = PERF_RECORD_COMM,
3934 .misc = 0,
3935 /* .size */
3936 },
3937 /* .pid */
3938 /* .tid */
3939 },
3940 };
3941
3942 perf_event_comm_event(&comm_event);
3943}
3944
3945/*
3946 * mmap tracking
3947 */
3948
3949struct perf_mmap_event {
3950 struct vm_area_struct *vma;
3951
3952 const char *file_name;
3953 int file_size;
3954
3955 struct {
3956 struct perf_event_header header;
3957
3958 u32 pid;
3959 u32 tid;
3960 u64 start;
3961 u64 len;
3962 u64 pgoff;
3963 } event_id;
3964};
3965
3966static void perf_event_mmap_output(struct perf_event *event,
3967 struct perf_mmap_event *mmap_event)
3968{
3969 struct perf_output_handle handle;
3970 int size = mmap_event->event_id.header.size;
3971 int ret = perf_output_begin(&handle, event, size, 0, 0);
3972
3973 if (ret)
3974 return;
3975
3976 mmap_event->event_id.pid = perf_event_pid(event, current);
3977 mmap_event->event_id.tid = perf_event_tid(event, current);
3978
3979 perf_output_put(&handle, mmap_event->event_id);
3980 perf_output_copy(&handle, mmap_event->file_name,
3981 mmap_event->file_size);
3982 perf_output_end(&handle);
3983}
3984
3985static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003986 struct perf_mmap_event *mmap_event,
3987 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003988{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003989 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003990 return 0;
3991
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003992 if (event->cpu != -1 && event->cpu != smp_processor_id())
3993 return 0;
3994
Eric B Munson3af9e852010-05-18 15:30:49 +01003995 if ((!executable && event->attr.mmap_data) ||
3996 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003997 return 1;
3998
3999 return 0;
4000}
4001
4002static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004003 struct perf_mmap_event *mmap_event,
4004 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004005{
4006 struct perf_event *event;
4007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004008 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004009 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010 perf_event_mmap_output(event, mmap_event);
4011 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012}
4013
4014static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4015{
4016 struct perf_cpu_context *cpuctx;
4017 struct perf_event_context *ctx;
4018 struct vm_area_struct *vma = mmap_event->vma;
4019 struct file *file = vma->vm_file;
4020 unsigned int size;
4021 char tmp[16];
4022 char *buf = NULL;
4023 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004024 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004025
4026 memset(tmp, 0, sizeof(tmp));
4027
4028 if (file) {
4029 /*
4030 * d_path works from the end of the buffer backwards, so we
4031 * need to add enough zero bytes after the string to handle
4032 * the 64bit alignment we do later.
4033 */
4034 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4035 if (!buf) {
4036 name = strncpy(tmp, "//enomem", sizeof(tmp));
4037 goto got_name;
4038 }
4039 name = d_path(&file->f_path, buf, PATH_MAX);
4040 if (IS_ERR(name)) {
4041 name = strncpy(tmp, "//toolong", sizeof(tmp));
4042 goto got_name;
4043 }
4044 } else {
4045 if (arch_vma_name(mmap_event->vma)) {
4046 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4047 sizeof(tmp));
4048 goto got_name;
4049 }
4050
4051 if (!vma->vm_mm) {
4052 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4053 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004054 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4055 vma->vm_end >= vma->vm_mm->brk) {
4056 name = strncpy(tmp, "[heap]", sizeof(tmp));
4057 goto got_name;
4058 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4059 vma->vm_end >= vma->vm_mm->start_stack) {
4060 name = strncpy(tmp, "[stack]", sizeof(tmp));
4061 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004062 }
4063
4064 name = strncpy(tmp, "//anon", sizeof(tmp));
4065 goto got_name;
4066 }
4067
4068got_name:
4069 size = ALIGN(strlen(name)+1, sizeof(u64));
4070
4071 mmap_event->file_name = name;
4072 mmap_event->file_size = size;
4073
4074 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4075
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004076 rcu_read_lock_sched();
4077 list_for_each_entry_rcu(pmu, &pmus, entry) {
4078 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
4079 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4080 vma->vm_flags & VM_EXEC);
4081 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082 ctx = rcu_dereference(current->perf_event_ctxp);
4083 if (ctx)
Eric B Munson3af9e852010-05-18 15:30:49 +01004084 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004085 rcu_read_unlock_sched();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004086
4087 kfree(buf);
4088}
4089
Eric B Munson3af9e852010-05-18 15:30:49 +01004090void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004091{
4092 struct perf_mmap_event mmap_event;
4093
4094 if (!atomic_read(&nr_mmap_events))
4095 return;
4096
4097 mmap_event = (struct perf_mmap_event){
4098 .vma = vma,
4099 /* .file_name */
4100 /* .file_size */
4101 .event_id = {
4102 .header = {
4103 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004104 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004105 /* .size */
4106 },
4107 /* .pid */
4108 /* .tid */
4109 .start = vma->vm_start,
4110 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004111 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112 },
4113 };
4114
4115 perf_event_mmap_event(&mmap_event);
4116}
4117
4118/*
4119 * IRQ throttle logging
4120 */
4121
4122static void perf_log_throttle(struct perf_event *event, int enable)
4123{
4124 struct perf_output_handle handle;
4125 int ret;
4126
4127 struct {
4128 struct perf_event_header header;
4129 u64 time;
4130 u64 id;
4131 u64 stream_id;
4132 } throttle_event = {
4133 .header = {
4134 .type = PERF_RECORD_THROTTLE,
4135 .misc = 0,
4136 .size = sizeof(throttle_event),
4137 },
4138 .time = perf_clock(),
4139 .id = primary_event_id(event),
4140 .stream_id = event->id,
4141 };
4142
4143 if (enable)
4144 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4145
4146 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4147 if (ret)
4148 return;
4149
4150 perf_output_put(&handle, throttle_event);
4151 perf_output_end(&handle);
4152}
4153
4154/*
4155 * Generic event overflow handling, sampling.
4156 */
4157
4158static int __perf_event_overflow(struct perf_event *event, int nmi,
4159 int throttle, struct perf_sample_data *data,
4160 struct pt_regs *regs)
4161{
4162 int events = atomic_read(&event->event_limit);
4163 struct hw_perf_event *hwc = &event->hw;
4164 int ret = 0;
4165
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004166 if (!throttle) {
4167 hwc->interrupts++;
4168 } else {
4169 if (hwc->interrupts != MAX_INTERRUPTS) {
4170 hwc->interrupts++;
4171 if (HZ * hwc->interrupts >
4172 (u64)sysctl_perf_event_sample_rate) {
4173 hwc->interrupts = MAX_INTERRUPTS;
4174 perf_log_throttle(event, 0);
4175 ret = 1;
4176 }
4177 } else {
4178 /*
4179 * Keep re-disabling events even though on the previous
4180 * pass we disabled it - just in case we raced with a
4181 * sched-in and the event got enabled again:
4182 */
4183 ret = 1;
4184 }
4185 }
4186
4187 if (event->attr.freq) {
4188 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004189 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190
Peter Zijlstraabd50712010-01-26 18:50:16 +01004191 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192
Peter Zijlstraabd50712010-01-26 18:50:16 +01004193 if (delta > 0 && delta < 2*TICK_NSEC)
4194 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195 }
4196
4197 /*
4198 * XXX event_limit might not quite work as expected on inherited
4199 * events
4200 */
4201
4202 event->pending_kill = POLL_IN;
4203 if (events && atomic_dec_and_test(&event->event_limit)) {
4204 ret = 1;
4205 event->pending_kill = POLL_HUP;
4206 if (nmi) {
4207 event->pending_disable = 1;
4208 perf_pending_queue(&event->pending,
4209 perf_pending_event);
4210 } else
4211 perf_event_disable(event);
4212 }
4213
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004214 if (event->overflow_handler)
4215 event->overflow_handler(event, nmi, data, regs);
4216 else
4217 perf_event_output(event, nmi, data, regs);
4218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219 return ret;
4220}
4221
4222int perf_event_overflow(struct perf_event *event, int nmi,
4223 struct perf_sample_data *data,
4224 struct pt_regs *regs)
4225{
4226 return __perf_event_overflow(event, nmi, 1, data, regs);
4227}
4228
4229/*
4230 * Generic software event infrastructure
4231 */
4232
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004233struct swevent_htable {
4234 struct swevent_hlist *swevent_hlist;
4235 struct mutex hlist_mutex;
4236 int hlist_refcount;
4237
4238 /* Recursion avoidance in each contexts */
4239 int recursion[PERF_NR_CONTEXTS];
4240};
4241
4242static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004244/*
4245 * We directly increment event->count and keep a second value in
4246 * event->hw.period_left to count intervals. This period event
4247 * is kept in the range [-sample_period, 0] so that we can use the
4248 * sign as trigger.
4249 */
4250
4251static u64 perf_swevent_set_period(struct perf_event *event)
4252{
4253 struct hw_perf_event *hwc = &event->hw;
4254 u64 period = hwc->last_period;
4255 u64 nr, offset;
4256 s64 old, val;
4257
4258 hwc->last_period = hwc->sample_period;
4259
4260again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004261 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262 if (val < 0)
4263 return 0;
4264
4265 nr = div64_u64(period + val, period);
4266 offset = nr * period;
4267 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004268 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 goto again;
4270
4271 return nr;
4272}
4273
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004274static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275 int nmi, struct perf_sample_data *data,
4276 struct pt_regs *regs)
4277{
4278 struct hw_perf_event *hwc = &event->hw;
4279 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280
4281 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004282 if (!overflow)
4283 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004284
4285 if (hwc->interrupts == MAX_INTERRUPTS)
4286 return;
4287
4288 for (; overflow; overflow--) {
4289 if (__perf_event_overflow(event, nmi, throttle,
4290 data, regs)) {
4291 /*
4292 * We inhibit the overflow from happening when
4293 * hwc->interrupts == MAX_INTERRUPTS.
4294 */
4295 break;
4296 }
4297 throttle = 1;
4298 }
4299}
4300
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004301static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302 int nmi, struct perf_sample_data *data,
4303 struct pt_regs *regs)
4304{
4305 struct hw_perf_event *hwc = &event->hw;
4306
Peter Zijlstrae7850592010-05-21 14:43:08 +02004307 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309 if (!regs)
4310 return;
4311
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004312 if (!hwc->sample_period)
4313 return;
4314
4315 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4316 return perf_swevent_overflow(event, 1, nmi, data, regs);
4317
Peter Zijlstrae7850592010-05-21 14:43:08 +02004318 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004319 return;
4320
4321 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004322}
4323
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004324static int perf_exclude_event(struct perf_event *event,
4325 struct pt_regs *regs)
4326{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004327 if (event->hw.state & PERF_HES_STOPPED)
4328 return 0;
4329
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004330 if (regs) {
4331 if (event->attr.exclude_user && user_mode(regs))
4332 return 1;
4333
4334 if (event->attr.exclude_kernel && !user_mode(regs))
4335 return 1;
4336 }
4337
4338 return 0;
4339}
4340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341static int perf_swevent_match(struct perf_event *event,
4342 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004343 u32 event_id,
4344 struct perf_sample_data *data,
4345 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004347 if (event->attr.type != type)
4348 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350 if (event->attr.config != event_id)
4351 return 0;
4352
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004353 if (perf_exclude_event(event, regs))
4354 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004355
4356 return 1;
4357}
4358
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004359static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004361 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004362
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004363 return hash_64(val, SWEVENT_HLIST_BITS);
4364}
4365
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004366static inline struct hlist_head *
4367__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004368{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004369 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004370
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004371 return &hlist->heads[hash];
4372}
4373
4374/* For the read side: events when they trigger */
4375static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004376find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004377{
4378 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004379
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004380 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004381 if (!hlist)
4382 return NULL;
4383
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004384 return __find_swevent_head(hlist, type, event_id);
4385}
4386
4387/* For the event head insertion and removal in the hlist */
4388static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004389find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004390{
4391 struct swevent_hlist *hlist;
4392 u32 event_id = event->attr.config;
4393 u64 type = event->attr.type;
4394
4395 /*
4396 * Event scheduling is always serialized against hlist allocation
4397 * and release. Which makes the protected version suitable here.
4398 * The context lock guarantees that.
4399 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004400 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004401 lockdep_is_held(&event->ctx->lock));
4402 if (!hlist)
4403 return NULL;
4404
4405 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004406}
4407
4408static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4409 u64 nr, int nmi,
4410 struct perf_sample_data *data,
4411 struct pt_regs *regs)
4412{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004413 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004414 struct perf_event *event;
4415 struct hlist_node *node;
4416 struct hlist_head *head;
4417
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004418 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004419 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004420 if (!head)
4421 goto end;
4422
4423 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004424 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004425 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004427end:
4428 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429}
4430
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004431int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004433 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004434
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004435 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004437EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004438
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004439void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004441 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004442
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004443 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004444}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4447 struct pt_regs *regs, u64 addr)
4448{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004449 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004450 int rctx;
4451
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004452 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004453 rctx = perf_swevent_get_recursion_context();
4454 if (rctx < 0)
4455 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004456
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004457 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004458
4459 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004460
4461 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004462 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463}
4464
4465static void perf_swevent_read(struct perf_event *event)
4466{
4467}
4468
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004469static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004470{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004471 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004472 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004473 struct hlist_head *head;
4474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475 if (hwc->sample_period) {
4476 hwc->last_period = hwc->sample_period;
4477 perf_swevent_set_period(event);
4478 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004479
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004480 hwc->state = !(flags & PERF_EF_START);
4481
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004482 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004483 if (WARN_ON_ONCE(!head))
4484 return -EINVAL;
4485
4486 hlist_add_head_rcu(&event->hlist_entry, head);
4487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488 return 0;
4489}
4490
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004491static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004492{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004493 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494}
4495
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004496static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004497{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004498 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004499}
4500
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004501static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004502{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004503 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004504}
4505
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004506/* Deref the hlist from the update side */
4507static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004508swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004509{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004510 return rcu_dereference_protected(swhash->swevent_hlist,
4511 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004512}
4513
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004514static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4515{
4516 struct swevent_hlist *hlist;
4517
4518 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4519 kfree(hlist);
4520}
4521
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004522static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004523{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004524 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004525
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004526 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004527 return;
4528
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004529 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004530 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4531}
4532
4533static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4534{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004535 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004536
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004537 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004538
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004539 if (!--swhash->hlist_refcount)
4540 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004541
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004542 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004543}
4544
4545static void swevent_hlist_put(struct perf_event *event)
4546{
4547 int cpu;
4548
4549 if (event->cpu != -1) {
4550 swevent_hlist_put_cpu(event, event->cpu);
4551 return;
4552 }
4553
4554 for_each_possible_cpu(cpu)
4555 swevent_hlist_put_cpu(event, cpu);
4556}
4557
4558static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4559{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004560 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004561 int err = 0;
4562
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004563 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004564
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004565 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004566 struct swevent_hlist *hlist;
4567
4568 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4569 if (!hlist) {
4570 err = -ENOMEM;
4571 goto exit;
4572 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004573 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004574 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004575 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004576exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004577 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004578
4579 return err;
4580}
4581
4582static int swevent_hlist_get(struct perf_event *event)
4583{
4584 int err;
4585 int cpu, failed_cpu;
4586
4587 if (event->cpu != -1)
4588 return swevent_hlist_get_cpu(event, event->cpu);
4589
4590 get_online_cpus();
4591 for_each_possible_cpu(cpu) {
4592 err = swevent_hlist_get_cpu(event, cpu);
4593 if (err) {
4594 failed_cpu = cpu;
4595 goto fail;
4596 }
4597 }
4598 put_online_cpus();
4599
4600 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004601fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004602 for_each_possible_cpu(cpu) {
4603 if (cpu == failed_cpu)
4604 break;
4605 swevent_hlist_put_cpu(event, cpu);
4606 }
4607
4608 put_online_cpus();
4609 return err;
4610}
4611
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004612atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004613
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004614static void sw_perf_event_destroy(struct perf_event *event)
4615{
4616 u64 event_id = event->attr.config;
4617
4618 WARN_ON(event->parent);
4619
4620 atomic_dec(&perf_swevent_enabled[event_id]);
4621 swevent_hlist_put(event);
4622}
4623
4624static int perf_swevent_init(struct perf_event *event)
4625{
4626 int event_id = event->attr.config;
4627
4628 if (event->attr.type != PERF_TYPE_SOFTWARE)
4629 return -ENOENT;
4630
4631 switch (event_id) {
4632 case PERF_COUNT_SW_CPU_CLOCK:
4633 case PERF_COUNT_SW_TASK_CLOCK:
4634 return -ENOENT;
4635
4636 default:
4637 break;
4638 }
4639
4640 if (event_id > PERF_COUNT_SW_MAX)
4641 return -ENOENT;
4642
4643 if (!event->parent) {
4644 int err;
4645
4646 err = swevent_hlist_get(event);
4647 if (err)
4648 return err;
4649
4650 atomic_inc(&perf_swevent_enabled[event_id]);
4651 event->destroy = sw_perf_event_destroy;
4652 }
4653
4654 return 0;
4655}
4656
4657static struct pmu perf_swevent = {
4658 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004659 .add = perf_swevent_add,
4660 .del = perf_swevent_del,
4661 .start = perf_swevent_start,
4662 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004663 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004664};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004665
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004666#ifdef CONFIG_EVENT_TRACING
4667
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004668static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004669 struct perf_sample_data *data)
4670{
4671 void *record = data->raw->data;
4672
4673 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4674 return 1;
4675 return 0;
4676}
4677
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004678static int perf_tp_event_match(struct perf_event *event,
4679 struct perf_sample_data *data,
4680 struct pt_regs *regs)
4681{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004682 /*
4683 * All tracepoints are from kernel-space.
4684 */
4685 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004686 return 0;
4687
4688 if (!perf_tp_filter_match(event, data))
4689 return 0;
4690
4691 return 1;
4692}
4693
4694void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004695 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004696{
4697 struct perf_sample_data data;
4698 struct perf_event *event;
4699 struct hlist_node *node;
4700
4701 struct perf_raw_record raw = {
4702 .size = entry_size,
4703 .data = record,
4704 };
4705
4706 perf_sample_data_init(&data, addr);
4707 data.raw = &raw;
4708
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004709 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4710 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004711 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004712 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004713
4714 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004715}
4716EXPORT_SYMBOL_GPL(perf_tp_event);
4717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004718static void tp_perf_event_destroy(struct perf_event *event)
4719{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004720 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004721}
4722
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004723static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004724{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004725 int err;
4726
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004727 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4728 return -ENOENT;
4729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730 /*
4731 * Raw tracepoint data is a severe data leak, only allow root to
4732 * have these.
4733 */
4734 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4735 perf_paranoid_tracepoint_raw() &&
4736 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004737 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004738
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004739 err = perf_trace_init(event);
4740 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004741 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004742
4743 event->destroy = tp_perf_event_destroy;
4744
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004745 return 0;
4746}
4747
4748static struct pmu perf_tracepoint = {
4749 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004750 .add = perf_trace_add,
4751 .del = perf_trace_del,
4752 .start = perf_swevent_start,
4753 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004754 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004755};
4756
4757static inline void perf_tp_register(void)
4758{
4759 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760}
Li Zefan6fb29152009-10-15 11:21:42 +08004761
4762static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4763{
4764 char *filter_str;
4765 int ret;
4766
4767 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4768 return -EINVAL;
4769
4770 filter_str = strndup_user(arg, PAGE_SIZE);
4771 if (IS_ERR(filter_str))
4772 return PTR_ERR(filter_str);
4773
4774 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4775
4776 kfree(filter_str);
4777 return ret;
4778}
4779
4780static void perf_event_free_filter(struct perf_event *event)
4781{
4782 ftrace_profile_free_filter(event);
4783}
4784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785#else
Li Zefan6fb29152009-10-15 11:21:42 +08004786
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004787static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004788{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004789}
Li Zefan6fb29152009-10-15 11:21:42 +08004790
4791static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4792{
4793 return -ENOENT;
4794}
4795
4796static void perf_event_free_filter(struct perf_event *event)
4797{
4798}
4799
Li Zefan07b139c2009-12-21 14:27:35 +08004800#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004801
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004802#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004803void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004804{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004805 struct perf_sample_data sample;
4806 struct pt_regs *regs = data;
4807
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004808 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004809
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004810 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4811 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004812}
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004813#endif
4814
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004815/*
4816 * hrtimer based swevent callback
4817 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004818
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004819static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004820{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004821 enum hrtimer_restart ret = HRTIMER_RESTART;
4822 struct perf_sample_data data;
4823 struct pt_regs *regs;
4824 struct perf_event *event;
4825 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004826
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004827 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4828 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004829
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004830 perf_sample_data_init(&data, 0);
4831 data.period = event->hw.last_period;
4832 regs = get_irq_regs();
4833
4834 if (regs && !perf_exclude_event(event, regs)) {
4835 if (!(event->attr.exclude_idle && current->pid == 0))
4836 if (perf_event_overflow(event, 0, &data, regs))
4837 ret = HRTIMER_NORESTART;
4838 }
4839
4840 period = max_t(u64, 10000, event->hw.sample_period);
4841 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4842
4843 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004844}
4845
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004846static void perf_swevent_start_hrtimer(struct perf_event *event)
4847{
4848 struct hw_perf_event *hwc = &event->hw;
4849
4850 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4851 hwc->hrtimer.function = perf_swevent_hrtimer;
4852 if (hwc->sample_period) {
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004853 s64 period = local64_read(&hwc->period_left);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004854
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004855 if (period) {
4856 if (period < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004857 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004858
4859 local64_set(&hwc->period_left, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004860 } else {
4861 period = max_t(u64, 10000, hwc->sample_period);
4862 }
4863 __hrtimer_start_range_ns(&hwc->hrtimer,
4864 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02004865 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004866 }
4867}
4868
4869static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4870{
4871 struct hw_perf_event *hwc = &event->hw;
4872
4873 if (hwc->sample_period) {
4874 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004875 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004876
4877 hrtimer_cancel(&hwc->hrtimer);
4878 }
4879}
4880
4881/*
4882 * Software event: cpu wall time clock
4883 */
4884
4885static void cpu_clock_event_update(struct perf_event *event)
4886{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004887 s64 prev;
4888 u64 now;
4889
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004890 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004891 prev = local64_xchg(&event->hw.prev_count, now);
4892 local64_add(now - prev, &event->count);
4893}
4894
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004895static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004896{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004897 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004898 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004899}
4900
4901static void cpu_clock_event_stop(struct perf_event *event, int flags)
4902{
4903 perf_swevent_cancel_hrtimer(event);
4904 cpu_clock_event_update(event);
4905}
4906
4907static int cpu_clock_event_add(struct perf_event *event, int flags)
4908{
4909 if (flags & PERF_EF_START)
4910 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004911
4912 return 0;
4913}
4914
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004915static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004916{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004917 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004918}
4919
4920static void cpu_clock_event_read(struct perf_event *event)
4921{
4922 cpu_clock_event_update(event);
4923}
4924
4925static int cpu_clock_event_init(struct perf_event *event)
4926{
4927 if (event->attr.type != PERF_TYPE_SOFTWARE)
4928 return -ENOENT;
4929
4930 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4931 return -ENOENT;
4932
4933 return 0;
4934}
4935
4936static struct pmu perf_cpu_clock = {
4937 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004938 .add = cpu_clock_event_add,
4939 .del = cpu_clock_event_del,
4940 .start = cpu_clock_event_start,
4941 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004942 .read = cpu_clock_event_read,
4943};
4944
4945/*
4946 * Software event: task time clock
4947 */
4948
4949static void task_clock_event_update(struct perf_event *event, u64 now)
4950{
4951 u64 prev;
4952 s64 delta;
4953
4954 prev = local64_xchg(&event->hw.prev_count, now);
4955 delta = now - prev;
4956 local64_add(delta, &event->count);
4957}
4958
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004959static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004960{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004961 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004962 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004963}
4964
4965static void task_clock_event_stop(struct perf_event *event, int flags)
4966{
4967 perf_swevent_cancel_hrtimer(event);
4968 task_clock_event_update(event, event->ctx->time);
4969}
4970
4971static int task_clock_event_add(struct perf_event *event, int flags)
4972{
4973 if (flags & PERF_EF_START)
4974 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004975
4976 return 0;
4977}
4978
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004979static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004980{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004981 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004982}
4983
4984static void task_clock_event_read(struct perf_event *event)
4985{
4986 u64 time;
4987
4988 if (!in_nmi()) {
4989 update_context_time(event->ctx);
4990 time = event->ctx->time;
4991 } else {
4992 u64 now = perf_clock();
4993 u64 delta = now - event->ctx->timestamp;
4994 time = event->ctx->time + delta;
4995 }
4996
4997 task_clock_event_update(event, time);
4998}
4999
5000static int task_clock_event_init(struct perf_event *event)
5001{
5002 if (event->attr.type != PERF_TYPE_SOFTWARE)
5003 return -ENOENT;
5004
5005 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5006 return -ENOENT;
5007
5008 return 0;
5009}
5010
5011static struct pmu perf_task_clock = {
5012 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005013 .add = task_clock_event_add,
5014 .del = task_clock_event_del,
5015 .start = task_clock_event_start,
5016 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005017 .read = task_clock_event_read,
5018};
5019
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005020static void perf_pmu_nop_void(struct pmu *pmu)
5021{
5022}
5023
5024static int perf_pmu_nop_int(struct pmu *pmu)
5025{
5026 return 0;
5027}
5028
5029static void perf_pmu_start_txn(struct pmu *pmu)
5030{
5031 perf_pmu_disable(pmu);
5032}
5033
5034static int perf_pmu_commit_txn(struct pmu *pmu)
5035{
5036 perf_pmu_enable(pmu);
5037 return 0;
5038}
5039
5040static void perf_pmu_cancel_txn(struct pmu *pmu)
5041{
5042 perf_pmu_enable(pmu);
5043}
5044
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005045int perf_pmu_register(struct pmu *pmu)
5046{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005047 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005048
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005049 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005050 ret = -ENOMEM;
5051 pmu->pmu_disable_count = alloc_percpu(int);
5052 if (!pmu->pmu_disable_count)
5053 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005054
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005055 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5056 if (!pmu->pmu_cpu_context)
5057 goto free_pdc;
5058
5059 for_each_possible_cpu(cpu) {
5060 struct perf_cpu_context *cpuctx;
5061
5062 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005063 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005064 cpuctx->ctx.pmu = pmu;
5065 cpuctx->timer_interval = TICK_NSEC;
5066 hrtimer_init(&cpuctx->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5067 cpuctx->timer.function = perf_event_context_tick;
5068 }
5069
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005070 if (!pmu->start_txn) {
5071 if (pmu->pmu_enable) {
5072 /*
5073 * If we have pmu_enable/pmu_disable calls, install
5074 * transaction stubs that use that to try and batch
5075 * hardware accesses.
5076 */
5077 pmu->start_txn = perf_pmu_start_txn;
5078 pmu->commit_txn = perf_pmu_commit_txn;
5079 pmu->cancel_txn = perf_pmu_cancel_txn;
5080 } else {
5081 pmu->start_txn = perf_pmu_nop_void;
5082 pmu->commit_txn = perf_pmu_nop_int;
5083 pmu->cancel_txn = perf_pmu_nop_void;
5084 }
5085 }
5086
5087 if (!pmu->pmu_enable) {
5088 pmu->pmu_enable = perf_pmu_nop_void;
5089 pmu->pmu_disable = perf_pmu_nop_void;
5090 }
5091
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005092 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005093 ret = 0;
5094unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005095 mutex_unlock(&pmus_lock);
5096
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005097 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005098
5099free_pdc:
5100 free_percpu(pmu->pmu_disable_count);
5101 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005102}
5103
5104void perf_pmu_unregister(struct pmu *pmu)
5105{
5106 mutex_lock(&pmus_lock);
5107 list_del_rcu(&pmu->entry);
5108 mutex_unlock(&pmus_lock);
5109
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005110 /*
5111 * We use the pmu list either under SRCU or preempt_disable,
5112 * synchronize_srcu() implies synchronize_sched() so we're good.
5113 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005114 synchronize_srcu(&pmus_srcu);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005115
5116 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005117 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005118}
5119
5120struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005121{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005122 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005123 int idx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005124
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005125 idx = srcu_read_lock(&pmus_srcu);
5126 list_for_each_entry_rcu(pmu, &pmus, entry) {
5127 int ret = pmu->event_init(event);
5128 if (!ret)
5129 break;
5130 if (ret != -ENOENT) {
5131 pmu = ERR_PTR(ret);
5132 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005133 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005135 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005136
5137 return pmu;
5138}
5139
5140/*
5141 * Allocate and initialize a event structure
5142 */
5143static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005144perf_event_alloc(struct perf_event_attr *attr, int cpu,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005145 struct perf_event *group_leader,
5146 struct perf_event *parent_event,
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005147 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005148{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005149 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005150 struct perf_event *event;
5151 struct hw_perf_event *hwc;
5152 long err;
5153
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005154 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005155 if (!event)
5156 return ERR_PTR(-ENOMEM);
5157
5158 /*
5159 * Single events are their own group leaders, with an
5160 * empty sibling list:
5161 */
5162 if (!group_leader)
5163 group_leader = event;
5164
5165 mutex_init(&event->child_mutex);
5166 INIT_LIST_HEAD(&event->child_list);
5167
5168 INIT_LIST_HEAD(&event->group_entry);
5169 INIT_LIST_HEAD(&event->event_entry);
5170 INIT_LIST_HEAD(&event->sibling_list);
5171 init_waitqueue_head(&event->waitq);
5172
5173 mutex_init(&event->mmap_mutex);
5174
5175 event->cpu = cpu;
5176 event->attr = *attr;
5177 event->group_leader = group_leader;
5178 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005179 event->oncpu = -1;
5180
5181 event->parent = parent_event;
5182
5183 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5184 event->id = atomic64_inc_return(&perf_event_id);
5185
5186 event->state = PERF_EVENT_STATE_INACTIVE;
5187
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005188 if (!overflow_handler && parent_event)
5189 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005190
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005191 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005193 if (attr->disabled)
5194 event->state = PERF_EVENT_STATE_OFF;
5195
5196 pmu = NULL;
5197
5198 hwc = &event->hw;
5199 hwc->sample_period = attr->sample_period;
5200 if (attr->freq && attr->sample_freq)
5201 hwc->sample_period = 1;
5202 hwc->last_period = hwc->sample_period;
5203
Peter Zijlstrae7850592010-05-21 14:43:08 +02005204 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205
5206 /*
5207 * we currently do not support PERF_FORMAT_GROUP on inherited events
5208 */
5209 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5210 goto done;
5211
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005212 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214done:
5215 err = 0;
5216 if (!pmu)
5217 err = -EINVAL;
5218 else if (IS_ERR(pmu))
5219 err = PTR_ERR(pmu);
5220
5221 if (err) {
5222 if (event->ns)
5223 put_pid_ns(event->ns);
5224 kfree(event);
5225 return ERR_PTR(err);
5226 }
5227
5228 event->pmu = pmu;
5229
5230 if (!event->parent) {
5231 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005232 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233 atomic_inc(&nr_mmap_events);
5234 if (event->attr.comm)
5235 atomic_inc(&nr_comm_events);
5236 if (event->attr.task)
5237 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005238 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5239 err = get_callchain_buffers();
5240 if (err) {
5241 free_event(event);
5242 return ERR_PTR(err);
5243 }
5244 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 }
5246
5247 return event;
5248}
5249
5250static int perf_copy_attr(struct perf_event_attr __user *uattr,
5251 struct perf_event_attr *attr)
5252{
5253 u32 size;
5254 int ret;
5255
5256 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5257 return -EFAULT;
5258
5259 /*
5260 * zero the full structure, so that a short copy will be nice.
5261 */
5262 memset(attr, 0, sizeof(*attr));
5263
5264 ret = get_user(size, &uattr->size);
5265 if (ret)
5266 return ret;
5267
5268 if (size > PAGE_SIZE) /* silly large */
5269 goto err_size;
5270
5271 if (!size) /* abi compat */
5272 size = PERF_ATTR_SIZE_VER0;
5273
5274 if (size < PERF_ATTR_SIZE_VER0)
5275 goto err_size;
5276
5277 /*
5278 * If we're handed a bigger struct than we know of,
5279 * ensure all the unknown bits are 0 - i.e. new
5280 * user-space does not rely on any kernel feature
5281 * extensions we dont know about yet.
5282 */
5283 if (size > sizeof(*attr)) {
5284 unsigned char __user *addr;
5285 unsigned char __user *end;
5286 unsigned char val;
5287
5288 addr = (void __user *)uattr + sizeof(*attr);
5289 end = (void __user *)uattr + size;
5290
5291 for (; addr < end; addr++) {
5292 ret = get_user(val, addr);
5293 if (ret)
5294 return ret;
5295 if (val)
5296 goto err_size;
5297 }
5298 size = sizeof(*attr);
5299 }
5300
5301 ret = copy_from_user(attr, uattr, size);
5302 if (ret)
5303 return -EFAULT;
5304
5305 /*
5306 * If the type exists, the corresponding creation will verify
5307 * the attr->config.
5308 */
5309 if (attr->type >= PERF_TYPE_MAX)
5310 return -EINVAL;
5311
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305312 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005313 return -EINVAL;
5314
5315 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5316 return -EINVAL;
5317
5318 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5319 return -EINVAL;
5320
5321out:
5322 return ret;
5323
5324err_size:
5325 put_user(sizeof(*attr), &uattr->size);
5326 ret = -E2BIG;
5327 goto out;
5328}
5329
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005330static int
5331perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005332{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005333 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005334 int ret = -EINVAL;
5335
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005336 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005337 goto set;
5338
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005339 /* don't allow circular references */
5340 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341 goto out;
5342
Peter Zijlstra0f139302010-05-20 14:35:15 +02005343 /*
5344 * Don't allow cross-cpu buffers
5345 */
5346 if (output_event->cpu != event->cpu)
5347 goto out;
5348
5349 /*
5350 * If its not a per-cpu buffer, it must be the same task.
5351 */
5352 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5353 goto out;
5354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005355set:
5356 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005357 /* Can't redirect output if we've got an active mmap() */
5358 if (atomic_read(&event->mmap_count))
5359 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005360
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005361 if (output_event) {
5362 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005363 buffer = perf_buffer_get(output_event);
5364 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005365 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005366 }
5367
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005368 old_buffer = event->buffer;
5369 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005370 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005371unlock:
5372 mutex_unlock(&event->mmap_mutex);
5373
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005374 if (old_buffer)
5375 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005376out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377 return ret;
5378}
5379
5380/**
5381 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5382 *
5383 * @attr_uptr: event_id type attributes for monitoring/sampling
5384 * @pid: target pid
5385 * @cpu: target cpu
5386 * @group_fd: group leader event fd
5387 */
5388SYSCALL_DEFINE5(perf_event_open,
5389 struct perf_event_attr __user *, attr_uptr,
5390 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5391{
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005392 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005393 struct perf_event_attr attr;
5394 struct perf_event_context *ctx;
5395 struct file *event_file = NULL;
5396 struct file *group_file = NULL;
Al Viroea635c62010-05-26 17:40:29 -04005397 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005398 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005399 int err;
5400
5401 /* for future expandability... */
5402 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5403 return -EINVAL;
5404
5405 err = perf_copy_attr(attr_uptr, &attr);
5406 if (err)
5407 return err;
5408
5409 if (!attr.exclude_kernel) {
5410 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5411 return -EACCES;
5412 }
5413
5414 if (attr.freq) {
5415 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5416 return -EINVAL;
5417 }
5418
Al Viroea635c62010-05-26 17:40:29 -04005419 event_fd = get_unused_fd_flags(O_RDWR);
5420 if (event_fd < 0)
5421 return event_fd;
5422
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005423 event = perf_event_alloc(&attr, cpu, group_leader, NULL, NULL);
5424 if (IS_ERR(event)) {
5425 err = PTR_ERR(event);
5426 goto err_fd;
5427 }
5428
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005429 /*
5430 * Get the target context (task or percpu):
5431 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005432 ctx = find_get_context(event->pmu, pid, cpu);
Al Viroea635c62010-05-26 17:40:29 -04005433 if (IS_ERR(ctx)) {
5434 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005435 goto err_alloc;
Al Viroea635c62010-05-26 17:40:29 -04005436 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005437
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005438 if (group_fd != -1) {
5439 group_leader = perf_fget_light(group_fd, &fput_needed);
5440 if (IS_ERR(group_leader)) {
5441 err = PTR_ERR(group_leader);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005442 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005443 }
5444 group_file = group_leader->filp;
5445 if (flags & PERF_FLAG_FD_OUTPUT)
5446 output_event = group_leader;
5447 if (flags & PERF_FLAG_FD_NO_GROUP)
5448 group_leader = NULL;
5449 }
5450
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005451 /*
5452 * Look up the group leader (we will attach this event to it):
5453 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005454 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005455 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005457 /*
5458 * Do not allow a recursive hierarchy (this new sibling
5459 * becoming part of another group-sibling):
5460 */
5461 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005462 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005463 /*
5464 * Do not allow to attach to a group in a different
5465 * task or CPU context:
5466 */
5467 if (group_leader->ctx != ctx)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005468 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469 /*
5470 * Only a group leader can be exclusive or pinned
5471 */
5472 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005473 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005474 }
5475
5476 if (output_event) {
5477 err = perf_event_set_output(event, output_event);
5478 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005479 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005480 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481
Al Viroea635c62010-05-26 17:40:29 -04005482 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5483 if (IS_ERR(event_file)) {
5484 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005485 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005486 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005488 event->filp = event_file;
5489 WARN_ON_ONCE(ctx->parent_ctx);
5490 mutex_lock(&ctx->mutex);
5491 perf_install_in_context(ctx, event, cpu);
5492 ++ctx->generation;
5493 mutex_unlock(&ctx->mutex);
5494
5495 event->owner = current;
5496 get_task_struct(current);
5497 mutex_lock(&current->perf_event_mutex);
5498 list_add_tail(&event->owner_entry, &current->perf_event_list);
5499 mutex_unlock(&current->perf_event_mutex);
5500
Peter Zijlstra8a495422010-05-27 15:47:49 +02005501 /*
5502 * Drop the reference on the group_event after placing the
5503 * new event on the sibling_list. This ensures destruction
5504 * of the group leader will find the pointer to itself in
5505 * perf_group_detach().
5506 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005508 fd_install(event_fd, event_file);
5509 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005510
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005511err_context:
Al Viroea635c62010-05-26 17:40:29 -04005512 fput_light(group_file, fput_needed);
5513 put_ctx(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005514err_alloc:
5515 free_event(event);
Al Viroea635c62010-05-26 17:40:29 -04005516err_fd:
5517 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005518 return err;
5519}
5520
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005521/**
5522 * perf_event_create_kernel_counter
5523 *
5524 * @attr: attributes of the counter to create
5525 * @cpu: cpu in which the counter is bound
5526 * @pid: task to profile
5527 */
5528struct perf_event *
5529perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005530 pid_t pid,
5531 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005532{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005533 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005534 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005535 int err;
5536
5537 /*
5538 * Get the target context (task or percpu):
5539 */
5540
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005541 event = perf_event_alloc(attr, cpu, NULL, NULL, overflow_handler);
5542 if (IS_ERR(event)) {
5543 err = PTR_ERR(event);
5544 goto err;
5545 }
5546
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005547 ctx = find_get_context(event->pmu, pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005548 if (IS_ERR(ctx)) {
5549 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005550 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005551 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005552
5553 event->filp = NULL;
5554 WARN_ON_ONCE(ctx->parent_ctx);
5555 mutex_lock(&ctx->mutex);
5556 perf_install_in_context(ctx, event, cpu);
5557 ++ctx->generation;
5558 mutex_unlock(&ctx->mutex);
5559
5560 event->owner = current;
5561 get_task_struct(current);
5562 mutex_lock(&current->perf_event_mutex);
5563 list_add_tail(&event->owner_entry, &current->perf_event_list);
5564 mutex_unlock(&current->perf_event_mutex);
5565
5566 return event;
5567
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005568err_free:
5569 free_event(event);
5570err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005571 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005572}
5573EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5574
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005575static void sync_child_event(struct perf_event *child_event,
5576 struct task_struct *child)
5577{
5578 struct perf_event *parent_event = child_event->parent;
5579 u64 child_val;
5580
5581 if (child_event->attr.inherit_stat)
5582 perf_event_read_event(child_event, child);
5583
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005584 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005585
5586 /*
5587 * Add back the child's count to the parent's count:
5588 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005589 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590 atomic64_add(child_event->total_time_enabled,
5591 &parent_event->child_total_time_enabled);
5592 atomic64_add(child_event->total_time_running,
5593 &parent_event->child_total_time_running);
5594
5595 /*
5596 * Remove this event from the parent's list
5597 */
5598 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5599 mutex_lock(&parent_event->child_mutex);
5600 list_del_init(&child_event->child_list);
5601 mutex_unlock(&parent_event->child_mutex);
5602
5603 /*
5604 * Release the parent event, if this was the last
5605 * reference to it.
5606 */
5607 fput(parent_event->filp);
5608}
5609
5610static void
5611__perf_event_exit_task(struct perf_event *child_event,
5612 struct perf_event_context *child_ctx,
5613 struct task_struct *child)
5614{
5615 struct perf_event *parent_event;
5616
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005617 perf_event_remove_from_context(child_event);
5618
5619 parent_event = child_event->parent;
5620 /*
5621 * It can happen that parent exits first, and has events
5622 * that are still around due to the child reference. These
5623 * events need to be zapped - but otherwise linger.
5624 */
5625 if (parent_event) {
5626 sync_child_event(child_event, child);
5627 free_event(child_event);
5628 }
5629}
5630
5631/*
5632 * When a child task exits, feed back event values to parent events.
5633 */
5634void perf_event_exit_task(struct task_struct *child)
5635{
5636 struct perf_event *child_event, *tmp;
5637 struct perf_event_context *child_ctx;
5638 unsigned long flags;
5639
5640 if (likely(!child->perf_event_ctxp)) {
5641 perf_event_task(child, NULL, 0);
5642 return;
5643 }
5644
5645 local_irq_save(flags);
5646 /*
5647 * We can't reschedule here because interrupts are disabled,
5648 * and either child is current or it is a task that can't be
5649 * scheduled, so we are now safe from rescheduling changing
5650 * our context.
5651 */
5652 child_ctx = child->perf_event_ctxp;
5653 __perf_event_task_sched_out(child_ctx);
5654
5655 /*
5656 * Take the context lock here so that if find_get_context is
5657 * reading child->perf_event_ctxp, we wait until it has
5658 * incremented the context's refcount before we do put_ctx below.
5659 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005660 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005661 child->perf_event_ctxp = NULL;
5662 /*
5663 * If this context is a clone; unclone it so it can't get
5664 * swapped to another process while we're removing all
5665 * the events from it.
5666 */
5667 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005668 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005669 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670
5671 /*
5672 * Report the task dead after unscheduling the events so that we
5673 * won't get any samples after PERF_RECORD_EXIT. We can however still
5674 * get a few PERF_RECORD_READ events.
5675 */
5676 perf_event_task(child, child_ctx, 0);
5677
5678 /*
5679 * We can recurse on the same lock type through:
5680 *
5681 * __perf_event_exit_task()
5682 * sync_child_event()
5683 * fput(parent_event->filp)
5684 * perf_release()
5685 * mutex_lock(&ctx->mutex)
5686 *
5687 * But since its the parent context it won't be the same instance.
5688 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005689 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005690
5691again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005692 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5693 group_entry)
5694 __perf_event_exit_task(child_event, child_ctx, child);
5695
5696 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005697 group_entry)
5698 __perf_event_exit_task(child_event, child_ctx, child);
5699
5700 /*
5701 * If the last event was a group event, it will have appended all
5702 * its siblings to the list, but we obtained 'tmp' before that which
5703 * will still point to the list head terminating the iteration.
5704 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005705 if (!list_empty(&child_ctx->pinned_groups) ||
5706 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005707 goto again;
5708
5709 mutex_unlock(&child_ctx->mutex);
5710
5711 put_ctx(child_ctx);
5712}
5713
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005714static void perf_free_event(struct perf_event *event,
5715 struct perf_event_context *ctx)
5716{
5717 struct perf_event *parent = event->parent;
5718
5719 if (WARN_ON_ONCE(!parent))
5720 return;
5721
5722 mutex_lock(&parent->child_mutex);
5723 list_del_init(&event->child_list);
5724 mutex_unlock(&parent->child_mutex);
5725
5726 fput(parent->filp);
5727
Peter Zijlstra8a495422010-05-27 15:47:49 +02005728 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005729 list_del_event(event, ctx);
5730 free_event(event);
5731}
5732
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005733/*
5734 * free an unexposed, unused context as created by inheritance by
5735 * init_task below, used by fork() in case of fail.
5736 */
5737void perf_event_free_task(struct task_struct *task)
5738{
5739 struct perf_event_context *ctx = task->perf_event_ctxp;
5740 struct perf_event *event, *tmp;
5741
5742 if (!ctx)
5743 return;
5744
5745 mutex_lock(&ctx->mutex);
5746again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005747 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5748 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005749
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005750 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5751 group_entry)
5752 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005754 if (!list_empty(&ctx->pinned_groups) ||
5755 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005756 goto again;
5757
5758 mutex_unlock(&ctx->mutex);
5759
5760 put_ctx(ctx);
5761}
5762
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005763/*
5764 * inherit a event from parent task to child task:
5765 */
5766static struct perf_event *
5767inherit_event(struct perf_event *parent_event,
5768 struct task_struct *parent,
5769 struct perf_event_context *parent_ctx,
5770 struct task_struct *child,
5771 struct perf_event *group_leader,
5772 struct perf_event_context *child_ctx)
5773{
5774 struct perf_event *child_event;
5775
5776 /*
5777 * Instead of creating recursive hierarchies of events,
5778 * we link inherited events back to the original parent,
5779 * which has a filp for sure, which we use as the reference
5780 * count:
5781 */
5782 if (parent_event->parent)
5783 parent_event = parent_event->parent;
5784
5785 child_event = perf_event_alloc(&parent_event->attr,
5786 parent_event->cpu,
5787 group_leader, parent_event,
5788 NULL);
5789 if (IS_ERR(child_event))
5790 return child_event;
5791 get_ctx(child_ctx);
5792
5793 /*
5794 * Make the child state follow the state of the parent event,
5795 * not its attr.disabled bit. We hold the parent's mutex,
5796 * so we won't race with perf_event_{en, dis}able_family.
5797 */
5798 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5799 child_event->state = PERF_EVENT_STATE_INACTIVE;
5800 else
5801 child_event->state = PERF_EVENT_STATE_OFF;
5802
5803 if (parent_event->attr.freq) {
5804 u64 sample_period = parent_event->hw.sample_period;
5805 struct hw_perf_event *hwc = &child_event->hw;
5806
5807 hwc->sample_period = sample_period;
5808 hwc->last_period = sample_period;
5809
5810 local64_set(&hwc->period_left, sample_period);
5811 }
5812
5813 child_event->ctx = child_ctx;
5814 child_event->overflow_handler = parent_event->overflow_handler;
5815
5816 /*
5817 * Link it up in the child's context:
5818 */
5819 add_event_to_ctx(child_event, child_ctx);
5820
5821 /*
5822 * Get a reference to the parent filp - we will fput it
5823 * when the child event exits. This is safe to do because
5824 * we are in the parent and we know that the filp still
5825 * exists and has a nonzero count:
5826 */
5827 atomic_long_inc(&parent_event->filp->f_count);
5828
5829 /*
5830 * Link this into the parent event's child list
5831 */
5832 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5833 mutex_lock(&parent_event->child_mutex);
5834 list_add_tail(&child_event->child_list, &parent_event->child_list);
5835 mutex_unlock(&parent_event->child_mutex);
5836
5837 return child_event;
5838}
5839
5840static int inherit_group(struct perf_event *parent_event,
5841 struct task_struct *parent,
5842 struct perf_event_context *parent_ctx,
5843 struct task_struct *child,
5844 struct perf_event_context *child_ctx)
5845{
5846 struct perf_event *leader;
5847 struct perf_event *sub;
5848 struct perf_event *child_ctr;
5849
5850 leader = inherit_event(parent_event, parent, parent_ctx,
5851 child, NULL, child_ctx);
5852 if (IS_ERR(leader))
5853 return PTR_ERR(leader);
5854 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5855 child_ctr = inherit_event(sub, parent, parent_ctx,
5856 child, leader, child_ctx);
5857 if (IS_ERR(child_ctr))
5858 return PTR_ERR(child_ctr);
5859 }
5860 return 0;
5861}
5862
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005863static int
5864inherit_task_group(struct perf_event *event, struct task_struct *parent,
5865 struct perf_event_context *parent_ctx,
5866 struct task_struct *child,
5867 int *inherited_all)
5868{
5869 int ret;
5870 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5871
5872 if (!event->attr.inherit) {
5873 *inherited_all = 0;
5874 return 0;
5875 }
5876
5877 if (!child_ctx) {
5878 /*
5879 * This is executed from the parent task context, so
5880 * inherit events that have been marked for cloning.
5881 * First allocate and initialize a context for the
5882 * child.
5883 */
5884
Peter Zijlstraeb184472010-09-07 15:55:13 +02005885 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005886 if (!child_ctx)
5887 return -ENOMEM;
5888
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005889 child->perf_event_ctxp = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005890 }
5891
5892 ret = inherit_group(event, parent, parent_ctx,
5893 child, child_ctx);
5894
5895 if (ret)
5896 *inherited_all = 0;
5897
5898 return ret;
5899}
5900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901/*
5902 * Initialize the perf_event context in task_struct
5903 */
5904int perf_event_init_task(struct task_struct *child)
5905{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005906 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005907 struct perf_event_context *cloned_ctx;
5908 struct perf_event *event;
5909 struct task_struct *parent = current;
5910 int inherited_all = 1;
5911 int ret = 0;
5912
5913 child->perf_event_ctxp = NULL;
5914
5915 mutex_init(&child->perf_event_mutex);
5916 INIT_LIST_HEAD(&child->perf_event_list);
5917
5918 if (likely(!parent->perf_event_ctxp))
5919 return 0;
5920
5921 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005922 * If the parent's context is a clone, pin it so it won't get
5923 * swapped under us.
5924 */
5925 parent_ctx = perf_pin_task_context(parent);
5926
5927 /*
5928 * No need to check if parent_ctx != NULL here; since we saw
5929 * it non-NULL earlier, the only reason for it to become NULL
5930 * is if we exit, and since we're currently in the middle of
5931 * a fork we can't be exiting at the same time.
5932 */
5933
5934 /*
5935 * Lock the parent list. No need to lock the child - not PID
5936 * hashed yet and not running, so nobody can access it.
5937 */
5938 mutex_lock(&parent_ctx->mutex);
5939
5940 /*
5941 * We dont have to disable NMIs - we are only looking at
5942 * the list, not manipulating it:
5943 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005944 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5945 ret = inherit_task_group(event, parent, parent_ctx, child,
5946 &inherited_all);
5947 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005948 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005949 }
5950
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005951 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5952 ret = inherit_task_group(event, parent, parent_ctx, child,
5953 &inherited_all);
5954 if (ret)
5955 break;
5956 }
5957
5958 child_ctx = child->perf_event_ctxp;
5959
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005960 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005961 /*
5962 * Mark the child context as a clone of the parent
5963 * context, or of whatever the parent is a clone of.
5964 * Note that if the parent is a clone, it could get
5965 * uncloned at any point, but that doesn't matter
5966 * because the list of events and the generation
5967 * count can't have changed since we took the mutex.
5968 */
5969 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5970 if (cloned_ctx) {
5971 child_ctx->parent_ctx = cloned_ctx;
5972 child_ctx->parent_gen = parent_ctx->parent_gen;
5973 } else {
5974 child_ctx->parent_ctx = parent_ctx;
5975 child_ctx->parent_gen = parent_ctx->generation;
5976 }
5977 get_ctx(child_ctx->parent_ctx);
5978 }
5979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005980 mutex_unlock(&parent_ctx->mutex);
5981
5982 perf_unpin_context(parent_ctx);
5983
5984 return ret;
5985}
5986
Paul Mackerras220b1402010-03-10 20:45:52 +11005987static void __init perf_event_init_all_cpus(void)
5988{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005989 struct swevent_htable *swhash;
5990 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11005991
5992 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005993 swhash = &per_cpu(swevent_htable, cpu);
5994 mutex_init(&swhash->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005995 }
5996}
5997
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005998static void __cpuinit perf_event_init_cpu(int cpu)
5999{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006000 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006001
6002 mutex_lock(&swhash->hlist_mutex);
6003 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006004 struct swevent_hlist *hlist;
6005
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006006 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6007 WARN_ON(!hlist);
6008 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006009 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006010 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006011}
6012
6013#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006014static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006015{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006016 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006017 struct perf_event *event, *tmp;
6018
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006019 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006020
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006021 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6022 __perf_event_remove_from_context(event);
6023 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006024 __perf_event_remove_from_context(event);
6025}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006026
6027static void perf_event_exit_cpu_context(int cpu)
6028{
6029 struct perf_event_context *ctx;
6030 struct pmu *pmu;
6031 int idx;
6032
6033 idx = srcu_read_lock(&pmus_srcu);
6034 list_for_each_entry_rcu(pmu, &pmus, entry) {
6035 ctx = &this_cpu_ptr(pmu->pmu_cpu_context)->ctx;
6036
6037 mutex_lock(&ctx->mutex);
6038 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6039 mutex_unlock(&ctx->mutex);
6040 }
6041 srcu_read_unlock(&pmus_srcu, idx);
6042
6043}
6044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006045static void perf_event_exit_cpu(int cpu)
6046{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006047 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006048
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006049 mutex_lock(&swhash->hlist_mutex);
6050 swevent_hlist_release(swhash);
6051 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006052
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006053 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006054}
6055#else
6056static inline void perf_event_exit_cpu(int cpu) { }
6057#endif
6058
6059static int __cpuinit
6060perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6061{
6062 unsigned int cpu = (long)hcpu;
6063
Peter Zijlstra5e116372010-06-11 13:35:08 +02006064 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006065
6066 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006067 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006068 perf_event_init_cpu(cpu);
6069 break;
6070
Peter Zijlstra5e116372010-06-11 13:35:08 +02006071 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006072 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073 perf_event_exit_cpu(cpu);
6074 break;
6075
6076 default:
6077 break;
6078 }
6079
6080 return NOTIFY_OK;
6081}
6082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006083void __init perf_event_init(void)
6084{
Paul Mackerras220b1402010-03-10 20:45:52 +11006085 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006086 init_srcu_struct(&pmus_srcu);
6087 perf_pmu_register(&perf_swevent);
6088 perf_pmu_register(&perf_cpu_clock);
6089 perf_pmu_register(&perf_task_clock);
6090 perf_tp_register();
6091 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092}