blob: 3f5309db72f1597a64353d5b5e29ff921bcc0af7 [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 *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200151perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200152{
153 struct perf_event_context *ctx;
154
155 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200156retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200157 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200158 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);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200170 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
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 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200189static struct perf_event_context *
190perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200191{
192 struct perf_event_context *ctx;
193 unsigned long flags;
194
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200195 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200196 if (ctx) {
197 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100198 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200199 }
200 return ctx;
201}
202
203static void perf_unpin_context(struct perf_event_context *ctx)
204{
205 unsigned long flags;
206
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100207 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200208 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100209 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210 put_ctx(ctx);
211}
212
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100213static inline u64 perf_clock(void)
214{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200215 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100216}
217
218/*
219 * Update the record of the current time in a context.
220 */
221static void update_context_time(struct perf_event_context *ctx)
222{
223 u64 now = perf_clock();
224
225 ctx->time += now - ctx->timestamp;
226 ctx->timestamp = now;
227}
228
229/*
230 * Update the total_time_enabled and total_time_running fields for a event.
231 */
232static void update_event_times(struct perf_event *event)
233{
234 struct perf_event_context *ctx = event->ctx;
235 u64 run_end;
236
237 if (event->state < PERF_EVENT_STATE_INACTIVE ||
238 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
239 return;
240
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100241 if (ctx->is_active)
242 run_end = ctx->time;
243 else
244 run_end = event->tstamp_stopped;
245
246 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100247
248 if (event->state == PERF_EVENT_STATE_INACTIVE)
249 run_end = event->tstamp_stopped;
250 else
251 run_end = ctx->time;
252
253 event->total_time_running = run_end - event->tstamp_running;
254}
255
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200256/*
257 * Update total_time_enabled and total_time_running for all events in a group.
258 */
259static void update_group_times(struct perf_event *leader)
260{
261 struct perf_event *event;
262
263 update_event_times(leader);
264 list_for_each_entry(event, &leader->sibling_list, group_entry)
265 update_event_times(event);
266}
267
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100268static struct list_head *
269ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
270{
271 if (event->attr.pinned)
272 return &ctx->pinned_groups;
273 else
274 return &ctx->flexible_groups;
275}
276
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200277/*
278 * Add a event from the lists for its context.
279 * Must be called with ctx->mutex and ctx->lock held.
280 */
281static void
282list_add_event(struct perf_event *event, struct perf_event_context *ctx)
283{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200284 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
285 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200286
287 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200288 * If we're a stand alone event or group leader, we go to the context
289 * list, group events are kept attached to the group so that
290 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200291 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200292 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100293 struct list_head *list;
294
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100295 if (is_software_event(event))
296 event->group_flags |= PERF_GROUP_SOFTWARE;
297
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100298 list = ctx_group_list(event, ctx);
299 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200300 }
301
302 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200303 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200304 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200305 ctx->nr_events++;
306 if (event->attr.inherit_stat)
307 ctx->nr_stat++;
308}
309
Peter Zijlstra8a495422010-05-27 15:47:49 +0200310static void perf_group_attach(struct perf_event *event)
311{
312 struct perf_event *group_leader = event->group_leader;
313
314 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
315 event->attach_state |= PERF_ATTACH_GROUP;
316
317 if (group_leader == event)
318 return;
319
320 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
321 !is_software_event(event))
322 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
323
324 list_add_tail(&event->group_entry, &group_leader->sibling_list);
325 group_leader->nr_siblings++;
326}
327
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200328/*
329 * Remove a event from the lists for its context.
330 * Must be called with ctx->mutex and ctx->lock held.
331 */
332static void
333list_del_event(struct perf_event *event, struct perf_event_context *ctx)
334{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200335 /*
336 * We can have double detach due to exit/hot-unplug + close.
337 */
338 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200339 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340
341 event->attach_state &= ~PERF_ATTACH_CONTEXT;
342
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200343 ctx->nr_events--;
344 if (event->attr.inherit_stat)
345 ctx->nr_stat--;
346
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200347 list_del_rcu(&event->event_entry);
348
Peter Zijlstra8a495422010-05-27 15:47:49 +0200349 if (event->group_leader == event)
350 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200351
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200352 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800353
354 /*
355 * If event was in error state, then keep it
356 * that way, otherwise bogus counts will be
357 * returned on read(). The only way to get out
358 * of error state is by explicit re-enabling
359 * of the event
360 */
361 if (event->state > PERF_EVENT_STATE_OFF)
362 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200363}
364
Peter Zijlstra8a495422010-05-27 15:47:49 +0200365static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200366{
367 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200368 struct list_head *list = NULL;
369
370 /*
371 * We can have double detach due to exit/hot-unplug + close.
372 */
373 if (!(event->attach_state & PERF_ATTACH_GROUP))
374 return;
375
376 event->attach_state &= ~PERF_ATTACH_GROUP;
377
378 /*
379 * If this is a sibling, remove it from its group.
380 */
381 if (event->group_leader != event) {
382 list_del_init(&event->group_entry);
383 event->group_leader->nr_siblings--;
384 return;
385 }
386
387 if (!list_empty(&event->group_entry))
388 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100389
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200390 /*
391 * If this was a group event with sibling events then
392 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200393 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200394 */
395 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200396 if (list)
397 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200398 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100399
400 /* Inherit group flags from the previous leader */
401 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402 }
403}
404
Stephane Eranianfa66f072010-08-26 16:40:01 +0200405static inline int
406event_filter_match(struct perf_event *event)
407{
408 return event->cpu == -1 || event->cpu == smp_processor_id();
409}
410
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200411static void
412event_sched_out(struct perf_event *event,
413 struct perf_cpu_context *cpuctx,
414 struct perf_event_context *ctx)
415{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200416 u64 delta;
417 /*
418 * An event which could not be activated because of
419 * filter mismatch still needs to have its timings
420 * maintained, otherwise bogus information is return
421 * via read() for time_enabled, time_running:
422 */
423 if (event->state == PERF_EVENT_STATE_INACTIVE
424 && !event_filter_match(event)) {
425 delta = ctx->time - event->tstamp_stopped;
426 event->tstamp_running += delta;
427 event->tstamp_stopped = ctx->time;
428 }
429
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200430 if (event->state != PERF_EVENT_STATE_ACTIVE)
431 return;
432
433 event->state = PERF_EVENT_STATE_INACTIVE;
434 if (event->pending_disable) {
435 event->pending_disable = 0;
436 event->state = PERF_EVENT_STATE_OFF;
437 }
438 event->tstamp_stopped = ctx->time;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200439 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200440 event->oncpu = -1;
441
442 if (!is_software_event(event))
443 cpuctx->active_oncpu--;
444 ctx->nr_active--;
445 if (event->attr.exclusive || !cpuctx->active_oncpu)
446 cpuctx->exclusive = 0;
447}
448
449static void
450group_sched_out(struct perf_event *group_event,
451 struct perf_cpu_context *cpuctx,
452 struct perf_event_context *ctx)
453{
454 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200455 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200456
457 event_sched_out(group_event, cpuctx, ctx);
458
459 /*
460 * Schedule out siblings (if any):
461 */
462 list_for_each_entry(event, &group_event->sibling_list, group_entry)
463 event_sched_out(event, cpuctx, ctx);
464
Stephane Eranianfa66f072010-08-26 16:40:01 +0200465 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200466 cpuctx->exclusive = 0;
467}
468
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200469static inline struct perf_cpu_context *
470__get_cpu_context(struct perf_event_context *ctx)
471{
472 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
473}
474
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200475/*
476 * Cross CPU call to remove a performance event
477 *
478 * We disable the event on the hardware level first. After that we
479 * remove it from the context list.
480 */
481static void __perf_event_remove_from_context(void *info)
482{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200483 struct perf_event *event = info;
484 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200485 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200486
487 /*
488 * If this is a task context, we need to check whether it is
489 * the current task context of this cpu. If not it has been
490 * scheduled out before the smp call arrived.
491 */
492 if (ctx->task && cpuctx->task_ctx != ctx)
493 return;
494
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100495 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200496
497 event_sched_out(event, cpuctx, ctx);
498
499 list_del_event(event, ctx);
500
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100501 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200502}
503
504
505/*
506 * Remove the event from a task's (or a CPU's) list of events.
507 *
508 * Must be called with ctx->mutex held.
509 *
510 * CPU events are removed with a smp call. For task events we only
511 * call when the task is on a CPU.
512 *
513 * If event->ctx is a cloned context, callers must make sure that
514 * every task struct that event->ctx->task could possibly point to
515 * remains valid. This is OK when called from perf_release since
516 * that only calls us on the top-level context, which can't be a clone.
517 * When called from perf_event_exit_task, it's OK because the
518 * context has been detached from its task.
519 */
520static void perf_event_remove_from_context(struct perf_event *event)
521{
522 struct perf_event_context *ctx = event->ctx;
523 struct task_struct *task = ctx->task;
524
525 if (!task) {
526 /*
527 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200528 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200529 */
530 smp_call_function_single(event->cpu,
531 __perf_event_remove_from_context,
532 event, 1);
533 return;
534 }
535
536retry:
537 task_oncpu_function_call(task, __perf_event_remove_from_context,
538 event);
539
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100540 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200541 /*
542 * If the context is active we need to retry the smp call.
543 */
544 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100545 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200546 goto retry;
547 }
548
549 /*
550 * The lock prevents that this context is scheduled in so we
551 * can remove the event safely, if the call above did not
552 * succeed.
553 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100554 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100556 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200557}
558
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200559/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200560 * Cross CPU call to disable a performance event
561 */
562static void __perf_event_disable(void *info)
563{
564 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200566 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200567
568 /*
569 * If this is a per-task event, need to check whether this
570 * event's task is the current task on this cpu.
571 */
572 if (ctx->task && cpuctx->task_ctx != ctx)
573 return;
574
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100575 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200576
577 /*
578 * If the event is on, turn it off.
579 * If it is in error state, leave it in error state.
580 */
581 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
582 update_context_time(ctx);
583 update_group_times(event);
584 if (event == event->group_leader)
585 group_sched_out(event, cpuctx, ctx);
586 else
587 event_sched_out(event, cpuctx, ctx);
588 event->state = PERF_EVENT_STATE_OFF;
589 }
590
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100591 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200592}
593
594/*
595 * Disable a event.
596 *
597 * If event->ctx is a cloned context, callers must make sure that
598 * every task struct that event->ctx->task could possibly point to
599 * remains valid. This condition is satisifed when called through
600 * perf_event_for_each_child or perf_event_for_each because they
601 * hold the top-level event's child_mutex, so any descendant that
602 * goes to exit will block in sync_child_event.
603 * When called from perf_pending_event it's OK because event->ctx
604 * is the current context on this CPU and preemption is disabled,
605 * hence we can't get into perf_event_task_sched_out for this context.
606 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100607void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200608{
609 struct perf_event_context *ctx = event->ctx;
610 struct task_struct *task = ctx->task;
611
612 if (!task) {
613 /*
614 * Disable the event on the cpu that it's on
615 */
616 smp_call_function_single(event->cpu, __perf_event_disable,
617 event, 1);
618 return;
619 }
620
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200621retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200622 task_oncpu_function_call(task, __perf_event_disable, event);
623
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100624 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200625 /*
626 * If the event is still active, we need to retry the cross-call.
627 */
628 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100629 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200630 goto retry;
631 }
632
633 /*
634 * Since we have the lock this context can't be scheduled
635 * in, so we can change the state safely.
636 */
637 if (event->state == PERF_EVENT_STATE_INACTIVE) {
638 update_group_times(event);
639 event->state = PERF_EVENT_STATE_OFF;
640 }
641
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100642 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200643}
644
645static int
646event_sched_in(struct perf_event *event,
647 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100648 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200649{
650 if (event->state <= PERF_EVENT_STATE_OFF)
651 return 0;
652
653 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100654 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200655 /*
656 * The new state must be visible before we turn it on in the hardware:
657 */
658 smp_wmb();
659
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200660 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200661 event->state = PERF_EVENT_STATE_INACTIVE;
662 event->oncpu = -1;
663 return -EAGAIN;
664 }
665
666 event->tstamp_running += ctx->time - event->tstamp_stopped;
667
668 if (!is_software_event(event))
669 cpuctx->active_oncpu++;
670 ctx->nr_active++;
671
672 if (event->attr.exclusive)
673 cpuctx->exclusive = 1;
674
675 return 0;
676}
677
678static int
679group_sched_in(struct perf_event *group_event,
680 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100681 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200682{
Lin Ming6bde9b62010-04-23 13:56:00 +0800683 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200684 struct pmu *pmu = group_event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200685
686 if (group_event->state == PERF_EVENT_STATE_OFF)
687 return 0;
688
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200689 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200690
Stephane Eranian90151c32010-05-25 16:23:10 +0200691 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200692 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200693 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200694 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200695
696 /*
697 * Schedule in siblings as one group (if any):
698 */
699 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100700 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701 partial_group = event;
702 goto group_error;
703 }
704 }
705
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200706 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000707 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800708
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200709group_error:
710 /*
711 * Groups can be scheduled in as one unit only, so undo any
712 * partial group before returning:
713 */
714 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
715 if (event == partial_group)
716 break;
717 event_sched_out(event, cpuctx, ctx);
718 }
719 event_sched_out(group_event, cpuctx, ctx);
720
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200721 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200722
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200723 return -EAGAIN;
724}
725
726/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200727 * Work out whether we can put this event group on the CPU now.
728 */
729static int group_can_go_on(struct perf_event *event,
730 struct perf_cpu_context *cpuctx,
731 int can_add_hw)
732{
733 /*
734 * Groups consisting entirely of software events can always go on.
735 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100736 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200737 return 1;
738 /*
739 * If an exclusive group is already on, no other hardware
740 * events can go on.
741 */
742 if (cpuctx->exclusive)
743 return 0;
744 /*
745 * If this group is exclusive and there are already
746 * events on the CPU, it can't go on.
747 */
748 if (event->attr.exclusive && cpuctx->active_oncpu)
749 return 0;
750 /*
751 * Otherwise, try to add it if all previous groups were able
752 * to go on.
753 */
754 return can_add_hw;
755}
756
757static void add_event_to_ctx(struct perf_event *event,
758 struct perf_event_context *ctx)
759{
760 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200761 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200762 event->tstamp_enabled = ctx->time;
763 event->tstamp_running = ctx->time;
764 event->tstamp_stopped = ctx->time;
765}
766
767/*
768 * Cross CPU call to install and enable a performance event
769 *
770 * Must be called with ctx->mutex held
771 */
772static void __perf_install_in_context(void *info)
773{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200774 struct perf_event *event = info;
775 struct perf_event_context *ctx = event->ctx;
776 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200777 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200778 int err;
779
780 /*
781 * If this is a task context, we need to check whether it is
782 * the current task context of this cpu. If not it has been
783 * scheduled out before the smp call arrived.
784 * Or possibly this is the right context but it isn't
785 * on this cpu because it had no events.
786 */
787 if (ctx->task && cpuctx->task_ctx != ctx) {
788 if (cpuctx->task_ctx || ctx->task != current)
789 return;
790 cpuctx->task_ctx = ctx;
791 }
792
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100793 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200794 ctx->is_active = 1;
795 update_context_time(ctx);
796
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200797 add_event_to_ctx(event, ctx);
798
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100799 if (event->cpu != -1 && event->cpu != smp_processor_id())
800 goto unlock;
801
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200802 /*
803 * Don't put the event on if it is disabled or if
804 * it is in a group and the group isn't on.
805 */
806 if (event->state != PERF_EVENT_STATE_INACTIVE ||
807 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
808 goto unlock;
809
810 /*
811 * An exclusive event can't go on if there are already active
812 * hardware events, and no hardware event can go on if there
813 * is already an exclusive event on.
814 */
815 if (!group_can_go_on(event, cpuctx, 1))
816 err = -EEXIST;
817 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100818 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200819
820 if (err) {
821 /*
822 * This event couldn't go on. If it is in a group
823 * then we have to pull the whole group off.
824 * If the event group is pinned then put it in error state.
825 */
826 if (leader != event)
827 group_sched_out(leader, cpuctx, ctx);
828 if (leader->attr.pinned) {
829 update_group_times(leader);
830 leader->state = PERF_EVENT_STATE_ERROR;
831 }
832 }
833
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200834unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100835 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200836}
837
838/*
839 * Attach a performance event to a context
840 *
841 * First we add the event to the list with the hardware enable bit
842 * in event->hw_config cleared.
843 *
844 * If the event is attached to a task which is on a CPU we use a smp
845 * call to enable it in the task context. The task might have been
846 * scheduled away, but we check this in the smp call again.
847 *
848 * Must be called with ctx->mutex held.
849 */
850static void
851perf_install_in_context(struct perf_event_context *ctx,
852 struct perf_event *event,
853 int cpu)
854{
855 struct task_struct *task = ctx->task;
856
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200857 event->ctx = ctx;
858
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200859 if (!task) {
860 /*
861 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200862 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863 */
864 smp_call_function_single(cpu, __perf_install_in_context,
865 event, 1);
866 return;
867 }
868
869retry:
870 task_oncpu_function_call(task, __perf_install_in_context,
871 event);
872
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100873 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200874 /*
875 * we need to retry the smp call.
876 */
877 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100878 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200879 goto retry;
880 }
881
882 /*
883 * The lock prevents that this context is scheduled in so we
884 * can add the event safely, if it the call above did not
885 * succeed.
886 */
887 if (list_empty(&event->group_entry))
888 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100889 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200890}
891
892/*
893 * Put a event into inactive state and update time fields.
894 * Enabling the leader of a group effectively enables all
895 * the group members that aren't explicitly disabled, so we
896 * have to update their ->tstamp_enabled also.
897 * Note: this works for group members as well as group leaders
898 * since the non-leader members' sibling_lists will be empty.
899 */
900static void __perf_event_mark_enabled(struct perf_event *event,
901 struct perf_event_context *ctx)
902{
903 struct perf_event *sub;
904
905 event->state = PERF_EVENT_STATE_INACTIVE;
906 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200907 list_for_each_entry(sub, &event->sibling_list, group_entry) {
908 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200909 sub->tstamp_enabled =
910 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200911 }
912 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200913}
914
915/*
916 * Cross CPU call to enable a performance event
917 */
918static void __perf_event_enable(void *info)
919{
920 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200921 struct perf_event_context *ctx = event->ctx;
922 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200923 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200924 int err;
925
926 /*
927 * If this is a per-task event, need to check whether this
928 * event's task is the current task on this cpu.
929 */
930 if (ctx->task && cpuctx->task_ctx != ctx) {
931 if (cpuctx->task_ctx || ctx->task != current)
932 return;
933 cpuctx->task_ctx = ctx;
934 }
935
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100936 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200937 ctx->is_active = 1;
938 update_context_time(ctx);
939
940 if (event->state >= PERF_EVENT_STATE_INACTIVE)
941 goto unlock;
942 __perf_event_mark_enabled(event, ctx);
943
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100944 if (event->cpu != -1 && event->cpu != smp_processor_id())
945 goto unlock;
946
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200947 /*
948 * If the event is in a group and isn't the group leader,
949 * then don't put it on unless the group is on.
950 */
951 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
952 goto unlock;
953
954 if (!group_can_go_on(event, cpuctx, 1)) {
955 err = -EEXIST;
956 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200957 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100958 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200959 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100960 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200961 }
962
963 if (err) {
964 /*
965 * If this event can't go on and it's part of a
966 * group, then the whole group has to come off.
967 */
968 if (leader != event)
969 group_sched_out(leader, cpuctx, ctx);
970 if (leader->attr.pinned) {
971 update_group_times(leader);
972 leader->state = PERF_EVENT_STATE_ERROR;
973 }
974 }
975
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200976unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100977 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978}
979
980/*
981 * Enable a event.
982 *
983 * If event->ctx is a cloned context, callers must make sure that
984 * every task struct that event->ctx->task could possibly point to
985 * remains valid. This condition is satisfied when called through
986 * perf_event_for_each_child or perf_event_for_each as described
987 * for perf_event_disable.
988 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100989void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200990{
991 struct perf_event_context *ctx = event->ctx;
992 struct task_struct *task = ctx->task;
993
994 if (!task) {
995 /*
996 * Enable the event on the cpu that it's on
997 */
998 smp_call_function_single(event->cpu, __perf_event_enable,
999 event, 1);
1000 return;
1001 }
1002
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001003 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001004 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1005 goto out;
1006
1007 /*
1008 * If the event is in error state, clear that first.
1009 * That way, if we see the event in error state below, we
1010 * know that it has gone back into error state, as distinct
1011 * from the task having been scheduled away before the
1012 * cross-call arrived.
1013 */
1014 if (event->state == PERF_EVENT_STATE_ERROR)
1015 event->state = PERF_EVENT_STATE_OFF;
1016
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001017retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001018 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001019 task_oncpu_function_call(task, __perf_event_enable, event);
1020
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001021 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022
1023 /*
1024 * If the context is active and the event is still off,
1025 * we need to retry the cross-call.
1026 */
1027 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1028 goto retry;
1029
1030 /*
1031 * Since we have the lock this context can't be scheduled
1032 * in, so we can change the state safely.
1033 */
1034 if (event->state == PERF_EVENT_STATE_OFF)
1035 __perf_event_mark_enabled(event, ctx);
1036
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001037out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001038 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001039}
1040
1041static int perf_event_refresh(struct perf_event *event, int refresh)
1042{
1043 /*
1044 * not supported on inherited events
1045 */
1046 if (event->attr.inherit)
1047 return -EINVAL;
1048
1049 atomic_add(refresh, &event->event_limit);
1050 perf_event_enable(event);
1051
1052 return 0;
1053}
1054
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001055enum event_type_t {
1056 EVENT_FLEXIBLE = 0x1,
1057 EVENT_PINNED = 0x2,
1058 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1059};
1060
1061static void ctx_sched_out(struct perf_event_context *ctx,
1062 struct perf_cpu_context *cpuctx,
1063 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001064{
1065 struct perf_event *event;
1066
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001067 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001068 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001069 ctx->is_active = 0;
1070 if (likely(!ctx->nr_events))
1071 goto out;
1072 update_context_time(ctx);
1073
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001074 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001075 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001076
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001077 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001078 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1079 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001080 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001081
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001082 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001083 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001084 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001085 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001086out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001087 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001088 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089}
1090
1091/*
1092 * Test whether two contexts are equivalent, i.e. whether they
1093 * have both been cloned from the same version of the same context
1094 * and they both have the same number of enabled events.
1095 * If the number of enabled events is the same, then the set
1096 * of enabled events should be the same, because these are both
1097 * inherited contexts, therefore we can't access individual events
1098 * in them directly with an fd; we can only enable/disable all
1099 * events via prctl, or enable/disable all events in a family
1100 * via ioctl, which will have the same effect on both contexts.
1101 */
1102static int context_equiv(struct perf_event_context *ctx1,
1103 struct perf_event_context *ctx2)
1104{
1105 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1106 && ctx1->parent_gen == ctx2->parent_gen
1107 && !ctx1->pin_count && !ctx2->pin_count;
1108}
1109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001110static void __perf_event_sync_stat(struct perf_event *event,
1111 struct perf_event *next_event)
1112{
1113 u64 value;
1114
1115 if (!event->attr.inherit_stat)
1116 return;
1117
1118 /*
1119 * Update the event value, we cannot use perf_event_read()
1120 * because we're in the middle of a context switch and have IRQs
1121 * disabled, which upsets smp_call_function_single(), however
1122 * we know the event must be on the current CPU, therefore we
1123 * don't need to use it.
1124 */
1125 switch (event->state) {
1126 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001127 event->pmu->read(event);
1128 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001129
1130 case PERF_EVENT_STATE_INACTIVE:
1131 update_event_times(event);
1132 break;
1133
1134 default:
1135 break;
1136 }
1137
1138 /*
1139 * In order to keep per-task stats reliable we need to flip the event
1140 * values when we flip the contexts.
1141 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001142 value = local64_read(&next_event->count);
1143 value = local64_xchg(&event->count, value);
1144 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001145
1146 swap(event->total_time_enabled, next_event->total_time_enabled);
1147 swap(event->total_time_running, next_event->total_time_running);
1148
1149 /*
1150 * Since we swizzled the values, update the user visible data too.
1151 */
1152 perf_event_update_userpage(event);
1153 perf_event_update_userpage(next_event);
1154}
1155
1156#define list_next_entry(pos, member) \
1157 list_entry(pos->member.next, typeof(*pos), member)
1158
1159static void perf_event_sync_stat(struct perf_event_context *ctx,
1160 struct perf_event_context *next_ctx)
1161{
1162 struct perf_event *event, *next_event;
1163
1164 if (!ctx->nr_stat)
1165 return;
1166
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001167 update_context_time(ctx);
1168
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001169 event = list_first_entry(&ctx->event_list,
1170 struct perf_event, event_entry);
1171
1172 next_event = list_first_entry(&next_ctx->event_list,
1173 struct perf_event, event_entry);
1174
1175 while (&event->event_entry != &ctx->event_list &&
1176 &next_event->event_entry != &next_ctx->event_list) {
1177
1178 __perf_event_sync_stat(event, next_event);
1179
1180 event = list_next_entry(event, event_entry);
1181 next_event = list_next_entry(next_event, event_entry);
1182 }
1183}
1184
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001185void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1186 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001187{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001188 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001189 struct perf_event_context *next_ctx;
1190 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001191 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001192 int do_switch = 1;
1193
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001194 if (likely(!ctx))
1195 return;
1196
1197 cpuctx = __get_cpu_context(ctx);
1198 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001199 return;
1200
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001201 rcu_read_lock();
1202 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001203 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204 if (parent && next_ctx &&
1205 rcu_dereference(next_ctx->parent_ctx) == parent) {
1206 /*
1207 * Looks like the two contexts are clones, so we might be
1208 * able to optimize the context switch. We lock both
1209 * contexts and check that they are clones under the
1210 * lock (including re-checking that neither has been
1211 * uncloned in the meantime). It doesn't matter which
1212 * order we take the locks because no other cpu could
1213 * be trying to lock both of these tasks.
1214 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001215 raw_spin_lock(&ctx->lock);
1216 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001217 if (context_equiv(ctx, next_ctx)) {
1218 /*
1219 * XXX do we need a memory barrier of sorts
1220 * wrt to rcu_dereference() of perf_event_ctxp
1221 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001222 task->perf_event_ctxp[ctxn] = next_ctx;
1223 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001224 ctx->task = next;
1225 next_ctx->task = task;
1226 do_switch = 0;
1227
1228 perf_event_sync_stat(ctx, next_ctx);
1229 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001230 raw_spin_unlock(&next_ctx->lock);
1231 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 }
1233 rcu_read_unlock();
1234
1235 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001236 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001237 cpuctx->task_ctx = NULL;
1238 }
1239}
1240
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001241#define for_each_task_context_nr(ctxn) \
1242 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1243
1244/*
1245 * Called from scheduler to remove the events of the current task,
1246 * with interrupts disabled.
1247 *
1248 * We stop each event and update the event value in event->count.
1249 *
1250 * This does not protect us against NMI, but disable()
1251 * sets the disabled bit in the control field of event _before_
1252 * accessing the event control register. If a NMI hits, then it will
1253 * not restart the event.
1254 */
1255void perf_event_task_sched_out(struct task_struct *task,
1256 struct task_struct *next)
1257{
1258 int ctxn;
1259
1260 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1261
1262 for_each_task_context_nr(ctxn)
1263 perf_event_context_sched_out(task, ctxn, next);
1264}
1265
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001266static void task_ctx_sched_out(struct perf_event_context *ctx,
1267 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001269 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270
1271 if (!cpuctx->task_ctx)
1272 return;
1273
1274 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1275 return;
1276
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001277 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001278 cpuctx->task_ctx = NULL;
1279}
1280
1281/*
1282 * Called with IRQs disabled
1283 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001284static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001285{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001286 task_ctx_sched_out(ctx, EVENT_ALL);
1287}
1288
1289/*
1290 * Called with IRQs disabled
1291 */
1292static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1293 enum event_type_t event_type)
1294{
1295 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001296}
1297
1298static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001299ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001300 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301{
1302 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001303
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001304 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1305 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001307 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 continue;
1309
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001310 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001311 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312
1313 /*
1314 * If this pinned group hasn't been scheduled,
1315 * put it in error state.
1316 */
1317 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1318 update_group_times(event);
1319 event->state = PERF_EVENT_STATE_ERROR;
1320 }
1321 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001322}
1323
1324static void
1325ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001326 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001327{
1328 struct perf_event *event;
1329 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001330
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001331 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1332 /* Ignore events in OFF or ERROR state */
1333 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001334 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001335 /*
1336 * Listen to the 'cpu' scheduling filter constraint
1337 * of events:
1338 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001339 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001340 continue;
1341
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001342 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001343 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001344 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001345 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001347}
1348
1349static void
1350ctx_sched_in(struct perf_event_context *ctx,
1351 struct perf_cpu_context *cpuctx,
1352 enum event_type_t event_type)
1353{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001354 raw_spin_lock(&ctx->lock);
1355 ctx->is_active = 1;
1356 if (likely(!ctx->nr_events))
1357 goto out;
1358
1359 ctx->timestamp = perf_clock();
1360
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001361 /*
1362 * First go through the list and put on any pinned groups
1363 * in order to give them the best chance of going on.
1364 */
1365 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001366 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001367
1368 /* Then walk through the lower prio flexible groups */
1369 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001370 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001371
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001372out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001373 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374}
1375
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001376static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1377 enum event_type_t event_type)
1378{
1379 struct perf_event_context *ctx = &cpuctx->ctx;
1380
1381 ctx_sched_in(ctx, cpuctx, event_type);
1382}
1383
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001384static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001385 enum event_type_t event_type)
1386{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001387 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001388
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001389 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001390 if (cpuctx->task_ctx == ctx)
1391 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001392
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001393 ctx_sched_in(ctx, cpuctx, event_type);
1394 cpuctx->task_ctx = ctx;
1395}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001397void perf_event_context_sched_in(struct perf_event_context *ctx)
1398{
1399 struct perf_cpu_context *cpuctx;
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
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001405 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001406 /*
1407 * We want to keep the following priority order:
1408 * cpu pinned (that don't need to move), task pinned,
1409 * cpu flexible, task flexible.
1410 */
1411 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1412
1413 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1414 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1415 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1416
1417 cpuctx->task_ctx = ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001418
1419 /*
1420 * Since these rotations are per-cpu, we need to ensure the
1421 * cpu-context we got scheduled on is actually rotating.
1422 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001423 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001424 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425}
1426
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001427/*
1428 * Called from scheduler to add the events of the current task
1429 * with interrupts disabled.
1430 *
1431 * We restore the event value and then enable it.
1432 *
1433 * This does not protect us against NMI, but enable()
1434 * sets the enabled bit in the control field of event _before_
1435 * accessing the event control register. If a NMI hits, then it will
1436 * keep the event running.
1437 */
1438void perf_event_task_sched_in(struct task_struct *task)
1439{
1440 struct perf_event_context *ctx;
1441 int ctxn;
1442
1443 for_each_task_context_nr(ctxn) {
1444 ctx = task->perf_event_ctxp[ctxn];
1445 if (likely(!ctx))
1446 continue;
1447
1448 perf_event_context_sched_in(ctx);
1449 }
1450}
1451
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001452#define MAX_INTERRUPTS (~0ULL)
1453
1454static void perf_log_throttle(struct perf_event *event, int enable);
1455
Peter Zijlstraabd50712010-01-26 18:50:16 +01001456static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1457{
1458 u64 frequency = event->attr.sample_freq;
1459 u64 sec = NSEC_PER_SEC;
1460 u64 divisor, dividend;
1461
1462 int count_fls, nsec_fls, frequency_fls, sec_fls;
1463
1464 count_fls = fls64(count);
1465 nsec_fls = fls64(nsec);
1466 frequency_fls = fls64(frequency);
1467 sec_fls = 30;
1468
1469 /*
1470 * We got @count in @nsec, with a target of sample_freq HZ
1471 * the target period becomes:
1472 *
1473 * @count * 10^9
1474 * period = -------------------
1475 * @nsec * sample_freq
1476 *
1477 */
1478
1479 /*
1480 * Reduce accuracy by one bit such that @a and @b converge
1481 * to a similar magnitude.
1482 */
1483#define REDUCE_FLS(a, b) \
1484do { \
1485 if (a##_fls > b##_fls) { \
1486 a >>= 1; \
1487 a##_fls--; \
1488 } else { \
1489 b >>= 1; \
1490 b##_fls--; \
1491 } \
1492} while (0)
1493
1494 /*
1495 * Reduce accuracy until either term fits in a u64, then proceed with
1496 * the other, so that finally we can do a u64/u64 division.
1497 */
1498 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1499 REDUCE_FLS(nsec, frequency);
1500 REDUCE_FLS(sec, count);
1501 }
1502
1503 if (count_fls + sec_fls > 64) {
1504 divisor = nsec * frequency;
1505
1506 while (count_fls + sec_fls > 64) {
1507 REDUCE_FLS(count, sec);
1508 divisor >>= 1;
1509 }
1510
1511 dividend = count * sec;
1512 } else {
1513 dividend = count * sec;
1514
1515 while (nsec_fls + frequency_fls > 64) {
1516 REDUCE_FLS(nsec, frequency);
1517 dividend >>= 1;
1518 }
1519
1520 divisor = nsec * frequency;
1521 }
1522
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001523 if (!divisor)
1524 return dividend;
1525
Peter Zijlstraabd50712010-01-26 18:50:16 +01001526 return div64_u64(dividend, divisor);
1527}
1528
1529static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001530{
1531 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001532 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533 s64 delta;
1534
Peter Zijlstraabd50712010-01-26 18:50:16 +01001535 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536
1537 delta = (s64)(period - hwc->sample_period);
1538 delta = (delta + 7) / 8; /* low pass filter */
1539
1540 sample_period = hwc->sample_period + delta;
1541
1542 if (!sample_period)
1543 sample_period = 1;
1544
1545 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001546
Peter Zijlstrae7850592010-05-21 14:43:08 +02001547 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001548 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001549 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001550 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001551 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552}
1553
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001554static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555{
1556 struct perf_event *event;
1557 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001558 u64 interrupts, now;
1559 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001561 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001562 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563 if (event->state != PERF_EVENT_STATE_ACTIVE)
1564 continue;
1565
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001566 if (event->cpu != -1 && event->cpu != smp_processor_id())
1567 continue;
1568
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569 hwc = &event->hw;
1570
1571 interrupts = hwc->interrupts;
1572 hwc->interrupts = 0;
1573
1574 /*
1575 * unthrottle events on the tick
1576 */
1577 if (interrupts == MAX_INTERRUPTS) {
1578 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001579 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 }
1581
1582 if (!event->attr.freq || !event->attr.sample_freq)
1583 continue;
1584
Peter Zijlstraabd50712010-01-26 18:50:16 +01001585 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001586 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001587 delta = now - hwc->freq_count_stamp;
1588 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589
Peter Zijlstraabd50712010-01-26 18:50:16 +01001590 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001591 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001592 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001593 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001594}
1595
1596/*
1597 * Round-robin a context's events:
1598 */
1599static void rotate_ctx(struct perf_event_context *ctx)
1600{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001601 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001602
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001603 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001604 list_rotate_left(&ctx->flexible_groups);
1605
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001606 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001607}
1608
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001609/*
1610 * Cannot race with ->pmu_rotate_start() because this is ran from hardirq
1611 * context, and ->pmu_rotate_start() is called with irqs disabled (both are
1612 * cpu affine, so there are no SMP races).
1613 */
1614static enum hrtimer_restart perf_event_context_tick(struct hrtimer *timer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001615{
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001616 enum hrtimer_restart restart = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001618 struct perf_event_context *ctx = NULL;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001619 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001621 cpuctx = container_of(timer, struct perf_cpu_context, timer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001622
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001623 if (cpuctx->ctx.nr_events) {
1624 restart = HRTIMER_RESTART;
1625 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1626 rotate = 1;
1627 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001629 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001630 if (ctx && ctx->nr_events) {
1631 restart = HRTIMER_RESTART;
1632 if (ctx->nr_events != ctx->nr_active)
1633 rotate = 1;
1634 }
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001635
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001636 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001637 perf_ctx_adjust_freq(&cpuctx->ctx, cpuctx->timer_interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638 if (ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001639 perf_ctx_adjust_freq(ctx, cpuctx->timer_interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001640
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001641 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001642 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001643
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001644 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001646 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647
1648 rotate_ctx(&cpuctx->ctx);
1649 if (ctx)
1650 rotate_ctx(ctx);
1651
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001652 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001654 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001655
1656done:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001657 perf_pmu_enable(cpuctx->ctx.pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001658 hrtimer_forward_now(timer, ns_to_ktime(cpuctx->timer_interval));
1659
1660 return restart;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661}
1662
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001663static int event_enable_on_exec(struct perf_event *event,
1664 struct perf_event_context *ctx)
1665{
1666 if (!event->attr.enable_on_exec)
1667 return 0;
1668
1669 event->attr.enable_on_exec = 0;
1670 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1671 return 0;
1672
1673 __perf_event_mark_enabled(event, ctx);
1674
1675 return 1;
1676}
1677
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678/*
1679 * Enable all of a task's events that have been marked enable-on-exec.
1680 * This expects task == current.
1681 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001682static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684 struct perf_event *event;
1685 unsigned long flags;
1686 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001687 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688
1689 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690 if (!ctx || !ctx->nr_events)
1691 goto out;
1692
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001693 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001694
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001695 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001697 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1698 ret = event_enable_on_exec(event, ctx);
1699 if (ret)
1700 enabled = 1;
1701 }
1702
1703 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1704 ret = event_enable_on_exec(event, ctx);
1705 if (ret)
1706 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001707 }
1708
1709 /*
1710 * Unclone this context if we enabled any event.
1711 */
1712 if (enabled)
1713 unclone_ctx(ctx);
1714
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001715 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001717 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001718out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719 local_irq_restore(flags);
1720}
1721
1722/*
1723 * Cross CPU call to read the hardware event
1724 */
1725static void __perf_event_read(void *info)
1726{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727 struct perf_event *event = info;
1728 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001729 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730
1731 /*
1732 * If this is a task context, we need to check whether it is
1733 * the current task context of this cpu. If not it has been
1734 * scheduled out before the smp call arrived. In that case
1735 * event->count would have been updated to a recent sample
1736 * when the event was scheduled out.
1737 */
1738 if (ctx->task && cpuctx->task_ctx != ctx)
1739 return;
1740
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001741 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001742 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001743 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001744 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001745
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001746 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747}
1748
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001749static inline u64 perf_event_count(struct perf_event *event)
1750{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001751 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001752}
1753
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001754static u64 perf_event_read(struct perf_event *event)
1755{
1756 /*
1757 * If event is enabled and currently active on a CPU, update the
1758 * value in the event structure:
1759 */
1760 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1761 smp_call_function_single(event->oncpu,
1762 __perf_event_read, event, 1);
1763 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001764 struct perf_event_context *ctx = event->ctx;
1765 unsigned long flags;
1766
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001767 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001768 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001770 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001771 }
1772
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001773 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774}
1775
1776/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001777 * Callchain support
1778 */
1779
1780struct callchain_cpus_entries {
1781 struct rcu_head rcu_head;
1782 struct perf_callchain_entry *cpu_entries[0];
1783};
1784
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001785static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001786static atomic_t nr_callchain_events;
1787static DEFINE_MUTEX(callchain_mutex);
1788struct callchain_cpus_entries *callchain_cpus_entries;
1789
1790
1791__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1792 struct pt_regs *regs)
1793{
1794}
1795
1796__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1797 struct pt_regs *regs)
1798{
1799}
1800
1801static void release_callchain_buffers_rcu(struct rcu_head *head)
1802{
1803 struct callchain_cpus_entries *entries;
1804 int cpu;
1805
1806 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1807
1808 for_each_possible_cpu(cpu)
1809 kfree(entries->cpu_entries[cpu]);
1810
1811 kfree(entries);
1812}
1813
1814static void release_callchain_buffers(void)
1815{
1816 struct callchain_cpus_entries *entries;
1817
1818 entries = callchain_cpus_entries;
1819 rcu_assign_pointer(callchain_cpus_entries, NULL);
1820 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1821}
1822
1823static int alloc_callchain_buffers(void)
1824{
1825 int cpu;
1826 int size;
1827 struct callchain_cpus_entries *entries;
1828
1829 /*
1830 * We can't use the percpu allocation API for data that can be
1831 * accessed from NMI. Use a temporary manual per cpu allocation
1832 * until that gets sorted out.
1833 */
1834 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1835 num_possible_cpus();
1836
1837 entries = kzalloc(size, GFP_KERNEL);
1838 if (!entries)
1839 return -ENOMEM;
1840
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001841 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001842
1843 for_each_possible_cpu(cpu) {
1844 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1845 cpu_to_node(cpu));
1846 if (!entries->cpu_entries[cpu])
1847 goto fail;
1848 }
1849
1850 rcu_assign_pointer(callchain_cpus_entries, entries);
1851
1852 return 0;
1853
1854fail:
1855 for_each_possible_cpu(cpu)
1856 kfree(entries->cpu_entries[cpu]);
1857 kfree(entries);
1858
1859 return -ENOMEM;
1860}
1861
1862static int get_callchain_buffers(void)
1863{
1864 int err = 0;
1865 int count;
1866
1867 mutex_lock(&callchain_mutex);
1868
1869 count = atomic_inc_return(&nr_callchain_events);
1870 if (WARN_ON_ONCE(count < 1)) {
1871 err = -EINVAL;
1872 goto exit;
1873 }
1874
1875 if (count > 1) {
1876 /* If the allocation failed, give up */
1877 if (!callchain_cpus_entries)
1878 err = -ENOMEM;
1879 goto exit;
1880 }
1881
1882 err = alloc_callchain_buffers();
1883 if (err)
1884 release_callchain_buffers();
1885exit:
1886 mutex_unlock(&callchain_mutex);
1887
1888 return err;
1889}
1890
1891static void put_callchain_buffers(void)
1892{
1893 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1894 release_callchain_buffers();
1895 mutex_unlock(&callchain_mutex);
1896 }
1897}
1898
1899static int get_recursion_context(int *recursion)
1900{
1901 int rctx;
1902
1903 if (in_nmi())
1904 rctx = 3;
1905 else if (in_irq())
1906 rctx = 2;
1907 else if (in_softirq())
1908 rctx = 1;
1909 else
1910 rctx = 0;
1911
1912 if (recursion[rctx])
1913 return -1;
1914
1915 recursion[rctx]++;
1916 barrier();
1917
1918 return rctx;
1919}
1920
1921static inline void put_recursion_context(int *recursion, int rctx)
1922{
1923 barrier();
1924 recursion[rctx]--;
1925}
1926
1927static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1928{
1929 int cpu;
1930 struct callchain_cpus_entries *entries;
1931
1932 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1933 if (*rctx == -1)
1934 return NULL;
1935
1936 entries = rcu_dereference(callchain_cpus_entries);
1937 if (!entries)
1938 return NULL;
1939
1940 cpu = smp_processor_id();
1941
1942 return &entries->cpu_entries[cpu][*rctx];
1943}
1944
1945static void
1946put_callchain_entry(int rctx)
1947{
1948 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1949}
1950
1951static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1952{
1953 int rctx;
1954 struct perf_callchain_entry *entry;
1955
1956
1957 entry = get_callchain_entry(&rctx);
1958 if (rctx == -1)
1959 return NULL;
1960
1961 if (!entry)
1962 goto exit_put;
1963
1964 entry->nr = 0;
1965
1966 if (!user_mode(regs)) {
1967 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1968 perf_callchain_kernel(entry, regs);
1969 if (current->mm)
1970 regs = task_pt_regs(current);
1971 else
1972 regs = NULL;
1973 }
1974
1975 if (regs) {
1976 perf_callchain_store(entry, PERF_CONTEXT_USER);
1977 perf_callchain_user(entry, regs);
1978 }
1979
1980exit_put:
1981 put_callchain_entry(rctx);
1982
1983 return entry;
1984}
1985
1986/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001987 * Initialize the perf_event context in a task_struct:
1988 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02001989static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001991 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001992 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001993 INIT_LIST_HEAD(&ctx->pinned_groups);
1994 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995 INIT_LIST_HEAD(&ctx->event_list);
1996 atomic_set(&ctx->refcount, 1);
Peter Zijlstraeb184472010-09-07 15:55:13 +02001997}
1998
1999static struct perf_event_context *
2000alloc_perf_context(struct pmu *pmu, struct task_struct *task)
2001{
2002 struct perf_event_context *ctx;
2003
2004 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2005 if (!ctx)
2006 return NULL;
2007
2008 __perf_event_init_context(ctx);
2009 if (task) {
2010 ctx->task = task;
2011 get_task_struct(task);
2012 }
2013 ctx->pmu = pmu;
2014
2015 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016}
2017
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002018static struct task_struct *
2019find_lively_task_by_vpid(pid_t vpid)
2020{
2021 struct task_struct *task;
2022 int err;
2023
2024 rcu_read_lock();
2025 if (!vpid)
2026 task = current;
2027 else
2028 task = find_task_by_vpid(vpid);
2029 if (task)
2030 get_task_struct(task);
2031 rcu_read_unlock();
2032
2033 if (!task)
2034 return ERR_PTR(-ESRCH);
2035
2036 /*
2037 * Can't attach events to a dying task.
2038 */
2039 err = -ESRCH;
2040 if (task->flags & PF_EXITING)
2041 goto errout;
2042
2043 /* Reuse ptrace permission checks for now. */
2044 err = -EACCES;
2045 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2046 goto errout;
2047
2048 return task;
2049errout:
2050 put_task_struct(task);
2051 return ERR_PTR(err);
2052
2053}
2054
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002055static struct perf_event_context *
2056find_get_context(struct pmu *pmu, pid_t pid, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002057{
2058 struct perf_event_context *ctx;
2059 struct perf_cpu_context *cpuctx;
2060 struct task_struct *task;
2061 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002062 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002064 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 /* Must be root to operate on a CPU event: */
2066 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2067 return ERR_PTR(-EACCES);
2068
Paul Mackerras0f624e72009-12-15 19:40:32 +11002069 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002070 return ERR_PTR(-EINVAL);
2071
2072 /*
2073 * We could be clever and allow to attach a event to an
2074 * offline CPU and activate it when the CPU comes up, but
2075 * that's for later.
2076 */
Rusty Russellf6325e32009-12-17 11:43:08 -06002077 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078 return ERR_PTR(-ENODEV);
2079
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002080 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002081 ctx = &cpuctx->ctx;
2082 get_ctx(ctx);
2083
2084 return ctx;
2085 }
2086
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002087 task = find_lively_task_by_vpid(pid);
2088 if (IS_ERR(task))
2089 return (void*)task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002090
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002091 err = -EINVAL;
2092 ctxn = pmu->task_ctx_nr;
2093 if (ctxn < 0)
2094 goto errout;
2095
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002096retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002097 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 if (ctx) {
2099 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002100 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101 }
2102
2103 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002104 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105 err = -ENOMEM;
2106 if (!ctx)
2107 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002108
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002109 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002110
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002111 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002112 /*
2113 * We raced with some other task; use
2114 * the context they set.
2115 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002116 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117 kfree(ctx);
2118 goto retry;
2119 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002120 }
2121
2122 put_task_struct(task);
2123 return ctx;
2124
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002125errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002126 put_task_struct(task);
2127 return ERR_PTR(err);
2128}
2129
Li Zefan6fb29152009-10-15 11:21:42 +08002130static void perf_event_free_filter(struct perf_event *event);
2131
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002132static void free_event_rcu(struct rcu_head *head)
2133{
2134 struct perf_event *event;
2135
2136 event = container_of(head, struct perf_event, rcu_head);
2137 if (event->ns)
2138 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002139 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140 kfree(event);
2141}
2142
2143static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002144static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145
2146static void free_event(struct perf_event *event)
2147{
2148 perf_pending_sync(event);
2149
2150 if (!event->parent) {
2151 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002152 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002153 atomic_dec(&nr_mmap_events);
2154 if (event->attr.comm)
2155 atomic_dec(&nr_comm_events);
2156 if (event->attr.task)
2157 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002158 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2159 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002160 }
2161
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002162 if (event->buffer) {
2163 perf_buffer_put(event->buffer);
2164 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002165 }
2166
2167 if (event->destroy)
2168 event->destroy(event);
2169
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002170 if (event->ctx)
2171 put_ctx(event->ctx);
2172
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002173 call_rcu(&event->rcu_head, free_event_rcu);
2174}
2175
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002176int perf_event_release_kernel(struct perf_event *event)
2177{
2178 struct perf_event_context *ctx = event->ctx;
2179
Peter Zijlstra050735b2010-05-11 11:51:53 +02002180 /*
2181 * Remove from the PMU, can't get re-enabled since we got
2182 * here because the last ref went.
2183 */
2184 perf_event_disable(event);
2185
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002186 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002187 /*
2188 * There are two ways this annotation is useful:
2189 *
2190 * 1) there is a lock recursion from perf_event_exit_task
2191 * see the comment there.
2192 *
2193 * 2) there is a lock-inversion with mmap_sem through
2194 * perf_event_read_group(), which takes faults while
2195 * holding ctx->mutex, however this is called after
2196 * the last filedesc died, so there is no possibility
2197 * to trigger the AB-BA case.
2198 */
2199 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002200 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002201 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002202 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002203 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002204 mutex_unlock(&ctx->mutex);
2205
2206 mutex_lock(&event->owner->perf_event_mutex);
2207 list_del_init(&event->owner_entry);
2208 mutex_unlock(&event->owner->perf_event_mutex);
2209 put_task_struct(event->owner);
2210
2211 free_event(event);
2212
2213 return 0;
2214}
2215EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2216
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002217/*
2218 * Called when the last reference to the file is gone.
2219 */
2220static int perf_release(struct inode *inode, struct file *file)
2221{
2222 struct perf_event *event = file->private_data;
2223
2224 file->private_data = NULL;
2225
2226 return perf_event_release_kernel(event);
2227}
2228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229static int perf_event_read_size(struct perf_event *event)
2230{
2231 int entry = sizeof(u64); /* value */
2232 int size = 0;
2233 int nr = 1;
2234
2235 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2236 size += sizeof(u64);
2237
2238 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2239 size += sizeof(u64);
2240
2241 if (event->attr.read_format & PERF_FORMAT_ID)
2242 entry += sizeof(u64);
2243
2244 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2245 nr += event->group_leader->nr_siblings;
2246 size += sizeof(u64);
2247 }
2248
2249 size += entry * nr;
2250
2251 return size;
2252}
2253
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002254u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002255{
2256 struct perf_event *child;
2257 u64 total = 0;
2258
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002259 *enabled = 0;
2260 *running = 0;
2261
Peter Zijlstra6f105812009-11-20 22:19:56 +01002262 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002263 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002264 *enabled += event->total_time_enabled +
2265 atomic64_read(&event->child_total_time_enabled);
2266 *running += event->total_time_running +
2267 atomic64_read(&event->child_total_time_running);
2268
2269 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002271 *enabled += child->total_time_enabled;
2272 *running += child->total_time_running;
2273 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002274 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275
2276 return total;
2277}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002278EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280static int perf_event_read_group(struct perf_event *event,
2281 u64 read_format, char __user *buf)
2282{
2283 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002284 int n = 0, size = 0, ret = -EFAULT;
2285 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002286 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002287 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002288
Peter Zijlstra6f105812009-11-20 22:19:56 +01002289 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002290 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291
2292 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002293 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2294 values[n++] = enabled;
2295 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2296 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002297 values[n++] = count;
2298 if (read_format & PERF_FORMAT_ID)
2299 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002300
2301 size = n * sizeof(u64);
2302
2303 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002304 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002305
Peter Zijlstra6f105812009-11-20 22:19:56 +01002306 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307
2308 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002309 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002310
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002311 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002312 if (read_format & PERF_FORMAT_ID)
2313 values[n++] = primary_event_id(sub);
2314
2315 size = n * sizeof(u64);
2316
Stephane Eranian184d3da2009-11-23 21:40:49 -08002317 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002318 ret = -EFAULT;
2319 goto unlock;
2320 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002321
2322 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002324unlock:
2325 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326
Peter Zijlstraabf48682009-11-20 22:19:49 +01002327 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002328}
2329
2330static int perf_event_read_one(struct perf_event *event,
2331 u64 read_format, char __user *buf)
2332{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002333 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334 u64 values[4];
2335 int n = 0;
2336
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002337 values[n++] = perf_event_read_value(event, &enabled, &running);
2338 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2339 values[n++] = enabled;
2340 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2341 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002342 if (read_format & PERF_FORMAT_ID)
2343 values[n++] = primary_event_id(event);
2344
2345 if (copy_to_user(buf, values, n * sizeof(u64)))
2346 return -EFAULT;
2347
2348 return n * sizeof(u64);
2349}
2350
2351/*
2352 * Read the performance event - simple non blocking version for now
2353 */
2354static ssize_t
2355perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2356{
2357 u64 read_format = event->attr.read_format;
2358 int ret;
2359
2360 /*
2361 * Return end-of-file for a read on a event that is in
2362 * error state (i.e. because it was pinned but it couldn't be
2363 * scheduled on to the CPU at some point).
2364 */
2365 if (event->state == PERF_EVENT_STATE_ERROR)
2366 return 0;
2367
2368 if (count < perf_event_read_size(event))
2369 return -ENOSPC;
2370
2371 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372 if (read_format & PERF_FORMAT_GROUP)
2373 ret = perf_event_read_group(event, read_format, buf);
2374 else
2375 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002376
2377 return ret;
2378}
2379
2380static ssize_t
2381perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2382{
2383 struct perf_event *event = file->private_data;
2384
2385 return perf_read_hw(event, buf, count);
2386}
2387
2388static unsigned int perf_poll(struct file *file, poll_table *wait)
2389{
2390 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002391 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392 unsigned int events = POLL_HUP;
2393
2394 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002395 buffer = rcu_dereference(event->buffer);
2396 if (buffer)
2397 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002398 rcu_read_unlock();
2399
2400 poll_wait(file, &event->waitq, wait);
2401
2402 return events;
2403}
2404
2405static void perf_event_reset(struct perf_event *event)
2406{
2407 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002408 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002409 perf_event_update_userpage(event);
2410}
2411
2412/*
2413 * Holding the top-level event's child_mutex means that any
2414 * descendant process that has inherited this event will block
2415 * in sync_child_event if it goes to exit, thus satisfying the
2416 * task existence requirements of perf_event_enable/disable.
2417 */
2418static void perf_event_for_each_child(struct perf_event *event,
2419 void (*func)(struct perf_event *))
2420{
2421 struct perf_event *child;
2422
2423 WARN_ON_ONCE(event->ctx->parent_ctx);
2424 mutex_lock(&event->child_mutex);
2425 func(event);
2426 list_for_each_entry(child, &event->child_list, child_list)
2427 func(child);
2428 mutex_unlock(&event->child_mutex);
2429}
2430
2431static void perf_event_for_each(struct perf_event *event,
2432 void (*func)(struct perf_event *))
2433{
2434 struct perf_event_context *ctx = event->ctx;
2435 struct perf_event *sibling;
2436
2437 WARN_ON_ONCE(ctx->parent_ctx);
2438 mutex_lock(&ctx->mutex);
2439 event = event->group_leader;
2440
2441 perf_event_for_each_child(event, func);
2442 func(event);
2443 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2444 perf_event_for_each_child(event, func);
2445 mutex_unlock(&ctx->mutex);
2446}
2447
2448static int perf_event_period(struct perf_event *event, u64 __user *arg)
2449{
2450 struct perf_event_context *ctx = event->ctx;
2451 unsigned long size;
2452 int ret = 0;
2453 u64 value;
2454
2455 if (!event->attr.sample_period)
2456 return -EINVAL;
2457
2458 size = copy_from_user(&value, arg, sizeof(value));
2459 if (size != sizeof(value))
2460 return -EFAULT;
2461
2462 if (!value)
2463 return -EINVAL;
2464
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002465 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002466 if (event->attr.freq) {
2467 if (value > sysctl_perf_event_sample_rate) {
2468 ret = -EINVAL;
2469 goto unlock;
2470 }
2471
2472 event->attr.sample_freq = value;
2473 } else {
2474 event->attr.sample_period = value;
2475 event->hw.sample_period = value;
2476 }
2477unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002478 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479
2480 return ret;
2481}
2482
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002483static const struct file_operations perf_fops;
2484
2485static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2486{
2487 struct file *file;
2488
2489 file = fget_light(fd, fput_needed);
2490 if (!file)
2491 return ERR_PTR(-EBADF);
2492
2493 if (file->f_op != &perf_fops) {
2494 fput_light(file, *fput_needed);
2495 *fput_needed = 0;
2496 return ERR_PTR(-EBADF);
2497 }
2498
2499 return file->private_data;
2500}
2501
2502static int perf_event_set_output(struct perf_event *event,
2503 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002504static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505
2506static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2507{
2508 struct perf_event *event = file->private_data;
2509 void (*func)(struct perf_event *);
2510 u32 flags = arg;
2511
2512 switch (cmd) {
2513 case PERF_EVENT_IOC_ENABLE:
2514 func = perf_event_enable;
2515 break;
2516 case PERF_EVENT_IOC_DISABLE:
2517 func = perf_event_disable;
2518 break;
2519 case PERF_EVENT_IOC_RESET:
2520 func = perf_event_reset;
2521 break;
2522
2523 case PERF_EVENT_IOC_REFRESH:
2524 return perf_event_refresh(event, arg);
2525
2526 case PERF_EVENT_IOC_PERIOD:
2527 return perf_event_period(event, (u64 __user *)arg);
2528
2529 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002530 {
2531 struct perf_event *output_event = NULL;
2532 int fput_needed = 0;
2533 int ret;
2534
2535 if (arg != -1) {
2536 output_event = perf_fget_light(arg, &fput_needed);
2537 if (IS_ERR(output_event))
2538 return PTR_ERR(output_event);
2539 }
2540
2541 ret = perf_event_set_output(event, output_event);
2542 if (output_event)
2543 fput_light(output_event->filp, fput_needed);
2544
2545 return ret;
2546 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002547
Li Zefan6fb29152009-10-15 11:21:42 +08002548 case PERF_EVENT_IOC_SET_FILTER:
2549 return perf_event_set_filter(event, (void __user *)arg);
2550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002551 default:
2552 return -ENOTTY;
2553 }
2554
2555 if (flags & PERF_IOC_FLAG_GROUP)
2556 perf_event_for_each(event, func);
2557 else
2558 perf_event_for_each_child(event, func);
2559
2560 return 0;
2561}
2562
2563int perf_event_task_enable(void)
2564{
2565 struct perf_event *event;
2566
2567 mutex_lock(&current->perf_event_mutex);
2568 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2569 perf_event_for_each_child(event, perf_event_enable);
2570 mutex_unlock(&current->perf_event_mutex);
2571
2572 return 0;
2573}
2574
2575int perf_event_task_disable(void)
2576{
2577 struct perf_event *event;
2578
2579 mutex_lock(&current->perf_event_mutex);
2580 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2581 perf_event_for_each_child(event, perf_event_disable);
2582 mutex_unlock(&current->perf_event_mutex);
2583
2584 return 0;
2585}
2586
2587#ifndef PERF_EVENT_INDEX_OFFSET
2588# define PERF_EVENT_INDEX_OFFSET 0
2589#endif
2590
2591static int perf_event_index(struct perf_event *event)
2592{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002593 if (event->hw.state & PERF_HES_STOPPED)
2594 return 0;
2595
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002596 if (event->state != PERF_EVENT_STATE_ACTIVE)
2597 return 0;
2598
2599 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2600}
2601
2602/*
2603 * Callers need to ensure there can be no nesting of this function, otherwise
2604 * the seqlock logic goes bad. We can not serialize this because the arch
2605 * code calls this from NMI context.
2606 */
2607void perf_event_update_userpage(struct perf_event *event)
2608{
2609 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002610 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002611
2612 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002613 buffer = rcu_dereference(event->buffer);
2614 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002615 goto unlock;
2616
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002617 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002618
2619 /*
2620 * Disable preemption so as to not let the corresponding user-space
2621 * spin too long if we get preempted.
2622 */
2623 preempt_disable();
2624 ++userpg->lock;
2625 barrier();
2626 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002627 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002628 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002629 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002630
2631 userpg->time_enabled = event->total_time_enabled +
2632 atomic64_read(&event->child_total_time_enabled);
2633
2634 userpg->time_running = event->total_time_running +
2635 atomic64_read(&event->child_total_time_running);
2636
2637 barrier();
2638 ++userpg->lock;
2639 preempt_enable();
2640unlock:
2641 rcu_read_unlock();
2642}
2643
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002644static unsigned long perf_data_size(struct perf_buffer *buffer);
2645
2646static void
2647perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2648{
2649 long max_size = perf_data_size(buffer);
2650
2651 if (watermark)
2652 buffer->watermark = min(max_size, watermark);
2653
2654 if (!buffer->watermark)
2655 buffer->watermark = max_size / 2;
2656
2657 if (flags & PERF_BUFFER_WRITABLE)
2658 buffer->writable = 1;
2659
2660 atomic_set(&buffer->refcount, 1);
2661}
2662
Peter Zijlstra906010b2009-09-21 16:08:49 +02002663#ifndef CONFIG_PERF_USE_VMALLOC
2664
2665/*
2666 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2667 */
2668
2669static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002670perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002671{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002672 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002673 return NULL;
2674
2675 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002676 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002677
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002678 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002679}
2680
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002681static void *perf_mmap_alloc_page(int cpu)
2682{
2683 struct page *page;
2684 int node;
2685
2686 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2687 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2688 if (!page)
2689 return NULL;
2690
2691 return page_address(page);
2692}
2693
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002694static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002695perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002697 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002698 unsigned long size;
2699 int i;
2700
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002701 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002702 size += nr_pages * sizeof(void *);
2703
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002704 buffer = kzalloc(size, GFP_KERNEL);
2705 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002706 goto fail;
2707
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002708 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002709 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002710 goto fail_user_page;
2711
2712 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002713 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002714 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002715 goto fail_data_pages;
2716 }
2717
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002718 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002720 perf_buffer_init(buffer, watermark, flags);
2721
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002722 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002723
2724fail_data_pages:
2725 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002726 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002727
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002728 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729
2730fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002731 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002732
2733fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002734 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735}
2736
2737static void perf_mmap_free_page(unsigned long addr)
2738{
2739 struct page *page = virt_to_page((void *)addr);
2740
2741 page->mapping = NULL;
2742 __free_page(page);
2743}
2744
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002745static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002746{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747 int i;
2748
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002749 perf_mmap_free_page((unsigned long)buffer->user_page);
2750 for (i = 0; i < buffer->nr_pages; i++)
2751 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2752 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002753}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002755static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002756{
2757 return 0;
2758}
2759
Peter Zijlstra906010b2009-09-21 16:08:49 +02002760#else
2761
2762/*
2763 * Back perf_mmap() with vmalloc memory.
2764 *
2765 * Required for architectures that have d-cache aliasing issues.
2766 */
2767
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002768static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002769{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002770 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002771}
2772
Peter Zijlstra906010b2009-09-21 16:08:49 +02002773static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002774perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002775{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002776 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002777 return NULL;
2778
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002779 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002780}
2781
2782static void perf_mmap_unmark_page(void *addr)
2783{
2784 struct page *page = vmalloc_to_page(addr);
2785
2786 page->mapping = NULL;
2787}
2788
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002789static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002790{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002791 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002792 void *base;
2793 int i, nr;
2794
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002795 buffer = container_of(work, struct perf_buffer, work);
2796 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002797
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002798 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002799 for (i = 0; i < nr + 1; i++)
2800 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2801
2802 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002803 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002804}
2805
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002806static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002807{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002808 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002809}
2810
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002811static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002812perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002813{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002814 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002815 unsigned long size;
2816 void *all_buf;
2817
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002818 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002819 size += sizeof(void *);
2820
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002821 buffer = kzalloc(size, GFP_KERNEL);
2822 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002823 goto fail;
2824
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002825 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002826
2827 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2828 if (!all_buf)
2829 goto fail_all_buf;
2830
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002831 buffer->user_page = all_buf;
2832 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2833 buffer->page_order = ilog2(nr_pages);
2834 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002835
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002836 perf_buffer_init(buffer, watermark, flags);
2837
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002838 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002839
2840fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002841 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002842
2843fail:
2844 return NULL;
2845}
2846
2847#endif
2848
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002849static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002850{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002851 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002852}
2853
Peter Zijlstra906010b2009-09-21 16:08:49 +02002854static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2855{
2856 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002857 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002858 int ret = VM_FAULT_SIGBUS;
2859
2860 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2861 if (vmf->pgoff == 0)
2862 ret = 0;
2863 return ret;
2864 }
2865
2866 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002867 buffer = rcu_dereference(event->buffer);
2868 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002869 goto unlock;
2870
2871 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2872 goto unlock;
2873
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002874 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002875 if (!vmf->page)
2876 goto unlock;
2877
2878 get_page(vmf->page);
2879 vmf->page->mapping = vma->vm_file->f_mapping;
2880 vmf->page->index = vmf->pgoff;
2881
2882 ret = 0;
2883unlock:
2884 rcu_read_unlock();
2885
2886 return ret;
2887}
2888
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002889static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002890{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002891 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002892
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002893 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2894 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002895}
2896
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002897static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002899 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002900
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002901 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002902 buffer = rcu_dereference(event->buffer);
2903 if (buffer) {
2904 if (!atomic_inc_not_zero(&buffer->refcount))
2905 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002906 }
2907 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002908
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002909 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002910}
2911
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002912static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002913{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002914 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002915 return;
2916
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002917 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002918}
2919
2920static void perf_mmap_open(struct vm_area_struct *vma)
2921{
2922 struct perf_event *event = vma->vm_file->private_data;
2923
2924 atomic_inc(&event->mmap_count);
2925}
2926
2927static void perf_mmap_close(struct vm_area_struct *vma)
2928{
2929 struct perf_event *event = vma->vm_file->private_data;
2930
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002931 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002932 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002933 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002934 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935
Peter Zijlstra906010b2009-09-21 16:08:49 +02002936 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002937 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002938 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002939 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002940
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002941 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002942 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002943 }
2944}
2945
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002946static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947 .open = perf_mmap_open,
2948 .close = perf_mmap_close,
2949 .fault = perf_mmap_fault,
2950 .page_mkwrite = perf_mmap_fault,
2951};
2952
2953static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2954{
2955 struct perf_event *event = file->private_data;
2956 unsigned long user_locked, user_lock_limit;
2957 struct user_struct *user = current_user();
2958 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002959 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002960 unsigned long vma_size;
2961 unsigned long nr_pages;
2962 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002963 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002964
Peter Zijlstrac7920612010-05-18 10:33:24 +02002965 /*
2966 * Don't allow mmap() of inherited per-task counters. This would
2967 * create a performance issue due to all children writing to the
2968 * same buffer.
2969 */
2970 if (event->cpu == -1 && event->attr.inherit)
2971 return -EINVAL;
2972
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002973 if (!(vma->vm_flags & VM_SHARED))
2974 return -EINVAL;
2975
2976 vma_size = vma->vm_end - vma->vm_start;
2977 nr_pages = (vma_size / PAGE_SIZE) - 1;
2978
2979 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002980 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002981 * can do bitmasks instead of modulo.
2982 */
2983 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2984 return -EINVAL;
2985
2986 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2987 return -EINVAL;
2988
2989 if (vma->vm_pgoff != 0)
2990 return -EINVAL;
2991
2992 WARN_ON_ONCE(event->ctx->parent_ctx);
2993 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002994 if (event->buffer) {
2995 if (event->buffer->nr_pages == nr_pages)
2996 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002997 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998 ret = -EINVAL;
2999 goto unlock;
3000 }
3001
3002 user_extra = nr_pages + 1;
3003 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3004
3005 /*
3006 * Increase the limit linearly with more CPUs:
3007 */
3008 user_lock_limit *= num_online_cpus();
3009
3010 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3011
3012 extra = 0;
3013 if (user_locked > user_lock_limit)
3014 extra = user_locked - user_lock_limit;
3015
Jiri Slaby78d7d402010-03-05 13:42:54 -08003016 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017 lock_limit >>= PAGE_SHIFT;
3018 locked = vma->vm_mm->locked_vm + extra;
3019
3020 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3021 !capable(CAP_IPC_LOCK)) {
3022 ret = -EPERM;
3023 goto unlock;
3024 }
3025
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003026 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003027
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003028 if (vma->vm_flags & VM_WRITE)
3029 flags |= PERF_BUFFER_WRITABLE;
3030
3031 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3032 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003033 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003034 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003035 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003036 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003037 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003038
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003039 atomic_long_add(user_extra, &user->locked_vm);
3040 event->mmap_locked = extra;
3041 event->mmap_user = get_current_user();
3042 vma->vm_mm->locked_vm += event->mmap_locked;
3043
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003045 if (!ret)
3046 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047 mutex_unlock(&event->mmap_mutex);
3048
3049 vma->vm_flags |= VM_RESERVED;
3050 vma->vm_ops = &perf_mmap_vmops;
3051
3052 return ret;
3053}
3054
3055static int perf_fasync(int fd, struct file *filp, int on)
3056{
3057 struct inode *inode = filp->f_path.dentry->d_inode;
3058 struct perf_event *event = filp->private_data;
3059 int retval;
3060
3061 mutex_lock(&inode->i_mutex);
3062 retval = fasync_helper(fd, filp, on, &event->fasync);
3063 mutex_unlock(&inode->i_mutex);
3064
3065 if (retval < 0)
3066 return retval;
3067
3068 return 0;
3069}
3070
3071static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003072 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003073 .release = perf_release,
3074 .read = perf_read,
3075 .poll = perf_poll,
3076 .unlocked_ioctl = perf_ioctl,
3077 .compat_ioctl = perf_ioctl,
3078 .mmap = perf_mmap,
3079 .fasync = perf_fasync,
3080};
3081
3082/*
3083 * Perf event wakeup
3084 *
3085 * If there's data, ensure we set the poll() state and publish everything
3086 * to user-space before waking everybody up.
3087 */
3088
3089void perf_event_wakeup(struct perf_event *event)
3090{
3091 wake_up_all(&event->waitq);
3092
3093 if (event->pending_kill) {
3094 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3095 event->pending_kill = 0;
3096 }
3097}
3098
3099/*
3100 * Pending wakeups
3101 *
3102 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3103 *
3104 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3105 * single linked list and use cmpxchg() to add entries lockless.
3106 */
3107
3108static void perf_pending_event(struct perf_pending_entry *entry)
3109{
3110 struct perf_event *event = container_of(entry,
3111 struct perf_event, pending);
3112
3113 if (event->pending_disable) {
3114 event->pending_disable = 0;
3115 __perf_event_disable(event);
3116 }
3117
3118 if (event->pending_wakeup) {
3119 event->pending_wakeup = 0;
3120 perf_event_wakeup(event);
3121 }
3122}
3123
3124#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3125
3126static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3127 PENDING_TAIL,
3128};
3129
3130static void perf_pending_queue(struct perf_pending_entry *entry,
3131 void (*func)(struct perf_pending_entry *))
3132{
3133 struct perf_pending_entry **head;
3134
3135 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3136 return;
3137
3138 entry->func = func;
3139
3140 head = &get_cpu_var(perf_pending_head);
3141
3142 do {
3143 entry->next = *head;
3144 } while (cmpxchg(head, entry->next, entry) != entry->next);
3145
3146 set_perf_event_pending();
3147
3148 put_cpu_var(perf_pending_head);
3149}
3150
3151static int __perf_pending_run(void)
3152{
3153 struct perf_pending_entry *list;
3154 int nr = 0;
3155
3156 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3157 while (list != PENDING_TAIL) {
3158 void (*func)(struct perf_pending_entry *);
3159 struct perf_pending_entry *entry = list;
3160
3161 list = list->next;
3162
3163 func = entry->func;
3164 entry->next = NULL;
3165 /*
3166 * Ensure we observe the unqueue before we issue the wakeup,
3167 * so that we won't be waiting forever.
3168 * -- see perf_not_pending().
3169 */
3170 smp_wmb();
3171
3172 func(entry);
3173 nr++;
3174 }
3175
3176 return nr;
3177}
3178
3179static inline int perf_not_pending(struct perf_event *event)
3180{
3181 /*
3182 * If we flush on whatever cpu we run, there is a chance we don't
3183 * need to wait.
3184 */
3185 get_cpu();
3186 __perf_pending_run();
3187 put_cpu();
3188
3189 /*
3190 * Ensure we see the proper queue state before going to sleep
3191 * so that we do not miss the wakeup. -- see perf_pending_handle()
3192 */
3193 smp_rmb();
3194 return event->pending.next == NULL;
3195}
3196
3197static void perf_pending_sync(struct perf_event *event)
3198{
3199 wait_event(event->waitq, perf_not_pending(event));
3200}
3201
3202void perf_event_do_pending(void)
3203{
3204 __perf_pending_run();
3205}
3206
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003208 * We assume there is only KVM supporting the callbacks.
3209 * Later on, we might change it to a list if there is
3210 * another virtualization implementation supporting the callbacks.
3211 */
3212struct perf_guest_info_callbacks *perf_guest_cbs;
3213
3214int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3215{
3216 perf_guest_cbs = cbs;
3217 return 0;
3218}
3219EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3220
3221int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3222{
3223 perf_guest_cbs = NULL;
3224 return 0;
3225}
3226EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3227
3228/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003229 * Output
3230 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003231static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232 unsigned long offset, unsigned long head)
3233{
3234 unsigned long mask;
3235
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003236 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003237 return true;
3238
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003239 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003240
3241 offset = (offset - tail) & mask;
3242 head = (head - tail) & mask;
3243
3244 if ((int)(head - offset) < 0)
3245 return false;
3246
3247 return true;
3248}
3249
3250static void perf_output_wakeup(struct perf_output_handle *handle)
3251{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003252 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253
3254 if (handle->nmi) {
3255 handle->event->pending_wakeup = 1;
3256 perf_pending_queue(&handle->event->pending,
3257 perf_pending_event);
3258 } else
3259 perf_event_wakeup(handle->event);
3260}
3261
3262/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003263 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003264 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003265 * cannot fully serialize things.
3266 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003267 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003268 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003269 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003270static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003271{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003272 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273
Peter Zijlstraef607772010-05-18 10:50:41 +02003274 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003275 local_inc(&buffer->nest);
3276 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277}
3278
Peter Zijlstraef607772010-05-18 10:50:41 +02003279static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003281 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003282 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283
3284again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003285 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286
3287 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003288 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289 */
3290
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003291 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003292 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293
3294 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003295 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003296 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003297 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003298 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003299 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003300
Peter Zijlstraef607772010-05-18 10:50:41 +02003301 /*
3302 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003303 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003304 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003305 if (unlikely(head != local_read(&buffer->head))) {
3306 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003307 goto again;
3308 }
3309
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003310 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003311 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003312
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003313out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003314 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315}
3316
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003317__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318 const void *buf, unsigned int len)
3319{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003320 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003321 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003322
3323 memcpy(handle->addr, buf, size);
3324
3325 len -= size;
3326 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003327 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003328 handle->size -= size;
3329 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003330 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003331
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003332 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003333 handle->page &= buffer->nr_pages - 1;
3334 handle->addr = buffer->data_pages[handle->page];
3335 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003336 }
3337 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338}
3339
3340int perf_output_begin(struct perf_output_handle *handle,
3341 struct perf_event *event, unsigned int size,
3342 int nmi, int sample)
3343{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003344 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345 unsigned long tail, offset, head;
3346 int have_lost;
3347 struct {
3348 struct perf_event_header header;
3349 u64 id;
3350 u64 lost;
3351 } lost_event;
3352
3353 rcu_read_lock();
3354 /*
3355 * For inherited events we send all the output towards the parent.
3356 */
3357 if (event->parent)
3358 event = event->parent;
3359
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003360 buffer = rcu_dereference(event->buffer);
3361 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003362 goto out;
3363
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003364 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365 handle->event = event;
3366 handle->nmi = nmi;
3367 handle->sample = sample;
3368
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003369 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003370 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003372 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003373 if (have_lost)
3374 size += sizeof(lost_event);
3375
Peter Zijlstraef607772010-05-18 10:50:41 +02003376 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377
3378 do {
3379 /*
3380 * Userspace could choose to issue a mb() before updating the
3381 * tail pointer. So that all reads will be completed before the
3382 * write is issued.
3383 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003384 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003385 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003386 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003388 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003390 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003392 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3393 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003394
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003395 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3396 handle->page &= buffer->nr_pages - 1;
3397 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3398 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003399 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003400 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 if (have_lost) {
3403 lost_event.header.type = PERF_RECORD_LOST;
3404 lost_event.header.misc = 0;
3405 lost_event.header.size = sizeof(lost_event);
3406 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003407 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003408
3409 perf_output_put(handle, lost_event);
3410 }
3411
3412 return 0;
3413
3414fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003415 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003416 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003417out:
3418 rcu_read_unlock();
3419
3420 return -ENOSPC;
3421}
3422
3423void perf_output_end(struct perf_output_handle *handle)
3424{
3425 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003426 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427
3428 int wakeup_events = event->attr.wakeup_events;
3429
3430 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003431 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003432 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003433 local_sub(wakeup_events, &buffer->events);
3434 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003435 }
3436 }
3437
Peter Zijlstraef607772010-05-18 10:50:41 +02003438 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003439 rcu_read_unlock();
3440}
3441
3442static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3443{
3444 /*
3445 * only top level events have the pid namespace they were created in
3446 */
3447 if (event->parent)
3448 event = event->parent;
3449
3450 return task_tgid_nr_ns(p, event->ns);
3451}
3452
3453static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3454{
3455 /*
3456 * only top level events have the pid namespace they were created in
3457 */
3458 if (event->parent)
3459 event = event->parent;
3460
3461 return task_pid_nr_ns(p, event->ns);
3462}
3463
3464static void perf_output_read_one(struct perf_output_handle *handle,
3465 struct perf_event *event)
3466{
3467 u64 read_format = event->attr.read_format;
3468 u64 values[4];
3469 int n = 0;
3470
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003471 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003472 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3473 values[n++] = event->total_time_enabled +
3474 atomic64_read(&event->child_total_time_enabled);
3475 }
3476 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3477 values[n++] = event->total_time_running +
3478 atomic64_read(&event->child_total_time_running);
3479 }
3480 if (read_format & PERF_FORMAT_ID)
3481 values[n++] = primary_event_id(event);
3482
3483 perf_output_copy(handle, values, n * sizeof(u64));
3484}
3485
3486/*
3487 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3488 */
3489static void perf_output_read_group(struct perf_output_handle *handle,
3490 struct perf_event *event)
3491{
3492 struct perf_event *leader = event->group_leader, *sub;
3493 u64 read_format = event->attr.read_format;
3494 u64 values[5];
3495 int n = 0;
3496
3497 values[n++] = 1 + leader->nr_siblings;
3498
3499 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3500 values[n++] = leader->total_time_enabled;
3501
3502 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3503 values[n++] = leader->total_time_running;
3504
3505 if (leader != event)
3506 leader->pmu->read(leader);
3507
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003508 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003509 if (read_format & PERF_FORMAT_ID)
3510 values[n++] = primary_event_id(leader);
3511
3512 perf_output_copy(handle, values, n * sizeof(u64));
3513
3514 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3515 n = 0;
3516
3517 if (sub != event)
3518 sub->pmu->read(sub);
3519
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003520 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003521 if (read_format & PERF_FORMAT_ID)
3522 values[n++] = primary_event_id(sub);
3523
3524 perf_output_copy(handle, values, n * sizeof(u64));
3525 }
3526}
3527
3528static void perf_output_read(struct perf_output_handle *handle,
3529 struct perf_event *event)
3530{
3531 if (event->attr.read_format & PERF_FORMAT_GROUP)
3532 perf_output_read_group(handle, event);
3533 else
3534 perf_output_read_one(handle, event);
3535}
3536
3537void perf_output_sample(struct perf_output_handle *handle,
3538 struct perf_event_header *header,
3539 struct perf_sample_data *data,
3540 struct perf_event *event)
3541{
3542 u64 sample_type = data->type;
3543
3544 perf_output_put(handle, *header);
3545
3546 if (sample_type & PERF_SAMPLE_IP)
3547 perf_output_put(handle, data->ip);
3548
3549 if (sample_type & PERF_SAMPLE_TID)
3550 perf_output_put(handle, data->tid_entry);
3551
3552 if (sample_type & PERF_SAMPLE_TIME)
3553 perf_output_put(handle, data->time);
3554
3555 if (sample_type & PERF_SAMPLE_ADDR)
3556 perf_output_put(handle, data->addr);
3557
3558 if (sample_type & PERF_SAMPLE_ID)
3559 perf_output_put(handle, data->id);
3560
3561 if (sample_type & PERF_SAMPLE_STREAM_ID)
3562 perf_output_put(handle, data->stream_id);
3563
3564 if (sample_type & PERF_SAMPLE_CPU)
3565 perf_output_put(handle, data->cpu_entry);
3566
3567 if (sample_type & PERF_SAMPLE_PERIOD)
3568 perf_output_put(handle, data->period);
3569
3570 if (sample_type & PERF_SAMPLE_READ)
3571 perf_output_read(handle, event);
3572
3573 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3574 if (data->callchain) {
3575 int size = 1;
3576
3577 if (data->callchain)
3578 size += data->callchain->nr;
3579
3580 size *= sizeof(u64);
3581
3582 perf_output_copy(handle, data->callchain, size);
3583 } else {
3584 u64 nr = 0;
3585 perf_output_put(handle, nr);
3586 }
3587 }
3588
3589 if (sample_type & PERF_SAMPLE_RAW) {
3590 if (data->raw) {
3591 perf_output_put(handle, data->raw->size);
3592 perf_output_copy(handle, data->raw->data,
3593 data->raw->size);
3594 } else {
3595 struct {
3596 u32 size;
3597 u32 data;
3598 } raw = {
3599 .size = sizeof(u32),
3600 .data = 0,
3601 };
3602 perf_output_put(handle, raw);
3603 }
3604 }
3605}
3606
3607void perf_prepare_sample(struct perf_event_header *header,
3608 struct perf_sample_data *data,
3609 struct perf_event *event,
3610 struct pt_regs *regs)
3611{
3612 u64 sample_type = event->attr.sample_type;
3613
3614 data->type = sample_type;
3615
3616 header->type = PERF_RECORD_SAMPLE;
3617 header->size = sizeof(*header);
3618
3619 header->misc = 0;
3620 header->misc |= perf_misc_flags(regs);
3621
3622 if (sample_type & PERF_SAMPLE_IP) {
3623 data->ip = perf_instruction_pointer(regs);
3624
3625 header->size += sizeof(data->ip);
3626 }
3627
3628 if (sample_type & PERF_SAMPLE_TID) {
3629 /* namespace issues */
3630 data->tid_entry.pid = perf_event_pid(event, current);
3631 data->tid_entry.tid = perf_event_tid(event, current);
3632
3633 header->size += sizeof(data->tid_entry);
3634 }
3635
3636 if (sample_type & PERF_SAMPLE_TIME) {
3637 data->time = perf_clock();
3638
3639 header->size += sizeof(data->time);
3640 }
3641
3642 if (sample_type & PERF_SAMPLE_ADDR)
3643 header->size += sizeof(data->addr);
3644
3645 if (sample_type & PERF_SAMPLE_ID) {
3646 data->id = primary_event_id(event);
3647
3648 header->size += sizeof(data->id);
3649 }
3650
3651 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3652 data->stream_id = event->id;
3653
3654 header->size += sizeof(data->stream_id);
3655 }
3656
3657 if (sample_type & PERF_SAMPLE_CPU) {
3658 data->cpu_entry.cpu = raw_smp_processor_id();
3659 data->cpu_entry.reserved = 0;
3660
3661 header->size += sizeof(data->cpu_entry);
3662 }
3663
3664 if (sample_type & PERF_SAMPLE_PERIOD)
3665 header->size += sizeof(data->period);
3666
3667 if (sample_type & PERF_SAMPLE_READ)
3668 header->size += perf_event_read_size(event);
3669
3670 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3671 int size = 1;
3672
3673 data->callchain = perf_callchain(regs);
3674
3675 if (data->callchain)
3676 size += data->callchain->nr;
3677
3678 header->size += size * sizeof(u64);
3679 }
3680
3681 if (sample_type & PERF_SAMPLE_RAW) {
3682 int size = sizeof(u32);
3683
3684 if (data->raw)
3685 size += data->raw->size;
3686 else
3687 size += sizeof(u32);
3688
3689 WARN_ON_ONCE(size & (sizeof(u64)-1));
3690 header->size += size;
3691 }
3692}
3693
3694static void perf_event_output(struct perf_event *event, int nmi,
3695 struct perf_sample_data *data,
3696 struct pt_regs *regs)
3697{
3698 struct perf_output_handle handle;
3699 struct perf_event_header header;
3700
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003701 /* protect the callchain buffers */
3702 rcu_read_lock();
3703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003704 perf_prepare_sample(&header, data, event, regs);
3705
3706 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003707 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003708
3709 perf_output_sample(&handle, &header, data, event);
3710
3711 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003712
3713exit:
3714 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003715}
3716
3717/*
3718 * read event_id
3719 */
3720
3721struct perf_read_event {
3722 struct perf_event_header header;
3723
3724 u32 pid;
3725 u32 tid;
3726};
3727
3728static void
3729perf_event_read_event(struct perf_event *event,
3730 struct task_struct *task)
3731{
3732 struct perf_output_handle handle;
3733 struct perf_read_event read_event = {
3734 .header = {
3735 .type = PERF_RECORD_READ,
3736 .misc = 0,
3737 .size = sizeof(read_event) + perf_event_read_size(event),
3738 },
3739 .pid = perf_event_pid(event, task),
3740 .tid = perf_event_tid(event, task),
3741 };
3742 int ret;
3743
3744 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3745 if (ret)
3746 return;
3747
3748 perf_output_put(&handle, read_event);
3749 perf_output_read(&handle, event);
3750
3751 perf_output_end(&handle);
3752}
3753
3754/*
3755 * task tracking -- fork/exit
3756 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003757 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003758 */
3759
3760struct perf_task_event {
3761 struct task_struct *task;
3762 struct perf_event_context *task_ctx;
3763
3764 struct {
3765 struct perf_event_header header;
3766
3767 u32 pid;
3768 u32 ppid;
3769 u32 tid;
3770 u32 ptid;
3771 u64 time;
3772 } event_id;
3773};
3774
3775static void perf_event_task_output(struct perf_event *event,
3776 struct perf_task_event *task_event)
3777{
3778 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003779 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003780 int size, ret;
3781
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782 size = task_event->event_id.header.size;
3783 ret = perf_output_begin(&handle, event, size, 0, 0);
3784
Peter Zijlstraef607772010-05-18 10:50:41 +02003785 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786 return;
3787
3788 task_event->event_id.pid = perf_event_pid(event, task);
3789 task_event->event_id.ppid = perf_event_pid(event, current);
3790
3791 task_event->event_id.tid = perf_event_tid(event, task);
3792 task_event->event_id.ptid = perf_event_tid(event, current);
3793
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003794 perf_output_put(&handle, task_event->event_id);
3795
3796 perf_output_end(&handle);
3797}
3798
3799static int perf_event_task_match(struct perf_event *event)
3800{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003801 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003802 return 0;
3803
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003804 if (event->cpu != -1 && event->cpu != smp_processor_id())
3805 return 0;
3806
Eric B Munson3af9e852010-05-18 15:30:49 +01003807 if (event->attr.comm || event->attr.mmap ||
3808 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003809 return 1;
3810
3811 return 0;
3812}
3813
3814static void perf_event_task_ctx(struct perf_event_context *ctx,
3815 struct perf_task_event *task_event)
3816{
3817 struct perf_event *event;
3818
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3820 if (perf_event_task_match(event))
3821 perf_event_task_output(event, task_event);
3822 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003823}
3824
3825static void perf_event_task_event(struct perf_task_event *task_event)
3826{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003827 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003828 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003829 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003830 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003831
Peter Zijlstracde8e882010-09-13 11:06:55 +02003832 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003833 list_for_each_entry_rcu(pmu, &pmus, entry) {
3834 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3835 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003836
3837 ctx = task_event->task_ctx;
3838 if (!ctx) {
3839 ctxn = pmu->task_ctx_nr;
3840 if (ctxn < 0)
3841 continue;
3842 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3843 }
3844 if (ctx)
3845 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003846 }
Peter Zijlstracde8e882010-09-13 11:06:55 +02003847 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848}
3849
3850static void perf_event_task(struct task_struct *task,
3851 struct perf_event_context *task_ctx,
3852 int new)
3853{
3854 struct perf_task_event task_event;
3855
3856 if (!atomic_read(&nr_comm_events) &&
3857 !atomic_read(&nr_mmap_events) &&
3858 !atomic_read(&nr_task_events))
3859 return;
3860
3861 task_event = (struct perf_task_event){
3862 .task = task,
3863 .task_ctx = task_ctx,
3864 .event_id = {
3865 .header = {
3866 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3867 .misc = 0,
3868 .size = sizeof(task_event.event_id),
3869 },
3870 /* .pid */
3871 /* .ppid */
3872 /* .tid */
3873 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003874 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003875 },
3876 };
3877
3878 perf_event_task_event(&task_event);
3879}
3880
3881void perf_event_fork(struct task_struct *task)
3882{
3883 perf_event_task(task, NULL, 1);
3884}
3885
3886/*
3887 * comm tracking
3888 */
3889
3890struct perf_comm_event {
3891 struct task_struct *task;
3892 char *comm;
3893 int comm_size;
3894
3895 struct {
3896 struct perf_event_header header;
3897
3898 u32 pid;
3899 u32 tid;
3900 } event_id;
3901};
3902
3903static void perf_event_comm_output(struct perf_event *event,
3904 struct perf_comm_event *comm_event)
3905{
3906 struct perf_output_handle handle;
3907 int size = comm_event->event_id.header.size;
3908 int ret = perf_output_begin(&handle, event, size, 0, 0);
3909
3910 if (ret)
3911 return;
3912
3913 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3914 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3915
3916 perf_output_put(&handle, comm_event->event_id);
3917 perf_output_copy(&handle, comm_event->comm,
3918 comm_event->comm_size);
3919 perf_output_end(&handle);
3920}
3921
3922static int perf_event_comm_match(struct perf_event *event)
3923{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003924 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003925 return 0;
3926
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003927 if (event->cpu != -1 && event->cpu != smp_processor_id())
3928 return 0;
3929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003930 if (event->attr.comm)
3931 return 1;
3932
3933 return 0;
3934}
3935
3936static void perf_event_comm_ctx(struct perf_event_context *ctx,
3937 struct perf_comm_event *comm_event)
3938{
3939 struct perf_event *event;
3940
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003941 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3942 if (perf_event_comm_match(event))
3943 perf_event_comm_output(event, comm_event);
3944 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003945}
3946
3947static void perf_event_comm_event(struct perf_comm_event *comm_event)
3948{
3949 struct perf_cpu_context *cpuctx;
3950 struct perf_event_context *ctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003951 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003952 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003953 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003954 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003955
3956 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003957 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003958 size = ALIGN(strlen(comm)+1, sizeof(u64));
3959
3960 comm_event->comm = comm;
3961 comm_event->comm_size = size;
3962
3963 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3964
Peter Zijlstracde8e882010-09-13 11:06:55 +02003965 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003966 list_for_each_entry_rcu(pmu, &pmus, entry) {
3967 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
3968 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003969
3970 ctxn = pmu->task_ctx_nr;
3971 if (ctxn < 0)
3972 continue;
3973
3974 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3975 if (ctx)
3976 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003977 }
Peter Zijlstracde8e882010-09-13 11:06:55 +02003978 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979}
3980
3981void perf_event_comm(struct task_struct *task)
3982{
3983 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003984 struct perf_event_context *ctx;
3985 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003986
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003987 for_each_task_context_nr(ctxn) {
3988 ctx = task->perf_event_ctxp[ctxn];
3989 if (!ctx)
3990 continue;
3991
3992 perf_event_enable_on_exec(ctx);
3993 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003994
3995 if (!atomic_read(&nr_comm_events))
3996 return;
3997
3998 comm_event = (struct perf_comm_event){
3999 .task = task,
4000 /* .comm */
4001 /* .comm_size */
4002 .event_id = {
4003 .header = {
4004 .type = PERF_RECORD_COMM,
4005 .misc = 0,
4006 /* .size */
4007 },
4008 /* .pid */
4009 /* .tid */
4010 },
4011 };
4012
4013 perf_event_comm_event(&comm_event);
4014}
4015
4016/*
4017 * mmap tracking
4018 */
4019
4020struct perf_mmap_event {
4021 struct vm_area_struct *vma;
4022
4023 const char *file_name;
4024 int file_size;
4025
4026 struct {
4027 struct perf_event_header header;
4028
4029 u32 pid;
4030 u32 tid;
4031 u64 start;
4032 u64 len;
4033 u64 pgoff;
4034 } event_id;
4035};
4036
4037static void perf_event_mmap_output(struct perf_event *event,
4038 struct perf_mmap_event *mmap_event)
4039{
4040 struct perf_output_handle handle;
4041 int size = mmap_event->event_id.header.size;
4042 int ret = perf_output_begin(&handle, event, size, 0, 0);
4043
4044 if (ret)
4045 return;
4046
4047 mmap_event->event_id.pid = perf_event_pid(event, current);
4048 mmap_event->event_id.tid = perf_event_tid(event, current);
4049
4050 perf_output_put(&handle, mmap_event->event_id);
4051 perf_output_copy(&handle, mmap_event->file_name,
4052 mmap_event->file_size);
4053 perf_output_end(&handle);
4054}
4055
4056static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004057 struct perf_mmap_event *mmap_event,
4058 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004059{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004060 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004061 return 0;
4062
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004063 if (event->cpu != -1 && event->cpu != smp_processor_id())
4064 return 0;
4065
Eric B Munson3af9e852010-05-18 15:30:49 +01004066 if ((!executable && event->attr.mmap_data) ||
4067 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004068 return 1;
4069
4070 return 0;
4071}
4072
4073static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004074 struct perf_mmap_event *mmap_event,
4075 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076{
4077 struct perf_event *event;
4078
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004080 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004081 perf_event_mmap_output(event, mmap_event);
4082 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004083}
4084
4085static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4086{
4087 struct perf_cpu_context *cpuctx;
4088 struct perf_event_context *ctx;
4089 struct vm_area_struct *vma = mmap_event->vma;
4090 struct file *file = vma->vm_file;
4091 unsigned int size;
4092 char tmp[16];
4093 char *buf = NULL;
4094 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004095 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004096 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004097
4098 memset(tmp, 0, sizeof(tmp));
4099
4100 if (file) {
4101 /*
4102 * d_path works from the end of the buffer backwards, so we
4103 * need to add enough zero bytes after the string to handle
4104 * the 64bit alignment we do later.
4105 */
4106 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4107 if (!buf) {
4108 name = strncpy(tmp, "//enomem", sizeof(tmp));
4109 goto got_name;
4110 }
4111 name = d_path(&file->f_path, buf, PATH_MAX);
4112 if (IS_ERR(name)) {
4113 name = strncpy(tmp, "//toolong", sizeof(tmp));
4114 goto got_name;
4115 }
4116 } else {
4117 if (arch_vma_name(mmap_event->vma)) {
4118 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4119 sizeof(tmp));
4120 goto got_name;
4121 }
4122
4123 if (!vma->vm_mm) {
4124 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4125 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004126 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4127 vma->vm_end >= vma->vm_mm->brk) {
4128 name = strncpy(tmp, "[heap]", sizeof(tmp));
4129 goto got_name;
4130 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4131 vma->vm_end >= vma->vm_mm->start_stack) {
4132 name = strncpy(tmp, "[stack]", sizeof(tmp));
4133 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134 }
4135
4136 name = strncpy(tmp, "//anon", sizeof(tmp));
4137 goto got_name;
4138 }
4139
4140got_name:
4141 size = ALIGN(strlen(name)+1, sizeof(u64));
4142
4143 mmap_event->file_name = name;
4144 mmap_event->file_size = size;
4145
4146 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4147
Peter Zijlstracde8e882010-09-13 11:06:55 +02004148 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004149 list_for_each_entry_rcu(pmu, &pmus, entry) {
4150 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
4151 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4152 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004153
4154 ctxn = pmu->task_ctx_nr;
4155 if (ctxn < 0)
4156 continue;
4157
4158 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4159 if (ctx) {
4160 perf_event_mmap_ctx(ctx, mmap_event,
4161 vma->vm_flags & VM_EXEC);
4162 }
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004163 }
Peter Zijlstracde8e882010-09-13 11:06:55 +02004164 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004165
4166 kfree(buf);
4167}
4168
Eric B Munson3af9e852010-05-18 15:30:49 +01004169void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004170{
4171 struct perf_mmap_event mmap_event;
4172
4173 if (!atomic_read(&nr_mmap_events))
4174 return;
4175
4176 mmap_event = (struct perf_mmap_event){
4177 .vma = vma,
4178 /* .file_name */
4179 /* .file_size */
4180 .event_id = {
4181 .header = {
4182 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004183 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004184 /* .size */
4185 },
4186 /* .pid */
4187 /* .tid */
4188 .start = vma->vm_start,
4189 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004190 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004191 },
4192 };
4193
4194 perf_event_mmap_event(&mmap_event);
4195}
4196
4197/*
4198 * IRQ throttle logging
4199 */
4200
4201static void perf_log_throttle(struct perf_event *event, int enable)
4202{
4203 struct perf_output_handle handle;
4204 int ret;
4205
4206 struct {
4207 struct perf_event_header header;
4208 u64 time;
4209 u64 id;
4210 u64 stream_id;
4211 } throttle_event = {
4212 .header = {
4213 .type = PERF_RECORD_THROTTLE,
4214 .misc = 0,
4215 .size = sizeof(throttle_event),
4216 },
4217 .time = perf_clock(),
4218 .id = primary_event_id(event),
4219 .stream_id = event->id,
4220 };
4221
4222 if (enable)
4223 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4224
4225 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4226 if (ret)
4227 return;
4228
4229 perf_output_put(&handle, throttle_event);
4230 perf_output_end(&handle);
4231}
4232
4233/*
4234 * Generic event overflow handling, sampling.
4235 */
4236
4237static int __perf_event_overflow(struct perf_event *event, int nmi,
4238 int throttle, struct perf_sample_data *data,
4239 struct pt_regs *regs)
4240{
4241 int events = atomic_read(&event->event_limit);
4242 struct hw_perf_event *hwc = &event->hw;
4243 int ret = 0;
4244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004245 if (!throttle) {
4246 hwc->interrupts++;
4247 } else {
4248 if (hwc->interrupts != MAX_INTERRUPTS) {
4249 hwc->interrupts++;
4250 if (HZ * hwc->interrupts >
4251 (u64)sysctl_perf_event_sample_rate) {
4252 hwc->interrupts = MAX_INTERRUPTS;
4253 perf_log_throttle(event, 0);
4254 ret = 1;
4255 }
4256 } else {
4257 /*
4258 * Keep re-disabling events even though on the previous
4259 * pass we disabled it - just in case we raced with a
4260 * sched-in and the event got enabled again:
4261 */
4262 ret = 1;
4263 }
4264 }
4265
4266 if (event->attr.freq) {
4267 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004268 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269
Peter Zijlstraabd50712010-01-26 18:50:16 +01004270 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004271
Peter Zijlstraabd50712010-01-26 18:50:16 +01004272 if (delta > 0 && delta < 2*TICK_NSEC)
4273 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004274 }
4275
4276 /*
4277 * XXX event_limit might not quite work as expected on inherited
4278 * events
4279 */
4280
4281 event->pending_kill = POLL_IN;
4282 if (events && atomic_dec_and_test(&event->event_limit)) {
4283 ret = 1;
4284 event->pending_kill = POLL_HUP;
4285 if (nmi) {
4286 event->pending_disable = 1;
4287 perf_pending_queue(&event->pending,
4288 perf_pending_event);
4289 } else
4290 perf_event_disable(event);
4291 }
4292
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004293 if (event->overflow_handler)
4294 event->overflow_handler(event, nmi, data, regs);
4295 else
4296 perf_event_output(event, nmi, data, regs);
4297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298 return ret;
4299}
4300
4301int perf_event_overflow(struct perf_event *event, int nmi,
4302 struct perf_sample_data *data,
4303 struct pt_regs *regs)
4304{
4305 return __perf_event_overflow(event, nmi, 1, data, regs);
4306}
4307
4308/*
4309 * Generic software event infrastructure
4310 */
4311
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004312struct swevent_htable {
4313 struct swevent_hlist *swevent_hlist;
4314 struct mutex hlist_mutex;
4315 int hlist_refcount;
4316
4317 /* Recursion avoidance in each contexts */
4318 int recursion[PERF_NR_CONTEXTS];
4319};
4320
4321static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004323/*
4324 * We directly increment event->count and keep a second value in
4325 * event->hw.period_left to count intervals. This period event
4326 * is kept in the range [-sample_period, 0] so that we can use the
4327 * sign as trigger.
4328 */
4329
4330static u64 perf_swevent_set_period(struct perf_event *event)
4331{
4332 struct hw_perf_event *hwc = &event->hw;
4333 u64 period = hwc->last_period;
4334 u64 nr, offset;
4335 s64 old, val;
4336
4337 hwc->last_period = hwc->sample_period;
4338
4339again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004340 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341 if (val < 0)
4342 return 0;
4343
4344 nr = div64_u64(period + val, period);
4345 offset = nr * period;
4346 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004347 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004348 goto again;
4349
4350 return nr;
4351}
4352
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004353static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004354 int nmi, struct perf_sample_data *data,
4355 struct pt_regs *regs)
4356{
4357 struct hw_perf_event *hwc = &event->hw;
4358 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004359
4360 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004361 if (!overflow)
4362 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363
4364 if (hwc->interrupts == MAX_INTERRUPTS)
4365 return;
4366
4367 for (; overflow; overflow--) {
4368 if (__perf_event_overflow(event, nmi, throttle,
4369 data, regs)) {
4370 /*
4371 * We inhibit the overflow from happening when
4372 * hwc->interrupts == MAX_INTERRUPTS.
4373 */
4374 break;
4375 }
4376 throttle = 1;
4377 }
4378}
4379
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004380static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004381 int nmi, struct perf_sample_data *data,
4382 struct pt_regs *regs)
4383{
4384 struct hw_perf_event *hwc = &event->hw;
4385
Peter Zijlstrae7850592010-05-21 14:43:08 +02004386 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004387
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004388 if (!regs)
4389 return;
4390
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004391 if (!hwc->sample_period)
4392 return;
4393
4394 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4395 return perf_swevent_overflow(event, 1, nmi, data, regs);
4396
Peter Zijlstrae7850592010-05-21 14:43:08 +02004397 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004398 return;
4399
4400 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004401}
4402
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004403static int perf_exclude_event(struct perf_event *event,
4404 struct pt_regs *regs)
4405{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004406 if (event->hw.state & PERF_HES_STOPPED)
4407 return 0;
4408
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004409 if (regs) {
4410 if (event->attr.exclude_user && user_mode(regs))
4411 return 1;
4412
4413 if (event->attr.exclude_kernel && !user_mode(regs))
4414 return 1;
4415 }
4416
4417 return 0;
4418}
4419
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420static int perf_swevent_match(struct perf_event *event,
4421 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004422 u32 event_id,
4423 struct perf_sample_data *data,
4424 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004425{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426 if (event->attr.type != type)
4427 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004428
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429 if (event->attr.config != event_id)
4430 return 0;
4431
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004432 if (perf_exclude_event(event, regs))
4433 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004434
4435 return 1;
4436}
4437
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004438static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004439{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004440 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004441
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004442 return hash_64(val, SWEVENT_HLIST_BITS);
4443}
4444
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004445static inline struct hlist_head *
4446__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004447{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004448 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004449
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004450 return &hlist->heads[hash];
4451}
4452
4453/* For the read side: events when they trigger */
4454static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004455find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004456{
4457 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004458
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004459 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004460 if (!hlist)
4461 return NULL;
4462
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004463 return __find_swevent_head(hlist, type, event_id);
4464}
4465
4466/* For the event head insertion and removal in the hlist */
4467static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004468find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004469{
4470 struct swevent_hlist *hlist;
4471 u32 event_id = event->attr.config;
4472 u64 type = event->attr.type;
4473
4474 /*
4475 * Event scheduling is always serialized against hlist allocation
4476 * and release. Which makes the protected version suitable here.
4477 * The context lock guarantees that.
4478 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004479 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004480 lockdep_is_held(&event->ctx->lock));
4481 if (!hlist)
4482 return NULL;
4483
4484 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004485}
4486
4487static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4488 u64 nr, int nmi,
4489 struct perf_sample_data *data,
4490 struct pt_regs *regs)
4491{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004492 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004493 struct perf_event *event;
4494 struct hlist_node *node;
4495 struct hlist_head *head;
4496
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004497 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004498 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004499 if (!head)
4500 goto end;
4501
4502 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004503 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004504 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004505 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004506end:
4507 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508}
4509
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004510int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004512 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004513
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004514 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004515}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004516EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004517
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004518void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004519{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004520 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004521
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004522 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004523}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004524
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004525void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4526 struct pt_regs *regs, u64 addr)
4527{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004528 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004529 int rctx;
4530
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004531 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004532 rctx = perf_swevent_get_recursion_context();
4533 if (rctx < 0)
4534 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004535
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004536 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004537
4538 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004539
4540 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004541 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004542}
4543
4544static void perf_swevent_read(struct perf_event *event)
4545{
4546}
4547
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004548static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004550 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004552 struct hlist_head *head;
4553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004554 if (hwc->sample_period) {
4555 hwc->last_period = hwc->sample_period;
4556 perf_swevent_set_period(event);
4557 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004558
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004559 hwc->state = !(flags & PERF_EF_START);
4560
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004561 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004562 if (WARN_ON_ONCE(!head))
4563 return -EINVAL;
4564
4565 hlist_add_head_rcu(&event->hlist_entry, head);
4566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567 return 0;
4568}
4569
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004570static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004571{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004572 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004573}
4574
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004575static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004576{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004577 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004578}
4579
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004580static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004581{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004582 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004583}
4584
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004585/* Deref the hlist from the update side */
4586static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004587swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004588{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004589 return rcu_dereference_protected(swhash->swevent_hlist,
4590 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004591}
4592
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004593static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4594{
4595 struct swevent_hlist *hlist;
4596
4597 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4598 kfree(hlist);
4599}
4600
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004601static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004602{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004603 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004604
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004605 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004606 return;
4607
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004608 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004609 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4610}
4611
4612static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4613{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004614 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004615
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004616 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004617
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004618 if (!--swhash->hlist_refcount)
4619 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004620
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004621 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004622}
4623
4624static void swevent_hlist_put(struct perf_event *event)
4625{
4626 int cpu;
4627
4628 if (event->cpu != -1) {
4629 swevent_hlist_put_cpu(event, event->cpu);
4630 return;
4631 }
4632
4633 for_each_possible_cpu(cpu)
4634 swevent_hlist_put_cpu(event, cpu);
4635}
4636
4637static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4638{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004639 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004640 int err = 0;
4641
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004642 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004643
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004644 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004645 struct swevent_hlist *hlist;
4646
4647 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4648 if (!hlist) {
4649 err = -ENOMEM;
4650 goto exit;
4651 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004652 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004653 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004654 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004655exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004656 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004657
4658 return err;
4659}
4660
4661static int swevent_hlist_get(struct perf_event *event)
4662{
4663 int err;
4664 int cpu, failed_cpu;
4665
4666 if (event->cpu != -1)
4667 return swevent_hlist_get_cpu(event, event->cpu);
4668
4669 get_online_cpus();
4670 for_each_possible_cpu(cpu) {
4671 err = swevent_hlist_get_cpu(event, cpu);
4672 if (err) {
4673 failed_cpu = cpu;
4674 goto fail;
4675 }
4676 }
4677 put_online_cpus();
4678
4679 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004680fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004681 for_each_possible_cpu(cpu) {
4682 if (cpu == failed_cpu)
4683 break;
4684 swevent_hlist_put_cpu(event, cpu);
4685 }
4686
4687 put_online_cpus();
4688 return err;
4689}
4690
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004691atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004692
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004693static void sw_perf_event_destroy(struct perf_event *event)
4694{
4695 u64 event_id = event->attr.config;
4696
4697 WARN_ON(event->parent);
4698
4699 atomic_dec(&perf_swevent_enabled[event_id]);
4700 swevent_hlist_put(event);
4701}
4702
4703static int perf_swevent_init(struct perf_event *event)
4704{
4705 int event_id = event->attr.config;
4706
4707 if (event->attr.type != PERF_TYPE_SOFTWARE)
4708 return -ENOENT;
4709
4710 switch (event_id) {
4711 case PERF_COUNT_SW_CPU_CLOCK:
4712 case PERF_COUNT_SW_TASK_CLOCK:
4713 return -ENOENT;
4714
4715 default:
4716 break;
4717 }
4718
4719 if (event_id > PERF_COUNT_SW_MAX)
4720 return -ENOENT;
4721
4722 if (!event->parent) {
4723 int err;
4724
4725 err = swevent_hlist_get(event);
4726 if (err)
4727 return err;
4728
4729 atomic_inc(&perf_swevent_enabled[event_id]);
4730 event->destroy = sw_perf_event_destroy;
4731 }
4732
4733 return 0;
4734}
4735
4736static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004737 .task_ctx_nr = perf_sw_context,
4738
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004739 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004740 .add = perf_swevent_add,
4741 .del = perf_swevent_del,
4742 .start = perf_swevent_start,
4743 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004744 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004745};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004746
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004747#ifdef CONFIG_EVENT_TRACING
4748
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004749static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004750 struct perf_sample_data *data)
4751{
4752 void *record = data->raw->data;
4753
4754 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4755 return 1;
4756 return 0;
4757}
4758
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004759static int perf_tp_event_match(struct perf_event *event,
4760 struct perf_sample_data *data,
4761 struct pt_regs *regs)
4762{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004763 /*
4764 * All tracepoints are from kernel-space.
4765 */
4766 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004767 return 0;
4768
4769 if (!perf_tp_filter_match(event, data))
4770 return 0;
4771
4772 return 1;
4773}
4774
4775void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004776 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004777{
4778 struct perf_sample_data data;
4779 struct perf_event *event;
4780 struct hlist_node *node;
4781
4782 struct perf_raw_record raw = {
4783 .size = entry_size,
4784 .data = record,
4785 };
4786
4787 perf_sample_data_init(&data, addr);
4788 data.raw = &raw;
4789
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004790 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4791 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004792 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004793 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004794
4795 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004796}
4797EXPORT_SYMBOL_GPL(perf_tp_event);
4798
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004799static void tp_perf_event_destroy(struct perf_event *event)
4800{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004801 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004802}
4803
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004804static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004805{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004806 int err;
4807
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004808 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4809 return -ENOENT;
4810
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004811 /*
4812 * Raw tracepoint data is a severe data leak, only allow root to
4813 * have these.
4814 */
4815 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4816 perf_paranoid_tracepoint_raw() &&
4817 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004818 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004819
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004820 err = perf_trace_init(event);
4821 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004822 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004823
4824 event->destroy = tp_perf_event_destroy;
4825
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004826 return 0;
4827}
4828
4829static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004830 .task_ctx_nr = perf_sw_context,
4831
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004832 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004833 .add = perf_trace_add,
4834 .del = perf_trace_del,
4835 .start = perf_swevent_start,
4836 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004837 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004838};
4839
4840static inline void perf_tp_register(void)
4841{
4842 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843}
Li Zefan6fb29152009-10-15 11:21:42 +08004844
4845static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4846{
4847 char *filter_str;
4848 int ret;
4849
4850 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4851 return -EINVAL;
4852
4853 filter_str = strndup_user(arg, PAGE_SIZE);
4854 if (IS_ERR(filter_str))
4855 return PTR_ERR(filter_str);
4856
4857 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4858
4859 kfree(filter_str);
4860 return ret;
4861}
4862
4863static void perf_event_free_filter(struct perf_event *event)
4864{
4865 ftrace_profile_free_filter(event);
4866}
4867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004868#else
Li Zefan6fb29152009-10-15 11:21:42 +08004869
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004870static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872}
Li Zefan6fb29152009-10-15 11:21:42 +08004873
4874static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4875{
4876 return -ENOENT;
4877}
4878
4879static void perf_event_free_filter(struct perf_event *event)
4880{
4881}
4882
Li Zefan07b139c2009-12-21 14:27:35 +08004883#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004884
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004885#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004886void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004887{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004888 struct perf_sample_data sample;
4889 struct pt_regs *regs = data;
4890
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004891 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004892
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004893 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4894 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004895}
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004896#endif
4897
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004898/*
4899 * hrtimer based swevent callback
4900 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004901
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004902static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004903{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004904 enum hrtimer_restart ret = HRTIMER_RESTART;
4905 struct perf_sample_data data;
4906 struct pt_regs *regs;
4907 struct perf_event *event;
4908 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004909
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004910 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4911 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004912
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004913 perf_sample_data_init(&data, 0);
4914 data.period = event->hw.last_period;
4915 regs = get_irq_regs();
4916
4917 if (regs && !perf_exclude_event(event, regs)) {
4918 if (!(event->attr.exclude_idle && current->pid == 0))
4919 if (perf_event_overflow(event, 0, &data, regs))
4920 ret = HRTIMER_NORESTART;
4921 }
4922
4923 period = max_t(u64, 10000, event->hw.sample_period);
4924 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4925
4926 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004927}
4928
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004929static void perf_swevent_start_hrtimer(struct perf_event *event)
4930{
4931 struct hw_perf_event *hwc = &event->hw;
4932
4933 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4934 hwc->hrtimer.function = perf_swevent_hrtimer;
4935 if (hwc->sample_period) {
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004936 s64 period = local64_read(&hwc->period_left);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004937
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004938 if (period) {
4939 if (period < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004940 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004941
4942 local64_set(&hwc->period_left, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004943 } else {
4944 period = max_t(u64, 10000, hwc->sample_period);
4945 }
4946 __hrtimer_start_range_ns(&hwc->hrtimer,
4947 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02004948 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004949 }
4950}
4951
4952static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4953{
4954 struct hw_perf_event *hwc = &event->hw;
4955
4956 if (hwc->sample_period) {
4957 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004958 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004959
4960 hrtimer_cancel(&hwc->hrtimer);
4961 }
4962}
4963
4964/*
4965 * Software event: cpu wall time clock
4966 */
4967
4968static void cpu_clock_event_update(struct perf_event *event)
4969{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004970 s64 prev;
4971 u64 now;
4972
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004973 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004974 prev = local64_xchg(&event->hw.prev_count, now);
4975 local64_add(now - prev, &event->count);
4976}
4977
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004978static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004979{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004980 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004981 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004982}
4983
4984static void cpu_clock_event_stop(struct perf_event *event, int flags)
4985{
4986 perf_swevent_cancel_hrtimer(event);
4987 cpu_clock_event_update(event);
4988}
4989
4990static int cpu_clock_event_add(struct perf_event *event, int flags)
4991{
4992 if (flags & PERF_EF_START)
4993 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004994
4995 return 0;
4996}
4997
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004998static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004999{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005000 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005001}
5002
5003static void cpu_clock_event_read(struct perf_event *event)
5004{
5005 cpu_clock_event_update(event);
5006}
5007
5008static int cpu_clock_event_init(struct perf_event *event)
5009{
5010 if (event->attr.type != PERF_TYPE_SOFTWARE)
5011 return -ENOENT;
5012
5013 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5014 return -ENOENT;
5015
5016 return 0;
5017}
5018
5019static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005020 .task_ctx_nr = perf_sw_context,
5021
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005022 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005023 .add = cpu_clock_event_add,
5024 .del = cpu_clock_event_del,
5025 .start = cpu_clock_event_start,
5026 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005027 .read = cpu_clock_event_read,
5028};
5029
5030/*
5031 * Software event: task time clock
5032 */
5033
5034static void task_clock_event_update(struct perf_event *event, u64 now)
5035{
5036 u64 prev;
5037 s64 delta;
5038
5039 prev = local64_xchg(&event->hw.prev_count, now);
5040 delta = now - prev;
5041 local64_add(delta, &event->count);
5042}
5043
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005044static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005045{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005046 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005047 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005048}
5049
5050static void task_clock_event_stop(struct perf_event *event, int flags)
5051{
5052 perf_swevent_cancel_hrtimer(event);
5053 task_clock_event_update(event, event->ctx->time);
5054}
5055
5056static int task_clock_event_add(struct perf_event *event, int flags)
5057{
5058 if (flags & PERF_EF_START)
5059 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005060
5061 return 0;
5062}
5063
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005064static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005065{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005066 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005067}
5068
5069static void task_clock_event_read(struct perf_event *event)
5070{
5071 u64 time;
5072
5073 if (!in_nmi()) {
5074 update_context_time(event->ctx);
5075 time = event->ctx->time;
5076 } else {
5077 u64 now = perf_clock();
5078 u64 delta = now - event->ctx->timestamp;
5079 time = event->ctx->time + delta;
5080 }
5081
5082 task_clock_event_update(event, time);
5083}
5084
5085static int task_clock_event_init(struct perf_event *event)
5086{
5087 if (event->attr.type != PERF_TYPE_SOFTWARE)
5088 return -ENOENT;
5089
5090 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5091 return -ENOENT;
5092
5093 return 0;
5094}
5095
5096static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005097 .task_ctx_nr = perf_sw_context,
5098
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005099 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005100 .add = task_clock_event_add,
5101 .del = task_clock_event_del,
5102 .start = task_clock_event_start,
5103 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005104 .read = task_clock_event_read,
5105};
5106
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005107static void perf_pmu_nop_void(struct pmu *pmu)
5108{
5109}
5110
5111static int perf_pmu_nop_int(struct pmu *pmu)
5112{
5113 return 0;
5114}
5115
5116static void perf_pmu_start_txn(struct pmu *pmu)
5117{
5118 perf_pmu_disable(pmu);
5119}
5120
5121static int perf_pmu_commit_txn(struct pmu *pmu)
5122{
5123 perf_pmu_enable(pmu);
5124 return 0;
5125}
5126
5127static void perf_pmu_cancel_txn(struct pmu *pmu)
5128{
5129 perf_pmu_enable(pmu);
5130}
5131
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005132/*
5133 * Ensures all contexts with the same task_ctx_nr have the same
5134 * pmu_cpu_context too.
5135 */
5136static void *find_pmu_context(int ctxn)
5137{
5138 struct pmu *pmu;
5139
5140 if (ctxn < 0)
5141 return NULL;
5142
5143 list_for_each_entry(pmu, &pmus, entry) {
5144 if (pmu->task_ctx_nr == ctxn)
5145 return pmu->pmu_cpu_context;
5146 }
5147
5148 return NULL;
5149}
5150
5151static void free_pmu_context(void * __percpu cpu_context)
5152{
5153 struct pmu *pmu;
5154
5155 mutex_lock(&pmus_lock);
5156 /*
5157 * Like a real lame refcount.
5158 */
5159 list_for_each_entry(pmu, &pmus, entry) {
5160 if (pmu->pmu_cpu_context == cpu_context)
5161 goto out;
5162 }
5163
5164 free_percpu(cpu_context);
5165out:
5166 mutex_unlock(&pmus_lock);
5167}
5168
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005169int perf_pmu_register(struct pmu *pmu)
5170{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005171 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005172
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005173 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005174 ret = -ENOMEM;
5175 pmu->pmu_disable_count = alloc_percpu(int);
5176 if (!pmu->pmu_disable_count)
5177 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005178
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005179 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5180 if (pmu->pmu_cpu_context)
5181 goto got_cpu_context;
5182
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005183 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5184 if (!pmu->pmu_cpu_context)
5185 goto free_pdc;
5186
5187 for_each_possible_cpu(cpu) {
5188 struct perf_cpu_context *cpuctx;
5189
5190 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005191 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005192 cpuctx->ctx.pmu = pmu;
5193 cpuctx->timer_interval = TICK_NSEC;
5194 hrtimer_init(&cpuctx->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5195 cpuctx->timer.function = perf_event_context_tick;
5196 }
5197
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005198got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005199 if (!pmu->start_txn) {
5200 if (pmu->pmu_enable) {
5201 /*
5202 * If we have pmu_enable/pmu_disable calls, install
5203 * transaction stubs that use that to try and batch
5204 * hardware accesses.
5205 */
5206 pmu->start_txn = perf_pmu_start_txn;
5207 pmu->commit_txn = perf_pmu_commit_txn;
5208 pmu->cancel_txn = perf_pmu_cancel_txn;
5209 } else {
5210 pmu->start_txn = perf_pmu_nop_void;
5211 pmu->commit_txn = perf_pmu_nop_int;
5212 pmu->cancel_txn = perf_pmu_nop_void;
5213 }
5214 }
5215
5216 if (!pmu->pmu_enable) {
5217 pmu->pmu_enable = perf_pmu_nop_void;
5218 pmu->pmu_disable = perf_pmu_nop_void;
5219 }
5220
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005221 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005222 ret = 0;
5223unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005224 mutex_unlock(&pmus_lock);
5225
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005226 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005227
5228free_pdc:
5229 free_percpu(pmu->pmu_disable_count);
5230 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005231}
5232
5233void perf_pmu_unregister(struct pmu *pmu)
5234{
5235 mutex_lock(&pmus_lock);
5236 list_del_rcu(&pmu->entry);
5237 mutex_unlock(&pmus_lock);
5238
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005239 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005240 * We dereference the pmu list under both SRCU and regular RCU, so
5241 * synchronize against both of those.
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005242 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005243 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005244 synchronize_rcu();
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005245
5246 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005247 free_pmu_context(pmu->pmu_cpu_context);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005248}
5249
5250struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005251{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005252 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005253 int idx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005254
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005255 idx = srcu_read_lock(&pmus_srcu);
5256 list_for_each_entry_rcu(pmu, &pmus, entry) {
5257 int ret = pmu->event_init(event);
5258 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005259 goto unlock;
5260
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005261 if (ret != -ENOENT) {
5262 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005263 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005264 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005266 pmu = ERR_PTR(-ENOENT);
5267unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005268 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269
5270 return pmu;
5271}
5272
5273/*
5274 * Allocate and initialize a event structure
5275 */
5276static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005277perf_event_alloc(struct perf_event_attr *attr, int cpu,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 struct perf_event *group_leader,
5279 struct perf_event *parent_event,
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005280 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005282 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283 struct perf_event *event;
5284 struct hw_perf_event *hwc;
5285 long err;
5286
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005287 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005288 if (!event)
5289 return ERR_PTR(-ENOMEM);
5290
5291 /*
5292 * Single events are their own group leaders, with an
5293 * empty sibling list:
5294 */
5295 if (!group_leader)
5296 group_leader = event;
5297
5298 mutex_init(&event->child_mutex);
5299 INIT_LIST_HEAD(&event->child_list);
5300
5301 INIT_LIST_HEAD(&event->group_entry);
5302 INIT_LIST_HEAD(&event->event_entry);
5303 INIT_LIST_HEAD(&event->sibling_list);
5304 init_waitqueue_head(&event->waitq);
5305
5306 mutex_init(&event->mmap_mutex);
5307
5308 event->cpu = cpu;
5309 event->attr = *attr;
5310 event->group_leader = group_leader;
5311 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005312 event->oncpu = -1;
5313
5314 event->parent = parent_event;
5315
5316 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5317 event->id = atomic64_inc_return(&perf_event_id);
5318
5319 event->state = PERF_EVENT_STATE_INACTIVE;
5320
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005321 if (!overflow_handler && parent_event)
5322 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005323
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005324 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005326 if (attr->disabled)
5327 event->state = PERF_EVENT_STATE_OFF;
5328
5329 pmu = NULL;
5330
5331 hwc = &event->hw;
5332 hwc->sample_period = attr->sample_period;
5333 if (attr->freq && attr->sample_freq)
5334 hwc->sample_period = 1;
5335 hwc->last_period = hwc->sample_period;
5336
Peter Zijlstrae7850592010-05-21 14:43:08 +02005337 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005338
5339 /*
5340 * we currently do not support PERF_FORMAT_GROUP on inherited events
5341 */
5342 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5343 goto done;
5344
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005345 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005347done:
5348 err = 0;
5349 if (!pmu)
5350 err = -EINVAL;
5351 else if (IS_ERR(pmu))
5352 err = PTR_ERR(pmu);
5353
5354 if (err) {
5355 if (event->ns)
5356 put_pid_ns(event->ns);
5357 kfree(event);
5358 return ERR_PTR(err);
5359 }
5360
5361 event->pmu = pmu;
5362
5363 if (!event->parent) {
5364 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005365 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005366 atomic_inc(&nr_mmap_events);
5367 if (event->attr.comm)
5368 atomic_inc(&nr_comm_events);
5369 if (event->attr.task)
5370 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005371 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5372 err = get_callchain_buffers();
5373 if (err) {
5374 free_event(event);
5375 return ERR_PTR(err);
5376 }
5377 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378 }
5379
5380 return event;
5381}
5382
5383static int perf_copy_attr(struct perf_event_attr __user *uattr,
5384 struct perf_event_attr *attr)
5385{
5386 u32 size;
5387 int ret;
5388
5389 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5390 return -EFAULT;
5391
5392 /*
5393 * zero the full structure, so that a short copy will be nice.
5394 */
5395 memset(attr, 0, sizeof(*attr));
5396
5397 ret = get_user(size, &uattr->size);
5398 if (ret)
5399 return ret;
5400
5401 if (size > PAGE_SIZE) /* silly large */
5402 goto err_size;
5403
5404 if (!size) /* abi compat */
5405 size = PERF_ATTR_SIZE_VER0;
5406
5407 if (size < PERF_ATTR_SIZE_VER0)
5408 goto err_size;
5409
5410 /*
5411 * If we're handed a bigger struct than we know of,
5412 * ensure all the unknown bits are 0 - i.e. new
5413 * user-space does not rely on any kernel feature
5414 * extensions we dont know about yet.
5415 */
5416 if (size > sizeof(*attr)) {
5417 unsigned char __user *addr;
5418 unsigned char __user *end;
5419 unsigned char val;
5420
5421 addr = (void __user *)uattr + sizeof(*attr);
5422 end = (void __user *)uattr + size;
5423
5424 for (; addr < end; addr++) {
5425 ret = get_user(val, addr);
5426 if (ret)
5427 return ret;
5428 if (val)
5429 goto err_size;
5430 }
5431 size = sizeof(*attr);
5432 }
5433
5434 ret = copy_from_user(attr, uattr, size);
5435 if (ret)
5436 return -EFAULT;
5437
5438 /*
5439 * If the type exists, the corresponding creation will verify
5440 * the attr->config.
5441 */
5442 if (attr->type >= PERF_TYPE_MAX)
5443 return -EINVAL;
5444
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305445 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005446 return -EINVAL;
5447
5448 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5449 return -EINVAL;
5450
5451 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5452 return -EINVAL;
5453
5454out:
5455 return ret;
5456
5457err_size:
5458 put_user(sizeof(*attr), &uattr->size);
5459 ret = -E2BIG;
5460 goto out;
5461}
5462
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005463static int
5464perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005465{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005466 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005467 int ret = -EINVAL;
5468
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005469 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005470 goto set;
5471
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005472 /* don't allow circular references */
5473 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005474 goto out;
5475
Peter Zijlstra0f139302010-05-20 14:35:15 +02005476 /*
5477 * Don't allow cross-cpu buffers
5478 */
5479 if (output_event->cpu != event->cpu)
5480 goto out;
5481
5482 /*
5483 * If its not a per-cpu buffer, it must be the same task.
5484 */
5485 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5486 goto out;
5487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005488set:
5489 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005490 /* Can't redirect output if we've got an active mmap() */
5491 if (atomic_read(&event->mmap_count))
5492 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005493
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005494 if (output_event) {
5495 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005496 buffer = perf_buffer_get(output_event);
5497 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005498 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005499 }
5500
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005501 old_buffer = event->buffer;
5502 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005503 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005504unlock:
5505 mutex_unlock(&event->mmap_mutex);
5506
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005507 if (old_buffer)
5508 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005509out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005510 return ret;
5511}
5512
5513/**
5514 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5515 *
5516 * @attr_uptr: event_id type attributes for monitoring/sampling
5517 * @pid: target pid
5518 * @cpu: target cpu
5519 * @group_fd: group leader event fd
5520 */
5521SYSCALL_DEFINE5(perf_event_open,
5522 struct perf_event_attr __user *, attr_uptr,
5523 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5524{
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005525 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005526 struct perf_event_attr attr;
5527 struct perf_event_context *ctx;
5528 struct file *event_file = NULL;
5529 struct file *group_file = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005530 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005531 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005533 int err;
5534
5535 /* for future expandability... */
5536 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5537 return -EINVAL;
5538
5539 err = perf_copy_attr(attr_uptr, &attr);
5540 if (err)
5541 return err;
5542
5543 if (!attr.exclude_kernel) {
5544 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5545 return -EACCES;
5546 }
5547
5548 if (attr.freq) {
5549 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5550 return -EINVAL;
5551 }
5552
Al Viroea635c62010-05-26 17:40:29 -04005553 event_fd = get_unused_fd_flags(O_RDWR);
5554 if (event_fd < 0)
5555 return event_fd;
5556
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005557 event = perf_event_alloc(&attr, cpu, group_leader, NULL, NULL);
5558 if (IS_ERR(event)) {
5559 err = PTR_ERR(event);
5560 goto err_fd;
5561 }
5562
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005563 if (group_fd != -1) {
5564 group_leader = perf_fget_light(group_fd, &fput_needed);
5565 if (IS_ERR(group_leader)) {
5566 err = PTR_ERR(group_leader);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005567 goto err_alloc;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005568 }
5569 group_file = group_leader->filp;
5570 if (flags & PERF_FLAG_FD_OUTPUT)
5571 output_event = group_leader;
5572 if (flags & PERF_FLAG_FD_NO_GROUP)
5573 group_leader = NULL;
5574 }
5575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005577 * Special case software events and allow them to be part of
5578 * any hardware group.
5579 */
5580 pmu = event->pmu;
5581 if ((pmu->task_ctx_nr == perf_sw_context) && group_leader)
5582 pmu = group_leader->pmu;
5583
5584 /*
5585 * Get the target context (task or percpu):
5586 */
5587 ctx = find_get_context(pmu, pid, cpu);
5588 if (IS_ERR(ctx)) {
5589 err = PTR_ERR(ctx);
5590 goto err_group_fd;
5591 }
5592
5593 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594 * Look up the group leader (we will attach this event to it):
5595 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005596 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005598
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599 /*
5600 * Do not allow a recursive hierarchy (this new sibling
5601 * becoming part of another group-sibling):
5602 */
5603 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005604 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005605 /*
5606 * Do not allow to attach to a group in a different
5607 * task or CPU context:
5608 */
5609 if (group_leader->ctx != ctx)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005610 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005611 /*
5612 * Only a group leader can be exclusive or pinned
5613 */
5614 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005615 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005616 }
5617
5618 if (output_event) {
5619 err = perf_event_set_output(event, output_event);
5620 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005621 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005622 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005623
Al Viroea635c62010-05-26 17:40:29 -04005624 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5625 if (IS_ERR(event_file)) {
5626 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005627 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005628 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005630 event->filp = event_file;
5631 WARN_ON_ONCE(ctx->parent_ctx);
5632 mutex_lock(&ctx->mutex);
5633 perf_install_in_context(ctx, event, cpu);
5634 ++ctx->generation;
5635 mutex_unlock(&ctx->mutex);
5636
5637 event->owner = current;
5638 get_task_struct(current);
5639 mutex_lock(&current->perf_event_mutex);
5640 list_add_tail(&event->owner_entry, &current->perf_event_list);
5641 mutex_unlock(&current->perf_event_mutex);
5642
Peter Zijlstra8a495422010-05-27 15:47:49 +02005643 /*
5644 * Drop the reference on the group_event after placing the
5645 * new event on the sibling_list. This ensures destruction
5646 * of the group leader will find the pointer to itself in
5647 * perf_group_detach().
5648 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005650 fd_install(event_fd, event_file);
5651 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005652
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005653err_context:
Al Viroea635c62010-05-26 17:40:29 -04005654 put_ctx(ctx);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005655err_group_fd:
5656 fput_light(group_file, fput_needed);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005657err_alloc:
5658 free_event(event);
Al Viroea635c62010-05-26 17:40:29 -04005659err_fd:
5660 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005661 return err;
5662}
5663
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005664/**
5665 * perf_event_create_kernel_counter
5666 *
5667 * @attr: attributes of the counter to create
5668 * @cpu: cpu in which the counter is bound
5669 * @pid: task to profile
5670 */
5671struct perf_event *
5672perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005673 pid_t pid,
5674 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005675{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005676 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005677 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005678 int err;
5679
5680 /*
5681 * Get the target context (task or percpu):
5682 */
5683
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005684 event = perf_event_alloc(attr, cpu, NULL, NULL, overflow_handler);
5685 if (IS_ERR(event)) {
5686 err = PTR_ERR(event);
5687 goto err;
5688 }
5689
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005690 ctx = find_get_context(event->pmu, pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005691 if (IS_ERR(ctx)) {
5692 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005693 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005694 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005695
5696 event->filp = NULL;
5697 WARN_ON_ONCE(ctx->parent_ctx);
5698 mutex_lock(&ctx->mutex);
5699 perf_install_in_context(ctx, event, cpu);
5700 ++ctx->generation;
5701 mutex_unlock(&ctx->mutex);
5702
5703 event->owner = current;
5704 get_task_struct(current);
5705 mutex_lock(&current->perf_event_mutex);
5706 list_add_tail(&event->owner_entry, &current->perf_event_list);
5707 mutex_unlock(&current->perf_event_mutex);
5708
5709 return event;
5710
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005711err_free:
5712 free_event(event);
5713err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005714 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005715}
5716EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005718static void sync_child_event(struct perf_event *child_event,
5719 struct task_struct *child)
5720{
5721 struct perf_event *parent_event = child_event->parent;
5722 u64 child_val;
5723
5724 if (child_event->attr.inherit_stat)
5725 perf_event_read_event(child_event, child);
5726
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005727 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005728
5729 /*
5730 * Add back the child's count to the parent's count:
5731 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005732 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005733 atomic64_add(child_event->total_time_enabled,
5734 &parent_event->child_total_time_enabled);
5735 atomic64_add(child_event->total_time_running,
5736 &parent_event->child_total_time_running);
5737
5738 /*
5739 * Remove this event from the parent's list
5740 */
5741 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5742 mutex_lock(&parent_event->child_mutex);
5743 list_del_init(&child_event->child_list);
5744 mutex_unlock(&parent_event->child_mutex);
5745
5746 /*
5747 * Release the parent event, if this was the last
5748 * reference to it.
5749 */
5750 fput(parent_event->filp);
5751}
5752
5753static void
5754__perf_event_exit_task(struct perf_event *child_event,
5755 struct perf_event_context *child_ctx,
5756 struct task_struct *child)
5757{
5758 struct perf_event *parent_event;
5759
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760 perf_event_remove_from_context(child_event);
5761
5762 parent_event = child_event->parent;
5763 /*
5764 * It can happen that parent exits first, and has events
5765 * that are still around due to the child reference. These
5766 * events need to be zapped - but otherwise linger.
5767 */
5768 if (parent_event) {
5769 sync_child_event(child_event, child);
5770 free_event(child_event);
5771 }
5772}
5773
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005774static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005775{
5776 struct perf_event *child_event, *tmp;
5777 struct perf_event_context *child_ctx;
5778 unsigned long flags;
5779
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005780 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005781 perf_event_task(child, NULL, 0);
5782 return;
5783 }
5784
5785 local_irq_save(flags);
5786 /*
5787 * We can't reschedule here because interrupts are disabled,
5788 * and either child is current or it is a task that can't be
5789 * scheduled, so we are now safe from rescheduling changing
5790 * our context.
5791 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005792 child_ctx = child->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005793 __perf_event_task_sched_out(child_ctx);
5794
5795 /*
5796 * Take the context lock here so that if find_get_context is
5797 * reading child->perf_event_ctxp, we wait until it has
5798 * incremented the context's refcount before we do put_ctx below.
5799 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005800 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005801 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005802 /*
5803 * If this context is a clone; unclone it so it can't get
5804 * swapped to another process while we're removing all
5805 * the events from it.
5806 */
5807 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005808 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005809 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005810
5811 /*
5812 * Report the task dead after unscheduling the events so that we
5813 * won't get any samples after PERF_RECORD_EXIT. We can however still
5814 * get a few PERF_RECORD_READ events.
5815 */
5816 perf_event_task(child, child_ctx, 0);
5817
5818 /*
5819 * We can recurse on the same lock type through:
5820 *
5821 * __perf_event_exit_task()
5822 * sync_child_event()
5823 * fput(parent_event->filp)
5824 * perf_release()
5825 * mutex_lock(&ctx->mutex)
5826 *
5827 * But since its the parent context it won't be the same instance.
5828 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005829 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005830
5831again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005832 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5833 group_entry)
5834 __perf_event_exit_task(child_event, child_ctx, child);
5835
5836 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005837 group_entry)
5838 __perf_event_exit_task(child_event, child_ctx, child);
5839
5840 /*
5841 * If the last event was a group event, it will have appended all
5842 * its siblings to the list, but we obtained 'tmp' before that which
5843 * will still point to the list head terminating the iteration.
5844 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005845 if (!list_empty(&child_ctx->pinned_groups) ||
5846 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005847 goto again;
5848
5849 mutex_unlock(&child_ctx->mutex);
5850
5851 put_ctx(child_ctx);
5852}
5853
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005854/*
5855 * When a child task exits, feed back event values to parent events.
5856 */
5857void perf_event_exit_task(struct task_struct *child)
5858{
5859 int ctxn;
5860
5861 for_each_task_context_nr(ctxn)
5862 perf_event_exit_task_context(child, ctxn);
5863}
5864
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005865static void perf_free_event(struct perf_event *event,
5866 struct perf_event_context *ctx)
5867{
5868 struct perf_event *parent = event->parent;
5869
5870 if (WARN_ON_ONCE(!parent))
5871 return;
5872
5873 mutex_lock(&parent->child_mutex);
5874 list_del_init(&event->child_list);
5875 mutex_unlock(&parent->child_mutex);
5876
5877 fput(parent->filp);
5878
Peter Zijlstra8a495422010-05-27 15:47:49 +02005879 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005880 list_del_event(event, ctx);
5881 free_event(event);
5882}
5883
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005884/*
5885 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005886 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005887 */
5888void perf_event_free_task(struct task_struct *task)
5889{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005890 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005891 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005892 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005893
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005894 for_each_task_context_nr(ctxn) {
5895 ctx = task->perf_event_ctxp[ctxn];
5896 if (!ctx)
5897 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005898
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005899 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005901 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5902 group_entry)
5903 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005904
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005905 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5906 group_entry)
5907 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005909 if (!list_empty(&ctx->pinned_groups) ||
5910 !list_empty(&ctx->flexible_groups))
5911 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005912
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005913 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005914
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005915 put_ctx(ctx);
5916 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005917}
5918
Peter Zijlstra4e231c72010-09-09 21:01:59 +02005919void perf_event_delayed_put(struct task_struct *task)
5920{
5921 int ctxn;
5922
5923 for_each_task_context_nr(ctxn)
5924 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5925}
5926
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005927/*
5928 * inherit a event from parent task to child task:
5929 */
5930static struct perf_event *
5931inherit_event(struct perf_event *parent_event,
5932 struct task_struct *parent,
5933 struct perf_event_context *parent_ctx,
5934 struct task_struct *child,
5935 struct perf_event *group_leader,
5936 struct perf_event_context *child_ctx)
5937{
5938 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02005939 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005940
5941 /*
5942 * Instead of creating recursive hierarchies of events,
5943 * we link inherited events back to the original parent,
5944 * which has a filp for sure, which we use as the reference
5945 * count:
5946 */
5947 if (parent_event->parent)
5948 parent_event = parent_event->parent;
5949
5950 child_event = perf_event_alloc(&parent_event->attr,
5951 parent_event->cpu,
5952 group_leader, parent_event,
5953 NULL);
5954 if (IS_ERR(child_event))
5955 return child_event;
5956 get_ctx(child_ctx);
5957
5958 /*
5959 * Make the child state follow the state of the parent event,
5960 * not its attr.disabled bit. We hold the parent's mutex,
5961 * so we won't race with perf_event_{en, dis}able_family.
5962 */
5963 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5964 child_event->state = PERF_EVENT_STATE_INACTIVE;
5965 else
5966 child_event->state = PERF_EVENT_STATE_OFF;
5967
5968 if (parent_event->attr.freq) {
5969 u64 sample_period = parent_event->hw.sample_period;
5970 struct hw_perf_event *hwc = &child_event->hw;
5971
5972 hwc->sample_period = sample_period;
5973 hwc->last_period = sample_period;
5974
5975 local64_set(&hwc->period_left, sample_period);
5976 }
5977
5978 child_event->ctx = child_ctx;
5979 child_event->overflow_handler = parent_event->overflow_handler;
5980
5981 /*
5982 * Link it up in the child's context:
5983 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02005984 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005985 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02005986 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005987
5988 /*
5989 * Get a reference to the parent filp - we will fput it
5990 * when the child event exits. This is safe to do because
5991 * we are in the parent and we know that the filp still
5992 * exists and has a nonzero count:
5993 */
5994 atomic_long_inc(&parent_event->filp->f_count);
5995
5996 /*
5997 * Link this into the parent event's child list
5998 */
5999 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6000 mutex_lock(&parent_event->child_mutex);
6001 list_add_tail(&child_event->child_list, &parent_event->child_list);
6002 mutex_unlock(&parent_event->child_mutex);
6003
6004 return child_event;
6005}
6006
6007static int inherit_group(struct perf_event *parent_event,
6008 struct task_struct *parent,
6009 struct perf_event_context *parent_ctx,
6010 struct task_struct *child,
6011 struct perf_event_context *child_ctx)
6012{
6013 struct perf_event *leader;
6014 struct perf_event *sub;
6015 struct perf_event *child_ctr;
6016
6017 leader = inherit_event(parent_event, parent, parent_ctx,
6018 child, NULL, child_ctx);
6019 if (IS_ERR(leader))
6020 return PTR_ERR(leader);
6021 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6022 child_ctr = inherit_event(sub, parent, parent_ctx,
6023 child, leader, child_ctx);
6024 if (IS_ERR(child_ctr))
6025 return PTR_ERR(child_ctr);
6026 }
6027 return 0;
6028}
6029
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006030static int
6031inherit_task_group(struct perf_event *event, struct task_struct *parent,
6032 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006033 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006034 int *inherited_all)
6035{
6036 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006037 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006038
6039 if (!event->attr.inherit) {
6040 *inherited_all = 0;
6041 return 0;
6042 }
6043
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006044 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006045 if (!child_ctx) {
6046 /*
6047 * This is executed from the parent task context, so
6048 * inherit events that have been marked for cloning.
6049 * First allocate and initialize a context for the
6050 * child.
6051 */
6052
Peter Zijlstraeb184472010-09-07 15:55:13 +02006053 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006054 if (!child_ctx)
6055 return -ENOMEM;
6056
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006057 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006058 }
6059
6060 ret = inherit_group(event, parent, parent_ctx,
6061 child, child_ctx);
6062
6063 if (ret)
6064 *inherited_all = 0;
6065
6066 return ret;
6067}
6068
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006069/*
6070 * Initialize the perf_event context in task_struct
6071 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006072int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006074 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006075 struct perf_event_context *cloned_ctx;
6076 struct perf_event *event;
6077 struct task_struct *parent = current;
6078 int inherited_all = 1;
6079 int ret = 0;
6080
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006081 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006082
6083 mutex_init(&child->perf_event_mutex);
6084 INIT_LIST_HEAD(&child->perf_event_list);
6085
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006086 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006087 return 0;
6088
6089 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006090 * If the parent's context is a clone, pin it so it won't get
6091 * swapped under us.
6092 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006093 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006094
6095 /*
6096 * No need to check if parent_ctx != NULL here; since we saw
6097 * it non-NULL earlier, the only reason for it to become NULL
6098 * is if we exit, and since we're currently in the middle of
6099 * a fork we can't be exiting at the same time.
6100 */
6101
6102 /*
6103 * Lock the parent list. No need to lock the child - not PID
6104 * hashed yet and not running, so nobody can access it.
6105 */
6106 mutex_lock(&parent_ctx->mutex);
6107
6108 /*
6109 * We dont have to disable NMIs - we are only looking at
6110 * the list, not manipulating it:
6111 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006112 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006113 ret = inherit_task_group(event, parent, parent_ctx,
6114 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006115 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006116 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006117 }
6118
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006119 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006120 ret = inherit_task_group(event, parent, parent_ctx,
6121 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006122 if (ret)
6123 break;
6124 }
6125
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006126 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006127
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006128 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006129 /*
6130 * Mark the child context as a clone of the parent
6131 * context, or of whatever the parent is a clone of.
6132 * Note that if the parent is a clone, it could get
6133 * uncloned at any point, but that doesn't matter
6134 * because the list of events and the generation
6135 * count can't have changed since we took the mutex.
6136 */
6137 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6138 if (cloned_ctx) {
6139 child_ctx->parent_ctx = cloned_ctx;
6140 child_ctx->parent_gen = parent_ctx->parent_gen;
6141 } else {
6142 child_ctx->parent_ctx = parent_ctx;
6143 child_ctx->parent_gen = parent_ctx->generation;
6144 }
6145 get_ctx(child_ctx->parent_ctx);
6146 }
6147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006148 mutex_unlock(&parent_ctx->mutex);
6149
6150 perf_unpin_context(parent_ctx);
6151
6152 return ret;
6153}
6154
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006155/*
6156 * Initialize the perf_event context in task_struct
6157 */
6158int perf_event_init_task(struct task_struct *child)
6159{
6160 int ctxn, ret;
6161
6162 for_each_task_context_nr(ctxn) {
6163 ret = perf_event_init_context(child, ctxn);
6164 if (ret)
6165 return ret;
6166 }
6167
6168 return 0;
6169}
6170
Paul Mackerras220b1402010-03-10 20:45:52 +11006171static void __init perf_event_init_all_cpus(void)
6172{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006173 struct swevent_htable *swhash;
6174 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006175
6176 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006177 swhash = &per_cpu(swevent_htable, cpu);
6178 mutex_init(&swhash->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11006179 }
6180}
6181
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006182static void __cpuinit perf_event_init_cpu(int cpu)
6183{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006184 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006185
6186 mutex_lock(&swhash->hlist_mutex);
6187 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006188 struct swevent_hlist *hlist;
6189
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006190 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6191 WARN_ON(!hlist);
6192 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006193 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006194 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006195}
6196
6197#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006198static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006199{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006200 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006201 struct perf_event *event, *tmp;
6202
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006203 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006204
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006205 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6206 __perf_event_remove_from_context(event);
6207 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006208 __perf_event_remove_from_context(event);
6209}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006210
6211static void perf_event_exit_cpu_context(int cpu)
6212{
6213 struct perf_event_context *ctx;
6214 struct pmu *pmu;
6215 int idx;
6216
6217 idx = srcu_read_lock(&pmus_srcu);
6218 list_for_each_entry_rcu(pmu, &pmus, entry) {
6219 ctx = &this_cpu_ptr(pmu->pmu_cpu_context)->ctx;
6220
6221 mutex_lock(&ctx->mutex);
6222 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6223 mutex_unlock(&ctx->mutex);
6224 }
6225 srcu_read_unlock(&pmus_srcu, idx);
6226
6227}
6228
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006229static void perf_event_exit_cpu(int cpu)
6230{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006231 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006232
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006233 mutex_lock(&swhash->hlist_mutex);
6234 swevent_hlist_release(swhash);
6235 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006236
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006237 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006238}
6239#else
6240static inline void perf_event_exit_cpu(int cpu) { }
6241#endif
6242
6243static int __cpuinit
6244perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6245{
6246 unsigned int cpu = (long)hcpu;
6247
Peter Zijlstra5e116372010-06-11 13:35:08 +02006248 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006249
6250 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006251 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006252 perf_event_init_cpu(cpu);
6253 break;
6254
Peter Zijlstra5e116372010-06-11 13:35:08 +02006255 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006256 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006257 perf_event_exit_cpu(cpu);
6258 break;
6259
6260 default:
6261 break;
6262 }
6263
6264 return NOTIFY_OK;
6265}
6266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006267void __init perf_event_init(void)
6268{
Paul Mackerras220b1402010-03-10 20:45:52 +11006269 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006270 init_srcu_struct(&pmus_srcu);
6271 perf_pmu_register(&perf_swevent);
6272 perf_pmu_register(&perf_cpu_clock);
6273 perf_pmu_register(&perf_task_clock);
6274 perf_tp_register();
6275 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006276}