blob: 517d827f498281da50210aa76a02b4693116154a [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
Peter Zijlstra82cd6de2010-10-14 17:57:23 +020037atomic_t perf_task_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038static 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
Matt Fleming84c79912010-10-03 21:41:13 +010066extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020067{
Matt Fleming84c79912010-10-03 21:41:13 +010068 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +020069}
70
Peter Zijlstra33696fc2010-06-14 08:49:00 +020071void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020072{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020073 int *count = this_cpu_ptr(pmu->pmu_disable_count);
74 if (!(*count)++)
75 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020076}
77
Peter Zijlstra33696fc2010-06-14 08:49:00 +020078void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020079{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020080 int *count = this_cpu_ptr(pmu->pmu_disable_count);
81 if (!--(*count))
82 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020083}
84
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020085static DEFINE_PER_CPU(struct list_head, rotation_list);
86
87/*
88 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
89 * because they're strictly cpu affine and rotate_start is called with IRQs
90 * disabled, while rotate_context is called from IRQ context.
91 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +020092static void perf_pmu_rotate_start(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 Zijlstrae9d2b062010-09-17 11:28:50 +020095 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020096
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020097 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020098
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020099 if (list_empty(&cpuctx->rotation_list))
100 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200101}
102
103static void get_ctx(struct perf_event_context *ctx)
104{
105 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
106}
107
108static void free_ctx(struct rcu_head *head)
109{
110 struct perf_event_context *ctx;
111
112 ctx = container_of(head, struct perf_event_context, rcu_head);
113 kfree(ctx);
114}
115
116static void put_ctx(struct perf_event_context *ctx)
117{
118 if (atomic_dec_and_test(&ctx->refcount)) {
119 if (ctx->parent_ctx)
120 put_ctx(ctx->parent_ctx);
121 if (ctx->task)
122 put_task_struct(ctx->task);
123 call_rcu(&ctx->rcu_head, free_ctx);
124 }
125}
126
127static void unclone_ctx(struct perf_event_context *ctx)
128{
129 if (ctx->parent_ctx) {
130 put_ctx(ctx->parent_ctx);
131 ctx->parent_ctx = NULL;
132 }
133}
134
135/*
136 * If we inherit events we want to return the parent event id
137 * to userspace.
138 */
139static u64 primary_event_id(struct perf_event *event)
140{
141 u64 id = event->id;
142
143 if (event->parent)
144 id = event->parent->id;
145
146 return id;
147}
148
149/*
150 * Get the perf_event_context for a task and lock it.
151 * This has to cope with with the fact that until it is locked,
152 * the context could get moved to another task.
153 */
154static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200155perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200156{
157 struct perf_event_context *ctx;
158
159 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200160retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200161 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162 if (ctx) {
163 /*
164 * If this context is a clone of another, it might
165 * get swapped for another underneath us by
166 * perf_event_task_sched_out, though the
167 * rcu_read_lock() protects us from any context
168 * getting freed. Lock the context and check if it
169 * got swapped before we could get the lock, and retry
170 * if so. If we locked the right context, then it
171 * can't get swapped on us any more.
172 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100173 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200174 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100175 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200176 goto retry;
177 }
178
179 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100180 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200181 ctx = NULL;
182 }
183 }
184 rcu_read_unlock();
185 return ctx;
186}
187
188/*
189 * Get the context for a task and increment its pin_count so it
190 * can't get swapped to another task. This also increments its
191 * reference count so that the context can't get freed.
192 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200193static struct perf_event_context *
194perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200195{
196 struct perf_event_context *ctx;
197 unsigned long flags;
198
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200199 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200200 if (ctx) {
201 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203 }
204 return ctx;
205}
206
207static void perf_unpin_context(struct perf_event_context *ctx)
208{
209 unsigned long flags;
210
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100211 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200212 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100213 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200214 put_ctx(ctx);
215}
216
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100217static inline u64 perf_clock(void)
218{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200219 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100220}
221
222/*
223 * Update the record of the current time in a context.
224 */
225static void update_context_time(struct perf_event_context *ctx)
226{
227 u64 now = perf_clock();
228
229 ctx->time += now - ctx->timestamp;
230 ctx->timestamp = now;
231}
232
233/*
234 * Update the total_time_enabled and total_time_running fields for a event.
235 */
236static void update_event_times(struct perf_event *event)
237{
238 struct perf_event_context *ctx = event->ctx;
239 u64 run_end;
240
241 if (event->state < PERF_EVENT_STATE_INACTIVE ||
242 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
243 return;
244
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100245 if (ctx->is_active)
246 run_end = ctx->time;
247 else
248 run_end = event->tstamp_stopped;
249
250 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100251
252 if (event->state == PERF_EVENT_STATE_INACTIVE)
253 run_end = event->tstamp_stopped;
254 else
255 run_end = ctx->time;
256
257 event->total_time_running = run_end - event->tstamp_running;
258}
259
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200260/*
261 * Update total_time_enabled and total_time_running for all events in a group.
262 */
263static void update_group_times(struct perf_event *leader)
264{
265 struct perf_event *event;
266
267 update_event_times(leader);
268 list_for_each_entry(event, &leader->sibling_list, group_entry)
269 update_event_times(event);
270}
271
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100272static struct list_head *
273ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
274{
275 if (event->attr.pinned)
276 return &ctx->pinned_groups;
277 else
278 return &ctx->flexible_groups;
279}
280
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200281/*
282 * Add a event from the lists for its context.
283 * Must be called with ctx->mutex and ctx->lock held.
284 */
285static void
286list_add_event(struct perf_event *event, struct perf_event_context *ctx)
287{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200288 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
289 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200290
291 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200292 * If we're a stand alone event or group leader, we go to the context
293 * list, group events are kept attached to the group so that
294 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200295 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200296 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100297 struct list_head *list;
298
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100299 if (is_software_event(event))
300 event->group_flags |= PERF_GROUP_SOFTWARE;
301
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100302 list = ctx_group_list(event, ctx);
303 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200304 }
305
306 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200307 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200308 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200309 ctx->nr_events++;
310 if (event->attr.inherit_stat)
311 ctx->nr_stat++;
312}
313
Peter Zijlstra8a495422010-05-27 15:47:49 +0200314static void perf_group_attach(struct perf_event *event)
315{
316 struct perf_event *group_leader = event->group_leader;
317
Peter Zijlstra74c33372010-10-15 11:40:29 +0200318 /*
319 * We can have double attach due to group movement in perf_event_open.
320 */
321 if (event->attach_state & PERF_ATTACH_GROUP)
322 return;
323
Peter Zijlstra8a495422010-05-27 15:47:49 +0200324 event->attach_state |= PERF_ATTACH_GROUP;
325
326 if (group_leader == event)
327 return;
328
329 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
330 !is_software_event(event))
331 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
332
333 list_add_tail(&event->group_entry, &group_leader->sibling_list);
334 group_leader->nr_siblings++;
335}
336
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200337/*
338 * Remove a event from the lists for its context.
339 * Must be called with ctx->mutex and ctx->lock held.
340 */
341static void
342list_del_event(struct perf_event *event, struct perf_event_context *ctx)
343{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200344 /*
345 * We can have double detach due to exit/hot-unplug + close.
346 */
347 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200348 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200349
350 event->attach_state &= ~PERF_ATTACH_CONTEXT;
351
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200352 ctx->nr_events--;
353 if (event->attr.inherit_stat)
354 ctx->nr_stat--;
355
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200356 list_del_rcu(&event->event_entry);
357
Peter Zijlstra8a495422010-05-27 15:47:49 +0200358 if (event->group_leader == event)
359 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200360
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200361 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800362
363 /*
364 * If event was in error state, then keep it
365 * that way, otherwise bogus counts will be
366 * returned on read(). The only way to get out
367 * of error state is by explicit re-enabling
368 * of the event
369 */
370 if (event->state > PERF_EVENT_STATE_OFF)
371 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200372}
373
Peter Zijlstra8a495422010-05-27 15:47:49 +0200374static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200375{
376 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200377 struct list_head *list = NULL;
378
379 /*
380 * We can have double detach due to exit/hot-unplug + close.
381 */
382 if (!(event->attach_state & PERF_ATTACH_GROUP))
383 return;
384
385 event->attach_state &= ~PERF_ATTACH_GROUP;
386
387 /*
388 * If this is a sibling, remove it from its group.
389 */
390 if (event->group_leader != event) {
391 list_del_init(&event->group_entry);
392 event->group_leader->nr_siblings--;
393 return;
394 }
395
396 if (!list_empty(&event->group_entry))
397 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100398
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200399 /*
400 * If this was a group event with sibling events then
401 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200402 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200403 */
404 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200405 if (list)
406 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200407 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100408
409 /* Inherit group flags from the previous leader */
410 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200411 }
412}
413
Stephane Eranianfa66f072010-08-26 16:40:01 +0200414static inline int
415event_filter_match(struct perf_event *event)
416{
417 return event->cpu == -1 || event->cpu == smp_processor_id();
418}
419
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200420static void
421event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200422 struct perf_cpu_context *cpuctx,
423 struct perf_event_context *ctx)
424{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200425 u64 delta;
426 /*
427 * An event which could not be activated because of
428 * filter mismatch still needs to have its timings
429 * maintained, otherwise bogus information is return
430 * via read() for time_enabled, time_running:
431 */
432 if (event->state == PERF_EVENT_STATE_INACTIVE
433 && !event_filter_match(event)) {
434 delta = ctx->time - event->tstamp_stopped;
435 event->tstamp_running += delta;
436 event->tstamp_stopped = ctx->time;
437 }
438
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200439 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200440 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200441
442 event->state = PERF_EVENT_STATE_INACTIVE;
443 if (event->pending_disable) {
444 event->pending_disable = 0;
445 event->state = PERF_EVENT_STATE_OFF;
446 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200447 event->tstamp_stopped = ctx->time;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200448 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200449 event->oncpu = -1;
450
451 if (!is_software_event(event))
452 cpuctx->active_oncpu--;
453 ctx->nr_active--;
454 if (event->attr.exclusive || !cpuctx->active_oncpu)
455 cpuctx->exclusive = 0;
456}
457
458static void
459group_sched_out(struct perf_event *group_event,
460 struct perf_cpu_context *cpuctx,
461 struct perf_event_context *ctx)
462{
463 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200464 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200465
466 event_sched_out(group_event, cpuctx, ctx);
467
468 /*
469 * Schedule out siblings (if any):
470 */
471 list_for_each_entry(event, &group_event->sibling_list, group_entry)
472 event_sched_out(event, cpuctx, ctx);
473
Stephane Eranianfa66f072010-08-26 16:40:01 +0200474 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200475 cpuctx->exclusive = 0;
476}
477
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200478static inline struct perf_cpu_context *
479__get_cpu_context(struct perf_event_context *ctx)
480{
481 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
482}
483
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200484/*
485 * Cross CPU call to remove a performance event
486 *
487 * We disable the event on the hardware level first. After that we
488 * remove it from the context list.
489 */
490static void __perf_event_remove_from_context(void *info)
491{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200492 struct perf_event *event = info;
493 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200494 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200495
496 /*
497 * If this is a task context, we need to check whether it is
498 * the current task context of this cpu. If not it has been
499 * scheduled out before the smp call arrived.
500 */
501 if (ctx->task && cpuctx->task_ctx != ctx)
502 return;
503
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100504 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200505
506 event_sched_out(event, cpuctx, ctx);
507
508 list_del_event(event, ctx);
509
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100510 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200511}
512
513
514/*
515 * Remove the event from a task's (or a CPU's) list of events.
516 *
517 * Must be called with ctx->mutex held.
518 *
519 * CPU events are removed with a smp call. For task events we only
520 * call when the task is on a CPU.
521 *
522 * If event->ctx is a cloned context, callers must make sure that
523 * every task struct that event->ctx->task could possibly point to
524 * remains valid. This is OK when called from perf_release since
525 * that only calls us on the top-level context, which can't be a clone.
526 * When called from perf_event_exit_task, it's OK because the
527 * context has been detached from its task.
528 */
529static void perf_event_remove_from_context(struct perf_event *event)
530{
531 struct perf_event_context *ctx = event->ctx;
532 struct task_struct *task = ctx->task;
533
534 if (!task) {
535 /*
536 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200537 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200538 */
539 smp_call_function_single(event->cpu,
540 __perf_event_remove_from_context,
541 event, 1);
542 return;
543 }
544
545retry:
546 task_oncpu_function_call(task, __perf_event_remove_from_context,
547 event);
548
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100549 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200550 /*
551 * If the context is active we need to retry the smp call.
552 */
553 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100554 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555 goto retry;
556 }
557
558 /*
559 * The lock prevents that this context is scheduled in so we
560 * can remove the event safely, if the call above did not
561 * succeed.
562 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100563 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200564 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100565 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200566}
567
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200568/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200569 * Cross CPU call to disable a performance event
570 */
571static void __perf_event_disable(void *info)
572{
573 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200574 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200575 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200576
577 /*
578 * If this is a per-task event, need to check whether this
579 * event's task is the current task on this cpu.
580 */
581 if (ctx->task && cpuctx->task_ctx != ctx)
582 return;
583
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100584 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200585
586 /*
587 * If the event is on, turn it off.
588 * If it is in error state, leave it in error state.
589 */
590 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
591 update_context_time(ctx);
592 update_group_times(event);
593 if (event == event->group_leader)
594 group_sched_out(event, cpuctx, ctx);
595 else
596 event_sched_out(event, cpuctx, ctx);
597 event->state = PERF_EVENT_STATE_OFF;
598 }
599
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100600 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200601}
602
603/*
604 * Disable a event.
605 *
606 * If event->ctx is a cloned context, callers must make sure that
607 * every task struct that event->ctx->task could possibly point to
608 * remains valid. This condition is satisifed when called through
609 * perf_event_for_each_child or perf_event_for_each because they
610 * hold the top-level event's child_mutex, so any descendant that
611 * goes to exit will block in sync_child_event.
612 * When called from perf_pending_event it's OK because event->ctx
613 * is the current context on this CPU and preemption is disabled,
614 * hence we can't get into perf_event_task_sched_out for this context.
615 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100616void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200617{
618 struct perf_event_context *ctx = event->ctx;
619 struct task_struct *task = ctx->task;
620
621 if (!task) {
622 /*
623 * Disable the event on the cpu that it's on
624 */
625 smp_call_function_single(event->cpu, __perf_event_disable,
626 event, 1);
627 return;
628 }
629
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200630retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200631 task_oncpu_function_call(task, __perf_event_disable, event);
632
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100633 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200634 /*
635 * If the event is still active, we need to retry the cross-call.
636 */
637 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100638 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200639 goto retry;
640 }
641
642 /*
643 * Since we have the lock this context can't be scheduled
644 * in, so we can change the state safely.
645 */
646 if (event->state == PERF_EVENT_STATE_INACTIVE) {
647 update_group_times(event);
648 event->state = PERF_EVENT_STATE_OFF;
649 }
650
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100651 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200652}
653
654static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200655event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100657 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200658{
659 if (event->state <= PERF_EVENT_STATE_OFF)
660 return 0;
661
662 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100663 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200664 /*
665 * The new state must be visible before we turn it on in the hardware:
666 */
667 smp_wmb();
668
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200669 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200670 event->state = PERF_EVENT_STATE_INACTIVE;
671 event->oncpu = -1;
672 return -EAGAIN;
673 }
674
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200675 event->tstamp_running += ctx->time - event->tstamp_stopped;
676
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200677 if (!is_software_event(event))
678 cpuctx->active_oncpu++;
679 ctx->nr_active++;
680
681 if (event->attr.exclusive)
682 cpuctx->exclusive = 1;
683
684 return 0;
685}
686
687static int
688group_sched_in(struct perf_event *group_event,
689 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100690 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200691{
Lin Ming6bde9b62010-04-23 13:56:00 +0800692 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200693 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +0200694 u64 now = ctx->time;
695 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200696
697 if (group_event->state == PERF_EVENT_STATE_OFF)
698 return 0;
699
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200700 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +0800701
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200702 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200703 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200704 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200705 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200706
707 /*
708 * Schedule in siblings as one group (if any):
709 */
710 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200711 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200712 partial_group = event;
713 goto group_error;
714 }
715 }
716
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200717 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000718 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200719
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200720group_error:
721 /*
722 * Groups can be scheduled in as one unit only, so undo any
723 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +0200724 * The events up to the failed event are scheduled out normally,
725 * tstamp_stopped will be updated.
726 *
727 * The failed events and the remaining siblings need to have
728 * their timings updated as if they had gone thru event_sched_in()
729 * and event_sched_out(). This is required to get consistent timings
730 * across the group. This also takes care of the case where the group
731 * could never be scheduled by ensuring tstamp_stopped is set to mark
732 * the time the event was actually stopped, such that time delta
733 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200734 */
735 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
736 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +0200737 simulate = true;
738
739 if (simulate) {
740 event->tstamp_running += now - event->tstamp_stopped;
741 event->tstamp_stopped = now;
742 } else {
743 event_sched_out(event, cpuctx, ctx);
744 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200745 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200746 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200747
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200748 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200749
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200750 return -EAGAIN;
751}
752
753/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200754 * Work out whether we can put this event group on the CPU now.
755 */
756static int group_can_go_on(struct perf_event *event,
757 struct perf_cpu_context *cpuctx,
758 int can_add_hw)
759{
760 /*
761 * Groups consisting entirely of software events can always go on.
762 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100763 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200764 return 1;
765 /*
766 * If an exclusive group is already on, no other hardware
767 * events can go on.
768 */
769 if (cpuctx->exclusive)
770 return 0;
771 /*
772 * If this group is exclusive and there are already
773 * events on the CPU, it can't go on.
774 */
775 if (event->attr.exclusive && cpuctx->active_oncpu)
776 return 0;
777 /*
778 * Otherwise, try to add it if all previous groups were able
779 * to go on.
780 */
781 return can_add_hw;
782}
783
784static void add_event_to_ctx(struct perf_event *event,
785 struct perf_event_context *ctx)
786{
787 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200788 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200789 event->tstamp_enabled = ctx->time;
790 event->tstamp_running = ctx->time;
791 event->tstamp_stopped = ctx->time;
792}
793
794/*
795 * Cross CPU call to install and enable a performance event
796 *
797 * Must be called with ctx->mutex held
798 */
799static void __perf_install_in_context(void *info)
800{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200801 struct perf_event *event = info;
802 struct perf_event_context *ctx = event->ctx;
803 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200804 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200805 int err;
806
807 /*
808 * If this is a task context, we need to check whether it is
809 * the current task context of this cpu. If not it has been
810 * scheduled out before the smp call arrived.
811 * Or possibly this is the right context but it isn't
812 * on this cpu because it had no events.
813 */
814 if (ctx->task && cpuctx->task_ctx != ctx) {
815 if (cpuctx->task_ctx || ctx->task != current)
816 return;
817 cpuctx->task_ctx = ctx;
818 }
819
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100820 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200821 ctx->is_active = 1;
822 update_context_time(ctx);
823
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200824 add_event_to_ctx(event, ctx);
825
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100826 if (event->cpu != -1 && event->cpu != smp_processor_id())
827 goto unlock;
828
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200829 /*
830 * Don't put the event on if it is disabled or if
831 * it is in a group and the group isn't on.
832 */
833 if (event->state != PERF_EVENT_STATE_INACTIVE ||
834 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
835 goto unlock;
836
837 /*
838 * An exclusive event can't go on if there are already active
839 * hardware events, and no hardware event can go on if there
840 * is already an exclusive event on.
841 */
842 if (!group_can_go_on(event, cpuctx, 1))
843 err = -EEXIST;
844 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100845 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200846
847 if (err) {
848 /*
849 * This event couldn't go on. If it is in a group
850 * then we have to pull the whole group off.
851 * If the event group is pinned then put it in error state.
852 */
853 if (leader != event)
854 group_sched_out(leader, cpuctx, ctx);
855 if (leader->attr.pinned) {
856 update_group_times(leader);
857 leader->state = PERF_EVENT_STATE_ERROR;
858 }
859 }
860
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200861unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100862 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863}
864
865/*
866 * Attach a performance event to a context
867 *
868 * First we add the event to the list with the hardware enable bit
869 * in event->hw_config cleared.
870 *
871 * If the event is attached to a task which is on a CPU we use a smp
872 * call to enable it in the task context. The task might have been
873 * scheduled away, but we check this in the smp call again.
874 *
875 * Must be called with ctx->mutex held.
876 */
877static void
878perf_install_in_context(struct perf_event_context *ctx,
879 struct perf_event *event,
880 int cpu)
881{
882 struct task_struct *task = ctx->task;
883
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200884 event->ctx = ctx;
885
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200886 if (!task) {
887 /*
888 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200889 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200890 */
891 smp_call_function_single(cpu, __perf_install_in_context,
892 event, 1);
893 return;
894 }
895
896retry:
897 task_oncpu_function_call(task, __perf_install_in_context,
898 event);
899
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100900 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200901 /*
902 * we need to retry the smp call.
903 */
904 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100905 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200906 goto retry;
907 }
908
909 /*
910 * The lock prevents that this context is scheduled in so we
911 * can add the event safely, if it the call above did not
912 * succeed.
913 */
914 if (list_empty(&event->group_entry))
915 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100916 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917}
918
919/*
920 * Put a event into inactive state and update time fields.
921 * Enabling the leader of a group effectively enables all
922 * the group members that aren't explicitly disabled, so we
923 * have to update their ->tstamp_enabled also.
924 * Note: this works for group members as well as group leaders
925 * since the non-leader members' sibling_lists will be empty.
926 */
927static void __perf_event_mark_enabled(struct perf_event *event,
928 struct perf_event_context *ctx)
929{
930 struct perf_event *sub;
931
932 event->state = PERF_EVENT_STATE_INACTIVE;
933 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200934 list_for_each_entry(sub, &event->sibling_list, group_entry) {
935 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200936 sub->tstamp_enabled =
937 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200938 }
939 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200940}
941
942/*
943 * Cross CPU call to enable a performance event
944 */
945static void __perf_event_enable(void *info)
946{
947 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200948 struct perf_event_context *ctx = event->ctx;
949 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200950 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200951 int err;
952
953 /*
954 * If this is a per-task event, need to check whether this
955 * event's task is the current task on this cpu.
956 */
957 if (ctx->task && cpuctx->task_ctx != ctx) {
958 if (cpuctx->task_ctx || ctx->task != current)
959 return;
960 cpuctx->task_ctx = ctx;
961 }
962
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100963 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200964 ctx->is_active = 1;
965 update_context_time(ctx);
966
967 if (event->state >= PERF_EVENT_STATE_INACTIVE)
968 goto unlock;
969 __perf_event_mark_enabled(event, ctx);
970
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100971 if (event->cpu != -1 && event->cpu != smp_processor_id())
972 goto unlock;
973
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200974 /*
975 * If the event is in a group and isn't the group leader,
976 * then don't put it on unless the group is on.
977 */
978 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
979 goto unlock;
980
981 if (!group_can_go_on(event, cpuctx, 1)) {
982 err = -EEXIST;
983 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200984 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100985 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200986 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100987 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200988 }
989
990 if (err) {
991 /*
992 * If this event can't go on and it's part of a
993 * group, then the whole group has to come off.
994 */
995 if (leader != event)
996 group_sched_out(leader, cpuctx, ctx);
997 if (leader->attr.pinned) {
998 update_group_times(leader);
999 leader->state = PERF_EVENT_STATE_ERROR;
1000 }
1001 }
1002
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001003unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001004 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005}
1006
1007/*
1008 * Enable a event.
1009 *
1010 * If event->ctx is a cloned context, callers must make sure that
1011 * every task struct that event->ctx->task could possibly point to
1012 * remains valid. This condition is satisfied when called through
1013 * perf_event_for_each_child or perf_event_for_each as described
1014 * for perf_event_disable.
1015 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001016void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001017{
1018 struct perf_event_context *ctx = event->ctx;
1019 struct task_struct *task = ctx->task;
1020
1021 if (!task) {
1022 /*
1023 * Enable the event on the cpu that it's on
1024 */
1025 smp_call_function_single(event->cpu, __perf_event_enable,
1026 event, 1);
1027 return;
1028 }
1029
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001030 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1032 goto out;
1033
1034 /*
1035 * If the event is in error state, clear that first.
1036 * That way, if we see the event in error state below, we
1037 * know that it has gone back into error state, as distinct
1038 * from the task having been scheduled away before the
1039 * cross-call arrived.
1040 */
1041 if (event->state == PERF_EVENT_STATE_ERROR)
1042 event->state = PERF_EVENT_STATE_OFF;
1043
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001044retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001045 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001046 task_oncpu_function_call(task, __perf_event_enable, event);
1047
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001048 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001049
1050 /*
1051 * If the context is active and the event is still off,
1052 * we need to retry the cross-call.
1053 */
1054 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1055 goto retry;
1056
1057 /*
1058 * Since we have the lock this context can't be scheduled
1059 * in, so we can change the state safely.
1060 */
1061 if (event->state == PERF_EVENT_STATE_OFF)
1062 __perf_event_mark_enabled(event, ctx);
1063
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001064out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001065 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001066}
1067
1068static int perf_event_refresh(struct perf_event *event, int refresh)
1069{
1070 /*
1071 * not supported on inherited events
1072 */
1073 if (event->attr.inherit)
1074 return -EINVAL;
1075
1076 atomic_add(refresh, &event->event_limit);
1077 perf_event_enable(event);
1078
1079 return 0;
1080}
1081
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001082enum event_type_t {
1083 EVENT_FLEXIBLE = 0x1,
1084 EVENT_PINNED = 0x2,
1085 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1086};
1087
1088static void ctx_sched_out(struct perf_event_context *ctx,
1089 struct perf_cpu_context *cpuctx,
1090 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091{
1092 struct perf_event *event;
1093
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001094 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001095 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 ctx->is_active = 0;
1097 if (likely(!ctx->nr_events))
1098 goto out;
1099 update_context_time(ctx);
1100
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001101 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001102 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001103
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001104 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001105 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1106 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001107 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001108
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001109 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001110 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001111 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001112 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001113out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001114 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001115 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001116}
1117
1118/*
1119 * Test whether two contexts are equivalent, i.e. whether they
1120 * have both been cloned from the same version of the same context
1121 * and they both have the same number of enabled events.
1122 * If the number of enabled events is the same, then the set
1123 * of enabled events should be the same, because these are both
1124 * inherited contexts, therefore we can't access individual events
1125 * in them directly with an fd; we can only enable/disable all
1126 * events via prctl, or enable/disable all events in a family
1127 * via ioctl, which will have the same effect on both contexts.
1128 */
1129static int context_equiv(struct perf_event_context *ctx1,
1130 struct perf_event_context *ctx2)
1131{
1132 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1133 && ctx1->parent_gen == ctx2->parent_gen
1134 && !ctx1->pin_count && !ctx2->pin_count;
1135}
1136
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137static void __perf_event_sync_stat(struct perf_event *event,
1138 struct perf_event *next_event)
1139{
1140 u64 value;
1141
1142 if (!event->attr.inherit_stat)
1143 return;
1144
1145 /*
1146 * Update the event value, we cannot use perf_event_read()
1147 * because we're in the middle of a context switch and have IRQs
1148 * disabled, which upsets smp_call_function_single(), however
1149 * we know the event must be on the current CPU, therefore we
1150 * don't need to use it.
1151 */
1152 switch (event->state) {
1153 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001154 event->pmu->read(event);
1155 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156
1157 case PERF_EVENT_STATE_INACTIVE:
1158 update_event_times(event);
1159 break;
1160
1161 default:
1162 break;
1163 }
1164
1165 /*
1166 * In order to keep per-task stats reliable we need to flip the event
1167 * values when we flip the contexts.
1168 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001169 value = local64_read(&next_event->count);
1170 value = local64_xchg(&event->count, value);
1171 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001172
1173 swap(event->total_time_enabled, next_event->total_time_enabled);
1174 swap(event->total_time_running, next_event->total_time_running);
1175
1176 /*
1177 * Since we swizzled the values, update the user visible data too.
1178 */
1179 perf_event_update_userpage(event);
1180 perf_event_update_userpage(next_event);
1181}
1182
1183#define list_next_entry(pos, member) \
1184 list_entry(pos->member.next, typeof(*pos), member)
1185
1186static void perf_event_sync_stat(struct perf_event_context *ctx,
1187 struct perf_event_context *next_ctx)
1188{
1189 struct perf_event *event, *next_event;
1190
1191 if (!ctx->nr_stat)
1192 return;
1193
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001194 update_context_time(ctx);
1195
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001196 event = list_first_entry(&ctx->event_list,
1197 struct perf_event, event_entry);
1198
1199 next_event = list_first_entry(&next_ctx->event_list,
1200 struct perf_event, event_entry);
1201
1202 while (&event->event_entry != &ctx->event_list &&
1203 &next_event->event_entry != &next_ctx->event_list) {
1204
1205 __perf_event_sync_stat(event, next_event);
1206
1207 event = list_next_entry(event, event_entry);
1208 next_event = list_next_entry(next_event, event_entry);
1209 }
1210}
1211
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001212void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1213 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001214{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001215 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001216 struct perf_event_context *next_ctx;
1217 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001218 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219 int do_switch = 1;
1220
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001221 if (likely(!ctx))
1222 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001223
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001224 cpuctx = __get_cpu_context(ctx);
1225 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001226 return;
1227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 rcu_read_lock();
1229 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001230 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001231 if (parent && next_ctx &&
1232 rcu_dereference(next_ctx->parent_ctx) == parent) {
1233 /*
1234 * Looks like the two contexts are clones, so we might be
1235 * able to optimize the context switch. We lock both
1236 * contexts and check that they are clones under the
1237 * lock (including re-checking that neither has been
1238 * uncloned in the meantime). It doesn't matter which
1239 * order we take the locks because no other cpu could
1240 * be trying to lock both of these tasks.
1241 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001242 raw_spin_lock(&ctx->lock);
1243 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001244 if (context_equiv(ctx, next_ctx)) {
1245 /*
1246 * XXX do we need a memory barrier of sorts
1247 * wrt to rcu_dereference() of perf_event_ctxp
1248 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001249 task->perf_event_ctxp[ctxn] = next_ctx;
1250 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251 ctx->task = next;
1252 next_ctx->task = task;
1253 do_switch = 0;
1254
1255 perf_event_sync_stat(ctx, next_ctx);
1256 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001257 raw_spin_unlock(&next_ctx->lock);
1258 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001259 }
1260 rcu_read_unlock();
1261
1262 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001263 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001264 cpuctx->task_ctx = NULL;
1265 }
1266}
1267
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001268#define for_each_task_context_nr(ctxn) \
1269 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1270
1271/*
1272 * Called from scheduler to remove the events of the current task,
1273 * with interrupts disabled.
1274 *
1275 * We stop each event and update the event value in event->count.
1276 *
1277 * This does not protect us against NMI, but disable()
1278 * sets the disabled bit in the control field of event _before_
1279 * accessing the event control register. If a NMI hits, then it will
1280 * not restart the event.
1281 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001282void __perf_event_task_sched_out(struct task_struct *task,
1283 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001284{
1285 int ctxn;
1286
1287 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1288
1289 for_each_task_context_nr(ctxn)
1290 perf_event_context_sched_out(task, ctxn, next);
1291}
1292
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001293static void task_ctx_sched_out(struct perf_event_context *ctx,
1294 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001295{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001296 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297
1298 if (!cpuctx->task_ctx)
1299 return;
1300
1301 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1302 return;
1303
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001304 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001305 cpuctx->task_ctx = NULL;
1306}
1307
1308/*
1309 * Called with IRQs disabled
1310 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001311static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1312 enum event_type_t event_type)
1313{
1314 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315}
1316
1317static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001318ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001319 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320{
1321 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001323 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1324 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001325 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001326 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001327 continue;
1328
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001329 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001330 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331
1332 /*
1333 * If this pinned group hasn't been scheduled,
1334 * put it in error state.
1335 */
1336 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1337 update_group_times(event);
1338 event->state = PERF_EVENT_STATE_ERROR;
1339 }
1340 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001341}
1342
1343static void
1344ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001345 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001346{
1347 struct perf_event *event;
1348 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001349
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001350 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1351 /* Ignore events in OFF or ERROR state */
1352 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001353 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001354 /*
1355 * Listen to the 'cpu' scheduling filter constraint
1356 * of events:
1357 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001358 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001359 continue;
1360
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001361 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001362 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001364 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001365 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001366}
1367
1368static void
1369ctx_sched_in(struct perf_event_context *ctx,
1370 struct perf_cpu_context *cpuctx,
1371 enum event_type_t event_type)
1372{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001373 raw_spin_lock(&ctx->lock);
1374 ctx->is_active = 1;
1375 if (likely(!ctx->nr_events))
1376 goto out;
1377
1378 ctx->timestamp = perf_clock();
1379
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001380 /*
1381 * First go through the list and put on any pinned groups
1382 * in order to give them the best chance of going on.
1383 */
1384 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001385 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001386
1387 /* Then walk through the lower prio flexible groups */
1388 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001389 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001390
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001391out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001392 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393}
1394
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001395static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1396 enum event_type_t event_type)
1397{
1398 struct perf_event_context *ctx = &cpuctx->ctx;
1399
1400 ctx_sched_in(ctx, cpuctx, event_type);
1401}
1402
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001403static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001404 enum event_type_t event_type)
1405{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001406 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001407
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001408 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001409 if (cpuctx->task_ctx == ctx)
1410 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001411
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001412 ctx_sched_in(ctx, cpuctx, event_type);
1413 cpuctx->task_ctx = ctx;
1414}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001416void perf_event_context_sched_in(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001417{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001418 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001419
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001420 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001421 if (cpuctx->task_ctx == ctx)
1422 return;
1423
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001424 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001425 /*
1426 * We want to keep the following priority order:
1427 * cpu pinned (that don't need to move), task pinned,
1428 * cpu flexible, task flexible.
1429 */
1430 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1431
1432 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1433 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1434 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1435
1436 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001437
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001438 /*
1439 * Since these rotations are per-cpu, we need to ensure the
1440 * cpu-context we got scheduled on is actually rotating.
1441 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001442 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001443 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444}
1445
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001446/*
1447 * Called from scheduler to add the events of the current task
1448 * with interrupts disabled.
1449 *
1450 * We restore the event value and then enable it.
1451 *
1452 * This does not protect us against NMI, but enable()
1453 * sets the enabled bit in the control field of event _before_
1454 * accessing the event control register. If a NMI hits, then it will
1455 * keep the event running.
1456 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001457void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001458{
1459 struct perf_event_context *ctx;
1460 int ctxn;
1461
1462 for_each_task_context_nr(ctxn) {
1463 ctx = task->perf_event_ctxp[ctxn];
1464 if (likely(!ctx))
1465 continue;
1466
1467 perf_event_context_sched_in(ctx);
1468 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469}
1470
1471#define MAX_INTERRUPTS (~0ULL)
1472
1473static void perf_log_throttle(struct perf_event *event, int enable);
1474
Peter Zijlstraabd50712010-01-26 18:50:16 +01001475static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1476{
1477 u64 frequency = event->attr.sample_freq;
1478 u64 sec = NSEC_PER_SEC;
1479 u64 divisor, dividend;
1480
1481 int count_fls, nsec_fls, frequency_fls, sec_fls;
1482
1483 count_fls = fls64(count);
1484 nsec_fls = fls64(nsec);
1485 frequency_fls = fls64(frequency);
1486 sec_fls = 30;
1487
1488 /*
1489 * We got @count in @nsec, with a target of sample_freq HZ
1490 * the target period becomes:
1491 *
1492 * @count * 10^9
1493 * period = -------------------
1494 * @nsec * sample_freq
1495 *
1496 */
1497
1498 /*
1499 * Reduce accuracy by one bit such that @a and @b converge
1500 * to a similar magnitude.
1501 */
1502#define REDUCE_FLS(a, b) \
1503do { \
1504 if (a##_fls > b##_fls) { \
1505 a >>= 1; \
1506 a##_fls--; \
1507 } else { \
1508 b >>= 1; \
1509 b##_fls--; \
1510 } \
1511} while (0)
1512
1513 /*
1514 * Reduce accuracy until either term fits in a u64, then proceed with
1515 * the other, so that finally we can do a u64/u64 division.
1516 */
1517 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1518 REDUCE_FLS(nsec, frequency);
1519 REDUCE_FLS(sec, count);
1520 }
1521
1522 if (count_fls + sec_fls > 64) {
1523 divisor = nsec * frequency;
1524
1525 while (count_fls + sec_fls > 64) {
1526 REDUCE_FLS(count, sec);
1527 divisor >>= 1;
1528 }
1529
1530 dividend = count * sec;
1531 } else {
1532 dividend = count * sec;
1533
1534 while (nsec_fls + frequency_fls > 64) {
1535 REDUCE_FLS(nsec, frequency);
1536 dividend >>= 1;
1537 }
1538
1539 divisor = nsec * frequency;
1540 }
1541
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001542 if (!divisor)
1543 return dividend;
1544
Peter Zijlstraabd50712010-01-26 18:50:16 +01001545 return div64_u64(dividend, divisor);
1546}
1547
1548static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549{
1550 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001551 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552 s64 delta;
1553
Peter Zijlstraabd50712010-01-26 18:50:16 +01001554 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555
1556 delta = (s64)(period - hwc->sample_period);
1557 delta = (delta + 7) / 8; /* low pass filter */
1558
1559 sample_period = hwc->sample_period + delta;
1560
1561 if (!sample_period)
1562 sample_period = 1;
1563
1564 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001565
Peter Zijlstrae7850592010-05-21 14:43:08 +02001566 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001567 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001568 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001569 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001570 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001571}
1572
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001573static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574{
1575 struct perf_event *event;
1576 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001577 u64 interrupts, now;
1578 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001579
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001580 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001581 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582 if (event->state != PERF_EVENT_STATE_ACTIVE)
1583 continue;
1584
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001585 if (event->cpu != -1 && event->cpu != smp_processor_id())
1586 continue;
1587
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001588 hwc = &event->hw;
1589
1590 interrupts = hwc->interrupts;
1591 hwc->interrupts = 0;
1592
1593 /*
1594 * unthrottle events on the tick
1595 */
1596 if (interrupts == MAX_INTERRUPTS) {
1597 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001598 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599 }
1600
1601 if (!event->attr.freq || !event->attr.sample_freq)
1602 continue;
1603
Peter Zijlstraabd50712010-01-26 18:50:16 +01001604 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001605 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001606 delta = now - hwc->freq_count_stamp;
1607 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608
Peter Zijlstraabd50712010-01-26 18:50:16 +01001609 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001610 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001612 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001613}
1614
1615/*
1616 * Round-robin a context's events:
1617 */
1618static void rotate_ctx(struct perf_event_context *ctx)
1619{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001620 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001621
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001622 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001623 list_rotate_left(&ctx->flexible_groups);
1624
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001625 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626}
1627
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001628/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001629 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1630 * because they're strictly cpu affine and rotate_start is called with IRQs
1631 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001632 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001633static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001634{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001635 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001636 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001637 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001639 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001640 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001641 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1642 rotate = 1;
1643 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001644
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001645 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001646 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001647 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001648 if (ctx->nr_events != ctx->nr_active)
1649 rotate = 1;
1650 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001651
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001652 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001653 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001655 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001657 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001658 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001659
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001660 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001662 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663
1664 rotate_ctx(&cpuctx->ctx);
1665 if (ctx)
1666 rotate_ctx(ctx);
1667
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001668 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001669 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001670 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001671
1672done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001673 if (remove)
1674 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001675
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001676 perf_pmu_enable(cpuctx->ctx.pmu);
1677}
1678
1679void perf_event_task_tick(void)
1680{
1681 struct list_head *head = &__get_cpu_var(rotation_list);
1682 struct perf_cpu_context *cpuctx, *tmp;
1683
1684 WARN_ON(!irqs_disabled());
1685
1686 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1687 if (cpuctx->jiffies_interval == 1 ||
1688 !(jiffies % cpuctx->jiffies_interval))
1689 perf_rotate_context(cpuctx);
1690 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691}
1692
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001693static int event_enable_on_exec(struct perf_event *event,
1694 struct perf_event_context *ctx)
1695{
1696 if (!event->attr.enable_on_exec)
1697 return 0;
1698
1699 event->attr.enable_on_exec = 0;
1700 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1701 return 0;
1702
1703 __perf_event_mark_enabled(event, ctx);
1704
1705 return 1;
1706}
1707
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708/*
1709 * Enable all of a task's events that have been marked enable-on-exec.
1710 * This expects task == current.
1711 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001712static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001713{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001714 struct perf_event *event;
1715 unsigned long flags;
1716 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001717 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718
1719 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720 if (!ctx || !ctx->nr_events)
1721 goto out;
1722
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001723 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001724
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001725 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001727 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1728 ret = event_enable_on_exec(event, ctx);
1729 if (ret)
1730 enabled = 1;
1731 }
1732
1733 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1734 ret = event_enable_on_exec(event, ctx);
1735 if (ret)
1736 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737 }
1738
1739 /*
1740 * Unclone this context if we enabled any event.
1741 */
1742 if (enabled)
1743 unclone_ctx(ctx);
1744
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001745 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001746
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001747 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001748out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001749 local_irq_restore(flags);
1750}
1751
1752/*
1753 * Cross CPU call to read the hardware event
1754 */
1755static void __perf_event_read(void *info)
1756{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001757 struct perf_event *event = info;
1758 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001759 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760
1761 /*
1762 * If this is a task context, we need to check whether it is
1763 * the current task context of this cpu. If not it has been
1764 * scheduled out before the smp call arrived. In that case
1765 * event->count would have been updated to a recent sample
1766 * when the event was scheduled out.
1767 */
1768 if (ctx->task && cpuctx->task_ctx != ctx)
1769 return;
1770
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001771 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001772 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001774 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001775
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001776 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777}
1778
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001779static inline u64 perf_event_count(struct perf_event *event)
1780{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001781 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001782}
1783
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784static u64 perf_event_read(struct perf_event *event)
1785{
1786 /*
1787 * If event is enabled and currently active on a CPU, update the
1788 * value in the event structure:
1789 */
1790 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1791 smp_call_function_single(event->oncpu,
1792 __perf_event_read, event, 1);
1793 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001794 struct perf_event_context *ctx = event->ctx;
1795 unsigned long flags;
1796
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001797 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02001798 /*
1799 * may read while context is not active
1800 * (e.g., thread is blocked), in that case
1801 * we cannot update context time
1802 */
1803 if (ctx->is_active)
1804 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001806 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807 }
1808
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001809 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001810}
1811
1812/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001813 * Callchain support
1814 */
1815
1816struct callchain_cpus_entries {
1817 struct rcu_head rcu_head;
1818 struct perf_callchain_entry *cpu_entries[0];
1819};
1820
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001821static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001822static atomic_t nr_callchain_events;
1823static DEFINE_MUTEX(callchain_mutex);
1824struct callchain_cpus_entries *callchain_cpus_entries;
1825
1826
1827__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1828 struct pt_regs *regs)
1829{
1830}
1831
1832__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1833 struct pt_regs *regs)
1834{
1835}
1836
1837static void release_callchain_buffers_rcu(struct rcu_head *head)
1838{
1839 struct callchain_cpus_entries *entries;
1840 int cpu;
1841
1842 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1843
1844 for_each_possible_cpu(cpu)
1845 kfree(entries->cpu_entries[cpu]);
1846
1847 kfree(entries);
1848}
1849
1850static void release_callchain_buffers(void)
1851{
1852 struct callchain_cpus_entries *entries;
1853
1854 entries = callchain_cpus_entries;
1855 rcu_assign_pointer(callchain_cpus_entries, NULL);
1856 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1857}
1858
1859static int alloc_callchain_buffers(void)
1860{
1861 int cpu;
1862 int size;
1863 struct callchain_cpus_entries *entries;
1864
1865 /*
1866 * We can't use the percpu allocation API for data that can be
1867 * accessed from NMI. Use a temporary manual per cpu allocation
1868 * until that gets sorted out.
1869 */
1870 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1871 num_possible_cpus();
1872
1873 entries = kzalloc(size, GFP_KERNEL);
1874 if (!entries)
1875 return -ENOMEM;
1876
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001877 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001878
1879 for_each_possible_cpu(cpu) {
1880 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1881 cpu_to_node(cpu));
1882 if (!entries->cpu_entries[cpu])
1883 goto fail;
1884 }
1885
1886 rcu_assign_pointer(callchain_cpus_entries, entries);
1887
1888 return 0;
1889
1890fail:
1891 for_each_possible_cpu(cpu)
1892 kfree(entries->cpu_entries[cpu]);
1893 kfree(entries);
1894
1895 return -ENOMEM;
1896}
1897
1898static int get_callchain_buffers(void)
1899{
1900 int err = 0;
1901 int count;
1902
1903 mutex_lock(&callchain_mutex);
1904
1905 count = atomic_inc_return(&nr_callchain_events);
1906 if (WARN_ON_ONCE(count < 1)) {
1907 err = -EINVAL;
1908 goto exit;
1909 }
1910
1911 if (count > 1) {
1912 /* If the allocation failed, give up */
1913 if (!callchain_cpus_entries)
1914 err = -ENOMEM;
1915 goto exit;
1916 }
1917
1918 err = alloc_callchain_buffers();
1919 if (err)
1920 release_callchain_buffers();
1921exit:
1922 mutex_unlock(&callchain_mutex);
1923
1924 return err;
1925}
1926
1927static void put_callchain_buffers(void)
1928{
1929 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1930 release_callchain_buffers();
1931 mutex_unlock(&callchain_mutex);
1932 }
1933}
1934
1935static int get_recursion_context(int *recursion)
1936{
1937 int rctx;
1938
1939 if (in_nmi())
1940 rctx = 3;
1941 else if (in_irq())
1942 rctx = 2;
1943 else if (in_softirq())
1944 rctx = 1;
1945 else
1946 rctx = 0;
1947
1948 if (recursion[rctx])
1949 return -1;
1950
1951 recursion[rctx]++;
1952 barrier();
1953
1954 return rctx;
1955}
1956
1957static inline void put_recursion_context(int *recursion, int rctx)
1958{
1959 barrier();
1960 recursion[rctx]--;
1961}
1962
1963static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1964{
1965 int cpu;
1966 struct callchain_cpus_entries *entries;
1967
1968 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1969 if (*rctx == -1)
1970 return NULL;
1971
1972 entries = rcu_dereference(callchain_cpus_entries);
1973 if (!entries)
1974 return NULL;
1975
1976 cpu = smp_processor_id();
1977
1978 return &entries->cpu_entries[cpu][*rctx];
1979}
1980
1981static void
1982put_callchain_entry(int rctx)
1983{
1984 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1985}
1986
1987static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1988{
1989 int rctx;
1990 struct perf_callchain_entry *entry;
1991
1992
1993 entry = get_callchain_entry(&rctx);
1994 if (rctx == -1)
1995 return NULL;
1996
1997 if (!entry)
1998 goto exit_put;
1999
2000 entry->nr = 0;
2001
2002 if (!user_mode(regs)) {
2003 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2004 perf_callchain_kernel(entry, regs);
2005 if (current->mm)
2006 regs = task_pt_regs(current);
2007 else
2008 regs = NULL;
2009 }
2010
2011 if (regs) {
2012 perf_callchain_store(entry, PERF_CONTEXT_USER);
2013 perf_callchain_user(entry, regs);
2014 }
2015
2016exit_put:
2017 put_callchain_entry(rctx);
2018
2019 return entry;
2020}
2021
2022/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002023 * Initialize the perf_event context in a task_struct:
2024 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002025static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002026{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002027 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002028 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002029 INIT_LIST_HEAD(&ctx->pinned_groups);
2030 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002031 INIT_LIST_HEAD(&ctx->event_list);
2032 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002033}
2034
Peter Zijlstraeb184472010-09-07 15:55:13 +02002035static struct perf_event_context *
2036alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002037{
2038 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002039
2040 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2041 if (!ctx)
2042 return NULL;
2043
2044 __perf_event_init_context(ctx);
2045 if (task) {
2046 ctx->task = task;
2047 get_task_struct(task);
2048 }
2049 ctx->pmu = pmu;
2050
2051 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052}
2053
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002054static struct task_struct *
2055find_lively_task_by_vpid(pid_t vpid)
2056{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002057 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058 int err;
2059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002061 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 task = current;
2063 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002064 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 if (task)
2066 get_task_struct(task);
2067 rcu_read_unlock();
2068
2069 if (!task)
2070 return ERR_PTR(-ESRCH);
2071
2072 /*
2073 * Can't attach events to a dying task.
2074 */
2075 err = -ESRCH;
2076 if (task->flags & PF_EXITING)
2077 goto errout;
2078
2079 /* Reuse ptrace permission checks for now. */
2080 err = -EACCES;
2081 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2082 goto errout;
2083
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002084 return task;
2085errout:
2086 put_task_struct(task);
2087 return ERR_PTR(err);
2088
2089}
2090
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002091static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002092find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093{
2094 struct perf_event_context *ctx;
2095 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002097 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098
Matt Helsley38a81da2010-09-13 13:01:20 -07002099 if (!task && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002100 /* Must be root to operate on a CPU event: */
2101 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2102 return ERR_PTR(-EACCES);
2103
2104 if (cpu < 0 || cpu >= nr_cpumask_bits)
2105 return ERR_PTR(-EINVAL);
2106
2107 /*
2108 * We could be clever and allow to attach a event to an
2109 * offline CPU and activate it when the CPU comes up, but
2110 * that's for later.
2111 */
2112 if (!cpu_online(cpu))
2113 return ERR_PTR(-ENODEV);
2114
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002115 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002116 ctx = &cpuctx->ctx;
2117 get_ctx(ctx);
2118
2119 return ctx;
2120 }
2121
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002122 err = -EINVAL;
2123 ctxn = pmu->task_ctx_nr;
2124 if (ctxn < 0)
2125 goto errout;
2126
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002127retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002128 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002129 if (ctx) {
2130 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002131 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002132 }
2133
2134 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002135 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 err = -ENOMEM;
2137 if (!ctx)
2138 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002141
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002142 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143 /*
2144 * We raced with some other task; use
2145 * the context they set.
2146 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002147 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148 kfree(ctx);
2149 goto retry;
2150 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151 }
2152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002153 return ctx;
2154
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002155errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002156 return ERR_PTR(err);
2157}
2158
Li Zefan6fb29152009-10-15 11:21:42 +08002159static void perf_event_free_filter(struct perf_event *event);
2160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002161static void free_event_rcu(struct rcu_head *head)
2162{
2163 struct perf_event *event;
2164
2165 event = container_of(head, struct perf_event, rcu_head);
2166 if (event->ns)
2167 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002168 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169 kfree(event);
2170}
2171
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002172static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002173
2174static void free_event(struct perf_event *event)
2175{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002176 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177
2178 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002179 if (event->attach_state & PERF_ATTACH_TASK)
2180 jump_label_dec(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002181 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182 atomic_dec(&nr_mmap_events);
2183 if (event->attr.comm)
2184 atomic_dec(&nr_comm_events);
2185 if (event->attr.task)
2186 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002187 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2188 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189 }
2190
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002191 if (event->buffer) {
2192 perf_buffer_put(event->buffer);
2193 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002194 }
2195
2196 if (event->destroy)
2197 event->destroy(event);
2198
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002199 if (event->ctx)
2200 put_ctx(event->ctx);
2201
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002202 call_rcu(&event->rcu_head, free_event_rcu);
2203}
2204
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002205int perf_event_release_kernel(struct perf_event *event)
2206{
2207 struct perf_event_context *ctx = event->ctx;
2208
Peter Zijlstra050735b2010-05-11 11:51:53 +02002209 /*
2210 * Remove from the PMU, can't get re-enabled since we got
2211 * here because the last ref went.
2212 */
2213 perf_event_disable(event);
2214
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002215 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002216 /*
2217 * There are two ways this annotation is useful:
2218 *
2219 * 1) there is a lock recursion from perf_event_exit_task
2220 * see the comment there.
2221 *
2222 * 2) there is a lock-inversion with mmap_sem through
2223 * perf_event_read_group(), which takes faults while
2224 * holding ctx->mutex, however this is called after
2225 * the last filedesc died, so there is no possibility
2226 * to trigger the AB-BA case.
2227 */
2228 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002229 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002230 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002231 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002232 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002233 mutex_unlock(&ctx->mutex);
2234
2235 mutex_lock(&event->owner->perf_event_mutex);
2236 list_del_init(&event->owner_entry);
2237 mutex_unlock(&event->owner->perf_event_mutex);
2238 put_task_struct(event->owner);
2239
2240 free_event(event);
2241
2242 return 0;
2243}
2244EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2245
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002246/*
2247 * Called when the last reference to the file is gone.
2248 */
2249static int perf_release(struct inode *inode, struct file *file)
2250{
2251 struct perf_event *event = file->private_data;
2252
2253 file->private_data = NULL;
2254
2255 return perf_event_release_kernel(event);
2256}
2257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002258static int perf_event_read_size(struct perf_event *event)
2259{
2260 int entry = sizeof(u64); /* value */
2261 int size = 0;
2262 int nr = 1;
2263
2264 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2265 size += sizeof(u64);
2266
2267 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2268 size += sizeof(u64);
2269
2270 if (event->attr.read_format & PERF_FORMAT_ID)
2271 entry += sizeof(u64);
2272
2273 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2274 nr += event->group_leader->nr_siblings;
2275 size += sizeof(u64);
2276 }
2277
2278 size += entry * nr;
2279
2280 return size;
2281}
2282
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002283u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284{
2285 struct perf_event *child;
2286 u64 total = 0;
2287
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002288 *enabled = 0;
2289 *running = 0;
2290
Peter Zijlstra6f105812009-11-20 22:19:56 +01002291 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002292 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002293 *enabled += event->total_time_enabled +
2294 atomic64_read(&event->child_total_time_enabled);
2295 *running += event->total_time_running +
2296 atomic64_read(&event->child_total_time_running);
2297
2298 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002300 *enabled += child->total_time_enabled;
2301 *running += child->total_time_running;
2302 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002303 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304
2305 return total;
2306}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002307EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309static int perf_event_read_group(struct perf_event *event,
2310 u64 read_format, char __user *buf)
2311{
2312 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002313 int n = 0, size = 0, ret = -EFAULT;
2314 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002315 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002316 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002317
Peter Zijlstra6f105812009-11-20 22:19:56 +01002318 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002319 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002320
2321 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002322 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2323 values[n++] = enabled;
2324 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2325 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002326 values[n++] = count;
2327 if (read_format & PERF_FORMAT_ID)
2328 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002329
2330 size = n * sizeof(u64);
2331
2332 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002333 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334
Peter Zijlstra6f105812009-11-20 22:19:56 +01002335 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002336
2337 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002338 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002339
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002340 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002341 if (read_format & PERF_FORMAT_ID)
2342 values[n++] = primary_event_id(sub);
2343
2344 size = n * sizeof(u64);
2345
Stephane Eranian184d3da2009-11-23 21:40:49 -08002346 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002347 ret = -EFAULT;
2348 goto unlock;
2349 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002350
2351 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002353unlock:
2354 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355
Peter Zijlstraabf48682009-11-20 22:19:49 +01002356 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002357}
2358
2359static int perf_event_read_one(struct perf_event *event,
2360 u64 read_format, char __user *buf)
2361{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002362 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363 u64 values[4];
2364 int n = 0;
2365
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002366 values[n++] = perf_event_read_value(event, &enabled, &running);
2367 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2368 values[n++] = enabled;
2369 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2370 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371 if (read_format & PERF_FORMAT_ID)
2372 values[n++] = primary_event_id(event);
2373
2374 if (copy_to_user(buf, values, n * sizeof(u64)))
2375 return -EFAULT;
2376
2377 return n * sizeof(u64);
2378}
2379
2380/*
2381 * Read the performance event - simple non blocking version for now
2382 */
2383static ssize_t
2384perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2385{
2386 u64 read_format = event->attr.read_format;
2387 int ret;
2388
2389 /*
2390 * Return end-of-file for a read on a event that is in
2391 * error state (i.e. because it was pinned but it couldn't be
2392 * scheduled on to the CPU at some point).
2393 */
2394 if (event->state == PERF_EVENT_STATE_ERROR)
2395 return 0;
2396
2397 if (count < perf_event_read_size(event))
2398 return -ENOSPC;
2399
2400 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401 if (read_format & PERF_FORMAT_GROUP)
2402 ret = perf_event_read_group(event, read_format, buf);
2403 else
2404 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002405
2406 return ret;
2407}
2408
2409static ssize_t
2410perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2411{
2412 struct perf_event *event = file->private_data;
2413
2414 return perf_read_hw(event, buf, count);
2415}
2416
2417static unsigned int perf_poll(struct file *file, poll_table *wait)
2418{
2419 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002420 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421 unsigned int events = POLL_HUP;
2422
2423 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002424 buffer = rcu_dereference(event->buffer);
2425 if (buffer)
2426 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427 rcu_read_unlock();
2428
2429 poll_wait(file, &event->waitq, wait);
2430
2431 return events;
2432}
2433
2434static void perf_event_reset(struct perf_event *event)
2435{
2436 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002437 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438 perf_event_update_userpage(event);
2439}
2440
2441/*
2442 * Holding the top-level event's child_mutex means that any
2443 * descendant process that has inherited this event will block
2444 * in sync_child_event if it goes to exit, thus satisfying the
2445 * task existence requirements of perf_event_enable/disable.
2446 */
2447static void perf_event_for_each_child(struct perf_event *event,
2448 void (*func)(struct perf_event *))
2449{
2450 struct perf_event *child;
2451
2452 WARN_ON_ONCE(event->ctx->parent_ctx);
2453 mutex_lock(&event->child_mutex);
2454 func(event);
2455 list_for_each_entry(child, &event->child_list, child_list)
2456 func(child);
2457 mutex_unlock(&event->child_mutex);
2458}
2459
2460static void perf_event_for_each(struct perf_event *event,
2461 void (*func)(struct perf_event *))
2462{
2463 struct perf_event_context *ctx = event->ctx;
2464 struct perf_event *sibling;
2465
2466 WARN_ON_ONCE(ctx->parent_ctx);
2467 mutex_lock(&ctx->mutex);
2468 event = event->group_leader;
2469
2470 perf_event_for_each_child(event, func);
2471 func(event);
2472 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2473 perf_event_for_each_child(event, func);
2474 mutex_unlock(&ctx->mutex);
2475}
2476
2477static int perf_event_period(struct perf_event *event, u64 __user *arg)
2478{
2479 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002480 int ret = 0;
2481 u64 value;
2482
2483 if (!event->attr.sample_period)
2484 return -EINVAL;
2485
John Blackwoodad0cf342010-09-28 18:03:11 -04002486 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487 return -EFAULT;
2488
2489 if (!value)
2490 return -EINVAL;
2491
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002492 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002493 if (event->attr.freq) {
2494 if (value > sysctl_perf_event_sample_rate) {
2495 ret = -EINVAL;
2496 goto unlock;
2497 }
2498
2499 event->attr.sample_freq = value;
2500 } else {
2501 event->attr.sample_period = value;
2502 event->hw.sample_period = value;
2503 }
2504unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002505 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002506
2507 return ret;
2508}
2509
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002510static const struct file_operations perf_fops;
2511
2512static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2513{
2514 struct file *file;
2515
2516 file = fget_light(fd, fput_needed);
2517 if (!file)
2518 return ERR_PTR(-EBADF);
2519
2520 if (file->f_op != &perf_fops) {
2521 fput_light(file, *fput_needed);
2522 *fput_needed = 0;
2523 return ERR_PTR(-EBADF);
2524 }
2525
2526 return file->private_data;
2527}
2528
2529static int perf_event_set_output(struct perf_event *event,
2530 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002531static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002532
2533static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2534{
2535 struct perf_event *event = file->private_data;
2536 void (*func)(struct perf_event *);
2537 u32 flags = arg;
2538
2539 switch (cmd) {
2540 case PERF_EVENT_IOC_ENABLE:
2541 func = perf_event_enable;
2542 break;
2543 case PERF_EVENT_IOC_DISABLE:
2544 func = perf_event_disable;
2545 break;
2546 case PERF_EVENT_IOC_RESET:
2547 func = perf_event_reset;
2548 break;
2549
2550 case PERF_EVENT_IOC_REFRESH:
2551 return perf_event_refresh(event, arg);
2552
2553 case PERF_EVENT_IOC_PERIOD:
2554 return perf_event_period(event, (u64 __user *)arg);
2555
2556 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002557 {
2558 struct perf_event *output_event = NULL;
2559 int fput_needed = 0;
2560 int ret;
2561
2562 if (arg != -1) {
2563 output_event = perf_fget_light(arg, &fput_needed);
2564 if (IS_ERR(output_event))
2565 return PTR_ERR(output_event);
2566 }
2567
2568 ret = perf_event_set_output(event, output_event);
2569 if (output_event)
2570 fput_light(output_event->filp, fput_needed);
2571
2572 return ret;
2573 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002574
Li Zefan6fb29152009-10-15 11:21:42 +08002575 case PERF_EVENT_IOC_SET_FILTER:
2576 return perf_event_set_filter(event, (void __user *)arg);
2577
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002578 default:
2579 return -ENOTTY;
2580 }
2581
2582 if (flags & PERF_IOC_FLAG_GROUP)
2583 perf_event_for_each(event, func);
2584 else
2585 perf_event_for_each_child(event, func);
2586
2587 return 0;
2588}
2589
2590int perf_event_task_enable(void)
2591{
2592 struct perf_event *event;
2593
2594 mutex_lock(&current->perf_event_mutex);
2595 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2596 perf_event_for_each_child(event, perf_event_enable);
2597 mutex_unlock(&current->perf_event_mutex);
2598
2599 return 0;
2600}
2601
2602int perf_event_task_disable(void)
2603{
2604 struct perf_event *event;
2605
2606 mutex_lock(&current->perf_event_mutex);
2607 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2608 perf_event_for_each_child(event, perf_event_disable);
2609 mutex_unlock(&current->perf_event_mutex);
2610
2611 return 0;
2612}
2613
2614#ifndef PERF_EVENT_INDEX_OFFSET
2615# define PERF_EVENT_INDEX_OFFSET 0
2616#endif
2617
2618static int perf_event_index(struct perf_event *event)
2619{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002620 if (event->hw.state & PERF_HES_STOPPED)
2621 return 0;
2622
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002623 if (event->state != PERF_EVENT_STATE_ACTIVE)
2624 return 0;
2625
2626 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2627}
2628
2629/*
2630 * Callers need to ensure there can be no nesting of this function, otherwise
2631 * the seqlock logic goes bad. We can not serialize this because the arch
2632 * code calls this from NMI context.
2633 */
2634void perf_event_update_userpage(struct perf_event *event)
2635{
2636 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002637 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002638
2639 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002640 buffer = rcu_dereference(event->buffer);
2641 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002642 goto unlock;
2643
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002644 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002645
2646 /*
2647 * Disable preemption so as to not let the corresponding user-space
2648 * spin too long if we get preempted.
2649 */
2650 preempt_disable();
2651 ++userpg->lock;
2652 barrier();
2653 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002654 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002655 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002656 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002657
2658 userpg->time_enabled = event->total_time_enabled +
2659 atomic64_read(&event->child_total_time_enabled);
2660
2661 userpg->time_running = event->total_time_running +
2662 atomic64_read(&event->child_total_time_running);
2663
2664 barrier();
2665 ++userpg->lock;
2666 preempt_enable();
2667unlock:
2668 rcu_read_unlock();
2669}
2670
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002671static unsigned long perf_data_size(struct perf_buffer *buffer);
2672
2673static void
2674perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2675{
2676 long max_size = perf_data_size(buffer);
2677
2678 if (watermark)
2679 buffer->watermark = min(max_size, watermark);
2680
2681 if (!buffer->watermark)
2682 buffer->watermark = max_size / 2;
2683
2684 if (flags & PERF_BUFFER_WRITABLE)
2685 buffer->writable = 1;
2686
2687 atomic_set(&buffer->refcount, 1);
2688}
2689
Peter Zijlstra906010b2009-09-21 16:08:49 +02002690#ifndef CONFIG_PERF_USE_VMALLOC
2691
2692/*
2693 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2694 */
2695
2696static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002697perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002698{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002699 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002700 return NULL;
2701
2702 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002703 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002704
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002705 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002706}
2707
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002708static void *perf_mmap_alloc_page(int cpu)
2709{
2710 struct page *page;
2711 int node;
2712
2713 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2714 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2715 if (!page)
2716 return NULL;
2717
2718 return page_address(page);
2719}
2720
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002721static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002722perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002723{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002724 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725 unsigned long size;
2726 int i;
2727
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002728 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002729 size += nr_pages * sizeof(void *);
2730
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002731 buffer = kzalloc(size, GFP_KERNEL);
2732 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002733 goto fail;
2734
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002735 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002736 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002737 goto fail_user_page;
2738
2739 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002740 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002741 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742 goto fail_data_pages;
2743 }
2744
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002745 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002746
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002747 perf_buffer_init(buffer, watermark, flags);
2748
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002749 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002750
2751fail_data_pages:
2752 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002753 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002754
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002755 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002756
2757fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002758 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002759
2760fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002761 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762}
2763
2764static void perf_mmap_free_page(unsigned long addr)
2765{
2766 struct page *page = virt_to_page((void *)addr);
2767
2768 page->mapping = NULL;
2769 __free_page(page);
2770}
2771
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002772static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002774 int i;
2775
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002776 perf_mmap_free_page((unsigned long)buffer->user_page);
2777 for (i = 0; i < buffer->nr_pages; i++)
2778 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2779 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002780}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002781
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002782static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002783{
2784 return 0;
2785}
2786
Peter Zijlstra906010b2009-09-21 16:08:49 +02002787#else
2788
2789/*
2790 * Back perf_mmap() with vmalloc memory.
2791 *
2792 * Required for architectures that have d-cache aliasing issues.
2793 */
2794
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002795static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002796{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002797 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002798}
2799
Peter Zijlstra906010b2009-09-21 16:08:49 +02002800static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002801perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002802{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002803 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002804 return NULL;
2805
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002806 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002807}
2808
2809static void perf_mmap_unmark_page(void *addr)
2810{
2811 struct page *page = vmalloc_to_page(addr);
2812
2813 page->mapping = NULL;
2814}
2815
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002816static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002817{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002818 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002819 void *base;
2820 int i, nr;
2821
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002822 buffer = container_of(work, struct perf_buffer, work);
2823 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002824
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002825 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002826 for (i = 0; i < nr + 1; i++)
2827 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2828
2829 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002830 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002831}
2832
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002833static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002834{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002835 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002836}
2837
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002838static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002839perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002840{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002841 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002842 unsigned long size;
2843 void *all_buf;
2844
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002845 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002846 size += sizeof(void *);
2847
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002848 buffer = kzalloc(size, GFP_KERNEL);
2849 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002850 goto fail;
2851
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002852 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002853
2854 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2855 if (!all_buf)
2856 goto fail_all_buf;
2857
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002858 buffer->user_page = all_buf;
2859 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2860 buffer->page_order = ilog2(nr_pages);
2861 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002862
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002863 perf_buffer_init(buffer, watermark, flags);
2864
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002865 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002866
2867fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002868 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002869
2870fail:
2871 return NULL;
2872}
2873
2874#endif
2875
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002876static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002877{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002878 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002879}
2880
Peter Zijlstra906010b2009-09-21 16:08:49 +02002881static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2882{
2883 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002884 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002885 int ret = VM_FAULT_SIGBUS;
2886
2887 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2888 if (vmf->pgoff == 0)
2889 ret = 0;
2890 return ret;
2891 }
2892
2893 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002894 buffer = rcu_dereference(event->buffer);
2895 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002896 goto unlock;
2897
2898 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2899 goto unlock;
2900
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002901 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002902 if (!vmf->page)
2903 goto unlock;
2904
2905 get_page(vmf->page);
2906 vmf->page->mapping = vma->vm_file->f_mapping;
2907 vmf->page->index = vmf->pgoff;
2908
2909 ret = 0;
2910unlock:
2911 rcu_read_unlock();
2912
2913 return ret;
2914}
2915
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002916static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002917{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002918 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002919
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002920 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2921 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002922}
2923
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002924static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002925{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002926 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002927
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002928 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002929 buffer = rcu_dereference(event->buffer);
2930 if (buffer) {
2931 if (!atomic_inc_not_zero(&buffer->refcount))
2932 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002933 }
2934 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002936 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002937}
2938
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002939static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002940{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002941 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002942 return;
2943
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002944 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002945}
2946
2947static void perf_mmap_open(struct vm_area_struct *vma)
2948{
2949 struct perf_event *event = vma->vm_file->private_data;
2950
2951 atomic_inc(&event->mmap_count);
2952}
2953
2954static void perf_mmap_close(struct vm_area_struct *vma)
2955{
2956 struct perf_event *event = vma->vm_file->private_data;
2957
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002958 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002959 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002960 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002961 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002962
Peter Zijlstra906010b2009-09-21 16:08:49 +02002963 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002964 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002965 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002966 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002967
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002968 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002969 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002970 }
2971}
2972
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002973static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002974 .open = perf_mmap_open,
2975 .close = perf_mmap_close,
2976 .fault = perf_mmap_fault,
2977 .page_mkwrite = perf_mmap_fault,
2978};
2979
2980static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2981{
2982 struct perf_event *event = file->private_data;
2983 unsigned long user_locked, user_lock_limit;
2984 struct user_struct *user = current_user();
2985 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002986 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002987 unsigned long vma_size;
2988 unsigned long nr_pages;
2989 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002990 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002991
Peter Zijlstrac7920612010-05-18 10:33:24 +02002992 /*
2993 * Don't allow mmap() of inherited per-task counters. This would
2994 * create a performance issue due to all children writing to the
2995 * same buffer.
2996 */
2997 if (event->cpu == -1 && event->attr.inherit)
2998 return -EINVAL;
2999
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003000 if (!(vma->vm_flags & VM_SHARED))
3001 return -EINVAL;
3002
3003 vma_size = vma->vm_end - vma->vm_start;
3004 nr_pages = (vma_size / PAGE_SIZE) - 1;
3005
3006 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003007 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003008 * can do bitmasks instead of modulo.
3009 */
3010 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3011 return -EINVAL;
3012
3013 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3014 return -EINVAL;
3015
3016 if (vma->vm_pgoff != 0)
3017 return -EINVAL;
3018
3019 WARN_ON_ONCE(event->ctx->parent_ctx);
3020 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003021 if (event->buffer) {
3022 if (event->buffer->nr_pages == nr_pages)
3023 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003024 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003025 ret = -EINVAL;
3026 goto unlock;
3027 }
3028
3029 user_extra = nr_pages + 1;
3030 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3031
3032 /*
3033 * Increase the limit linearly with more CPUs:
3034 */
3035 user_lock_limit *= num_online_cpus();
3036
3037 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3038
3039 extra = 0;
3040 if (user_locked > user_lock_limit)
3041 extra = user_locked - user_lock_limit;
3042
Jiri Slaby78d7d402010-03-05 13:42:54 -08003043 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044 lock_limit >>= PAGE_SHIFT;
3045 locked = vma->vm_mm->locked_vm + extra;
3046
3047 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3048 !capable(CAP_IPC_LOCK)) {
3049 ret = -EPERM;
3050 goto unlock;
3051 }
3052
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003053 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003054
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003055 if (vma->vm_flags & VM_WRITE)
3056 flags |= PERF_BUFFER_WRITABLE;
3057
3058 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3059 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003060 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003061 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003062 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003063 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003064 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003065
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003066 atomic_long_add(user_extra, &user->locked_vm);
3067 event->mmap_locked = extra;
3068 event->mmap_user = get_current_user();
3069 vma->vm_mm->locked_vm += event->mmap_locked;
3070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003071unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003072 if (!ret)
3073 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003074 mutex_unlock(&event->mmap_mutex);
3075
3076 vma->vm_flags |= VM_RESERVED;
3077 vma->vm_ops = &perf_mmap_vmops;
3078
3079 return ret;
3080}
3081
3082static int perf_fasync(int fd, struct file *filp, int on)
3083{
3084 struct inode *inode = filp->f_path.dentry->d_inode;
3085 struct perf_event *event = filp->private_data;
3086 int retval;
3087
3088 mutex_lock(&inode->i_mutex);
3089 retval = fasync_helper(fd, filp, on, &event->fasync);
3090 mutex_unlock(&inode->i_mutex);
3091
3092 if (retval < 0)
3093 return retval;
3094
3095 return 0;
3096}
3097
3098static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003099 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003100 .release = perf_release,
3101 .read = perf_read,
3102 .poll = perf_poll,
3103 .unlocked_ioctl = perf_ioctl,
3104 .compat_ioctl = perf_ioctl,
3105 .mmap = perf_mmap,
3106 .fasync = perf_fasync,
3107};
3108
3109/*
3110 * Perf event wakeup
3111 *
3112 * If there's data, ensure we set the poll() state and publish everything
3113 * to user-space before waking everybody up.
3114 */
3115
3116void perf_event_wakeup(struct perf_event *event)
3117{
3118 wake_up_all(&event->waitq);
3119
3120 if (event->pending_kill) {
3121 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3122 event->pending_kill = 0;
3123 }
3124}
3125
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003126static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003127{
3128 struct perf_event *event = container_of(entry,
3129 struct perf_event, pending);
3130
3131 if (event->pending_disable) {
3132 event->pending_disable = 0;
3133 __perf_event_disable(event);
3134 }
3135
3136 if (event->pending_wakeup) {
3137 event->pending_wakeup = 0;
3138 perf_event_wakeup(event);
3139 }
3140}
3141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003143 * We assume there is only KVM supporting the callbacks.
3144 * Later on, we might change it to a list if there is
3145 * another virtualization implementation supporting the callbacks.
3146 */
3147struct perf_guest_info_callbacks *perf_guest_cbs;
3148
3149int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3150{
3151 perf_guest_cbs = cbs;
3152 return 0;
3153}
3154EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3155
3156int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3157{
3158 perf_guest_cbs = NULL;
3159 return 0;
3160}
3161EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3162
3163/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003164 * Output
3165 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003166static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167 unsigned long offset, unsigned long head)
3168{
3169 unsigned long mask;
3170
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003171 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003172 return true;
3173
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003174 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175
3176 offset = (offset - tail) & mask;
3177 head = (head - tail) & mask;
3178
3179 if ((int)(head - offset) < 0)
3180 return false;
3181
3182 return true;
3183}
3184
3185static void perf_output_wakeup(struct perf_output_handle *handle)
3186{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003187 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003188
3189 if (handle->nmi) {
3190 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003191 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192 } else
3193 perf_event_wakeup(handle->event);
3194}
3195
3196/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003197 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003198 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199 * cannot fully serialize things.
3200 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003202 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003204static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003206 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207
Peter Zijlstraef607772010-05-18 10:50:41 +02003208 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003209 local_inc(&buffer->nest);
3210 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211}
3212
Peter Zijlstraef607772010-05-18 10:50:41 +02003213static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003215 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217
3218again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003219 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003220
3221 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003222 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003223 */
3224
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003225 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003226 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227
3228 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003229 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003230 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003231 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003233 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234
Peter Zijlstraef607772010-05-18 10:50:41 +02003235 /*
3236 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003237 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003238 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003239 if (unlikely(head != local_read(&buffer->head))) {
3240 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003241 goto again;
3242 }
3243
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003244 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003246
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003247out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003248 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003249}
3250
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003251__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252 const void *buf, unsigned int len)
3253{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003254 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003255 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003256
3257 memcpy(handle->addr, buf, size);
3258
3259 len -= size;
3260 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003261 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003262 handle->size -= size;
3263 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003264 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003265
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003266 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003267 handle->page &= buffer->nr_pages - 1;
3268 handle->addr = buffer->data_pages[handle->page];
3269 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003270 }
3271 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003272}
3273
3274int perf_output_begin(struct perf_output_handle *handle,
3275 struct perf_event *event, unsigned int size,
3276 int nmi, int sample)
3277{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003278 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279 unsigned long tail, offset, head;
3280 int have_lost;
3281 struct {
3282 struct perf_event_header header;
3283 u64 id;
3284 u64 lost;
3285 } lost_event;
3286
3287 rcu_read_lock();
3288 /*
3289 * For inherited events we send all the output towards the parent.
3290 */
3291 if (event->parent)
3292 event = event->parent;
3293
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003294 buffer = rcu_dereference(event->buffer);
3295 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003296 goto out;
3297
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003298 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299 handle->event = event;
3300 handle->nmi = nmi;
3301 handle->sample = sample;
3302
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003303 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003304 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003306 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003307 if (have_lost)
3308 size += sizeof(lost_event);
3309
Peter Zijlstraef607772010-05-18 10:50:41 +02003310 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003311
3312 do {
3313 /*
3314 * Userspace could choose to issue a mb() before updating the
3315 * tail pointer. So that all reads will be completed before the
3316 * write is issued.
3317 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003318 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003319 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003320 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003322 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003324 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003326 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3327 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003328
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003329 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3330 handle->page &= buffer->nr_pages - 1;
3331 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3332 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003333 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003334 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336 if (have_lost) {
3337 lost_event.header.type = PERF_RECORD_LOST;
3338 lost_event.header.misc = 0;
3339 lost_event.header.size = sizeof(lost_event);
3340 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003341 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342
3343 perf_output_put(handle, lost_event);
3344 }
3345
3346 return 0;
3347
3348fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003350 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351out:
3352 rcu_read_unlock();
3353
3354 return -ENOSPC;
3355}
3356
3357void perf_output_end(struct perf_output_handle *handle)
3358{
3359 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003360 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361
3362 int wakeup_events = event->attr.wakeup_events;
3363
3364 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003365 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003367 local_sub(wakeup_events, &buffer->events);
3368 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003369 }
3370 }
3371
Peter Zijlstraef607772010-05-18 10:50:41 +02003372 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003373 rcu_read_unlock();
3374}
3375
3376static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3377{
3378 /*
3379 * only top level events have the pid namespace they were created in
3380 */
3381 if (event->parent)
3382 event = event->parent;
3383
3384 return task_tgid_nr_ns(p, event->ns);
3385}
3386
3387static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3388{
3389 /*
3390 * only top level events have the pid namespace they were created in
3391 */
3392 if (event->parent)
3393 event = event->parent;
3394
3395 return task_pid_nr_ns(p, event->ns);
3396}
3397
3398static void perf_output_read_one(struct perf_output_handle *handle,
3399 struct perf_event *event)
3400{
3401 u64 read_format = event->attr.read_format;
3402 u64 values[4];
3403 int n = 0;
3404
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003405 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3407 values[n++] = event->total_time_enabled +
3408 atomic64_read(&event->child_total_time_enabled);
3409 }
3410 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3411 values[n++] = event->total_time_running +
3412 atomic64_read(&event->child_total_time_running);
3413 }
3414 if (read_format & PERF_FORMAT_ID)
3415 values[n++] = primary_event_id(event);
3416
3417 perf_output_copy(handle, values, n * sizeof(u64));
3418}
3419
3420/*
3421 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3422 */
3423static void perf_output_read_group(struct perf_output_handle *handle,
3424 struct perf_event *event)
3425{
3426 struct perf_event *leader = event->group_leader, *sub;
3427 u64 read_format = event->attr.read_format;
3428 u64 values[5];
3429 int n = 0;
3430
3431 values[n++] = 1 + leader->nr_siblings;
3432
3433 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3434 values[n++] = leader->total_time_enabled;
3435
3436 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3437 values[n++] = leader->total_time_running;
3438
3439 if (leader != event)
3440 leader->pmu->read(leader);
3441
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003442 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003443 if (read_format & PERF_FORMAT_ID)
3444 values[n++] = primary_event_id(leader);
3445
3446 perf_output_copy(handle, values, n * sizeof(u64));
3447
3448 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3449 n = 0;
3450
3451 if (sub != event)
3452 sub->pmu->read(sub);
3453
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003454 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455 if (read_format & PERF_FORMAT_ID)
3456 values[n++] = primary_event_id(sub);
3457
3458 perf_output_copy(handle, values, n * sizeof(u64));
3459 }
3460}
3461
3462static void perf_output_read(struct perf_output_handle *handle,
3463 struct perf_event *event)
3464{
3465 if (event->attr.read_format & PERF_FORMAT_GROUP)
3466 perf_output_read_group(handle, event);
3467 else
3468 perf_output_read_one(handle, event);
3469}
3470
3471void perf_output_sample(struct perf_output_handle *handle,
3472 struct perf_event_header *header,
3473 struct perf_sample_data *data,
3474 struct perf_event *event)
3475{
3476 u64 sample_type = data->type;
3477
3478 perf_output_put(handle, *header);
3479
3480 if (sample_type & PERF_SAMPLE_IP)
3481 perf_output_put(handle, data->ip);
3482
3483 if (sample_type & PERF_SAMPLE_TID)
3484 perf_output_put(handle, data->tid_entry);
3485
3486 if (sample_type & PERF_SAMPLE_TIME)
3487 perf_output_put(handle, data->time);
3488
3489 if (sample_type & PERF_SAMPLE_ADDR)
3490 perf_output_put(handle, data->addr);
3491
3492 if (sample_type & PERF_SAMPLE_ID)
3493 perf_output_put(handle, data->id);
3494
3495 if (sample_type & PERF_SAMPLE_STREAM_ID)
3496 perf_output_put(handle, data->stream_id);
3497
3498 if (sample_type & PERF_SAMPLE_CPU)
3499 perf_output_put(handle, data->cpu_entry);
3500
3501 if (sample_type & PERF_SAMPLE_PERIOD)
3502 perf_output_put(handle, data->period);
3503
3504 if (sample_type & PERF_SAMPLE_READ)
3505 perf_output_read(handle, event);
3506
3507 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3508 if (data->callchain) {
3509 int size = 1;
3510
3511 if (data->callchain)
3512 size += data->callchain->nr;
3513
3514 size *= sizeof(u64);
3515
3516 perf_output_copy(handle, data->callchain, size);
3517 } else {
3518 u64 nr = 0;
3519 perf_output_put(handle, nr);
3520 }
3521 }
3522
3523 if (sample_type & PERF_SAMPLE_RAW) {
3524 if (data->raw) {
3525 perf_output_put(handle, data->raw->size);
3526 perf_output_copy(handle, data->raw->data,
3527 data->raw->size);
3528 } else {
3529 struct {
3530 u32 size;
3531 u32 data;
3532 } raw = {
3533 .size = sizeof(u32),
3534 .data = 0,
3535 };
3536 perf_output_put(handle, raw);
3537 }
3538 }
3539}
3540
3541void perf_prepare_sample(struct perf_event_header *header,
3542 struct perf_sample_data *data,
3543 struct perf_event *event,
3544 struct pt_regs *regs)
3545{
3546 u64 sample_type = event->attr.sample_type;
3547
3548 data->type = sample_type;
3549
3550 header->type = PERF_RECORD_SAMPLE;
3551 header->size = sizeof(*header);
3552
3553 header->misc = 0;
3554 header->misc |= perf_misc_flags(regs);
3555
3556 if (sample_type & PERF_SAMPLE_IP) {
3557 data->ip = perf_instruction_pointer(regs);
3558
3559 header->size += sizeof(data->ip);
3560 }
3561
3562 if (sample_type & PERF_SAMPLE_TID) {
3563 /* namespace issues */
3564 data->tid_entry.pid = perf_event_pid(event, current);
3565 data->tid_entry.tid = perf_event_tid(event, current);
3566
3567 header->size += sizeof(data->tid_entry);
3568 }
3569
3570 if (sample_type & PERF_SAMPLE_TIME) {
3571 data->time = perf_clock();
3572
3573 header->size += sizeof(data->time);
3574 }
3575
3576 if (sample_type & PERF_SAMPLE_ADDR)
3577 header->size += sizeof(data->addr);
3578
3579 if (sample_type & PERF_SAMPLE_ID) {
3580 data->id = primary_event_id(event);
3581
3582 header->size += sizeof(data->id);
3583 }
3584
3585 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3586 data->stream_id = event->id;
3587
3588 header->size += sizeof(data->stream_id);
3589 }
3590
3591 if (sample_type & PERF_SAMPLE_CPU) {
3592 data->cpu_entry.cpu = raw_smp_processor_id();
3593 data->cpu_entry.reserved = 0;
3594
3595 header->size += sizeof(data->cpu_entry);
3596 }
3597
3598 if (sample_type & PERF_SAMPLE_PERIOD)
3599 header->size += sizeof(data->period);
3600
3601 if (sample_type & PERF_SAMPLE_READ)
3602 header->size += perf_event_read_size(event);
3603
3604 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3605 int size = 1;
3606
3607 data->callchain = perf_callchain(regs);
3608
3609 if (data->callchain)
3610 size += data->callchain->nr;
3611
3612 header->size += size * sizeof(u64);
3613 }
3614
3615 if (sample_type & PERF_SAMPLE_RAW) {
3616 int size = sizeof(u32);
3617
3618 if (data->raw)
3619 size += data->raw->size;
3620 else
3621 size += sizeof(u32);
3622
3623 WARN_ON_ONCE(size & (sizeof(u64)-1));
3624 header->size += size;
3625 }
3626}
3627
3628static void perf_event_output(struct perf_event *event, int nmi,
3629 struct perf_sample_data *data,
3630 struct pt_regs *regs)
3631{
3632 struct perf_output_handle handle;
3633 struct perf_event_header header;
3634
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003635 /* protect the callchain buffers */
3636 rcu_read_lock();
3637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003638 perf_prepare_sample(&header, data, event, regs);
3639
3640 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003641 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003642
3643 perf_output_sample(&handle, &header, data, event);
3644
3645 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003646
3647exit:
3648 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649}
3650
3651/*
3652 * read event_id
3653 */
3654
3655struct perf_read_event {
3656 struct perf_event_header header;
3657
3658 u32 pid;
3659 u32 tid;
3660};
3661
3662static void
3663perf_event_read_event(struct perf_event *event,
3664 struct task_struct *task)
3665{
3666 struct perf_output_handle handle;
3667 struct perf_read_event read_event = {
3668 .header = {
3669 .type = PERF_RECORD_READ,
3670 .misc = 0,
3671 .size = sizeof(read_event) + perf_event_read_size(event),
3672 },
3673 .pid = perf_event_pid(event, task),
3674 .tid = perf_event_tid(event, task),
3675 };
3676 int ret;
3677
3678 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3679 if (ret)
3680 return;
3681
3682 perf_output_put(&handle, read_event);
3683 perf_output_read(&handle, event);
3684
3685 perf_output_end(&handle);
3686}
3687
3688/*
3689 * task tracking -- fork/exit
3690 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003691 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003692 */
3693
3694struct perf_task_event {
3695 struct task_struct *task;
3696 struct perf_event_context *task_ctx;
3697
3698 struct {
3699 struct perf_event_header header;
3700
3701 u32 pid;
3702 u32 ppid;
3703 u32 tid;
3704 u32 ptid;
3705 u64 time;
3706 } event_id;
3707};
3708
3709static void perf_event_task_output(struct perf_event *event,
3710 struct perf_task_event *task_event)
3711{
3712 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003713 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003714 int size, ret;
3715
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003716 size = task_event->event_id.header.size;
3717 ret = perf_output_begin(&handle, event, size, 0, 0);
3718
Peter Zijlstraef607772010-05-18 10:50:41 +02003719 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003720 return;
3721
3722 task_event->event_id.pid = perf_event_pid(event, task);
3723 task_event->event_id.ppid = perf_event_pid(event, current);
3724
3725 task_event->event_id.tid = perf_event_tid(event, task);
3726 task_event->event_id.ptid = perf_event_tid(event, current);
3727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003728 perf_output_put(&handle, task_event->event_id);
3729
3730 perf_output_end(&handle);
3731}
3732
3733static int perf_event_task_match(struct perf_event *event)
3734{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003735 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003736 return 0;
3737
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003738 if (event->cpu != -1 && event->cpu != smp_processor_id())
3739 return 0;
3740
Eric B Munson3af9e852010-05-18 15:30:49 +01003741 if (event->attr.comm || event->attr.mmap ||
3742 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003743 return 1;
3744
3745 return 0;
3746}
3747
3748static void perf_event_task_ctx(struct perf_event_context *ctx,
3749 struct perf_task_event *task_event)
3750{
3751 struct perf_event *event;
3752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003753 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3754 if (perf_event_task_match(event))
3755 perf_event_task_output(event, task_event);
3756 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757}
3758
3759static void perf_event_task_event(struct perf_task_event *task_event)
3760{
3761 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003762 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003763 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003764 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003765
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003766 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003767 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003768 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003769 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003770
3771 ctx = task_event->task_ctx;
3772 if (!ctx) {
3773 ctxn = pmu->task_ctx_nr;
3774 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003775 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003776 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3777 }
3778 if (ctx)
3779 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003780next:
3781 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003782 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003783 rcu_read_unlock();
3784}
3785
3786static void perf_event_task(struct task_struct *task,
3787 struct perf_event_context *task_ctx,
3788 int new)
3789{
3790 struct perf_task_event task_event;
3791
3792 if (!atomic_read(&nr_comm_events) &&
3793 !atomic_read(&nr_mmap_events) &&
3794 !atomic_read(&nr_task_events))
3795 return;
3796
3797 task_event = (struct perf_task_event){
3798 .task = task,
3799 .task_ctx = task_ctx,
3800 .event_id = {
3801 .header = {
3802 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3803 .misc = 0,
3804 .size = sizeof(task_event.event_id),
3805 },
3806 /* .pid */
3807 /* .ppid */
3808 /* .tid */
3809 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003810 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003811 },
3812 };
3813
3814 perf_event_task_event(&task_event);
3815}
3816
3817void perf_event_fork(struct task_struct *task)
3818{
3819 perf_event_task(task, NULL, 1);
3820}
3821
3822/*
3823 * comm tracking
3824 */
3825
3826struct perf_comm_event {
3827 struct task_struct *task;
3828 char *comm;
3829 int comm_size;
3830
3831 struct {
3832 struct perf_event_header header;
3833
3834 u32 pid;
3835 u32 tid;
3836 } event_id;
3837};
3838
3839static void perf_event_comm_output(struct perf_event *event,
3840 struct perf_comm_event *comm_event)
3841{
3842 struct perf_output_handle handle;
3843 int size = comm_event->event_id.header.size;
3844 int ret = perf_output_begin(&handle, event, size, 0, 0);
3845
3846 if (ret)
3847 return;
3848
3849 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3850 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3851
3852 perf_output_put(&handle, comm_event->event_id);
3853 perf_output_copy(&handle, comm_event->comm,
3854 comm_event->comm_size);
3855 perf_output_end(&handle);
3856}
3857
3858static int perf_event_comm_match(struct perf_event *event)
3859{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003860 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003861 return 0;
3862
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003863 if (event->cpu != -1 && event->cpu != smp_processor_id())
3864 return 0;
3865
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003866 if (event->attr.comm)
3867 return 1;
3868
3869 return 0;
3870}
3871
3872static void perf_event_comm_ctx(struct perf_event_context *ctx,
3873 struct perf_comm_event *comm_event)
3874{
3875 struct perf_event *event;
3876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003877 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3878 if (perf_event_comm_match(event))
3879 perf_event_comm_output(event, comm_event);
3880 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881}
3882
3883static void perf_event_comm_event(struct perf_comm_event *comm_event)
3884{
3885 struct perf_cpu_context *cpuctx;
3886 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003887 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003889 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003890 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003891
3892 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003893 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003894 size = ALIGN(strlen(comm)+1, sizeof(u64));
3895
3896 comm_event->comm = comm;
3897 comm_event->comm_size = size;
3898
3899 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3900
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003901 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003902 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003903 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003904 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003905
3906 ctxn = pmu->task_ctx_nr;
3907 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003908 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003909
3910 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3911 if (ctx)
3912 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003913next:
3914 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003915 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 rcu_read_unlock();
3917}
3918
3919void perf_event_comm(struct task_struct *task)
3920{
3921 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003922 struct perf_event_context *ctx;
3923 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003925 for_each_task_context_nr(ctxn) {
3926 ctx = task->perf_event_ctxp[ctxn];
3927 if (!ctx)
3928 continue;
3929
3930 perf_event_enable_on_exec(ctx);
3931 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003932
3933 if (!atomic_read(&nr_comm_events))
3934 return;
3935
3936 comm_event = (struct perf_comm_event){
3937 .task = task,
3938 /* .comm */
3939 /* .comm_size */
3940 .event_id = {
3941 .header = {
3942 .type = PERF_RECORD_COMM,
3943 .misc = 0,
3944 /* .size */
3945 },
3946 /* .pid */
3947 /* .tid */
3948 },
3949 };
3950
3951 perf_event_comm_event(&comm_event);
3952}
3953
3954/*
3955 * mmap tracking
3956 */
3957
3958struct perf_mmap_event {
3959 struct vm_area_struct *vma;
3960
3961 const char *file_name;
3962 int file_size;
3963
3964 struct {
3965 struct perf_event_header header;
3966
3967 u32 pid;
3968 u32 tid;
3969 u64 start;
3970 u64 len;
3971 u64 pgoff;
3972 } event_id;
3973};
3974
3975static void perf_event_mmap_output(struct perf_event *event,
3976 struct perf_mmap_event *mmap_event)
3977{
3978 struct perf_output_handle handle;
3979 int size = mmap_event->event_id.header.size;
3980 int ret = perf_output_begin(&handle, event, size, 0, 0);
3981
3982 if (ret)
3983 return;
3984
3985 mmap_event->event_id.pid = perf_event_pid(event, current);
3986 mmap_event->event_id.tid = perf_event_tid(event, current);
3987
3988 perf_output_put(&handle, mmap_event->event_id);
3989 perf_output_copy(&handle, mmap_event->file_name,
3990 mmap_event->file_size);
3991 perf_output_end(&handle);
3992}
3993
3994static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003995 struct perf_mmap_event *mmap_event,
3996 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003997{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003998 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003999 return 0;
4000
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004001 if (event->cpu != -1 && event->cpu != smp_processor_id())
4002 return 0;
4003
Eric B Munson3af9e852010-05-18 15:30:49 +01004004 if ((!executable && event->attr.mmap_data) ||
4005 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004006 return 1;
4007
4008 return 0;
4009}
4010
4011static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004012 struct perf_mmap_event *mmap_event,
4013 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004014{
4015 struct perf_event *event;
4016
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004017 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004018 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004019 perf_event_mmap_output(event, mmap_event);
4020 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004021}
4022
4023static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4024{
4025 struct perf_cpu_context *cpuctx;
4026 struct perf_event_context *ctx;
4027 struct vm_area_struct *vma = mmap_event->vma;
4028 struct file *file = vma->vm_file;
4029 unsigned int size;
4030 char tmp[16];
4031 char *buf = NULL;
4032 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004033 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004034 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004035
4036 memset(tmp, 0, sizeof(tmp));
4037
4038 if (file) {
4039 /*
4040 * d_path works from the end of the buffer backwards, so we
4041 * need to add enough zero bytes after the string to handle
4042 * the 64bit alignment we do later.
4043 */
4044 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4045 if (!buf) {
4046 name = strncpy(tmp, "//enomem", sizeof(tmp));
4047 goto got_name;
4048 }
4049 name = d_path(&file->f_path, buf, PATH_MAX);
4050 if (IS_ERR(name)) {
4051 name = strncpy(tmp, "//toolong", sizeof(tmp));
4052 goto got_name;
4053 }
4054 } else {
4055 if (arch_vma_name(mmap_event->vma)) {
4056 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4057 sizeof(tmp));
4058 goto got_name;
4059 }
4060
4061 if (!vma->vm_mm) {
4062 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4063 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004064 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4065 vma->vm_end >= vma->vm_mm->brk) {
4066 name = strncpy(tmp, "[heap]", sizeof(tmp));
4067 goto got_name;
4068 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4069 vma->vm_end >= vma->vm_mm->start_stack) {
4070 name = strncpy(tmp, "[stack]", sizeof(tmp));
4071 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004072 }
4073
4074 name = strncpy(tmp, "//anon", sizeof(tmp));
4075 goto got_name;
4076 }
4077
4078got_name:
4079 size = ALIGN(strlen(name)+1, sizeof(u64));
4080
4081 mmap_event->file_name = name;
4082 mmap_event->file_size = size;
4083
4084 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4085
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004086 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004087 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004088 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004089 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4090 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004091
4092 ctxn = pmu->task_ctx_nr;
4093 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004094 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004095
4096 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4097 if (ctx) {
4098 perf_event_mmap_ctx(ctx, mmap_event,
4099 vma->vm_flags & VM_EXEC);
4100 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004101next:
4102 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004103 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004104 rcu_read_unlock();
4105
4106 kfree(buf);
4107}
4108
Eric B Munson3af9e852010-05-18 15:30:49 +01004109void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110{
4111 struct perf_mmap_event mmap_event;
4112
4113 if (!atomic_read(&nr_mmap_events))
4114 return;
4115
4116 mmap_event = (struct perf_mmap_event){
4117 .vma = vma,
4118 /* .file_name */
4119 /* .file_size */
4120 .event_id = {
4121 .header = {
4122 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004123 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004124 /* .size */
4125 },
4126 /* .pid */
4127 /* .tid */
4128 .start = vma->vm_start,
4129 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004130 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004131 },
4132 };
4133
4134 perf_event_mmap_event(&mmap_event);
4135}
4136
4137/*
4138 * IRQ throttle logging
4139 */
4140
4141static void perf_log_throttle(struct perf_event *event, int enable)
4142{
4143 struct perf_output_handle handle;
4144 int ret;
4145
4146 struct {
4147 struct perf_event_header header;
4148 u64 time;
4149 u64 id;
4150 u64 stream_id;
4151 } throttle_event = {
4152 .header = {
4153 .type = PERF_RECORD_THROTTLE,
4154 .misc = 0,
4155 .size = sizeof(throttle_event),
4156 },
4157 .time = perf_clock(),
4158 .id = primary_event_id(event),
4159 .stream_id = event->id,
4160 };
4161
4162 if (enable)
4163 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4164
4165 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4166 if (ret)
4167 return;
4168
4169 perf_output_put(&handle, throttle_event);
4170 perf_output_end(&handle);
4171}
4172
4173/*
4174 * Generic event overflow handling, sampling.
4175 */
4176
4177static int __perf_event_overflow(struct perf_event *event, int nmi,
4178 int throttle, struct perf_sample_data *data,
4179 struct pt_regs *regs)
4180{
4181 int events = atomic_read(&event->event_limit);
4182 struct hw_perf_event *hwc = &event->hw;
4183 int ret = 0;
4184
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004185 if (!throttle) {
4186 hwc->interrupts++;
4187 } else {
4188 if (hwc->interrupts != MAX_INTERRUPTS) {
4189 hwc->interrupts++;
4190 if (HZ * hwc->interrupts >
4191 (u64)sysctl_perf_event_sample_rate) {
4192 hwc->interrupts = MAX_INTERRUPTS;
4193 perf_log_throttle(event, 0);
4194 ret = 1;
4195 }
4196 } else {
4197 /*
4198 * Keep re-disabling events even though on the previous
4199 * pass we disabled it - just in case we raced with a
4200 * sched-in and the event got enabled again:
4201 */
4202 ret = 1;
4203 }
4204 }
4205
4206 if (event->attr.freq) {
4207 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004208 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004209
Peter Zijlstraabd50712010-01-26 18:50:16 +01004210 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004211
Peter Zijlstraabd50712010-01-26 18:50:16 +01004212 if (delta > 0 && delta < 2*TICK_NSEC)
4213 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004214 }
4215
4216 /*
4217 * XXX event_limit might not quite work as expected on inherited
4218 * events
4219 */
4220
4221 event->pending_kill = POLL_IN;
4222 if (events && atomic_dec_and_test(&event->event_limit)) {
4223 ret = 1;
4224 event->pending_kill = POLL_HUP;
4225 if (nmi) {
4226 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004227 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228 } else
4229 perf_event_disable(event);
4230 }
4231
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004232 if (event->overflow_handler)
4233 event->overflow_handler(event, nmi, data, regs);
4234 else
4235 perf_event_output(event, nmi, data, regs);
4236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004237 return ret;
4238}
4239
4240int perf_event_overflow(struct perf_event *event, int nmi,
4241 struct perf_sample_data *data,
4242 struct pt_regs *regs)
4243{
4244 return __perf_event_overflow(event, nmi, 1, data, regs);
4245}
4246
4247/*
4248 * Generic software event infrastructure
4249 */
4250
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004251struct swevent_htable {
4252 struct swevent_hlist *swevent_hlist;
4253 struct mutex hlist_mutex;
4254 int hlist_refcount;
4255
4256 /* Recursion avoidance in each contexts */
4257 int recursion[PERF_NR_CONTEXTS];
4258};
4259
4260static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4261
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262/*
4263 * We directly increment event->count and keep a second value in
4264 * event->hw.period_left to count intervals. This period event
4265 * is kept in the range [-sample_period, 0] so that we can use the
4266 * sign as trigger.
4267 */
4268
4269static u64 perf_swevent_set_period(struct perf_event *event)
4270{
4271 struct hw_perf_event *hwc = &event->hw;
4272 u64 period = hwc->last_period;
4273 u64 nr, offset;
4274 s64 old, val;
4275
4276 hwc->last_period = hwc->sample_period;
4277
4278again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004279 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280 if (val < 0)
4281 return 0;
4282
4283 nr = div64_u64(period + val, period);
4284 offset = nr * period;
4285 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004286 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004287 goto again;
4288
4289 return nr;
4290}
4291
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004292static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004293 int nmi, struct perf_sample_data *data,
4294 struct pt_regs *regs)
4295{
4296 struct hw_perf_event *hwc = &event->hw;
4297 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298
4299 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004300 if (!overflow)
4301 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302
4303 if (hwc->interrupts == MAX_INTERRUPTS)
4304 return;
4305
4306 for (; overflow; overflow--) {
4307 if (__perf_event_overflow(event, nmi, throttle,
4308 data, regs)) {
4309 /*
4310 * We inhibit the overflow from happening when
4311 * hwc->interrupts == MAX_INTERRUPTS.
4312 */
4313 break;
4314 }
4315 throttle = 1;
4316 }
4317}
4318
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004319static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004320 int nmi, struct perf_sample_data *data,
4321 struct pt_regs *regs)
4322{
4323 struct hw_perf_event *hwc = &event->hw;
4324
Peter Zijlstrae7850592010-05-21 14:43:08 +02004325 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004327 if (!regs)
4328 return;
4329
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004330 if (!hwc->sample_period)
4331 return;
4332
4333 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4334 return perf_swevent_overflow(event, 1, nmi, data, regs);
4335
Peter Zijlstrae7850592010-05-21 14:43:08 +02004336 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004337 return;
4338
4339 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004340}
4341
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004342static int perf_exclude_event(struct perf_event *event,
4343 struct pt_regs *regs)
4344{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004345 if (event->hw.state & PERF_HES_STOPPED)
4346 return 0;
4347
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004348 if (regs) {
4349 if (event->attr.exclude_user && user_mode(regs))
4350 return 1;
4351
4352 if (event->attr.exclude_kernel && !user_mode(regs))
4353 return 1;
4354 }
4355
4356 return 0;
4357}
4358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004359static int perf_swevent_match(struct perf_event *event,
4360 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004361 u32 event_id,
4362 struct perf_sample_data *data,
4363 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004364{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004365 if (event->attr.type != type)
4366 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 if (event->attr.config != event_id)
4369 return 0;
4370
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004371 if (perf_exclude_event(event, regs))
4372 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004373
4374 return 1;
4375}
4376
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004377static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004378{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004379 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004380
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004381 return hash_64(val, SWEVENT_HLIST_BITS);
4382}
4383
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004384static inline struct hlist_head *
4385__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004386{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004387 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004388
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004389 return &hlist->heads[hash];
4390}
4391
4392/* For the read side: events when they trigger */
4393static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004394find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004395{
4396 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004397
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004398 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004399 if (!hlist)
4400 return NULL;
4401
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004402 return __find_swevent_head(hlist, type, event_id);
4403}
4404
4405/* For the event head insertion and removal in the hlist */
4406static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004407find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004408{
4409 struct swevent_hlist *hlist;
4410 u32 event_id = event->attr.config;
4411 u64 type = event->attr.type;
4412
4413 /*
4414 * Event scheduling is always serialized against hlist allocation
4415 * and release. Which makes the protected version suitable here.
4416 * The context lock guarantees that.
4417 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004418 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004419 lockdep_is_held(&event->ctx->lock));
4420 if (!hlist)
4421 return NULL;
4422
4423 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004424}
4425
4426static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4427 u64 nr, int nmi,
4428 struct perf_sample_data *data,
4429 struct pt_regs *regs)
4430{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004431 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004432 struct perf_event *event;
4433 struct hlist_node *node;
4434 struct hlist_head *head;
4435
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004436 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004437 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004438 if (!head)
4439 goto end;
4440
4441 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004442 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004443 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004444 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004445end:
4446 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004447}
4448
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004449int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004450{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004451 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004452
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004453 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004455EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004456
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004457void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004459 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004460
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004461 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004462}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004464void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4465 struct pt_regs *regs, u64 addr)
4466{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004467 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004468 int rctx;
4469
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004470 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004471 rctx = perf_swevent_get_recursion_context();
4472 if (rctx < 0)
4473 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004474
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004475 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004476
4477 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004478
4479 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004480 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481}
4482
4483static void perf_swevent_read(struct perf_event *event)
4484{
4485}
4486
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004487static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004489 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004490 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004491 struct hlist_head *head;
4492
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493 if (hwc->sample_period) {
4494 hwc->last_period = hwc->sample_period;
4495 perf_swevent_set_period(event);
4496 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004497
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004498 hwc->state = !(flags & PERF_EF_START);
4499
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004500 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004501 if (WARN_ON_ONCE(!head))
4502 return -EINVAL;
4503
4504 hlist_add_head_rcu(&event->hlist_entry, head);
4505
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506 return 0;
4507}
4508
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004509static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004511 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512}
4513
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004514static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004515{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004516 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004517}
4518
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004519static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004520{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004521 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004522}
4523
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004524/* Deref the hlist from the update side */
4525static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004526swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004527{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004528 return rcu_dereference_protected(swhash->swevent_hlist,
4529 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004530}
4531
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004532static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4533{
4534 struct swevent_hlist *hlist;
4535
4536 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4537 kfree(hlist);
4538}
4539
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004540static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004541{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004542 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004543
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004544 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004545 return;
4546
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004547 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004548 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4549}
4550
4551static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4552{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004553 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004554
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004555 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004556
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004557 if (!--swhash->hlist_refcount)
4558 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004559
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004560 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004561}
4562
4563static void swevent_hlist_put(struct perf_event *event)
4564{
4565 int cpu;
4566
4567 if (event->cpu != -1) {
4568 swevent_hlist_put_cpu(event, event->cpu);
4569 return;
4570 }
4571
4572 for_each_possible_cpu(cpu)
4573 swevent_hlist_put_cpu(event, cpu);
4574}
4575
4576static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4577{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004578 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004579 int err = 0;
4580
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004581 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004582
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004583 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004584 struct swevent_hlist *hlist;
4585
4586 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4587 if (!hlist) {
4588 err = -ENOMEM;
4589 goto exit;
4590 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004591 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004592 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004593 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004594exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004595 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004596
4597 return err;
4598}
4599
4600static int swevent_hlist_get(struct perf_event *event)
4601{
4602 int err;
4603 int cpu, failed_cpu;
4604
4605 if (event->cpu != -1)
4606 return swevent_hlist_get_cpu(event, event->cpu);
4607
4608 get_online_cpus();
4609 for_each_possible_cpu(cpu) {
4610 err = swevent_hlist_get_cpu(event, cpu);
4611 if (err) {
4612 failed_cpu = cpu;
4613 goto fail;
4614 }
4615 }
4616 put_online_cpus();
4617
4618 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004619fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004620 for_each_possible_cpu(cpu) {
4621 if (cpu == failed_cpu)
4622 break;
4623 swevent_hlist_put_cpu(event, cpu);
4624 }
4625
4626 put_online_cpus();
4627 return err;
4628}
4629
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004630atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004631
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004632static void sw_perf_event_destroy(struct perf_event *event)
4633{
4634 u64 event_id = event->attr.config;
4635
4636 WARN_ON(event->parent);
4637
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004638 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004639 swevent_hlist_put(event);
4640}
4641
4642static int perf_swevent_init(struct perf_event *event)
4643{
4644 int event_id = event->attr.config;
4645
4646 if (event->attr.type != PERF_TYPE_SOFTWARE)
4647 return -ENOENT;
4648
4649 switch (event_id) {
4650 case PERF_COUNT_SW_CPU_CLOCK:
4651 case PERF_COUNT_SW_TASK_CLOCK:
4652 return -ENOENT;
4653
4654 default:
4655 break;
4656 }
4657
4658 if (event_id > PERF_COUNT_SW_MAX)
4659 return -ENOENT;
4660
4661 if (!event->parent) {
4662 int err;
4663
4664 err = swevent_hlist_get(event);
4665 if (err)
4666 return err;
4667
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004668 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004669 event->destroy = sw_perf_event_destroy;
4670 }
4671
4672 return 0;
4673}
4674
4675static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004676 .task_ctx_nr = perf_sw_context,
4677
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004678 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004679 .add = perf_swevent_add,
4680 .del = perf_swevent_del,
4681 .start = perf_swevent_start,
4682 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004683 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004684};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004685
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004686#ifdef CONFIG_EVENT_TRACING
4687
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004688static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004689 struct perf_sample_data *data)
4690{
4691 void *record = data->raw->data;
4692
4693 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4694 return 1;
4695 return 0;
4696}
4697
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004698static int perf_tp_event_match(struct perf_event *event,
4699 struct perf_sample_data *data,
4700 struct pt_regs *regs)
4701{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004702 /*
4703 * All tracepoints are from kernel-space.
4704 */
4705 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004706 return 0;
4707
4708 if (!perf_tp_filter_match(event, data))
4709 return 0;
4710
4711 return 1;
4712}
4713
4714void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004715 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004716{
4717 struct perf_sample_data data;
4718 struct perf_event *event;
4719 struct hlist_node *node;
4720
4721 struct perf_raw_record raw = {
4722 .size = entry_size,
4723 .data = record,
4724 };
4725
4726 perf_sample_data_init(&data, addr);
4727 data.raw = &raw;
4728
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004729 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4730 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004731 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004732 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004733
4734 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004735}
4736EXPORT_SYMBOL_GPL(perf_tp_event);
4737
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004738static void tp_perf_event_destroy(struct perf_event *event)
4739{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004740 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004741}
4742
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004743static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004744{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004745 int err;
4746
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004747 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4748 return -ENOENT;
4749
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004750 /*
4751 * Raw tracepoint data is a severe data leak, only allow root to
4752 * have these.
4753 */
4754 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4755 perf_paranoid_tracepoint_raw() &&
4756 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004757 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004758
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004759 err = perf_trace_init(event);
4760 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004761 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004762
4763 event->destroy = tp_perf_event_destroy;
4764
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004765 return 0;
4766}
4767
4768static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004769 .task_ctx_nr = perf_sw_context,
4770
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004771 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004772 .add = perf_trace_add,
4773 .del = perf_trace_del,
4774 .start = perf_swevent_start,
4775 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004776 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004777};
4778
4779static inline void perf_tp_register(void)
4780{
4781 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004782}
Li Zefan6fb29152009-10-15 11:21:42 +08004783
4784static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4785{
4786 char *filter_str;
4787 int ret;
4788
4789 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4790 return -EINVAL;
4791
4792 filter_str = strndup_user(arg, PAGE_SIZE);
4793 if (IS_ERR(filter_str))
4794 return PTR_ERR(filter_str);
4795
4796 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4797
4798 kfree(filter_str);
4799 return ret;
4800}
4801
4802static void perf_event_free_filter(struct perf_event *event)
4803{
4804 ftrace_profile_free_filter(event);
4805}
4806
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004807#else
Li Zefan6fb29152009-10-15 11:21:42 +08004808
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004809static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004810{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004811}
Li Zefan6fb29152009-10-15 11:21:42 +08004812
4813static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4814{
4815 return -ENOENT;
4816}
4817
4818static void perf_event_free_filter(struct perf_event *event)
4819{
4820}
4821
Li Zefan07b139c2009-12-21 14:27:35 +08004822#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004823
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004824#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004825void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004826{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004827 struct perf_sample_data sample;
4828 struct pt_regs *regs = data;
4829
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004830 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004831
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004832 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4833 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004834}
4835#endif
4836
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004837/*
4838 * hrtimer based swevent callback
4839 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004840
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004841static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004842{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004843 enum hrtimer_restart ret = HRTIMER_RESTART;
4844 struct perf_sample_data data;
4845 struct pt_regs *regs;
4846 struct perf_event *event;
4847 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004849 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4850 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004852 perf_sample_data_init(&data, 0);
4853 data.period = event->hw.last_period;
4854 regs = get_irq_regs();
4855
4856 if (regs && !perf_exclude_event(event, regs)) {
4857 if (!(event->attr.exclude_idle && current->pid == 0))
4858 if (perf_event_overflow(event, 0, &data, regs))
4859 ret = HRTIMER_NORESTART;
4860 }
4861
4862 period = max_t(u64, 10000, event->hw.sample_period);
4863 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4864
4865 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866}
4867
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004868static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004869{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004870 struct hw_perf_event *hwc = &event->hw;
4871
4872 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4873 hwc->hrtimer.function = perf_swevent_hrtimer;
4874 if (hwc->sample_period) {
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004875 s64 period = local64_read(&hwc->period_left);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004876
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004877 if (period) {
4878 if (period < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004879 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004880
4881 local64_set(&hwc->period_left, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004882 } else {
4883 period = max_t(u64, 10000, hwc->sample_period);
4884 }
4885 __hrtimer_start_range_ns(&hwc->hrtimer,
4886 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02004887 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004888 }
4889}
4890
4891static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4892{
4893 struct hw_perf_event *hwc = &event->hw;
4894
4895 if (hwc->sample_period) {
4896 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004897 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004898
4899 hrtimer_cancel(&hwc->hrtimer);
4900 }
4901}
4902
4903/*
4904 * Software event: cpu wall time clock
4905 */
4906
4907static void cpu_clock_event_update(struct perf_event *event)
4908{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004909 s64 prev;
4910 u64 now;
4911
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004912 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004913 prev = local64_xchg(&event->hw.prev_count, now);
4914 local64_add(now - prev, &event->count);
4915}
4916
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004917static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004918{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004919 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004920 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004921}
4922
4923static void cpu_clock_event_stop(struct perf_event *event, int flags)
4924{
4925 perf_swevent_cancel_hrtimer(event);
4926 cpu_clock_event_update(event);
4927}
4928
4929static int cpu_clock_event_add(struct perf_event *event, int flags)
4930{
4931 if (flags & PERF_EF_START)
4932 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004933
4934 return 0;
4935}
4936
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004937static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004938{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004939 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004940}
4941
4942static void cpu_clock_event_read(struct perf_event *event)
4943{
4944 cpu_clock_event_update(event);
4945}
4946
4947static int cpu_clock_event_init(struct perf_event *event)
4948{
4949 if (event->attr.type != PERF_TYPE_SOFTWARE)
4950 return -ENOENT;
4951
4952 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4953 return -ENOENT;
4954
4955 return 0;
4956}
4957
4958static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004959 .task_ctx_nr = perf_sw_context,
4960
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004961 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004962 .add = cpu_clock_event_add,
4963 .del = cpu_clock_event_del,
4964 .start = cpu_clock_event_start,
4965 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004966 .read = cpu_clock_event_read,
4967};
4968
4969/*
4970 * Software event: task time clock
4971 */
4972
4973static void task_clock_event_update(struct perf_event *event, u64 now)
4974{
4975 u64 prev;
4976 s64 delta;
4977
4978 prev = local64_xchg(&event->hw.prev_count, now);
4979 delta = now - prev;
4980 local64_add(delta, &event->count);
4981}
4982
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004983static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004984{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004985 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004986 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004987}
4988
4989static void task_clock_event_stop(struct perf_event *event, int flags)
4990{
4991 perf_swevent_cancel_hrtimer(event);
4992 task_clock_event_update(event, event->ctx->time);
4993}
4994
4995static int task_clock_event_add(struct perf_event *event, int flags)
4996{
4997 if (flags & PERF_EF_START)
4998 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004999
5000 return 0;
5001}
5002
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005003static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005004{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005005 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005006}
5007
5008static void task_clock_event_read(struct perf_event *event)
5009{
5010 u64 time;
5011
5012 if (!in_nmi()) {
5013 update_context_time(event->ctx);
5014 time = event->ctx->time;
5015 } else {
5016 u64 now = perf_clock();
5017 u64 delta = now - event->ctx->timestamp;
5018 time = event->ctx->time + delta;
5019 }
5020
5021 task_clock_event_update(event, time);
5022}
5023
5024static int task_clock_event_init(struct perf_event *event)
5025{
5026 if (event->attr.type != PERF_TYPE_SOFTWARE)
5027 return -ENOENT;
5028
5029 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5030 return -ENOENT;
5031
5032 return 0;
5033}
5034
5035static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005036 .task_ctx_nr = perf_sw_context,
5037
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005038 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005039 .add = task_clock_event_add,
5040 .del = task_clock_event_del,
5041 .start = task_clock_event_start,
5042 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005043 .read = task_clock_event_read,
5044};
5045
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005046static void perf_pmu_nop_void(struct pmu *pmu)
5047{
5048}
5049
5050static int perf_pmu_nop_int(struct pmu *pmu)
5051{
5052 return 0;
5053}
5054
5055static void perf_pmu_start_txn(struct pmu *pmu)
5056{
5057 perf_pmu_disable(pmu);
5058}
5059
5060static int perf_pmu_commit_txn(struct pmu *pmu)
5061{
5062 perf_pmu_enable(pmu);
5063 return 0;
5064}
5065
5066static void perf_pmu_cancel_txn(struct pmu *pmu)
5067{
5068 perf_pmu_enable(pmu);
5069}
5070
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005071/*
5072 * Ensures all contexts with the same task_ctx_nr have the same
5073 * pmu_cpu_context too.
5074 */
5075static void *find_pmu_context(int ctxn)
5076{
5077 struct pmu *pmu;
5078
5079 if (ctxn < 0)
5080 return NULL;
5081
5082 list_for_each_entry(pmu, &pmus, entry) {
5083 if (pmu->task_ctx_nr == ctxn)
5084 return pmu->pmu_cpu_context;
5085 }
5086
5087 return NULL;
5088}
5089
5090static void free_pmu_context(void * __percpu cpu_context)
5091{
5092 struct pmu *pmu;
5093
5094 mutex_lock(&pmus_lock);
5095 /*
5096 * Like a real lame refcount.
5097 */
5098 list_for_each_entry(pmu, &pmus, entry) {
5099 if (pmu->pmu_cpu_context == cpu_context)
5100 goto out;
5101 }
5102
5103 free_percpu(cpu_context);
5104out:
5105 mutex_unlock(&pmus_lock);
5106}
5107
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005108int perf_pmu_register(struct pmu *pmu)
5109{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005110 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005111
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005112 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005113 ret = -ENOMEM;
5114 pmu->pmu_disable_count = alloc_percpu(int);
5115 if (!pmu->pmu_disable_count)
5116 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005117
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005118 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5119 if (pmu->pmu_cpu_context)
5120 goto got_cpu_context;
5121
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005122 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5123 if (!pmu->pmu_cpu_context)
5124 goto free_pdc;
5125
5126 for_each_possible_cpu(cpu) {
5127 struct perf_cpu_context *cpuctx;
5128
5129 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005130 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005131 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005132 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005133 cpuctx->jiffies_interval = 1;
5134 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005135 }
5136
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005137got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005138 if (!pmu->start_txn) {
5139 if (pmu->pmu_enable) {
5140 /*
5141 * If we have pmu_enable/pmu_disable calls, install
5142 * transaction stubs that use that to try and batch
5143 * hardware accesses.
5144 */
5145 pmu->start_txn = perf_pmu_start_txn;
5146 pmu->commit_txn = perf_pmu_commit_txn;
5147 pmu->cancel_txn = perf_pmu_cancel_txn;
5148 } else {
5149 pmu->start_txn = perf_pmu_nop_void;
5150 pmu->commit_txn = perf_pmu_nop_int;
5151 pmu->cancel_txn = perf_pmu_nop_void;
5152 }
5153 }
5154
5155 if (!pmu->pmu_enable) {
5156 pmu->pmu_enable = perf_pmu_nop_void;
5157 pmu->pmu_disable = perf_pmu_nop_void;
5158 }
5159
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005160 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005161 ret = 0;
5162unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005163 mutex_unlock(&pmus_lock);
5164
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005165 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005166
5167free_pdc:
5168 free_percpu(pmu->pmu_disable_count);
5169 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005170}
5171
5172void perf_pmu_unregister(struct pmu *pmu)
5173{
5174 mutex_lock(&pmus_lock);
5175 list_del_rcu(&pmu->entry);
5176 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177
5178 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005179 * We dereference the pmu list under both SRCU and regular RCU, so
5180 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005181 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005182 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005183 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005185 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005186 free_pmu_context(pmu->pmu_cpu_context);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005187}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005189struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005191 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005192 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005193
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005194 idx = srcu_read_lock(&pmus_srcu);
5195 list_for_each_entry_rcu(pmu, &pmus, entry) {
5196 int ret = pmu->event_init(event);
5197 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005198 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005199
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005200 if (ret != -ENOENT) {
5201 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005202 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005203 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005204 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005205 pmu = ERR_PTR(-ENOENT);
5206unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005207 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005208
5209 return pmu;
5210}
5211
5212/*
5213 * Allocate and initialize a event structure
5214 */
5215static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005216perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005217 struct task_struct *task,
5218 struct perf_event *group_leader,
5219 struct perf_event *parent_event,
5220 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005221{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005222 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005223 struct perf_event *event;
5224 struct hw_perf_event *hwc;
5225 long err;
5226
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005227 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228 if (!event)
5229 return ERR_PTR(-ENOMEM);
5230
5231 /*
5232 * Single events are their own group leaders, with an
5233 * empty sibling list:
5234 */
5235 if (!group_leader)
5236 group_leader = event;
5237
5238 mutex_init(&event->child_mutex);
5239 INIT_LIST_HEAD(&event->child_list);
5240
5241 INIT_LIST_HEAD(&event->group_entry);
5242 INIT_LIST_HEAD(&event->event_entry);
5243 INIT_LIST_HEAD(&event->sibling_list);
5244 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005245 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246
5247 mutex_init(&event->mmap_mutex);
5248
5249 event->cpu = cpu;
5250 event->attr = *attr;
5251 event->group_leader = group_leader;
5252 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253 event->oncpu = -1;
5254
5255 event->parent = parent_event;
5256
5257 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5258 event->id = atomic64_inc_return(&perf_event_id);
5259
5260 event->state = PERF_EVENT_STATE_INACTIVE;
5261
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005262 if (task) {
5263 event->attach_state = PERF_ATTACH_TASK;
5264#ifdef CONFIG_HAVE_HW_BREAKPOINT
5265 /*
5266 * hw_breakpoint is a bit difficult here..
5267 */
5268 if (attr->type == PERF_TYPE_BREAKPOINT)
5269 event->hw.bp_target = task;
5270#endif
5271 }
5272
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005273 if (!overflow_handler && parent_event)
5274 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005275
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005276 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 if (attr->disabled)
5279 event->state = PERF_EVENT_STATE_OFF;
5280
5281 pmu = NULL;
5282
5283 hwc = &event->hw;
5284 hwc->sample_period = attr->sample_period;
5285 if (attr->freq && attr->sample_freq)
5286 hwc->sample_period = 1;
5287 hwc->last_period = hwc->sample_period;
5288
Peter Zijlstrae7850592010-05-21 14:43:08 +02005289 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005290
5291 /*
5292 * we currently do not support PERF_FORMAT_GROUP on inherited events
5293 */
5294 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5295 goto done;
5296
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005297 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299done:
5300 err = 0;
5301 if (!pmu)
5302 err = -EINVAL;
5303 else if (IS_ERR(pmu))
5304 err = PTR_ERR(pmu);
5305
5306 if (err) {
5307 if (event->ns)
5308 put_pid_ns(event->ns);
5309 kfree(event);
5310 return ERR_PTR(err);
5311 }
5312
5313 event->pmu = pmu;
5314
5315 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005316 if (event->attach_state & PERF_ATTACH_TASK)
5317 jump_label_inc(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005318 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005319 atomic_inc(&nr_mmap_events);
5320 if (event->attr.comm)
5321 atomic_inc(&nr_comm_events);
5322 if (event->attr.task)
5323 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005324 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5325 err = get_callchain_buffers();
5326 if (err) {
5327 free_event(event);
5328 return ERR_PTR(err);
5329 }
5330 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005331 }
5332
5333 return event;
5334}
5335
5336static int perf_copy_attr(struct perf_event_attr __user *uattr,
5337 struct perf_event_attr *attr)
5338{
5339 u32 size;
5340 int ret;
5341
5342 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5343 return -EFAULT;
5344
5345 /*
5346 * zero the full structure, so that a short copy will be nice.
5347 */
5348 memset(attr, 0, sizeof(*attr));
5349
5350 ret = get_user(size, &uattr->size);
5351 if (ret)
5352 return ret;
5353
5354 if (size > PAGE_SIZE) /* silly large */
5355 goto err_size;
5356
5357 if (!size) /* abi compat */
5358 size = PERF_ATTR_SIZE_VER0;
5359
5360 if (size < PERF_ATTR_SIZE_VER0)
5361 goto err_size;
5362
5363 /*
5364 * If we're handed a bigger struct than we know of,
5365 * ensure all the unknown bits are 0 - i.e. new
5366 * user-space does not rely on any kernel feature
5367 * extensions we dont know about yet.
5368 */
5369 if (size > sizeof(*attr)) {
5370 unsigned char __user *addr;
5371 unsigned char __user *end;
5372 unsigned char val;
5373
5374 addr = (void __user *)uattr + sizeof(*attr);
5375 end = (void __user *)uattr + size;
5376
5377 for (; addr < end; addr++) {
5378 ret = get_user(val, addr);
5379 if (ret)
5380 return ret;
5381 if (val)
5382 goto err_size;
5383 }
5384 size = sizeof(*attr);
5385 }
5386
5387 ret = copy_from_user(attr, uattr, size);
5388 if (ret)
5389 return -EFAULT;
5390
5391 /*
5392 * If the type exists, the corresponding creation will verify
5393 * the attr->config.
5394 */
5395 if (attr->type >= PERF_TYPE_MAX)
5396 return -EINVAL;
5397
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305398 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005399 return -EINVAL;
5400
5401 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5402 return -EINVAL;
5403
5404 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5405 return -EINVAL;
5406
5407out:
5408 return ret;
5409
5410err_size:
5411 put_user(sizeof(*attr), &uattr->size);
5412 ret = -E2BIG;
5413 goto out;
5414}
5415
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005416static int
5417perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005418{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005419 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005420 int ret = -EINVAL;
5421
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005422 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423 goto set;
5424
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005425 /* don't allow circular references */
5426 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005427 goto out;
5428
Peter Zijlstra0f139302010-05-20 14:35:15 +02005429 /*
5430 * Don't allow cross-cpu buffers
5431 */
5432 if (output_event->cpu != event->cpu)
5433 goto out;
5434
5435 /*
5436 * If its not a per-cpu buffer, it must be the same task.
5437 */
5438 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5439 goto out;
5440
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005441set:
5442 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005443 /* Can't redirect output if we've got an active mmap() */
5444 if (atomic_read(&event->mmap_count))
5445 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005446
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005447 if (output_event) {
5448 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005449 buffer = perf_buffer_get(output_event);
5450 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005451 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452 }
5453
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005454 old_buffer = event->buffer;
5455 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005457unlock:
5458 mutex_unlock(&event->mmap_mutex);
5459
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005460 if (old_buffer)
5461 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005462out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005463 return ret;
5464}
5465
5466/**
5467 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5468 *
5469 * @attr_uptr: event_id type attributes for monitoring/sampling
5470 * @pid: target pid
5471 * @cpu: target cpu
5472 * @group_fd: group leader event fd
5473 */
5474SYSCALL_DEFINE5(perf_event_open,
5475 struct perf_event_attr __user *, attr_uptr,
5476 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5477{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005478 struct perf_event *group_leader = NULL, *output_event = NULL;
5479 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005480 struct perf_event_attr attr;
5481 struct perf_event_context *ctx;
5482 struct file *event_file = NULL;
5483 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07005484 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005485 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005486 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005487 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005488 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005489 int err;
5490
5491 /* for future expandability... */
5492 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5493 return -EINVAL;
5494
5495 err = perf_copy_attr(attr_uptr, &attr);
5496 if (err)
5497 return err;
5498
5499 if (!attr.exclude_kernel) {
5500 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5501 return -EACCES;
5502 }
5503
5504 if (attr.freq) {
5505 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5506 return -EINVAL;
5507 }
5508
Al Viroea635c62010-05-26 17:40:29 -04005509 event_fd = get_unused_fd_flags(O_RDWR);
5510 if (event_fd < 0)
5511 return event_fd;
5512
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005513 if (group_fd != -1) {
5514 group_leader = perf_fget_light(group_fd, &fput_needed);
5515 if (IS_ERR(group_leader)) {
5516 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005517 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005518 }
5519 group_file = group_leader->filp;
5520 if (flags & PERF_FLAG_FD_OUTPUT)
5521 output_event = group_leader;
5522 if (flags & PERF_FLAG_FD_NO_GROUP)
5523 group_leader = NULL;
5524 }
5525
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005526 if (pid != -1) {
5527 task = find_lively_task_by_vpid(pid);
5528 if (IS_ERR(task)) {
5529 err = PTR_ERR(task);
5530 goto err_group_fd;
5531 }
5532 }
5533
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005534 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005535 if (IS_ERR(event)) {
5536 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005537 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005538 }
5539
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005540 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005541 * Special case software events and allow them to be part of
5542 * any hardware group.
5543 */
5544 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005545
5546 if (group_leader &&
5547 (is_software_event(event) != is_software_event(group_leader))) {
5548 if (is_software_event(event)) {
5549 /*
5550 * If event and group_leader are not both a software
5551 * event, and event is, then group leader is not.
5552 *
5553 * Allow the addition of software events to !software
5554 * groups, this is safe because software events never
5555 * fail to schedule.
5556 */
5557 pmu = group_leader->pmu;
5558 } else if (is_software_event(group_leader) &&
5559 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5560 /*
5561 * In case the group is a pure software group, and we
5562 * try to add a hardware event, move the whole group to
5563 * the hardware context.
5564 */
5565 move_group = 1;
5566 }
5567 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005568
5569 /*
5570 * Get the target context (task or percpu):
5571 */
Matt Helsley38a81da2010-09-13 13:01:20 -07005572 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005573 if (IS_ERR(ctx)) {
5574 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005575 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005576 }
5577
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005578 /*
5579 * Look up the group leader (we will attach this event to it):
5580 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005581 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005582 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005583
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584 /*
5585 * Do not allow a recursive hierarchy (this new sibling
5586 * becoming part of another group-sibling):
5587 */
5588 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005589 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590 /*
5591 * Do not allow to attach to a group in a different
5592 * task or CPU context:
5593 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005594 if (move_group) {
5595 if (group_leader->ctx->type != ctx->type)
5596 goto err_context;
5597 } else {
5598 if (group_leader->ctx != ctx)
5599 goto err_context;
5600 }
5601
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005602 /*
5603 * Only a group leader can be exclusive or pinned
5604 */
5605 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005606 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005607 }
5608
5609 if (output_event) {
5610 err = perf_event_set_output(event, output_event);
5611 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005612 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005613 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005614
Al Viroea635c62010-05-26 17:40:29 -04005615 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5616 if (IS_ERR(event_file)) {
5617 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005618 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005619 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005620
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005621 if (move_group) {
5622 struct perf_event_context *gctx = group_leader->ctx;
5623
5624 mutex_lock(&gctx->mutex);
5625 perf_event_remove_from_context(group_leader);
5626 list_for_each_entry(sibling, &group_leader->sibling_list,
5627 group_entry) {
5628 perf_event_remove_from_context(sibling);
5629 put_ctx(gctx);
5630 }
5631 mutex_unlock(&gctx->mutex);
5632 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005633 }
5634
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005635 event->filp = event_file;
5636 WARN_ON_ONCE(ctx->parent_ctx);
5637 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005638
5639 if (move_group) {
5640 perf_install_in_context(ctx, group_leader, cpu);
5641 get_ctx(ctx);
5642 list_for_each_entry(sibling, &group_leader->sibling_list,
5643 group_entry) {
5644 perf_install_in_context(ctx, sibling, cpu);
5645 get_ctx(ctx);
5646 }
5647 }
5648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649 perf_install_in_context(ctx, event, cpu);
5650 ++ctx->generation;
5651 mutex_unlock(&ctx->mutex);
5652
5653 event->owner = current;
5654 get_task_struct(current);
5655 mutex_lock(&current->perf_event_mutex);
5656 list_add_tail(&event->owner_entry, &current->perf_event_list);
5657 mutex_unlock(&current->perf_event_mutex);
5658
Peter Zijlstra8a495422010-05-27 15:47:49 +02005659 /*
5660 * Drop the reference on the group_event after placing the
5661 * new event on the sibling_list. This ensures destruction
5662 * of the group leader will find the pointer to itself in
5663 * perf_group_detach().
5664 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005665 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005666 fd_install(event_fd, event_file);
5667 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005668
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005669err_context:
Al Viroea635c62010-05-26 17:40:29 -04005670 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005671err_alloc:
5672 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02005673err_task:
5674 if (task)
5675 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005676err_group_fd:
5677 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005678err_fd:
5679 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005680 return err;
5681}
5682
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005683/**
5684 * perf_event_create_kernel_counter
5685 *
5686 * @attr: attributes of the counter to create
5687 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07005688 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005689 */
5690struct perf_event *
5691perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07005692 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005693 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005694{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005695 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005696 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005697 int err;
5698
5699 /*
5700 * Get the target context (task or percpu):
5701 */
5702
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005703 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005704 if (IS_ERR(event)) {
5705 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005706 goto err;
5707 }
5708
Matt Helsley38a81da2010-09-13 13:01:20 -07005709 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005710 if (IS_ERR(ctx)) {
5711 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005712 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005713 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005714
5715 event->filp = NULL;
5716 WARN_ON_ONCE(ctx->parent_ctx);
5717 mutex_lock(&ctx->mutex);
5718 perf_install_in_context(ctx, event, cpu);
5719 ++ctx->generation;
5720 mutex_unlock(&ctx->mutex);
5721
5722 event->owner = current;
5723 get_task_struct(current);
5724 mutex_lock(&current->perf_event_mutex);
5725 list_add_tail(&event->owner_entry, &current->perf_event_list);
5726 mutex_unlock(&current->perf_event_mutex);
5727
5728 return event;
5729
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005730err_free:
5731 free_event(event);
5732err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005733 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005734}
5735EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5736
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005737static void sync_child_event(struct perf_event *child_event,
5738 struct task_struct *child)
5739{
5740 struct perf_event *parent_event = child_event->parent;
5741 u64 child_val;
5742
5743 if (child_event->attr.inherit_stat)
5744 perf_event_read_event(child_event, child);
5745
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005746 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005747
5748 /*
5749 * Add back the child's count to the parent's count:
5750 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005751 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005752 atomic64_add(child_event->total_time_enabled,
5753 &parent_event->child_total_time_enabled);
5754 atomic64_add(child_event->total_time_running,
5755 &parent_event->child_total_time_running);
5756
5757 /*
5758 * Remove this event from the parent's list
5759 */
5760 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5761 mutex_lock(&parent_event->child_mutex);
5762 list_del_init(&child_event->child_list);
5763 mutex_unlock(&parent_event->child_mutex);
5764
5765 /*
5766 * Release the parent event, if this was the last
5767 * reference to it.
5768 */
5769 fput(parent_event->filp);
5770}
5771
5772static void
5773__perf_event_exit_task(struct perf_event *child_event,
5774 struct perf_event_context *child_ctx,
5775 struct task_struct *child)
5776{
5777 struct perf_event *parent_event;
5778
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005779 perf_event_remove_from_context(child_event);
5780
5781 parent_event = child_event->parent;
5782 /*
5783 * It can happen that parent exits first, and has events
5784 * that are still around due to the child reference. These
5785 * events need to be zapped - but otherwise linger.
5786 */
5787 if (parent_event) {
5788 sync_child_event(child_event, child);
5789 free_event(child_event);
5790 }
5791}
5792
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005793static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005794{
5795 struct perf_event *child_event, *tmp;
5796 struct perf_event_context *child_ctx;
5797 unsigned long flags;
5798
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005799 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800 perf_event_task(child, NULL, 0);
5801 return;
5802 }
5803
5804 local_irq_save(flags);
5805 /*
5806 * We can't reschedule here because interrupts are disabled,
5807 * and either child is current or it is a task that can't be
5808 * scheduled, so we are now safe from rescheduling changing
5809 * our context.
5810 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005811 child_ctx = child->perf_event_ctxp[ctxn];
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005812 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005813
5814 /*
5815 * Take the context lock here so that if find_get_context is
5816 * reading child->perf_event_ctxp, we wait until it has
5817 * incremented the context's refcount before we do put_ctx below.
5818 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005819 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005820 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005821 /*
5822 * If this context is a clone; unclone it so it can't get
5823 * swapped to another process while we're removing all
5824 * the events from it.
5825 */
5826 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005827 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005828 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005829
5830 /*
5831 * Report the task dead after unscheduling the events so that we
5832 * won't get any samples after PERF_RECORD_EXIT. We can however still
5833 * get a few PERF_RECORD_READ events.
5834 */
5835 perf_event_task(child, child_ctx, 0);
5836
5837 /*
5838 * We can recurse on the same lock type through:
5839 *
5840 * __perf_event_exit_task()
5841 * sync_child_event()
5842 * fput(parent_event->filp)
5843 * perf_release()
5844 * mutex_lock(&ctx->mutex)
5845 *
5846 * But since its the parent context it won't be the same instance.
5847 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005848 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005849
5850again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005851 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5852 group_entry)
5853 __perf_event_exit_task(child_event, child_ctx, child);
5854
5855 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005856 group_entry)
5857 __perf_event_exit_task(child_event, child_ctx, child);
5858
5859 /*
5860 * If the last event was a group event, it will have appended all
5861 * its siblings to the list, but we obtained 'tmp' before that which
5862 * will still point to the list head terminating the iteration.
5863 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005864 if (!list_empty(&child_ctx->pinned_groups) ||
5865 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005866 goto again;
5867
5868 mutex_unlock(&child_ctx->mutex);
5869
5870 put_ctx(child_ctx);
5871}
5872
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005873/*
5874 * When a child task exits, feed back event values to parent events.
5875 */
5876void perf_event_exit_task(struct task_struct *child)
5877{
5878 int ctxn;
5879
5880 for_each_task_context_nr(ctxn)
5881 perf_event_exit_task_context(child, ctxn);
5882}
5883
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005884static void perf_free_event(struct perf_event *event,
5885 struct perf_event_context *ctx)
5886{
5887 struct perf_event *parent = event->parent;
5888
5889 if (WARN_ON_ONCE(!parent))
5890 return;
5891
5892 mutex_lock(&parent->child_mutex);
5893 list_del_init(&event->child_list);
5894 mutex_unlock(&parent->child_mutex);
5895
5896 fput(parent->filp);
5897
Peter Zijlstra8a495422010-05-27 15:47:49 +02005898 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005899 list_del_event(event, ctx);
5900 free_event(event);
5901}
5902
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005903/*
5904 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005905 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005906 */
5907void perf_event_free_task(struct task_struct *task)
5908{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005909 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005910 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005911 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005912
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005913 for_each_task_context_nr(ctxn) {
5914 ctx = task->perf_event_ctxp[ctxn];
5915 if (!ctx)
5916 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005917
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005918 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005919again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005920 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5921 group_entry)
5922 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005923
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005924 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5925 group_entry)
5926 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005928 if (!list_empty(&ctx->pinned_groups) ||
5929 !list_empty(&ctx->flexible_groups))
5930 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005932 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005933
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005934 put_ctx(ctx);
5935 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005936}
5937
Peter Zijlstra4e231c72010-09-09 21:01:59 +02005938void perf_event_delayed_put(struct task_struct *task)
5939{
5940 int ctxn;
5941
5942 for_each_task_context_nr(ctxn)
5943 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5944}
5945
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005946/*
5947 * inherit a event from parent task to child task:
5948 */
5949static struct perf_event *
5950inherit_event(struct perf_event *parent_event,
5951 struct task_struct *parent,
5952 struct perf_event_context *parent_ctx,
5953 struct task_struct *child,
5954 struct perf_event *group_leader,
5955 struct perf_event_context *child_ctx)
5956{
5957 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02005958 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005959
5960 /*
5961 * Instead of creating recursive hierarchies of events,
5962 * we link inherited events back to the original parent,
5963 * which has a filp for sure, which we use as the reference
5964 * count:
5965 */
5966 if (parent_event->parent)
5967 parent_event = parent_event->parent;
5968
5969 child_event = perf_event_alloc(&parent_event->attr,
5970 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005971 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005972 group_leader, parent_event,
5973 NULL);
5974 if (IS_ERR(child_event))
5975 return child_event;
5976 get_ctx(child_ctx);
5977
5978 /*
5979 * Make the child state follow the state of the parent event,
5980 * not its attr.disabled bit. We hold the parent's mutex,
5981 * so we won't race with perf_event_{en, dis}able_family.
5982 */
5983 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5984 child_event->state = PERF_EVENT_STATE_INACTIVE;
5985 else
5986 child_event->state = PERF_EVENT_STATE_OFF;
5987
5988 if (parent_event->attr.freq) {
5989 u64 sample_period = parent_event->hw.sample_period;
5990 struct hw_perf_event *hwc = &child_event->hw;
5991
5992 hwc->sample_period = sample_period;
5993 hwc->last_period = sample_period;
5994
5995 local64_set(&hwc->period_left, sample_period);
5996 }
5997
5998 child_event->ctx = child_ctx;
5999 child_event->overflow_handler = parent_event->overflow_handler;
6000
6001 /*
6002 * Link it up in the child's context:
6003 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006004 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006005 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006006 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006007
6008 /*
6009 * Get a reference to the parent filp - we will fput it
6010 * when the child event exits. This is safe to do because
6011 * we are in the parent and we know that the filp still
6012 * exists and has a nonzero count:
6013 */
6014 atomic_long_inc(&parent_event->filp->f_count);
6015
6016 /*
6017 * Link this into the parent event's child list
6018 */
6019 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6020 mutex_lock(&parent_event->child_mutex);
6021 list_add_tail(&child_event->child_list, &parent_event->child_list);
6022 mutex_unlock(&parent_event->child_mutex);
6023
6024 return child_event;
6025}
6026
6027static int inherit_group(struct perf_event *parent_event,
6028 struct task_struct *parent,
6029 struct perf_event_context *parent_ctx,
6030 struct task_struct *child,
6031 struct perf_event_context *child_ctx)
6032{
6033 struct perf_event *leader;
6034 struct perf_event *sub;
6035 struct perf_event *child_ctr;
6036
6037 leader = inherit_event(parent_event, parent, parent_ctx,
6038 child, NULL, child_ctx);
6039 if (IS_ERR(leader))
6040 return PTR_ERR(leader);
6041 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6042 child_ctr = inherit_event(sub, parent, parent_ctx,
6043 child, leader, child_ctx);
6044 if (IS_ERR(child_ctr))
6045 return PTR_ERR(child_ctr);
6046 }
6047 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006048}
6049
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006050static int
6051inherit_task_group(struct perf_event *event, struct task_struct *parent,
6052 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006053 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006054 int *inherited_all)
6055{
6056 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006057 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006058
6059 if (!event->attr.inherit) {
6060 *inherited_all = 0;
6061 return 0;
6062 }
6063
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006064 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006065 if (!child_ctx) {
6066 /*
6067 * This is executed from the parent task context, so
6068 * inherit events that have been marked for cloning.
6069 * First allocate and initialize a context for the
6070 * child.
6071 */
6072
Peter Zijlstraeb184472010-09-07 15:55:13 +02006073 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006074 if (!child_ctx)
6075 return -ENOMEM;
6076
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006077 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006078 }
6079
6080 ret = inherit_group(event, parent, parent_ctx,
6081 child, child_ctx);
6082
6083 if (ret)
6084 *inherited_all = 0;
6085
6086 return ret;
6087}
6088
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089/*
6090 * Initialize the perf_event context in task_struct
6091 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006092int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006093{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006094 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006095 struct perf_event_context *cloned_ctx;
6096 struct perf_event *event;
6097 struct task_struct *parent = current;
6098 int inherited_all = 1;
6099 int ret = 0;
6100
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006101 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006102
6103 mutex_init(&child->perf_event_mutex);
6104 INIT_LIST_HEAD(&child->perf_event_list);
6105
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006106 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006107 return 0;
6108
6109 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006110 * If the parent's context is a clone, pin it so it won't get
6111 * swapped under us.
6112 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006113 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006114
6115 /*
6116 * No need to check if parent_ctx != NULL here; since we saw
6117 * it non-NULL earlier, the only reason for it to become NULL
6118 * is if we exit, and since we're currently in the middle of
6119 * a fork we can't be exiting at the same time.
6120 */
6121
6122 /*
6123 * Lock the parent list. No need to lock the child - not PID
6124 * hashed yet and not running, so nobody can access it.
6125 */
6126 mutex_lock(&parent_ctx->mutex);
6127
6128 /*
6129 * We dont have to disable NMIs - we are only looking at
6130 * the list, not manipulating it:
6131 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006132 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006133 ret = inherit_task_group(event, parent, parent_ctx,
6134 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006135 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006136 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137 }
6138
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006139 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006140 ret = inherit_task_group(event, parent, parent_ctx,
6141 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006142 if (ret)
6143 break;
6144 }
6145
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006146 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006147
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006148 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006149 /*
6150 * Mark the child context as a clone of the parent
6151 * context, or of whatever the parent is a clone of.
6152 * Note that if the parent is a clone, it could get
6153 * uncloned at any point, but that doesn't matter
6154 * because the list of events and the generation
6155 * count can't have changed since we took the mutex.
6156 */
6157 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6158 if (cloned_ctx) {
6159 child_ctx->parent_ctx = cloned_ctx;
6160 child_ctx->parent_gen = parent_ctx->parent_gen;
6161 } else {
6162 child_ctx->parent_ctx = parent_ctx;
6163 child_ctx->parent_gen = parent_ctx->generation;
6164 }
6165 get_ctx(child_ctx->parent_ctx);
6166 }
6167
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006168 mutex_unlock(&parent_ctx->mutex);
6169
6170 perf_unpin_context(parent_ctx);
6171
6172 return ret;
6173}
6174
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006175/*
6176 * Initialize the perf_event context in task_struct
6177 */
6178int perf_event_init_task(struct task_struct *child)
6179{
6180 int ctxn, ret;
6181
6182 for_each_task_context_nr(ctxn) {
6183 ret = perf_event_init_context(child, ctxn);
6184 if (ret)
6185 return ret;
6186 }
6187
6188 return 0;
6189}
6190
Paul Mackerras220b1402010-03-10 20:45:52 +11006191static void __init perf_event_init_all_cpus(void)
6192{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006193 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006194 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006195
6196 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006197 swhash = &per_cpu(swevent_htable, cpu);
6198 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006199 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006200 }
6201}
6202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006203static void __cpuinit perf_event_init_cpu(int cpu)
6204{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006205 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006206
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006207 mutex_lock(&swhash->hlist_mutex);
6208 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006209 struct swevent_hlist *hlist;
6210
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006211 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6212 WARN_ON(!hlist);
6213 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006214 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006215 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006216}
6217
6218#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006219static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006220{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006221 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6222
6223 WARN_ON(!irqs_disabled());
6224
6225 list_del_init(&cpuctx->rotation_list);
6226}
6227
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006228static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006229{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006230 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006231 struct perf_event *event, *tmp;
6232
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006233 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006234
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006235 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6236 __perf_event_remove_from_context(event);
6237 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006238 __perf_event_remove_from_context(event);
6239}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006240
6241static void perf_event_exit_cpu_context(int cpu)
6242{
6243 struct perf_event_context *ctx;
6244 struct pmu *pmu;
6245 int idx;
6246
6247 idx = srcu_read_lock(&pmus_srcu);
6248 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006249 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006250
6251 mutex_lock(&ctx->mutex);
6252 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6253 mutex_unlock(&ctx->mutex);
6254 }
6255 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006256}
6257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006258static void perf_event_exit_cpu(int cpu)
6259{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006260 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006261
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006262 mutex_lock(&swhash->hlist_mutex);
6263 swevent_hlist_release(swhash);
6264 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006265
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006266 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006267}
6268#else
6269static inline void perf_event_exit_cpu(int cpu) { }
6270#endif
6271
6272static int __cpuinit
6273perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6274{
6275 unsigned int cpu = (long)hcpu;
6276
Peter Zijlstra5e116372010-06-11 13:35:08 +02006277 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006278
6279 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006280 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006281 perf_event_init_cpu(cpu);
6282 break;
6283
Peter Zijlstra5e116372010-06-11 13:35:08 +02006284 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006285 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006286 perf_event_exit_cpu(cpu);
6287 break;
6288
6289 default:
6290 break;
6291 }
6292
6293 return NOTIFY_OK;
6294}
6295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006296void __init perf_event_init(void)
6297{
Paul Mackerras220b1402010-03-10 20:45:52 +11006298 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006299 init_srcu_struct(&pmus_srcu);
6300 perf_pmu_register(&perf_swevent);
6301 perf_pmu_register(&perf_cpu_clock);
6302 perf_pmu_register(&perf_task_clock);
6303 perf_tp_register();
6304 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006305}