blob: e7eeba1794fddac685f5c80a29e9675df17f5511 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
8 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
16#include <linux/file.h>
17#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020019#include <linux/hash.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020020#include <linux/sysfs.h>
21#include <linux/dcache.h>
22#include <linux/percpu.h>
23#include <linux/ptrace.h>
24#include <linux/vmstat.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020025#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/hardirq.h>
27#include <linux/rculist.h>
28#include <linux/uaccess.h>
29#include <linux/syscalls.h>
30#include <linux/anon_inodes.h>
31#include <linux/kernel_stat.h>
32#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080033#include <linux/ftrace_event.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020034
35#include <asm/irq_regs.h>
36
Ingo Molnarcdd6c482009-09-21 12:02:48 +020037static atomic_t nr_events __read_mostly;
38static atomic_t nr_mmap_events __read_mostly;
39static atomic_t nr_comm_events __read_mostly;
40static atomic_t nr_task_events __read_mostly;
41
Peter Zijlstra108b02c2010-09-06 14:32:03 +020042static LIST_HEAD(pmus);
43static DEFINE_MUTEX(pmus_lock);
44static struct srcu_struct pmus_srcu;
45
Ingo Molnarcdd6c482009-09-21 12:02:48 +020046/*
47 * perf event paranoia level:
48 * -1 - not paranoid at all
49 * 0 - disallow raw tracepoint access for unpriv
50 * 1 - disallow cpu events for unpriv
51 * 2 - disallow kernel profiling for unpriv
52 */
53int sysctl_perf_event_paranoid __read_mostly = 1;
54
Ingo Molnarcdd6c482009-09-21 12:02:48 +020055int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
56
57/*
58 * max perf event sample rate
59 */
60int sysctl_perf_event_sample_rate __read_mostly = 100000;
61
62static atomic64_t perf_event_id;
63
Ingo Molnarcdd6c482009-09-21 12:02:48 +020064void __weak perf_event_print_debug(void) { }
65
Matt Fleming84c79912010-10-03 21:41:13 +010066extern __weak const char *perf_pmu_name(void)
67{
68 return "pmu";
69}
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
318 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
319 event->attach_state |= PERF_ATTACH_GROUP;
320
321 if (group_leader == event)
322 return;
323
324 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
325 !is_software_event(event))
326 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
327
328 list_add_tail(&event->group_entry, &group_leader->sibling_list);
329 group_leader->nr_siblings++;
330}
331
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200332/*
333 * Remove a event from the lists for its context.
334 * Must be called with ctx->mutex and ctx->lock held.
335 */
336static void
337list_del_event(struct perf_event *event, struct perf_event_context *ctx)
338{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200339 /*
340 * We can have double detach due to exit/hot-unplug + close.
341 */
342 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200343 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200344
345 event->attach_state &= ~PERF_ATTACH_CONTEXT;
346
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200347 ctx->nr_events--;
348 if (event->attr.inherit_stat)
349 ctx->nr_stat--;
350
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200351 list_del_rcu(&event->event_entry);
352
Peter Zijlstra8a495422010-05-27 15:47:49 +0200353 if (event->group_leader == event)
354 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200355
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200356 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800357
358 /*
359 * If event was in error state, then keep it
360 * that way, otherwise bogus counts will be
361 * returned on read(). The only way to get out
362 * of error state is by explicit re-enabling
363 * of the event
364 */
365 if (event->state > PERF_EVENT_STATE_OFF)
366 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200367}
368
Peter Zijlstra8a495422010-05-27 15:47:49 +0200369static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200370{
371 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200372 struct list_head *list = NULL;
373
374 /*
375 * We can have double detach due to exit/hot-unplug + close.
376 */
377 if (!(event->attach_state & PERF_ATTACH_GROUP))
378 return;
379
380 event->attach_state &= ~PERF_ATTACH_GROUP;
381
382 /*
383 * If this is a sibling, remove it from its group.
384 */
385 if (event->group_leader != event) {
386 list_del_init(&event->group_entry);
387 event->group_leader->nr_siblings--;
388 return;
389 }
390
391 if (!list_empty(&event->group_entry))
392 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100393
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200394 /*
395 * If this was a group event with sibling events then
396 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200397 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200398 */
399 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200400 if (list)
401 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100403
404 /* Inherit group flags from the previous leader */
405 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200406 }
407}
408
Stephane Eranianfa66f072010-08-26 16:40:01 +0200409static inline int
410event_filter_match(struct perf_event *event)
411{
412 return event->cpu == -1 || event->cpu == smp_processor_id();
413}
414
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200415static void
416event_sched_out(struct perf_event *event,
417 struct perf_cpu_context *cpuctx,
418 struct perf_event_context *ctx)
419{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200420 u64 delta;
421 /*
422 * An event which could not be activated because of
423 * filter mismatch still needs to have its timings
424 * maintained, otherwise bogus information is return
425 * via read() for time_enabled, time_running:
426 */
427 if (event->state == PERF_EVENT_STATE_INACTIVE
428 && !event_filter_match(event)) {
429 delta = ctx->time - event->tstamp_stopped;
430 event->tstamp_running += delta;
431 event->tstamp_stopped = ctx->time;
432 }
433
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200434 if (event->state != PERF_EVENT_STATE_ACTIVE)
435 return;
436
437 event->state = PERF_EVENT_STATE_INACTIVE;
438 if (event->pending_disable) {
439 event->pending_disable = 0;
440 event->state = PERF_EVENT_STATE_OFF;
441 }
442 event->tstamp_stopped = ctx->time;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200443 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200444 event->oncpu = -1;
445
446 if (!is_software_event(event))
447 cpuctx->active_oncpu--;
448 ctx->nr_active--;
449 if (event->attr.exclusive || !cpuctx->active_oncpu)
450 cpuctx->exclusive = 0;
451}
452
453static void
454group_sched_out(struct perf_event *group_event,
455 struct perf_cpu_context *cpuctx,
456 struct perf_event_context *ctx)
457{
458 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200459 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200460
461 event_sched_out(group_event, cpuctx, ctx);
462
463 /*
464 * Schedule out siblings (if any):
465 */
466 list_for_each_entry(event, &group_event->sibling_list, group_entry)
467 event_sched_out(event, cpuctx, ctx);
468
Stephane Eranianfa66f072010-08-26 16:40:01 +0200469 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200470 cpuctx->exclusive = 0;
471}
472
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200473static inline struct perf_cpu_context *
474__get_cpu_context(struct perf_event_context *ctx)
475{
476 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
477}
478
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200479/*
480 * Cross CPU call to remove a performance event
481 *
482 * We disable the event on the hardware level first. After that we
483 * remove it from the context list.
484 */
485static void __perf_event_remove_from_context(void *info)
486{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200487 struct perf_event *event = info;
488 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200489 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200490
491 /*
492 * If this is a task context, we need to check whether it is
493 * the current task context of this cpu. If not it has been
494 * scheduled out before the smp call arrived.
495 */
496 if (ctx->task && cpuctx->task_ctx != ctx)
497 return;
498
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100499 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200500
501 event_sched_out(event, cpuctx, ctx);
502
503 list_del_event(event, ctx);
504
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100505 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200506}
507
508
509/*
510 * Remove the event from a task's (or a CPU's) list of events.
511 *
512 * Must be called with ctx->mutex held.
513 *
514 * CPU events are removed with a smp call. For task events we only
515 * call when the task is on a CPU.
516 *
517 * If event->ctx is a cloned context, callers must make sure that
518 * every task struct that event->ctx->task could possibly point to
519 * remains valid. This is OK when called from perf_release since
520 * that only calls us on the top-level context, which can't be a clone.
521 * When called from perf_event_exit_task, it's OK because the
522 * context has been detached from its task.
523 */
524static void perf_event_remove_from_context(struct perf_event *event)
525{
526 struct perf_event_context *ctx = event->ctx;
527 struct task_struct *task = ctx->task;
528
529 if (!task) {
530 /*
531 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200532 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200533 */
534 smp_call_function_single(event->cpu,
535 __perf_event_remove_from_context,
536 event, 1);
537 return;
538 }
539
540retry:
541 task_oncpu_function_call(task, __perf_event_remove_from_context,
542 event);
543
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100544 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200545 /*
546 * If the context is active we need to retry the smp call.
547 */
548 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100549 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200550 goto retry;
551 }
552
553 /*
554 * The lock prevents that this context is scheduled in so we
555 * can remove the event safely, if the call above did not
556 * succeed.
557 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100558 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200559 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100560 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200561}
562
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200563/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200564 * Cross CPU call to disable a performance event
565 */
566static void __perf_event_disable(void *info)
567{
568 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200569 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200570 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200571
572 /*
573 * If this is a per-task event, need to check whether this
574 * event's task is the current task on this cpu.
575 */
576 if (ctx->task && cpuctx->task_ctx != ctx)
577 return;
578
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100579 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200580
581 /*
582 * If the event is on, turn it off.
583 * If it is in error state, leave it in error state.
584 */
585 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
586 update_context_time(ctx);
587 update_group_times(event);
588 if (event == event->group_leader)
589 group_sched_out(event, cpuctx, ctx);
590 else
591 event_sched_out(event, cpuctx, ctx);
592 event->state = PERF_EVENT_STATE_OFF;
593 }
594
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100595 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200596}
597
598/*
599 * Disable a event.
600 *
601 * If event->ctx is a cloned context, callers must make sure that
602 * every task struct that event->ctx->task could possibly point to
603 * remains valid. This condition is satisifed when called through
604 * perf_event_for_each_child or perf_event_for_each because they
605 * hold the top-level event's child_mutex, so any descendant that
606 * goes to exit will block in sync_child_event.
607 * When called from perf_pending_event it's OK because event->ctx
608 * is the current context on this CPU and preemption is disabled,
609 * hence we can't get into perf_event_task_sched_out for this context.
610 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100611void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200612{
613 struct perf_event_context *ctx = event->ctx;
614 struct task_struct *task = ctx->task;
615
616 if (!task) {
617 /*
618 * Disable the event on the cpu that it's on
619 */
620 smp_call_function_single(event->cpu, __perf_event_disable,
621 event, 1);
622 return;
623 }
624
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200625retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200626 task_oncpu_function_call(task, __perf_event_disable, event);
627
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100628 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200629 /*
630 * If the event is still active, we need to retry the cross-call.
631 */
632 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100633 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200634 goto retry;
635 }
636
637 /*
638 * Since we have the lock this context can't be scheduled
639 * in, so we can change the state safely.
640 */
641 if (event->state == PERF_EVENT_STATE_INACTIVE) {
642 update_group_times(event);
643 event->state = PERF_EVENT_STATE_OFF;
644 }
645
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100646 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200647}
648
649static int
650event_sched_in(struct perf_event *event,
651 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100652 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200653{
654 if (event->state <= PERF_EVENT_STATE_OFF)
655 return 0;
656
657 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100658 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200659 /*
660 * The new state must be visible before we turn it on in the hardware:
661 */
662 smp_wmb();
663
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200664 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200665 event->state = PERF_EVENT_STATE_INACTIVE;
666 event->oncpu = -1;
667 return -EAGAIN;
668 }
669
670 event->tstamp_running += ctx->time - event->tstamp_stopped;
671
672 if (!is_software_event(event))
673 cpuctx->active_oncpu++;
674 ctx->nr_active++;
675
676 if (event->attr.exclusive)
677 cpuctx->exclusive = 1;
678
679 return 0;
680}
681
682static int
683group_sched_in(struct perf_event *group_event,
684 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100685 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200686{
Lin Ming6bde9b62010-04-23 13:56:00 +0800687 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200688 struct pmu *pmu = group_event->pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200689
690 if (group_event->state == PERF_EVENT_STATE_OFF)
691 return 0;
692
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200693 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200694
Stephane Eranian90151c32010-05-25 16:23:10 +0200695 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200696 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200698 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200699
700 /*
701 * Schedule in siblings as one group (if any):
702 */
703 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100704 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200705 partial_group = event;
706 goto group_error;
707 }
708 }
709
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200710 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000711 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800712
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200713group_error:
714 /*
715 * Groups can be scheduled in as one unit only, so undo any
716 * partial group before returning:
717 */
718 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
719 if (event == partial_group)
720 break;
721 event_sched_out(event, cpuctx, ctx);
722 }
723 event_sched_out(group_event, cpuctx, ctx);
724
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200725 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200726
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200727 return -EAGAIN;
728}
729
730/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200731 * Work out whether we can put this event group on the CPU now.
732 */
733static int group_can_go_on(struct perf_event *event,
734 struct perf_cpu_context *cpuctx,
735 int can_add_hw)
736{
737 /*
738 * Groups consisting entirely of software events can always go on.
739 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100740 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200741 return 1;
742 /*
743 * If an exclusive group is already on, no other hardware
744 * events can go on.
745 */
746 if (cpuctx->exclusive)
747 return 0;
748 /*
749 * If this group is exclusive and there are already
750 * events on the CPU, it can't go on.
751 */
752 if (event->attr.exclusive && cpuctx->active_oncpu)
753 return 0;
754 /*
755 * Otherwise, try to add it if all previous groups were able
756 * to go on.
757 */
758 return can_add_hw;
759}
760
761static void add_event_to_ctx(struct perf_event *event,
762 struct perf_event_context *ctx)
763{
764 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200765 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200766 event->tstamp_enabled = ctx->time;
767 event->tstamp_running = ctx->time;
768 event->tstamp_stopped = ctx->time;
769}
770
771/*
772 * Cross CPU call to install and enable a performance event
773 *
774 * Must be called with ctx->mutex held
775 */
776static void __perf_install_in_context(void *info)
777{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200778 struct perf_event *event = info;
779 struct perf_event_context *ctx = event->ctx;
780 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200781 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200782 int err;
783
784 /*
785 * If this is a task context, we need to check whether it is
786 * the current task context of this cpu. If not it has been
787 * scheduled out before the smp call arrived.
788 * Or possibly this is the right context but it isn't
789 * on this cpu because it had no events.
790 */
791 if (ctx->task && cpuctx->task_ctx != ctx) {
792 if (cpuctx->task_ctx || ctx->task != current)
793 return;
794 cpuctx->task_ctx = ctx;
795 }
796
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100797 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200798 ctx->is_active = 1;
799 update_context_time(ctx);
800
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200801 add_event_to_ctx(event, ctx);
802
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100803 if (event->cpu != -1 && event->cpu != smp_processor_id())
804 goto unlock;
805
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200806 /*
807 * Don't put the event on if it is disabled or if
808 * it is in a group and the group isn't on.
809 */
810 if (event->state != PERF_EVENT_STATE_INACTIVE ||
811 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
812 goto unlock;
813
814 /*
815 * An exclusive event can't go on if there are already active
816 * hardware events, and no hardware event can go on if there
817 * is already an exclusive event on.
818 */
819 if (!group_can_go_on(event, cpuctx, 1))
820 err = -EEXIST;
821 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100822 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200823
824 if (err) {
825 /*
826 * This event couldn't go on. If it is in a group
827 * then we have to pull the whole group off.
828 * If the event group is pinned then put it in error state.
829 */
830 if (leader != event)
831 group_sched_out(leader, cpuctx, ctx);
832 if (leader->attr.pinned) {
833 update_group_times(leader);
834 leader->state = PERF_EVENT_STATE_ERROR;
835 }
836 }
837
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200838unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100839 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200840}
841
842/*
843 * Attach a performance event to a context
844 *
845 * First we add the event to the list with the hardware enable bit
846 * in event->hw_config cleared.
847 *
848 * If the event is attached to a task which is on a CPU we use a smp
849 * call to enable it in the task context. The task might have been
850 * scheduled away, but we check this in the smp call again.
851 *
852 * Must be called with ctx->mutex held.
853 */
854static void
855perf_install_in_context(struct perf_event_context *ctx,
856 struct perf_event *event,
857 int cpu)
858{
859 struct task_struct *task = ctx->task;
860
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200861 event->ctx = ctx;
862
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863 if (!task) {
864 /*
865 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200866 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867 */
868 smp_call_function_single(cpu, __perf_install_in_context,
869 event, 1);
870 return;
871 }
872
873retry:
874 task_oncpu_function_call(task, __perf_install_in_context,
875 event);
876
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100877 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878 /*
879 * we need to retry the smp call.
880 */
881 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100882 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200883 goto retry;
884 }
885
886 /*
887 * The lock prevents that this context is scheduled in so we
888 * can add the event safely, if it the call above did not
889 * succeed.
890 */
891 if (list_empty(&event->group_entry))
892 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100893 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200894}
895
896/*
897 * Put a event into inactive state and update time fields.
898 * Enabling the leader of a group effectively enables all
899 * the group members that aren't explicitly disabled, so we
900 * have to update their ->tstamp_enabled also.
901 * Note: this works for group members as well as group leaders
902 * since the non-leader members' sibling_lists will be empty.
903 */
904static void __perf_event_mark_enabled(struct perf_event *event,
905 struct perf_event_context *ctx)
906{
907 struct perf_event *sub;
908
909 event->state = PERF_EVENT_STATE_INACTIVE;
910 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200911 list_for_each_entry(sub, &event->sibling_list, group_entry) {
912 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200913 sub->tstamp_enabled =
914 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200915 }
916 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917}
918
919/*
920 * Cross CPU call to enable a performance event
921 */
922static void __perf_event_enable(void *info)
923{
924 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200925 struct perf_event_context *ctx = event->ctx;
926 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200927 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200928 int err;
929
930 /*
931 * If this is a per-task event, need to check whether this
932 * event's task is the current task on this cpu.
933 */
934 if (ctx->task && cpuctx->task_ctx != ctx) {
935 if (cpuctx->task_ctx || ctx->task != current)
936 return;
937 cpuctx->task_ctx = ctx;
938 }
939
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100940 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200941 ctx->is_active = 1;
942 update_context_time(ctx);
943
944 if (event->state >= PERF_EVENT_STATE_INACTIVE)
945 goto unlock;
946 __perf_event_mark_enabled(event, ctx);
947
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100948 if (event->cpu != -1 && event->cpu != smp_processor_id())
949 goto unlock;
950
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200951 /*
952 * If the event is in a group and isn't the group leader,
953 * then don't put it on unless the group is on.
954 */
955 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
956 goto unlock;
957
958 if (!group_can_go_on(event, cpuctx, 1)) {
959 err = -EEXIST;
960 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200961 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100962 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200963 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100964 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965 }
966
967 if (err) {
968 /*
969 * If this event can't go on and it's part of a
970 * group, then the whole group has to come off.
971 */
972 if (leader != event)
973 group_sched_out(leader, cpuctx, ctx);
974 if (leader->attr.pinned) {
975 update_group_times(leader);
976 leader->state = PERF_EVENT_STATE_ERROR;
977 }
978 }
979
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200980unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100981 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200982}
983
984/*
985 * Enable a event.
986 *
987 * If event->ctx is a cloned context, callers must make sure that
988 * every task struct that event->ctx->task could possibly point to
989 * remains valid. This condition is satisfied when called through
990 * perf_event_for_each_child or perf_event_for_each as described
991 * for perf_event_disable.
992 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100993void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200994{
995 struct perf_event_context *ctx = event->ctx;
996 struct task_struct *task = ctx->task;
997
998 if (!task) {
999 /*
1000 * Enable the event on the cpu that it's on
1001 */
1002 smp_call_function_single(event->cpu, __perf_event_enable,
1003 event, 1);
1004 return;
1005 }
1006
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001007 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1009 goto out;
1010
1011 /*
1012 * If the event is in error state, clear that first.
1013 * That way, if we see the event in error state below, we
1014 * know that it has gone back into error state, as distinct
1015 * from the task having been scheduled away before the
1016 * cross-call arrived.
1017 */
1018 if (event->state == PERF_EVENT_STATE_ERROR)
1019 event->state = PERF_EVENT_STATE_OFF;
1020
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001021retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001022 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001023 task_oncpu_function_call(task, __perf_event_enable, event);
1024
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001025 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026
1027 /*
1028 * If the context is active and the event is still off,
1029 * we need to retry the cross-call.
1030 */
1031 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1032 goto retry;
1033
1034 /*
1035 * Since we have the lock this context can't be scheduled
1036 * in, so we can change the state safely.
1037 */
1038 if (event->state == PERF_EVENT_STATE_OFF)
1039 __perf_event_mark_enabled(event, ctx);
1040
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001041out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001042 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001043}
1044
1045static int perf_event_refresh(struct perf_event *event, int refresh)
1046{
1047 /*
1048 * not supported on inherited events
1049 */
1050 if (event->attr.inherit)
1051 return -EINVAL;
1052
1053 atomic_add(refresh, &event->event_limit);
1054 perf_event_enable(event);
1055
1056 return 0;
1057}
1058
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001059enum event_type_t {
1060 EVENT_FLEXIBLE = 0x1,
1061 EVENT_PINNED = 0x2,
1062 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1063};
1064
1065static void ctx_sched_out(struct perf_event_context *ctx,
1066 struct perf_cpu_context *cpuctx,
1067 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001068{
1069 struct perf_event *event;
1070
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001071 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001072 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001073 ctx->is_active = 0;
1074 if (likely(!ctx->nr_events))
1075 goto out;
1076 update_context_time(ctx);
1077
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001078 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001079 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001080
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001081 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001082 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1083 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001084 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001085
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001086 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001087 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001088 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001089 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001090out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001091 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001092 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001093}
1094
1095/*
1096 * Test whether two contexts are equivalent, i.e. whether they
1097 * have both been cloned from the same version of the same context
1098 * and they both have the same number of enabled events.
1099 * If the number of enabled events is the same, then the set
1100 * of enabled events should be the same, because these are both
1101 * inherited contexts, therefore we can't access individual events
1102 * in them directly with an fd; we can only enable/disable all
1103 * events via prctl, or enable/disable all events in a family
1104 * via ioctl, which will have the same effect on both contexts.
1105 */
1106static int context_equiv(struct perf_event_context *ctx1,
1107 struct perf_event_context *ctx2)
1108{
1109 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1110 && ctx1->parent_gen == ctx2->parent_gen
1111 && !ctx1->pin_count && !ctx2->pin_count;
1112}
1113
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001114static void __perf_event_sync_stat(struct perf_event *event,
1115 struct perf_event *next_event)
1116{
1117 u64 value;
1118
1119 if (!event->attr.inherit_stat)
1120 return;
1121
1122 /*
1123 * Update the event value, we cannot use perf_event_read()
1124 * because we're in the middle of a context switch and have IRQs
1125 * disabled, which upsets smp_call_function_single(), however
1126 * we know the event must be on the current CPU, therefore we
1127 * don't need to use it.
1128 */
1129 switch (event->state) {
1130 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001131 event->pmu->read(event);
1132 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001133
1134 case PERF_EVENT_STATE_INACTIVE:
1135 update_event_times(event);
1136 break;
1137
1138 default:
1139 break;
1140 }
1141
1142 /*
1143 * In order to keep per-task stats reliable we need to flip the event
1144 * values when we flip the contexts.
1145 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001146 value = local64_read(&next_event->count);
1147 value = local64_xchg(&event->count, value);
1148 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149
1150 swap(event->total_time_enabled, next_event->total_time_enabled);
1151 swap(event->total_time_running, next_event->total_time_running);
1152
1153 /*
1154 * Since we swizzled the values, update the user visible data too.
1155 */
1156 perf_event_update_userpage(event);
1157 perf_event_update_userpage(next_event);
1158}
1159
1160#define list_next_entry(pos, member) \
1161 list_entry(pos->member.next, typeof(*pos), member)
1162
1163static void perf_event_sync_stat(struct perf_event_context *ctx,
1164 struct perf_event_context *next_ctx)
1165{
1166 struct perf_event *event, *next_event;
1167
1168 if (!ctx->nr_stat)
1169 return;
1170
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001171 update_context_time(ctx);
1172
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001173 event = list_first_entry(&ctx->event_list,
1174 struct perf_event, event_entry);
1175
1176 next_event = list_first_entry(&next_ctx->event_list,
1177 struct perf_event, event_entry);
1178
1179 while (&event->event_entry != &ctx->event_list &&
1180 &next_event->event_entry != &next_ctx->event_list) {
1181
1182 __perf_event_sync_stat(event, next_event);
1183
1184 event = list_next_entry(event, event_entry);
1185 next_event = list_next_entry(next_event, event_entry);
1186 }
1187}
1188
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001189void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1190 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001192 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193 struct perf_event_context *next_ctx;
1194 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001195 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001196 int do_switch = 1;
1197
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001198 if (likely(!ctx))
1199 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001201 cpuctx = __get_cpu_context(ctx);
1202 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001203 return;
1204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 rcu_read_lock();
1206 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001207 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208 if (parent && next_ctx &&
1209 rcu_dereference(next_ctx->parent_ctx) == parent) {
1210 /*
1211 * Looks like the two contexts are clones, so we might be
1212 * able to optimize the context switch. We lock both
1213 * contexts and check that they are clones under the
1214 * lock (including re-checking that neither has been
1215 * uncloned in the meantime). It doesn't matter which
1216 * order we take the locks because no other cpu could
1217 * be trying to lock both of these tasks.
1218 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001219 raw_spin_lock(&ctx->lock);
1220 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001221 if (context_equiv(ctx, next_ctx)) {
1222 /*
1223 * XXX do we need a memory barrier of sorts
1224 * wrt to rcu_dereference() of perf_event_ctxp
1225 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001226 task->perf_event_ctxp[ctxn] = next_ctx;
1227 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 ctx->task = next;
1229 next_ctx->task = task;
1230 do_switch = 0;
1231
1232 perf_event_sync_stat(ctx, next_ctx);
1233 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001234 raw_spin_unlock(&next_ctx->lock);
1235 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001236 }
1237 rcu_read_unlock();
1238
1239 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001240 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 cpuctx->task_ctx = NULL;
1242 }
1243}
1244
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001245#define for_each_task_context_nr(ctxn) \
1246 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1247
1248/*
1249 * Called from scheduler to remove the events of the current task,
1250 * with interrupts disabled.
1251 *
1252 * We stop each event and update the event value in event->count.
1253 *
1254 * This does not protect us against NMI, but disable()
1255 * sets the disabled bit in the control field of event _before_
1256 * accessing the event control register. If a NMI hits, then it will
1257 * not restart the event.
1258 */
1259void perf_event_task_sched_out(struct task_struct *task,
1260 struct task_struct *next)
1261{
1262 int ctxn;
1263
1264 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
1265
1266 for_each_task_context_nr(ctxn)
1267 perf_event_context_sched_out(task, ctxn, next);
1268}
1269
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001270static void task_ctx_sched_out(struct perf_event_context *ctx,
1271 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001272{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001273 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274
1275 if (!cpuctx->task_ctx)
1276 return;
1277
1278 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1279 return;
1280
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001281 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001282 cpuctx->task_ctx = NULL;
1283}
1284
1285/*
1286 * Called with IRQs disabled
1287 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001288static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001289{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001290 task_ctx_sched_out(ctx, EVENT_ALL);
1291}
1292
1293/*
1294 * Called with IRQs disabled
1295 */
1296static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1297 enum event_type_t event_type)
1298{
1299 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001300}
1301
1302static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001303ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001304 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001305{
1306 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001307
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001308 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1309 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001310 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001311 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312 continue;
1313
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001314 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001315 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316
1317 /*
1318 * If this pinned group hasn't been scheduled,
1319 * put it in error state.
1320 */
1321 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1322 update_group_times(event);
1323 event->state = PERF_EVENT_STATE_ERROR;
1324 }
1325 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001326}
1327
1328static void
1329ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001330 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001331{
1332 struct perf_event *event;
1333 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001334
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001335 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1336 /* Ignore events in OFF or ERROR state */
1337 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001339 /*
1340 * Listen to the 'cpu' scheduling filter constraint
1341 * of events:
1342 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001343 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001344 continue;
1345
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001346 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001347 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001348 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001349 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001351}
1352
1353static void
1354ctx_sched_in(struct perf_event_context *ctx,
1355 struct perf_cpu_context *cpuctx,
1356 enum event_type_t event_type)
1357{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001358 raw_spin_lock(&ctx->lock);
1359 ctx->is_active = 1;
1360 if (likely(!ctx->nr_events))
1361 goto out;
1362
1363 ctx->timestamp = perf_clock();
1364
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001365 /*
1366 * First go through the list and put on any pinned groups
1367 * in order to give them the best chance of going on.
1368 */
1369 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001370 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001371
1372 /* Then walk through the lower prio flexible groups */
1373 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001374 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001375
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001376out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001377 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001378}
1379
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001380static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1381 enum event_type_t event_type)
1382{
1383 struct perf_event_context *ctx = &cpuctx->ctx;
1384
1385 ctx_sched_in(ctx, cpuctx, event_type);
1386}
1387
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001388static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001389 enum event_type_t event_type)
1390{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001391 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001392
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001393 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001394 if (cpuctx->task_ctx == ctx)
1395 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001396
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001397 ctx_sched_in(ctx, cpuctx, event_type);
1398 cpuctx->task_ctx = ctx;
1399}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001400
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001401void perf_event_context_sched_in(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001402{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001403 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001404
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001405 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001406 if (cpuctx->task_ctx == ctx)
1407 return;
1408
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001409 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001410 /*
1411 * We want to keep the following priority order:
1412 * cpu pinned (that don't need to move), task pinned,
1413 * cpu flexible, task flexible.
1414 */
1415 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1416
1417 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1418 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1419 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1420
1421 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001422
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001423 /*
1424 * Since these rotations are per-cpu, we need to ensure the
1425 * cpu-context we got scheduled on is actually rotating.
1426 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001427 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001428 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001429}
1430
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001431/*
1432 * Called from scheduler to add the events of the current task
1433 * with interrupts disabled.
1434 *
1435 * We restore the event value and then enable it.
1436 *
1437 * This does not protect us against NMI, but enable()
1438 * sets the enabled bit in the control field of event _before_
1439 * accessing the event control register. If a NMI hits, then it will
1440 * keep the event running.
1441 */
1442void perf_event_task_sched_in(struct task_struct *task)
1443{
1444 struct perf_event_context *ctx;
1445 int ctxn;
1446
1447 for_each_task_context_nr(ctxn) {
1448 ctx = task->perf_event_ctxp[ctxn];
1449 if (likely(!ctx))
1450 continue;
1451
1452 perf_event_context_sched_in(ctx);
1453 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001454}
1455
1456#define MAX_INTERRUPTS (~0ULL)
1457
1458static void perf_log_throttle(struct perf_event *event, int enable);
1459
Peter Zijlstraabd50712010-01-26 18:50:16 +01001460static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1461{
1462 u64 frequency = event->attr.sample_freq;
1463 u64 sec = NSEC_PER_SEC;
1464 u64 divisor, dividend;
1465
1466 int count_fls, nsec_fls, frequency_fls, sec_fls;
1467
1468 count_fls = fls64(count);
1469 nsec_fls = fls64(nsec);
1470 frequency_fls = fls64(frequency);
1471 sec_fls = 30;
1472
1473 /*
1474 * We got @count in @nsec, with a target of sample_freq HZ
1475 * the target period becomes:
1476 *
1477 * @count * 10^9
1478 * period = -------------------
1479 * @nsec * sample_freq
1480 *
1481 */
1482
1483 /*
1484 * Reduce accuracy by one bit such that @a and @b converge
1485 * to a similar magnitude.
1486 */
1487#define REDUCE_FLS(a, b) \
1488do { \
1489 if (a##_fls > b##_fls) { \
1490 a >>= 1; \
1491 a##_fls--; \
1492 } else { \
1493 b >>= 1; \
1494 b##_fls--; \
1495 } \
1496} while (0)
1497
1498 /*
1499 * Reduce accuracy until either term fits in a u64, then proceed with
1500 * the other, so that finally we can do a u64/u64 division.
1501 */
1502 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1503 REDUCE_FLS(nsec, frequency);
1504 REDUCE_FLS(sec, count);
1505 }
1506
1507 if (count_fls + sec_fls > 64) {
1508 divisor = nsec * frequency;
1509
1510 while (count_fls + sec_fls > 64) {
1511 REDUCE_FLS(count, sec);
1512 divisor >>= 1;
1513 }
1514
1515 dividend = count * sec;
1516 } else {
1517 dividend = count * sec;
1518
1519 while (nsec_fls + frequency_fls > 64) {
1520 REDUCE_FLS(nsec, frequency);
1521 dividend >>= 1;
1522 }
1523
1524 divisor = nsec * frequency;
1525 }
1526
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001527 if (!divisor)
1528 return dividend;
1529
Peter Zijlstraabd50712010-01-26 18:50:16 +01001530 return div64_u64(dividend, divisor);
1531}
1532
1533static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001534{
1535 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001536 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537 s64 delta;
1538
Peter Zijlstraabd50712010-01-26 18:50:16 +01001539 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001540
1541 delta = (s64)(period - hwc->sample_period);
1542 delta = (delta + 7) / 8; /* low pass filter */
1543
1544 sample_period = hwc->sample_period + delta;
1545
1546 if (!sample_period)
1547 sample_period = 1;
1548
1549 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001550
Peter Zijlstrae7850592010-05-21 14:43:08 +02001551 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001552 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001553 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001554 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001555 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001556}
1557
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001558static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559{
1560 struct perf_event *event;
1561 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001562 u64 interrupts, now;
1563 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001565 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001566 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567 if (event->state != PERF_EVENT_STATE_ACTIVE)
1568 continue;
1569
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001570 if (event->cpu != -1 && event->cpu != smp_processor_id())
1571 continue;
1572
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001573 hwc = &event->hw;
1574
1575 interrupts = hwc->interrupts;
1576 hwc->interrupts = 0;
1577
1578 /*
1579 * unthrottle events on the tick
1580 */
1581 if (interrupts == MAX_INTERRUPTS) {
1582 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001583 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 }
1585
1586 if (!event->attr.freq || !event->attr.sample_freq)
1587 continue;
1588
Peter Zijlstraabd50712010-01-26 18:50:16 +01001589 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001590 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001591 delta = now - hwc->freq_count_stamp;
1592 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593
Peter Zijlstraabd50712010-01-26 18:50:16 +01001594 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001595 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001596 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001597 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001598}
1599
1600/*
1601 * Round-robin a context's events:
1602 */
1603static void rotate_ctx(struct perf_event_context *ctx)
1604{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001605 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001606
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001607 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001608 list_rotate_left(&ctx->flexible_groups);
1609
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001610 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611}
1612
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001613/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001614 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1615 * because they're strictly cpu affine and rotate_start is called with IRQs
1616 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001617 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001618static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001620 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001621 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001622 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001624 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001625 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001626 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1627 rotate = 1;
1628 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001629
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001630 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001631 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001632 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001633 if (ctx->nr_events != ctx->nr_active)
1634 rotate = 1;
1635 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001636
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001637 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001638 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001639 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001640 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001641
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001642 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001643 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001644
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001645 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001646 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001647 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001648
1649 rotate_ctx(&cpuctx->ctx);
1650 if (ctx)
1651 rotate_ctx(ctx);
1652
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001653 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001655 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001656
1657done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001658 if (remove)
1659 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001660
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001661 perf_pmu_enable(cpuctx->ctx.pmu);
1662}
1663
1664void perf_event_task_tick(void)
1665{
1666 struct list_head *head = &__get_cpu_var(rotation_list);
1667 struct perf_cpu_context *cpuctx, *tmp;
1668
1669 WARN_ON(!irqs_disabled());
1670
1671 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1672 if (cpuctx->jiffies_interval == 1 ||
1673 !(jiffies % cpuctx->jiffies_interval))
1674 perf_rotate_context(cpuctx);
1675 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676}
1677
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001678static int event_enable_on_exec(struct perf_event *event,
1679 struct perf_event_context *ctx)
1680{
1681 if (!event->attr.enable_on_exec)
1682 return 0;
1683
1684 event->attr.enable_on_exec = 0;
1685 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1686 return 0;
1687
1688 __perf_event_mark_enabled(event, ctx);
1689
1690 return 1;
1691}
1692
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693/*
1694 * Enable all of a task's events that have been marked enable-on-exec.
1695 * This expects task == current.
1696 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001697static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001698{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 struct perf_event *event;
1700 unsigned long flags;
1701 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001702 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703
1704 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001705 if (!ctx || !ctx->nr_events)
1706 goto out;
1707
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001708 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001710 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001712 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1713 ret = event_enable_on_exec(event, ctx);
1714 if (ret)
1715 enabled = 1;
1716 }
1717
1718 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1719 ret = event_enable_on_exec(event, ctx);
1720 if (ret)
1721 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001722 }
1723
1724 /*
1725 * Unclone this context if we enabled any event.
1726 */
1727 if (enabled)
1728 unclone_ctx(ctx);
1729
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001730 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001732 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001733out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734 local_irq_restore(flags);
1735}
1736
1737/*
1738 * Cross CPU call to read the hardware event
1739 */
1740static void __perf_event_read(void *info)
1741{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001742 struct perf_event *event = info;
1743 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001744 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745
1746 /*
1747 * If this is a task context, we need to check whether it is
1748 * the current task context of this cpu. If not it has been
1749 * scheduled out before the smp call arrived. In that case
1750 * event->count would have been updated to a recent sample
1751 * when the event was scheduled out.
1752 */
1753 if (ctx->task && cpuctx->task_ctx != ctx)
1754 return;
1755
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001756 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001757 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001759 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001760
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001761 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001762}
1763
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001764static inline u64 perf_event_count(struct perf_event *event)
1765{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001766 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001767}
1768
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769static u64 perf_event_read(struct perf_event *event)
1770{
1771 /*
1772 * If event is enabled and currently active on a CPU, update the
1773 * value in the event structure:
1774 */
1775 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1776 smp_call_function_single(event->oncpu,
1777 __perf_event_read, event, 1);
1778 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001779 struct perf_event_context *ctx = event->ctx;
1780 unsigned long flags;
1781
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001782 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02001783 /*
1784 * may read while context is not active
1785 * (e.g., thread is blocked), in that case
1786 * we cannot update context time
1787 */
1788 if (ctx->is_active)
1789 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001790 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001791 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001792 }
1793
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001794 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001795}
1796
1797/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001798 * Callchain support
1799 */
1800
1801struct callchain_cpus_entries {
1802 struct rcu_head rcu_head;
1803 struct perf_callchain_entry *cpu_entries[0];
1804};
1805
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001806static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001807static atomic_t nr_callchain_events;
1808static DEFINE_MUTEX(callchain_mutex);
1809struct callchain_cpus_entries *callchain_cpus_entries;
1810
1811
1812__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1813 struct pt_regs *regs)
1814{
1815}
1816
1817__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1818 struct pt_regs *regs)
1819{
1820}
1821
1822static void release_callchain_buffers_rcu(struct rcu_head *head)
1823{
1824 struct callchain_cpus_entries *entries;
1825 int cpu;
1826
1827 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1828
1829 for_each_possible_cpu(cpu)
1830 kfree(entries->cpu_entries[cpu]);
1831
1832 kfree(entries);
1833}
1834
1835static void release_callchain_buffers(void)
1836{
1837 struct callchain_cpus_entries *entries;
1838
1839 entries = callchain_cpus_entries;
1840 rcu_assign_pointer(callchain_cpus_entries, NULL);
1841 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1842}
1843
1844static int alloc_callchain_buffers(void)
1845{
1846 int cpu;
1847 int size;
1848 struct callchain_cpus_entries *entries;
1849
1850 /*
1851 * We can't use the percpu allocation API for data that can be
1852 * accessed from NMI. Use a temporary manual per cpu allocation
1853 * until that gets sorted out.
1854 */
1855 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1856 num_possible_cpus();
1857
1858 entries = kzalloc(size, GFP_KERNEL);
1859 if (!entries)
1860 return -ENOMEM;
1861
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001862 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001863
1864 for_each_possible_cpu(cpu) {
1865 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1866 cpu_to_node(cpu));
1867 if (!entries->cpu_entries[cpu])
1868 goto fail;
1869 }
1870
1871 rcu_assign_pointer(callchain_cpus_entries, entries);
1872
1873 return 0;
1874
1875fail:
1876 for_each_possible_cpu(cpu)
1877 kfree(entries->cpu_entries[cpu]);
1878 kfree(entries);
1879
1880 return -ENOMEM;
1881}
1882
1883static int get_callchain_buffers(void)
1884{
1885 int err = 0;
1886 int count;
1887
1888 mutex_lock(&callchain_mutex);
1889
1890 count = atomic_inc_return(&nr_callchain_events);
1891 if (WARN_ON_ONCE(count < 1)) {
1892 err = -EINVAL;
1893 goto exit;
1894 }
1895
1896 if (count > 1) {
1897 /* If the allocation failed, give up */
1898 if (!callchain_cpus_entries)
1899 err = -ENOMEM;
1900 goto exit;
1901 }
1902
1903 err = alloc_callchain_buffers();
1904 if (err)
1905 release_callchain_buffers();
1906exit:
1907 mutex_unlock(&callchain_mutex);
1908
1909 return err;
1910}
1911
1912static void put_callchain_buffers(void)
1913{
1914 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1915 release_callchain_buffers();
1916 mutex_unlock(&callchain_mutex);
1917 }
1918}
1919
1920static int get_recursion_context(int *recursion)
1921{
1922 int rctx;
1923
1924 if (in_nmi())
1925 rctx = 3;
1926 else if (in_irq())
1927 rctx = 2;
1928 else if (in_softirq())
1929 rctx = 1;
1930 else
1931 rctx = 0;
1932
1933 if (recursion[rctx])
1934 return -1;
1935
1936 recursion[rctx]++;
1937 barrier();
1938
1939 return rctx;
1940}
1941
1942static inline void put_recursion_context(int *recursion, int rctx)
1943{
1944 barrier();
1945 recursion[rctx]--;
1946}
1947
1948static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1949{
1950 int cpu;
1951 struct callchain_cpus_entries *entries;
1952
1953 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1954 if (*rctx == -1)
1955 return NULL;
1956
1957 entries = rcu_dereference(callchain_cpus_entries);
1958 if (!entries)
1959 return NULL;
1960
1961 cpu = smp_processor_id();
1962
1963 return &entries->cpu_entries[cpu][*rctx];
1964}
1965
1966static void
1967put_callchain_entry(int rctx)
1968{
1969 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1970}
1971
1972static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1973{
1974 int rctx;
1975 struct perf_callchain_entry *entry;
1976
1977
1978 entry = get_callchain_entry(&rctx);
1979 if (rctx == -1)
1980 return NULL;
1981
1982 if (!entry)
1983 goto exit_put;
1984
1985 entry->nr = 0;
1986
1987 if (!user_mode(regs)) {
1988 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1989 perf_callchain_kernel(entry, regs);
1990 if (current->mm)
1991 regs = task_pt_regs(current);
1992 else
1993 regs = NULL;
1994 }
1995
1996 if (regs) {
1997 perf_callchain_store(entry, PERF_CONTEXT_USER);
1998 perf_callchain_user(entry, regs);
1999 }
2000
2001exit_put:
2002 put_callchain_entry(rctx);
2003
2004 return entry;
2005}
2006
2007/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008 * Initialize the perf_event context in a task_struct:
2009 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002010static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002011{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002012 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002013 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002014 INIT_LIST_HEAD(&ctx->pinned_groups);
2015 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016 INIT_LIST_HEAD(&ctx->event_list);
2017 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018}
2019
Peter Zijlstraeb184472010-09-07 15:55:13 +02002020static struct perf_event_context *
2021alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022{
2023 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002024
2025 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2026 if (!ctx)
2027 return NULL;
2028
2029 __perf_event_init_context(ctx);
2030 if (task) {
2031 ctx->task = task;
2032 get_task_struct(task);
2033 }
2034 ctx->pmu = pmu;
2035
2036 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002037}
2038
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002039static struct task_struct *
2040find_lively_task_by_vpid(pid_t vpid)
2041{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002043 int err;
2044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002046 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047 task = current;
2048 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002049 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050 if (task)
2051 get_task_struct(task);
2052 rcu_read_unlock();
2053
2054 if (!task)
2055 return ERR_PTR(-ESRCH);
2056
2057 /*
2058 * Can't attach events to a dying task.
2059 */
2060 err = -ESRCH;
2061 if (task->flags & PF_EXITING)
2062 goto errout;
2063
2064 /* Reuse ptrace permission checks for now. */
2065 err = -EACCES;
2066 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2067 goto errout;
2068
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002069 return task;
2070errout:
2071 put_task_struct(task);
2072 return ERR_PTR(err);
2073
2074}
2075
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002076static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002077find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078{
2079 struct perf_event_context *ctx;
2080 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002081 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002082 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002083
Matt Helsley38a81da2010-09-13 13:01:20 -07002084 if (!task && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085 /* Must be root to operate on a CPU event: */
2086 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2087 return ERR_PTR(-EACCES);
2088
2089 if (cpu < 0 || cpu >= nr_cpumask_bits)
2090 return ERR_PTR(-EINVAL);
2091
2092 /*
2093 * We could be clever and allow to attach a event to an
2094 * offline CPU and activate it when the CPU comes up, but
2095 * that's for later.
2096 */
2097 if (!cpu_online(cpu))
2098 return ERR_PTR(-ENODEV);
2099
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002100 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101 ctx = &cpuctx->ctx;
2102 get_ctx(ctx);
2103
2104 return ctx;
2105 }
2106
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002107 err = -EINVAL;
2108 ctxn = pmu->task_ctx_nr;
2109 if (ctxn < 0)
2110 goto errout;
2111
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002112retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002113 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002114 if (ctx) {
2115 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002116 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117 }
2118
2119 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002120 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121 err = -ENOMEM;
2122 if (!ctx)
2123 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002126
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002127 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002128 /*
2129 * We raced with some other task; use
2130 * the context they set.
2131 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002132 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 kfree(ctx);
2134 goto retry;
2135 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 }
2137
2138 put_task_struct(task);
2139 return ctx;
2140
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002141errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142 put_task_struct(task);
2143 return ERR_PTR(err);
2144}
2145
Li Zefan6fb29152009-10-15 11:21:42 +08002146static void perf_event_free_filter(struct perf_event *event);
2147
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148static void free_event_rcu(struct rcu_head *head)
2149{
2150 struct perf_event *event;
2151
2152 event = container_of(head, struct perf_event, rcu_head);
2153 if (event->ns)
2154 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002155 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002156 kfree(event);
2157}
2158
2159static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002160static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002161
2162static void free_event(struct perf_event *event)
2163{
2164 perf_pending_sync(event);
2165
2166 if (!event->parent) {
2167 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002168 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169 atomic_dec(&nr_mmap_events);
2170 if (event->attr.comm)
2171 atomic_dec(&nr_comm_events);
2172 if (event->attr.task)
2173 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002174 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2175 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176 }
2177
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002178 if (event->buffer) {
2179 perf_buffer_put(event->buffer);
2180 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002181 }
2182
2183 if (event->destroy)
2184 event->destroy(event);
2185
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002186 if (event->ctx)
2187 put_ctx(event->ctx);
2188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189 call_rcu(&event->rcu_head, free_event_rcu);
2190}
2191
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002192int perf_event_release_kernel(struct perf_event *event)
2193{
2194 struct perf_event_context *ctx = event->ctx;
2195
Peter Zijlstra050735b2010-05-11 11:51:53 +02002196 /*
2197 * Remove from the PMU, can't get re-enabled since we got
2198 * here because the last ref went.
2199 */
2200 perf_event_disable(event);
2201
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002202 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002203 /*
2204 * There are two ways this annotation is useful:
2205 *
2206 * 1) there is a lock recursion from perf_event_exit_task
2207 * see the comment there.
2208 *
2209 * 2) there is a lock-inversion with mmap_sem through
2210 * perf_event_read_group(), which takes faults while
2211 * holding ctx->mutex, however this is called after
2212 * the last filedesc died, so there is no possibility
2213 * to trigger the AB-BA case.
2214 */
2215 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002216 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002217 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002218 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002219 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002220 mutex_unlock(&ctx->mutex);
2221
2222 mutex_lock(&event->owner->perf_event_mutex);
2223 list_del_init(&event->owner_entry);
2224 mutex_unlock(&event->owner->perf_event_mutex);
2225 put_task_struct(event->owner);
2226
2227 free_event(event);
2228
2229 return 0;
2230}
2231EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2232
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002233/*
2234 * Called when the last reference to the file is gone.
2235 */
2236static int perf_release(struct inode *inode, struct file *file)
2237{
2238 struct perf_event *event = file->private_data;
2239
2240 file->private_data = NULL;
2241
2242 return perf_event_release_kernel(event);
2243}
2244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245static int perf_event_read_size(struct perf_event *event)
2246{
2247 int entry = sizeof(u64); /* value */
2248 int size = 0;
2249 int nr = 1;
2250
2251 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2252 size += sizeof(u64);
2253
2254 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2255 size += sizeof(u64);
2256
2257 if (event->attr.read_format & PERF_FORMAT_ID)
2258 entry += sizeof(u64);
2259
2260 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2261 nr += event->group_leader->nr_siblings;
2262 size += sizeof(u64);
2263 }
2264
2265 size += entry * nr;
2266
2267 return size;
2268}
2269
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002270u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271{
2272 struct perf_event *child;
2273 u64 total = 0;
2274
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002275 *enabled = 0;
2276 *running = 0;
2277
Peter Zijlstra6f105812009-11-20 22:19:56 +01002278 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002280 *enabled += event->total_time_enabled +
2281 atomic64_read(&event->child_total_time_enabled);
2282 *running += event->total_time_running +
2283 atomic64_read(&event->child_total_time_running);
2284
2285 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002286 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002287 *enabled += child->total_time_enabled;
2288 *running += child->total_time_running;
2289 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002290 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291
2292 return total;
2293}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002294EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002296static int perf_event_read_group(struct perf_event *event,
2297 u64 read_format, char __user *buf)
2298{
2299 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002300 int n = 0, size = 0, ret = -EFAULT;
2301 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002302 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002303 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002304
Peter Zijlstra6f105812009-11-20 22:19:56 +01002305 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002306 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307
2308 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002309 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2310 values[n++] = enabled;
2311 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2312 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002313 values[n++] = count;
2314 if (read_format & PERF_FORMAT_ID)
2315 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316
2317 size = n * sizeof(u64);
2318
2319 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002320 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321
Peter Zijlstra6f105812009-11-20 22:19:56 +01002322 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323
2324 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002325 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002327 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002328 if (read_format & PERF_FORMAT_ID)
2329 values[n++] = primary_event_id(sub);
2330
2331 size = n * sizeof(u64);
2332
Stephane Eranian184d3da2009-11-23 21:40:49 -08002333 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002334 ret = -EFAULT;
2335 goto unlock;
2336 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002337
2338 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002339 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002340unlock:
2341 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002342
Peter Zijlstraabf48682009-11-20 22:19:49 +01002343 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002344}
2345
2346static int perf_event_read_one(struct perf_event *event,
2347 u64 read_format, char __user *buf)
2348{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002349 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002350 u64 values[4];
2351 int n = 0;
2352
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002353 values[n++] = perf_event_read_value(event, &enabled, &running);
2354 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2355 values[n++] = enabled;
2356 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2357 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002358 if (read_format & PERF_FORMAT_ID)
2359 values[n++] = primary_event_id(event);
2360
2361 if (copy_to_user(buf, values, n * sizeof(u64)))
2362 return -EFAULT;
2363
2364 return n * sizeof(u64);
2365}
2366
2367/*
2368 * Read the performance event - simple non blocking version for now
2369 */
2370static ssize_t
2371perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2372{
2373 u64 read_format = event->attr.read_format;
2374 int ret;
2375
2376 /*
2377 * Return end-of-file for a read on a event that is in
2378 * error state (i.e. because it was pinned but it couldn't be
2379 * scheduled on to the CPU at some point).
2380 */
2381 if (event->state == PERF_EVENT_STATE_ERROR)
2382 return 0;
2383
2384 if (count < perf_event_read_size(event))
2385 return -ENOSPC;
2386
2387 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002388 if (read_format & PERF_FORMAT_GROUP)
2389 ret = perf_event_read_group(event, read_format, buf);
2390 else
2391 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392
2393 return ret;
2394}
2395
2396static ssize_t
2397perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2398{
2399 struct perf_event *event = file->private_data;
2400
2401 return perf_read_hw(event, buf, count);
2402}
2403
2404static unsigned int perf_poll(struct file *file, poll_table *wait)
2405{
2406 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002407 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408 unsigned int events = POLL_HUP;
2409
2410 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002411 buffer = rcu_dereference(event->buffer);
2412 if (buffer)
2413 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414 rcu_read_unlock();
2415
2416 poll_wait(file, &event->waitq, wait);
2417
2418 return events;
2419}
2420
2421static void perf_event_reset(struct perf_event *event)
2422{
2423 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002424 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002425 perf_event_update_userpage(event);
2426}
2427
2428/*
2429 * Holding the top-level event's child_mutex means that any
2430 * descendant process that has inherited this event will block
2431 * in sync_child_event if it goes to exit, thus satisfying the
2432 * task existence requirements of perf_event_enable/disable.
2433 */
2434static void perf_event_for_each_child(struct perf_event *event,
2435 void (*func)(struct perf_event *))
2436{
2437 struct perf_event *child;
2438
2439 WARN_ON_ONCE(event->ctx->parent_ctx);
2440 mutex_lock(&event->child_mutex);
2441 func(event);
2442 list_for_each_entry(child, &event->child_list, child_list)
2443 func(child);
2444 mutex_unlock(&event->child_mutex);
2445}
2446
2447static void perf_event_for_each(struct perf_event *event,
2448 void (*func)(struct perf_event *))
2449{
2450 struct perf_event_context *ctx = event->ctx;
2451 struct perf_event *sibling;
2452
2453 WARN_ON_ONCE(ctx->parent_ctx);
2454 mutex_lock(&ctx->mutex);
2455 event = event->group_leader;
2456
2457 perf_event_for_each_child(event, func);
2458 func(event);
2459 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2460 perf_event_for_each_child(event, func);
2461 mutex_unlock(&ctx->mutex);
2462}
2463
2464static int perf_event_period(struct perf_event *event, u64 __user *arg)
2465{
2466 struct perf_event_context *ctx = event->ctx;
2467 unsigned long size;
2468 int ret = 0;
2469 u64 value;
2470
2471 if (!event->attr.sample_period)
2472 return -EINVAL;
2473
2474 size = copy_from_user(&value, arg, sizeof(value));
2475 if (size != sizeof(value))
2476 return -EFAULT;
2477
2478 if (!value)
2479 return -EINVAL;
2480
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002481 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482 if (event->attr.freq) {
2483 if (value > sysctl_perf_event_sample_rate) {
2484 ret = -EINVAL;
2485 goto unlock;
2486 }
2487
2488 event->attr.sample_freq = value;
2489 } else {
2490 event->attr.sample_period = value;
2491 event->hw.sample_period = value;
2492 }
2493unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002494 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002495
2496 return ret;
2497}
2498
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002499static const struct file_operations perf_fops;
2500
2501static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2502{
2503 struct file *file;
2504
2505 file = fget_light(fd, fput_needed);
2506 if (!file)
2507 return ERR_PTR(-EBADF);
2508
2509 if (file->f_op != &perf_fops) {
2510 fput_light(file, *fput_needed);
2511 *fput_needed = 0;
2512 return ERR_PTR(-EBADF);
2513 }
2514
2515 return file->private_data;
2516}
2517
2518static int perf_event_set_output(struct perf_event *event,
2519 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002520static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002521
2522static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2523{
2524 struct perf_event *event = file->private_data;
2525 void (*func)(struct perf_event *);
2526 u32 flags = arg;
2527
2528 switch (cmd) {
2529 case PERF_EVENT_IOC_ENABLE:
2530 func = perf_event_enable;
2531 break;
2532 case PERF_EVENT_IOC_DISABLE:
2533 func = perf_event_disable;
2534 break;
2535 case PERF_EVENT_IOC_RESET:
2536 func = perf_event_reset;
2537 break;
2538
2539 case PERF_EVENT_IOC_REFRESH:
2540 return perf_event_refresh(event, arg);
2541
2542 case PERF_EVENT_IOC_PERIOD:
2543 return perf_event_period(event, (u64 __user *)arg);
2544
2545 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002546 {
2547 struct perf_event *output_event = NULL;
2548 int fput_needed = 0;
2549 int ret;
2550
2551 if (arg != -1) {
2552 output_event = perf_fget_light(arg, &fput_needed);
2553 if (IS_ERR(output_event))
2554 return PTR_ERR(output_event);
2555 }
2556
2557 ret = perf_event_set_output(event, output_event);
2558 if (output_event)
2559 fput_light(output_event->filp, fput_needed);
2560
2561 return ret;
2562 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002563
Li Zefan6fb29152009-10-15 11:21:42 +08002564 case PERF_EVENT_IOC_SET_FILTER:
2565 return perf_event_set_filter(event, (void __user *)arg);
2566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002567 default:
2568 return -ENOTTY;
2569 }
2570
2571 if (flags & PERF_IOC_FLAG_GROUP)
2572 perf_event_for_each(event, func);
2573 else
2574 perf_event_for_each_child(event, func);
2575
2576 return 0;
2577}
2578
2579int perf_event_task_enable(void)
2580{
2581 struct perf_event *event;
2582
2583 mutex_lock(&current->perf_event_mutex);
2584 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2585 perf_event_for_each_child(event, perf_event_enable);
2586 mutex_unlock(&current->perf_event_mutex);
2587
2588 return 0;
2589}
2590
2591int perf_event_task_disable(void)
2592{
2593 struct perf_event *event;
2594
2595 mutex_lock(&current->perf_event_mutex);
2596 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2597 perf_event_for_each_child(event, perf_event_disable);
2598 mutex_unlock(&current->perf_event_mutex);
2599
2600 return 0;
2601}
2602
2603#ifndef PERF_EVENT_INDEX_OFFSET
2604# define PERF_EVENT_INDEX_OFFSET 0
2605#endif
2606
2607static int perf_event_index(struct perf_event *event)
2608{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002609 if (event->hw.state & PERF_HES_STOPPED)
2610 return 0;
2611
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002612 if (event->state != PERF_EVENT_STATE_ACTIVE)
2613 return 0;
2614
2615 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2616}
2617
2618/*
2619 * Callers need to ensure there can be no nesting of this function, otherwise
2620 * the seqlock logic goes bad. We can not serialize this because the arch
2621 * code calls this from NMI context.
2622 */
2623void perf_event_update_userpage(struct perf_event *event)
2624{
2625 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002626 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002627
2628 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002629 buffer = rcu_dereference(event->buffer);
2630 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002631 goto unlock;
2632
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002633 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002634
2635 /*
2636 * Disable preemption so as to not let the corresponding user-space
2637 * spin too long if we get preempted.
2638 */
2639 preempt_disable();
2640 ++userpg->lock;
2641 barrier();
2642 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002643 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002644 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002645 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002646
2647 userpg->time_enabled = event->total_time_enabled +
2648 atomic64_read(&event->child_total_time_enabled);
2649
2650 userpg->time_running = event->total_time_running +
2651 atomic64_read(&event->child_total_time_running);
2652
2653 barrier();
2654 ++userpg->lock;
2655 preempt_enable();
2656unlock:
2657 rcu_read_unlock();
2658}
2659
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002660static unsigned long perf_data_size(struct perf_buffer *buffer);
2661
2662static void
2663perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2664{
2665 long max_size = perf_data_size(buffer);
2666
2667 if (watermark)
2668 buffer->watermark = min(max_size, watermark);
2669
2670 if (!buffer->watermark)
2671 buffer->watermark = max_size / 2;
2672
2673 if (flags & PERF_BUFFER_WRITABLE)
2674 buffer->writable = 1;
2675
2676 atomic_set(&buffer->refcount, 1);
2677}
2678
Peter Zijlstra906010b2009-09-21 16:08:49 +02002679#ifndef CONFIG_PERF_USE_VMALLOC
2680
2681/*
2682 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2683 */
2684
2685static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002686perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002687{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002688 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002689 return NULL;
2690
2691 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002692 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002693
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002694 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002695}
2696
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002697static void *perf_mmap_alloc_page(int cpu)
2698{
2699 struct page *page;
2700 int node;
2701
2702 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2703 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2704 if (!page)
2705 return NULL;
2706
2707 return page_address(page);
2708}
2709
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002710static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002711perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002712{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002713 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714 unsigned long size;
2715 int i;
2716
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002717 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002718 size += nr_pages * sizeof(void *);
2719
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002720 buffer = kzalloc(size, GFP_KERNEL);
2721 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002722 goto fail;
2723
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002724 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002725 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002726 goto fail_user_page;
2727
2728 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002729 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002730 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731 goto fail_data_pages;
2732 }
2733
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002734 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002735
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002736 perf_buffer_init(buffer, watermark, flags);
2737
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002738 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739
2740fail_data_pages:
2741 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002742 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002744 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745
2746fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002747 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002748
2749fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002750 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751}
2752
2753static void perf_mmap_free_page(unsigned long addr)
2754{
2755 struct page *page = virt_to_page((void *)addr);
2756
2757 page->mapping = NULL;
2758 __free_page(page);
2759}
2760
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002761static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763 int i;
2764
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002765 perf_mmap_free_page((unsigned long)buffer->user_page);
2766 for (i = 0; i < buffer->nr_pages; i++)
2767 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2768 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002769}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002770
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002771static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002772{
2773 return 0;
2774}
2775
Peter Zijlstra906010b2009-09-21 16:08:49 +02002776#else
2777
2778/*
2779 * Back perf_mmap() with vmalloc memory.
2780 *
2781 * Required for architectures that have d-cache aliasing issues.
2782 */
2783
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002784static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002785{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002786 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002787}
2788
Peter Zijlstra906010b2009-09-21 16:08:49 +02002789static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002790perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002791{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002792 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002793 return NULL;
2794
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002795 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002796}
2797
2798static void perf_mmap_unmark_page(void *addr)
2799{
2800 struct page *page = vmalloc_to_page(addr);
2801
2802 page->mapping = NULL;
2803}
2804
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002805static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002806{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002807 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002808 void *base;
2809 int i, nr;
2810
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002811 buffer = container_of(work, struct perf_buffer, work);
2812 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002813
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002814 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002815 for (i = 0; i < nr + 1; i++)
2816 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2817
2818 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002819 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002820}
2821
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002822static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002823{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002824 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002825}
2826
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002827static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002828perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002829{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002830 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002831 unsigned long size;
2832 void *all_buf;
2833
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002834 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002835 size += sizeof(void *);
2836
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002837 buffer = kzalloc(size, GFP_KERNEL);
2838 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002839 goto fail;
2840
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002841 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002842
2843 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2844 if (!all_buf)
2845 goto fail_all_buf;
2846
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002847 buffer->user_page = all_buf;
2848 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2849 buffer->page_order = ilog2(nr_pages);
2850 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002851
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002852 perf_buffer_init(buffer, watermark, flags);
2853
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002854 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002855
2856fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002857 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002858
2859fail:
2860 return NULL;
2861}
2862
2863#endif
2864
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002865static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002866{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002867 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002868}
2869
Peter Zijlstra906010b2009-09-21 16:08:49 +02002870static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2871{
2872 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002873 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002874 int ret = VM_FAULT_SIGBUS;
2875
2876 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2877 if (vmf->pgoff == 0)
2878 ret = 0;
2879 return ret;
2880 }
2881
2882 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002883 buffer = rcu_dereference(event->buffer);
2884 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002885 goto unlock;
2886
2887 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2888 goto unlock;
2889
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002890 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002891 if (!vmf->page)
2892 goto unlock;
2893
2894 get_page(vmf->page);
2895 vmf->page->mapping = vma->vm_file->f_mapping;
2896 vmf->page->index = vmf->pgoff;
2897
2898 ret = 0;
2899unlock:
2900 rcu_read_unlock();
2901
2902 return ret;
2903}
2904
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002905static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002906{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002907 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002908
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002909 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2910 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002911}
2912
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002913static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002914{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002915 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002916
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002917 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002918 buffer = rcu_dereference(event->buffer);
2919 if (buffer) {
2920 if (!atomic_inc_not_zero(&buffer->refcount))
2921 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002922 }
2923 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002924
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002925 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002926}
2927
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002928static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002929{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002930 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002931 return;
2932
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002933 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002934}
2935
2936static void perf_mmap_open(struct vm_area_struct *vma)
2937{
2938 struct perf_event *event = vma->vm_file->private_data;
2939
2940 atomic_inc(&event->mmap_count);
2941}
2942
2943static void perf_mmap_close(struct vm_area_struct *vma)
2944{
2945 struct perf_event *event = vma->vm_file->private_data;
2946
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002947 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002948 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002949 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002950 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951
Peter Zijlstra906010b2009-09-21 16:08:49 +02002952 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002953 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002954 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002955 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002956
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002957 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002958 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959 }
2960}
2961
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002962static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002963 .open = perf_mmap_open,
2964 .close = perf_mmap_close,
2965 .fault = perf_mmap_fault,
2966 .page_mkwrite = perf_mmap_fault,
2967};
2968
2969static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2970{
2971 struct perf_event *event = file->private_data;
2972 unsigned long user_locked, user_lock_limit;
2973 struct user_struct *user = current_user();
2974 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002975 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976 unsigned long vma_size;
2977 unsigned long nr_pages;
2978 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002979 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002980
Peter Zijlstrac7920612010-05-18 10:33:24 +02002981 /*
2982 * Don't allow mmap() of inherited per-task counters. This would
2983 * create a performance issue due to all children writing to the
2984 * same buffer.
2985 */
2986 if (event->cpu == -1 && event->attr.inherit)
2987 return -EINVAL;
2988
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989 if (!(vma->vm_flags & VM_SHARED))
2990 return -EINVAL;
2991
2992 vma_size = vma->vm_end - vma->vm_start;
2993 nr_pages = (vma_size / PAGE_SIZE) - 1;
2994
2995 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002996 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002997 * can do bitmasks instead of modulo.
2998 */
2999 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3000 return -EINVAL;
3001
3002 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3003 return -EINVAL;
3004
3005 if (vma->vm_pgoff != 0)
3006 return -EINVAL;
3007
3008 WARN_ON_ONCE(event->ctx->parent_ctx);
3009 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003010 if (event->buffer) {
3011 if (event->buffer->nr_pages == nr_pages)
3012 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003013 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014 ret = -EINVAL;
3015 goto unlock;
3016 }
3017
3018 user_extra = nr_pages + 1;
3019 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3020
3021 /*
3022 * Increase the limit linearly with more CPUs:
3023 */
3024 user_lock_limit *= num_online_cpus();
3025
3026 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3027
3028 extra = 0;
3029 if (user_locked > user_lock_limit)
3030 extra = user_locked - user_lock_limit;
3031
Jiri Slaby78d7d402010-03-05 13:42:54 -08003032 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003033 lock_limit >>= PAGE_SHIFT;
3034 locked = vma->vm_mm->locked_vm + extra;
3035
3036 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3037 !capable(CAP_IPC_LOCK)) {
3038 ret = -EPERM;
3039 goto unlock;
3040 }
3041
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003042 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003043
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003044 if (vma->vm_flags & VM_WRITE)
3045 flags |= PERF_BUFFER_WRITABLE;
3046
3047 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3048 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003049 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003050 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003052 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003053 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003054
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003055 atomic_long_add(user_extra, &user->locked_vm);
3056 event->mmap_locked = extra;
3057 event->mmap_user = get_current_user();
3058 vma->vm_mm->locked_vm += event->mmap_locked;
3059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003061 if (!ret)
3062 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063 mutex_unlock(&event->mmap_mutex);
3064
3065 vma->vm_flags |= VM_RESERVED;
3066 vma->vm_ops = &perf_mmap_vmops;
3067
3068 return ret;
3069}
3070
3071static int perf_fasync(int fd, struct file *filp, int on)
3072{
3073 struct inode *inode = filp->f_path.dentry->d_inode;
3074 struct perf_event *event = filp->private_data;
3075 int retval;
3076
3077 mutex_lock(&inode->i_mutex);
3078 retval = fasync_helper(fd, filp, on, &event->fasync);
3079 mutex_unlock(&inode->i_mutex);
3080
3081 if (retval < 0)
3082 return retval;
3083
3084 return 0;
3085}
3086
3087static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003088 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003089 .release = perf_release,
3090 .read = perf_read,
3091 .poll = perf_poll,
3092 .unlocked_ioctl = perf_ioctl,
3093 .compat_ioctl = perf_ioctl,
3094 .mmap = perf_mmap,
3095 .fasync = perf_fasync,
3096};
3097
3098/*
3099 * Perf event wakeup
3100 *
3101 * If there's data, ensure we set the poll() state and publish everything
3102 * to user-space before waking everybody up.
3103 */
3104
3105void perf_event_wakeup(struct perf_event *event)
3106{
3107 wake_up_all(&event->waitq);
3108
3109 if (event->pending_kill) {
3110 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3111 event->pending_kill = 0;
3112 }
3113}
3114
3115/*
3116 * Pending wakeups
3117 *
3118 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3119 *
3120 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3121 * single linked list and use cmpxchg() to add entries lockless.
3122 */
3123
3124static void perf_pending_event(struct perf_pending_entry *entry)
3125{
3126 struct perf_event *event = container_of(entry,
3127 struct perf_event, pending);
3128
3129 if (event->pending_disable) {
3130 event->pending_disable = 0;
3131 __perf_event_disable(event);
3132 }
3133
3134 if (event->pending_wakeup) {
3135 event->pending_wakeup = 0;
3136 perf_event_wakeup(event);
3137 }
3138}
3139
3140#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3141
3142static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3143 PENDING_TAIL,
3144};
3145
3146static void perf_pending_queue(struct perf_pending_entry *entry,
3147 void (*func)(struct perf_pending_entry *))
3148{
3149 struct perf_pending_entry **head;
3150
3151 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3152 return;
3153
3154 entry->func = func;
3155
3156 head = &get_cpu_var(perf_pending_head);
3157
3158 do {
3159 entry->next = *head;
3160 } while (cmpxchg(head, entry->next, entry) != entry->next);
3161
3162 set_perf_event_pending();
3163
3164 put_cpu_var(perf_pending_head);
3165}
3166
3167static int __perf_pending_run(void)
3168{
3169 struct perf_pending_entry *list;
3170 int nr = 0;
3171
3172 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3173 while (list != PENDING_TAIL) {
3174 void (*func)(struct perf_pending_entry *);
3175 struct perf_pending_entry *entry = list;
3176
3177 list = list->next;
3178
3179 func = entry->func;
3180 entry->next = NULL;
3181 /*
3182 * Ensure we observe the unqueue before we issue the wakeup,
3183 * so that we won't be waiting forever.
3184 * -- see perf_not_pending().
3185 */
3186 smp_wmb();
3187
3188 func(entry);
3189 nr++;
3190 }
3191
3192 return nr;
3193}
3194
3195static inline int perf_not_pending(struct perf_event *event)
3196{
3197 /*
3198 * If we flush on whatever cpu we run, there is a chance we don't
3199 * need to wait.
3200 */
3201 get_cpu();
3202 __perf_pending_run();
3203 put_cpu();
3204
3205 /*
3206 * Ensure we see the proper queue state before going to sleep
3207 * so that we do not miss the wakeup. -- see perf_pending_handle()
3208 */
3209 smp_rmb();
3210 return event->pending.next == NULL;
3211}
3212
3213static void perf_pending_sync(struct perf_event *event)
3214{
3215 wait_event(event->waitq, perf_not_pending(event));
3216}
3217
3218void perf_event_do_pending(void)
3219{
3220 __perf_pending_run();
3221}
3222
3223/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003224 * We assume there is only KVM supporting the callbacks.
3225 * Later on, we might change it to a list if there is
3226 * another virtualization implementation supporting the callbacks.
3227 */
3228struct perf_guest_info_callbacks *perf_guest_cbs;
3229
3230int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3231{
3232 perf_guest_cbs = cbs;
3233 return 0;
3234}
3235EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3236
3237int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3238{
3239 perf_guest_cbs = NULL;
3240 return 0;
3241}
3242EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3243
3244/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245 * Output
3246 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003247static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003248 unsigned long offset, unsigned long head)
3249{
3250 unsigned long mask;
3251
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003252 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253 return true;
3254
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003255 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003256
3257 offset = (offset - tail) & mask;
3258 head = (head - tail) & mask;
3259
3260 if ((int)(head - offset) < 0)
3261 return false;
3262
3263 return true;
3264}
3265
3266static void perf_output_wakeup(struct perf_output_handle *handle)
3267{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003268 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003269
3270 if (handle->nmi) {
3271 handle->event->pending_wakeup = 1;
3272 perf_pending_queue(&handle->event->pending,
3273 perf_pending_event);
3274 } else
3275 perf_event_wakeup(handle->event);
3276}
3277
3278/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003279 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003280 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 * cannot fully serialize things.
3282 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003283 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003284 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003285 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003286static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003287{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003288 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289
Peter Zijlstraef607772010-05-18 10:50:41 +02003290 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003291 local_inc(&buffer->nest);
3292 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293}
3294
Peter Zijlstraef607772010-05-18 10:50:41 +02003295static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003296{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003297 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003298 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299
3300again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003301 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003302
3303 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003304 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305 */
3306
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003307 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003308 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003309
3310 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003311 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003312 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003313 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003314 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003315 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003316
Peter Zijlstraef607772010-05-18 10:50:41 +02003317 /*
3318 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003319 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003320 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003321 if (unlikely(head != local_read(&buffer->head))) {
3322 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323 goto again;
3324 }
3325
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003326 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003327 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003328
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003329out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003330 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331}
3332
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003333__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334 const void *buf, unsigned int len)
3335{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003336 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003337 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003338
3339 memcpy(handle->addr, buf, size);
3340
3341 len -= size;
3342 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003343 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003344 handle->size -= size;
3345 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003346 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003347
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003348 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 handle->page &= buffer->nr_pages - 1;
3350 handle->addr = buffer->data_pages[handle->page];
3351 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003352 }
3353 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003354}
3355
3356int perf_output_begin(struct perf_output_handle *handle,
3357 struct perf_event *event, unsigned int size,
3358 int nmi, int sample)
3359{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003360 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361 unsigned long tail, offset, head;
3362 int have_lost;
3363 struct {
3364 struct perf_event_header header;
3365 u64 id;
3366 u64 lost;
3367 } lost_event;
3368
3369 rcu_read_lock();
3370 /*
3371 * For inherited events we send all the output towards the parent.
3372 */
3373 if (event->parent)
3374 event = event->parent;
3375
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003376 buffer = rcu_dereference(event->buffer);
3377 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003378 goto out;
3379
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003380 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003381 handle->event = event;
3382 handle->nmi = nmi;
3383 handle->sample = sample;
3384
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003385 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003386 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003387
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003388 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389 if (have_lost)
3390 size += sizeof(lost_event);
3391
Peter Zijlstraef607772010-05-18 10:50:41 +02003392 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003393
3394 do {
3395 /*
3396 * Userspace could choose to issue a mb() before updating the
3397 * tail pointer. So that all reads will be completed before the
3398 * write is issued.
3399 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003400 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003402 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003403 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003404 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003406 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003408 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3409 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003410
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003411 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3412 handle->page &= buffer->nr_pages - 1;
3413 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3414 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003415 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003416 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418 if (have_lost) {
3419 lost_event.header.type = PERF_RECORD_LOST;
3420 lost_event.header.misc = 0;
3421 lost_event.header.size = sizeof(lost_event);
3422 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003423 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424
3425 perf_output_put(handle, lost_event);
3426 }
3427
3428 return 0;
3429
3430fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003431 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003432 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003433out:
3434 rcu_read_unlock();
3435
3436 return -ENOSPC;
3437}
3438
3439void perf_output_end(struct perf_output_handle *handle)
3440{
3441 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003442 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003443
3444 int wakeup_events = event->attr.wakeup_events;
3445
3446 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003447 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003448 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003449 local_sub(wakeup_events, &buffer->events);
3450 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003451 }
3452 }
3453
Peter Zijlstraef607772010-05-18 10:50:41 +02003454 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455 rcu_read_unlock();
3456}
3457
3458static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3459{
3460 /*
3461 * only top level events have the pid namespace they were created in
3462 */
3463 if (event->parent)
3464 event = event->parent;
3465
3466 return task_tgid_nr_ns(p, event->ns);
3467}
3468
3469static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3470{
3471 /*
3472 * only top level events have the pid namespace they were created in
3473 */
3474 if (event->parent)
3475 event = event->parent;
3476
3477 return task_pid_nr_ns(p, event->ns);
3478}
3479
3480static void perf_output_read_one(struct perf_output_handle *handle,
3481 struct perf_event *event)
3482{
3483 u64 read_format = event->attr.read_format;
3484 u64 values[4];
3485 int n = 0;
3486
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003487 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003488 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3489 values[n++] = event->total_time_enabled +
3490 atomic64_read(&event->child_total_time_enabled);
3491 }
3492 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3493 values[n++] = event->total_time_running +
3494 atomic64_read(&event->child_total_time_running);
3495 }
3496 if (read_format & PERF_FORMAT_ID)
3497 values[n++] = primary_event_id(event);
3498
3499 perf_output_copy(handle, values, n * sizeof(u64));
3500}
3501
3502/*
3503 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3504 */
3505static void perf_output_read_group(struct perf_output_handle *handle,
3506 struct perf_event *event)
3507{
3508 struct perf_event *leader = event->group_leader, *sub;
3509 u64 read_format = event->attr.read_format;
3510 u64 values[5];
3511 int n = 0;
3512
3513 values[n++] = 1 + leader->nr_siblings;
3514
3515 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3516 values[n++] = leader->total_time_enabled;
3517
3518 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3519 values[n++] = leader->total_time_running;
3520
3521 if (leader != event)
3522 leader->pmu->read(leader);
3523
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003524 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 if (read_format & PERF_FORMAT_ID)
3526 values[n++] = primary_event_id(leader);
3527
3528 perf_output_copy(handle, values, n * sizeof(u64));
3529
3530 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3531 n = 0;
3532
3533 if (sub != event)
3534 sub->pmu->read(sub);
3535
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003536 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003537 if (read_format & PERF_FORMAT_ID)
3538 values[n++] = primary_event_id(sub);
3539
3540 perf_output_copy(handle, values, n * sizeof(u64));
3541 }
3542}
3543
3544static void perf_output_read(struct perf_output_handle *handle,
3545 struct perf_event *event)
3546{
3547 if (event->attr.read_format & PERF_FORMAT_GROUP)
3548 perf_output_read_group(handle, event);
3549 else
3550 perf_output_read_one(handle, event);
3551}
3552
3553void perf_output_sample(struct perf_output_handle *handle,
3554 struct perf_event_header *header,
3555 struct perf_sample_data *data,
3556 struct perf_event *event)
3557{
3558 u64 sample_type = data->type;
3559
3560 perf_output_put(handle, *header);
3561
3562 if (sample_type & PERF_SAMPLE_IP)
3563 perf_output_put(handle, data->ip);
3564
3565 if (sample_type & PERF_SAMPLE_TID)
3566 perf_output_put(handle, data->tid_entry);
3567
3568 if (sample_type & PERF_SAMPLE_TIME)
3569 perf_output_put(handle, data->time);
3570
3571 if (sample_type & PERF_SAMPLE_ADDR)
3572 perf_output_put(handle, data->addr);
3573
3574 if (sample_type & PERF_SAMPLE_ID)
3575 perf_output_put(handle, data->id);
3576
3577 if (sample_type & PERF_SAMPLE_STREAM_ID)
3578 perf_output_put(handle, data->stream_id);
3579
3580 if (sample_type & PERF_SAMPLE_CPU)
3581 perf_output_put(handle, data->cpu_entry);
3582
3583 if (sample_type & PERF_SAMPLE_PERIOD)
3584 perf_output_put(handle, data->period);
3585
3586 if (sample_type & PERF_SAMPLE_READ)
3587 perf_output_read(handle, event);
3588
3589 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3590 if (data->callchain) {
3591 int size = 1;
3592
3593 if (data->callchain)
3594 size += data->callchain->nr;
3595
3596 size *= sizeof(u64);
3597
3598 perf_output_copy(handle, data->callchain, size);
3599 } else {
3600 u64 nr = 0;
3601 perf_output_put(handle, nr);
3602 }
3603 }
3604
3605 if (sample_type & PERF_SAMPLE_RAW) {
3606 if (data->raw) {
3607 perf_output_put(handle, data->raw->size);
3608 perf_output_copy(handle, data->raw->data,
3609 data->raw->size);
3610 } else {
3611 struct {
3612 u32 size;
3613 u32 data;
3614 } raw = {
3615 .size = sizeof(u32),
3616 .data = 0,
3617 };
3618 perf_output_put(handle, raw);
3619 }
3620 }
3621}
3622
3623void perf_prepare_sample(struct perf_event_header *header,
3624 struct perf_sample_data *data,
3625 struct perf_event *event,
3626 struct pt_regs *regs)
3627{
3628 u64 sample_type = event->attr.sample_type;
3629
3630 data->type = sample_type;
3631
3632 header->type = PERF_RECORD_SAMPLE;
3633 header->size = sizeof(*header);
3634
3635 header->misc = 0;
3636 header->misc |= perf_misc_flags(regs);
3637
3638 if (sample_type & PERF_SAMPLE_IP) {
3639 data->ip = perf_instruction_pointer(regs);
3640
3641 header->size += sizeof(data->ip);
3642 }
3643
3644 if (sample_type & PERF_SAMPLE_TID) {
3645 /* namespace issues */
3646 data->tid_entry.pid = perf_event_pid(event, current);
3647 data->tid_entry.tid = perf_event_tid(event, current);
3648
3649 header->size += sizeof(data->tid_entry);
3650 }
3651
3652 if (sample_type & PERF_SAMPLE_TIME) {
3653 data->time = perf_clock();
3654
3655 header->size += sizeof(data->time);
3656 }
3657
3658 if (sample_type & PERF_SAMPLE_ADDR)
3659 header->size += sizeof(data->addr);
3660
3661 if (sample_type & PERF_SAMPLE_ID) {
3662 data->id = primary_event_id(event);
3663
3664 header->size += sizeof(data->id);
3665 }
3666
3667 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3668 data->stream_id = event->id;
3669
3670 header->size += sizeof(data->stream_id);
3671 }
3672
3673 if (sample_type & PERF_SAMPLE_CPU) {
3674 data->cpu_entry.cpu = raw_smp_processor_id();
3675 data->cpu_entry.reserved = 0;
3676
3677 header->size += sizeof(data->cpu_entry);
3678 }
3679
3680 if (sample_type & PERF_SAMPLE_PERIOD)
3681 header->size += sizeof(data->period);
3682
3683 if (sample_type & PERF_SAMPLE_READ)
3684 header->size += perf_event_read_size(event);
3685
3686 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3687 int size = 1;
3688
3689 data->callchain = perf_callchain(regs);
3690
3691 if (data->callchain)
3692 size += data->callchain->nr;
3693
3694 header->size += size * sizeof(u64);
3695 }
3696
3697 if (sample_type & PERF_SAMPLE_RAW) {
3698 int size = sizeof(u32);
3699
3700 if (data->raw)
3701 size += data->raw->size;
3702 else
3703 size += sizeof(u32);
3704
3705 WARN_ON_ONCE(size & (sizeof(u64)-1));
3706 header->size += size;
3707 }
3708}
3709
3710static void perf_event_output(struct perf_event *event, int nmi,
3711 struct perf_sample_data *data,
3712 struct pt_regs *regs)
3713{
3714 struct perf_output_handle handle;
3715 struct perf_event_header header;
3716
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003717 /* protect the callchain buffers */
3718 rcu_read_lock();
3719
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003720 perf_prepare_sample(&header, data, event, regs);
3721
3722 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003723 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003724
3725 perf_output_sample(&handle, &header, data, event);
3726
3727 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003728
3729exit:
3730 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003731}
3732
3733/*
3734 * read event_id
3735 */
3736
3737struct perf_read_event {
3738 struct perf_event_header header;
3739
3740 u32 pid;
3741 u32 tid;
3742};
3743
3744static void
3745perf_event_read_event(struct perf_event *event,
3746 struct task_struct *task)
3747{
3748 struct perf_output_handle handle;
3749 struct perf_read_event read_event = {
3750 .header = {
3751 .type = PERF_RECORD_READ,
3752 .misc = 0,
3753 .size = sizeof(read_event) + perf_event_read_size(event),
3754 },
3755 .pid = perf_event_pid(event, task),
3756 .tid = perf_event_tid(event, task),
3757 };
3758 int ret;
3759
3760 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3761 if (ret)
3762 return;
3763
3764 perf_output_put(&handle, read_event);
3765 perf_output_read(&handle, event);
3766
3767 perf_output_end(&handle);
3768}
3769
3770/*
3771 * task tracking -- fork/exit
3772 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003773 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003774 */
3775
3776struct perf_task_event {
3777 struct task_struct *task;
3778 struct perf_event_context *task_ctx;
3779
3780 struct {
3781 struct perf_event_header header;
3782
3783 u32 pid;
3784 u32 ppid;
3785 u32 tid;
3786 u32 ptid;
3787 u64 time;
3788 } event_id;
3789};
3790
3791static void perf_event_task_output(struct perf_event *event,
3792 struct perf_task_event *task_event)
3793{
3794 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003795 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003796 int size, ret;
3797
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003798 size = task_event->event_id.header.size;
3799 ret = perf_output_begin(&handle, event, size, 0, 0);
3800
Peter Zijlstraef607772010-05-18 10:50:41 +02003801 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003802 return;
3803
3804 task_event->event_id.pid = perf_event_pid(event, task);
3805 task_event->event_id.ppid = perf_event_pid(event, current);
3806
3807 task_event->event_id.tid = perf_event_tid(event, task);
3808 task_event->event_id.ptid = perf_event_tid(event, current);
3809
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003810 perf_output_put(&handle, task_event->event_id);
3811
3812 perf_output_end(&handle);
3813}
3814
3815static int perf_event_task_match(struct perf_event *event)
3816{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003817 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003818 return 0;
3819
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003820 if (event->cpu != -1 && event->cpu != smp_processor_id())
3821 return 0;
3822
Eric B Munson3af9e852010-05-18 15:30:49 +01003823 if (event->attr.comm || event->attr.mmap ||
3824 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003825 return 1;
3826
3827 return 0;
3828}
3829
3830static void perf_event_task_ctx(struct perf_event_context *ctx,
3831 struct perf_task_event *task_event)
3832{
3833 struct perf_event *event;
3834
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003835 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3836 if (perf_event_task_match(event))
3837 perf_event_task_output(event, task_event);
3838 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003839}
3840
3841static void perf_event_task_event(struct perf_task_event *task_event)
3842{
3843 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003844 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003845 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003846 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003847
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003848 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003849 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003850 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003851 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003852
3853 ctx = task_event->task_ctx;
3854 if (!ctx) {
3855 ctxn = pmu->task_ctx_nr;
3856 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003857 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003858 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3859 }
3860 if (ctx)
3861 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003862next:
3863 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003864 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003865 rcu_read_unlock();
3866}
3867
3868static void perf_event_task(struct task_struct *task,
3869 struct perf_event_context *task_ctx,
3870 int new)
3871{
3872 struct perf_task_event task_event;
3873
3874 if (!atomic_read(&nr_comm_events) &&
3875 !atomic_read(&nr_mmap_events) &&
3876 !atomic_read(&nr_task_events))
3877 return;
3878
3879 task_event = (struct perf_task_event){
3880 .task = task,
3881 .task_ctx = task_ctx,
3882 .event_id = {
3883 .header = {
3884 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3885 .misc = 0,
3886 .size = sizeof(task_event.event_id),
3887 },
3888 /* .pid */
3889 /* .ppid */
3890 /* .tid */
3891 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003892 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003893 },
3894 };
3895
3896 perf_event_task_event(&task_event);
3897}
3898
3899void perf_event_fork(struct task_struct *task)
3900{
3901 perf_event_task(task, NULL, 1);
3902}
3903
3904/*
3905 * comm tracking
3906 */
3907
3908struct perf_comm_event {
3909 struct task_struct *task;
3910 char *comm;
3911 int comm_size;
3912
3913 struct {
3914 struct perf_event_header header;
3915
3916 u32 pid;
3917 u32 tid;
3918 } event_id;
3919};
3920
3921static void perf_event_comm_output(struct perf_event *event,
3922 struct perf_comm_event *comm_event)
3923{
3924 struct perf_output_handle handle;
3925 int size = comm_event->event_id.header.size;
3926 int ret = perf_output_begin(&handle, event, size, 0, 0);
3927
3928 if (ret)
3929 return;
3930
3931 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3932 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3933
3934 perf_output_put(&handle, comm_event->event_id);
3935 perf_output_copy(&handle, comm_event->comm,
3936 comm_event->comm_size);
3937 perf_output_end(&handle);
3938}
3939
3940static int perf_event_comm_match(struct perf_event *event)
3941{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003942 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003943 return 0;
3944
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003945 if (event->cpu != -1 && event->cpu != smp_processor_id())
3946 return 0;
3947
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003948 if (event->attr.comm)
3949 return 1;
3950
3951 return 0;
3952}
3953
3954static void perf_event_comm_ctx(struct perf_event_context *ctx,
3955 struct perf_comm_event *comm_event)
3956{
3957 struct perf_event *event;
3958
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3960 if (perf_event_comm_match(event))
3961 perf_event_comm_output(event, comm_event);
3962 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963}
3964
3965static void perf_event_comm_event(struct perf_comm_event *comm_event)
3966{
3967 struct perf_cpu_context *cpuctx;
3968 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003969 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003970 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003971 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003972 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003973
3974 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003975 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003976 size = ALIGN(strlen(comm)+1, sizeof(u64));
3977
3978 comm_event->comm = comm;
3979 comm_event->comm_size = size;
3980
3981 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3982
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003983 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003984 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003985 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003986 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003987
3988 ctxn = pmu->task_ctx_nr;
3989 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003990 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003991
3992 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3993 if (ctx)
3994 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003995next:
3996 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003997 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998 rcu_read_unlock();
3999}
4000
4001void perf_event_comm(struct task_struct *task)
4002{
4003 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004004 struct perf_event_context *ctx;
4005 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004006
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004007 for_each_task_context_nr(ctxn) {
4008 ctx = task->perf_event_ctxp[ctxn];
4009 if (!ctx)
4010 continue;
4011
4012 perf_event_enable_on_exec(ctx);
4013 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004014
4015 if (!atomic_read(&nr_comm_events))
4016 return;
4017
4018 comm_event = (struct perf_comm_event){
4019 .task = task,
4020 /* .comm */
4021 /* .comm_size */
4022 .event_id = {
4023 .header = {
4024 .type = PERF_RECORD_COMM,
4025 .misc = 0,
4026 /* .size */
4027 },
4028 /* .pid */
4029 /* .tid */
4030 },
4031 };
4032
4033 perf_event_comm_event(&comm_event);
4034}
4035
4036/*
4037 * mmap tracking
4038 */
4039
4040struct perf_mmap_event {
4041 struct vm_area_struct *vma;
4042
4043 const char *file_name;
4044 int file_size;
4045
4046 struct {
4047 struct perf_event_header header;
4048
4049 u32 pid;
4050 u32 tid;
4051 u64 start;
4052 u64 len;
4053 u64 pgoff;
4054 } event_id;
4055};
4056
4057static void perf_event_mmap_output(struct perf_event *event,
4058 struct perf_mmap_event *mmap_event)
4059{
4060 struct perf_output_handle handle;
4061 int size = mmap_event->event_id.header.size;
4062 int ret = perf_output_begin(&handle, event, size, 0, 0);
4063
4064 if (ret)
4065 return;
4066
4067 mmap_event->event_id.pid = perf_event_pid(event, current);
4068 mmap_event->event_id.tid = perf_event_tid(event, current);
4069
4070 perf_output_put(&handle, mmap_event->event_id);
4071 perf_output_copy(&handle, mmap_event->file_name,
4072 mmap_event->file_size);
4073 perf_output_end(&handle);
4074}
4075
4076static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004077 struct perf_mmap_event *mmap_event,
4078 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004079{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004080 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004081 return 0;
4082
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004083 if (event->cpu != -1 && event->cpu != smp_processor_id())
4084 return 0;
4085
Eric B Munson3af9e852010-05-18 15:30:49 +01004086 if ((!executable && event->attr.mmap_data) ||
4087 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 return 1;
4089
4090 return 0;
4091}
4092
4093static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004094 struct perf_mmap_event *mmap_event,
4095 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096{
4097 struct perf_event *event;
4098
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004099 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004100 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004101 perf_event_mmap_output(event, mmap_event);
4102 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004103}
4104
4105static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4106{
4107 struct perf_cpu_context *cpuctx;
4108 struct perf_event_context *ctx;
4109 struct vm_area_struct *vma = mmap_event->vma;
4110 struct file *file = vma->vm_file;
4111 unsigned int size;
4112 char tmp[16];
4113 char *buf = NULL;
4114 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004115 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004116 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117
4118 memset(tmp, 0, sizeof(tmp));
4119
4120 if (file) {
4121 /*
4122 * d_path works from the end of the buffer backwards, so we
4123 * need to add enough zero bytes after the string to handle
4124 * the 64bit alignment we do later.
4125 */
4126 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4127 if (!buf) {
4128 name = strncpy(tmp, "//enomem", sizeof(tmp));
4129 goto got_name;
4130 }
4131 name = d_path(&file->f_path, buf, PATH_MAX);
4132 if (IS_ERR(name)) {
4133 name = strncpy(tmp, "//toolong", sizeof(tmp));
4134 goto got_name;
4135 }
4136 } else {
4137 if (arch_vma_name(mmap_event->vma)) {
4138 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4139 sizeof(tmp));
4140 goto got_name;
4141 }
4142
4143 if (!vma->vm_mm) {
4144 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4145 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004146 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4147 vma->vm_end >= vma->vm_mm->brk) {
4148 name = strncpy(tmp, "[heap]", sizeof(tmp));
4149 goto got_name;
4150 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4151 vma->vm_end >= vma->vm_mm->start_stack) {
4152 name = strncpy(tmp, "[stack]", sizeof(tmp));
4153 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154 }
4155
4156 name = strncpy(tmp, "//anon", sizeof(tmp));
4157 goto got_name;
4158 }
4159
4160got_name:
4161 size = ALIGN(strlen(name)+1, sizeof(u64));
4162
4163 mmap_event->file_name = name;
4164 mmap_event->file_size = size;
4165
4166 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4167
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004168 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004169 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004170 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004171 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4172 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004173
4174 ctxn = pmu->task_ctx_nr;
4175 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004176 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004177
4178 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4179 if (ctx) {
4180 perf_event_mmap_ctx(ctx, mmap_event,
4181 vma->vm_flags & VM_EXEC);
4182 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004183next:
4184 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004185 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004186 rcu_read_unlock();
4187
4188 kfree(buf);
4189}
4190
Eric B Munson3af9e852010-05-18 15:30:49 +01004191void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192{
4193 struct perf_mmap_event mmap_event;
4194
4195 if (!atomic_read(&nr_mmap_events))
4196 return;
4197
4198 mmap_event = (struct perf_mmap_event){
4199 .vma = vma,
4200 /* .file_name */
4201 /* .file_size */
4202 .event_id = {
4203 .header = {
4204 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004205 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004206 /* .size */
4207 },
4208 /* .pid */
4209 /* .tid */
4210 .start = vma->vm_start,
4211 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004212 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004213 },
4214 };
4215
4216 perf_event_mmap_event(&mmap_event);
4217}
4218
4219/*
4220 * IRQ throttle logging
4221 */
4222
4223static void perf_log_throttle(struct perf_event *event, int enable)
4224{
4225 struct perf_output_handle handle;
4226 int ret;
4227
4228 struct {
4229 struct perf_event_header header;
4230 u64 time;
4231 u64 id;
4232 u64 stream_id;
4233 } throttle_event = {
4234 .header = {
4235 .type = PERF_RECORD_THROTTLE,
4236 .misc = 0,
4237 .size = sizeof(throttle_event),
4238 },
4239 .time = perf_clock(),
4240 .id = primary_event_id(event),
4241 .stream_id = event->id,
4242 };
4243
4244 if (enable)
4245 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4246
4247 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4248 if (ret)
4249 return;
4250
4251 perf_output_put(&handle, throttle_event);
4252 perf_output_end(&handle);
4253}
4254
4255/*
4256 * Generic event overflow handling, sampling.
4257 */
4258
4259static int __perf_event_overflow(struct perf_event *event, int nmi,
4260 int throttle, struct perf_sample_data *data,
4261 struct pt_regs *regs)
4262{
4263 int events = atomic_read(&event->event_limit);
4264 struct hw_perf_event *hwc = &event->hw;
4265 int ret = 0;
4266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267 if (!throttle) {
4268 hwc->interrupts++;
4269 } else {
4270 if (hwc->interrupts != MAX_INTERRUPTS) {
4271 hwc->interrupts++;
4272 if (HZ * hwc->interrupts >
4273 (u64)sysctl_perf_event_sample_rate) {
4274 hwc->interrupts = MAX_INTERRUPTS;
4275 perf_log_throttle(event, 0);
4276 ret = 1;
4277 }
4278 } else {
4279 /*
4280 * Keep re-disabling events even though on the previous
4281 * pass we disabled it - just in case we raced with a
4282 * sched-in and the event got enabled again:
4283 */
4284 ret = 1;
4285 }
4286 }
4287
4288 if (event->attr.freq) {
4289 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004290 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004291
Peter Zijlstraabd50712010-01-26 18:50:16 +01004292 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004293
Peter Zijlstraabd50712010-01-26 18:50:16 +01004294 if (delta > 0 && delta < 2*TICK_NSEC)
4295 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004296 }
4297
4298 /*
4299 * XXX event_limit might not quite work as expected on inherited
4300 * events
4301 */
4302
4303 event->pending_kill = POLL_IN;
4304 if (events && atomic_dec_and_test(&event->event_limit)) {
4305 ret = 1;
4306 event->pending_kill = POLL_HUP;
4307 if (nmi) {
4308 event->pending_disable = 1;
4309 perf_pending_queue(&event->pending,
4310 perf_pending_event);
4311 } else
4312 perf_event_disable(event);
4313 }
4314
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004315 if (event->overflow_handler)
4316 event->overflow_handler(event, nmi, data, regs);
4317 else
4318 perf_event_output(event, nmi, data, regs);
4319
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004320 return ret;
4321}
4322
4323int perf_event_overflow(struct perf_event *event, int nmi,
4324 struct perf_sample_data *data,
4325 struct pt_regs *regs)
4326{
4327 return __perf_event_overflow(event, nmi, 1, data, regs);
4328}
4329
4330/*
4331 * Generic software event infrastructure
4332 */
4333
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004334struct swevent_htable {
4335 struct swevent_hlist *swevent_hlist;
4336 struct mutex hlist_mutex;
4337 int hlist_refcount;
4338
4339 /* Recursion avoidance in each contexts */
4340 int recursion[PERF_NR_CONTEXTS];
4341};
4342
4343static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004345/*
4346 * We directly increment event->count and keep a second value in
4347 * event->hw.period_left to count intervals. This period event
4348 * is kept in the range [-sample_period, 0] so that we can use the
4349 * sign as trigger.
4350 */
4351
4352static u64 perf_swevent_set_period(struct perf_event *event)
4353{
4354 struct hw_perf_event *hwc = &event->hw;
4355 u64 period = hwc->last_period;
4356 u64 nr, offset;
4357 s64 old, val;
4358
4359 hwc->last_period = hwc->sample_period;
4360
4361again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004362 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363 if (val < 0)
4364 return 0;
4365
4366 nr = div64_u64(period + val, period);
4367 offset = nr * period;
4368 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004369 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004370 goto again;
4371
4372 return nr;
4373}
4374
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004375static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004376 int nmi, struct perf_sample_data *data,
4377 struct pt_regs *regs)
4378{
4379 struct hw_perf_event *hwc = &event->hw;
4380 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004381
4382 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004383 if (!overflow)
4384 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004385
4386 if (hwc->interrupts == MAX_INTERRUPTS)
4387 return;
4388
4389 for (; overflow; overflow--) {
4390 if (__perf_event_overflow(event, nmi, throttle,
4391 data, regs)) {
4392 /*
4393 * We inhibit the overflow from happening when
4394 * hwc->interrupts == MAX_INTERRUPTS.
4395 */
4396 break;
4397 }
4398 throttle = 1;
4399 }
4400}
4401
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004402static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004403 int nmi, struct perf_sample_data *data,
4404 struct pt_regs *regs)
4405{
4406 struct hw_perf_event *hwc = &event->hw;
4407
Peter Zijlstrae7850592010-05-21 14:43:08 +02004408 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410 if (!regs)
4411 return;
4412
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004413 if (!hwc->sample_period)
4414 return;
4415
4416 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4417 return perf_swevent_overflow(event, 1, nmi, data, regs);
4418
Peter Zijlstrae7850592010-05-21 14:43:08 +02004419 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004420 return;
4421
4422 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004423}
4424
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004425static int perf_exclude_event(struct perf_event *event,
4426 struct pt_regs *regs)
4427{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004428 if (event->hw.state & PERF_HES_STOPPED)
4429 return 0;
4430
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004431 if (regs) {
4432 if (event->attr.exclude_user && user_mode(regs))
4433 return 1;
4434
4435 if (event->attr.exclude_kernel && !user_mode(regs))
4436 return 1;
4437 }
4438
4439 return 0;
4440}
4441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004442static int perf_swevent_match(struct perf_event *event,
4443 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004444 u32 event_id,
4445 struct perf_sample_data *data,
4446 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004447{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448 if (event->attr.type != type)
4449 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004450
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451 if (event->attr.config != event_id)
4452 return 0;
4453
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004454 if (perf_exclude_event(event, regs))
4455 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004456
4457 return 1;
4458}
4459
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004460static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004461{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004462 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004463
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004464 return hash_64(val, SWEVENT_HLIST_BITS);
4465}
4466
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004467static inline struct hlist_head *
4468__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004469{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004470 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004471
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004472 return &hlist->heads[hash];
4473}
4474
4475/* For the read side: events when they trigger */
4476static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004477find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004478{
4479 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004480
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004481 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004482 if (!hlist)
4483 return NULL;
4484
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004485 return __find_swevent_head(hlist, type, event_id);
4486}
4487
4488/* For the event head insertion and removal in the hlist */
4489static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004490find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004491{
4492 struct swevent_hlist *hlist;
4493 u32 event_id = event->attr.config;
4494 u64 type = event->attr.type;
4495
4496 /*
4497 * Event scheduling is always serialized against hlist allocation
4498 * and release. Which makes the protected version suitable here.
4499 * The context lock guarantees that.
4500 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004501 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004502 lockdep_is_held(&event->ctx->lock));
4503 if (!hlist)
4504 return NULL;
4505
4506 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004507}
4508
4509static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4510 u64 nr, int nmi,
4511 struct perf_sample_data *data,
4512 struct pt_regs *regs)
4513{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004514 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004515 struct perf_event *event;
4516 struct hlist_node *node;
4517 struct hlist_head *head;
4518
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004519 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004520 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004521 if (!head)
4522 goto end;
4523
4524 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004525 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004526 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004527 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004528end:
4529 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530}
4531
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004532int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004534 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004535
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004536 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004537}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004538EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004539
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004540void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004541{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004542 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004543
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004544 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004545}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004547void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4548 struct pt_regs *regs, u64 addr)
4549{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004550 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004551 int rctx;
4552
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004553 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004554 rctx = perf_swevent_get_recursion_context();
4555 if (rctx < 0)
4556 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004557
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004558 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004559
4560 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004561
4562 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004563 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564}
4565
4566static void perf_swevent_read(struct perf_event *event)
4567{
4568}
4569
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004570static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004571{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004572 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004573 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004574 struct hlist_head *head;
4575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004576 if (hwc->sample_period) {
4577 hwc->last_period = hwc->sample_period;
4578 perf_swevent_set_period(event);
4579 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004580
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004581 hwc->state = !(flags & PERF_EF_START);
4582
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004583 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004584 if (WARN_ON_ONCE(!head))
4585 return -EINVAL;
4586
4587 hlist_add_head_rcu(&event->hlist_entry, head);
4588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004589 return 0;
4590}
4591
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004592static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004593{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004594 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004595}
4596
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004597static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004598{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004599 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004600}
4601
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004602static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004603{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004604 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004605}
4606
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004607/* Deref the hlist from the update side */
4608static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004609swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004610{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004611 return rcu_dereference_protected(swhash->swevent_hlist,
4612 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004613}
4614
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004615static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4616{
4617 struct swevent_hlist *hlist;
4618
4619 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4620 kfree(hlist);
4621}
4622
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004623static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004624{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004625 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004626
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004627 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004628 return;
4629
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004630 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004631 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4632}
4633
4634static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4635{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004636 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004637
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004638 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004639
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004640 if (!--swhash->hlist_refcount)
4641 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004642
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004643 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004644}
4645
4646static void swevent_hlist_put(struct perf_event *event)
4647{
4648 int cpu;
4649
4650 if (event->cpu != -1) {
4651 swevent_hlist_put_cpu(event, event->cpu);
4652 return;
4653 }
4654
4655 for_each_possible_cpu(cpu)
4656 swevent_hlist_put_cpu(event, cpu);
4657}
4658
4659static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4660{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004661 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004662 int err = 0;
4663
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004664 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004665
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004666 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004667 struct swevent_hlist *hlist;
4668
4669 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4670 if (!hlist) {
4671 err = -ENOMEM;
4672 goto exit;
4673 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004674 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004675 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004676 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004677exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004678 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004679
4680 return err;
4681}
4682
4683static int swevent_hlist_get(struct perf_event *event)
4684{
4685 int err;
4686 int cpu, failed_cpu;
4687
4688 if (event->cpu != -1)
4689 return swevent_hlist_get_cpu(event, event->cpu);
4690
4691 get_online_cpus();
4692 for_each_possible_cpu(cpu) {
4693 err = swevent_hlist_get_cpu(event, cpu);
4694 if (err) {
4695 failed_cpu = cpu;
4696 goto fail;
4697 }
4698 }
4699 put_online_cpus();
4700
4701 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004702fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004703 for_each_possible_cpu(cpu) {
4704 if (cpu == failed_cpu)
4705 break;
4706 swevent_hlist_put_cpu(event, cpu);
4707 }
4708
4709 put_online_cpus();
4710 return err;
4711}
4712
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004713atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004714
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004715static void sw_perf_event_destroy(struct perf_event *event)
4716{
4717 u64 event_id = event->attr.config;
4718
4719 WARN_ON(event->parent);
4720
4721 atomic_dec(&perf_swevent_enabled[event_id]);
4722 swevent_hlist_put(event);
4723}
4724
4725static int perf_swevent_init(struct perf_event *event)
4726{
4727 int event_id = event->attr.config;
4728
4729 if (event->attr.type != PERF_TYPE_SOFTWARE)
4730 return -ENOENT;
4731
4732 switch (event_id) {
4733 case PERF_COUNT_SW_CPU_CLOCK:
4734 case PERF_COUNT_SW_TASK_CLOCK:
4735 return -ENOENT;
4736
4737 default:
4738 break;
4739 }
4740
4741 if (event_id > PERF_COUNT_SW_MAX)
4742 return -ENOENT;
4743
4744 if (!event->parent) {
4745 int err;
4746
4747 err = swevent_hlist_get(event);
4748 if (err)
4749 return err;
4750
4751 atomic_inc(&perf_swevent_enabled[event_id]);
4752 event->destroy = sw_perf_event_destroy;
4753 }
4754
4755 return 0;
4756}
4757
4758static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004759 .task_ctx_nr = perf_sw_context,
4760
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004761 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004762 .add = perf_swevent_add,
4763 .del = perf_swevent_del,
4764 .start = perf_swevent_start,
4765 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004766 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004767};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004768
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004769#ifdef CONFIG_EVENT_TRACING
4770
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004771static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004772 struct perf_sample_data *data)
4773{
4774 void *record = data->raw->data;
4775
4776 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4777 return 1;
4778 return 0;
4779}
4780
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004781static int perf_tp_event_match(struct perf_event *event,
4782 struct perf_sample_data *data,
4783 struct pt_regs *regs)
4784{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004785 /*
4786 * All tracepoints are from kernel-space.
4787 */
4788 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004789 return 0;
4790
4791 if (!perf_tp_filter_match(event, data))
4792 return 0;
4793
4794 return 1;
4795}
4796
4797void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004798 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004799{
4800 struct perf_sample_data data;
4801 struct perf_event *event;
4802 struct hlist_node *node;
4803
4804 struct perf_raw_record raw = {
4805 .size = entry_size,
4806 .data = record,
4807 };
4808
4809 perf_sample_data_init(&data, addr);
4810 data.raw = &raw;
4811
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004812 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4813 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004814 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004815 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004816
4817 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004818}
4819EXPORT_SYMBOL_GPL(perf_tp_event);
4820
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004821static void tp_perf_event_destroy(struct perf_event *event)
4822{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004823 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004824}
4825
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004826static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004827{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004828 int err;
4829
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004830 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4831 return -ENOENT;
4832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004833 /*
4834 * Raw tracepoint data is a severe data leak, only allow root to
4835 * have these.
4836 */
4837 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4838 perf_paranoid_tracepoint_raw() &&
4839 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004840 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004841
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004842 err = perf_trace_init(event);
4843 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004844 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004845
4846 event->destroy = tp_perf_event_destroy;
4847
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004848 return 0;
4849}
4850
4851static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004852 .task_ctx_nr = perf_sw_context,
4853
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004854 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004855 .add = perf_trace_add,
4856 .del = perf_trace_del,
4857 .start = perf_swevent_start,
4858 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004859 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004860};
4861
4862static inline void perf_tp_register(void)
4863{
4864 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004865}
Li Zefan6fb29152009-10-15 11:21:42 +08004866
4867static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4868{
4869 char *filter_str;
4870 int ret;
4871
4872 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4873 return -EINVAL;
4874
4875 filter_str = strndup_user(arg, PAGE_SIZE);
4876 if (IS_ERR(filter_str))
4877 return PTR_ERR(filter_str);
4878
4879 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4880
4881 kfree(filter_str);
4882 return ret;
4883}
4884
4885static void perf_event_free_filter(struct perf_event *event)
4886{
4887 ftrace_profile_free_filter(event);
4888}
4889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004890#else
Li Zefan6fb29152009-10-15 11:21:42 +08004891
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004892static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004894}
Li Zefan6fb29152009-10-15 11:21:42 +08004895
4896static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4897{
4898 return -ENOENT;
4899}
4900
4901static void perf_event_free_filter(struct perf_event *event)
4902{
4903}
4904
Li Zefan07b139c2009-12-21 14:27:35 +08004905#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004906
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004907#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004908void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004909{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004910 struct perf_sample_data sample;
4911 struct pt_regs *regs = data;
4912
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004913 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004914
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004915 if (!bp->hw.state && !perf_exclude_event(bp, regs))
4916 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004917}
4918#endif
4919
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004920/*
4921 * hrtimer based swevent callback
4922 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004923
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004924static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004925{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004926 enum hrtimer_restart ret = HRTIMER_RESTART;
4927 struct perf_sample_data data;
4928 struct pt_regs *regs;
4929 struct perf_event *event;
4930 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004931
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004932 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4933 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004934
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004935 perf_sample_data_init(&data, 0);
4936 data.period = event->hw.last_period;
4937 regs = get_irq_regs();
4938
4939 if (regs && !perf_exclude_event(event, regs)) {
4940 if (!(event->attr.exclude_idle && current->pid == 0))
4941 if (perf_event_overflow(event, 0, &data, regs))
4942 ret = HRTIMER_NORESTART;
4943 }
4944
4945 period = max_t(u64, 10000, event->hw.sample_period);
4946 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4947
4948 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004949}
4950
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004951static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004953 struct hw_perf_event *hwc = &event->hw;
4954
4955 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4956 hwc->hrtimer.function = perf_swevent_hrtimer;
4957 if (hwc->sample_period) {
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004958 s64 period = local64_read(&hwc->period_left);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004959
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004960 if (period) {
4961 if (period < 0)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004962 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004963
4964 local64_set(&hwc->period_left, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004965 } else {
4966 period = max_t(u64, 10000, hwc->sample_period);
4967 }
4968 __hrtimer_start_range_ns(&hwc->hrtimer,
4969 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02004970 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004971 }
4972}
4973
4974static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4975{
4976 struct hw_perf_event *hwc = &event->hw;
4977
4978 if (hwc->sample_period) {
4979 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02004980 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004981
4982 hrtimer_cancel(&hwc->hrtimer);
4983 }
4984}
4985
4986/*
4987 * Software event: cpu wall time clock
4988 */
4989
4990static void cpu_clock_event_update(struct perf_event *event)
4991{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004992 s64 prev;
4993 u64 now;
4994
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004995 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004996 prev = local64_xchg(&event->hw.prev_count, now);
4997 local64_add(now - prev, &event->count);
4998}
4999
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005000static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005001{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005002 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005003 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005004}
5005
5006static void cpu_clock_event_stop(struct perf_event *event, int flags)
5007{
5008 perf_swevent_cancel_hrtimer(event);
5009 cpu_clock_event_update(event);
5010}
5011
5012static int cpu_clock_event_add(struct perf_event *event, int flags)
5013{
5014 if (flags & PERF_EF_START)
5015 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005016
5017 return 0;
5018}
5019
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005020static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005021{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005022 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005023}
5024
5025static void cpu_clock_event_read(struct perf_event *event)
5026{
5027 cpu_clock_event_update(event);
5028}
5029
5030static int cpu_clock_event_init(struct perf_event *event)
5031{
5032 if (event->attr.type != PERF_TYPE_SOFTWARE)
5033 return -ENOENT;
5034
5035 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5036 return -ENOENT;
5037
5038 return 0;
5039}
5040
5041static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005042 .task_ctx_nr = perf_sw_context,
5043
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005044 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005045 .add = cpu_clock_event_add,
5046 .del = cpu_clock_event_del,
5047 .start = cpu_clock_event_start,
5048 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005049 .read = cpu_clock_event_read,
5050};
5051
5052/*
5053 * Software event: task time clock
5054 */
5055
5056static void task_clock_event_update(struct perf_event *event, u64 now)
5057{
5058 u64 prev;
5059 s64 delta;
5060
5061 prev = local64_xchg(&event->hw.prev_count, now);
5062 delta = now - prev;
5063 local64_add(delta, &event->count);
5064}
5065
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005066static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005067{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005068 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005069 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005070}
5071
5072static void task_clock_event_stop(struct perf_event *event, int flags)
5073{
5074 perf_swevent_cancel_hrtimer(event);
5075 task_clock_event_update(event, event->ctx->time);
5076}
5077
5078static int task_clock_event_add(struct perf_event *event, int flags)
5079{
5080 if (flags & PERF_EF_START)
5081 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005082
5083 return 0;
5084}
5085
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005086static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005087{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005088 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005089}
5090
5091static void task_clock_event_read(struct perf_event *event)
5092{
5093 u64 time;
5094
5095 if (!in_nmi()) {
5096 update_context_time(event->ctx);
5097 time = event->ctx->time;
5098 } else {
5099 u64 now = perf_clock();
5100 u64 delta = now - event->ctx->timestamp;
5101 time = event->ctx->time + delta;
5102 }
5103
5104 task_clock_event_update(event, time);
5105}
5106
5107static int task_clock_event_init(struct perf_event *event)
5108{
5109 if (event->attr.type != PERF_TYPE_SOFTWARE)
5110 return -ENOENT;
5111
5112 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5113 return -ENOENT;
5114
5115 return 0;
5116}
5117
5118static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005119 .task_ctx_nr = perf_sw_context,
5120
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005121 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005122 .add = task_clock_event_add,
5123 .del = task_clock_event_del,
5124 .start = task_clock_event_start,
5125 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005126 .read = task_clock_event_read,
5127};
5128
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005129static void perf_pmu_nop_void(struct pmu *pmu)
5130{
5131}
5132
5133static int perf_pmu_nop_int(struct pmu *pmu)
5134{
5135 return 0;
5136}
5137
5138static void perf_pmu_start_txn(struct pmu *pmu)
5139{
5140 perf_pmu_disable(pmu);
5141}
5142
5143static int perf_pmu_commit_txn(struct pmu *pmu)
5144{
5145 perf_pmu_enable(pmu);
5146 return 0;
5147}
5148
5149static void perf_pmu_cancel_txn(struct pmu *pmu)
5150{
5151 perf_pmu_enable(pmu);
5152}
5153
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005154/*
5155 * Ensures all contexts with the same task_ctx_nr have the same
5156 * pmu_cpu_context too.
5157 */
5158static void *find_pmu_context(int ctxn)
5159{
5160 struct pmu *pmu;
5161
5162 if (ctxn < 0)
5163 return NULL;
5164
5165 list_for_each_entry(pmu, &pmus, entry) {
5166 if (pmu->task_ctx_nr == ctxn)
5167 return pmu->pmu_cpu_context;
5168 }
5169
5170 return NULL;
5171}
5172
5173static void free_pmu_context(void * __percpu cpu_context)
5174{
5175 struct pmu *pmu;
5176
5177 mutex_lock(&pmus_lock);
5178 /*
5179 * Like a real lame refcount.
5180 */
5181 list_for_each_entry(pmu, &pmus, entry) {
5182 if (pmu->pmu_cpu_context == cpu_context)
5183 goto out;
5184 }
5185
5186 free_percpu(cpu_context);
5187out:
5188 mutex_unlock(&pmus_lock);
5189}
5190
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005191int perf_pmu_register(struct pmu *pmu)
5192{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005193 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005194
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005195 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005196 ret = -ENOMEM;
5197 pmu->pmu_disable_count = alloc_percpu(int);
5198 if (!pmu->pmu_disable_count)
5199 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005200
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005201 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5202 if (pmu->pmu_cpu_context)
5203 goto got_cpu_context;
5204
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005205 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5206 if (!pmu->pmu_cpu_context)
5207 goto free_pdc;
5208
5209 for_each_possible_cpu(cpu) {
5210 struct perf_cpu_context *cpuctx;
5211
5212 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005213 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005214 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005215 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005216 cpuctx->jiffies_interval = 1;
5217 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005218 }
5219
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005220got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005221 if (!pmu->start_txn) {
5222 if (pmu->pmu_enable) {
5223 /*
5224 * If we have pmu_enable/pmu_disable calls, install
5225 * transaction stubs that use that to try and batch
5226 * hardware accesses.
5227 */
5228 pmu->start_txn = perf_pmu_start_txn;
5229 pmu->commit_txn = perf_pmu_commit_txn;
5230 pmu->cancel_txn = perf_pmu_cancel_txn;
5231 } else {
5232 pmu->start_txn = perf_pmu_nop_void;
5233 pmu->commit_txn = perf_pmu_nop_int;
5234 pmu->cancel_txn = perf_pmu_nop_void;
5235 }
5236 }
5237
5238 if (!pmu->pmu_enable) {
5239 pmu->pmu_enable = perf_pmu_nop_void;
5240 pmu->pmu_disable = perf_pmu_nop_void;
5241 }
5242
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005243 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005244 ret = 0;
5245unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005246 mutex_unlock(&pmus_lock);
5247
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005248 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005249
5250free_pdc:
5251 free_percpu(pmu->pmu_disable_count);
5252 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005253}
5254
5255void perf_pmu_unregister(struct pmu *pmu)
5256{
5257 mutex_lock(&pmus_lock);
5258 list_del_rcu(&pmu->entry);
5259 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260
5261 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005262 * We dereference the pmu list under both SRCU and regular RCU, so
5263 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005264 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005265 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005266 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005267
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005268 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005269 free_pmu_context(pmu->pmu_cpu_context);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005270}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005272struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005273{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005274 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005275 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005276
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005277 idx = srcu_read_lock(&pmus_srcu);
5278 list_for_each_entry_rcu(pmu, &pmus, entry) {
5279 int ret = pmu->event_init(event);
5280 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005281 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005282
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005283 if (ret != -ENOENT) {
5284 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005285 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005286 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005287 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005288 pmu = ERR_PTR(-ENOENT);
5289unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005290 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005291
5292 return pmu;
5293}
5294
5295/*
5296 * Allocate and initialize a event structure
5297 */
5298static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005299perf_event_alloc(struct perf_event_attr *attr, int cpu,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005300 struct perf_event *group_leader,
5301 struct perf_event *parent_event,
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005302 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005304 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005305 struct perf_event *event;
5306 struct hw_perf_event *hwc;
5307 long err;
5308
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005309 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005310 if (!event)
5311 return ERR_PTR(-ENOMEM);
5312
5313 /*
5314 * Single events are their own group leaders, with an
5315 * empty sibling list:
5316 */
5317 if (!group_leader)
5318 group_leader = event;
5319
5320 mutex_init(&event->child_mutex);
5321 INIT_LIST_HEAD(&event->child_list);
5322
5323 INIT_LIST_HEAD(&event->group_entry);
5324 INIT_LIST_HEAD(&event->event_entry);
5325 INIT_LIST_HEAD(&event->sibling_list);
5326 init_waitqueue_head(&event->waitq);
5327
5328 mutex_init(&event->mmap_mutex);
5329
5330 event->cpu = cpu;
5331 event->attr = *attr;
5332 event->group_leader = group_leader;
5333 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005334 event->oncpu = -1;
5335
5336 event->parent = parent_event;
5337
5338 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5339 event->id = atomic64_inc_return(&perf_event_id);
5340
5341 event->state = PERF_EVENT_STATE_INACTIVE;
5342
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005343 if (!overflow_handler && parent_event)
5344 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005345
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005346 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005348 if (attr->disabled)
5349 event->state = PERF_EVENT_STATE_OFF;
5350
5351 pmu = NULL;
5352
5353 hwc = &event->hw;
5354 hwc->sample_period = attr->sample_period;
5355 if (attr->freq && attr->sample_freq)
5356 hwc->sample_period = 1;
5357 hwc->last_period = hwc->sample_period;
5358
Peter Zijlstrae7850592010-05-21 14:43:08 +02005359 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005360
5361 /*
5362 * we currently do not support PERF_FORMAT_GROUP on inherited events
5363 */
5364 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5365 goto done;
5366
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005367 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369done:
5370 err = 0;
5371 if (!pmu)
5372 err = -EINVAL;
5373 else if (IS_ERR(pmu))
5374 err = PTR_ERR(pmu);
5375
5376 if (err) {
5377 if (event->ns)
5378 put_pid_ns(event->ns);
5379 kfree(event);
5380 return ERR_PTR(err);
5381 }
5382
5383 event->pmu = pmu;
5384
5385 if (!event->parent) {
5386 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005387 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005388 atomic_inc(&nr_mmap_events);
5389 if (event->attr.comm)
5390 atomic_inc(&nr_comm_events);
5391 if (event->attr.task)
5392 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005393 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5394 err = get_callchain_buffers();
5395 if (err) {
5396 free_event(event);
5397 return ERR_PTR(err);
5398 }
5399 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005400 }
5401
5402 return event;
5403}
5404
5405static int perf_copy_attr(struct perf_event_attr __user *uattr,
5406 struct perf_event_attr *attr)
5407{
5408 u32 size;
5409 int ret;
5410
5411 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5412 return -EFAULT;
5413
5414 /*
5415 * zero the full structure, so that a short copy will be nice.
5416 */
5417 memset(attr, 0, sizeof(*attr));
5418
5419 ret = get_user(size, &uattr->size);
5420 if (ret)
5421 return ret;
5422
5423 if (size > PAGE_SIZE) /* silly large */
5424 goto err_size;
5425
5426 if (!size) /* abi compat */
5427 size = PERF_ATTR_SIZE_VER0;
5428
5429 if (size < PERF_ATTR_SIZE_VER0)
5430 goto err_size;
5431
5432 /*
5433 * If we're handed a bigger struct than we know of,
5434 * ensure all the unknown bits are 0 - i.e. new
5435 * user-space does not rely on any kernel feature
5436 * extensions we dont know about yet.
5437 */
5438 if (size > sizeof(*attr)) {
5439 unsigned char __user *addr;
5440 unsigned char __user *end;
5441 unsigned char val;
5442
5443 addr = (void __user *)uattr + sizeof(*attr);
5444 end = (void __user *)uattr + size;
5445
5446 for (; addr < end; addr++) {
5447 ret = get_user(val, addr);
5448 if (ret)
5449 return ret;
5450 if (val)
5451 goto err_size;
5452 }
5453 size = sizeof(*attr);
5454 }
5455
5456 ret = copy_from_user(attr, uattr, size);
5457 if (ret)
5458 return -EFAULT;
5459
5460 /*
5461 * If the type exists, the corresponding creation will verify
5462 * the attr->config.
5463 */
5464 if (attr->type >= PERF_TYPE_MAX)
5465 return -EINVAL;
5466
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305467 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005468 return -EINVAL;
5469
5470 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5471 return -EINVAL;
5472
5473 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5474 return -EINVAL;
5475
5476out:
5477 return ret;
5478
5479err_size:
5480 put_user(sizeof(*attr), &uattr->size);
5481 ret = -E2BIG;
5482 goto out;
5483}
5484
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005485static int
5486perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005487{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005488 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005489 int ret = -EINVAL;
5490
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005491 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005492 goto set;
5493
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005494 /* don't allow circular references */
5495 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005496 goto out;
5497
Peter Zijlstra0f139302010-05-20 14:35:15 +02005498 /*
5499 * Don't allow cross-cpu buffers
5500 */
5501 if (output_event->cpu != event->cpu)
5502 goto out;
5503
5504 /*
5505 * If its not a per-cpu buffer, it must be the same task.
5506 */
5507 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5508 goto out;
5509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005510set:
5511 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005512 /* Can't redirect output if we've got an active mmap() */
5513 if (atomic_read(&event->mmap_count))
5514 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005515
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005516 if (output_event) {
5517 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005518 buffer = perf_buffer_get(output_event);
5519 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005520 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005521 }
5522
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005523 old_buffer = event->buffer;
5524 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005525 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005526unlock:
5527 mutex_unlock(&event->mmap_mutex);
5528
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005529 if (old_buffer)
5530 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005531out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532 return ret;
5533}
5534
5535/**
5536 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5537 *
5538 * @attr_uptr: event_id type attributes for monitoring/sampling
5539 * @pid: target pid
5540 * @cpu: target cpu
5541 * @group_fd: group leader event fd
5542 */
5543SYSCALL_DEFINE5(perf_event_open,
5544 struct perf_event_attr __user *, attr_uptr,
5545 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5546{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005547 struct perf_event *group_leader = NULL, *output_event = NULL;
5548 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005549 struct perf_event_attr attr;
5550 struct perf_event_context *ctx;
5551 struct file *event_file = NULL;
5552 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07005553 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005554 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005555 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005556 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005558 int err;
5559
5560 /* for future expandability... */
5561 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5562 return -EINVAL;
5563
5564 err = perf_copy_attr(attr_uptr, &attr);
5565 if (err)
5566 return err;
5567
5568 if (!attr.exclude_kernel) {
5569 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5570 return -EACCES;
5571 }
5572
5573 if (attr.freq) {
5574 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5575 return -EINVAL;
5576 }
5577
Al Viroea635c62010-05-26 17:40:29 -04005578 event_fd = get_unused_fd_flags(O_RDWR);
5579 if (event_fd < 0)
5580 return event_fd;
5581
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005582 if (group_fd != -1) {
5583 group_leader = perf_fget_light(group_fd, &fput_needed);
5584 if (IS_ERR(group_leader)) {
5585 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005586 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005587 }
5588 group_file = group_leader->filp;
5589 if (flags & PERF_FLAG_FD_OUTPUT)
5590 output_event = group_leader;
5591 if (flags & PERF_FLAG_FD_NO_GROUP)
5592 group_leader = NULL;
5593 }
5594
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005595 event = perf_event_alloc(&attr, cpu, group_leader, NULL, NULL);
5596 if (IS_ERR(event)) {
5597 err = PTR_ERR(event);
5598 goto err_fd;
5599 }
5600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005602 * Special case software events and allow them to be part of
5603 * any hardware group.
5604 */
5605 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005606
5607 if (group_leader &&
5608 (is_software_event(event) != is_software_event(group_leader))) {
5609 if (is_software_event(event)) {
5610 /*
5611 * If event and group_leader are not both a software
5612 * event, and event is, then group leader is not.
5613 *
5614 * Allow the addition of software events to !software
5615 * groups, this is safe because software events never
5616 * fail to schedule.
5617 */
5618 pmu = group_leader->pmu;
5619 } else if (is_software_event(group_leader) &&
5620 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5621 /*
5622 * In case the group is a pure software group, and we
5623 * try to add a hardware event, move the whole group to
5624 * the hardware context.
5625 */
5626 move_group = 1;
5627 }
5628 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005629
Stephane Eranian540804b2010-10-04 12:00:02 +02005630 if (pid != -1) {
Matt Helsley38a81da2010-09-13 13:01:20 -07005631 task = find_lively_task_by_vpid(pid);
Stephane Eranian540804b2010-10-04 12:00:02 +02005632 if (IS_ERR(task)) {
5633 err = PTR_ERR(task);
5634 goto err_group_fd;
5635 }
5636 }
Matt Helsley38a81da2010-09-13 13:01:20 -07005637
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005638 /*
5639 * Get the target context (task or percpu):
5640 */
Matt Helsley38a81da2010-09-13 13:01:20 -07005641 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005642 if (IS_ERR(ctx)) {
5643 err = PTR_ERR(ctx);
5644 goto err_group_fd;
5645 }
5646
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647 /*
5648 * Look up the group leader (we will attach this event to it):
5649 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005650 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005651 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005652
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005653 /*
5654 * Do not allow a recursive hierarchy (this new sibling
5655 * becoming part of another group-sibling):
5656 */
5657 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005658 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005659 /*
5660 * Do not allow to attach to a group in a different
5661 * task or CPU context:
5662 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005663 if (move_group) {
5664 if (group_leader->ctx->type != ctx->type)
5665 goto err_context;
5666 } else {
5667 if (group_leader->ctx != ctx)
5668 goto err_context;
5669 }
5670
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005671 /*
5672 * Only a group leader can be exclusive or pinned
5673 */
5674 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005675 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005676 }
5677
5678 if (output_event) {
5679 err = perf_event_set_output(event, output_event);
5680 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005681 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005682 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005683
Al Viroea635c62010-05-26 17:40:29 -04005684 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5685 if (IS_ERR(event_file)) {
5686 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005687 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005688 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005689
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005690 if (move_group) {
5691 struct perf_event_context *gctx = group_leader->ctx;
5692
5693 mutex_lock(&gctx->mutex);
5694 perf_event_remove_from_context(group_leader);
5695 list_for_each_entry(sibling, &group_leader->sibling_list,
5696 group_entry) {
5697 perf_event_remove_from_context(sibling);
5698 put_ctx(gctx);
5699 }
5700 mutex_unlock(&gctx->mutex);
5701 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005702 }
5703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005704 event->filp = event_file;
5705 WARN_ON_ONCE(ctx->parent_ctx);
5706 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005707
5708 if (move_group) {
5709 perf_install_in_context(ctx, group_leader, cpu);
5710 get_ctx(ctx);
5711 list_for_each_entry(sibling, &group_leader->sibling_list,
5712 group_entry) {
5713 perf_install_in_context(ctx, sibling, cpu);
5714 get_ctx(ctx);
5715 }
5716 }
5717
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005718 perf_install_in_context(ctx, event, cpu);
5719 ++ctx->generation;
5720 mutex_unlock(&ctx->mutex);
5721
5722 event->owner = current;
5723 get_task_struct(current);
5724 mutex_lock(&current->perf_event_mutex);
5725 list_add_tail(&event->owner_entry, &current->perf_event_list);
5726 mutex_unlock(&current->perf_event_mutex);
5727
Peter Zijlstra8a495422010-05-27 15:47:49 +02005728 /*
5729 * Drop the reference on the group_event after placing the
5730 * new event on the sibling_list. This ensures destruction
5731 * of the group leader will find the pointer to itself in
5732 * perf_group_detach().
5733 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005734 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005735 fd_install(event_fd, event_file);
5736 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005737
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005738err_context:
Al Viroea635c62010-05-26 17:40:29 -04005739 put_ctx(ctx);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005740err_group_fd:
5741 fput_light(group_file, fput_needed);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005742 free_event(event);
Al Viroea635c62010-05-26 17:40:29 -04005743err_fd:
5744 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005745 return err;
5746}
5747
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005748/**
5749 * perf_event_create_kernel_counter
5750 *
5751 * @attr: attributes of the counter to create
5752 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07005753 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005754 */
5755struct perf_event *
5756perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07005757 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005758 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005759{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005760 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005761 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005762 int err;
5763
5764 /*
5765 * Get the target context (task or percpu):
5766 */
5767
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005768 event = perf_event_alloc(attr, cpu, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005769 if (IS_ERR(event)) {
5770 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005771 goto err;
5772 }
5773
Matt Helsley38a81da2010-09-13 13:01:20 -07005774 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005775 if (IS_ERR(ctx)) {
5776 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005777 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005778 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005779
5780 event->filp = NULL;
5781 WARN_ON_ONCE(ctx->parent_ctx);
5782 mutex_lock(&ctx->mutex);
5783 perf_install_in_context(ctx, event, cpu);
5784 ++ctx->generation;
5785 mutex_unlock(&ctx->mutex);
5786
5787 event->owner = current;
5788 get_task_struct(current);
5789 mutex_lock(&current->perf_event_mutex);
5790 list_add_tail(&event->owner_entry, &current->perf_event_list);
5791 mutex_unlock(&current->perf_event_mutex);
5792
5793 return event;
5794
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005795err_free:
5796 free_event(event);
5797err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005798 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005799}
5800EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5801
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005802static void sync_child_event(struct perf_event *child_event,
5803 struct task_struct *child)
5804{
5805 struct perf_event *parent_event = child_event->parent;
5806 u64 child_val;
5807
5808 if (child_event->attr.inherit_stat)
5809 perf_event_read_event(child_event, child);
5810
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005811 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005812
5813 /*
5814 * Add back the child's count to the parent's count:
5815 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005816 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005817 atomic64_add(child_event->total_time_enabled,
5818 &parent_event->child_total_time_enabled);
5819 atomic64_add(child_event->total_time_running,
5820 &parent_event->child_total_time_running);
5821
5822 /*
5823 * Remove this event from the parent's list
5824 */
5825 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5826 mutex_lock(&parent_event->child_mutex);
5827 list_del_init(&child_event->child_list);
5828 mutex_unlock(&parent_event->child_mutex);
5829
5830 /*
5831 * Release the parent event, if this was the last
5832 * reference to it.
5833 */
5834 fput(parent_event->filp);
5835}
5836
5837static void
5838__perf_event_exit_task(struct perf_event *child_event,
5839 struct perf_event_context *child_ctx,
5840 struct task_struct *child)
5841{
5842 struct perf_event *parent_event;
5843
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005844 perf_event_remove_from_context(child_event);
5845
5846 parent_event = child_event->parent;
5847 /*
5848 * It can happen that parent exits first, and has events
5849 * that are still around due to the child reference. These
5850 * events need to be zapped - but otherwise linger.
5851 */
5852 if (parent_event) {
5853 sync_child_event(child_event, child);
5854 free_event(child_event);
5855 }
5856}
5857
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005858static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005859{
5860 struct perf_event *child_event, *tmp;
5861 struct perf_event_context *child_ctx;
5862 unsigned long flags;
5863
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005864 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005865 perf_event_task(child, NULL, 0);
5866 return;
5867 }
5868
5869 local_irq_save(flags);
5870 /*
5871 * We can't reschedule here because interrupts are disabled,
5872 * and either child is current or it is a task that can't be
5873 * scheduled, so we are now safe from rescheduling changing
5874 * our context.
5875 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005876 child_ctx = child->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005877 __perf_event_task_sched_out(child_ctx);
5878
5879 /*
5880 * Take the context lock here so that if find_get_context is
5881 * reading child->perf_event_ctxp, we wait until it has
5882 * incremented the context's refcount before we do put_ctx below.
5883 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005884 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005885 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005886 /*
5887 * If this context is a clone; unclone it so it can't get
5888 * swapped to another process while we're removing all
5889 * the events from it.
5890 */
5891 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005892 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005893 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005894
5895 /*
5896 * Report the task dead after unscheduling the events so that we
5897 * won't get any samples after PERF_RECORD_EXIT. We can however still
5898 * get a few PERF_RECORD_READ events.
5899 */
5900 perf_event_task(child, child_ctx, 0);
5901
5902 /*
5903 * We can recurse on the same lock type through:
5904 *
5905 * __perf_event_exit_task()
5906 * sync_child_event()
5907 * fput(parent_event->filp)
5908 * perf_release()
5909 * mutex_lock(&ctx->mutex)
5910 *
5911 * But since its the parent context it won't be the same instance.
5912 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005913 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005914
5915again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005916 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5917 group_entry)
5918 __perf_event_exit_task(child_event, child_ctx, child);
5919
5920 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005921 group_entry)
5922 __perf_event_exit_task(child_event, child_ctx, child);
5923
5924 /*
5925 * If the last event was a group event, it will have appended all
5926 * its siblings to the list, but we obtained 'tmp' before that which
5927 * will still point to the list head terminating the iteration.
5928 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005929 if (!list_empty(&child_ctx->pinned_groups) ||
5930 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931 goto again;
5932
5933 mutex_unlock(&child_ctx->mutex);
5934
5935 put_ctx(child_ctx);
5936}
5937
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005938/*
5939 * When a child task exits, feed back event values to parent events.
5940 */
5941void perf_event_exit_task(struct task_struct *child)
5942{
5943 int ctxn;
5944
5945 for_each_task_context_nr(ctxn)
5946 perf_event_exit_task_context(child, ctxn);
5947}
5948
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005949static void perf_free_event(struct perf_event *event,
5950 struct perf_event_context *ctx)
5951{
5952 struct perf_event *parent = event->parent;
5953
5954 if (WARN_ON_ONCE(!parent))
5955 return;
5956
5957 mutex_lock(&parent->child_mutex);
5958 list_del_init(&event->child_list);
5959 mutex_unlock(&parent->child_mutex);
5960
5961 fput(parent->filp);
5962
Peter Zijlstra8a495422010-05-27 15:47:49 +02005963 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005964 list_del_event(event, ctx);
5965 free_event(event);
5966}
5967
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005968/*
5969 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005970 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971 */
5972void perf_event_free_task(struct task_struct *task)
5973{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005974 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005975 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005976 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005977
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005978 for_each_task_context_nr(ctxn) {
5979 ctx = task->perf_event_ctxp[ctxn];
5980 if (!ctx)
5981 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005982
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005983 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005984again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005985 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
5986 group_entry)
5987 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005989 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5990 group_entry)
5991 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005992
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005993 if (!list_empty(&ctx->pinned_groups) ||
5994 !list_empty(&ctx->flexible_groups))
5995 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005996
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005997 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005998
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005999 put_ctx(ctx);
6000 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006001}
6002
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006003void perf_event_delayed_put(struct task_struct *task)
6004{
6005 int ctxn;
6006
6007 for_each_task_context_nr(ctxn)
6008 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6009}
6010
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006011/*
6012 * inherit a event from parent task to child task:
6013 */
6014static struct perf_event *
6015inherit_event(struct perf_event *parent_event,
6016 struct task_struct *parent,
6017 struct perf_event_context *parent_ctx,
6018 struct task_struct *child,
6019 struct perf_event *group_leader,
6020 struct perf_event_context *child_ctx)
6021{
6022 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006023 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006024
6025 /*
6026 * Instead of creating recursive hierarchies of events,
6027 * we link inherited events back to the original parent,
6028 * which has a filp for sure, which we use as the reference
6029 * count:
6030 */
6031 if (parent_event->parent)
6032 parent_event = parent_event->parent;
6033
6034 child_event = perf_event_alloc(&parent_event->attr,
6035 parent_event->cpu,
6036 group_leader, parent_event,
6037 NULL);
6038 if (IS_ERR(child_event))
6039 return child_event;
6040 get_ctx(child_ctx);
6041
6042 /*
6043 * Make the child state follow the state of the parent event,
6044 * not its attr.disabled bit. We hold the parent's mutex,
6045 * so we won't race with perf_event_{en, dis}able_family.
6046 */
6047 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6048 child_event->state = PERF_EVENT_STATE_INACTIVE;
6049 else
6050 child_event->state = PERF_EVENT_STATE_OFF;
6051
6052 if (parent_event->attr.freq) {
6053 u64 sample_period = parent_event->hw.sample_period;
6054 struct hw_perf_event *hwc = &child_event->hw;
6055
6056 hwc->sample_period = sample_period;
6057 hwc->last_period = sample_period;
6058
6059 local64_set(&hwc->period_left, sample_period);
6060 }
6061
6062 child_event->ctx = child_ctx;
6063 child_event->overflow_handler = parent_event->overflow_handler;
6064
6065 /*
6066 * Link it up in the child's context:
6067 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006068 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006069 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006070 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006071
6072 /*
6073 * Get a reference to the parent filp - we will fput it
6074 * when the child event exits. This is safe to do because
6075 * we are in the parent and we know that the filp still
6076 * exists and has a nonzero count:
6077 */
6078 atomic_long_inc(&parent_event->filp->f_count);
6079
6080 /*
6081 * Link this into the parent event's child list
6082 */
6083 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6084 mutex_lock(&parent_event->child_mutex);
6085 list_add_tail(&child_event->child_list, &parent_event->child_list);
6086 mutex_unlock(&parent_event->child_mutex);
6087
6088 return child_event;
6089}
6090
6091static int inherit_group(struct perf_event *parent_event,
6092 struct task_struct *parent,
6093 struct perf_event_context *parent_ctx,
6094 struct task_struct *child,
6095 struct perf_event_context *child_ctx)
6096{
6097 struct perf_event *leader;
6098 struct perf_event *sub;
6099 struct perf_event *child_ctr;
6100
6101 leader = inherit_event(parent_event, parent, parent_ctx,
6102 child, NULL, child_ctx);
6103 if (IS_ERR(leader))
6104 return PTR_ERR(leader);
6105 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6106 child_ctr = inherit_event(sub, parent, parent_ctx,
6107 child, leader, child_ctx);
6108 if (IS_ERR(child_ctr))
6109 return PTR_ERR(child_ctr);
6110 }
6111 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006112}
6113
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006114static int
6115inherit_task_group(struct perf_event *event, struct task_struct *parent,
6116 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006117 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006118 int *inherited_all)
6119{
6120 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006121 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006122
6123 if (!event->attr.inherit) {
6124 *inherited_all = 0;
6125 return 0;
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 if (!child_ctx) {
6130 /*
6131 * This is executed from the parent task context, so
6132 * inherit events that have been marked for cloning.
6133 * First allocate and initialize a context for the
6134 * child.
6135 */
6136
Peter Zijlstraeb184472010-09-07 15:55:13 +02006137 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006138 if (!child_ctx)
6139 return -ENOMEM;
6140
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006141 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006142 }
6143
6144 ret = inherit_group(event, parent, parent_ctx,
6145 child, child_ctx);
6146
6147 if (ret)
6148 *inherited_all = 0;
6149
6150 return ret;
6151}
6152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006153/*
6154 * Initialize the perf_event context in task_struct
6155 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006156int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006157{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006158 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006159 struct perf_event_context *cloned_ctx;
6160 struct perf_event *event;
6161 struct task_struct *parent = current;
6162 int inherited_all = 1;
6163 int ret = 0;
6164
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006165 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006166
6167 mutex_init(&child->perf_event_mutex);
6168 INIT_LIST_HEAD(&child->perf_event_list);
6169
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006170 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006171 return 0;
6172
6173 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006174 * If the parent's context is a clone, pin it so it won't get
6175 * swapped under us.
6176 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006177 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006178
6179 /*
6180 * No need to check if parent_ctx != NULL here; since we saw
6181 * it non-NULL earlier, the only reason for it to become NULL
6182 * is if we exit, and since we're currently in the middle of
6183 * a fork we can't be exiting at the same time.
6184 */
6185
6186 /*
6187 * Lock the parent list. No need to lock the child - not PID
6188 * hashed yet and not running, so nobody can access it.
6189 */
6190 mutex_lock(&parent_ctx->mutex);
6191
6192 /*
6193 * We dont have to disable NMIs - we are only looking at
6194 * the list, not manipulating it:
6195 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006196 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006197 ret = inherit_task_group(event, parent, parent_ctx,
6198 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006199 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006200 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006201 }
6202
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006203 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006204 ret = inherit_task_group(event, parent, parent_ctx,
6205 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006206 if (ret)
6207 break;
6208 }
6209
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006210 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006211
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006212 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006213 /*
6214 * Mark the child context as a clone of the parent
6215 * context, or of whatever the parent is a clone of.
6216 * Note that if the parent is a clone, it could get
6217 * uncloned at any point, but that doesn't matter
6218 * because the list of events and the generation
6219 * count can't have changed since we took the mutex.
6220 */
6221 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6222 if (cloned_ctx) {
6223 child_ctx->parent_ctx = cloned_ctx;
6224 child_ctx->parent_gen = parent_ctx->parent_gen;
6225 } else {
6226 child_ctx->parent_ctx = parent_ctx;
6227 child_ctx->parent_gen = parent_ctx->generation;
6228 }
6229 get_ctx(child_ctx->parent_ctx);
6230 }
6231
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006232 mutex_unlock(&parent_ctx->mutex);
6233
6234 perf_unpin_context(parent_ctx);
6235
6236 return ret;
6237}
6238
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006239/*
6240 * Initialize the perf_event context in task_struct
6241 */
6242int perf_event_init_task(struct task_struct *child)
6243{
6244 int ctxn, ret;
6245
6246 for_each_task_context_nr(ctxn) {
6247 ret = perf_event_init_context(child, ctxn);
6248 if (ret)
6249 return ret;
6250 }
6251
6252 return 0;
6253}
6254
Paul Mackerras220b1402010-03-10 20:45:52 +11006255static void __init perf_event_init_all_cpus(void)
6256{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006257 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006258 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006259
6260 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006261 swhash = &per_cpu(swevent_htable, cpu);
6262 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006263 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006264 }
6265}
6266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006267static void __cpuinit perf_event_init_cpu(int cpu)
6268{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006269 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006270
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006271 mutex_lock(&swhash->hlist_mutex);
6272 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006273 struct swevent_hlist *hlist;
6274
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006275 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6276 WARN_ON(!hlist);
6277 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006278 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006279 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006280}
6281
6282#ifdef CONFIG_HOTPLUG_CPU
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006283static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006284{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006285 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6286
6287 WARN_ON(!irqs_disabled());
6288
6289 list_del_init(&cpuctx->rotation_list);
6290}
6291
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006292static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006293{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006294 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006295 struct perf_event *event, *tmp;
6296
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006297 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006298
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006299 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6300 __perf_event_remove_from_context(event);
6301 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006302 __perf_event_remove_from_context(event);
6303}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006304
6305static void perf_event_exit_cpu_context(int cpu)
6306{
6307 struct perf_event_context *ctx;
6308 struct pmu *pmu;
6309 int idx;
6310
6311 idx = srcu_read_lock(&pmus_srcu);
6312 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006313 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006314
6315 mutex_lock(&ctx->mutex);
6316 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6317 mutex_unlock(&ctx->mutex);
6318 }
6319 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006320}
6321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006322static void perf_event_exit_cpu(int cpu)
6323{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006324 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006325
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006326 mutex_lock(&swhash->hlist_mutex);
6327 swevent_hlist_release(swhash);
6328 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006329
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006330 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006331}
6332#else
6333static inline void perf_event_exit_cpu(int cpu) { }
6334#endif
6335
6336static int __cpuinit
6337perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6338{
6339 unsigned int cpu = (long)hcpu;
6340
Peter Zijlstra5e116372010-06-11 13:35:08 +02006341 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006342
6343 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006344 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006345 perf_event_init_cpu(cpu);
6346 break;
6347
Peter Zijlstra5e116372010-06-11 13:35:08 +02006348 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006349 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006350 perf_event_exit_cpu(cpu);
6351 break;
6352
6353 default:
6354 break;
6355 }
6356
6357 return NOTIFY_OK;
6358}
6359
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006360void __init perf_event_init(void)
6361{
Paul Mackerras220b1402010-03-10 20:45:52 +11006362 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006363 init_srcu_struct(&pmus_srcu);
6364 perf_pmu_register(&perf_swevent);
6365 perf_pmu_register(&perf_cpu_clock);
6366 perf_pmu_register(&perf_task_clock);
6367 perf_tp_register();
6368 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006369}