blob: 39afdb07d75810b9cb26da3c1c5e778adae43674 [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;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200694
695 if (group_event->state == PERF_EVENT_STATE_OFF)
696 return 0;
697
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200698 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +0800699
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200700 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200701 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200702 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200703 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200704
705 /*
706 * Schedule in siblings as one group (if any):
707 */
708 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200709 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200710 partial_group = event;
711 goto group_error;
712 }
713 }
714
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200715 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000716 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200717
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200718group_error:
719 /*
720 * Groups can be scheduled in as one unit only, so undo any
721 * partial group before returning:
722 */
723 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
724 if (event == partial_group)
725 break;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200726 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200727 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200728 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200729
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200730 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200731
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200732 return -EAGAIN;
733}
734
735/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200736 * Work out whether we can put this event group on the CPU now.
737 */
738static int group_can_go_on(struct perf_event *event,
739 struct perf_cpu_context *cpuctx,
740 int can_add_hw)
741{
742 /*
743 * Groups consisting entirely of software events can always go on.
744 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100745 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200746 return 1;
747 /*
748 * If an exclusive group is already on, no other hardware
749 * events can go on.
750 */
751 if (cpuctx->exclusive)
752 return 0;
753 /*
754 * If this group is exclusive and there are already
755 * events on the CPU, it can't go on.
756 */
757 if (event->attr.exclusive && cpuctx->active_oncpu)
758 return 0;
759 /*
760 * Otherwise, try to add it if all previous groups were able
761 * to go on.
762 */
763 return can_add_hw;
764}
765
766static void add_event_to_ctx(struct perf_event *event,
767 struct perf_event_context *ctx)
768{
769 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200770 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200771 event->tstamp_enabled = ctx->time;
772 event->tstamp_running = ctx->time;
773 event->tstamp_stopped = ctx->time;
774}
775
776/*
777 * Cross CPU call to install and enable a performance event
778 *
779 * Must be called with ctx->mutex held
780 */
781static void __perf_install_in_context(void *info)
782{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200783 struct perf_event *event = info;
784 struct perf_event_context *ctx = event->ctx;
785 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200786 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200787 int err;
788
789 /*
790 * If this is a task context, we need to check whether it is
791 * the current task context of this cpu. If not it has been
792 * scheduled out before the smp call arrived.
793 * Or possibly this is the right context but it isn't
794 * on this cpu because it had no events.
795 */
796 if (ctx->task && cpuctx->task_ctx != ctx) {
797 if (cpuctx->task_ctx || ctx->task != current)
798 return;
799 cpuctx->task_ctx = ctx;
800 }
801
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100802 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200803 ctx->is_active = 1;
804 update_context_time(ctx);
805
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200806 add_event_to_ctx(event, ctx);
807
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100808 if (event->cpu != -1 && event->cpu != smp_processor_id())
809 goto unlock;
810
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200811 /*
812 * Don't put the event on if it is disabled or if
813 * it is in a group and the group isn't on.
814 */
815 if (event->state != PERF_EVENT_STATE_INACTIVE ||
816 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
817 goto unlock;
818
819 /*
820 * An exclusive event can't go on if there are already active
821 * hardware events, and no hardware event can go on if there
822 * is already an exclusive event on.
823 */
824 if (!group_can_go_on(event, cpuctx, 1))
825 err = -EEXIST;
826 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100827 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200828
829 if (err) {
830 /*
831 * This event couldn't go on. If it is in a group
832 * then we have to pull the whole group off.
833 * If the event group is pinned then put it in error state.
834 */
835 if (leader != event)
836 group_sched_out(leader, cpuctx, ctx);
837 if (leader->attr.pinned) {
838 update_group_times(leader);
839 leader->state = PERF_EVENT_STATE_ERROR;
840 }
841 }
842
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200843unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100844 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200845}
846
847/*
848 * Attach a performance event to a context
849 *
850 * First we add the event to the list with the hardware enable bit
851 * in event->hw_config cleared.
852 *
853 * If the event is attached to a task which is on a CPU we use a smp
854 * call to enable it in the task context. The task might have been
855 * scheduled away, but we check this in the smp call again.
856 *
857 * Must be called with ctx->mutex held.
858 */
859static void
860perf_install_in_context(struct perf_event_context *ctx,
861 struct perf_event *event,
862 int cpu)
863{
864 struct task_struct *task = ctx->task;
865
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200866 event->ctx = ctx;
867
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200868 if (!task) {
869 /*
870 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200871 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200872 */
873 smp_call_function_single(cpu, __perf_install_in_context,
874 event, 1);
875 return;
876 }
877
878retry:
879 task_oncpu_function_call(task, __perf_install_in_context,
880 event);
881
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100882 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200883 /*
884 * we need to retry the smp call.
885 */
886 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100887 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200888 goto retry;
889 }
890
891 /*
892 * The lock prevents that this context is scheduled in so we
893 * can add the event safely, if it the call above did not
894 * succeed.
895 */
896 if (list_empty(&event->group_entry))
897 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100898 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200899}
900
901/*
902 * Put a event into inactive state and update time fields.
903 * Enabling the leader of a group effectively enables all
904 * the group members that aren't explicitly disabled, so we
905 * have to update their ->tstamp_enabled also.
906 * Note: this works for group members as well as group leaders
907 * since the non-leader members' sibling_lists will be empty.
908 */
909static void __perf_event_mark_enabled(struct perf_event *event,
910 struct perf_event_context *ctx)
911{
912 struct perf_event *sub;
913
914 event->state = PERF_EVENT_STATE_INACTIVE;
915 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200916 list_for_each_entry(sub, &event->sibling_list, group_entry) {
917 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200918 sub->tstamp_enabled =
919 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200920 }
921 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200922}
923
924/*
925 * Cross CPU call to enable a performance event
926 */
927static void __perf_event_enable(void *info)
928{
929 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200930 struct perf_event_context *ctx = event->ctx;
931 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200932 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200933 int err;
934
935 /*
936 * If this is a per-task event, need to check whether this
937 * event's task is the current task on this cpu.
938 */
939 if (ctx->task && cpuctx->task_ctx != ctx) {
940 if (cpuctx->task_ctx || ctx->task != current)
941 return;
942 cpuctx->task_ctx = ctx;
943 }
944
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100945 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200946 ctx->is_active = 1;
947 update_context_time(ctx);
948
949 if (event->state >= PERF_EVENT_STATE_INACTIVE)
950 goto unlock;
951 __perf_event_mark_enabled(event, ctx);
952
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100953 if (event->cpu != -1 && event->cpu != smp_processor_id())
954 goto unlock;
955
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200956 /*
957 * If the event is in a group and isn't the group leader,
958 * then don't put it on unless the group is on.
959 */
960 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
961 goto unlock;
962
963 if (!group_can_go_on(event, cpuctx, 1)) {
964 err = -EEXIST;
965 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200966 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100967 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200968 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100969 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200970 }
971
972 if (err) {
973 /*
974 * If this event can't go on and it's part of a
975 * group, then the whole group has to come off.
976 */
977 if (leader != event)
978 group_sched_out(leader, cpuctx, ctx);
979 if (leader->attr.pinned) {
980 update_group_times(leader);
981 leader->state = PERF_EVENT_STATE_ERROR;
982 }
983 }
984
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200985unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100986 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987}
988
989/*
990 * Enable a event.
991 *
992 * If event->ctx is a cloned context, callers must make sure that
993 * every task struct that event->ctx->task could possibly point to
994 * remains valid. This condition is satisfied when called through
995 * perf_event_for_each_child or perf_event_for_each as described
996 * for perf_event_disable.
997 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100998void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200999{
1000 struct perf_event_context *ctx = event->ctx;
1001 struct task_struct *task = ctx->task;
1002
1003 if (!task) {
1004 /*
1005 * Enable the event on the cpu that it's on
1006 */
1007 smp_call_function_single(event->cpu, __perf_event_enable,
1008 event, 1);
1009 return;
1010 }
1011
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001012 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1014 goto out;
1015
1016 /*
1017 * If the event is in error state, clear that first.
1018 * That way, if we see the event in error state below, we
1019 * know that it has gone back into error state, as distinct
1020 * from the task having been scheduled away before the
1021 * cross-call arrived.
1022 */
1023 if (event->state == PERF_EVENT_STATE_ERROR)
1024 event->state = PERF_EVENT_STATE_OFF;
1025
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001026retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001027 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028 task_oncpu_function_call(task, __perf_event_enable, event);
1029
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001030 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031
1032 /*
1033 * If the context is active and the event is still off,
1034 * we need to retry the cross-call.
1035 */
1036 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1037 goto retry;
1038
1039 /*
1040 * Since we have the lock this context can't be scheduled
1041 * in, so we can change the state safely.
1042 */
1043 if (event->state == PERF_EVENT_STATE_OFF)
1044 __perf_event_mark_enabled(event, ctx);
1045
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001046out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001047 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048}
1049
1050static int perf_event_refresh(struct perf_event *event, int refresh)
1051{
1052 /*
1053 * not supported on inherited events
1054 */
1055 if (event->attr.inherit)
1056 return -EINVAL;
1057
1058 atomic_add(refresh, &event->event_limit);
1059 perf_event_enable(event);
1060
1061 return 0;
1062}
1063
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001064enum event_type_t {
1065 EVENT_FLEXIBLE = 0x1,
1066 EVENT_PINNED = 0x2,
1067 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1068};
1069
1070static void ctx_sched_out(struct perf_event_context *ctx,
1071 struct perf_cpu_context *cpuctx,
1072 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001073{
1074 struct perf_event *event;
1075
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001076 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001077 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001078 ctx->is_active = 0;
1079 if (likely(!ctx->nr_events))
1080 goto out;
1081 update_context_time(ctx);
1082
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001083 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001084 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001085
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001086 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001087 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1088 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001089 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001090
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001091 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001092 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001093 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001094 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001095out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001096 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001097 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098}
1099
1100/*
1101 * Test whether two contexts are equivalent, i.e. whether they
1102 * have both been cloned from the same version of the same context
1103 * and they both have the same number of enabled events.
1104 * If the number of enabled events is the same, then the set
1105 * of enabled events should be the same, because these are both
1106 * inherited contexts, therefore we can't access individual events
1107 * in them directly with an fd; we can only enable/disable all
1108 * events via prctl, or enable/disable all events in a family
1109 * via ioctl, which will have the same effect on both contexts.
1110 */
1111static int context_equiv(struct perf_event_context *ctx1,
1112 struct perf_event_context *ctx2)
1113{
1114 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1115 && ctx1->parent_gen == ctx2->parent_gen
1116 && !ctx1->pin_count && !ctx2->pin_count;
1117}
1118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119static void __perf_event_sync_stat(struct perf_event *event,
1120 struct perf_event *next_event)
1121{
1122 u64 value;
1123
1124 if (!event->attr.inherit_stat)
1125 return;
1126
1127 /*
1128 * Update the event value, we cannot use perf_event_read()
1129 * because we're in the middle of a context switch and have IRQs
1130 * disabled, which upsets smp_call_function_single(), however
1131 * we know the event must be on the current CPU, therefore we
1132 * don't need to use it.
1133 */
1134 switch (event->state) {
1135 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001136 event->pmu->read(event);
1137 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001138
1139 case PERF_EVENT_STATE_INACTIVE:
1140 update_event_times(event);
1141 break;
1142
1143 default:
1144 break;
1145 }
1146
1147 /*
1148 * In order to keep per-task stats reliable we need to flip the event
1149 * values when we flip the contexts.
1150 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001151 value = local64_read(&next_event->count);
1152 value = local64_xchg(&event->count, value);
1153 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154
1155 swap(event->total_time_enabled, next_event->total_time_enabled);
1156 swap(event->total_time_running, next_event->total_time_running);
1157
1158 /*
1159 * Since we swizzled the values, update the user visible data too.
1160 */
1161 perf_event_update_userpage(event);
1162 perf_event_update_userpage(next_event);
1163}
1164
1165#define list_next_entry(pos, member) \
1166 list_entry(pos->member.next, typeof(*pos), member)
1167
1168static void perf_event_sync_stat(struct perf_event_context *ctx,
1169 struct perf_event_context *next_ctx)
1170{
1171 struct perf_event *event, *next_event;
1172
1173 if (!ctx->nr_stat)
1174 return;
1175
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001176 update_context_time(ctx);
1177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001178 event = list_first_entry(&ctx->event_list,
1179 struct perf_event, event_entry);
1180
1181 next_event = list_first_entry(&next_ctx->event_list,
1182 struct perf_event, event_entry);
1183
1184 while (&event->event_entry != &ctx->event_list &&
1185 &next_event->event_entry != &next_ctx->event_list) {
1186
1187 __perf_event_sync_stat(event, next_event);
1188
1189 event = list_next_entry(event, event_entry);
1190 next_event = list_next_entry(next_event, event_entry);
1191 }
1192}
1193
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001194void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1195 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001196{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001197 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001198 struct perf_event_context *next_ctx;
1199 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001200 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001201 int do_switch = 1;
1202
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001203 if (likely(!ctx))
1204 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001206 cpuctx = __get_cpu_context(ctx);
1207 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208 return;
1209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001210 rcu_read_lock();
1211 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001212 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001213 if (parent && next_ctx &&
1214 rcu_dereference(next_ctx->parent_ctx) == parent) {
1215 /*
1216 * Looks like the two contexts are clones, so we might be
1217 * able to optimize the context switch. We lock both
1218 * contexts and check that they are clones under the
1219 * lock (including re-checking that neither has been
1220 * uncloned in the meantime). It doesn't matter which
1221 * order we take the locks because no other cpu could
1222 * be trying to lock both of these tasks.
1223 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001224 raw_spin_lock(&ctx->lock);
1225 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001226 if (context_equiv(ctx, next_ctx)) {
1227 /*
1228 * XXX do we need a memory barrier of sorts
1229 * wrt to rcu_dereference() of perf_event_ctxp
1230 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001231 task->perf_event_ctxp[ctxn] = next_ctx;
1232 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233 ctx->task = next;
1234 next_ctx->task = task;
1235 do_switch = 0;
1236
1237 perf_event_sync_stat(ctx, next_ctx);
1238 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001239 raw_spin_unlock(&next_ctx->lock);
1240 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 }
1242 rcu_read_unlock();
1243
1244 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001245 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246 cpuctx->task_ctx = NULL;
1247 }
1248}
1249
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001250#define for_each_task_context_nr(ctxn) \
1251 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1252
1253/*
1254 * Called from scheduler to remove the events of the current task,
1255 * with interrupts disabled.
1256 *
1257 * We stop each event and update the event value in event->count.
1258 *
1259 * This does not protect us against NMI, but disable()
1260 * sets the disabled bit in the control field of event _before_
1261 * accessing the event control register. If a NMI hits, then it will
1262 * not restart the event.
1263 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001264void __perf_event_task_sched_out(struct task_struct *task,
1265 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001266{
1267 int ctxn;
1268
1269 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1270
1271 for_each_task_context_nr(ctxn)
1272 perf_event_context_sched_out(task, ctxn, next);
1273}
1274
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001275static void task_ctx_sched_out(struct perf_event_context *ctx,
1276 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001277{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001278 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001279
1280 if (!cpuctx->task_ctx)
1281 return;
1282
1283 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1284 return;
1285
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001286 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001287 cpuctx->task_ctx = NULL;
1288}
1289
1290/*
1291 * Called with IRQs disabled
1292 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001293static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1294 enum event_type_t event_type)
1295{
1296 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297}
1298
1299static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001300ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001301 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001302{
1303 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001304
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001305 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1306 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001307 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001308 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001309 continue;
1310
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001311 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001312 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001313
1314 /*
1315 * If this pinned group hasn't been scheduled,
1316 * put it in error state.
1317 */
1318 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1319 update_group_times(event);
1320 event->state = PERF_EVENT_STATE_ERROR;
1321 }
1322 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001323}
1324
1325static void
1326ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001327 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001328{
1329 struct perf_event *event;
1330 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001332 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1333 /* Ignore events in OFF or ERROR state */
1334 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001335 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336 /*
1337 * Listen to the 'cpu' scheduling filter constraint
1338 * of events:
1339 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001340 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341 continue;
1342
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001343 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001344 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001346 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001348}
1349
1350static void
1351ctx_sched_in(struct perf_event_context *ctx,
1352 struct perf_cpu_context *cpuctx,
1353 enum event_type_t event_type)
1354{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001355 raw_spin_lock(&ctx->lock);
1356 ctx->is_active = 1;
1357 if (likely(!ctx->nr_events))
1358 goto out;
1359
1360 ctx->timestamp = perf_clock();
1361
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001362 /*
1363 * First go through the list and put on any pinned groups
1364 * in order to give them the best chance of going on.
1365 */
1366 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001367 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001368
1369 /* Then walk through the lower prio flexible groups */
1370 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001371 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001372
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001373out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001374 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001375}
1376
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001377static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1378 enum event_type_t event_type)
1379{
1380 struct perf_event_context *ctx = &cpuctx->ctx;
1381
1382 ctx_sched_in(ctx, cpuctx, event_type);
1383}
1384
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001385static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001386 enum event_type_t event_type)
1387{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001388 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001389
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001390 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001391 if (cpuctx->task_ctx == ctx)
1392 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001393
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001394 ctx_sched_in(ctx, cpuctx, event_type);
1395 cpuctx->task_ctx = ctx;
1396}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001397
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001398void perf_event_context_sched_in(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001399{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001400 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001401
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001402 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001403 if (cpuctx->task_ctx == ctx)
1404 return;
1405
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001406 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001407 /*
1408 * We want to keep the following priority order:
1409 * cpu pinned (that don't need to move), task pinned,
1410 * cpu flexible, task flexible.
1411 */
1412 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1413
1414 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1415 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1416 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1417
1418 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001419
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001420 /*
1421 * Since these rotations are per-cpu, we need to ensure the
1422 * cpu-context we got scheduled on is actually rotating.
1423 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001424 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001425 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426}
1427
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001428/*
1429 * Called from scheduler to add the events of the current task
1430 * with interrupts disabled.
1431 *
1432 * We restore the event value and then enable it.
1433 *
1434 * This does not protect us against NMI, but enable()
1435 * sets the enabled bit in the control field of event _before_
1436 * accessing the event control register. If a NMI hits, then it will
1437 * keep the event running.
1438 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001439void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001440{
1441 struct perf_event_context *ctx;
1442 int ctxn;
1443
1444 for_each_task_context_nr(ctxn) {
1445 ctx = task->perf_event_ctxp[ctxn];
1446 if (likely(!ctx))
1447 continue;
1448
1449 perf_event_context_sched_in(ctx);
1450 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001451}
1452
1453#define MAX_INTERRUPTS (~0ULL)
1454
1455static void perf_log_throttle(struct perf_event *event, int enable);
1456
Peter Zijlstraabd50712010-01-26 18:50:16 +01001457static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1458{
1459 u64 frequency = event->attr.sample_freq;
1460 u64 sec = NSEC_PER_SEC;
1461 u64 divisor, dividend;
1462
1463 int count_fls, nsec_fls, frequency_fls, sec_fls;
1464
1465 count_fls = fls64(count);
1466 nsec_fls = fls64(nsec);
1467 frequency_fls = fls64(frequency);
1468 sec_fls = 30;
1469
1470 /*
1471 * We got @count in @nsec, with a target of sample_freq HZ
1472 * the target period becomes:
1473 *
1474 * @count * 10^9
1475 * period = -------------------
1476 * @nsec * sample_freq
1477 *
1478 */
1479
1480 /*
1481 * Reduce accuracy by one bit such that @a and @b converge
1482 * to a similar magnitude.
1483 */
1484#define REDUCE_FLS(a, b) \
1485do { \
1486 if (a##_fls > b##_fls) { \
1487 a >>= 1; \
1488 a##_fls--; \
1489 } else { \
1490 b >>= 1; \
1491 b##_fls--; \
1492 } \
1493} while (0)
1494
1495 /*
1496 * Reduce accuracy until either term fits in a u64, then proceed with
1497 * the other, so that finally we can do a u64/u64 division.
1498 */
1499 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1500 REDUCE_FLS(nsec, frequency);
1501 REDUCE_FLS(sec, count);
1502 }
1503
1504 if (count_fls + sec_fls > 64) {
1505 divisor = nsec * frequency;
1506
1507 while (count_fls + sec_fls > 64) {
1508 REDUCE_FLS(count, sec);
1509 divisor >>= 1;
1510 }
1511
1512 dividend = count * sec;
1513 } else {
1514 dividend = count * sec;
1515
1516 while (nsec_fls + frequency_fls > 64) {
1517 REDUCE_FLS(nsec, frequency);
1518 dividend >>= 1;
1519 }
1520
1521 divisor = nsec * frequency;
1522 }
1523
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001524 if (!divisor)
1525 return dividend;
1526
Peter Zijlstraabd50712010-01-26 18:50:16 +01001527 return div64_u64(dividend, divisor);
1528}
1529
1530static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001531{
1532 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001533 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001534 s64 delta;
1535
Peter Zijlstraabd50712010-01-26 18:50:16 +01001536 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537
1538 delta = (s64)(period - hwc->sample_period);
1539 delta = (delta + 7) / 8; /* low pass filter */
1540
1541 sample_period = hwc->sample_period + delta;
1542
1543 if (!sample_period)
1544 sample_period = 1;
1545
1546 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001547
Peter Zijlstrae7850592010-05-21 14:43:08 +02001548 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001549 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001550 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001551 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001552 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553}
1554
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001555static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001556{
1557 struct perf_event *event;
1558 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001559 u64 interrupts, now;
1560 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001562 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001563 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564 if (event->state != PERF_EVENT_STATE_ACTIVE)
1565 continue;
1566
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001567 if (event->cpu != -1 && event->cpu != smp_processor_id())
1568 continue;
1569
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001570 hwc = &event->hw;
1571
1572 interrupts = hwc->interrupts;
1573 hwc->interrupts = 0;
1574
1575 /*
1576 * unthrottle events on the tick
1577 */
1578 if (interrupts == MAX_INTERRUPTS) {
1579 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001580 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581 }
1582
1583 if (!event->attr.freq || !event->attr.sample_freq)
1584 continue;
1585
Peter Zijlstraabd50712010-01-26 18:50:16 +01001586 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001587 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001588 delta = now - hwc->freq_count_stamp;
1589 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001590
Peter Zijlstraabd50712010-01-26 18:50:16 +01001591 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001592 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001594 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595}
1596
1597/*
1598 * Round-robin a context's events:
1599 */
1600static void rotate_ctx(struct perf_event_context *ctx)
1601{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001602 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001603
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001604 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001605 list_rotate_left(&ctx->flexible_groups);
1606
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001607 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608}
1609
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001610/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001611 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1612 * because they're strictly cpu affine and rotate_start is called with IRQs
1613 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001614 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001615static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001616{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001617 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001618 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001619 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001621 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001622 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001623 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1624 rotate = 1;
1625 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001627 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001628 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001629 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001630 if (ctx->nr_events != ctx->nr_active)
1631 rotate = 1;
1632 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001633
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001634 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001635 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001636 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001637 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001639 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001640 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001641
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001642 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001643 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001644 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001645
1646 rotate_ctx(&cpuctx->ctx);
1647 if (ctx)
1648 rotate_ctx(ctx);
1649
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001650 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001651 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001652 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001653
1654done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001655 if (remove)
1656 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001657
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001658 perf_pmu_enable(cpuctx->ctx.pmu);
1659}
1660
1661void perf_event_task_tick(void)
1662{
1663 struct list_head *head = &__get_cpu_var(rotation_list);
1664 struct perf_cpu_context *cpuctx, *tmp;
1665
1666 WARN_ON(!irqs_disabled());
1667
1668 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1669 if (cpuctx->jiffies_interval == 1 ||
1670 !(jiffies % cpuctx->jiffies_interval))
1671 perf_rotate_context(cpuctx);
1672 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001673}
1674
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001675static int event_enable_on_exec(struct perf_event *event,
1676 struct perf_event_context *ctx)
1677{
1678 if (!event->attr.enable_on_exec)
1679 return 0;
1680
1681 event->attr.enable_on_exec = 0;
1682 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1683 return 0;
1684
1685 __perf_event_mark_enabled(event, ctx);
1686
1687 return 1;
1688}
1689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690/*
1691 * Enable all of a task's events that have been marked enable-on-exec.
1692 * This expects task == current.
1693 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001694static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001696 struct perf_event *event;
1697 unsigned long flags;
1698 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001699 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700
1701 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001702 if (!ctx || !ctx->nr_events)
1703 goto out;
1704
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001705 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001706
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001707 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001709 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1710 ret = event_enable_on_exec(event, ctx);
1711 if (ret)
1712 enabled = 1;
1713 }
1714
1715 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1716 ret = event_enable_on_exec(event, ctx);
1717 if (ret)
1718 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719 }
1720
1721 /*
1722 * Unclone this context if we enabled any event.
1723 */
1724 if (enabled)
1725 unclone_ctx(ctx);
1726
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001727 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001728
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001729 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001730out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731 local_irq_restore(flags);
1732}
1733
1734/*
1735 * Cross CPU call to read the hardware event
1736 */
1737static void __perf_event_read(void *info)
1738{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 struct perf_event *event = info;
1740 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001741 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742
1743 /*
1744 * If this is a task context, we need to check whether it is
1745 * the current task context of this cpu. If not it has been
1746 * scheduled out before the smp call arrived. In that case
1747 * event->count would have been updated to a recent sample
1748 * when the event was scheduled out.
1749 */
1750 if (ctx->task && cpuctx->task_ctx != ctx)
1751 return;
1752
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001753 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001754 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001755 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001756 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001757
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001758 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001759}
1760
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001761static inline u64 perf_event_count(struct perf_event *event)
1762{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001763 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001764}
1765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766static u64 perf_event_read(struct perf_event *event)
1767{
1768 /*
1769 * If event is enabled and currently active on a CPU, update the
1770 * value in the event structure:
1771 */
1772 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1773 smp_call_function_single(event->oncpu,
1774 __perf_event_read, event, 1);
1775 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001776 struct perf_event_context *ctx = event->ctx;
1777 unsigned long flags;
1778
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001779 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02001780 /*
1781 * may read while context is not active
1782 * (e.g., thread is blocked), in that case
1783 * we cannot update context time
1784 */
1785 if (ctx->is_active)
1786 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001788 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789 }
1790
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001791 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792}
1793
1794/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001795 * Callchain support
1796 */
1797
1798struct callchain_cpus_entries {
1799 struct rcu_head rcu_head;
1800 struct perf_callchain_entry *cpu_entries[0];
1801};
1802
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001803static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001804static atomic_t nr_callchain_events;
1805static DEFINE_MUTEX(callchain_mutex);
1806struct callchain_cpus_entries *callchain_cpus_entries;
1807
1808
1809__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1810 struct pt_regs *regs)
1811{
1812}
1813
1814__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1815 struct pt_regs *regs)
1816{
1817}
1818
1819static void release_callchain_buffers_rcu(struct rcu_head *head)
1820{
1821 struct callchain_cpus_entries *entries;
1822 int cpu;
1823
1824 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1825
1826 for_each_possible_cpu(cpu)
1827 kfree(entries->cpu_entries[cpu]);
1828
1829 kfree(entries);
1830}
1831
1832static void release_callchain_buffers(void)
1833{
1834 struct callchain_cpus_entries *entries;
1835
1836 entries = callchain_cpus_entries;
1837 rcu_assign_pointer(callchain_cpus_entries, NULL);
1838 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1839}
1840
1841static int alloc_callchain_buffers(void)
1842{
1843 int cpu;
1844 int size;
1845 struct callchain_cpus_entries *entries;
1846
1847 /*
1848 * We can't use the percpu allocation API for data that can be
1849 * accessed from NMI. Use a temporary manual per cpu allocation
1850 * until that gets sorted out.
1851 */
1852 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1853 num_possible_cpus();
1854
1855 entries = kzalloc(size, GFP_KERNEL);
1856 if (!entries)
1857 return -ENOMEM;
1858
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001859 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001860
1861 for_each_possible_cpu(cpu) {
1862 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1863 cpu_to_node(cpu));
1864 if (!entries->cpu_entries[cpu])
1865 goto fail;
1866 }
1867
1868 rcu_assign_pointer(callchain_cpus_entries, entries);
1869
1870 return 0;
1871
1872fail:
1873 for_each_possible_cpu(cpu)
1874 kfree(entries->cpu_entries[cpu]);
1875 kfree(entries);
1876
1877 return -ENOMEM;
1878}
1879
1880static int get_callchain_buffers(void)
1881{
1882 int err = 0;
1883 int count;
1884
1885 mutex_lock(&callchain_mutex);
1886
1887 count = atomic_inc_return(&nr_callchain_events);
1888 if (WARN_ON_ONCE(count < 1)) {
1889 err = -EINVAL;
1890 goto exit;
1891 }
1892
1893 if (count > 1) {
1894 /* If the allocation failed, give up */
1895 if (!callchain_cpus_entries)
1896 err = -ENOMEM;
1897 goto exit;
1898 }
1899
1900 err = alloc_callchain_buffers();
1901 if (err)
1902 release_callchain_buffers();
1903exit:
1904 mutex_unlock(&callchain_mutex);
1905
1906 return err;
1907}
1908
1909static void put_callchain_buffers(void)
1910{
1911 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1912 release_callchain_buffers();
1913 mutex_unlock(&callchain_mutex);
1914 }
1915}
1916
1917static int get_recursion_context(int *recursion)
1918{
1919 int rctx;
1920
1921 if (in_nmi())
1922 rctx = 3;
1923 else if (in_irq())
1924 rctx = 2;
1925 else if (in_softirq())
1926 rctx = 1;
1927 else
1928 rctx = 0;
1929
1930 if (recursion[rctx])
1931 return -1;
1932
1933 recursion[rctx]++;
1934 barrier();
1935
1936 return rctx;
1937}
1938
1939static inline void put_recursion_context(int *recursion, int rctx)
1940{
1941 barrier();
1942 recursion[rctx]--;
1943}
1944
1945static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1946{
1947 int cpu;
1948 struct callchain_cpus_entries *entries;
1949
1950 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1951 if (*rctx == -1)
1952 return NULL;
1953
1954 entries = rcu_dereference(callchain_cpus_entries);
1955 if (!entries)
1956 return NULL;
1957
1958 cpu = smp_processor_id();
1959
1960 return &entries->cpu_entries[cpu][*rctx];
1961}
1962
1963static void
1964put_callchain_entry(int rctx)
1965{
1966 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1967}
1968
1969static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1970{
1971 int rctx;
1972 struct perf_callchain_entry *entry;
1973
1974
1975 entry = get_callchain_entry(&rctx);
1976 if (rctx == -1)
1977 return NULL;
1978
1979 if (!entry)
1980 goto exit_put;
1981
1982 entry->nr = 0;
1983
1984 if (!user_mode(regs)) {
1985 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1986 perf_callchain_kernel(entry, regs);
1987 if (current->mm)
1988 regs = task_pt_regs(current);
1989 else
1990 regs = NULL;
1991 }
1992
1993 if (regs) {
1994 perf_callchain_store(entry, PERF_CONTEXT_USER);
1995 perf_callchain_user(entry, regs);
1996 }
1997
1998exit_put:
1999 put_callchain_entry(rctx);
2000
2001 return entry;
2002}
2003
2004/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002005 * Initialize the perf_event context in a task_struct:
2006 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002007static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002009 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002011 INIT_LIST_HEAD(&ctx->pinned_groups);
2012 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013 INIT_LIST_HEAD(&ctx->event_list);
2014 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002015}
2016
Peter Zijlstraeb184472010-09-07 15:55:13 +02002017static struct perf_event_context *
2018alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019{
2020 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002021
2022 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2023 if (!ctx)
2024 return NULL;
2025
2026 __perf_event_init_context(ctx);
2027 if (task) {
2028 ctx->task = task;
2029 get_task_struct(task);
2030 }
2031 ctx->pmu = pmu;
2032
2033 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002034}
2035
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002036static struct task_struct *
2037find_lively_task_by_vpid(pid_t vpid)
2038{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002039 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002040 int err;
2041
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002043 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002044 task = current;
2045 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002046 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047 if (task)
2048 get_task_struct(task);
2049 rcu_read_unlock();
2050
2051 if (!task)
2052 return ERR_PTR(-ESRCH);
2053
2054 /*
2055 * Can't attach events to a dying task.
2056 */
2057 err = -ESRCH;
2058 if (task->flags & PF_EXITING)
2059 goto errout;
2060
2061 /* Reuse ptrace permission checks for now. */
2062 err = -EACCES;
2063 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2064 goto errout;
2065
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002066 return task;
2067errout:
2068 put_task_struct(task);
2069 return ERR_PTR(err);
2070
2071}
2072
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002073static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002074find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075{
2076 struct perf_event_context *ctx;
2077 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002079 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080
Matt Helsley38a81da2010-09-13 13:01:20 -07002081 if (!task && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 /* Must be root to operate on a CPU event: */
2083 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2084 return ERR_PTR(-EACCES);
2085
2086 if (cpu < 0 || cpu >= nr_cpumask_bits)
2087 return ERR_PTR(-EINVAL);
2088
2089 /*
2090 * We could be clever and allow to attach a event to an
2091 * offline CPU and activate it when the CPU comes up, but
2092 * that's for later.
2093 */
2094 if (!cpu_online(cpu))
2095 return ERR_PTR(-ENODEV);
2096
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002097 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 ctx = &cpuctx->ctx;
2099 get_ctx(ctx);
2100
2101 return ctx;
2102 }
2103
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002104 err = -EINVAL;
2105 ctxn = pmu->task_ctx_nr;
2106 if (ctxn < 0)
2107 goto errout;
2108
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002109retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002110 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111 if (ctx) {
2112 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002113 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114 }
2115
2116 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002117 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118 err = -ENOMEM;
2119 if (!ctx)
2120 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002122 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002123
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002124 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125 /*
2126 * We raced with some other task; use
2127 * the context they set.
2128 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002129 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002130 kfree(ctx);
2131 goto retry;
2132 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 }
2134
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002135 return ctx;
2136
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002137errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138 return ERR_PTR(err);
2139}
2140
Li Zefan6fb29152009-10-15 11:21:42 +08002141static void perf_event_free_filter(struct perf_event *event);
2142
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143static void free_event_rcu(struct rcu_head *head)
2144{
2145 struct perf_event *event;
2146
2147 event = container_of(head, struct perf_event, rcu_head);
2148 if (event->ns)
2149 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002150 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151 kfree(event);
2152}
2153
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002154static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155
2156static void free_event(struct perf_event *event)
2157{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002158 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002159
2160 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002161 if (event->attach_state & PERF_ATTACH_TASK)
2162 jump_label_dec(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002163 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002164 atomic_dec(&nr_mmap_events);
2165 if (event->attr.comm)
2166 atomic_dec(&nr_comm_events);
2167 if (event->attr.task)
2168 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002169 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2170 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171 }
2172
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002173 if (event->buffer) {
2174 perf_buffer_put(event->buffer);
2175 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176 }
2177
2178 if (event->destroy)
2179 event->destroy(event);
2180
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002181 if (event->ctx)
2182 put_ctx(event->ctx);
2183
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184 call_rcu(&event->rcu_head, free_event_rcu);
2185}
2186
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002187int perf_event_release_kernel(struct perf_event *event)
2188{
2189 struct perf_event_context *ctx = event->ctx;
2190
Peter Zijlstra050735b2010-05-11 11:51:53 +02002191 /*
2192 * Remove from the PMU, can't get re-enabled since we got
2193 * here because the last ref went.
2194 */
2195 perf_event_disable(event);
2196
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002197 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002198 /*
2199 * There are two ways this annotation is useful:
2200 *
2201 * 1) there is a lock recursion from perf_event_exit_task
2202 * see the comment there.
2203 *
2204 * 2) there is a lock-inversion with mmap_sem through
2205 * perf_event_read_group(), which takes faults while
2206 * holding ctx->mutex, however this is called after
2207 * the last filedesc died, so there is no possibility
2208 * to trigger the AB-BA case.
2209 */
2210 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002211 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002212 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002213 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002214 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002215 mutex_unlock(&ctx->mutex);
2216
2217 mutex_lock(&event->owner->perf_event_mutex);
2218 list_del_init(&event->owner_entry);
2219 mutex_unlock(&event->owner->perf_event_mutex);
2220 put_task_struct(event->owner);
2221
2222 free_event(event);
2223
2224 return 0;
2225}
2226EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2227
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002228/*
2229 * Called when the last reference to the file is gone.
2230 */
2231static int perf_release(struct inode *inode, struct file *file)
2232{
2233 struct perf_event *event = file->private_data;
2234
2235 file->private_data = NULL;
2236
2237 return perf_event_release_kernel(event);
2238}
2239
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002240static int perf_event_read_size(struct perf_event *event)
2241{
2242 int entry = sizeof(u64); /* value */
2243 int size = 0;
2244 int nr = 1;
2245
2246 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2247 size += sizeof(u64);
2248
2249 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2250 size += sizeof(u64);
2251
2252 if (event->attr.read_format & PERF_FORMAT_ID)
2253 entry += sizeof(u64);
2254
2255 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2256 nr += event->group_leader->nr_siblings;
2257 size += sizeof(u64);
2258 }
2259
2260 size += entry * nr;
2261
2262 return size;
2263}
2264
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002265u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266{
2267 struct perf_event *child;
2268 u64 total = 0;
2269
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002270 *enabled = 0;
2271 *running = 0;
2272
Peter Zijlstra6f105812009-11-20 22:19:56 +01002273 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002274 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002275 *enabled += event->total_time_enabled +
2276 atomic64_read(&event->child_total_time_enabled);
2277 *running += event->total_time_running +
2278 atomic64_read(&event->child_total_time_running);
2279
2280 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002282 *enabled += child->total_time_enabled;
2283 *running += child->total_time_running;
2284 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002285 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002286
2287 return total;
2288}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002289EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291static int perf_event_read_group(struct perf_event *event,
2292 u64 read_format, char __user *buf)
2293{
2294 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002295 int n = 0, size = 0, ret = -EFAULT;
2296 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002297 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002298 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002299
Peter Zijlstra6f105812009-11-20 22:19:56 +01002300 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002301 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002302
2303 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002304 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2305 values[n++] = enabled;
2306 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2307 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002308 values[n++] = count;
2309 if (read_format & PERF_FORMAT_ID)
2310 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311
2312 size = n * sizeof(u64);
2313
2314 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002315 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316
Peter Zijlstra6f105812009-11-20 22:19:56 +01002317 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002318
2319 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002320 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002322 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002323 if (read_format & PERF_FORMAT_ID)
2324 values[n++] = primary_event_id(sub);
2325
2326 size = n * sizeof(u64);
2327
Stephane Eranian184d3da2009-11-23 21:40:49 -08002328 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002329 ret = -EFAULT;
2330 goto unlock;
2331 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002332
2333 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002335unlock:
2336 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337
Peter Zijlstraabf48682009-11-20 22:19:49 +01002338 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002339}
2340
2341static int perf_event_read_one(struct perf_event *event,
2342 u64 read_format, char __user *buf)
2343{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002344 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345 u64 values[4];
2346 int n = 0;
2347
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002348 values[n++] = perf_event_read_value(event, &enabled, &running);
2349 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2350 values[n++] = enabled;
2351 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2352 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002353 if (read_format & PERF_FORMAT_ID)
2354 values[n++] = primary_event_id(event);
2355
2356 if (copy_to_user(buf, values, n * sizeof(u64)))
2357 return -EFAULT;
2358
2359 return n * sizeof(u64);
2360}
2361
2362/*
2363 * Read the performance event - simple non blocking version for now
2364 */
2365static ssize_t
2366perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2367{
2368 u64 read_format = event->attr.read_format;
2369 int ret;
2370
2371 /*
2372 * Return end-of-file for a read on a event that is in
2373 * error state (i.e. because it was pinned but it couldn't be
2374 * scheduled on to the CPU at some point).
2375 */
2376 if (event->state == PERF_EVENT_STATE_ERROR)
2377 return 0;
2378
2379 if (count < perf_event_read_size(event))
2380 return -ENOSPC;
2381
2382 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383 if (read_format & PERF_FORMAT_GROUP)
2384 ret = perf_event_read_group(event, read_format, buf);
2385 else
2386 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002387
2388 return ret;
2389}
2390
2391static ssize_t
2392perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2393{
2394 struct perf_event *event = file->private_data;
2395
2396 return perf_read_hw(event, buf, count);
2397}
2398
2399static unsigned int perf_poll(struct file *file, poll_table *wait)
2400{
2401 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002402 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002403 unsigned int events = POLL_HUP;
2404
2405 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002406 buffer = rcu_dereference(event->buffer);
2407 if (buffer)
2408 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002409 rcu_read_unlock();
2410
2411 poll_wait(file, &event->waitq, wait);
2412
2413 return events;
2414}
2415
2416static void perf_event_reset(struct perf_event *event)
2417{
2418 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002419 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420 perf_event_update_userpage(event);
2421}
2422
2423/*
2424 * Holding the top-level event's child_mutex means that any
2425 * descendant process that has inherited this event will block
2426 * in sync_child_event if it goes to exit, thus satisfying the
2427 * task existence requirements of perf_event_enable/disable.
2428 */
2429static void perf_event_for_each_child(struct perf_event *event,
2430 void (*func)(struct perf_event *))
2431{
2432 struct perf_event *child;
2433
2434 WARN_ON_ONCE(event->ctx->parent_ctx);
2435 mutex_lock(&event->child_mutex);
2436 func(event);
2437 list_for_each_entry(child, &event->child_list, child_list)
2438 func(child);
2439 mutex_unlock(&event->child_mutex);
2440}
2441
2442static void perf_event_for_each(struct perf_event *event,
2443 void (*func)(struct perf_event *))
2444{
2445 struct perf_event_context *ctx = event->ctx;
2446 struct perf_event *sibling;
2447
2448 WARN_ON_ONCE(ctx->parent_ctx);
2449 mutex_lock(&ctx->mutex);
2450 event = event->group_leader;
2451
2452 perf_event_for_each_child(event, func);
2453 func(event);
2454 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2455 perf_event_for_each_child(event, func);
2456 mutex_unlock(&ctx->mutex);
2457}
2458
2459static int perf_event_period(struct perf_event *event, u64 __user *arg)
2460{
2461 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462 int ret = 0;
2463 u64 value;
2464
2465 if (!event->attr.sample_period)
2466 return -EINVAL;
2467
John Blackwoodad0cf342010-09-28 18:03:11 -04002468 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002469 return -EFAULT;
2470
2471 if (!value)
2472 return -EINVAL;
2473
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002474 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002475 if (event->attr.freq) {
2476 if (value > sysctl_perf_event_sample_rate) {
2477 ret = -EINVAL;
2478 goto unlock;
2479 }
2480
2481 event->attr.sample_freq = value;
2482 } else {
2483 event->attr.sample_period = value;
2484 event->hw.sample_period = value;
2485 }
2486unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002487 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002488
2489 return ret;
2490}
2491
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002492static const struct file_operations perf_fops;
2493
2494static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2495{
2496 struct file *file;
2497
2498 file = fget_light(fd, fput_needed);
2499 if (!file)
2500 return ERR_PTR(-EBADF);
2501
2502 if (file->f_op != &perf_fops) {
2503 fput_light(file, *fput_needed);
2504 *fput_needed = 0;
2505 return ERR_PTR(-EBADF);
2506 }
2507
2508 return file->private_data;
2509}
2510
2511static int perf_event_set_output(struct perf_event *event,
2512 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002513static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514
2515static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2516{
2517 struct perf_event *event = file->private_data;
2518 void (*func)(struct perf_event *);
2519 u32 flags = arg;
2520
2521 switch (cmd) {
2522 case PERF_EVENT_IOC_ENABLE:
2523 func = perf_event_enable;
2524 break;
2525 case PERF_EVENT_IOC_DISABLE:
2526 func = perf_event_disable;
2527 break;
2528 case PERF_EVENT_IOC_RESET:
2529 func = perf_event_reset;
2530 break;
2531
2532 case PERF_EVENT_IOC_REFRESH:
2533 return perf_event_refresh(event, arg);
2534
2535 case PERF_EVENT_IOC_PERIOD:
2536 return perf_event_period(event, (u64 __user *)arg);
2537
2538 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002539 {
2540 struct perf_event *output_event = NULL;
2541 int fput_needed = 0;
2542 int ret;
2543
2544 if (arg != -1) {
2545 output_event = perf_fget_light(arg, &fput_needed);
2546 if (IS_ERR(output_event))
2547 return PTR_ERR(output_event);
2548 }
2549
2550 ret = perf_event_set_output(event, output_event);
2551 if (output_event)
2552 fput_light(output_event->filp, fput_needed);
2553
2554 return ret;
2555 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002556
Li Zefan6fb29152009-10-15 11:21:42 +08002557 case PERF_EVENT_IOC_SET_FILTER:
2558 return perf_event_set_filter(event, (void __user *)arg);
2559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002560 default:
2561 return -ENOTTY;
2562 }
2563
2564 if (flags & PERF_IOC_FLAG_GROUP)
2565 perf_event_for_each(event, func);
2566 else
2567 perf_event_for_each_child(event, func);
2568
2569 return 0;
2570}
2571
2572int perf_event_task_enable(void)
2573{
2574 struct perf_event *event;
2575
2576 mutex_lock(&current->perf_event_mutex);
2577 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2578 perf_event_for_each_child(event, perf_event_enable);
2579 mutex_unlock(&current->perf_event_mutex);
2580
2581 return 0;
2582}
2583
2584int perf_event_task_disable(void)
2585{
2586 struct perf_event *event;
2587
2588 mutex_lock(&current->perf_event_mutex);
2589 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2590 perf_event_for_each_child(event, perf_event_disable);
2591 mutex_unlock(&current->perf_event_mutex);
2592
2593 return 0;
2594}
2595
2596#ifndef PERF_EVENT_INDEX_OFFSET
2597# define PERF_EVENT_INDEX_OFFSET 0
2598#endif
2599
2600static int perf_event_index(struct perf_event *event)
2601{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002602 if (event->hw.state & PERF_HES_STOPPED)
2603 return 0;
2604
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002605 if (event->state != PERF_EVENT_STATE_ACTIVE)
2606 return 0;
2607
2608 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2609}
2610
2611/*
2612 * Callers need to ensure there can be no nesting of this function, otherwise
2613 * the seqlock logic goes bad. We can not serialize this because the arch
2614 * code calls this from NMI context.
2615 */
2616void perf_event_update_userpage(struct perf_event *event)
2617{
2618 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002619 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002620
2621 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002622 buffer = rcu_dereference(event->buffer);
2623 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002624 goto unlock;
2625
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002626 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002627
2628 /*
2629 * Disable preemption so as to not let the corresponding user-space
2630 * spin too long if we get preempted.
2631 */
2632 preempt_disable();
2633 ++userpg->lock;
2634 barrier();
2635 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002636 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002637 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002638 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002639
2640 userpg->time_enabled = event->total_time_enabled +
2641 atomic64_read(&event->child_total_time_enabled);
2642
2643 userpg->time_running = event->total_time_running +
2644 atomic64_read(&event->child_total_time_running);
2645
2646 barrier();
2647 ++userpg->lock;
2648 preempt_enable();
2649unlock:
2650 rcu_read_unlock();
2651}
2652
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002653static unsigned long perf_data_size(struct perf_buffer *buffer);
2654
2655static void
2656perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2657{
2658 long max_size = perf_data_size(buffer);
2659
2660 if (watermark)
2661 buffer->watermark = min(max_size, watermark);
2662
2663 if (!buffer->watermark)
2664 buffer->watermark = max_size / 2;
2665
2666 if (flags & PERF_BUFFER_WRITABLE)
2667 buffer->writable = 1;
2668
2669 atomic_set(&buffer->refcount, 1);
2670}
2671
Peter Zijlstra906010b2009-09-21 16:08:49 +02002672#ifndef CONFIG_PERF_USE_VMALLOC
2673
2674/*
2675 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2676 */
2677
2678static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002679perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002680{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002681 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002682 return NULL;
2683
2684 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002685 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002686
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002687 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002688}
2689
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002690static void *perf_mmap_alloc_page(int cpu)
2691{
2692 struct page *page;
2693 int node;
2694
2695 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2696 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2697 if (!page)
2698 return NULL;
2699
2700 return page_address(page);
2701}
2702
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002703static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002704perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002705{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002706 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002707 unsigned long size;
2708 int i;
2709
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002710 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002711 size += nr_pages * sizeof(void *);
2712
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002713 buffer = kzalloc(size, GFP_KERNEL);
2714 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002715 goto fail;
2716
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002717 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002718 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719 goto fail_user_page;
2720
2721 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002722 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002723 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724 goto fail_data_pages;
2725 }
2726
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002727 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002729 perf_buffer_init(buffer, watermark, flags);
2730
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002731 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002732
2733fail_data_pages:
2734 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002735 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002736
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002737 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002738
2739fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002740 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002741
2742fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002743 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002744}
2745
2746static void perf_mmap_free_page(unsigned long addr)
2747{
2748 struct page *page = virt_to_page((void *)addr);
2749
2750 page->mapping = NULL;
2751 __free_page(page);
2752}
2753
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002754static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002756 int i;
2757
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002758 perf_mmap_free_page((unsigned long)buffer->user_page);
2759 for (i = 0; i < buffer->nr_pages; i++)
2760 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2761 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002762}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002764static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002765{
2766 return 0;
2767}
2768
Peter Zijlstra906010b2009-09-21 16:08:49 +02002769#else
2770
2771/*
2772 * Back perf_mmap() with vmalloc memory.
2773 *
2774 * Required for architectures that have d-cache aliasing issues.
2775 */
2776
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002777static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002778{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002779 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002780}
2781
Peter Zijlstra906010b2009-09-21 16:08:49 +02002782static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002783perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002784{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002785 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002786 return NULL;
2787
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002788 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002789}
2790
2791static void perf_mmap_unmark_page(void *addr)
2792{
2793 struct page *page = vmalloc_to_page(addr);
2794
2795 page->mapping = NULL;
2796}
2797
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002798static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002799{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002800 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002801 void *base;
2802 int i, nr;
2803
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002804 buffer = container_of(work, struct perf_buffer, work);
2805 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002806
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002807 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002808 for (i = 0; i < nr + 1; i++)
2809 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2810
2811 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002812 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002813}
2814
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002815static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002816{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002817 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002818}
2819
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002820static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002821perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002822{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002823 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002824 unsigned long size;
2825 void *all_buf;
2826
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002827 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002828 size += sizeof(void *);
2829
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002830 buffer = kzalloc(size, GFP_KERNEL);
2831 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002832 goto fail;
2833
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002834 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002835
2836 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2837 if (!all_buf)
2838 goto fail_all_buf;
2839
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002840 buffer->user_page = all_buf;
2841 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2842 buffer->page_order = ilog2(nr_pages);
2843 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002844
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002845 perf_buffer_init(buffer, watermark, flags);
2846
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002847 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002848
2849fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002850 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002851
2852fail:
2853 return NULL;
2854}
2855
2856#endif
2857
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002858static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002859{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002860 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002861}
2862
Peter Zijlstra906010b2009-09-21 16:08:49 +02002863static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2864{
2865 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002866 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002867 int ret = VM_FAULT_SIGBUS;
2868
2869 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2870 if (vmf->pgoff == 0)
2871 ret = 0;
2872 return ret;
2873 }
2874
2875 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002876 buffer = rcu_dereference(event->buffer);
2877 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002878 goto unlock;
2879
2880 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2881 goto unlock;
2882
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002883 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002884 if (!vmf->page)
2885 goto unlock;
2886
2887 get_page(vmf->page);
2888 vmf->page->mapping = vma->vm_file->f_mapping;
2889 vmf->page->index = vmf->pgoff;
2890
2891 ret = 0;
2892unlock:
2893 rcu_read_unlock();
2894
2895 return ret;
2896}
2897
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002898static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002899{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002900 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002901
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002902 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2903 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904}
2905
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002906static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002908 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002910 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002911 buffer = rcu_dereference(event->buffer);
2912 if (buffer) {
2913 if (!atomic_inc_not_zero(&buffer->refcount))
2914 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002915 }
2916 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002917
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002918 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002919}
2920
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002921static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002922{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002923 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002924 return;
2925
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002926 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002927}
2928
2929static void perf_mmap_open(struct vm_area_struct *vma)
2930{
2931 struct perf_event *event = vma->vm_file->private_data;
2932
2933 atomic_inc(&event->mmap_count);
2934}
2935
2936static void perf_mmap_close(struct vm_area_struct *vma)
2937{
2938 struct perf_event *event = vma->vm_file->private_data;
2939
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002940 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002941 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002942 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002943 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002944
Peter Zijlstra906010b2009-09-21 16:08:49 +02002945 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002946 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002947 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002949
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002950 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002951 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952 }
2953}
2954
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002955static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002956 .open = perf_mmap_open,
2957 .close = perf_mmap_close,
2958 .fault = perf_mmap_fault,
2959 .page_mkwrite = perf_mmap_fault,
2960};
2961
2962static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2963{
2964 struct perf_event *event = file->private_data;
2965 unsigned long user_locked, user_lock_limit;
2966 struct user_struct *user = current_user();
2967 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002968 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969 unsigned long vma_size;
2970 unsigned long nr_pages;
2971 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002972 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002973
Peter Zijlstrac7920612010-05-18 10:33:24 +02002974 /*
2975 * Don't allow mmap() of inherited per-task counters. This would
2976 * create a performance issue due to all children writing to the
2977 * same buffer.
2978 */
2979 if (event->cpu == -1 && event->attr.inherit)
2980 return -EINVAL;
2981
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002982 if (!(vma->vm_flags & VM_SHARED))
2983 return -EINVAL;
2984
2985 vma_size = vma->vm_end - vma->vm_start;
2986 nr_pages = (vma_size / PAGE_SIZE) - 1;
2987
2988 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002989 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990 * can do bitmasks instead of modulo.
2991 */
2992 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2993 return -EINVAL;
2994
2995 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2996 return -EINVAL;
2997
2998 if (vma->vm_pgoff != 0)
2999 return -EINVAL;
3000
3001 WARN_ON_ONCE(event->ctx->parent_ctx);
3002 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003003 if (event->buffer) {
3004 if (event->buffer->nr_pages == nr_pages)
3005 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003006 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007 ret = -EINVAL;
3008 goto unlock;
3009 }
3010
3011 user_extra = nr_pages + 1;
3012 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3013
3014 /*
3015 * Increase the limit linearly with more CPUs:
3016 */
3017 user_lock_limit *= num_online_cpus();
3018
3019 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3020
3021 extra = 0;
3022 if (user_locked > user_lock_limit)
3023 extra = user_locked - user_lock_limit;
3024
Jiri Slaby78d7d402010-03-05 13:42:54 -08003025 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026 lock_limit >>= PAGE_SHIFT;
3027 locked = vma->vm_mm->locked_vm + extra;
3028
3029 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3030 !capable(CAP_IPC_LOCK)) {
3031 ret = -EPERM;
3032 goto unlock;
3033 }
3034
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003035 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003036
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003037 if (vma->vm_flags & VM_WRITE)
3038 flags |= PERF_BUFFER_WRITABLE;
3039
3040 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3041 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003042 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003043 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003045 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003046 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003048 atomic_long_add(user_extra, &user->locked_vm);
3049 event->mmap_locked = extra;
3050 event->mmap_user = get_current_user();
3051 vma->vm_mm->locked_vm += event->mmap_locked;
3052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003054 if (!ret)
3055 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003056 mutex_unlock(&event->mmap_mutex);
3057
3058 vma->vm_flags |= VM_RESERVED;
3059 vma->vm_ops = &perf_mmap_vmops;
3060
3061 return ret;
3062}
3063
3064static int perf_fasync(int fd, struct file *filp, int on)
3065{
3066 struct inode *inode = filp->f_path.dentry->d_inode;
3067 struct perf_event *event = filp->private_data;
3068 int retval;
3069
3070 mutex_lock(&inode->i_mutex);
3071 retval = fasync_helper(fd, filp, on, &event->fasync);
3072 mutex_unlock(&inode->i_mutex);
3073
3074 if (retval < 0)
3075 return retval;
3076
3077 return 0;
3078}
3079
3080static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003081 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003082 .release = perf_release,
3083 .read = perf_read,
3084 .poll = perf_poll,
3085 .unlocked_ioctl = perf_ioctl,
3086 .compat_ioctl = perf_ioctl,
3087 .mmap = perf_mmap,
3088 .fasync = perf_fasync,
3089};
3090
3091/*
3092 * Perf event wakeup
3093 *
3094 * If there's data, ensure we set the poll() state and publish everything
3095 * to user-space before waking everybody up.
3096 */
3097
3098void perf_event_wakeup(struct perf_event *event)
3099{
3100 wake_up_all(&event->waitq);
3101
3102 if (event->pending_kill) {
3103 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3104 event->pending_kill = 0;
3105 }
3106}
3107
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003108static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003109{
3110 struct perf_event *event = container_of(entry,
3111 struct perf_event, pending);
3112
3113 if (event->pending_disable) {
3114 event->pending_disable = 0;
3115 __perf_event_disable(event);
3116 }
3117
3118 if (event->pending_wakeup) {
3119 event->pending_wakeup = 0;
3120 perf_event_wakeup(event);
3121 }
3122}
3123
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003124/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003125 * We assume there is only KVM supporting the callbacks.
3126 * Later on, we might change it to a list if there is
3127 * another virtualization implementation supporting the callbacks.
3128 */
3129struct perf_guest_info_callbacks *perf_guest_cbs;
3130
3131int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3132{
3133 perf_guest_cbs = cbs;
3134 return 0;
3135}
3136EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3137
3138int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3139{
3140 perf_guest_cbs = NULL;
3141 return 0;
3142}
3143EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3144
3145/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003146 * Output
3147 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003148static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003149 unsigned long offset, unsigned long head)
3150{
3151 unsigned long mask;
3152
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003153 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003154 return true;
3155
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003156 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003157
3158 offset = (offset - tail) & mask;
3159 head = (head - tail) & mask;
3160
3161 if ((int)(head - offset) < 0)
3162 return false;
3163
3164 return true;
3165}
3166
3167static void perf_output_wakeup(struct perf_output_handle *handle)
3168{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003169 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003170
3171 if (handle->nmi) {
3172 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003173 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003174 } else
3175 perf_event_wakeup(handle->event);
3176}
3177
3178/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003179 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003180 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003181 * cannot fully serialize things.
3182 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003183 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003184 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003185 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003186static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003188 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189
Peter Zijlstraef607772010-05-18 10:50:41 +02003190 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003191 local_inc(&buffer->nest);
3192 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193}
3194
Peter Zijlstraef607772010-05-18 10:50:41 +02003195static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003196{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003197 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003198 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199
3200again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003201 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202
3203 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003204 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205 */
3206
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003207 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003208 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003209
3210 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003211 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003212 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003213 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003214 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003215 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216
Peter Zijlstraef607772010-05-18 10:50:41 +02003217 /*
3218 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003219 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003220 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003221 if (unlikely(head != local_read(&buffer->head))) {
3222 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003223 goto again;
3224 }
3225
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003226 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003228
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003229out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003230 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231}
3232
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003233__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234 const void *buf, unsigned int len)
3235{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003236 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003237 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003238
3239 memcpy(handle->addr, buf, size);
3240
3241 len -= size;
3242 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003243 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003244 handle->size -= size;
3245 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003246 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003247
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003248 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003249 handle->page &= buffer->nr_pages - 1;
3250 handle->addr = buffer->data_pages[handle->page];
3251 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003252 }
3253 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003254}
3255
3256int perf_output_begin(struct perf_output_handle *handle,
3257 struct perf_event *event, unsigned int size,
3258 int nmi, int sample)
3259{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003260 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261 unsigned long tail, offset, head;
3262 int have_lost;
3263 struct {
3264 struct perf_event_header header;
3265 u64 id;
3266 u64 lost;
3267 } lost_event;
3268
3269 rcu_read_lock();
3270 /*
3271 * For inherited events we send all the output towards the parent.
3272 */
3273 if (event->parent)
3274 event = event->parent;
3275
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003276 buffer = rcu_dereference(event->buffer);
3277 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278 goto out;
3279
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003280 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 handle->event = event;
3282 handle->nmi = nmi;
3283 handle->sample = sample;
3284
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003285 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003286 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003287
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003288 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289 if (have_lost)
3290 size += sizeof(lost_event);
3291
Peter Zijlstraef607772010-05-18 10:50:41 +02003292 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293
3294 do {
3295 /*
3296 * Userspace could choose to issue a mb() before updating the
3297 * tail pointer. So that all reads will be completed before the
3298 * write is issued.
3299 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003300 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003302 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003303 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003304 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003306 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003307
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003308 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3309 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003311 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3312 handle->page &= buffer->nr_pages - 1;
3313 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3314 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003315 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003316 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318 if (have_lost) {
3319 lost_event.header.type = PERF_RECORD_LOST;
3320 lost_event.header.misc = 0;
3321 lost_event.header.size = sizeof(lost_event);
3322 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003323 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003324
3325 perf_output_put(handle, lost_event);
3326 }
3327
3328 return 0;
3329
3330fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003331 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003332 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333out:
3334 rcu_read_unlock();
3335
3336 return -ENOSPC;
3337}
3338
3339void perf_output_end(struct perf_output_handle *handle)
3340{
3341 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003342 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343
3344 int wakeup_events = event->attr.wakeup_events;
3345
3346 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003347 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 local_sub(wakeup_events, &buffer->events);
3350 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351 }
3352 }
3353
Peter Zijlstraef607772010-05-18 10:50:41 +02003354 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003355 rcu_read_unlock();
3356}
3357
3358static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3359{
3360 /*
3361 * only top level events have the pid namespace they were created in
3362 */
3363 if (event->parent)
3364 event = event->parent;
3365
3366 return task_tgid_nr_ns(p, event->ns);
3367}
3368
3369static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3370{
3371 /*
3372 * only top level events have the pid namespace they were created in
3373 */
3374 if (event->parent)
3375 event = event->parent;
3376
3377 return task_pid_nr_ns(p, event->ns);
3378}
3379
3380static void perf_output_read_one(struct perf_output_handle *handle,
3381 struct perf_event *event)
3382{
3383 u64 read_format = event->attr.read_format;
3384 u64 values[4];
3385 int n = 0;
3386
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003387 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003388 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3389 values[n++] = event->total_time_enabled +
3390 atomic64_read(&event->child_total_time_enabled);
3391 }
3392 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3393 values[n++] = event->total_time_running +
3394 atomic64_read(&event->child_total_time_running);
3395 }
3396 if (read_format & PERF_FORMAT_ID)
3397 values[n++] = primary_event_id(event);
3398
3399 perf_output_copy(handle, values, n * sizeof(u64));
3400}
3401
3402/*
3403 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3404 */
3405static void perf_output_read_group(struct perf_output_handle *handle,
3406 struct perf_event *event)
3407{
3408 struct perf_event *leader = event->group_leader, *sub;
3409 u64 read_format = event->attr.read_format;
3410 u64 values[5];
3411 int n = 0;
3412
3413 values[n++] = 1 + leader->nr_siblings;
3414
3415 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3416 values[n++] = leader->total_time_enabled;
3417
3418 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3419 values[n++] = leader->total_time_running;
3420
3421 if (leader != event)
3422 leader->pmu->read(leader);
3423
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003424 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425 if (read_format & PERF_FORMAT_ID)
3426 values[n++] = primary_event_id(leader);
3427
3428 perf_output_copy(handle, values, n * sizeof(u64));
3429
3430 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3431 n = 0;
3432
3433 if (sub != event)
3434 sub->pmu->read(sub);
3435
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003436 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003437 if (read_format & PERF_FORMAT_ID)
3438 values[n++] = primary_event_id(sub);
3439
3440 perf_output_copy(handle, values, n * sizeof(u64));
3441 }
3442}
3443
3444static void perf_output_read(struct perf_output_handle *handle,
3445 struct perf_event *event)
3446{
3447 if (event->attr.read_format & PERF_FORMAT_GROUP)
3448 perf_output_read_group(handle, event);
3449 else
3450 perf_output_read_one(handle, event);
3451}
3452
3453void perf_output_sample(struct perf_output_handle *handle,
3454 struct perf_event_header *header,
3455 struct perf_sample_data *data,
3456 struct perf_event *event)
3457{
3458 u64 sample_type = data->type;
3459
3460 perf_output_put(handle, *header);
3461
3462 if (sample_type & PERF_SAMPLE_IP)
3463 perf_output_put(handle, data->ip);
3464
3465 if (sample_type & PERF_SAMPLE_TID)
3466 perf_output_put(handle, data->tid_entry);
3467
3468 if (sample_type & PERF_SAMPLE_TIME)
3469 perf_output_put(handle, data->time);
3470
3471 if (sample_type & PERF_SAMPLE_ADDR)
3472 perf_output_put(handle, data->addr);
3473
3474 if (sample_type & PERF_SAMPLE_ID)
3475 perf_output_put(handle, data->id);
3476
3477 if (sample_type & PERF_SAMPLE_STREAM_ID)
3478 perf_output_put(handle, data->stream_id);
3479
3480 if (sample_type & PERF_SAMPLE_CPU)
3481 perf_output_put(handle, data->cpu_entry);
3482
3483 if (sample_type & PERF_SAMPLE_PERIOD)
3484 perf_output_put(handle, data->period);
3485
3486 if (sample_type & PERF_SAMPLE_READ)
3487 perf_output_read(handle, event);
3488
3489 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3490 if (data->callchain) {
3491 int size = 1;
3492
3493 if (data->callchain)
3494 size += data->callchain->nr;
3495
3496 size *= sizeof(u64);
3497
3498 perf_output_copy(handle, data->callchain, size);
3499 } else {
3500 u64 nr = 0;
3501 perf_output_put(handle, nr);
3502 }
3503 }
3504
3505 if (sample_type & PERF_SAMPLE_RAW) {
3506 if (data->raw) {
3507 perf_output_put(handle, data->raw->size);
3508 perf_output_copy(handle, data->raw->data,
3509 data->raw->size);
3510 } else {
3511 struct {
3512 u32 size;
3513 u32 data;
3514 } raw = {
3515 .size = sizeof(u32),
3516 .data = 0,
3517 };
3518 perf_output_put(handle, raw);
3519 }
3520 }
3521}
3522
3523void perf_prepare_sample(struct perf_event_header *header,
3524 struct perf_sample_data *data,
3525 struct perf_event *event,
3526 struct pt_regs *regs)
3527{
3528 u64 sample_type = event->attr.sample_type;
3529
3530 data->type = sample_type;
3531
3532 header->type = PERF_RECORD_SAMPLE;
3533 header->size = sizeof(*header);
3534
3535 header->misc = 0;
3536 header->misc |= perf_misc_flags(regs);
3537
3538 if (sample_type & PERF_SAMPLE_IP) {
3539 data->ip = perf_instruction_pointer(regs);
3540
3541 header->size += sizeof(data->ip);
3542 }
3543
3544 if (sample_type & PERF_SAMPLE_TID) {
3545 /* namespace issues */
3546 data->tid_entry.pid = perf_event_pid(event, current);
3547 data->tid_entry.tid = perf_event_tid(event, current);
3548
3549 header->size += sizeof(data->tid_entry);
3550 }
3551
3552 if (sample_type & PERF_SAMPLE_TIME) {
3553 data->time = perf_clock();
3554
3555 header->size += sizeof(data->time);
3556 }
3557
3558 if (sample_type & PERF_SAMPLE_ADDR)
3559 header->size += sizeof(data->addr);
3560
3561 if (sample_type & PERF_SAMPLE_ID) {
3562 data->id = primary_event_id(event);
3563
3564 header->size += sizeof(data->id);
3565 }
3566
3567 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3568 data->stream_id = event->id;
3569
3570 header->size += sizeof(data->stream_id);
3571 }
3572
3573 if (sample_type & PERF_SAMPLE_CPU) {
3574 data->cpu_entry.cpu = raw_smp_processor_id();
3575 data->cpu_entry.reserved = 0;
3576
3577 header->size += sizeof(data->cpu_entry);
3578 }
3579
3580 if (sample_type & PERF_SAMPLE_PERIOD)
3581 header->size += sizeof(data->period);
3582
3583 if (sample_type & PERF_SAMPLE_READ)
3584 header->size += perf_event_read_size(event);
3585
3586 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3587 int size = 1;
3588
3589 data->callchain = perf_callchain(regs);
3590
3591 if (data->callchain)
3592 size += data->callchain->nr;
3593
3594 header->size += size * sizeof(u64);
3595 }
3596
3597 if (sample_type & PERF_SAMPLE_RAW) {
3598 int size = sizeof(u32);
3599
3600 if (data->raw)
3601 size += data->raw->size;
3602 else
3603 size += sizeof(u32);
3604
3605 WARN_ON_ONCE(size & (sizeof(u64)-1));
3606 header->size += size;
3607 }
3608}
3609
3610static void perf_event_output(struct perf_event *event, int nmi,
3611 struct perf_sample_data *data,
3612 struct pt_regs *regs)
3613{
3614 struct perf_output_handle handle;
3615 struct perf_event_header header;
3616
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003617 /* protect the callchain buffers */
3618 rcu_read_lock();
3619
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003620 perf_prepare_sample(&header, data, event, regs);
3621
3622 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003623 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003624
3625 perf_output_sample(&handle, &header, data, event);
3626
3627 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003628
3629exit:
3630 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003631}
3632
3633/*
3634 * read event_id
3635 */
3636
3637struct perf_read_event {
3638 struct perf_event_header header;
3639
3640 u32 pid;
3641 u32 tid;
3642};
3643
3644static void
3645perf_event_read_event(struct perf_event *event,
3646 struct task_struct *task)
3647{
3648 struct perf_output_handle handle;
3649 struct perf_read_event read_event = {
3650 .header = {
3651 .type = PERF_RECORD_READ,
3652 .misc = 0,
3653 .size = sizeof(read_event) + perf_event_read_size(event),
3654 },
3655 .pid = perf_event_pid(event, task),
3656 .tid = perf_event_tid(event, task),
3657 };
3658 int ret;
3659
3660 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3661 if (ret)
3662 return;
3663
3664 perf_output_put(&handle, read_event);
3665 perf_output_read(&handle, event);
3666
3667 perf_output_end(&handle);
3668}
3669
3670/*
3671 * task tracking -- fork/exit
3672 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003673 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003674 */
3675
3676struct perf_task_event {
3677 struct task_struct *task;
3678 struct perf_event_context *task_ctx;
3679
3680 struct {
3681 struct perf_event_header header;
3682
3683 u32 pid;
3684 u32 ppid;
3685 u32 tid;
3686 u32 ptid;
3687 u64 time;
3688 } event_id;
3689};
3690
3691static void perf_event_task_output(struct perf_event *event,
3692 struct perf_task_event *task_event)
3693{
3694 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003695 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003696 int size, ret;
3697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003698 size = task_event->event_id.header.size;
3699 ret = perf_output_begin(&handle, event, size, 0, 0);
3700
Peter Zijlstraef607772010-05-18 10:50:41 +02003701 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702 return;
3703
3704 task_event->event_id.pid = perf_event_pid(event, task);
3705 task_event->event_id.ppid = perf_event_pid(event, current);
3706
3707 task_event->event_id.tid = perf_event_tid(event, task);
3708 task_event->event_id.ptid = perf_event_tid(event, current);
3709
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003710 perf_output_put(&handle, task_event->event_id);
3711
3712 perf_output_end(&handle);
3713}
3714
3715static int perf_event_task_match(struct perf_event *event)
3716{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003717 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003718 return 0;
3719
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003720 if (event->cpu != -1 && event->cpu != smp_processor_id())
3721 return 0;
3722
Eric B Munson3af9e852010-05-18 15:30:49 +01003723 if (event->attr.comm || event->attr.mmap ||
3724 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003725 return 1;
3726
3727 return 0;
3728}
3729
3730static void perf_event_task_ctx(struct perf_event_context *ctx,
3731 struct perf_task_event *task_event)
3732{
3733 struct perf_event *event;
3734
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003735 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3736 if (perf_event_task_match(event))
3737 perf_event_task_output(event, task_event);
3738 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003739}
3740
3741static void perf_event_task_event(struct perf_task_event *task_event)
3742{
3743 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003744 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003745 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003746 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003747
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003748 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003749 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003750 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003751 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003752
3753 ctx = task_event->task_ctx;
3754 if (!ctx) {
3755 ctxn = pmu->task_ctx_nr;
3756 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003757 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003758 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3759 }
3760 if (ctx)
3761 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003762next:
3763 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003764 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003765 rcu_read_unlock();
3766}
3767
3768static void perf_event_task(struct task_struct *task,
3769 struct perf_event_context *task_ctx,
3770 int new)
3771{
3772 struct perf_task_event task_event;
3773
3774 if (!atomic_read(&nr_comm_events) &&
3775 !atomic_read(&nr_mmap_events) &&
3776 !atomic_read(&nr_task_events))
3777 return;
3778
3779 task_event = (struct perf_task_event){
3780 .task = task,
3781 .task_ctx = task_ctx,
3782 .event_id = {
3783 .header = {
3784 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3785 .misc = 0,
3786 .size = sizeof(task_event.event_id),
3787 },
3788 /* .pid */
3789 /* .ppid */
3790 /* .tid */
3791 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003792 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003793 },
3794 };
3795
3796 perf_event_task_event(&task_event);
3797}
3798
3799void perf_event_fork(struct task_struct *task)
3800{
3801 perf_event_task(task, NULL, 1);
3802}
3803
3804/*
3805 * comm tracking
3806 */
3807
3808struct perf_comm_event {
3809 struct task_struct *task;
3810 char *comm;
3811 int comm_size;
3812
3813 struct {
3814 struct perf_event_header header;
3815
3816 u32 pid;
3817 u32 tid;
3818 } event_id;
3819};
3820
3821static void perf_event_comm_output(struct perf_event *event,
3822 struct perf_comm_event *comm_event)
3823{
3824 struct perf_output_handle handle;
3825 int size = comm_event->event_id.header.size;
3826 int ret = perf_output_begin(&handle, event, size, 0, 0);
3827
3828 if (ret)
3829 return;
3830
3831 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3832 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3833
3834 perf_output_put(&handle, comm_event->event_id);
3835 perf_output_copy(&handle, comm_event->comm,
3836 comm_event->comm_size);
3837 perf_output_end(&handle);
3838}
3839
3840static int perf_event_comm_match(struct perf_event *event)
3841{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003842 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003843 return 0;
3844
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003845 if (event->cpu != -1 && event->cpu != smp_processor_id())
3846 return 0;
3847
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848 if (event->attr.comm)
3849 return 1;
3850
3851 return 0;
3852}
3853
3854static void perf_event_comm_ctx(struct perf_event_context *ctx,
3855 struct perf_comm_event *comm_event)
3856{
3857 struct perf_event *event;
3858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003859 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3860 if (perf_event_comm_match(event))
3861 perf_event_comm_output(event, comm_event);
3862 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003863}
3864
3865static void perf_event_comm_event(struct perf_comm_event *comm_event)
3866{
3867 struct perf_cpu_context *cpuctx;
3868 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003869 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003870 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003871 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003872 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003873
3874 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003875 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003876 size = ALIGN(strlen(comm)+1, sizeof(u64));
3877
3878 comm_event->comm = comm;
3879 comm_event->comm_size = size;
3880
3881 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3882
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003883 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003884 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003885 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003886 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003887
3888 ctxn = pmu->task_ctx_nr;
3889 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003890 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003891
3892 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3893 if (ctx)
3894 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003895next:
3896 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003897 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003898 rcu_read_unlock();
3899}
3900
3901void perf_event_comm(struct task_struct *task)
3902{
3903 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003904 struct perf_event_context *ctx;
3905 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003906
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003907 for_each_task_context_nr(ctxn) {
3908 ctx = task->perf_event_ctxp[ctxn];
3909 if (!ctx)
3910 continue;
3911
3912 perf_event_enable_on_exec(ctx);
3913 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003914
3915 if (!atomic_read(&nr_comm_events))
3916 return;
3917
3918 comm_event = (struct perf_comm_event){
3919 .task = task,
3920 /* .comm */
3921 /* .comm_size */
3922 .event_id = {
3923 .header = {
3924 .type = PERF_RECORD_COMM,
3925 .misc = 0,
3926 /* .size */
3927 },
3928 /* .pid */
3929 /* .tid */
3930 },
3931 };
3932
3933 perf_event_comm_event(&comm_event);
3934}
3935
3936/*
3937 * mmap tracking
3938 */
3939
3940struct perf_mmap_event {
3941 struct vm_area_struct *vma;
3942
3943 const char *file_name;
3944 int file_size;
3945
3946 struct {
3947 struct perf_event_header header;
3948
3949 u32 pid;
3950 u32 tid;
3951 u64 start;
3952 u64 len;
3953 u64 pgoff;
3954 } event_id;
3955};
3956
3957static void perf_event_mmap_output(struct perf_event *event,
3958 struct perf_mmap_event *mmap_event)
3959{
3960 struct perf_output_handle handle;
3961 int size = mmap_event->event_id.header.size;
3962 int ret = perf_output_begin(&handle, event, size, 0, 0);
3963
3964 if (ret)
3965 return;
3966
3967 mmap_event->event_id.pid = perf_event_pid(event, current);
3968 mmap_event->event_id.tid = perf_event_tid(event, current);
3969
3970 perf_output_put(&handle, mmap_event->event_id);
3971 perf_output_copy(&handle, mmap_event->file_name,
3972 mmap_event->file_size);
3973 perf_output_end(&handle);
3974}
3975
3976static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003977 struct perf_mmap_event *mmap_event,
3978 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003980 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003981 return 0;
3982
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003983 if (event->cpu != -1 && event->cpu != smp_processor_id())
3984 return 0;
3985
Eric B Munson3af9e852010-05-18 15:30:49 +01003986 if ((!executable && event->attr.mmap_data) ||
3987 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003988 return 1;
3989
3990 return 0;
3991}
3992
3993static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01003994 struct perf_mmap_event *mmap_event,
3995 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003996{
3997 struct perf_event *event;
3998
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003999 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004000 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004001 perf_event_mmap_output(event, mmap_event);
4002 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004003}
4004
4005static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4006{
4007 struct perf_cpu_context *cpuctx;
4008 struct perf_event_context *ctx;
4009 struct vm_area_struct *vma = mmap_event->vma;
4010 struct file *file = vma->vm_file;
4011 unsigned int size;
4012 char tmp[16];
4013 char *buf = NULL;
4014 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004015 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004016 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004017
4018 memset(tmp, 0, sizeof(tmp));
4019
4020 if (file) {
4021 /*
4022 * d_path works from the end of the buffer backwards, so we
4023 * need to add enough zero bytes after the string to handle
4024 * the 64bit alignment we do later.
4025 */
4026 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4027 if (!buf) {
4028 name = strncpy(tmp, "//enomem", sizeof(tmp));
4029 goto got_name;
4030 }
4031 name = d_path(&file->f_path, buf, PATH_MAX);
4032 if (IS_ERR(name)) {
4033 name = strncpy(tmp, "//toolong", sizeof(tmp));
4034 goto got_name;
4035 }
4036 } else {
4037 if (arch_vma_name(mmap_event->vma)) {
4038 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4039 sizeof(tmp));
4040 goto got_name;
4041 }
4042
4043 if (!vma->vm_mm) {
4044 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4045 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004046 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4047 vma->vm_end >= vma->vm_mm->brk) {
4048 name = strncpy(tmp, "[heap]", sizeof(tmp));
4049 goto got_name;
4050 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4051 vma->vm_end >= vma->vm_mm->start_stack) {
4052 name = strncpy(tmp, "[stack]", sizeof(tmp));
4053 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004054 }
4055
4056 name = strncpy(tmp, "//anon", sizeof(tmp));
4057 goto got_name;
4058 }
4059
4060got_name:
4061 size = ALIGN(strlen(name)+1, sizeof(u64));
4062
4063 mmap_event->file_name = name;
4064 mmap_event->file_size = size;
4065
4066 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4067
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004068 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004069 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004070 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004071 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4072 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004073
4074 ctxn = pmu->task_ctx_nr;
4075 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004076 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004077
4078 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4079 if (ctx) {
4080 perf_event_mmap_ctx(ctx, mmap_event,
4081 vma->vm_flags & VM_EXEC);
4082 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004083next:
4084 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004085 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004086 rcu_read_unlock();
4087
4088 kfree(buf);
4089}
4090
Eric B Munson3af9e852010-05-18 15:30:49 +01004091void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004092{
4093 struct perf_mmap_event mmap_event;
4094
4095 if (!atomic_read(&nr_mmap_events))
4096 return;
4097
4098 mmap_event = (struct perf_mmap_event){
4099 .vma = vma,
4100 /* .file_name */
4101 /* .file_size */
4102 .event_id = {
4103 .header = {
4104 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004105 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004106 /* .size */
4107 },
4108 /* .pid */
4109 /* .tid */
4110 .start = vma->vm_start,
4111 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004112 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004113 },
4114 };
4115
4116 perf_event_mmap_event(&mmap_event);
4117}
4118
4119/*
4120 * IRQ throttle logging
4121 */
4122
4123static void perf_log_throttle(struct perf_event *event, int enable)
4124{
4125 struct perf_output_handle handle;
4126 int ret;
4127
4128 struct {
4129 struct perf_event_header header;
4130 u64 time;
4131 u64 id;
4132 u64 stream_id;
4133 } throttle_event = {
4134 .header = {
4135 .type = PERF_RECORD_THROTTLE,
4136 .misc = 0,
4137 .size = sizeof(throttle_event),
4138 },
4139 .time = perf_clock(),
4140 .id = primary_event_id(event),
4141 .stream_id = event->id,
4142 };
4143
4144 if (enable)
4145 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4146
4147 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4148 if (ret)
4149 return;
4150
4151 perf_output_put(&handle, throttle_event);
4152 perf_output_end(&handle);
4153}
4154
4155/*
4156 * Generic event overflow handling, sampling.
4157 */
4158
4159static int __perf_event_overflow(struct perf_event *event, int nmi,
4160 int throttle, struct perf_sample_data *data,
4161 struct pt_regs *regs)
4162{
4163 int events = atomic_read(&event->event_limit);
4164 struct hw_perf_event *hwc = &event->hw;
4165 int ret = 0;
4166
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004167 if (!throttle) {
4168 hwc->interrupts++;
4169 } else {
4170 if (hwc->interrupts != MAX_INTERRUPTS) {
4171 hwc->interrupts++;
4172 if (HZ * hwc->interrupts >
4173 (u64)sysctl_perf_event_sample_rate) {
4174 hwc->interrupts = MAX_INTERRUPTS;
4175 perf_log_throttle(event, 0);
4176 ret = 1;
4177 }
4178 } else {
4179 /*
4180 * Keep re-disabling events even though on the previous
4181 * pass we disabled it - just in case we raced with a
4182 * sched-in and the event got enabled again:
4183 */
4184 ret = 1;
4185 }
4186 }
4187
4188 if (event->attr.freq) {
4189 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004190 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004191
Peter Zijlstraabd50712010-01-26 18:50:16 +01004192 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004193
Peter Zijlstraabd50712010-01-26 18:50:16 +01004194 if (delta > 0 && delta < 2*TICK_NSEC)
4195 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004196 }
4197
4198 /*
4199 * XXX event_limit might not quite work as expected on inherited
4200 * events
4201 */
4202
4203 event->pending_kill = POLL_IN;
4204 if (events && atomic_dec_and_test(&event->event_limit)) {
4205 ret = 1;
4206 event->pending_kill = POLL_HUP;
4207 if (nmi) {
4208 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004209 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004210 } else
4211 perf_event_disable(event);
4212 }
4213
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004214 if (event->overflow_handler)
4215 event->overflow_handler(event, nmi, data, regs);
4216 else
4217 perf_event_output(event, nmi, data, regs);
4218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004219 return ret;
4220}
4221
4222int perf_event_overflow(struct perf_event *event, int nmi,
4223 struct perf_sample_data *data,
4224 struct pt_regs *regs)
4225{
4226 return __perf_event_overflow(event, nmi, 1, data, regs);
4227}
4228
4229/*
4230 * Generic software event infrastructure
4231 */
4232
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004233struct swevent_htable {
4234 struct swevent_hlist *swevent_hlist;
4235 struct mutex hlist_mutex;
4236 int hlist_refcount;
4237
4238 /* Recursion avoidance in each contexts */
4239 int recursion[PERF_NR_CONTEXTS];
4240};
4241
4242static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004244/*
4245 * We directly increment event->count and keep a second value in
4246 * event->hw.period_left to count intervals. This period event
4247 * is kept in the range [-sample_period, 0] so that we can use the
4248 * sign as trigger.
4249 */
4250
4251static u64 perf_swevent_set_period(struct perf_event *event)
4252{
4253 struct hw_perf_event *hwc = &event->hw;
4254 u64 period = hwc->last_period;
4255 u64 nr, offset;
4256 s64 old, val;
4257
4258 hwc->last_period = hwc->sample_period;
4259
4260again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004261 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004262 if (val < 0)
4263 return 0;
4264
4265 nr = div64_u64(period + val, period);
4266 offset = nr * period;
4267 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004268 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 goto again;
4270
4271 return nr;
4272}
4273
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004274static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004275 int nmi, struct perf_sample_data *data,
4276 struct pt_regs *regs)
4277{
4278 struct hw_perf_event *hwc = &event->hw;
4279 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280
4281 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004282 if (!overflow)
4283 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004284
4285 if (hwc->interrupts == MAX_INTERRUPTS)
4286 return;
4287
4288 for (; overflow; overflow--) {
4289 if (__perf_event_overflow(event, nmi, throttle,
4290 data, regs)) {
4291 /*
4292 * We inhibit the overflow from happening when
4293 * hwc->interrupts == MAX_INTERRUPTS.
4294 */
4295 break;
4296 }
4297 throttle = 1;
4298 }
4299}
4300
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004301static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302 int nmi, struct perf_sample_data *data,
4303 struct pt_regs *regs)
4304{
4305 struct hw_perf_event *hwc = &event->hw;
4306
Peter Zijlstrae7850592010-05-21 14:43:08 +02004307 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004308
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309 if (!regs)
4310 return;
4311
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004312 if (!hwc->sample_period)
4313 return;
4314
4315 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4316 return perf_swevent_overflow(event, 1, nmi, data, regs);
4317
Peter Zijlstrae7850592010-05-21 14:43:08 +02004318 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004319 return;
4320
4321 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004322}
4323
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004324static int perf_exclude_event(struct perf_event *event,
4325 struct pt_regs *regs)
4326{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004327 if (event->hw.state & PERF_HES_STOPPED)
4328 return 0;
4329
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004330 if (regs) {
4331 if (event->attr.exclude_user && user_mode(regs))
4332 return 1;
4333
4334 if (event->attr.exclude_kernel && !user_mode(regs))
4335 return 1;
4336 }
4337
4338 return 0;
4339}
4340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341static int perf_swevent_match(struct perf_event *event,
4342 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004343 u32 event_id,
4344 struct perf_sample_data *data,
4345 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004347 if (event->attr.type != type)
4348 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350 if (event->attr.config != event_id)
4351 return 0;
4352
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004353 if (perf_exclude_event(event, regs))
4354 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004355
4356 return 1;
4357}
4358
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004359static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004361 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004362
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004363 return hash_64(val, SWEVENT_HLIST_BITS);
4364}
4365
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004366static inline struct hlist_head *
4367__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004368{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004369 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004370
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004371 return &hlist->heads[hash];
4372}
4373
4374/* For the read side: events when they trigger */
4375static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004376find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004377{
4378 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004379
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004380 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004381 if (!hlist)
4382 return NULL;
4383
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004384 return __find_swevent_head(hlist, type, event_id);
4385}
4386
4387/* For the event head insertion and removal in the hlist */
4388static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004389find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004390{
4391 struct swevent_hlist *hlist;
4392 u32 event_id = event->attr.config;
4393 u64 type = event->attr.type;
4394
4395 /*
4396 * Event scheduling is always serialized against hlist allocation
4397 * and release. Which makes the protected version suitable here.
4398 * The context lock guarantees that.
4399 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004400 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004401 lockdep_is_held(&event->ctx->lock));
4402 if (!hlist)
4403 return NULL;
4404
4405 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004406}
4407
4408static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4409 u64 nr, int nmi,
4410 struct perf_sample_data *data,
4411 struct pt_regs *regs)
4412{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004413 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004414 struct perf_event *event;
4415 struct hlist_node *node;
4416 struct hlist_head *head;
4417
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004418 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004419 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004420 if (!head)
4421 goto end;
4422
4423 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004424 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004425 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004426 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004427end:
4428 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429}
4430
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004431int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004433 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004434
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004435 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004436}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004437EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004438
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004439void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004441 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004442
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004443 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004444}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4447 struct pt_regs *regs, u64 addr)
4448{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004449 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004450 int rctx;
4451
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004452 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004453 rctx = perf_swevent_get_recursion_context();
4454 if (rctx < 0)
4455 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004456
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004457 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004458
4459 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004460
4461 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004462 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463}
4464
4465static void perf_swevent_read(struct perf_event *event)
4466{
4467}
4468
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004469static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004470{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004471 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004472 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004473 struct hlist_head *head;
4474
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475 if (hwc->sample_period) {
4476 hwc->last_period = hwc->sample_period;
4477 perf_swevent_set_period(event);
4478 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004479
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004480 hwc->state = !(flags & PERF_EF_START);
4481
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004482 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004483 if (WARN_ON_ONCE(!head))
4484 return -EINVAL;
4485
4486 hlist_add_head_rcu(&event->hlist_entry, head);
4487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488 return 0;
4489}
4490
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004491static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004492{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004493 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494}
4495
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004496static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004497{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004498 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004499}
4500
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004501static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004502{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004503 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004504}
4505
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004506/* Deref the hlist from the update side */
4507static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004508swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004509{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004510 return rcu_dereference_protected(swhash->swevent_hlist,
4511 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004512}
4513
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004514static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4515{
4516 struct swevent_hlist *hlist;
4517
4518 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4519 kfree(hlist);
4520}
4521
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004522static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004523{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004524 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004525
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004526 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004527 return;
4528
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004529 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004530 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4531}
4532
4533static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4534{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004535 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004536
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004537 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004538
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004539 if (!--swhash->hlist_refcount)
4540 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004541
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004542 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004543}
4544
4545static void swevent_hlist_put(struct perf_event *event)
4546{
4547 int cpu;
4548
4549 if (event->cpu != -1) {
4550 swevent_hlist_put_cpu(event, event->cpu);
4551 return;
4552 }
4553
4554 for_each_possible_cpu(cpu)
4555 swevent_hlist_put_cpu(event, cpu);
4556}
4557
4558static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4559{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004560 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004561 int err = 0;
4562
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004563 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004564
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004565 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004566 struct swevent_hlist *hlist;
4567
4568 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4569 if (!hlist) {
4570 err = -ENOMEM;
4571 goto exit;
4572 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004573 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004574 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004575 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004576exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004577 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004578
4579 return err;
4580}
4581
4582static int swevent_hlist_get(struct perf_event *event)
4583{
4584 int err;
4585 int cpu, failed_cpu;
4586
4587 if (event->cpu != -1)
4588 return swevent_hlist_get_cpu(event, event->cpu);
4589
4590 get_online_cpus();
4591 for_each_possible_cpu(cpu) {
4592 err = swevent_hlist_get_cpu(event, cpu);
4593 if (err) {
4594 failed_cpu = cpu;
4595 goto fail;
4596 }
4597 }
4598 put_online_cpus();
4599
4600 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004601fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004602 for_each_possible_cpu(cpu) {
4603 if (cpu == failed_cpu)
4604 break;
4605 swevent_hlist_put_cpu(event, cpu);
4606 }
4607
4608 put_online_cpus();
4609 return err;
4610}
4611
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004612atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004613
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004614static void sw_perf_event_destroy(struct perf_event *event)
4615{
4616 u64 event_id = event->attr.config;
4617
4618 WARN_ON(event->parent);
4619
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004620 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004621 swevent_hlist_put(event);
4622}
4623
4624static int perf_swevent_init(struct perf_event *event)
4625{
4626 int event_id = event->attr.config;
4627
4628 if (event->attr.type != PERF_TYPE_SOFTWARE)
4629 return -ENOENT;
4630
4631 switch (event_id) {
4632 case PERF_COUNT_SW_CPU_CLOCK:
4633 case PERF_COUNT_SW_TASK_CLOCK:
4634 return -ENOENT;
4635
4636 default:
4637 break;
4638 }
4639
4640 if (event_id > PERF_COUNT_SW_MAX)
4641 return -ENOENT;
4642
4643 if (!event->parent) {
4644 int err;
4645
4646 err = swevent_hlist_get(event);
4647 if (err)
4648 return err;
4649
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004650 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004651 event->destroy = sw_perf_event_destroy;
4652 }
4653
4654 return 0;
4655}
4656
4657static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004658 .task_ctx_nr = perf_sw_context,
4659
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004660 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004661 .add = perf_swevent_add,
4662 .del = perf_swevent_del,
4663 .start = perf_swevent_start,
4664 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004665 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004666};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004667
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004668#ifdef CONFIG_EVENT_TRACING
4669
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004670static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004671 struct perf_sample_data *data)
4672{
4673 void *record = data->raw->data;
4674
4675 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4676 return 1;
4677 return 0;
4678}
4679
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004680static int perf_tp_event_match(struct perf_event *event,
4681 struct perf_sample_data *data,
4682 struct pt_regs *regs)
4683{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004684 /*
4685 * All tracepoints are from kernel-space.
4686 */
4687 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004688 return 0;
4689
4690 if (!perf_tp_filter_match(event, data))
4691 return 0;
4692
4693 return 1;
4694}
4695
4696void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004697 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004698{
4699 struct perf_sample_data data;
4700 struct perf_event *event;
4701 struct hlist_node *node;
4702
4703 struct perf_raw_record raw = {
4704 .size = entry_size,
4705 .data = record,
4706 };
4707
4708 perf_sample_data_init(&data, addr);
4709 data.raw = &raw;
4710
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004711 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4712 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004713 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004714 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004715
4716 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004717}
4718EXPORT_SYMBOL_GPL(perf_tp_event);
4719
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004720static void tp_perf_event_destroy(struct perf_event *event)
4721{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004722 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004723}
4724
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004725static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004726{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004727 int err;
4728
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004729 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4730 return -ENOENT;
4731
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732 /*
4733 * Raw tracepoint data is a severe data leak, only allow root to
4734 * have these.
4735 */
4736 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4737 perf_paranoid_tracepoint_raw() &&
4738 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004739 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004740
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004741 err = perf_trace_init(event);
4742 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004743 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004744
4745 event->destroy = tp_perf_event_destroy;
4746
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004747 return 0;
4748}
4749
4750static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004751 .task_ctx_nr = perf_sw_context,
4752
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004753 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004754 .add = perf_trace_add,
4755 .del = perf_trace_del,
4756 .start = perf_swevent_start,
4757 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004758 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004759};
4760
4761static inline void perf_tp_register(void)
4762{
4763 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764}
Li Zefan6fb29152009-10-15 11:21:42 +08004765
4766static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4767{
4768 char *filter_str;
4769 int ret;
4770
4771 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4772 return -EINVAL;
4773
4774 filter_str = strndup_user(arg, PAGE_SIZE);
4775 if (IS_ERR(filter_str))
4776 return PTR_ERR(filter_str);
4777
4778 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4779
4780 kfree(filter_str);
4781 return ret;
4782}
4783
4784static void perf_event_free_filter(struct perf_event *event)
4785{
4786 ftrace_profile_free_filter(event);
4787}
4788
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004789#else
Li Zefan6fb29152009-10-15 11:21:42 +08004790
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004791static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004792{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004793}
Li Zefan6fb29152009-10-15 11:21:42 +08004794
4795static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4796{
4797 return -ENOENT;
4798}
4799
4800static void perf_event_free_filter(struct perf_event *event)
4801{
4802}
4803
Li Zefan07b139c2009-12-21 14:27:35 +08004804#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004805
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004806#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004807void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004808{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004809 struct perf_sample_data sample;
4810 struct pt_regs *regs = data;
4811
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004812 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004813
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004814 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4815 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004816}
4817#endif
4818
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004819/*
4820 * hrtimer based swevent callback
4821 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004822
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004823static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004824{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004825 enum hrtimer_restart ret = HRTIMER_RESTART;
4826 struct perf_sample_data data;
4827 struct pt_regs *regs;
4828 struct perf_event *event;
4829 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004830
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004831 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4832 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004833
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004834 perf_sample_data_init(&data, 0);
4835 data.period = event->hw.last_period;
4836 regs = get_irq_regs();
4837
4838 if (regs && !perf_exclude_event(event, regs)) {
4839 if (!(event->attr.exclude_idle && current->pid == 0))
4840 if (perf_event_overflow(event, 0, &data, regs))
4841 ret = HRTIMER_NORESTART;
4842 }
4843
4844 period = max_t(u64, 10000, event->hw.sample_period);
4845 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4846
4847 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004848}
4849
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004850static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004852 struct hw_perf_event *hwc = &event->hw;
4853
4854 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4855 hwc->hrtimer.function = perf_swevent_hrtimer;
4856 if (hwc->sample_period) {
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004857 s64 period = local64_read(&hwc->period_left);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004858
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004859 if (period) {
4860 if (period < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004861 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004862
4863 local64_set(&hwc->period_left, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004864 } else {
4865 period = max_t(u64, 10000, hwc->sample_period);
4866 }
4867 __hrtimer_start_range_ns(&hwc->hrtimer,
4868 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02004869 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004870 }
4871}
4872
4873static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4874{
4875 struct hw_perf_event *hwc = &event->hw;
4876
4877 if (hwc->sample_period) {
4878 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004879 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004880
4881 hrtimer_cancel(&hwc->hrtimer);
4882 }
4883}
4884
4885/*
4886 * Software event: cpu wall time clock
4887 */
4888
4889static void cpu_clock_event_update(struct perf_event *event)
4890{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004891 s64 prev;
4892 u64 now;
4893
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004894 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004895 prev = local64_xchg(&event->hw.prev_count, now);
4896 local64_add(now - prev, &event->count);
4897}
4898
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004899static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004900{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004901 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004902 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004903}
4904
4905static void cpu_clock_event_stop(struct perf_event *event, int flags)
4906{
4907 perf_swevent_cancel_hrtimer(event);
4908 cpu_clock_event_update(event);
4909}
4910
4911static int cpu_clock_event_add(struct perf_event *event, int flags)
4912{
4913 if (flags & PERF_EF_START)
4914 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004915
4916 return 0;
4917}
4918
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004919static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004920{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004921 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004922}
4923
4924static void cpu_clock_event_read(struct perf_event *event)
4925{
4926 cpu_clock_event_update(event);
4927}
4928
4929static int cpu_clock_event_init(struct perf_event *event)
4930{
4931 if (event->attr.type != PERF_TYPE_SOFTWARE)
4932 return -ENOENT;
4933
4934 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4935 return -ENOENT;
4936
4937 return 0;
4938}
4939
4940static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004941 .task_ctx_nr = perf_sw_context,
4942
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004943 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004944 .add = cpu_clock_event_add,
4945 .del = cpu_clock_event_del,
4946 .start = cpu_clock_event_start,
4947 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004948 .read = cpu_clock_event_read,
4949};
4950
4951/*
4952 * Software event: task time clock
4953 */
4954
4955static void task_clock_event_update(struct perf_event *event, u64 now)
4956{
4957 u64 prev;
4958 s64 delta;
4959
4960 prev = local64_xchg(&event->hw.prev_count, now);
4961 delta = now - prev;
4962 local64_add(delta, &event->count);
4963}
4964
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004965static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004966{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004967 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004968 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004969}
4970
4971static void task_clock_event_stop(struct perf_event *event, int flags)
4972{
4973 perf_swevent_cancel_hrtimer(event);
4974 task_clock_event_update(event, event->ctx->time);
4975}
4976
4977static int task_clock_event_add(struct perf_event *event, int flags)
4978{
4979 if (flags & PERF_EF_START)
4980 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004981
4982 return 0;
4983}
4984
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004985static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004986{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004987 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004988}
4989
4990static void task_clock_event_read(struct perf_event *event)
4991{
4992 u64 time;
4993
4994 if (!in_nmi()) {
4995 update_context_time(event->ctx);
4996 time = event->ctx->time;
4997 } else {
4998 u64 now = perf_clock();
4999 u64 delta = now - event->ctx->timestamp;
5000 time = event->ctx->time + delta;
5001 }
5002
5003 task_clock_event_update(event, time);
5004}
5005
5006static int task_clock_event_init(struct perf_event *event)
5007{
5008 if (event->attr.type != PERF_TYPE_SOFTWARE)
5009 return -ENOENT;
5010
5011 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5012 return -ENOENT;
5013
5014 return 0;
5015}
5016
5017static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005018 .task_ctx_nr = perf_sw_context,
5019
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005020 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005021 .add = task_clock_event_add,
5022 .del = task_clock_event_del,
5023 .start = task_clock_event_start,
5024 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005025 .read = task_clock_event_read,
5026};
5027
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005028static void perf_pmu_nop_void(struct pmu *pmu)
5029{
5030}
5031
5032static int perf_pmu_nop_int(struct pmu *pmu)
5033{
5034 return 0;
5035}
5036
5037static void perf_pmu_start_txn(struct pmu *pmu)
5038{
5039 perf_pmu_disable(pmu);
5040}
5041
5042static int perf_pmu_commit_txn(struct pmu *pmu)
5043{
5044 perf_pmu_enable(pmu);
5045 return 0;
5046}
5047
5048static void perf_pmu_cancel_txn(struct pmu *pmu)
5049{
5050 perf_pmu_enable(pmu);
5051}
5052
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005053/*
5054 * Ensures all contexts with the same task_ctx_nr have the same
5055 * pmu_cpu_context too.
5056 */
5057static void *find_pmu_context(int ctxn)
5058{
5059 struct pmu *pmu;
5060
5061 if (ctxn < 0)
5062 return NULL;
5063
5064 list_for_each_entry(pmu, &pmus, entry) {
5065 if (pmu->task_ctx_nr == ctxn)
5066 return pmu->pmu_cpu_context;
5067 }
5068
5069 return NULL;
5070}
5071
5072static void free_pmu_context(void * __percpu cpu_context)
5073{
5074 struct pmu *pmu;
5075
5076 mutex_lock(&pmus_lock);
5077 /*
5078 * Like a real lame refcount.
5079 */
5080 list_for_each_entry(pmu, &pmus, entry) {
5081 if (pmu->pmu_cpu_context == cpu_context)
5082 goto out;
5083 }
5084
5085 free_percpu(cpu_context);
5086out:
5087 mutex_unlock(&pmus_lock);
5088}
5089
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005090int perf_pmu_register(struct pmu *pmu)
5091{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005092 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005093
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005094 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005095 ret = -ENOMEM;
5096 pmu->pmu_disable_count = alloc_percpu(int);
5097 if (!pmu->pmu_disable_count)
5098 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005099
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005100 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5101 if (pmu->pmu_cpu_context)
5102 goto got_cpu_context;
5103
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005104 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5105 if (!pmu->pmu_cpu_context)
5106 goto free_pdc;
5107
5108 for_each_possible_cpu(cpu) {
5109 struct perf_cpu_context *cpuctx;
5110
5111 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005112 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005113 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005114 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005115 cpuctx->jiffies_interval = 1;
5116 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005117 }
5118
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005119got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005120 if (!pmu->start_txn) {
5121 if (pmu->pmu_enable) {
5122 /*
5123 * If we have pmu_enable/pmu_disable calls, install
5124 * transaction stubs that use that to try and batch
5125 * hardware accesses.
5126 */
5127 pmu->start_txn = perf_pmu_start_txn;
5128 pmu->commit_txn = perf_pmu_commit_txn;
5129 pmu->cancel_txn = perf_pmu_cancel_txn;
5130 } else {
5131 pmu->start_txn = perf_pmu_nop_void;
5132 pmu->commit_txn = perf_pmu_nop_int;
5133 pmu->cancel_txn = perf_pmu_nop_void;
5134 }
5135 }
5136
5137 if (!pmu->pmu_enable) {
5138 pmu->pmu_enable = perf_pmu_nop_void;
5139 pmu->pmu_disable = perf_pmu_nop_void;
5140 }
5141
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005142 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005143 ret = 0;
5144unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005145 mutex_unlock(&pmus_lock);
5146
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005147 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005148
5149free_pdc:
5150 free_percpu(pmu->pmu_disable_count);
5151 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005152}
5153
5154void perf_pmu_unregister(struct pmu *pmu)
5155{
5156 mutex_lock(&pmus_lock);
5157 list_del_rcu(&pmu->entry);
5158 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159
5160 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005161 * We dereference the pmu list under both SRCU and regular RCU, so
5162 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005163 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005164 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005165 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005166
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005167 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005168 free_pmu_context(pmu->pmu_cpu_context);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005169}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005170
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005171struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005172{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005173 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005174 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005175
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005176 idx = srcu_read_lock(&pmus_srcu);
5177 list_for_each_entry_rcu(pmu, &pmus, entry) {
5178 int ret = pmu->event_init(event);
5179 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005180 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005181
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005182 if (ret != -ENOENT) {
5183 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005184 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005185 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005187 pmu = ERR_PTR(-ENOENT);
5188unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005189 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190
5191 return pmu;
5192}
5193
5194/*
5195 * Allocate and initialize a event structure
5196 */
5197static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005198perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005199 struct task_struct *task,
5200 struct perf_event *group_leader,
5201 struct perf_event *parent_event,
5202 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005203{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005204 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205 struct perf_event *event;
5206 struct hw_perf_event *hwc;
5207 long err;
5208
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005209 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005210 if (!event)
5211 return ERR_PTR(-ENOMEM);
5212
5213 /*
5214 * Single events are their own group leaders, with an
5215 * empty sibling list:
5216 */
5217 if (!group_leader)
5218 group_leader = event;
5219
5220 mutex_init(&event->child_mutex);
5221 INIT_LIST_HEAD(&event->child_list);
5222
5223 INIT_LIST_HEAD(&event->group_entry);
5224 INIT_LIST_HEAD(&event->event_entry);
5225 INIT_LIST_HEAD(&event->sibling_list);
5226 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005227 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228
5229 mutex_init(&event->mmap_mutex);
5230
5231 event->cpu = cpu;
5232 event->attr = *attr;
5233 event->group_leader = group_leader;
5234 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235 event->oncpu = -1;
5236
5237 event->parent = parent_event;
5238
5239 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5240 event->id = atomic64_inc_return(&perf_event_id);
5241
5242 event->state = PERF_EVENT_STATE_INACTIVE;
5243
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005244 if (task) {
5245 event->attach_state = PERF_ATTACH_TASK;
5246#ifdef CONFIG_HAVE_HW_BREAKPOINT
5247 /*
5248 * hw_breakpoint is a bit difficult here..
5249 */
5250 if (attr->type == PERF_TYPE_BREAKPOINT)
5251 event->hw.bp_target = task;
5252#endif
5253 }
5254
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005255 if (!overflow_handler && parent_event)
5256 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005257
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005258 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260 if (attr->disabled)
5261 event->state = PERF_EVENT_STATE_OFF;
5262
5263 pmu = NULL;
5264
5265 hwc = &event->hw;
5266 hwc->sample_period = attr->sample_period;
5267 if (attr->freq && attr->sample_freq)
5268 hwc->sample_period = 1;
5269 hwc->last_period = hwc->sample_period;
5270
Peter Zijlstrae7850592010-05-21 14:43:08 +02005271 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005272
5273 /*
5274 * we currently do not support PERF_FORMAT_GROUP on inherited events
5275 */
5276 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5277 goto done;
5278
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005279 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281done:
5282 err = 0;
5283 if (!pmu)
5284 err = -EINVAL;
5285 else if (IS_ERR(pmu))
5286 err = PTR_ERR(pmu);
5287
5288 if (err) {
5289 if (event->ns)
5290 put_pid_ns(event->ns);
5291 kfree(event);
5292 return ERR_PTR(err);
5293 }
5294
5295 event->pmu = pmu;
5296
5297 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005298 if (event->attach_state & PERF_ATTACH_TASK)
5299 jump_label_inc(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005300 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301 atomic_inc(&nr_mmap_events);
5302 if (event->attr.comm)
5303 atomic_inc(&nr_comm_events);
5304 if (event->attr.task)
5305 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005306 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5307 err = get_callchain_buffers();
5308 if (err) {
5309 free_event(event);
5310 return ERR_PTR(err);
5311 }
5312 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005313 }
5314
5315 return event;
5316}
5317
5318static int perf_copy_attr(struct perf_event_attr __user *uattr,
5319 struct perf_event_attr *attr)
5320{
5321 u32 size;
5322 int ret;
5323
5324 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5325 return -EFAULT;
5326
5327 /*
5328 * zero the full structure, so that a short copy will be nice.
5329 */
5330 memset(attr, 0, sizeof(*attr));
5331
5332 ret = get_user(size, &uattr->size);
5333 if (ret)
5334 return ret;
5335
5336 if (size > PAGE_SIZE) /* silly large */
5337 goto err_size;
5338
5339 if (!size) /* abi compat */
5340 size = PERF_ATTR_SIZE_VER0;
5341
5342 if (size < PERF_ATTR_SIZE_VER0)
5343 goto err_size;
5344
5345 /*
5346 * If we're handed a bigger struct than we know of,
5347 * ensure all the unknown bits are 0 - i.e. new
5348 * user-space does not rely on any kernel feature
5349 * extensions we dont know about yet.
5350 */
5351 if (size > sizeof(*attr)) {
5352 unsigned char __user *addr;
5353 unsigned char __user *end;
5354 unsigned char val;
5355
5356 addr = (void __user *)uattr + sizeof(*attr);
5357 end = (void __user *)uattr + size;
5358
5359 for (; addr < end; addr++) {
5360 ret = get_user(val, addr);
5361 if (ret)
5362 return ret;
5363 if (val)
5364 goto err_size;
5365 }
5366 size = sizeof(*attr);
5367 }
5368
5369 ret = copy_from_user(attr, uattr, size);
5370 if (ret)
5371 return -EFAULT;
5372
5373 /*
5374 * If the type exists, the corresponding creation will verify
5375 * the attr->config.
5376 */
5377 if (attr->type >= PERF_TYPE_MAX)
5378 return -EINVAL;
5379
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305380 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381 return -EINVAL;
5382
5383 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5384 return -EINVAL;
5385
5386 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5387 return -EINVAL;
5388
5389out:
5390 return ret;
5391
5392err_size:
5393 put_user(sizeof(*attr), &uattr->size);
5394 ret = -E2BIG;
5395 goto out;
5396}
5397
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005398static int
5399perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005400{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005401 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005402 int ret = -EINVAL;
5403
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005404 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005405 goto set;
5406
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005407 /* don't allow circular references */
5408 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005409 goto out;
5410
Peter Zijlstra0f139302010-05-20 14:35:15 +02005411 /*
5412 * Don't allow cross-cpu buffers
5413 */
5414 if (output_event->cpu != event->cpu)
5415 goto out;
5416
5417 /*
5418 * If its not a per-cpu buffer, it must be the same task.
5419 */
5420 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5421 goto out;
5422
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423set:
5424 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005425 /* Can't redirect output if we've got an active mmap() */
5426 if (atomic_read(&event->mmap_count))
5427 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005429 if (output_event) {
5430 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005431 buffer = perf_buffer_get(output_event);
5432 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005433 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005434 }
5435
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005436 old_buffer = event->buffer;
5437 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005439unlock:
5440 mutex_unlock(&event->mmap_mutex);
5441
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005442 if (old_buffer)
5443 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005444out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 return ret;
5446}
5447
5448/**
5449 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5450 *
5451 * @attr_uptr: event_id type attributes for monitoring/sampling
5452 * @pid: target pid
5453 * @cpu: target cpu
5454 * @group_fd: group leader event fd
5455 */
5456SYSCALL_DEFINE5(perf_event_open,
5457 struct perf_event_attr __user *, attr_uptr,
5458 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5459{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005460 struct perf_event *group_leader = NULL, *output_event = NULL;
5461 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005462 struct perf_event_attr attr;
5463 struct perf_event_context *ctx;
5464 struct file *event_file = NULL;
5465 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07005466 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005467 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005468 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005469 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005470 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005471 int err;
5472
5473 /* for future expandability... */
5474 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5475 return -EINVAL;
5476
5477 err = perf_copy_attr(attr_uptr, &attr);
5478 if (err)
5479 return err;
5480
5481 if (!attr.exclude_kernel) {
5482 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5483 return -EACCES;
5484 }
5485
5486 if (attr.freq) {
5487 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5488 return -EINVAL;
5489 }
5490
Al Viroea635c62010-05-26 17:40:29 -04005491 event_fd = get_unused_fd_flags(O_RDWR);
5492 if (event_fd < 0)
5493 return event_fd;
5494
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005495 if (group_fd != -1) {
5496 group_leader = perf_fget_light(group_fd, &fput_needed);
5497 if (IS_ERR(group_leader)) {
5498 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005499 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005500 }
5501 group_file = group_leader->filp;
5502 if (flags & PERF_FLAG_FD_OUTPUT)
5503 output_event = group_leader;
5504 if (flags & PERF_FLAG_FD_NO_GROUP)
5505 group_leader = NULL;
5506 }
5507
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005508 if (pid != -1) {
5509 task = find_lively_task_by_vpid(pid);
5510 if (IS_ERR(task)) {
5511 err = PTR_ERR(task);
5512 goto err_group_fd;
5513 }
5514 }
5515
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005516 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005517 if (IS_ERR(event)) {
5518 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005519 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005520 }
5521
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005522 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005523 * Special case software events and allow them to be part of
5524 * any hardware group.
5525 */
5526 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005527
5528 if (group_leader &&
5529 (is_software_event(event) != is_software_event(group_leader))) {
5530 if (is_software_event(event)) {
5531 /*
5532 * If event and group_leader are not both a software
5533 * event, and event is, then group leader is not.
5534 *
5535 * Allow the addition of software events to !software
5536 * groups, this is safe because software events never
5537 * fail to schedule.
5538 */
5539 pmu = group_leader->pmu;
5540 } else if (is_software_event(group_leader) &&
5541 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5542 /*
5543 * In case the group is a pure software group, and we
5544 * try to add a hardware event, move the whole group to
5545 * the hardware context.
5546 */
5547 move_group = 1;
5548 }
5549 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005550
5551 /*
5552 * Get the target context (task or percpu):
5553 */
Matt Helsley38a81da2010-09-13 13:01:20 -07005554 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005555 if (IS_ERR(ctx)) {
5556 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005557 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005558 }
5559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005560 /*
5561 * Look up the group leader (we will attach this event to it):
5562 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005563 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005564 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005566 /*
5567 * Do not allow a recursive hierarchy (this new sibling
5568 * becoming part of another group-sibling):
5569 */
5570 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005571 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005572 /*
5573 * Do not allow to attach to a group in a different
5574 * task or CPU context:
5575 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005576 if (move_group) {
5577 if (group_leader->ctx->type != ctx->type)
5578 goto err_context;
5579 } else {
5580 if (group_leader->ctx != ctx)
5581 goto err_context;
5582 }
5583
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584 /*
5585 * Only a group leader can be exclusive or pinned
5586 */
5587 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005588 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005589 }
5590
5591 if (output_event) {
5592 err = perf_event_set_output(event, output_event);
5593 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005594 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005595 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005596
Al Viroea635c62010-05-26 17:40:29 -04005597 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5598 if (IS_ERR(event_file)) {
5599 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005600 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005601 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005602
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005603 if (move_group) {
5604 struct perf_event_context *gctx = group_leader->ctx;
5605
5606 mutex_lock(&gctx->mutex);
5607 perf_event_remove_from_context(group_leader);
5608 list_for_each_entry(sibling, &group_leader->sibling_list,
5609 group_entry) {
5610 perf_event_remove_from_context(sibling);
5611 put_ctx(gctx);
5612 }
5613 mutex_unlock(&gctx->mutex);
5614 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005615 }
5616
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005617 event->filp = event_file;
5618 WARN_ON_ONCE(ctx->parent_ctx);
5619 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005620
5621 if (move_group) {
5622 perf_install_in_context(ctx, group_leader, cpu);
5623 get_ctx(ctx);
5624 list_for_each_entry(sibling, &group_leader->sibling_list,
5625 group_entry) {
5626 perf_install_in_context(ctx, sibling, cpu);
5627 get_ctx(ctx);
5628 }
5629 }
5630
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005631 perf_install_in_context(ctx, event, cpu);
5632 ++ctx->generation;
5633 mutex_unlock(&ctx->mutex);
5634
5635 event->owner = current;
5636 get_task_struct(current);
5637 mutex_lock(&current->perf_event_mutex);
5638 list_add_tail(&event->owner_entry, &current->perf_event_list);
5639 mutex_unlock(&current->perf_event_mutex);
5640
Peter Zijlstra8a495422010-05-27 15:47:49 +02005641 /*
5642 * Drop the reference on the group_event after placing the
5643 * new event on the sibling_list. This ensures destruction
5644 * of the group leader will find the pointer to itself in
5645 * perf_group_detach().
5646 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005648 fd_install(event_fd, event_file);
5649 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005650
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005651err_context:
Al Viroea635c62010-05-26 17:40:29 -04005652 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005653err_alloc:
5654 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02005655err_task:
5656 if (task)
5657 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005658err_group_fd:
5659 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005660err_fd:
5661 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005662 return err;
5663}
5664
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005665/**
5666 * perf_event_create_kernel_counter
5667 *
5668 * @attr: attributes of the counter to create
5669 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07005670 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005671 */
5672struct perf_event *
5673perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07005674 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005675 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005676{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005677 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005678 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005679 int err;
5680
5681 /*
5682 * Get the target context (task or percpu):
5683 */
5684
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005685 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005686 if (IS_ERR(event)) {
5687 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005688 goto err;
5689 }
5690
Matt Helsley38a81da2010-09-13 13:01:20 -07005691 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005692 if (IS_ERR(ctx)) {
5693 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005694 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005695 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005696
5697 event->filp = NULL;
5698 WARN_ON_ONCE(ctx->parent_ctx);
5699 mutex_lock(&ctx->mutex);
5700 perf_install_in_context(ctx, event, cpu);
5701 ++ctx->generation;
5702 mutex_unlock(&ctx->mutex);
5703
5704 event->owner = current;
5705 get_task_struct(current);
5706 mutex_lock(&current->perf_event_mutex);
5707 list_add_tail(&event->owner_entry, &current->perf_event_list);
5708 mutex_unlock(&current->perf_event_mutex);
5709
5710 return event;
5711
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005712err_free:
5713 free_event(event);
5714err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005715 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005716}
5717EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5718
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005719static void sync_child_event(struct perf_event *child_event,
5720 struct task_struct *child)
5721{
5722 struct perf_event *parent_event = child_event->parent;
5723 u64 child_val;
5724
5725 if (child_event->attr.inherit_stat)
5726 perf_event_read_event(child_event, child);
5727
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005728 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005729
5730 /*
5731 * Add back the child's count to the parent's count:
5732 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005733 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005734 atomic64_add(child_event->total_time_enabled,
5735 &parent_event->child_total_time_enabled);
5736 atomic64_add(child_event->total_time_running,
5737 &parent_event->child_total_time_running);
5738
5739 /*
5740 * Remove this event from the parent's list
5741 */
5742 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5743 mutex_lock(&parent_event->child_mutex);
5744 list_del_init(&child_event->child_list);
5745 mutex_unlock(&parent_event->child_mutex);
5746
5747 /*
5748 * Release the parent event, if this was the last
5749 * reference to it.
5750 */
5751 fput(parent_event->filp);
5752}
5753
5754static void
5755__perf_event_exit_task(struct perf_event *child_event,
5756 struct perf_event_context *child_ctx,
5757 struct task_struct *child)
5758{
5759 struct perf_event *parent_event;
5760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005761 perf_event_remove_from_context(child_event);
5762
5763 parent_event = child_event->parent;
5764 /*
5765 * It can happen that parent exits first, and has events
5766 * that are still around due to the child reference. These
5767 * events need to be zapped - but otherwise linger.
5768 */
5769 if (parent_event) {
5770 sync_child_event(child_event, child);
5771 free_event(child_event);
5772 }
5773}
5774
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005775static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005776{
5777 struct perf_event *child_event, *tmp;
5778 struct perf_event_context *child_ctx;
5779 unsigned long flags;
5780
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005781 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005782 perf_event_task(child, NULL, 0);
5783 return;
5784 }
5785
5786 local_irq_save(flags);
5787 /*
5788 * We can't reschedule here because interrupts are disabled,
5789 * and either child is current or it is a task that can't be
5790 * scheduled, so we are now safe from rescheduling changing
5791 * our context.
5792 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005793 child_ctx = child->perf_event_ctxp[ctxn];
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005794 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795
5796 /*
5797 * Take the context lock here so that if find_get_context is
5798 * reading child->perf_event_ctxp, we wait until it has
5799 * incremented the context's refcount before we do put_ctx below.
5800 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005801 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005802 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005803 /*
5804 * If this context is a clone; unclone it so it can't get
5805 * swapped to another process while we're removing all
5806 * the events from it.
5807 */
5808 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005809 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005810 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811
5812 /*
5813 * Report the task dead after unscheduling the events so that we
5814 * won't get any samples after PERF_RECORD_EXIT. We can however still
5815 * get a few PERF_RECORD_READ events.
5816 */
5817 perf_event_task(child, child_ctx, 0);
5818
5819 /*
5820 * We can recurse on the same lock type through:
5821 *
5822 * __perf_event_exit_task()
5823 * sync_child_event()
5824 * fput(parent_event->filp)
5825 * perf_release()
5826 * mutex_lock(&ctx->mutex)
5827 *
5828 * But since its the parent context it won't be the same instance.
5829 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005830 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005831
5832again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005833 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5834 group_entry)
5835 __perf_event_exit_task(child_event, child_ctx, child);
5836
5837 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005838 group_entry)
5839 __perf_event_exit_task(child_event, child_ctx, child);
5840
5841 /*
5842 * If the last event was a group event, it will have appended all
5843 * its siblings to the list, but we obtained 'tmp' before that which
5844 * will still point to the list head terminating the iteration.
5845 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005846 if (!list_empty(&child_ctx->pinned_groups) ||
5847 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005848 goto again;
5849
5850 mutex_unlock(&child_ctx->mutex);
5851
5852 put_ctx(child_ctx);
5853}
5854
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005855/*
5856 * When a child task exits, feed back event values to parent events.
5857 */
5858void perf_event_exit_task(struct task_struct *child)
5859{
5860 int ctxn;
5861
5862 for_each_task_context_nr(ctxn)
5863 perf_event_exit_task_context(child, ctxn);
5864}
5865
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005866static void perf_free_event(struct perf_event *event,
5867 struct perf_event_context *ctx)
5868{
5869 struct perf_event *parent = event->parent;
5870
5871 if (WARN_ON_ONCE(!parent))
5872 return;
5873
5874 mutex_lock(&parent->child_mutex);
5875 list_del_init(&event->child_list);
5876 mutex_unlock(&parent->child_mutex);
5877
5878 fput(parent->filp);
5879
Peter Zijlstra8a495422010-05-27 15:47:49 +02005880 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005881 list_del_event(event, ctx);
5882 free_event(event);
5883}
5884
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005885/*
5886 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005887 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005888 */
5889void perf_event_free_task(struct task_struct *task)
5890{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005891 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005892 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005893 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005894
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005895 for_each_task_context_nr(ctxn) {
5896 ctx = task->perf_event_ctxp[ctxn];
5897 if (!ctx)
5898 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005899
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005900 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005902 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5903 group_entry)
5904 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005905
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005906 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5907 group_entry)
5908 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005909
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005910 if (!list_empty(&ctx->pinned_groups) ||
5911 !list_empty(&ctx->flexible_groups))
5912 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005913
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005914 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005915
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005916 put_ctx(ctx);
5917 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005918}
5919
Peter Zijlstra4e231c72010-09-09 21:01:59 +02005920void perf_event_delayed_put(struct task_struct *task)
5921{
5922 int ctxn;
5923
5924 for_each_task_context_nr(ctxn)
5925 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
5926}
5927
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005928/*
5929 * inherit a event from parent task to child task:
5930 */
5931static struct perf_event *
5932inherit_event(struct perf_event *parent_event,
5933 struct task_struct *parent,
5934 struct perf_event_context *parent_ctx,
5935 struct task_struct *child,
5936 struct perf_event *group_leader,
5937 struct perf_event_context *child_ctx)
5938{
5939 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02005940 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005941
5942 /*
5943 * Instead of creating recursive hierarchies of events,
5944 * we link inherited events back to the original parent,
5945 * which has a filp for sure, which we use as the reference
5946 * count:
5947 */
5948 if (parent_event->parent)
5949 parent_event = parent_event->parent;
5950
5951 child_event = perf_event_alloc(&parent_event->attr,
5952 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005953 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005954 group_leader, parent_event,
5955 NULL);
5956 if (IS_ERR(child_event))
5957 return child_event;
5958 get_ctx(child_ctx);
5959
5960 /*
5961 * Make the child state follow the state of the parent event,
5962 * not its attr.disabled bit. We hold the parent's mutex,
5963 * so we won't race with perf_event_{en, dis}able_family.
5964 */
5965 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5966 child_event->state = PERF_EVENT_STATE_INACTIVE;
5967 else
5968 child_event->state = PERF_EVENT_STATE_OFF;
5969
5970 if (parent_event->attr.freq) {
5971 u64 sample_period = parent_event->hw.sample_period;
5972 struct hw_perf_event *hwc = &child_event->hw;
5973
5974 hwc->sample_period = sample_period;
5975 hwc->last_period = sample_period;
5976
5977 local64_set(&hwc->period_left, sample_period);
5978 }
5979
5980 child_event->ctx = child_ctx;
5981 child_event->overflow_handler = parent_event->overflow_handler;
5982
5983 /*
5984 * Link it up in the child's context:
5985 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02005986 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005987 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02005988 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02005989
5990 /*
5991 * Get a reference to the parent filp - we will fput it
5992 * when the child event exits. This is safe to do because
5993 * we are in the parent and we know that the filp still
5994 * exists and has a nonzero count:
5995 */
5996 atomic_long_inc(&parent_event->filp->f_count);
5997
5998 /*
5999 * Link this into the parent event's child list
6000 */
6001 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6002 mutex_lock(&parent_event->child_mutex);
6003 list_add_tail(&child_event->child_list, &parent_event->child_list);
6004 mutex_unlock(&parent_event->child_mutex);
6005
6006 return child_event;
6007}
6008
6009static int inherit_group(struct perf_event *parent_event,
6010 struct task_struct *parent,
6011 struct perf_event_context *parent_ctx,
6012 struct task_struct *child,
6013 struct perf_event_context *child_ctx)
6014{
6015 struct perf_event *leader;
6016 struct perf_event *sub;
6017 struct perf_event *child_ctr;
6018
6019 leader = inherit_event(parent_event, parent, parent_ctx,
6020 child, NULL, child_ctx);
6021 if (IS_ERR(leader))
6022 return PTR_ERR(leader);
6023 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6024 child_ctr = inherit_event(sub, parent, parent_ctx,
6025 child, leader, child_ctx);
6026 if (IS_ERR(child_ctr))
6027 return PTR_ERR(child_ctr);
6028 }
6029 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006030}
6031
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006032static int
6033inherit_task_group(struct perf_event *event, struct task_struct *parent,
6034 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006035 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006036 int *inherited_all)
6037{
6038 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006039 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006040
6041 if (!event->attr.inherit) {
6042 *inherited_all = 0;
6043 return 0;
6044 }
6045
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006046 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006047 if (!child_ctx) {
6048 /*
6049 * This is executed from the parent task context, so
6050 * inherit events that have been marked for cloning.
6051 * First allocate and initialize a context for the
6052 * child.
6053 */
6054
Peter Zijlstraeb184472010-09-07 15:55:13 +02006055 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006056 if (!child_ctx)
6057 return -ENOMEM;
6058
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006059 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006060 }
6061
6062 ret = inherit_group(event, parent, parent_ctx,
6063 child, child_ctx);
6064
6065 if (ret)
6066 *inherited_all = 0;
6067
6068 return ret;
6069}
6070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006071/*
6072 * Initialize the perf_event context in task_struct
6073 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006074int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006075{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006076 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006077 struct perf_event_context *cloned_ctx;
6078 struct perf_event *event;
6079 struct task_struct *parent = current;
6080 int inherited_all = 1;
6081 int ret = 0;
6082
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006083 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006084
6085 mutex_init(&child->perf_event_mutex);
6086 INIT_LIST_HEAD(&child->perf_event_list);
6087
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006088 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089 return 0;
6090
6091 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006092 * If the parent's context is a clone, pin it so it won't get
6093 * swapped under us.
6094 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006095 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006096
6097 /*
6098 * No need to check if parent_ctx != NULL here; since we saw
6099 * it non-NULL earlier, the only reason for it to become NULL
6100 * is if we exit, and since we're currently in the middle of
6101 * a fork we can't be exiting at the same time.
6102 */
6103
6104 /*
6105 * Lock the parent list. No need to lock the child - not PID
6106 * hashed yet and not running, so nobody can access it.
6107 */
6108 mutex_lock(&parent_ctx->mutex);
6109
6110 /*
6111 * We dont have to disable NMIs - we are only looking at
6112 * the list, not manipulating it:
6113 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006114 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006115 ret = inherit_task_group(event, parent, parent_ctx,
6116 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006117 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006119 }
6120
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006121 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006122 ret = inherit_task_group(event, parent, parent_ctx,
6123 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006124 if (ret)
6125 break;
6126 }
6127
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006128 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006129
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006130 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006131 /*
6132 * Mark the child context as a clone of the parent
6133 * context, or of whatever the parent is a clone of.
6134 * Note that if the parent is a clone, it could get
6135 * uncloned at any point, but that doesn't matter
6136 * because the list of events and the generation
6137 * count can't have changed since we took the mutex.
6138 */
6139 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6140 if (cloned_ctx) {
6141 child_ctx->parent_ctx = cloned_ctx;
6142 child_ctx->parent_gen = parent_ctx->parent_gen;
6143 } else {
6144 child_ctx->parent_ctx = parent_ctx;
6145 child_ctx->parent_gen = parent_ctx->generation;
6146 }
6147 get_ctx(child_ctx->parent_ctx);
6148 }
6149
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006150 mutex_unlock(&parent_ctx->mutex);
6151
6152 perf_unpin_context(parent_ctx);
6153
6154 return ret;
6155}
6156
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006157/*
6158 * Initialize the perf_event context in task_struct
6159 */
6160int perf_event_init_task(struct task_struct *child)
6161{
6162 int ctxn, ret;
6163
6164 for_each_task_context_nr(ctxn) {
6165 ret = perf_event_init_context(child, ctxn);
6166 if (ret)
6167 return ret;
6168 }
6169
6170 return 0;
6171}
6172
Paul Mackerras220b1402010-03-10 20:45:52 +11006173static void __init perf_event_init_all_cpus(void)
6174{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006175 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006176 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006177
6178 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006179 swhash = &per_cpu(swevent_htable, cpu);
6180 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006181 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006182 }
6183}
6184
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006185static void __cpuinit perf_event_init_cpu(int cpu)
6186{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006187 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006188
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006189 mutex_lock(&swhash->hlist_mutex);
6190 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006191 struct swevent_hlist *hlist;
6192
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006193 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6194 WARN_ON(!hlist);
6195 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006196 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006197 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006198}
6199
6200#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006201static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006202{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006203 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6204
6205 WARN_ON(!irqs_disabled());
6206
6207 list_del_init(&cpuctx->rotation_list);
6208}
6209
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006210static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006211{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006212 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006213 struct perf_event *event, *tmp;
6214
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006215 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006216
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006217 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6218 __perf_event_remove_from_context(event);
6219 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006220 __perf_event_remove_from_context(event);
6221}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006222
6223static void perf_event_exit_cpu_context(int cpu)
6224{
6225 struct perf_event_context *ctx;
6226 struct pmu *pmu;
6227 int idx;
6228
6229 idx = srcu_read_lock(&pmus_srcu);
6230 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006231 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006232
6233 mutex_lock(&ctx->mutex);
6234 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6235 mutex_unlock(&ctx->mutex);
6236 }
6237 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006238}
6239
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006240static void perf_event_exit_cpu(int cpu)
6241{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006242 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006243
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006244 mutex_lock(&swhash->hlist_mutex);
6245 swevent_hlist_release(swhash);
6246 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006247
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006248 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006249}
6250#else
6251static inline void perf_event_exit_cpu(int cpu) { }
6252#endif
6253
6254static int __cpuinit
6255perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6256{
6257 unsigned int cpu = (long)hcpu;
6258
Peter Zijlstra5e116372010-06-11 13:35:08 +02006259 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006260
6261 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006262 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006263 perf_event_init_cpu(cpu);
6264 break;
6265
Peter Zijlstra5e116372010-06-11 13:35:08 +02006266 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006267 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006268 perf_event_exit_cpu(cpu);
6269 break;
6270
6271 default:
6272 break;
6273 }
6274
6275 return NOTIFY_OK;
6276}
6277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006278void __init perf_event_init(void)
6279{
Paul Mackerras220b1402010-03-10 20:45:52 +11006280 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006281 init_srcu_struct(&pmus_srcu);
6282 perf_pmu_register(&perf_swevent);
6283 perf_pmu_register(&perf_cpu_clock);
6284 perf_pmu_register(&perf_task_clock);
6285 perf_tp_register();
6286 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006287}