Blame view

kernel/perf_event.c 152 KB
0793a61d4   Thomas Gleixner   performance count...
1
  /*
57c0c15b5   Ingo Molnar   perf: Tidy up aft...
2
   * Performance events core code:
0793a61d4   Thomas Gleixner   performance count...
3
   *
981445114   Ingo Molnar   perf_counter: add...
4
5
6
   *  Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
   *  Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
   *  Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
c5dd016cd   Paul Mackerras   perf_counter: upd...
7
   *  Copyright  ©  2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
7b732a750   Peter Zijlstra   perf_counter: new...
8
   *
57c0c15b5   Ingo Molnar   perf: Tidy up aft...
9
   * For licensing details see kernel-base/COPYING
0793a61d4   Thomas Gleixner   performance count...
10
11
12
   */
  
  #include <linux/fs.h>
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
13
  #include <linux/mm.h>
0793a61d4   Thomas Gleixner   performance count...
14
15
  #include <linux/cpu.h>
  #include <linux/smp.h>
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
16
  #include <linux/idr.h>
04289bb98   Ingo Molnar   perf counters: ad...
17
  #include <linux/file.h>
0793a61d4   Thomas Gleixner   performance count...
18
  #include <linux/poll.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
19
  #include <linux/slab.h>
76e1d9047   Frederic Weisbecker   perf: Store activ...
20
  #include <linux/hash.h>
0793a61d4   Thomas Gleixner   performance count...
21
  #include <linux/sysfs.h>
22a4f650d   Ingo Molnar   perf_counter: Tid...
22
  #include <linux/dcache.h>
0793a61d4   Thomas Gleixner   performance count...
23
  #include <linux/percpu.h>
22a4f650d   Ingo Molnar   perf_counter: Tid...
24
  #include <linux/ptrace.h>
c277443cf   Peter Zijlstra   perf: Stop all co...
25
  #include <linux/reboot.h>
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
26
  #include <linux/vmstat.h>
abe434005   Peter Zijlstra   perf: Sysfs enume...
27
  #include <linux/device.h>
906010b21   Peter Zijlstra   perf_event: Provi...
28
  #include <linux/vmalloc.h>
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
29
30
  #include <linux/hardirq.h>
  #include <linux/rculist.h>
0793a61d4   Thomas Gleixner   performance count...
31
32
33
  #include <linux/uaccess.h>
  #include <linux/syscalls.h>
  #include <linux/anon_inodes.h>
aa9c4c0f9   Ingo Molnar   perfcounters: fix...
34
  #include <linux/kernel_stat.h>
cdd6c482c   Ingo Molnar   perf: Do the big ...
35
  #include <linux/perf_event.h>
6fb2915df   Li Zefan   tracing/profile: ...
36
  #include <linux/ftrace_event.h>
3c502e7a0   Jason Wessel   perf,hw_breakpoin...
37
  #include <linux/hw_breakpoint.h>
0793a61d4   Thomas Gleixner   performance count...
38

4e193bd4d   Tim Blechmann   perf_counter: inc...
39
  #include <asm/irq_regs.h>
0b3fcf178   Stephane Eranian   perf_events: Move...
40
41
42
43
44
  enum event_type_t {
  	EVENT_FLEXIBLE = 0x1,
  	EVENT_PINNED = 0x2,
  	EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  };
82cd6def9   Peter Zijlstra   perf: Use jump_la...
45
  atomic_t perf_task_events __read_mostly;
cdd6c482c   Ingo Molnar   perf: Do the big ...
46
47
48
  static atomic_t nr_mmap_events __read_mostly;
  static atomic_t nr_comm_events __read_mostly;
  static atomic_t nr_task_events __read_mostly;
9ee318a78   Peter Zijlstra   perf_counter: opt...
49

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
50
51
52
  static LIST_HEAD(pmus);
  static DEFINE_MUTEX(pmus_lock);
  static struct srcu_struct pmus_srcu;
0764771da   Peter Zijlstra   perf_counter: Mor...
53
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
54
   * perf event paranoia level:
0fbdea19e   Ingo Molnar   perf_counter: Int...
55
56
   *  -1 - not paranoid at all
   *   0 - disallow raw tracepoint access for unpriv
cdd6c482c   Ingo Molnar   perf: Do the big ...
57
   *   1 - disallow cpu events for unpriv
0fbdea19e   Ingo Molnar   perf_counter: Int...
58
   *   2 - disallow kernel profiling for unpriv
0764771da   Peter Zijlstra   perf_counter: Mor...
59
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
60
  int sysctl_perf_event_paranoid __read_mostly = 1;
0764771da   Peter Zijlstra   perf_counter: Mor...
61

cdd6c482c   Ingo Molnar   perf: Do the big ...
62
  int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
df58ab24b   Peter Zijlstra   perf_counter: Ren...
63
64
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
65
   * max perf event sample rate
df58ab24b   Peter Zijlstra   perf_counter: Ren...
66
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
67
  int sysctl_perf_event_sample_rate __read_mostly = 100000;
1ccd15497   Peter Zijlstra   perf_counter: sys...
68

cdd6c482c   Ingo Molnar   perf: Do the big ...
69
  static atomic64_t perf_event_id;
a96bbc164   Peter Zijlstra   perf_counter: Fix...
70

0b3fcf178   Stephane Eranian   perf_events: Move...
71
72
73
74
75
  static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  			      enum event_type_t event_type);
  
  static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  			     enum event_type_t event_type);
cdd6c482c   Ingo Molnar   perf: Do the big ...
76
  void __weak perf_event_print_debug(void)	{ }
0793a61d4   Thomas Gleixner   performance count...
77

84c799105   Matt Fleming   perf: New helper ...
78
  extern __weak const char *perf_pmu_name(void)
0793a61d4   Thomas Gleixner   performance count...
79
  {
84c799105   Matt Fleming   perf: New helper ...
80
  	return "pmu";
0793a61d4   Thomas Gleixner   performance count...
81
  }
0b3fcf178   Stephane Eranian   perf_events: Move...
82
83
84
85
  static inline u64 perf_clock(void)
  {
  	return local_clock();
  }
33696fc0d   Peter Zijlstra   perf: Per PMU dis...
86
  void perf_pmu_disable(struct pmu *pmu)
9e35ad388   Peter Zijlstra   perf_counter: Rew...
87
  {
33696fc0d   Peter Zijlstra   perf: Per PMU dis...
88
89
90
  	int *count = this_cpu_ptr(pmu->pmu_disable_count);
  	if (!(*count)++)
  		pmu->pmu_disable(pmu);
9e35ad388   Peter Zijlstra   perf_counter: Rew...
91
  }
9e35ad388   Peter Zijlstra   perf_counter: Rew...
92

33696fc0d   Peter Zijlstra   perf: Per PMU dis...
93
  void perf_pmu_enable(struct pmu *pmu)
9e35ad388   Peter Zijlstra   perf_counter: Rew...
94
  {
33696fc0d   Peter Zijlstra   perf: Per PMU dis...
95
96
97
  	int *count = this_cpu_ptr(pmu->pmu_disable_count);
  	if (!--(*count))
  		pmu->pmu_enable(pmu);
9e35ad388   Peter Zijlstra   perf_counter: Rew...
98
  }
9e35ad388   Peter Zijlstra   perf_counter: Rew...
99

e9d2b0641   Peter Zijlstra   perf: Undo the pe...
100
101
102
103
104
105
106
  static DEFINE_PER_CPU(struct list_head, rotation_list);
  
  /*
   * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
   * because they're strictly cpu affine and rotate_start is called with IRQs
   * disabled, while rotate_context is called from IRQ context.
   */
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
107
  static void perf_pmu_rotate_start(struct pmu *pmu)
9e35ad388   Peter Zijlstra   perf_counter: Rew...
108
  {
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
109
  	struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
110
  	struct list_head *head = &__get_cpu_var(rotation_list);
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
111

e9d2b0641   Peter Zijlstra   perf: Undo the pe...
112
  	WARN_ON(!irqs_disabled());
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
113

e9d2b0641   Peter Zijlstra   perf: Undo the pe...
114
115
  	if (list_empty(&cpuctx->rotation_list))
  		list_add(&cpuctx->rotation_list, head);
9e35ad388   Peter Zijlstra   perf_counter: Rew...
116
  }
9e35ad388   Peter Zijlstra   perf_counter: Rew...
117

cdd6c482c   Ingo Molnar   perf: Do the big ...
118
  static void get_ctx(struct perf_event_context *ctx)
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
119
  {
e5289d4a1   Peter Zijlstra   perf_counter: Sim...
120
  	WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
121
  }
c93f76690   Paul Mackerras   perf_counter: Fix...
122
123
  static void free_ctx(struct rcu_head *head)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
124
  	struct perf_event_context *ctx;
c93f76690   Paul Mackerras   perf_counter: Fix...
125

cdd6c482c   Ingo Molnar   perf: Do the big ...
126
  	ctx = container_of(head, struct perf_event_context, rcu_head);
c93f76690   Paul Mackerras   perf_counter: Fix...
127
128
  	kfree(ctx);
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
129
  static void put_ctx(struct perf_event_context *ctx)
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
130
  {
564c2b210   Paul Mackerras   perf_counter: Opt...
131
132
133
  	if (atomic_dec_and_test(&ctx->refcount)) {
  		if (ctx->parent_ctx)
  			put_ctx(ctx->parent_ctx);
c93f76690   Paul Mackerras   perf_counter: Fix...
134
135
136
  		if (ctx->task)
  			put_task_struct(ctx->task);
  		call_rcu(&ctx->rcu_head, free_ctx);
564c2b210   Paul Mackerras   perf_counter: Opt...
137
  	}
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
138
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
139
  static void unclone_ctx(struct perf_event_context *ctx)
71a851b4d   Peter Zijlstra   perf_counter: Sto...
140
141
142
143
144
145
  {
  	if (ctx->parent_ctx) {
  		put_ctx(ctx->parent_ctx);
  		ctx->parent_ctx = NULL;
  	}
  }
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  {
  	/*
  	 * only top level events have the pid namespace they were created in
  	 */
  	if (event->parent)
  		event = event->parent;
  
  	return task_tgid_nr_ns(p, event->ns);
  }
  
  static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  {
  	/*
  	 * only top level events have the pid namespace they were created in
  	 */
  	if (event->parent)
  		event = event->parent;
  
  	return task_pid_nr_ns(p, event->ns);
  }
fccc714b3   Peter Zijlstra   perf_counter: San...
167
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
168
   * If we inherit events we want to return the parent event id
7f453c24b   Peter Zijlstra   perf_counter: PER...
169
170
   * to userspace.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
171
  static u64 primary_event_id(struct perf_event *event)
7f453c24b   Peter Zijlstra   perf_counter: PER...
172
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
173
  	u64 id = event->id;
7f453c24b   Peter Zijlstra   perf_counter: PER...
174

cdd6c482c   Ingo Molnar   perf: Do the big ...
175
176
  	if (event->parent)
  		id = event->parent->id;
7f453c24b   Peter Zijlstra   perf_counter: PER...
177
178
179
  
  	return id;
  }
fccc714b3   Peter Zijlstra   perf_counter: San...
180
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
181
   * Get the perf_event_context for a task and lock it.
25346b93c   Paul Mackerras   perf_counter: Pro...
182
183
184
   * This has to cope with with the fact that until it is locked,
   * the context could get moved to another task.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
185
  static struct perf_event_context *
8dc85d547   Peter Zijlstra   perf: Multiple ta...
186
  perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
25346b93c   Paul Mackerras   perf_counter: Pro...
187
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
188
  	struct perf_event_context *ctx;
25346b93c   Paul Mackerras   perf_counter: Pro...
189
190
  
  	rcu_read_lock();
9ed6060d2   Peter Zijlstra   perf: Unindent la...
191
  retry:
8dc85d547   Peter Zijlstra   perf: Multiple ta...
192
  	ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
25346b93c   Paul Mackerras   perf_counter: Pro...
193
194
195
196
  	if (ctx) {
  		/*
  		 * If this context is a clone of another, it might
  		 * get swapped for another underneath us by
cdd6c482c   Ingo Molnar   perf: Do the big ...
197
  		 * perf_event_task_sched_out, though the
25346b93c   Paul Mackerras   perf_counter: Pro...
198
199
200
201
202
203
  		 * rcu_read_lock() protects us from any context
  		 * getting freed.  Lock the context and check if it
  		 * got swapped before we could get the lock, and retry
  		 * if so.  If we locked the right context, then it
  		 * can't get swapped on us any more.
  		 */
e625cce1b   Thomas Gleixner   perf_event: Conve...
204
  		raw_spin_lock_irqsave(&ctx->lock, *flags);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
205
  		if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
e625cce1b   Thomas Gleixner   perf_event: Conve...
206
  			raw_spin_unlock_irqrestore(&ctx->lock, *flags);
25346b93c   Paul Mackerras   perf_counter: Pro...
207
208
  			goto retry;
  		}
b49a9e7e7   Peter Zijlstra   perf_counter: Clo...
209
210
  
  		if (!atomic_inc_not_zero(&ctx->refcount)) {
e625cce1b   Thomas Gleixner   perf_event: Conve...
211
  			raw_spin_unlock_irqrestore(&ctx->lock, *flags);
b49a9e7e7   Peter Zijlstra   perf_counter: Clo...
212
213
  			ctx = NULL;
  		}
25346b93c   Paul Mackerras   perf_counter: Pro...
214
215
216
217
218
219
220
221
222
223
  	}
  	rcu_read_unlock();
  	return ctx;
  }
  
  /*
   * Get the context for a task and increment its pin_count so it
   * can't get swapped to another task.  This also increments its
   * reference count so that the context can't get freed.
   */
8dc85d547   Peter Zijlstra   perf: Multiple ta...
224
225
  static struct perf_event_context *
  perf_pin_task_context(struct task_struct *task, int ctxn)
25346b93c   Paul Mackerras   perf_counter: Pro...
226
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
227
  	struct perf_event_context *ctx;
25346b93c   Paul Mackerras   perf_counter: Pro...
228
  	unsigned long flags;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
229
  	ctx = perf_lock_task_context(task, ctxn, &flags);
25346b93c   Paul Mackerras   perf_counter: Pro...
230
231
  	if (ctx) {
  		++ctx->pin_count;
e625cce1b   Thomas Gleixner   perf_event: Conve...
232
  		raw_spin_unlock_irqrestore(&ctx->lock, flags);
25346b93c   Paul Mackerras   perf_counter: Pro...
233
234
235
  	}
  	return ctx;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
236
  static void perf_unpin_context(struct perf_event_context *ctx)
25346b93c   Paul Mackerras   perf_counter: Pro...
237
238
  {
  	unsigned long flags;
e625cce1b   Thomas Gleixner   perf_event: Conve...
239
  	raw_spin_lock_irqsave(&ctx->lock, flags);
25346b93c   Paul Mackerras   perf_counter: Pro...
240
  	--ctx->pin_count;
e625cce1b   Thomas Gleixner   perf_event: Conve...
241
  	raw_spin_unlock_irqrestore(&ctx->lock, flags);
25346b93c   Paul Mackerras   perf_counter: Pro...
242
243
  	put_ctx(ctx);
  }
f67218c3e   Peter Zijlstra   perf_events: Fix ...
244
245
246
247
248
249
250
251
252
253
  /*
   * Update the record of the current time in a context.
   */
  static void update_context_time(struct perf_event_context *ctx)
  {
  	u64 now = perf_clock();
  
  	ctx->time += now - ctx->timestamp;
  	ctx->timestamp = now;
  }
4158755d3   Stephane Eranian   perf_events: Add ...
254
255
256
257
258
  static u64 perf_event_time(struct perf_event *event)
  {
  	struct perf_event_context *ctx = event->ctx;
  	return ctx ? ctx->time : 0;
  }
f67218c3e   Peter Zijlstra   perf_events: Fix ...
259
260
261
262
263
264
265
266
267
268
269
  /*
   * Update the total_time_enabled and total_time_running fields for a event.
   */
  static void update_event_times(struct perf_event *event)
  {
  	struct perf_event_context *ctx = event->ctx;
  	u64 run_end;
  
  	if (event->state < PERF_EVENT_STATE_INACTIVE ||
  	    event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  		return;
acd1d7c1f   Peter Zijlstra   perf_events: Rest...
270
  	if (ctx->is_active)
4158755d3   Stephane Eranian   perf_events: Add ...
271
  		run_end = perf_event_time(event);
acd1d7c1f   Peter Zijlstra   perf_events: Rest...
272
273
274
275
  	else
  		run_end = event->tstamp_stopped;
  
  	event->total_time_enabled = run_end - event->tstamp_enabled;
f67218c3e   Peter Zijlstra   perf_events: Fix ...
276
277
278
279
  
  	if (event->state == PERF_EVENT_STATE_INACTIVE)
  		run_end = event->tstamp_stopped;
  	else
4158755d3   Stephane Eranian   perf_events: Add ...
280
  		run_end = perf_event_time(event);
f67218c3e   Peter Zijlstra   perf_events: Fix ...
281
282
283
  
  	event->total_time_running = run_end - event->tstamp_running;
  }
96c21a460   Peter Zijlstra   perf: Fix exit() ...
284
285
286
287
288
289
290
291
292
293
294
  /*
   * Update total_time_enabled and total_time_running for all events in a group.
   */
  static void update_group_times(struct perf_event *leader)
  {
  	struct perf_event *event;
  
  	update_event_times(leader);
  	list_for_each_entry(event, &leader->sibling_list, group_entry)
  		update_event_times(event);
  }
889ff0150   Frederic Weisbecker   perf/core: Split ...
295
296
297
298
299
300
301
302
  static struct list_head *
  ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  {
  	if (event->attr.pinned)
  		return &ctx->pinned_groups;
  	else
  		return &ctx->flexible_groups;
  }
25346b93c   Paul Mackerras   perf_counter: Pro...
303
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
304
   * Add a event from the lists for its context.
fccc714b3   Peter Zijlstra   perf_counter: San...
305
306
   * Must be called with ctx->mutex and ctx->lock held.
   */
04289bb98   Ingo Molnar   perf counters: ad...
307
  static void
cdd6c482c   Ingo Molnar   perf: Do the big ...
308
  list_add_event(struct perf_event *event, struct perf_event_context *ctx)
04289bb98   Ingo Molnar   perf counters: ad...
309
  {
8a49542c0   Peter Zijlstra   perf_events: Fix ...
310
311
  	WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  	event->attach_state |= PERF_ATTACH_CONTEXT;
04289bb98   Ingo Molnar   perf counters: ad...
312
313
  
  	/*
8a49542c0   Peter Zijlstra   perf_events: Fix ...
314
315
316
  	 * If we're a stand alone event or group leader, we go to the context
  	 * list, group events are kept attached to the group so that
  	 * perf_group_detach can, at all times, locate all siblings.
04289bb98   Ingo Molnar   perf counters: ad...
317
  	 */
8a49542c0   Peter Zijlstra   perf_events: Fix ...
318
  	if (event->group_leader == event) {
889ff0150   Frederic Weisbecker   perf/core: Split ...
319
  		struct list_head *list;
d6f962b57   Frederic Weisbecker   perf: Export soft...
320
321
  		if (is_software_event(event))
  			event->group_flags |= PERF_GROUP_SOFTWARE;
889ff0150   Frederic Weisbecker   perf/core: Split ...
322
323
  		list = ctx_group_list(event, ctx);
  		list_add_tail(&event->group_entry, list);
5c1481943   Peter Zijlstra   perf_counter: out...
324
  	}
592903cdc   Peter Zijlstra   perf_counter: add...
325

cdd6c482c   Ingo Molnar   perf: Do the big ...
326
  	list_add_rcu(&event->event_entry, &ctx->event_list);
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
327
  	if (!ctx->nr_events)
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
328
  		perf_pmu_rotate_start(ctx->pmu);
cdd6c482c   Ingo Molnar   perf: Do the big ...
329
330
  	ctx->nr_events++;
  	if (event->attr.inherit_stat)
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
331
  		ctx->nr_stat++;
04289bb98   Ingo Molnar   perf counters: ad...
332
  }
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  /*
   * Called at perf_event creation and when events are attached/detached from a
   * group.
   */
  static void perf_event__read_size(struct perf_event *event)
  {
  	int entry = sizeof(u64); /* value */
  	int size = 0;
  	int nr = 1;
  
  	if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  		size += sizeof(u64);
  
  	if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  		size += sizeof(u64);
  
  	if (event->attr.read_format & PERF_FORMAT_ID)
  		entry += sizeof(u64);
  
  	if (event->attr.read_format & PERF_FORMAT_GROUP) {
  		nr += event->group_leader->nr_siblings;
  		size += sizeof(u64);
  	}
  
  	size += entry * nr;
  	event->read_size = size;
  }
  
  static void perf_event__header_size(struct perf_event *event)
  {
  	struct perf_sample_data *data;
  	u64 sample_type = event->attr.sample_type;
  	u16 size = 0;
  
  	perf_event__read_size(event);
  
  	if (sample_type & PERF_SAMPLE_IP)
  		size += sizeof(data->ip);
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
  	if (sample_type & PERF_SAMPLE_ADDR)
  		size += sizeof(data->addr);
  
  	if (sample_type & PERF_SAMPLE_PERIOD)
  		size += sizeof(data->period);
  
  	if (sample_type & PERF_SAMPLE_READ)
  		size += event->read_size;
  
  	event->header_size = size;
  }
  
  static void perf_event__id_header_size(struct perf_event *event)
  {
  	struct perf_sample_data *data;
  	u64 sample_type = event->attr.sample_type;
  	u16 size = 0;
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
388
389
390
391
392
  	if (sample_type & PERF_SAMPLE_TID)
  		size += sizeof(data->tid_entry);
  
  	if (sample_type & PERF_SAMPLE_TIME)
  		size += sizeof(data->time);
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
393
394
395
396
397
398
399
400
  	if (sample_type & PERF_SAMPLE_ID)
  		size += sizeof(data->id);
  
  	if (sample_type & PERF_SAMPLE_STREAM_ID)
  		size += sizeof(data->stream_id);
  
  	if (sample_type & PERF_SAMPLE_CPU)
  		size += sizeof(data->cpu_entry);
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
401
  	event->id_header_size = size;
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
402
  }
8a49542c0   Peter Zijlstra   perf_events: Fix ...
403
404
  static void perf_group_attach(struct perf_event *event)
  {
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
405
  	struct perf_event *group_leader = event->group_leader, *pos;
8a49542c0   Peter Zijlstra   perf_events: Fix ...
406

74c3337c2   Peter Zijlstra   perf: Fix group m...
407
408
409
410
411
  	/*
  	 * We can have double attach due to group movement in perf_event_open.
  	 */
  	if (event->attach_state & PERF_ATTACH_GROUP)
  		return;
8a49542c0   Peter Zijlstra   perf_events: Fix ...
412
413
414
415
416
417
418
419
420
421
422
  	event->attach_state |= PERF_ATTACH_GROUP;
  
  	if (group_leader == event)
  		return;
  
  	if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  			!is_software_event(event))
  		group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  
  	list_add_tail(&event->group_entry, &group_leader->sibling_list);
  	group_leader->nr_siblings++;
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
423
424
425
426
427
  
  	perf_event__header_size(group_leader);
  
  	list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  		perf_event__header_size(pos);
8a49542c0   Peter Zijlstra   perf_events: Fix ...
428
  }
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
429
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
430
   * Remove a event from the lists for its context.
fccc714b3   Peter Zijlstra   perf_counter: San...
431
   * Must be called with ctx->mutex and ctx->lock held.
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
432
   */
04289bb98   Ingo Molnar   perf counters: ad...
433
  static void
cdd6c482c   Ingo Molnar   perf: Do the big ...
434
  list_del_event(struct perf_event *event, struct perf_event_context *ctx)
04289bb98   Ingo Molnar   perf counters: ad...
435
  {
8a49542c0   Peter Zijlstra   perf_events: Fix ...
436
437
438
439
  	/*
  	 * We can have double detach due to exit/hot-unplug + close.
  	 */
  	if (!(event->attach_state & PERF_ATTACH_CONTEXT))
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
440
  		return;
8a49542c0   Peter Zijlstra   perf_events: Fix ...
441
442
  
  	event->attach_state &= ~PERF_ATTACH_CONTEXT;
cdd6c482c   Ingo Molnar   perf: Do the big ...
443
444
  	ctx->nr_events--;
  	if (event->attr.inherit_stat)
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
445
  		ctx->nr_stat--;
8bc209595   Peter Zijlstra   perf_counter: Fix...
446

cdd6c482c   Ingo Molnar   perf: Do the big ...
447
  	list_del_rcu(&event->event_entry);
04289bb98   Ingo Molnar   perf counters: ad...
448

8a49542c0   Peter Zijlstra   perf_events: Fix ...
449
450
  	if (event->group_leader == event)
  		list_del_init(&event->group_entry);
5c1481943   Peter Zijlstra   perf_counter: out...
451

96c21a460   Peter Zijlstra   perf: Fix exit() ...
452
  	update_group_times(event);
b2e74a265   Stephane Eranian   perf_events: Fix ...
453
454
455
456
457
458
459
460
461
462
  
  	/*
  	 * If event was in error state, then keep it
  	 * that way, otherwise bogus counts will be
  	 * returned on read(). The only way to get out
  	 * of error state is by explicit re-enabling
  	 * of the event
  	 */
  	if (event->state > PERF_EVENT_STATE_OFF)
  		event->state = PERF_EVENT_STATE_OFF;
050735b08   Peter Zijlstra   perf: Fix exit() ...
463
  }
8a49542c0   Peter Zijlstra   perf_events: Fix ...
464
  static void perf_group_detach(struct perf_event *event)
050735b08   Peter Zijlstra   perf: Fix exit() ...
465
466
  {
  	struct perf_event *sibling, *tmp;
8a49542c0   Peter Zijlstra   perf_events: Fix ...
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  	struct list_head *list = NULL;
  
  	/*
  	 * We can have double detach due to exit/hot-unplug + close.
  	 */
  	if (!(event->attach_state & PERF_ATTACH_GROUP))
  		return;
  
  	event->attach_state &= ~PERF_ATTACH_GROUP;
  
  	/*
  	 * If this is a sibling, remove it from its group.
  	 */
  	if (event->group_leader != event) {
  		list_del_init(&event->group_entry);
  		event->group_leader->nr_siblings--;
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
483
  		goto out;
8a49542c0   Peter Zijlstra   perf_events: Fix ...
484
485
486
487
  	}
  
  	if (!list_empty(&event->group_entry))
  		list = &event->group_entry;
2e2af50b1   Peter Zijlstra   perf_events: Disa...
488

04289bb98   Ingo Molnar   perf counters: ad...
489
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
490
491
  	 * If this was a group event with sibling events then
  	 * upgrade the siblings to singleton events by adding them
8a49542c0   Peter Zijlstra   perf_events: Fix ...
492
  	 * to whatever list we are on.
04289bb98   Ingo Molnar   perf counters: ad...
493
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
494
  	list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
8a49542c0   Peter Zijlstra   perf_events: Fix ...
495
496
  		if (list)
  			list_move_tail(&sibling->group_entry, list);
04289bb98   Ingo Molnar   perf counters: ad...
497
  		sibling->group_leader = sibling;
d6f962b57   Frederic Weisbecker   perf: Export soft...
498
499
500
  
  		/* Inherit group flags from the previous leader */
  		sibling->group_flags = event->group_flags;
04289bb98   Ingo Molnar   perf counters: ad...
501
  	}
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
502
503
504
505
506
507
  
  out:
  	perf_event__header_size(event->group_leader);
  
  	list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  		perf_event__header_size(tmp);
04289bb98   Ingo Molnar   perf counters: ad...
508
  }
fa66f07aa   Stephane Eranian   perf_events: Fix ...
509
510
511
512
513
  static inline int
  event_filter_match(struct perf_event *event)
  {
  	return event->cpu == -1 || event->cpu == smp_processor_id();
  }
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
514
515
  static void
  event_sched_out(struct perf_event *event,
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
516
  		  struct perf_cpu_context *cpuctx,
cdd6c482c   Ingo Molnar   perf: Do the big ...
517
  		  struct perf_event_context *ctx)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
518
  {
4158755d3   Stephane Eranian   perf_events: Add ...
519
  	u64 tstamp = perf_event_time(event);
fa66f07aa   Stephane Eranian   perf_events: Fix ...
520
521
522
523
524
525
526
527
528
529
530
  	u64 delta;
  	/*
  	 * An event which could not be activated because of
  	 * filter mismatch still needs to have its timings
  	 * maintained, otherwise bogus information is return
  	 * via read() for time_enabled, time_running:
  	 */
  	if (event->state == PERF_EVENT_STATE_INACTIVE
  	    && !event_filter_match(event)) {
  		delta = ctx->time - event->tstamp_stopped;
  		event->tstamp_running += delta;
4158755d3   Stephane Eranian   perf_events: Add ...
531
  		event->tstamp_stopped = tstamp;
fa66f07aa   Stephane Eranian   perf_events: Fix ...
532
  	}
cdd6c482c   Ingo Molnar   perf: Do the big ...
533
  	if (event->state != PERF_EVENT_STATE_ACTIVE)
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
534
  		return;
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
535

cdd6c482c   Ingo Molnar   perf: Do the big ...
536
537
538
539
  	event->state = PERF_EVENT_STATE_INACTIVE;
  	if (event->pending_disable) {
  		event->pending_disable = 0;
  		event->state = PERF_EVENT_STATE_OFF;
970892a90   Peter Zijlstra   perf_counter: Fix...
540
  	}
4158755d3   Stephane Eranian   perf_events: Add ...
541
  	event->tstamp_stopped = tstamp;
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
542
  	event->pmu->del(event, 0);
cdd6c482c   Ingo Molnar   perf: Do the big ...
543
  	event->oncpu = -1;
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
544

cdd6c482c   Ingo Molnar   perf: Do the big ...
545
  	if (!is_software_event(event))
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
546
547
  		cpuctx->active_oncpu--;
  	ctx->nr_active--;
cdd6c482c   Ingo Molnar   perf: Do the big ...
548
  	if (event->attr.exclusive || !cpuctx->active_oncpu)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
549
550
  		cpuctx->exclusive = 0;
  }
d859e29fe   Paul Mackerras   perf_counter: Add...
551
  static void
cdd6c482c   Ingo Molnar   perf: Do the big ...
552
  group_sched_out(struct perf_event *group_event,
d859e29fe   Paul Mackerras   perf_counter: Add...
553
  		struct perf_cpu_context *cpuctx,
cdd6c482c   Ingo Molnar   perf: Do the big ...
554
  		struct perf_event_context *ctx)
d859e29fe   Paul Mackerras   perf_counter: Add...
555
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
556
  	struct perf_event *event;
fa66f07aa   Stephane Eranian   perf_events: Fix ...
557
  	int state = group_event->state;
d859e29fe   Paul Mackerras   perf_counter: Add...
558

cdd6c482c   Ingo Molnar   perf: Do the big ...
559
  	event_sched_out(group_event, cpuctx, ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
560
561
562
563
  
  	/*
  	 * Schedule out siblings (if any):
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
564
565
  	list_for_each_entry(event, &group_event->sibling_list, group_entry)
  		event_sched_out(event, cpuctx, ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
566

fa66f07aa   Stephane Eranian   perf_events: Fix ...
567
  	if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
d859e29fe   Paul Mackerras   perf_counter: Add...
568
569
  		cpuctx->exclusive = 0;
  }
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
570
571
572
573
574
  static inline struct perf_cpu_context *
  __get_cpu_context(struct perf_event_context *ctx)
  {
  	return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  }
0793a61d4   Thomas Gleixner   performance count...
575
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
576
   * Cross CPU call to remove a performance event
0793a61d4   Thomas Gleixner   performance count...
577
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
578
   * We disable the event on the hardware level first. After that we
0793a61d4   Thomas Gleixner   performance count...
579
580
   * remove it from the context list.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
581
  static void __perf_event_remove_from_context(void *info)
0793a61d4   Thomas Gleixner   performance count...
582
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
583
584
  	struct perf_event *event = info;
  	struct perf_event_context *ctx = event->ctx;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
585
  	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
0793a61d4   Thomas Gleixner   performance count...
586
587
588
589
590
591
  
  	/*
  	 * If this is a task context, we need to check whether it is
  	 * the current task context of this cpu. If not it has been
  	 * scheduled out before the smp call arrived.
  	 */
665c2142a   Peter Zijlstra   perf_counter: Cle...
592
  	if (ctx->task && cpuctx->task_ctx != ctx)
0793a61d4   Thomas Gleixner   performance count...
593
  		return;
e625cce1b   Thomas Gleixner   perf_event: Conve...
594
  	raw_spin_lock(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
595

cdd6c482c   Ingo Molnar   perf: Do the big ...
596
  	event_sched_out(event, cpuctx, ctx);
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
597

cdd6c482c   Ingo Molnar   perf: Do the big ...
598
  	list_del_event(event, ctx);
0793a61d4   Thomas Gleixner   performance count...
599

e625cce1b   Thomas Gleixner   perf_event: Conve...
600
  	raw_spin_unlock(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
601
602
603
604
  }
  
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
605
   * Remove the event from a task's (or a CPU's) list of events.
0793a61d4   Thomas Gleixner   performance count...
606
   *
fccc714b3   Peter Zijlstra   perf_counter: San...
607
   * Must be called with ctx->mutex held.
0793a61d4   Thomas Gleixner   performance count...
608
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
609
   * CPU events are removed with a smp call. For task events we only
0793a61d4   Thomas Gleixner   performance count...
610
   * call when the task is on a CPU.
c93f76690   Paul Mackerras   perf_counter: Fix...
611
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
612
613
   * If event->ctx is a cloned context, callers must make sure that
   * every task struct that event->ctx->task could possibly point to
c93f76690   Paul Mackerras   perf_counter: Fix...
614
615
   * remains valid.  This is OK when called from perf_release since
   * that only calls us on the top-level context, which can't be a clone.
cdd6c482c   Ingo Molnar   perf: Do the big ...
616
   * When called from perf_event_exit_task, it's OK because the
c93f76690   Paul Mackerras   perf_counter: Fix...
617
   * context has been detached from its task.
0793a61d4   Thomas Gleixner   performance count...
618
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
619
  static void perf_event_remove_from_context(struct perf_event *event)
0793a61d4   Thomas Gleixner   performance count...
620
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
621
  	struct perf_event_context *ctx = event->ctx;
0793a61d4   Thomas Gleixner   performance count...
622
623
624
625
  	struct task_struct *task = ctx->task;
  
  	if (!task) {
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
626
  		 * Per cpu events are removed via an smp call and
af901ca18   André Goddard Rosa   tree-wide: fix as...
627
  		 * the removal is always successful.
0793a61d4   Thomas Gleixner   performance count...
628
  		 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
629
630
631
  		smp_call_function_single(event->cpu,
  					 __perf_event_remove_from_context,
  					 event, 1);
0793a61d4   Thomas Gleixner   performance count...
632
633
634
635
  		return;
  	}
  
  retry:
cdd6c482c   Ingo Molnar   perf: Do the big ...
636
637
  	task_oncpu_function_call(task, __perf_event_remove_from_context,
  				 event);
0793a61d4   Thomas Gleixner   performance count...
638

e625cce1b   Thomas Gleixner   perf_event: Conve...
639
  	raw_spin_lock_irq(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
640
641
642
  	/*
  	 * If the context is active we need to retry the smp call.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
643
  	if (ctx->nr_active && !list_empty(&event->group_entry)) {
e625cce1b   Thomas Gleixner   perf_event: Conve...
644
  		raw_spin_unlock_irq(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
645
646
647
648
649
  		goto retry;
  	}
  
  	/*
  	 * The lock prevents that this context is scheduled in so we
cdd6c482c   Ingo Molnar   perf: Do the big ...
650
  	 * can remove the event safely, if the call above did not
0793a61d4   Thomas Gleixner   performance count...
651
652
  	 * succeed.
  	 */
6c2bfcbe5   Peter Zijlstra   perf_events: Fix ...
653
  	if (!list_empty(&event->group_entry))
cdd6c482c   Ingo Molnar   perf: Do the big ...
654
  		list_del_event(event, ctx);
e625cce1b   Thomas Gleixner   perf_event: Conve...
655
  	raw_spin_unlock_irq(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
656
  }
53cfbf593   Paul Mackerras   perf_counter: rec...
657
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
658
   * Cross CPU call to disable a performance event
d859e29fe   Paul Mackerras   perf_counter: Add...
659
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
660
  static void __perf_event_disable(void *info)
d859e29fe   Paul Mackerras   perf_counter: Add...
661
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
662
  	struct perf_event *event = info;
cdd6c482c   Ingo Molnar   perf: Do the big ...
663
  	struct perf_event_context *ctx = event->ctx;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
664
  	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
665
666
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
667
668
  	 * If this is a per-task event, need to check whether this
  	 * event's task is the current task on this cpu.
d859e29fe   Paul Mackerras   perf_counter: Add...
669
  	 */
665c2142a   Peter Zijlstra   perf_counter: Cle...
670
  	if (ctx->task && cpuctx->task_ctx != ctx)
d859e29fe   Paul Mackerras   perf_counter: Add...
671
  		return;
e625cce1b   Thomas Gleixner   perf_event: Conve...
672
  	raw_spin_lock(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
673
674
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
675
  	 * If the event is on, turn it off.
d859e29fe   Paul Mackerras   perf_counter: Add...
676
677
  	 * If it is in error state, leave it in error state.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
678
  	if (event->state >= PERF_EVENT_STATE_INACTIVE) {
4af4998b8   Peter Zijlstra   perf_counter: rew...
679
  		update_context_time(ctx);
cdd6c482c   Ingo Molnar   perf: Do the big ...
680
681
682
  		update_group_times(event);
  		if (event == event->group_leader)
  			group_sched_out(event, cpuctx, ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
683
  		else
cdd6c482c   Ingo Molnar   perf: Do the big ...
684
685
  			event_sched_out(event, cpuctx, ctx);
  		event->state = PERF_EVENT_STATE_OFF;
d859e29fe   Paul Mackerras   perf_counter: Add...
686
  	}
e625cce1b   Thomas Gleixner   perf_event: Conve...
687
  	raw_spin_unlock(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
688
689
690
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
691
   * Disable a event.
c93f76690   Paul Mackerras   perf_counter: Fix...
692
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
693
694
   * If event->ctx is a cloned context, callers must make sure that
   * every task struct that event->ctx->task could possibly point to
c93f76690   Paul Mackerras   perf_counter: Fix...
695
   * remains valid.  This condition is satisifed when called through
cdd6c482c   Ingo Molnar   perf: Do the big ...
696
697
698
699
   * perf_event_for_each_child or perf_event_for_each because they
   * hold the top-level event's child_mutex, so any descendant that
   * goes to exit will block in sync_child_event.
   * When called from perf_pending_event it's OK because event->ctx
c93f76690   Paul Mackerras   perf_counter: Fix...
700
   * is the current context on this CPU and preemption is disabled,
cdd6c482c   Ingo Molnar   perf: Do the big ...
701
   * hence we can't get into perf_event_task_sched_out for this context.
d859e29fe   Paul Mackerras   perf_counter: Add...
702
   */
44234adcd   Frederic Weisbecker   hw-breakpoints: M...
703
  void perf_event_disable(struct perf_event *event)
d859e29fe   Paul Mackerras   perf_counter: Add...
704
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
705
  	struct perf_event_context *ctx = event->ctx;
d859e29fe   Paul Mackerras   perf_counter: Add...
706
707
708
709
  	struct task_struct *task = ctx->task;
  
  	if (!task) {
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
710
  		 * Disable the event on the cpu that it's on
d859e29fe   Paul Mackerras   perf_counter: Add...
711
  		 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
712
713
  		smp_call_function_single(event->cpu, __perf_event_disable,
  					 event, 1);
d859e29fe   Paul Mackerras   perf_counter: Add...
714
715
  		return;
  	}
9ed6060d2   Peter Zijlstra   perf: Unindent la...
716
  retry:
cdd6c482c   Ingo Molnar   perf: Do the big ...
717
  	task_oncpu_function_call(task, __perf_event_disable, event);
d859e29fe   Paul Mackerras   perf_counter: Add...
718

e625cce1b   Thomas Gleixner   perf_event: Conve...
719
  	raw_spin_lock_irq(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
720
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
721
  	 * If the event is still active, we need to retry the cross-call.
d859e29fe   Paul Mackerras   perf_counter: Add...
722
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
723
  	if (event->state == PERF_EVENT_STATE_ACTIVE) {
e625cce1b   Thomas Gleixner   perf_event: Conve...
724
  		raw_spin_unlock_irq(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
725
726
727
728
729
730
731
  		goto retry;
  	}
  
  	/*
  	 * Since we have the lock this context can't be scheduled
  	 * in, so we can change the state safely.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
732
733
734
  	if (event->state == PERF_EVENT_STATE_INACTIVE) {
  		update_group_times(event);
  		event->state = PERF_EVENT_STATE_OFF;
53cfbf593   Paul Mackerras   perf_counter: rec...
735
  	}
d859e29fe   Paul Mackerras   perf_counter: Add...
736

e625cce1b   Thomas Gleixner   perf_event: Conve...
737
  	raw_spin_unlock_irq(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
738
  }
235c7fc7c   Ingo Molnar   perfcounters: gen...
739
  static int
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
740
  event_sched_in(struct perf_event *event,
235c7fc7c   Ingo Molnar   perfcounters: gen...
741
  		 struct perf_cpu_context *cpuctx,
6e37738a2   Peter Zijlstra   perf_events: Simp...
742
  		 struct perf_event_context *ctx)
235c7fc7c   Ingo Molnar   perfcounters: gen...
743
  {
4158755d3   Stephane Eranian   perf_events: Add ...
744
  	u64 tstamp = perf_event_time(event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
745
  	if (event->state <= PERF_EVENT_STATE_OFF)
235c7fc7c   Ingo Molnar   perfcounters: gen...
746
  		return 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
747
  	event->state = PERF_EVENT_STATE_ACTIVE;
6e37738a2   Peter Zijlstra   perf_events: Simp...
748
  	event->oncpu = smp_processor_id();
235c7fc7c   Ingo Molnar   perfcounters: gen...
749
750
751
752
  	/*
  	 * The new state must be visible before we turn it on in the hardware:
  	 */
  	smp_wmb();
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
753
  	if (event->pmu->add(event, PERF_EF_START)) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
754
755
  		event->state = PERF_EVENT_STATE_INACTIVE;
  		event->oncpu = -1;
235c7fc7c   Ingo Molnar   perfcounters: gen...
756
757
  		return -EAGAIN;
  	}
4158755d3   Stephane Eranian   perf_events: Add ...
758
  	event->tstamp_running += tstamp - event->tstamp_stopped;
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
759

4158755d3   Stephane Eranian   perf_events: Add ...
760
  	event->shadow_ctx_time = tstamp - ctx->timestamp;
eed01528a   Stephane Eranian   perf_events: Fix ...
761

cdd6c482c   Ingo Molnar   perf: Do the big ...
762
  	if (!is_software_event(event))
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
763
  		cpuctx->active_oncpu++;
235c7fc7c   Ingo Molnar   perfcounters: gen...
764
  	ctx->nr_active++;
cdd6c482c   Ingo Molnar   perf: Do the big ...
765
  	if (event->attr.exclusive)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
766
  		cpuctx->exclusive = 1;
235c7fc7c   Ingo Molnar   perfcounters: gen...
767
768
  	return 0;
  }
6751b71ea   Paul Mackerras   perf_counter: Put...
769
  static int
cdd6c482c   Ingo Molnar   perf: Do the big ...
770
  group_sched_in(struct perf_event *group_event,
6751b71ea   Paul Mackerras   perf_counter: Put...
771
  	       struct perf_cpu_context *cpuctx,
6e37738a2   Peter Zijlstra   perf_events: Simp...
772
  	       struct perf_event_context *ctx)
6751b71ea   Paul Mackerras   perf_counter: Put...
773
  {
6bde9b6ce   Lin Ming   perf: Add group s...
774
  	struct perf_event *event, *partial_group = NULL;
51b0fe395   Peter Zijlstra   perf: Deconstify ...
775
  	struct pmu *pmu = group_event->pmu;
d7842da47   Stephane Eranian   perf_events: Fix ...
776
777
  	u64 now = ctx->time;
  	bool simulate = false;
6751b71ea   Paul Mackerras   perf_counter: Put...
778

cdd6c482c   Ingo Molnar   perf: Do the big ...
779
  	if (group_event->state == PERF_EVENT_STATE_OFF)
6751b71ea   Paul Mackerras   perf_counter: Put...
780
  		return 0;
ad5133b70   Peter Zijlstra   perf: Default PMU...
781
  	pmu->start_txn(pmu);
6bde9b6ce   Lin Ming   perf: Add group s...
782

9ffcfa6f1   Stephane Eranian   perf_events: Reve...
783
  	if (event_sched_in(group_event, cpuctx, ctx)) {
ad5133b70   Peter Zijlstra   perf: Default PMU...
784
  		pmu->cancel_txn(pmu);
6751b71ea   Paul Mackerras   perf_counter: Put...
785
  		return -EAGAIN;
90151c35b   Stephane Eranian   perf_events: Fix ...
786
  	}
6751b71ea   Paul Mackerras   perf_counter: Put...
787
788
789
790
  
  	/*
  	 * Schedule in siblings as one group (if any):
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
791
  	list_for_each_entry(event, &group_event->sibling_list, group_entry) {
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
792
  		if (event_sched_in(event, cpuctx, ctx)) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
793
  			partial_group = event;
6751b71ea   Paul Mackerras   perf_counter: Put...
794
795
796
  			goto group_error;
  		}
  	}
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
797
  	if (!pmu->commit_txn(pmu))
6e85158cf   Paul Mackerras   perf_event: Make ...
798
  		return 0;
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
799

6751b71ea   Paul Mackerras   perf_counter: Put...
800
801
802
803
  group_error:
  	/*
  	 * Groups can be scheduled in as one unit only, so undo any
  	 * partial group before returning:
d7842da47   Stephane Eranian   perf_events: Fix ...
804
805
806
807
808
809
810
811
812
813
  	 * The events up to the failed event are scheduled out normally,
  	 * tstamp_stopped will be updated.
  	 *
  	 * The failed events and the remaining siblings need to have
  	 * their timings updated as if they had gone thru event_sched_in()
  	 * and event_sched_out(). This is required to get consistent timings
  	 * across the group. This also takes care of the case where the group
  	 * could never be scheduled by ensuring tstamp_stopped is set to mark
  	 * the time the event was actually stopped, such that time delta
  	 * calculation in update_event_times() is correct.
6751b71ea   Paul Mackerras   perf_counter: Put...
814
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
815
816
  	list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  		if (event == partial_group)
d7842da47   Stephane Eranian   perf_events: Fix ...
817
818
819
820
821
822
823
824
  			simulate = true;
  
  		if (simulate) {
  			event->tstamp_running += now - event->tstamp_stopped;
  			event->tstamp_stopped = now;
  		} else {
  			event_sched_out(event, cpuctx, ctx);
  		}
6751b71ea   Paul Mackerras   perf_counter: Put...
825
  	}
9ffcfa6f1   Stephane Eranian   perf_events: Reve...
826
  	event_sched_out(group_event, cpuctx, ctx);
6751b71ea   Paul Mackerras   perf_counter: Put...
827

ad5133b70   Peter Zijlstra   perf: Default PMU...
828
  	pmu->cancel_txn(pmu);
90151c35b   Stephane Eranian   perf_events: Fix ...
829

6751b71ea   Paul Mackerras   perf_counter: Put...
830
831
  	return -EAGAIN;
  }
0793a61d4   Thomas Gleixner   performance count...
832
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
833
   * Work out whether we can put this event group on the CPU now.
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
834
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
835
  static int group_can_go_on(struct perf_event *event,
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
836
837
838
839
  			   struct perf_cpu_context *cpuctx,
  			   int can_add_hw)
  {
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
840
  	 * Groups consisting entirely of software events can always go on.
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
841
  	 */
d6f962b57   Frederic Weisbecker   perf: Export soft...
842
  	if (event->group_flags & PERF_GROUP_SOFTWARE)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
843
844
845
  		return 1;
  	/*
  	 * If an exclusive group is already on, no other hardware
cdd6c482c   Ingo Molnar   perf: Do the big ...
846
  	 * events can go on.
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
847
848
849
850
851
  	 */
  	if (cpuctx->exclusive)
  		return 0;
  	/*
  	 * If this group is exclusive and there are already
cdd6c482c   Ingo Molnar   perf: Do the big ...
852
  	 * events on the CPU, it can't go on.
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
853
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
854
  	if (event->attr.exclusive && cpuctx->active_oncpu)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
855
856
857
858
859
860
861
  		return 0;
  	/*
  	 * Otherwise, try to add it if all previous groups were able
  	 * to go on.
  	 */
  	return can_add_hw;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
862
863
  static void add_event_to_ctx(struct perf_event *event,
  			       struct perf_event_context *ctx)
53cfbf593   Paul Mackerras   perf_counter: rec...
864
  {
4158755d3   Stephane Eranian   perf_events: Add ...
865
  	u64 tstamp = perf_event_time(event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
866
  	list_add_event(event, ctx);
8a49542c0   Peter Zijlstra   perf_events: Fix ...
867
  	perf_group_attach(event);
4158755d3   Stephane Eranian   perf_events: Add ...
868
869
870
  	event->tstamp_enabled = tstamp;
  	event->tstamp_running = tstamp;
  	event->tstamp_stopped = tstamp;
53cfbf593   Paul Mackerras   perf_counter: rec...
871
  }
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
872
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
873
   * Cross CPU call to install and enable a performance event
682076ae1   Peter Zijlstra   perf_counter: San...
874
875
   *
   * Must be called with ctx->mutex held
0793a61d4   Thomas Gleixner   performance count...
876
877
878
   */
  static void __perf_install_in_context(void *info)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
879
880
881
  	struct perf_event *event = info;
  	struct perf_event_context *ctx = event->ctx;
  	struct perf_event *leader = event->group_leader;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
882
  	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
883
  	int err;
0793a61d4   Thomas Gleixner   performance count...
884
885
886
887
888
  
  	/*
  	 * If this is a task context, we need to check whether it is
  	 * the current task context of this cpu. If not it has been
  	 * scheduled out before the smp call arrived.
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
889
  	 * Or possibly this is the right context but it isn't
cdd6c482c   Ingo Molnar   perf: Do the big ...
890
  	 * on this cpu because it had no events.
0793a61d4   Thomas Gleixner   performance count...
891
  	 */
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
892
  	if (ctx->task && cpuctx->task_ctx != ctx) {
665c2142a   Peter Zijlstra   perf_counter: Cle...
893
  		if (cpuctx->task_ctx || ctx->task != current)
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
894
895
896
  			return;
  		cpuctx->task_ctx = ctx;
  	}
0793a61d4   Thomas Gleixner   performance count...
897

e625cce1b   Thomas Gleixner   perf_event: Conve...
898
  	raw_spin_lock(&ctx->lock);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
899
  	ctx->is_active = 1;
4af4998b8   Peter Zijlstra   perf_counter: rew...
900
  	update_context_time(ctx);
0793a61d4   Thomas Gleixner   performance count...
901

cdd6c482c   Ingo Molnar   perf: Do the big ...
902
  	add_event_to_ctx(event, ctx);
0793a61d4   Thomas Gleixner   performance count...
903

5632ab12e   Stephane Eranian   perf_events: Gene...
904
  	if (!event_filter_match(event))
f4c4176f2   Peter Zijlstra   perf events: Allo...
905
  		goto unlock;
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
906
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
907
  	 * Don't put the event on if it is disabled or if
d859e29fe   Paul Mackerras   perf_counter: Add...
908
909
  	 * it is in a group and the group isn't on.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
910
911
  	if (event->state != PERF_EVENT_STATE_INACTIVE ||
  	    (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
d859e29fe   Paul Mackerras   perf_counter: Add...
912
913
914
  		goto unlock;
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
915
916
917
  	 * An exclusive event can't go on if there are already active
  	 * hardware events, and no hardware event can go on if there
  	 * is already an exclusive event on.
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
918
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
919
  	if (!group_can_go_on(event, cpuctx, 1))
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
920
921
  		err = -EEXIST;
  	else
6e37738a2   Peter Zijlstra   perf_events: Simp...
922
  		err = event_sched_in(event, cpuctx, ctx);
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
923

d859e29fe   Paul Mackerras   perf_counter: Add...
924
925
  	if (err) {
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
926
  		 * This event couldn't go on.  If it is in a group
d859e29fe   Paul Mackerras   perf_counter: Add...
927
  		 * then we have to pull the whole group off.
cdd6c482c   Ingo Molnar   perf: Do the big ...
928
  		 * If the event group is pinned then put it in error state.
d859e29fe   Paul Mackerras   perf_counter: Add...
929
  		 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
930
  		if (leader != event)
d859e29fe   Paul Mackerras   perf_counter: Add...
931
  			group_sched_out(leader, cpuctx, ctx);
0d48696f8   Peter Zijlstra   perf_counter: Ren...
932
  		if (leader->attr.pinned) {
53cfbf593   Paul Mackerras   perf_counter: rec...
933
  			update_group_times(leader);
cdd6c482c   Ingo Molnar   perf: Do the big ...
934
  			leader->state = PERF_EVENT_STATE_ERROR;
53cfbf593   Paul Mackerras   perf_counter: rec...
935
  		}
d859e29fe   Paul Mackerras   perf_counter: Add...
936
  	}
0793a61d4   Thomas Gleixner   performance count...
937

9ed6060d2   Peter Zijlstra   perf: Unindent la...
938
  unlock:
e625cce1b   Thomas Gleixner   perf_event: Conve...
939
  	raw_spin_unlock(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
940
941
942
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
943
   * Attach a performance event to a context
0793a61d4   Thomas Gleixner   performance count...
944
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
945
946
   * First we add the event to the list with the hardware enable bit
   * in event->hw_config cleared.
0793a61d4   Thomas Gleixner   performance count...
947
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
948
   * If the event is attached to a task which is on a CPU we use a smp
0793a61d4   Thomas Gleixner   performance count...
949
950
   * call to enable it in the task context. The task might have been
   * scheduled away, but we check this in the smp call again.
d859e29fe   Paul Mackerras   perf_counter: Add...
951
952
   *
   * Must be called with ctx->mutex held.
0793a61d4   Thomas Gleixner   performance count...
953
954
   */
  static void
cdd6c482c   Ingo Molnar   perf: Do the big ...
955
956
  perf_install_in_context(struct perf_event_context *ctx,
  			struct perf_event *event,
0793a61d4   Thomas Gleixner   performance count...
957
958
959
  			int cpu)
  {
  	struct task_struct *task = ctx->task;
c3f00c702   Peter Zijlstra   perf: Separate fi...
960
  	event->ctx = ctx;
0793a61d4   Thomas Gleixner   performance count...
961
962
  	if (!task) {
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
963
  		 * Per cpu events are installed via an smp call and
af901ca18   André Goddard Rosa   tree-wide: fix as...
964
  		 * the install is always successful.
0793a61d4   Thomas Gleixner   performance count...
965
966
  		 */
  		smp_call_function_single(cpu, __perf_install_in_context,
cdd6c482c   Ingo Molnar   perf: Do the big ...
967
  					 event, 1);
0793a61d4   Thomas Gleixner   performance count...
968
969
  		return;
  	}
0793a61d4   Thomas Gleixner   performance count...
970
971
  retry:
  	task_oncpu_function_call(task, __perf_install_in_context,
cdd6c482c   Ingo Molnar   perf: Do the big ...
972
  				 event);
0793a61d4   Thomas Gleixner   performance count...
973

e625cce1b   Thomas Gleixner   perf_event: Conve...
974
  	raw_spin_lock_irq(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
975
  	/*
0793a61d4   Thomas Gleixner   performance count...
976
977
  	 * we need to retry the smp call.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
978
  	if (ctx->is_active && list_empty(&event->group_entry)) {
e625cce1b   Thomas Gleixner   perf_event: Conve...
979
  		raw_spin_unlock_irq(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
980
981
982
983
984
  		goto retry;
  	}
  
  	/*
  	 * The lock prevents that this context is scheduled in so we
cdd6c482c   Ingo Molnar   perf: Do the big ...
985
  	 * can add the event safely, if it the call above did not
0793a61d4   Thomas Gleixner   performance count...
986
987
  	 * succeed.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
988
989
  	if (list_empty(&event->group_entry))
  		add_event_to_ctx(event, ctx);
e625cce1b   Thomas Gleixner   perf_event: Conve...
990
  	raw_spin_unlock_irq(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
991
  }
d859e29fe   Paul Mackerras   perf_counter: Add...
992
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
993
   * Put a event into inactive state and update time fields.
fa289beca   Paul Mackerras   perf_counter: Sta...
994
995
996
997
998
999
   * Enabling the leader of a group effectively enables all
   * the group members that aren't explicitly disabled, so we
   * have to update their ->tstamp_enabled also.
   * Note: this works for group members as well as group leaders
   * since the non-leader members' sibling_lists will be empty.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1000
1001
  static void __perf_event_mark_enabled(struct perf_event *event,
  					struct perf_event_context *ctx)
fa289beca   Paul Mackerras   perf_counter: Sta...
1002
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1003
  	struct perf_event *sub;
4158755d3   Stephane Eranian   perf_events: Add ...
1004
  	u64 tstamp = perf_event_time(event);
fa289beca   Paul Mackerras   perf_counter: Sta...
1005

cdd6c482c   Ingo Molnar   perf: Do the big ...
1006
  	event->state = PERF_EVENT_STATE_INACTIVE;
4158755d3   Stephane Eranian   perf_events: Add ...
1007
  	event->tstamp_enabled = tstamp - event->total_time_enabled;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1008
  	list_for_each_entry(sub, &event->sibling_list, group_entry) {
4158755d3   Stephane Eranian   perf_events: Add ...
1009
1010
  		if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  			sub->tstamp_enabled = tstamp - sub->total_time_enabled;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1011
  	}
fa289beca   Paul Mackerras   perf_counter: Sta...
1012
1013
1014
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1015
   * Cross CPU call to enable a performance event
d859e29fe   Paul Mackerras   perf_counter: Add...
1016
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1017
  static void __perf_event_enable(void *info)
04289bb98   Ingo Molnar   perf counters: ad...
1018
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1019
  	struct perf_event *event = info;
cdd6c482c   Ingo Molnar   perf: Do the big ...
1020
1021
  	struct perf_event_context *ctx = event->ctx;
  	struct perf_event *leader = event->group_leader;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1022
  	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
1023
  	int err;
04289bb98   Ingo Molnar   perf counters: ad...
1024

d859e29fe   Paul Mackerras   perf_counter: Add...
1025
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1026
1027
  	 * If this is a per-task event, need to check whether this
  	 * event's task is the current task on this cpu.
d859e29fe   Paul Mackerras   perf_counter: Add...
1028
  	 */
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1029
  	if (ctx->task && cpuctx->task_ctx != ctx) {
665c2142a   Peter Zijlstra   perf_counter: Cle...
1030
  		if (cpuctx->task_ctx || ctx->task != current)
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1031
1032
1033
  			return;
  		cpuctx->task_ctx = ctx;
  	}
3cbed429a   Paul Mackerras   perf_counter: Add...
1034

e625cce1b   Thomas Gleixner   perf_event: Conve...
1035
  	raw_spin_lock(&ctx->lock);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1036
  	ctx->is_active = 1;
4af4998b8   Peter Zijlstra   perf_counter: rew...
1037
  	update_context_time(ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
1038

cdd6c482c   Ingo Molnar   perf: Do the big ...
1039
  	if (event->state >= PERF_EVENT_STATE_INACTIVE)
d859e29fe   Paul Mackerras   perf_counter: Add...
1040
  		goto unlock;
cdd6c482c   Ingo Molnar   perf: Do the big ...
1041
  	__perf_event_mark_enabled(event, ctx);
04289bb98   Ingo Molnar   perf counters: ad...
1042

5632ab12e   Stephane Eranian   perf_events: Gene...
1043
  	if (!event_filter_match(event))
f4c4176f2   Peter Zijlstra   perf events: Allo...
1044
  		goto unlock;
04289bb98   Ingo Molnar   perf counters: ad...
1045
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1046
  	 * If the event is in a group and isn't the group leader,
d859e29fe   Paul Mackerras   perf_counter: Add...
1047
  	 * then don't put it on unless the group is on.
04289bb98   Ingo Molnar   perf counters: ad...
1048
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1049
  	if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
d859e29fe   Paul Mackerras   perf_counter: Add...
1050
  		goto unlock;
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1051

cdd6c482c   Ingo Molnar   perf: Do the big ...
1052
  	if (!group_can_go_on(event, cpuctx, 1)) {
d859e29fe   Paul Mackerras   perf_counter: Add...
1053
  		err = -EEXIST;
e758a33d6   Paul Mackerras   perf_counter: cal...
1054
  	} else {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1055
  		if (event == leader)
6e37738a2   Peter Zijlstra   perf_events: Simp...
1056
  			err = group_sched_in(event, cpuctx, ctx);
e758a33d6   Paul Mackerras   perf_counter: cal...
1057
  		else
6e37738a2   Peter Zijlstra   perf_events: Simp...
1058
  			err = event_sched_in(event, cpuctx, ctx);
e758a33d6   Paul Mackerras   perf_counter: cal...
1059
  	}
d859e29fe   Paul Mackerras   perf_counter: Add...
1060
1061
1062
  
  	if (err) {
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1063
  		 * If this event can't go on and it's part of a
d859e29fe   Paul Mackerras   perf_counter: Add...
1064
1065
  		 * group, then the whole group has to come off.
  		 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1066
  		if (leader != event)
d859e29fe   Paul Mackerras   perf_counter: Add...
1067
  			group_sched_out(leader, cpuctx, ctx);
0d48696f8   Peter Zijlstra   perf_counter: Ren...
1068
  		if (leader->attr.pinned) {
53cfbf593   Paul Mackerras   perf_counter: rec...
1069
  			update_group_times(leader);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1070
  			leader->state = PERF_EVENT_STATE_ERROR;
53cfbf593   Paul Mackerras   perf_counter: rec...
1071
  		}
d859e29fe   Paul Mackerras   perf_counter: Add...
1072
  	}
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1073
  unlock:
e625cce1b   Thomas Gleixner   perf_event: Conve...
1074
  	raw_spin_unlock(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
1075
1076
1077
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1078
   * Enable a event.
c93f76690   Paul Mackerras   perf_counter: Fix...
1079
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
1080
1081
   * If event->ctx is a cloned context, callers must make sure that
   * every task struct that event->ctx->task could possibly point to
c93f76690   Paul Mackerras   perf_counter: Fix...
1082
   * remains valid.  This condition is satisfied when called through
cdd6c482c   Ingo Molnar   perf: Do the big ...
1083
1084
   * perf_event_for_each_child or perf_event_for_each as described
   * for perf_event_disable.
d859e29fe   Paul Mackerras   perf_counter: Add...
1085
   */
44234adcd   Frederic Weisbecker   hw-breakpoints: M...
1086
  void perf_event_enable(struct perf_event *event)
d859e29fe   Paul Mackerras   perf_counter: Add...
1087
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1088
  	struct perf_event_context *ctx = event->ctx;
d859e29fe   Paul Mackerras   perf_counter: Add...
1089
1090
1091
1092
  	struct task_struct *task = ctx->task;
  
  	if (!task) {
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1093
  		 * Enable the event on the cpu that it's on
d859e29fe   Paul Mackerras   perf_counter: Add...
1094
  		 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1095
1096
  		smp_call_function_single(event->cpu, __perf_event_enable,
  					 event, 1);
d859e29fe   Paul Mackerras   perf_counter: Add...
1097
1098
  		return;
  	}
e625cce1b   Thomas Gleixner   perf_event: Conve...
1099
  	raw_spin_lock_irq(&ctx->lock);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1100
  	if (event->state >= PERF_EVENT_STATE_INACTIVE)
d859e29fe   Paul Mackerras   perf_counter: Add...
1101
1102
1103
  		goto out;
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1104
1105
  	 * If the event is in error state, clear that first.
  	 * That way, if we see the event in error state below, we
d859e29fe   Paul Mackerras   perf_counter: Add...
1106
1107
1108
1109
  	 * know that it has gone back into error state, as distinct
  	 * from the task having been scheduled away before the
  	 * cross-call arrived.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1110
1111
  	if (event->state == PERF_EVENT_STATE_ERROR)
  		event->state = PERF_EVENT_STATE_OFF;
d859e29fe   Paul Mackerras   perf_counter: Add...
1112

9ed6060d2   Peter Zijlstra   perf: Unindent la...
1113
  retry:
e625cce1b   Thomas Gleixner   perf_event: Conve...
1114
  	raw_spin_unlock_irq(&ctx->lock);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1115
  	task_oncpu_function_call(task, __perf_event_enable, event);
d859e29fe   Paul Mackerras   perf_counter: Add...
1116

e625cce1b   Thomas Gleixner   perf_event: Conve...
1117
  	raw_spin_lock_irq(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
1118
1119
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1120
  	 * If the context is active and the event is still off,
d859e29fe   Paul Mackerras   perf_counter: Add...
1121
1122
  	 * we need to retry the cross-call.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1123
  	if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
d859e29fe   Paul Mackerras   perf_counter: Add...
1124
1125
1126
1127
1128
1129
  		goto retry;
  
  	/*
  	 * Since we have the lock this context can't be scheduled
  	 * in, so we can change the state safely.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1130
1131
  	if (event->state == PERF_EVENT_STATE_OFF)
  		__perf_event_mark_enabled(event, ctx);
fa289beca   Paul Mackerras   perf_counter: Sta...
1132

9ed6060d2   Peter Zijlstra   perf: Unindent la...
1133
  out:
e625cce1b   Thomas Gleixner   perf_event: Conve...
1134
  	raw_spin_unlock_irq(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
1135
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
1136
  static int perf_event_refresh(struct perf_event *event, int refresh)
79f146415   Peter Zijlstra   perf_counter: cou...
1137
  {
2023b3592   Peter Zijlstra   perf_counter: inh...
1138
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1139
  	 * not supported on inherited events
2023b3592   Peter Zijlstra   perf_counter: inh...
1140
  	 */
2e939d1da   Franck Bui-Huu   perf: Limit event...
1141
  	if (event->attr.inherit || !is_sampling_event(event))
2023b3592   Peter Zijlstra   perf_counter: inh...
1142
  		return -EINVAL;
cdd6c482c   Ingo Molnar   perf: Do the big ...
1143
1144
  	atomic_add(refresh, &event->event_limit);
  	perf_event_enable(event);
2023b3592   Peter Zijlstra   perf_counter: inh...
1145
1146
  
  	return 0;
79f146415   Peter Zijlstra   perf_counter: cou...
1147
  }
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1148
1149
1150
  static void ctx_sched_out(struct perf_event_context *ctx,
  			  struct perf_cpu_context *cpuctx,
  			  enum event_type_t event_type)
235c7fc7c   Ingo Molnar   perfcounters: gen...
1151
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1152
  	struct perf_event *event;
235c7fc7c   Ingo Molnar   perfcounters: gen...
1153

e625cce1b   Thomas Gleixner   perf_event: Conve...
1154
  	raw_spin_lock(&ctx->lock);
1b9a644fe   Peter Zijlstra   perf: Optimize co...
1155
  	perf_pmu_disable(ctx->pmu);
d859e29fe   Paul Mackerras   perf_counter: Add...
1156
  	ctx->is_active = 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
1157
  	if (likely(!ctx->nr_events))
d859e29fe   Paul Mackerras   perf_counter: Add...
1158
  		goto out;
4af4998b8   Peter Zijlstra   perf_counter: rew...
1159
  	update_context_time(ctx);
235c7fc7c   Ingo Molnar   perfcounters: gen...
1160

5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1161
  	if (!ctx->nr_active)
24cd7f54a   Peter Zijlstra   perf: Reduce perf...
1162
  		goto out;
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1163

9ed6060d2   Peter Zijlstra   perf: Unindent la...
1164
  	if (event_type & EVENT_PINNED) {
889ff0150   Frederic Weisbecker   perf/core: Split ...
1165
1166
  		list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  			group_sched_out(event, cpuctx, ctx);
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1167
  	}
889ff0150   Frederic Weisbecker   perf/core: Split ...
1168

9ed6060d2   Peter Zijlstra   perf: Unindent la...
1169
  	if (event_type & EVENT_FLEXIBLE) {
889ff0150   Frederic Weisbecker   perf/core: Split ...
1170
  		list_for_each_entry(event, &ctx->flexible_groups, group_entry)
8c9ed8e14   Xiao Guangrong   perf_event: Fix e...
1171
  			group_sched_out(event, cpuctx, ctx);
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1172
  	}
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1173
  out:
1b9a644fe   Peter Zijlstra   perf: Optimize co...
1174
  	perf_pmu_enable(ctx->pmu);
e625cce1b   Thomas Gleixner   perf_event: Conve...
1175
  	raw_spin_unlock(&ctx->lock);
235c7fc7c   Ingo Molnar   perfcounters: gen...
1176
  }
0793a61d4   Thomas Gleixner   performance count...
1177
  /*
564c2b210   Paul Mackerras   perf_counter: Opt...
1178
1179
   * Test whether two contexts are equivalent, i.e. whether they
   * have both been cloned from the same version of the same context
cdd6c482c   Ingo Molnar   perf: Do the big ...
1180
1181
1182
1183
   * and they both have the same number of enabled events.
   * If the number of enabled events is the same, then the set
   * of enabled events should be the same, because these are both
   * inherited contexts, therefore we can't access individual events
564c2b210   Paul Mackerras   perf_counter: Opt...
1184
   * in them directly with an fd; we can only enable/disable all
cdd6c482c   Ingo Molnar   perf: Do the big ...
1185
   * events via prctl, or enable/disable all events in a family
564c2b210   Paul Mackerras   perf_counter: Opt...
1186
1187
   * via ioctl, which will have the same effect on both contexts.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1188
1189
  static int context_equiv(struct perf_event_context *ctx1,
  			 struct perf_event_context *ctx2)
564c2b210   Paul Mackerras   perf_counter: Opt...
1190
1191
  {
  	return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
ad3a37de8   Paul Mackerras   perf_counter: Don...
1192
  		&& ctx1->parent_gen == ctx2->parent_gen
25346b93c   Paul Mackerras   perf_counter: Pro...
1193
  		&& !ctx1->pin_count && !ctx2->pin_count;
564c2b210   Paul Mackerras   perf_counter: Opt...
1194
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
1195
1196
  static void __perf_event_sync_stat(struct perf_event *event,
  				     struct perf_event *next_event)
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1197
1198
  {
  	u64 value;
cdd6c482c   Ingo Molnar   perf: Do the big ...
1199
  	if (!event->attr.inherit_stat)
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1200
1201
1202
  		return;
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1203
  	 * Update the event value, we cannot use perf_event_read()
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1204
1205
  	 * because we're in the middle of a context switch and have IRQs
  	 * disabled, which upsets smp_call_function_single(), however
cdd6c482c   Ingo Molnar   perf: Do the big ...
1206
  	 * we know the event must be on the current CPU, therefore we
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1207
1208
  	 * don't need to use it.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1209
1210
  	switch (event->state) {
  	case PERF_EVENT_STATE_ACTIVE:
3dbebf15c   Peter Zijlstra   perf: Simplify __...
1211
1212
  		event->pmu->read(event);
  		/* fall-through */
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1213

cdd6c482c   Ingo Molnar   perf: Do the big ...
1214
1215
  	case PERF_EVENT_STATE_INACTIVE:
  		update_event_times(event);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1216
1217
1218
1219
1220
1221
1222
  		break;
  
  	default:
  		break;
  	}
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1223
  	 * In order to keep per-task stats reliable we need to flip the event
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1224
1225
  	 * values when we flip the contexts.
  	 */
e78505958   Peter Zijlstra   perf: Convert per...
1226
1227
1228
  	value = local64_read(&next_event->count);
  	value = local64_xchg(&event->count, value);
  	local64_set(&next_event->count, value);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1229

cdd6c482c   Ingo Molnar   perf: Do the big ...
1230
1231
  	swap(event->total_time_enabled, next_event->total_time_enabled);
  	swap(event->total_time_running, next_event->total_time_running);
19d2e7554   Peter Zijlstra   perf_counter: Com...
1232

bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1233
  	/*
19d2e7554   Peter Zijlstra   perf_counter: Com...
1234
  	 * Since we swizzled the values, update the user visible data too.
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1235
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1236
1237
  	perf_event_update_userpage(event);
  	perf_event_update_userpage(next_event);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1238
1239
1240
1241
  }
  
  #define list_next_entry(pos, member) \
  	list_entry(pos->member.next, typeof(*pos), member)
cdd6c482c   Ingo Molnar   perf: Do the big ...
1242
1243
  static void perf_event_sync_stat(struct perf_event_context *ctx,
  				   struct perf_event_context *next_ctx)
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1244
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1245
  	struct perf_event *event, *next_event;
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1246
1247
1248
  
  	if (!ctx->nr_stat)
  		return;
02ffdbc86   Peter Zijlstra   perf: Optimize pe...
1249
  	update_context_time(ctx);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1250
1251
  	event = list_first_entry(&ctx->event_list,
  				   struct perf_event, event_entry);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1252

cdd6c482c   Ingo Molnar   perf: Do the big ...
1253
1254
  	next_event = list_first_entry(&next_ctx->event_list,
  					struct perf_event, event_entry);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1255

cdd6c482c   Ingo Molnar   perf: Do the big ...
1256
1257
  	while (&event->event_entry != &ctx->event_list &&
  	       &next_event->event_entry != &next_ctx->event_list) {
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1258

cdd6c482c   Ingo Molnar   perf: Do the big ...
1259
  		__perf_event_sync_stat(event, next_event);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1260

cdd6c482c   Ingo Molnar   perf: Do the big ...
1261
1262
  		event = list_next_entry(event, event_entry);
  		next_event = list_next_entry(next_event, event_entry);
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1263
1264
  	}
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1265
1266
  void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  				  struct task_struct *next)
0793a61d4   Thomas Gleixner   performance count...
1267
  {
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1268
  	struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
cdd6c482c   Ingo Molnar   perf: Do the big ...
1269
1270
  	struct perf_event_context *next_ctx;
  	struct perf_event_context *parent;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1271
  	struct perf_cpu_context *cpuctx;
c93f76690   Paul Mackerras   perf_counter: Fix...
1272
  	int do_switch = 1;
0793a61d4   Thomas Gleixner   performance count...
1273

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1274
1275
  	if (likely(!ctx))
  		return;
10989fb24   Peter Zijlstra   perf_counter: Fix...
1276

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1277
1278
  	cpuctx = __get_cpu_context(ctx);
  	if (!cpuctx->task_ctx)
0793a61d4   Thomas Gleixner   performance count...
1279
  		return;
c93f76690   Paul Mackerras   perf_counter: Fix...
1280
1281
  	rcu_read_lock();
  	parent = rcu_dereference(ctx->parent_ctx);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1282
  	next_ctx = next->perf_event_ctxp[ctxn];
c93f76690   Paul Mackerras   perf_counter: Fix...
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
  	if (parent && next_ctx &&
  	    rcu_dereference(next_ctx->parent_ctx) == parent) {
  		/*
  		 * Looks like the two contexts are clones, so we might be
  		 * able to optimize the context switch.  We lock both
  		 * contexts and check that they are clones under the
  		 * lock (including re-checking that neither has been
  		 * uncloned in the meantime).  It doesn't matter which
  		 * order we take the locks because no other cpu could
  		 * be trying to lock both of these tasks.
  		 */
e625cce1b   Thomas Gleixner   perf_event: Conve...
1294
1295
  		raw_spin_lock(&ctx->lock);
  		raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
c93f76690   Paul Mackerras   perf_counter: Fix...
1296
  		if (context_equiv(ctx, next_ctx)) {
665c2142a   Peter Zijlstra   perf_counter: Cle...
1297
1298
  			/*
  			 * XXX do we need a memory barrier of sorts
cdd6c482c   Ingo Molnar   perf: Do the big ...
1299
  			 * wrt to rcu_dereference() of perf_event_ctxp
665c2142a   Peter Zijlstra   perf_counter: Cle...
1300
  			 */
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1301
1302
  			task->perf_event_ctxp[ctxn] = next_ctx;
  			next->perf_event_ctxp[ctxn] = ctx;
c93f76690   Paul Mackerras   perf_counter: Fix...
1303
1304
1305
  			ctx->task = next;
  			next_ctx->task = task;
  			do_switch = 0;
bfbd3381e   Peter Zijlstra   perf_counter: Imp...
1306

cdd6c482c   Ingo Molnar   perf: Do the big ...
1307
  			perf_event_sync_stat(ctx, next_ctx);
c93f76690   Paul Mackerras   perf_counter: Fix...
1308
  		}
e625cce1b   Thomas Gleixner   perf_event: Conve...
1309
1310
  		raw_spin_unlock(&next_ctx->lock);
  		raw_spin_unlock(&ctx->lock);
564c2b210   Paul Mackerras   perf_counter: Opt...
1311
  	}
c93f76690   Paul Mackerras   perf_counter: Fix...
1312
  	rcu_read_unlock();
564c2b210   Paul Mackerras   perf_counter: Opt...
1313

c93f76690   Paul Mackerras   perf_counter: Fix...
1314
  	if (do_switch) {
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1315
  		ctx_sched_out(ctx, cpuctx, EVENT_ALL);
c93f76690   Paul Mackerras   perf_counter: Fix...
1316
1317
  		cpuctx->task_ctx = NULL;
  	}
0793a61d4   Thomas Gleixner   performance count...
1318
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
  #define for_each_task_context_nr(ctxn)					\
  	for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  
  /*
   * Called from scheduler to remove the events of the current task,
   * with interrupts disabled.
   *
   * We stop each event and update the event value in event->count.
   *
   * This does not protect us against NMI, but disable()
   * sets the disabled bit in the control field of event _before_
   * accessing the event control register. If a NMI hits, then it will
   * not restart the event.
   */
82cd6def9   Peter Zijlstra   perf: Use jump_la...
1333
1334
  void __perf_event_task_sched_out(struct task_struct *task,
  				 struct task_struct *next)
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1335
1336
  {
  	int ctxn;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1337
1338
1339
  	for_each_task_context_nr(ctxn)
  		perf_event_context_sched_out(task, ctxn, next);
  }
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1340
1341
  static void task_ctx_sched_out(struct perf_event_context *ctx,
  			       enum event_type_t event_type)
a08b159fc   Paul Mackerras   perf_counter: don...
1342
  {
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1343
  	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
a08b159fc   Paul Mackerras   perf_counter: don...
1344

a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1345
1346
  	if (!cpuctx->task_ctx)
  		return;
012b84dae   Ingo Molnar   perf_counter: Rob...
1347
1348
1349
  
  	if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  		return;
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1350
  	ctx_sched_out(ctx, cpuctx, event_type);
a08b159fc   Paul Mackerras   perf_counter: don...
1351
1352
  	cpuctx->task_ctx = NULL;
  }
665c2142a   Peter Zijlstra   perf_counter: Cle...
1353
1354
1355
  /*
   * Called with IRQs disabled
   */
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1356
1357
1358
1359
  static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  			      enum event_type_t event_type)
  {
  	ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
04289bb98   Ingo Molnar   perf counters: ad...
1360
  }
235c7fc7c   Ingo Molnar   perfcounters: gen...
1361
  static void
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1362
  ctx_pinned_sched_in(struct perf_event_context *ctx,
6e37738a2   Peter Zijlstra   perf_events: Simp...
1363
  		    struct perf_cpu_context *cpuctx)
0793a61d4   Thomas Gleixner   performance count...
1364
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1365
  	struct perf_event *event;
0793a61d4   Thomas Gleixner   performance count...
1366

889ff0150   Frederic Weisbecker   perf/core: Split ...
1367
1368
  	list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  		if (event->state <= PERF_EVENT_STATE_OFF)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1369
  			continue;
5632ab12e   Stephane Eranian   perf_events: Gene...
1370
  		if (!event_filter_match(event))
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1371
  			continue;
8c9ed8e14   Xiao Guangrong   perf_event: Fix e...
1372
  		if (group_can_go_on(event, cpuctx, 1))
6e37738a2   Peter Zijlstra   perf_events: Simp...
1373
  			group_sched_in(event, cpuctx, ctx);
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1374
1375
1376
1377
1378
  
  		/*
  		 * If this pinned group hasn't been scheduled,
  		 * put it in error state.
  		 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1379
1380
1381
  		if (event->state == PERF_EVENT_STATE_INACTIVE) {
  			update_group_times(event);
  			event->state = PERF_EVENT_STATE_ERROR;
53cfbf593   Paul Mackerras   perf_counter: rec...
1382
  		}
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1383
  	}
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1384
1385
1386
1387
  }
  
  static void
  ctx_flexible_sched_in(struct perf_event_context *ctx,
6e37738a2   Peter Zijlstra   perf_events: Simp...
1388
  		      struct perf_cpu_context *cpuctx)
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1389
1390
1391
  {
  	struct perf_event *event;
  	int can_add_hw = 1;
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1392

889ff0150   Frederic Weisbecker   perf/core: Split ...
1393
1394
1395
  	list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  		/* Ignore events in OFF or ERROR state */
  		if (event->state <= PERF_EVENT_STATE_OFF)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1396
  			continue;
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
1397
  		/*
04289bb98   Ingo Molnar   perf counters: ad...
1398
  		 * Listen to the 'cpu' scheduling filter constraint
cdd6c482c   Ingo Molnar   perf: Do the big ...
1399
  		 * of events:
04289bb98   Ingo Molnar   perf counters: ad...
1400
  		 */
5632ab12e   Stephane Eranian   perf_events: Gene...
1401
  		if (!event_filter_match(event))
0793a61d4   Thomas Gleixner   performance count...
1402
  			continue;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1403
  		if (group_can_go_on(event, cpuctx, can_add_hw)) {
6e37738a2   Peter Zijlstra   perf_events: Simp...
1404
  			if (group_sched_in(event, cpuctx, ctx))
dd0e6ba22   Paul Mackerras   perf_counter: Alw...
1405
  				can_add_hw = 0;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1406
  		}
0793a61d4   Thomas Gleixner   performance count...
1407
  	}
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1408
1409
1410
1411
1412
1413
1414
  }
  
  static void
  ctx_sched_in(struct perf_event_context *ctx,
  	     struct perf_cpu_context *cpuctx,
  	     enum event_type_t event_type)
  {
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1415
1416
1417
1418
1419
1420
  	raw_spin_lock(&ctx->lock);
  	ctx->is_active = 1;
  	if (likely(!ctx->nr_events))
  		goto out;
  
  	ctx->timestamp = perf_clock();
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1421
1422
1423
1424
1425
  	/*
  	 * First go through the list and put on any pinned groups
  	 * in order to give them the best chance of going on.
  	 */
  	if (event_type & EVENT_PINNED)
6e37738a2   Peter Zijlstra   perf_events: Simp...
1426
  		ctx_pinned_sched_in(ctx, cpuctx);
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1427
1428
1429
  
  	/* Then walk through the lower prio flexible groups */
  	if (event_type & EVENT_FLEXIBLE)
6e37738a2   Peter Zijlstra   perf_events: Simp...
1430
  		ctx_flexible_sched_in(ctx, cpuctx);
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1431

9ed6060d2   Peter Zijlstra   perf: Unindent la...
1432
  out:
e625cce1b   Thomas Gleixner   perf_event: Conve...
1433
  	raw_spin_unlock(&ctx->lock);
235c7fc7c   Ingo Molnar   perfcounters: gen...
1434
  }
329c0e012   Frederic Weisbecker   perf: Better orde...
1435
1436
1437
1438
1439
1440
1441
  static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  			     enum event_type_t event_type)
  {
  	struct perf_event_context *ctx = &cpuctx->ctx;
  
  	ctx_sched_in(ctx, cpuctx, event_type);
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1442
  static void task_ctx_sched_in(struct perf_event_context *ctx,
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1443
1444
  			      enum event_type_t event_type)
  {
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1445
  	struct perf_cpu_context *cpuctx;
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1446

8dc85d547   Peter Zijlstra   perf: Multiple ta...
1447
         	cpuctx = __get_cpu_context(ctx);
5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1448
1449
  	if (cpuctx->task_ctx == ctx)
  		return;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1450

5b0311e1f   Frederic Weisbecker   perf: Allow pinne...
1451
1452
1453
  	ctx_sched_in(ctx, cpuctx, event_type);
  	cpuctx->task_ctx = ctx;
  }
0793a61d4   Thomas Gleixner   performance count...
1454

8dc85d547   Peter Zijlstra   perf: Multiple ta...
1455
  void perf_event_context_sched_in(struct perf_event_context *ctx)
235c7fc7c   Ingo Molnar   perfcounters: gen...
1456
  {
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1457
  	struct perf_cpu_context *cpuctx;
235c7fc7c   Ingo Molnar   perfcounters: gen...
1458

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1459
  	cpuctx = __get_cpu_context(ctx);
329c0e012   Frederic Weisbecker   perf: Better orde...
1460
1461
  	if (cpuctx->task_ctx == ctx)
  		return;
1b9a644fe   Peter Zijlstra   perf: Optimize co...
1462
  	perf_pmu_disable(ctx->pmu);
329c0e012   Frederic Weisbecker   perf: Better orde...
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
  	/*
  	 * We want to keep the following priority order:
  	 * cpu pinned (that don't need to move), task pinned,
  	 * cpu flexible, task flexible.
  	 */
  	cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  
  	ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  	cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  	ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  
  	cpuctx->task_ctx = ctx;
9b33fa6ba   eranian@google.com   perf_events: Impr...
1475

b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1476
1477
1478
1479
  	/*
  	 * Since these rotations are per-cpu, we need to ensure the
  	 * cpu-context we got scheduled on is actually rotating.
  	 */
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1480
  	perf_pmu_rotate_start(ctx->pmu);
1b9a644fe   Peter Zijlstra   perf: Optimize co...
1481
  	perf_pmu_enable(ctx->pmu);
235c7fc7c   Ingo Molnar   perfcounters: gen...
1482
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
  /*
   * Called from scheduler to add the events of the current task
   * with interrupts disabled.
   *
   * We restore the event value and then enable it.
   *
   * This does not protect us against NMI, but enable()
   * sets the enabled bit in the control field of event _before_
   * accessing the event control register. If a NMI hits, then it will
   * keep the event running.
   */
82cd6def9   Peter Zijlstra   perf: Use jump_la...
1494
  void __perf_event_task_sched_in(struct task_struct *task)
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
  {
  	struct perf_event_context *ctx;
  	int ctxn;
  
  	for_each_task_context_nr(ctxn) {
  		ctx = task->perf_event_ctxp[ctxn];
  		if (likely(!ctx))
  			continue;
  
  		perf_event_context_sched_in(ctx);
  	}
235c7fc7c   Ingo Molnar   perfcounters: gen...
1506
  }
a78ac3258   Peter Zijlstra   perf_counter: Gen...
1507
  #define MAX_INTERRUPTS (~0ULL)
cdd6c482c   Ingo Molnar   perf: Do the big ...
1508
  static void perf_log_throttle(struct perf_event *event, int enable);
26b119bc8   Peter Zijlstra   perf_counter: Log...
1509

abd507139   Peter Zijlstra   perf: Reimplement...
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
  static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  {
  	u64 frequency = event->attr.sample_freq;
  	u64 sec = NSEC_PER_SEC;
  	u64 divisor, dividend;
  
  	int count_fls, nsec_fls, frequency_fls, sec_fls;
  
  	count_fls = fls64(count);
  	nsec_fls = fls64(nsec);
  	frequency_fls = fls64(frequency);
  	sec_fls = 30;
  
  	/*
  	 * We got @count in @nsec, with a target of sample_freq HZ
  	 * the target period becomes:
  	 *
  	 *             @count * 10^9
  	 * period = -------------------
  	 *          @nsec * sample_freq
  	 *
  	 */
  
  	/*
  	 * Reduce accuracy by one bit such that @a and @b converge
  	 * to a similar magnitude.
  	 */
  #define REDUCE_FLS(a, b) 		\
  do {					\
  	if (a##_fls > b##_fls) {	\
  		a >>= 1;		\
  		a##_fls--;		\
  	} else {			\
  		b >>= 1;		\
  		b##_fls--;		\
  	}				\
  } while (0)
  
  	/*
  	 * Reduce accuracy until either term fits in a u64, then proceed with
  	 * the other, so that finally we can do a u64/u64 division.
  	 */
  	while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  		REDUCE_FLS(nsec, frequency);
  		REDUCE_FLS(sec, count);
  	}
  
  	if (count_fls + sec_fls > 64) {
  		divisor = nsec * frequency;
  
  		while (count_fls + sec_fls > 64) {
  			REDUCE_FLS(count, sec);
  			divisor >>= 1;
  		}
  
  		dividend = count * sec;
  	} else {
  		dividend = count * sec;
  
  		while (nsec_fls + frequency_fls > 64) {
  			REDUCE_FLS(nsec, frequency);
  			dividend >>= 1;
  		}
  
  		divisor = nsec * frequency;
  	}
f6ab91add   Peter Zijlstra   perf: Fix signed ...
1576
1577
  	if (!divisor)
  		return dividend;
abd507139   Peter Zijlstra   perf: Reimplement...
1578
1579
1580
1581
  	return div64_u64(dividend, divisor);
  }
  
  static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1582
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1583
  	struct hw_perf_event *hwc = &event->hw;
f6ab91add   Peter Zijlstra   perf: Fix signed ...
1584
  	s64 period, sample_period;
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1585
  	s64 delta;
abd507139   Peter Zijlstra   perf: Reimplement...
1586
  	period = perf_calculate_period(event, nsec, count);
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1587
1588
1589
1590
1591
1592
1593
1594
  
  	delta = (s64)(period - hwc->sample_period);
  	delta = (delta + 7) / 8; /* low pass filter */
  
  	sample_period = hwc->sample_period + delta;
  
  	if (!sample_period)
  		sample_period = 1;
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1595
  	hwc->sample_period = sample_period;
abd507139   Peter Zijlstra   perf: Reimplement...
1596

e78505958   Peter Zijlstra   perf: Convert per...
1597
  	if (local64_read(&hwc->period_left) > 8*sample_period) {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
1598
  		event->pmu->stop(event, PERF_EF_UPDATE);
e78505958   Peter Zijlstra   perf: Convert per...
1599
  		local64_set(&hwc->period_left, 0);
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
1600
  		event->pmu->start(event, PERF_EF_RELOAD);
abd507139   Peter Zijlstra   perf: Reimplement...
1601
  	}
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1602
  }
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1603
  static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
60db5e09c   Peter Zijlstra   perf_counter: fre...
1604
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1605
1606
  	struct perf_event *event;
  	struct hw_perf_event *hwc;
abd507139   Peter Zijlstra   perf: Reimplement...
1607
1608
  	u64 interrupts, now;
  	s64 delta;
60db5e09c   Peter Zijlstra   perf_counter: fre...
1609

e625cce1b   Thomas Gleixner   perf_event: Conve...
1610
  	raw_spin_lock(&ctx->lock);
03541f8b6   Paul Mackerras   perf_event: Adjus...
1611
  	list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1612
  		if (event->state != PERF_EVENT_STATE_ACTIVE)
60db5e09c   Peter Zijlstra   perf_counter: fre...
1613
  			continue;
5632ab12e   Stephane Eranian   perf_events: Gene...
1614
  		if (!event_filter_match(event))
5d27c23df   Peter Zijlstra   perf events: Dont...
1615
  			continue;
cdd6c482c   Ingo Molnar   perf: Do the big ...
1616
  		hwc = &event->hw;
6a24ed6c6   Peter Zijlstra   perf_counter: Fix...
1617
1618
1619
  
  		interrupts = hwc->interrupts;
  		hwc->interrupts = 0;
a78ac3258   Peter Zijlstra   perf_counter: Gen...
1620

bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1621
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1622
  		 * unthrottle events on the tick
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
1623
  		 */
a78ac3258   Peter Zijlstra   perf_counter: Gen...
1624
  		if (interrupts == MAX_INTERRUPTS) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1625
  			perf_log_throttle(event, 1);
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
1626
  			event->pmu->start(event, 0);
a78ac3258   Peter Zijlstra   perf_counter: Gen...
1627
  		}
cdd6c482c   Ingo Molnar   perf: Do the big ...
1628
  		if (!event->attr.freq || !event->attr.sample_freq)
60db5e09c   Peter Zijlstra   perf_counter: fre...
1629
  			continue;
abd507139   Peter Zijlstra   perf: Reimplement...
1630
  		event->pmu->read(event);
e78505958   Peter Zijlstra   perf: Convert per...
1631
  		now = local64_read(&event->count);
abd507139   Peter Zijlstra   perf: Reimplement...
1632
1633
  		delta = now - hwc->freq_count_stamp;
  		hwc->freq_count_stamp = now;
60db5e09c   Peter Zijlstra   perf_counter: fre...
1634

abd507139   Peter Zijlstra   perf: Reimplement...
1635
  		if (delta > 0)
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1636
  			perf_adjust_period(event, period, delta);
60db5e09c   Peter Zijlstra   perf_counter: fre...
1637
  	}
e625cce1b   Thomas Gleixner   perf_event: Conve...
1638
  	raw_spin_unlock(&ctx->lock);
60db5e09c   Peter Zijlstra   perf_counter: fre...
1639
  }
235c7fc7c   Ingo Molnar   perfcounters: gen...
1640
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1641
   * Round-robin a context's events:
235c7fc7c   Ingo Molnar   perfcounters: gen...
1642
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1643
  static void rotate_ctx(struct perf_event_context *ctx)
0793a61d4   Thomas Gleixner   performance count...
1644
  {
e625cce1b   Thomas Gleixner   perf_event: Conve...
1645
  	raw_spin_lock(&ctx->lock);
e28641737   Frederic Weisbecker   perf: Round robin...
1646

dddd3379a   Thomas Gleixner   perf: Fix inherit...
1647
1648
1649
1650
1651
1652
  	/*
  	 * Rotate the first entry last of non-pinned groups. Rotation might be
  	 * disabled by the inheritance code.
  	 */
  	if (!ctx->rotate_disable)
  		list_rotate_left(&ctx->flexible_groups);
e28641737   Frederic Weisbecker   perf: Round robin...
1653

e625cce1b   Thomas Gleixner   perf_event: Conve...
1654
  	raw_spin_unlock(&ctx->lock);
235c7fc7c   Ingo Molnar   perfcounters: gen...
1655
  }
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1656
  /*
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1657
1658
1659
   * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
   * because they're strictly cpu affine and rotate_start is called with IRQs
   * disabled, while rotate_context is called from IRQ context.
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1660
   */
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1661
  static void perf_rotate_context(struct perf_cpu_context *cpuctx)
235c7fc7c   Ingo Molnar   perfcounters: gen...
1662
  {
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1663
  	u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1664
  	struct perf_event_context *ctx = NULL;
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1665
  	int rotate = 0, remove = 1;
7fc23a538   Peter Zijlstra   perf_counter: opt...
1666

b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1667
  	if (cpuctx->ctx.nr_events) {
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1668
  		remove = 0;
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1669
1670
1671
  		if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  			rotate = 1;
  	}
235c7fc7c   Ingo Molnar   perfcounters: gen...
1672

8dc85d547   Peter Zijlstra   perf: Multiple ta...
1673
  	ctx = cpuctx->task_ctx;
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1674
  	if (ctx && ctx->nr_events) {
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1675
  		remove = 0;
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1676
1677
1678
  		if (ctx->nr_events != ctx->nr_active)
  			rotate = 1;
  	}
9717e6cd3   Peter Zijlstra   perf_events: Opti...
1679

1b9a644fe   Peter Zijlstra   perf: Optimize co...
1680
  	perf_pmu_disable(cpuctx->ctx.pmu);
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1681
  	perf_ctx_adjust_freq(&cpuctx->ctx, interval);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1682
  	if (ctx)
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1683
  		perf_ctx_adjust_freq(ctx, interval);
60db5e09c   Peter Zijlstra   perf_counter: fre...
1684

d4944a066   Peter Zijlstra   perf: Provide bet...
1685
  	if (!rotate)
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1686
  		goto done;
d4944a066   Peter Zijlstra   perf: Provide bet...
1687

7defb0f87   Frederic Weisbecker   perf: Don't sched...
1688
  	cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1689
  	if (ctx)
7defb0f87   Frederic Weisbecker   perf: Don't sched...
1690
  		task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
0793a61d4   Thomas Gleixner   performance count...
1691

b82914ce3   Ingo Molnar   perf_counter: rou...
1692
  	rotate_ctx(&cpuctx->ctx);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1693
1694
  	if (ctx)
  		rotate_ctx(ctx);
235c7fc7c   Ingo Molnar   perfcounters: gen...
1695

7defb0f87   Frederic Weisbecker   perf: Don't sched...
1696
  	cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1697
  	if (ctx)
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1698
  		task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1699
1700
  
  done:
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1701
1702
  	if (remove)
  		list_del_init(&cpuctx->rotation_list);
1b9a644fe   Peter Zijlstra   perf: Optimize co...
1703
  	perf_pmu_enable(cpuctx->ctx.pmu);
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1704
1705
1706
1707
1708
1709
  }
  
  void perf_event_task_tick(void)
  {
  	struct list_head *head = &__get_cpu_var(rotation_list);
  	struct perf_cpu_context *cpuctx, *tmp;
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
1710

e9d2b0641   Peter Zijlstra   perf: Undo the pe...
1711
1712
1713
1714
1715
1716
1717
  	WARN_ON(!irqs_disabled());
  
  	list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
  		if (cpuctx->jiffies_interval == 1 ||
  				!(jiffies % cpuctx->jiffies_interval))
  			perf_rotate_context(cpuctx);
  	}
0793a61d4   Thomas Gleixner   performance count...
1718
  }
889ff0150   Frederic Weisbecker   perf/core: Split ...
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
  static int event_enable_on_exec(struct perf_event *event,
  				struct perf_event_context *ctx)
  {
  	if (!event->attr.enable_on_exec)
  		return 0;
  
  	event->attr.enable_on_exec = 0;
  	if (event->state >= PERF_EVENT_STATE_INACTIVE)
  		return 0;
  
  	__perf_event_mark_enabled(event, ctx);
  
  	return 1;
  }
0793a61d4   Thomas Gleixner   performance count...
1733
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1734
   * Enable all of a task's events that have been marked enable-on-exec.
57e7986ed   Paul Mackerras   perf_counter: Pro...
1735
1736
   * This expects task == current.
   */
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1737
  static void perf_event_enable_on_exec(struct perf_event_context *ctx)
57e7986ed   Paul Mackerras   perf_counter: Pro...
1738
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1739
  	struct perf_event *event;
57e7986ed   Paul Mackerras   perf_counter: Pro...
1740
1741
  	unsigned long flags;
  	int enabled = 0;
889ff0150   Frederic Weisbecker   perf/core: Split ...
1742
  	int ret;
57e7986ed   Paul Mackerras   perf_counter: Pro...
1743
1744
  
  	local_irq_save(flags);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1745
  	if (!ctx || !ctx->nr_events)
57e7986ed   Paul Mackerras   perf_counter: Pro...
1746
  		goto out;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
1747
  	task_ctx_sched_out(ctx, EVENT_ALL);
57e7986ed   Paul Mackerras   perf_counter: Pro...
1748

e625cce1b   Thomas Gleixner   perf_event: Conve...
1749
  	raw_spin_lock(&ctx->lock);
57e7986ed   Paul Mackerras   perf_counter: Pro...
1750

889ff0150   Frederic Weisbecker   perf/core: Split ...
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
  	list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  		ret = event_enable_on_exec(event, ctx);
  		if (ret)
  			enabled = 1;
  	}
  
  	list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  		ret = event_enable_on_exec(event, ctx);
  		if (ret)
  			enabled = 1;
57e7986ed   Paul Mackerras   perf_counter: Pro...
1761
1762
1763
  	}
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1764
  	 * Unclone this context if we enabled any event.
57e7986ed   Paul Mackerras   perf_counter: Pro...
1765
  	 */
71a851b4d   Peter Zijlstra   perf_counter: Sto...
1766
1767
  	if (enabled)
  		unclone_ctx(ctx);
57e7986ed   Paul Mackerras   perf_counter: Pro...
1768

e625cce1b   Thomas Gleixner   perf_event: Conve...
1769
  	raw_spin_unlock(&ctx->lock);
57e7986ed   Paul Mackerras   perf_counter: Pro...
1770

8dc85d547   Peter Zijlstra   perf: Multiple ta...
1771
  	perf_event_context_sched_in(ctx);
9ed6060d2   Peter Zijlstra   perf: Unindent la...
1772
  out:
57e7986ed   Paul Mackerras   perf_counter: Pro...
1773
1774
1775
1776
  	local_irq_restore(flags);
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1777
   * Cross CPU call to read the hardware event
0793a61d4   Thomas Gleixner   performance count...
1778
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1779
  static void __perf_event_read(void *info)
0793a61d4   Thomas Gleixner   performance count...
1780
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
1781
1782
  	struct perf_event *event = info;
  	struct perf_event_context *ctx = event->ctx;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
1783
  	struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
621a01eac   Ingo Molnar   perf counters: hw...
1784

e1ac3614f   Paul Mackerras   perf_counter: Che...
1785
1786
1787
1788
  	/*
  	 * If this is a task context, we need to check whether it is
  	 * the current task context of this cpu.  If not it has been
  	 * scheduled out before the smp call arrived.  In that case
cdd6c482c   Ingo Molnar   perf: Do the big ...
1789
1790
  	 * event->count would have been updated to a recent sample
  	 * when the event was scheduled out.
e1ac3614f   Paul Mackerras   perf_counter: Che...
1791
1792
1793
  	 */
  	if (ctx->task && cpuctx->task_ctx != ctx)
  		return;
e625cce1b   Thomas Gleixner   perf_event: Conve...
1794
  	raw_spin_lock(&ctx->lock);
542e72fc9   Peter Zijlstra   perf: Fix reading...
1795
1796
  	if (ctx->is_active)
  		update_context_time(ctx);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1797
  	update_event_times(event);
542e72fc9   Peter Zijlstra   perf: Fix reading...
1798
1799
  	if (event->state == PERF_EVENT_STATE_ACTIVE)
  		event->pmu->read(event);
e625cce1b   Thomas Gleixner   perf_event: Conve...
1800
  	raw_spin_unlock(&ctx->lock);
0793a61d4   Thomas Gleixner   performance count...
1801
  }
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
1802
1803
  static inline u64 perf_event_count(struct perf_event *event)
  {
e78505958   Peter Zijlstra   perf: Convert per...
1804
  	return local64_read(&event->count) + atomic64_read(&event->child_count);
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
1805
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
1806
  static u64 perf_event_read(struct perf_event *event)
0793a61d4   Thomas Gleixner   performance count...
1807
1808
  {
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
1809
1810
  	 * If event is enabled and currently active on a CPU, update the
  	 * value in the event structure:
0793a61d4   Thomas Gleixner   performance count...
1811
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
1812
1813
1814
1815
  	if (event->state == PERF_EVENT_STATE_ACTIVE) {
  		smp_call_function_single(event->oncpu,
  					 __perf_event_read, event, 1);
  	} else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2b8988c9f   Peter Zijlstra   perf: Fix time lo...
1816
1817
  		struct perf_event_context *ctx = event->ctx;
  		unsigned long flags;
e625cce1b   Thomas Gleixner   perf_event: Conve...
1818
  		raw_spin_lock_irqsave(&ctx->lock, flags);
c530ccd9a   Stephane Eranian   perf_events: Fix ...
1819
1820
1821
1822
1823
1824
1825
  		/*
  		 * may read while context is not active
  		 * (e.g., thread is blocked), in that case
  		 * we cannot update context time
  		 */
  		if (ctx->is_active)
  			update_context_time(ctx);
cdd6c482c   Ingo Molnar   perf: Do the big ...
1826
  		update_event_times(event);
e625cce1b   Thomas Gleixner   perf_event: Conve...
1827
  		raw_spin_unlock_irqrestore(&ctx->lock, flags);
0793a61d4   Thomas Gleixner   performance count...
1828
  	}
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
1829
  	return perf_event_count(event);
0793a61d4   Thomas Gleixner   performance count...
1830
  }
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1831
  /*
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1832
   * Callchain support
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1833
   */
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1834
1835
1836
1837
1838
  
  struct callchain_cpus_entries {
  	struct rcu_head			rcu_head;
  	struct perf_callchain_entry	*cpu_entries[0];
  };
7ae07ea3a   Frederic Weisbecker   perf: Humanize th...
1839
  static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1840
1841
1842
1843
1844
1845
1846
  static atomic_t nr_callchain_events;
  static DEFINE_MUTEX(callchain_mutex);
  struct callchain_cpus_entries *callchain_cpus_entries;
  
  
  __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  				  struct pt_regs *regs)
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1847
  {
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1848
  }
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1849
1850
  __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  				struct pt_regs *regs)
0793a61d4   Thomas Gleixner   performance count...
1851
  {
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1852
  }
0793a61d4   Thomas Gleixner   performance count...
1853

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1854
1855
1856
1857
  static void release_callchain_buffers_rcu(struct rcu_head *head)
  {
  	struct callchain_cpus_entries *entries;
  	int cpu;
0793a61d4   Thomas Gleixner   performance count...
1858

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1859
  	entries = container_of(head, struct callchain_cpus_entries, rcu_head);
0793a61d4   Thomas Gleixner   performance count...
1860

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1861
1862
  	for_each_possible_cpu(cpu)
  		kfree(entries->cpu_entries[cpu]);
0793a61d4   Thomas Gleixner   performance count...
1863

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1864
1865
  	kfree(entries);
  }
0793a61d4   Thomas Gleixner   performance count...
1866

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1867
1868
1869
  static void release_callchain_buffers(void)
  {
  	struct callchain_cpus_entries *entries;
0793a61d4   Thomas Gleixner   performance count...
1870

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1871
1872
1873
1874
  	entries = callchain_cpus_entries;
  	rcu_assign_pointer(callchain_cpus_entries, NULL);
  	call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  }
0793a61d4   Thomas Gleixner   performance count...
1875

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1876
1877
1878
1879
1880
  static int alloc_callchain_buffers(void)
  {
  	int cpu;
  	int size;
  	struct callchain_cpus_entries *entries;
0793a61d4   Thomas Gleixner   performance count...
1881

c93f76690   Paul Mackerras   perf_counter: Fix...
1882
  	/*
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1883
1884
1885
  	 * We can't use the percpu allocation API for data that can be
  	 * accessed from NMI. Use a temporary manual per cpu allocation
  	 * until that gets sorted out.
c93f76690   Paul Mackerras   perf_counter: Fix...
1886
  	 */
88d4f0db7   Eric Dumazet   perf: Fix alloc_c...
1887
  	size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
c93f76690   Paul Mackerras   perf_counter: Fix...
1888

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1889
1890
1891
  	entries = kzalloc(size, GFP_KERNEL);
  	if (!entries)
  		return -ENOMEM;
c93f76690   Paul Mackerras   perf_counter: Fix...
1892

7ae07ea3a   Frederic Weisbecker   perf: Humanize th...
1893
  	size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
0793a61d4   Thomas Gleixner   performance count...
1894

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1895
1896
1897
1898
1899
  	for_each_possible_cpu(cpu) {
  		entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  							 cpu_to_node(cpu));
  		if (!entries->cpu_entries[cpu])
  			goto fail;
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
1900
  	}
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1901
  	rcu_assign_pointer(callchain_cpus_entries, entries);
0793a61d4   Thomas Gleixner   performance count...
1902

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
  	return 0;
  
  fail:
  	for_each_possible_cpu(cpu)
  		kfree(entries->cpu_entries[cpu]);
  	kfree(entries);
  
  	return -ENOMEM;
  }
  
  static int get_callchain_buffers(void)
  {
  	int err = 0;
  	int count;
  
  	mutex_lock(&callchain_mutex);
  
  	count = atomic_inc_return(&nr_callchain_events);
  	if (WARN_ON_ONCE(count < 1)) {
  		err = -EINVAL;
  		goto exit;
  	}
  
  	if (count > 1) {
  		/* If the allocation failed, give up */
  		if (!callchain_cpus_entries)
  			err = -ENOMEM;
  		goto exit;
  	}
  
  	err = alloc_callchain_buffers();
  	if (err)
  		release_callchain_buffers();
  exit:
  	mutex_unlock(&callchain_mutex);
  
  	return err;
  }
  
  static void put_callchain_buffers(void)
  {
  	if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  		release_callchain_buffers();
  		mutex_unlock(&callchain_mutex);
  	}
  }
  
  static int get_recursion_context(int *recursion)
  {
  	int rctx;
  
  	if (in_nmi())
  		rctx = 3;
  	else if (in_irq())
  		rctx = 2;
  	else if (in_softirq())
  		rctx = 1;
  	else
  		rctx = 0;
  
  	if (recursion[rctx])
  		return -1;
  
  	recursion[rctx]++;
  	barrier();
  
  	return rctx;
  }
  
  static inline void put_recursion_context(int *recursion, int rctx)
  {
  	barrier();
  	recursion[rctx]--;
  }
  
  static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  {
  	int cpu;
  	struct callchain_cpus_entries *entries;
  
  	*rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
  	if (*rctx == -1)
  		return NULL;
  
  	entries = rcu_dereference(callchain_cpus_entries);
  	if (!entries)
  		return NULL;
  
  	cpu = smp_processor_id();
  
  	return &entries->cpu_entries[cpu][*rctx];
  }
  
  static void
  put_callchain_entry(int rctx)
  {
  	put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
  }
  
  static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  {
  	int rctx;
  	struct perf_callchain_entry *entry;
  
  
  	entry = get_callchain_entry(&rctx);
  	if (rctx == -1)
  		return NULL;
  
  	if (!entry)
  		goto exit_put;
  
  	entry->nr = 0;
  
  	if (!user_mode(regs)) {
  		perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  		perf_callchain_kernel(entry, regs);
  		if (current->mm)
  			regs = task_pt_regs(current);
  		else
  			regs = NULL;
  	}
  
  	if (regs) {
  		perf_callchain_store(entry, PERF_CONTEXT_USER);
  		perf_callchain_user(entry, regs);
  	}
  
  exit_put:
  	put_callchain_entry(rctx);
  
  	return entry;
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
2038
   * Initialize the perf_event context in a task_struct:
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2039
   */
eb1844798   Peter Zijlstra   perf: Clean up pe...
2040
  static void __perf_event_init_context(struct perf_event_context *ctx)
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2041
  {
e625cce1b   Thomas Gleixner   perf_event: Conve...
2042
  	raw_spin_lock_init(&ctx->lock);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2043
  	mutex_init(&ctx->mutex);
889ff0150   Frederic Weisbecker   perf/core: Split ...
2044
2045
  	INIT_LIST_HEAD(&ctx->pinned_groups);
  	INIT_LIST_HEAD(&ctx->flexible_groups);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2046
2047
  	INIT_LIST_HEAD(&ctx->event_list);
  	atomic_set(&ctx->refcount, 1);
eb1844798   Peter Zijlstra   perf: Clean up pe...
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
  }
  
  static struct perf_event_context *
  alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  {
  	struct perf_event_context *ctx;
  
  	ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  	if (!ctx)
  		return NULL;
  
  	__perf_event_init_context(ctx);
  	if (task) {
  		ctx->task = task;
  		get_task_struct(task);
0793a61d4   Thomas Gleixner   performance count...
2063
  	}
eb1844798   Peter Zijlstra   perf: Clean up pe...
2064
2065
2066
  	ctx->pmu = pmu;
  
  	return ctx;
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2067
  }
2ebd4ffb6   Matt Helsley   perf events: Spli...
2068
2069
2070
2071
2072
  static struct task_struct *
  find_lively_task_by_vpid(pid_t vpid)
  {
  	struct task_struct *task;
  	int err;
0793a61d4   Thomas Gleixner   performance count...
2073
2074
  
  	rcu_read_lock();
2ebd4ffb6   Matt Helsley   perf events: Spli...
2075
  	if (!vpid)
0793a61d4   Thomas Gleixner   performance count...
2076
2077
  		task = current;
  	else
2ebd4ffb6   Matt Helsley   perf events: Spli...
2078
  		task = find_task_by_vpid(vpid);
0793a61d4   Thomas Gleixner   performance count...
2079
2080
2081
2082
2083
2084
  	if (task)
  		get_task_struct(task);
  	rcu_read_unlock();
  
  	if (!task)
  		return ERR_PTR(-ESRCH);
0793a61d4   Thomas Gleixner   performance count...
2085
  	/* Reuse ptrace permission checks for now. */
c93f76690   Paul Mackerras   perf_counter: Fix...
2086
2087
2088
  	err = -EACCES;
  	if (!ptrace_may_access(task, PTRACE_MODE_READ))
  		goto errout;
2ebd4ffb6   Matt Helsley   perf events: Spli...
2089
2090
2091
2092
2093
2094
  	return task;
  errout:
  	put_task_struct(task);
  	return ERR_PTR(err);
  
  }
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
2095
  static struct perf_event_context *
38a81da22   Matt Helsley   perf events: Clea...
2096
  find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
0793a61d4   Thomas Gleixner   performance count...
2097
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2098
  	struct perf_event_context *ctx;
22a4f650d   Ingo Molnar   perf_counter: Tid...
2099
  	struct perf_cpu_context *cpuctx;
25346b93c   Paul Mackerras   perf_counter: Pro...
2100
  	unsigned long flags;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
2101
  	int ctxn, err;
0793a61d4   Thomas Gleixner   performance count...
2102

22a4ec729   Oleg Nesterov   perf: Find_get_co...
2103
  	if (!task) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2104
  		/* Must be root to operate on a CPU event: */
0764771da   Peter Zijlstra   perf_counter: Mor...
2105
  		if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
0793a61d4   Thomas Gleixner   performance count...
2106
  			return ERR_PTR(-EACCES);
0793a61d4   Thomas Gleixner   performance count...
2107
  		/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
2108
  		 * We could be clever and allow to attach a event to an
0793a61d4   Thomas Gleixner   performance count...
2109
2110
2111
  		 * offline CPU and activate it when the CPU comes up, but
  		 * that's for later.
  		 */
f6325e30e   Rusty Russell   cpumask: use cpu_...
2112
  		if (!cpu_online(cpu))
0793a61d4   Thomas Gleixner   performance count...
2113
  			return ERR_PTR(-ENODEV);
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
2114
  		cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
0793a61d4   Thomas Gleixner   performance count...
2115
  		ctx = &cpuctx->ctx;
c93f76690   Paul Mackerras   perf_counter: Fix...
2116
  		get_ctx(ctx);
0793a61d4   Thomas Gleixner   performance count...
2117

0793a61d4   Thomas Gleixner   performance count...
2118
2119
  		return ctx;
  	}
8dc85d547   Peter Zijlstra   perf: Multiple ta...
2120
2121
2122
2123
  	err = -EINVAL;
  	ctxn = pmu->task_ctx_nr;
  	if (ctxn < 0)
  		goto errout;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
2124
  retry:
8dc85d547   Peter Zijlstra   perf: Multiple ta...
2125
  	ctx = perf_lock_task_context(task, ctxn, &flags);
c93f76690   Paul Mackerras   perf_counter: Fix...
2126
  	if (ctx) {
71a851b4d   Peter Zijlstra   perf_counter: Sto...
2127
  		unclone_ctx(ctx);
e625cce1b   Thomas Gleixner   perf_event: Conve...
2128
  		raw_spin_unlock_irqrestore(&ctx->lock, flags);
0793a61d4   Thomas Gleixner   performance count...
2129
  	}
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2130
  	if (!ctx) {
eb1844798   Peter Zijlstra   perf: Clean up pe...
2131
  		ctx = alloc_perf_context(pmu, task);
c93f76690   Paul Mackerras   perf_counter: Fix...
2132
2133
2134
  		err = -ENOMEM;
  		if (!ctx)
  			goto errout;
eb1844798   Peter Zijlstra   perf: Clean up pe...
2135

c93f76690   Paul Mackerras   perf_counter: Fix...
2136
  		get_ctx(ctx);
eb1844798   Peter Zijlstra   perf: Clean up pe...
2137

dbe08d82c   Oleg Nesterov   perf: Fix find_ge...
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
  		err = 0;
  		mutex_lock(&task->perf_event_mutex);
  		/*
  		 * If it has already passed perf_event_exit_task().
  		 * we must see PF_EXITING, it takes this mutex too.
  		 */
  		if (task->flags & PF_EXITING)
  			err = -ESRCH;
  		else if (task->perf_event_ctxp[ctxn])
  			err = -EAGAIN;
  		else
  			rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  		mutex_unlock(&task->perf_event_mutex);
  
  		if (unlikely(err)) {
eb1844798   Peter Zijlstra   perf: Clean up pe...
2153
  			put_task_struct(task);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2154
  			kfree(ctx);
dbe08d82c   Oleg Nesterov   perf: Fix find_ge...
2155
2156
2157
2158
  
  			if (err == -EAGAIN)
  				goto retry;
  			goto errout;
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
2159
2160
  		}
  	}
0793a61d4   Thomas Gleixner   performance count...
2161
  	return ctx;
c93f76690   Paul Mackerras   perf_counter: Fix...
2162

9ed6060d2   Peter Zijlstra   perf: Unindent la...
2163
  errout:
c93f76690   Paul Mackerras   perf_counter: Fix...
2164
  	return ERR_PTR(err);
0793a61d4   Thomas Gleixner   performance count...
2165
  }
6fb2915df   Li Zefan   tracing/profile: ...
2166
  static void perf_event_free_filter(struct perf_event *event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2167
  static void free_event_rcu(struct rcu_head *head)
592903cdc   Peter Zijlstra   perf_counter: add...
2168
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2169
  	struct perf_event *event;
592903cdc   Peter Zijlstra   perf_counter: add...
2170

cdd6c482c   Ingo Molnar   perf: Do the big ...
2171
2172
2173
  	event = container_of(head, struct perf_event, rcu_head);
  	if (event->ns)
  		put_pid_ns(event->ns);
6fb2915df   Li Zefan   tracing/profile: ...
2174
  	perf_event_free_filter(event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2175
  	kfree(event);
592903cdc   Peter Zijlstra   perf_counter: add...
2176
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2177
  static void perf_buffer_put(struct perf_buffer *buffer);
925d519ab   Peter Zijlstra   perf_counter: uni...
2178

cdd6c482c   Ingo Molnar   perf: Do the big ...
2179
  static void free_event(struct perf_event *event)
f16009527   Peter Zijlstra   perf_counter: fix...
2180
  {
e360adbe2   Peter Zijlstra   irq_work: Add gen...
2181
  	irq_work_sync(&event->pending);
925d519ab   Peter Zijlstra   perf_counter: uni...
2182

cdd6c482c   Ingo Molnar   perf: Do the big ...
2183
  	if (!event->parent) {
82cd6def9   Peter Zijlstra   perf: Use jump_la...
2184
2185
  		if (event->attach_state & PERF_ATTACH_TASK)
  			jump_label_dec(&perf_task_events);
3af9e8592   Eric B Munson   perf: Add non-exe...
2186
  		if (event->attr.mmap || event->attr.mmap_data)
cdd6c482c   Ingo Molnar   perf: Do the big ...
2187
2188
2189
2190
2191
  			atomic_dec(&nr_mmap_events);
  		if (event->attr.comm)
  			atomic_dec(&nr_comm_events);
  		if (event->attr.task)
  			atomic_dec(&nr_task_events);
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
2192
2193
  		if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  			put_callchain_buffers();
f344011cc   Peter Zijlstra   perf_counter: Opt...
2194
  	}
9ee318a78   Peter Zijlstra   perf_counter: opt...
2195

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2196
2197
2198
  	if (event->buffer) {
  		perf_buffer_put(event->buffer);
  		event->buffer = NULL;
a4be7c277   Peter Zijlstra   perf_counter: All...
2199
  	}
cdd6c482c   Ingo Molnar   perf: Do the big ...
2200
2201
  	if (event->destroy)
  		event->destroy(event);
e077df4f4   Peter Zijlstra   perf_counter: hoo...
2202

0c67b4087   Peter Zijlstra   perf: Fix free_ev...
2203
2204
  	if (event->ctx)
  		put_ctx(event->ctx);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2205
  	call_rcu(&event->rcu_head, free_event_rcu);
f16009527   Peter Zijlstra   perf_counter: fix...
2206
  }
a66a3052e   Peter Zijlstra   perf_events: Undo...
2207
  int perf_event_release_kernel(struct perf_event *event)
0793a61d4   Thomas Gleixner   performance count...
2208
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2209
  	struct perf_event_context *ctx = event->ctx;
0793a61d4   Thomas Gleixner   performance count...
2210

050735b08   Peter Zijlstra   perf: Fix exit() ...
2211
2212
2213
2214
2215
  	/*
  	 * Remove from the PMU, can't get re-enabled since we got
  	 * here because the last ref went.
  	 */
  	perf_event_disable(event);
ad3a37de8   Paul Mackerras   perf_counter: Don...
2216
  	WARN_ON_ONCE(ctx->parent_ctx);
a0507c84b   Peter Zijlstra   perf: Annotate pe...
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
  	/*
  	 * There are two ways this annotation is useful:
  	 *
  	 *  1) there is a lock recursion from perf_event_exit_task
  	 *     see the comment there.
  	 *
  	 *  2) there is a lock-inversion with mmap_sem through
  	 *     perf_event_read_group(), which takes faults while
  	 *     holding ctx->mutex, however this is called after
  	 *     the last filedesc died, so there is no possibility
  	 *     to trigger the AB-BA case.
  	 */
  	mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
050735b08   Peter Zijlstra   perf: Fix exit() ...
2230
  	raw_spin_lock_irq(&ctx->lock);
8a49542c0   Peter Zijlstra   perf_events: Fix ...
2231
  	perf_group_detach(event);
050735b08   Peter Zijlstra   perf: Fix exit() ...
2232
  	list_del_event(event, ctx);
050735b08   Peter Zijlstra   perf: Fix exit() ...
2233
  	raw_spin_unlock_irq(&ctx->lock);
d859e29fe   Paul Mackerras   perf_counter: Add...
2234
  	mutex_unlock(&ctx->mutex);
0793a61d4   Thomas Gleixner   performance count...
2235

cdd6c482c   Ingo Molnar   perf: Do the big ...
2236
  	free_event(event);
0793a61d4   Thomas Gleixner   performance count...
2237
2238
2239
  
  	return 0;
  }
a66a3052e   Peter Zijlstra   perf_events: Undo...
2240
  EXPORT_SYMBOL_GPL(perf_event_release_kernel);
0793a61d4   Thomas Gleixner   performance count...
2241

a66a3052e   Peter Zijlstra   perf_events: Undo...
2242
2243
2244
2245
  /*
   * Called when the last reference to the file is gone.
   */
  static int perf_release(struct inode *inode, struct file *file)
fb0459d75   Arjan van de Ven   perf/core: Provid...
2246
  {
a66a3052e   Peter Zijlstra   perf_events: Undo...
2247
  	struct perf_event *event = file->private_data;
8882135bc   Peter Zijlstra   perf: Fix owner-l...
2248
  	struct task_struct *owner;
fb0459d75   Arjan van de Ven   perf/core: Provid...
2249

a66a3052e   Peter Zijlstra   perf_events: Undo...
2250
  	file->private_data = NULL;
fb0459d75   Arjan van de Ven   perf/core: Provid...
2251

8882135bc   Peter Zijlstra   perf: Fix owner-l...
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
  	rcu_read_lock();
  	owner = ACCESS_ONCE(event->owner);
  	/*
  	 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  	 * !owner it means the list deletion is complete and we can indeed
  	 * free this event, otherwise we need to serialize on
  	 * owner->perf_event_mutex.
  	 */
  	smp_read_barrier_depends();
  	if (owner) {
  		/*
  		 * Since delayed_put_task_struct() also drops the last
  		 * task reference we can safely take a new reference
  		 * while holding the rcu_read_lock().
  		 */
  		get_task_struct(owner);
  	}
  	rcu_read_unlock();
  
  	if (owner) {
  		mutex_lock(&owner->perf_event_mutex);
  		/*
  		 * We have to re-check the event->owner field, if it is cleared
  		 * we raced with perf_event_exit_task(), acquiring the mutex
  		 * ensured they're done, and we can proceed with freeing the
  		 * event.
  		 */
  		if (event->owner)
  			list_del_init(&event->owner_entry);
  		mutex_unlock(&owner->perf_event_mutex);
  		put_task_struct(owner);
  	}
a66a3052e   Peter Zijlstra   perf_events: Undo...
2284
  	return perf_event_release_kernel(event);
fb0459d75   Arjan van de Ven   perf/core: Provid...
2285
  }
fb0459d75   Arjan van de Ven   perf/core: Provid...
2286

59ed446f7   Peter Zijlstra   perf: Fix event s...
2287
  u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
e53c09947   Peter Zijlstra   perf_counter: Col...
2288
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2289
  	struct perf_event *child;
e53c09947   Peter Zijlstra   perf_counter: Col...
2290
  	u64 total = 0;
59ed446f7   Peter Zijlstra   perf: Fix event s...
2291
2292
  	*enabled = 0;
  	*running = 0;
6f10581ae   Peter Zijlstra   perf: Fix locking...
2293
  	mutex_lock(&event->child_mutex);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2294
  	total += perf_event_read(event);
59ed446f7   Peter Zijlstra   perf: Fix event s...
2295
2296
2297
2298
2299
2300
  	*enabled += event->total_time_enabled +
  			atomic64_read(&event->child_total_time_enabled);
  	*running += event->total_time_running +
  			atomic64_read(&event->child_total_time_running);
  
  	list_for_each_entry(child, &event->child_list, child_list) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2301
  		total += perf_event_read(child);
59ed446f7   Peter Zijlstra   perf: Fix event s...
2302
2303
2304
  		*enabled += child->total_time_enabled;
  		*running += child->total_time_running;
  	}
6f10581ae   Peter Zijlstra   perf: Fix locking...
2305
  	mutex_unlock(&event->child_mutex);
e53c09947   Peter Zijlstra   perf_counter: Col...
2306
2307
2308
  
  	return total;
  }
fb0459d75   Arjan van de Ven   perf/core: Provid...
2309
  EXPORT_SYMBOL_GPL(perf_event_read_value);
e53c09947   Peter Zijlstra   perf_counter: Col...
2310

cdd6c482c   Ingo Molnar   perf: Do the big ...
2311
  static int perf_event_read_group(struct perf_event *event,
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2312
2313
  				   u64 read_format, char __user *buf)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2314
  	struct perf_event *leader = event->group_leader, *sub;
6f10581ae   Peter Zijlstra   perf: Fix locking...
2315
2316
  	int n = 0, size = 0, ret = -EFAULT;
  	struct perf_event_context *ctx = leader->ctx;
abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2317
  	u64 values[5];
59ed446f7   Peter Zijlstra   perf: Fix event s...
2318
  	u64 count, enabled, running;
abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2319

6f10581ae   Peter Zijlstra   perf: Fix locking...
2320
  	mutex_lock(&ctx->mutex);
59ed446f7   Peter Zijlstra   perf: Fix event s...
2321
  	count = perf_event_read_value(leader, &enabled, &running);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2322
2323
  
  	values[n++] = 1 + leader->nr_siblings;
59ed446f7   Peter Zijlstra   perf: Fix event s...
2324
2325
2326
2327
  	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  		values[n++] = enabled;
  	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  		values[n++] = running;
abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2328
2329
2330
  	values[n++] = count;
  	if (read_format & PERF_FORMAT_ID)
  		values[n++] = primary_event_id(leader);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2331
2332
2333
2334
  
  	size = n * sizeof(u64);
  
  	if (copy_to_user(buf, values, size))
6f10581ae   Peter Zijlstra   perf: Fix locking...
2335
  		goto unlock;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2336

6f10581ae   Peter Zijlstra   perf: Fix locking...
2337
  	ret = size;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2338

65abc8653   Ingo Molnar   perf_counter: Ren...
2339
  	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2340
  		n = 0;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2341

59ed446f7   Peter Zijlstra   perf: Fix event s...
2342
  		values[n++] = perf_event_read_value(sub, &enabled, &running);
abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2343
2344
2345
2346
  		if (read_format & PERF_FORMAT_ID)
  			values[n++] = primary_event_id(sub);
  
  		size = n * sizeof(u64);
184d3da8e   Stephane Eranian   perf_events: Fix ...
2347
  		if (copy_to_user(buf + ret, values, size)) {
6f10581ae   Peter Zijlstra   perf: Fix locking...
2348
2349
2350
  			ret = -EFAULT;
  			goto unlock;
  		}
abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2351
2352
  
  		ret += size;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2353
  	}
6f10581ae   Peter Zijlstra   perf: Fix locking...
2354
2355
  unlock:
  	mutex_unlock(&ctx->mutex);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2356

abf4868b8   Peter Zijlstra   perf: Fix PERF_FO...
2357
  	return ret;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2358
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2359
  static int perf_event_read_one(struct perf_event *event,
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2360
2361
  				 u64 read_format, char __user *buf)
  {
59ed446f7   Peter Zijlstra   perf: Fix event s...
2362
  	u64 enabled, running;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2363
2364
  	u64 values[4];
  	int n = 0;
59ed446f7   Peter Zijlstra   perf: Fix event s...
2365
2366
2367
2368
2369
  	values[n++] = perf_event_read_value(event, &enabled, &running);
  	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  		values[n++] = enabled;
  	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  		values[n++] = running;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2370
  	if (read_format & PERF_FORMAT_ID)
cdd6c482c   Ingo Molnar   perf: Do the big ...
2371
  		values[n++] = primary_event_id(event);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2372
2373
2374
2375
2376
2377
  
  	if (copy_to_user(buf, values, n * sizeof(u64)))
  		return -EFAULT;
  
  	return n * sizeof(u64);
  }
0793a61d4   Thomas Gleixner   performance count...
2378
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
2379
   * Read the performance event - simple non blocking version for now
0793a61d4   Thomas Gleixner   performance count...
2380
2381
   */
  static ssize_t
cdd6c482c   Ingo Molnar   perf: Do the big ...
2382
  perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
0793a61d4   Thomas Gleixner   performance count...
2383
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2384
  	u64 read_format = event->attr.read_format;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2385
  	int ret;
0793a61d4   Thomas Gleixner   performance count...
2386

3b6f9e5cb   Paul Mackerras   perf_counter: Add...
2387
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
2388
  	 * Return end-of-file for a read on a event that is in
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
2389
2390
2391
  	 * error state (i.e. because it was pinned but it couldn't be
  	 * scheduled on to the CPU at some point).
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
2392
  	if (event->state == PERF_EVENT_STATE_ERROR)
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
2393
  		return 0;
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
2394
  	if (count < event->read_size)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2395
  		return -ENOSPC;
cdd6c482c   Ingo Molnar   perf: Do the big ...
2396
  	WARN_ON_ONCE(event->ctx->parent_ctx);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2397
  	if (read_format & PERF_FORMAT_GROUP)
cdd6c482c   Ingo Molnar   perf: Do the big ...
2398
  		ret = perf_event_read_group(event, read_format, buf);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2399
  	else
cdd6c482c   Ingo Molnar   perf: Do the big ...
2400
  		ret = perf_event_read_one(event, read_format, buf);
0793a61d4   Thomas Gleixner   performance count...
2401

3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
2402
  	return ret;
0793a61d4   Thomas Gleixner   performance count...
2403
2404
2405
  }
  
  static ssize_t
0793a61d4   Thomas Gleixner   performance count...
2406
2407
  perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2408
  	struct perf_event *event = file->private_data;
0793a61d4   Thomas Gleixner   performance count...
2409

cdd6c482c   Ingo Molnar   perf: Do the big ...
2410
  	return perf_read_hw(event, buf, count);
0793a61d4   Thomas Gleixner   performance count...
2411
2412
2413
2414
  }
  
  static unsigned int perf_poll(struct file *file, poll_table *wait)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2415
  	struct perf_event *event = file->private_data;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2416
  	struct perf_buffer *buffer;
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
2417
  	unsigned int events = POLL_HUP;
c7138f37f   Peter Zijlstra   perf_counter: fix...
2418
2419
  
  	rcu_read_lock();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2420
2421
2422
  	buffer = rcu_dereference(event->buffer);
  	if (buffer)
  		events = atomic_xchg(&buffer->poll, 0);
c7138f37f   Peter Zijlstra   perf_counter: fix...
2423
  	rcu_read_unlock();
0793a61d4   Thomas Gleixner   performance count...
2424

cdd6c482c   Ingo Molnar   perf: Do the big ...
2425
  	poll_wait(file, &event->waitq, wait);
0793a61d4   Thomas Gleixner   performance count...
2426

0793a61d4   Thomas Gleixner   performance count...
2427
2428
  	return events;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2429
  static void perf_event_reset(struct perf_event *event)
6de6a7b95   Peter Zijlstra   perf_counter: add...
2430
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2431
  	(void)perf_event_read(event);
e78505958   Peter Zijlstra   perf: Convert per...
2432
  	local64_set(&event->count, 0);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2433
  	perf_event_update_userpage(event);
3df5edad8   Peter Zijlstra   perf_counter: rew...
2434
  }
c93f76690   Paul Mackerras   perf_counter: Fix...
2435
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
2436
2437
2438
2439
   * Holding the top-level event's child_mutex means that any
   * descendant process that has inherited this event will block
   * in sync_child_event if it goes to exit, thus satisfying the
   * task existence requirements of perf_event_enable/disable.
c93f76690   Paul Mackerras   perf_counter: Fix...
2440
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
2441
2442
  static void perf_event_for_each_child(struct perf_event *event,
  					void (*func)(struct perf_event *))
3df5edad8   Peter Zijlstra   perf_counter: rew...
2443
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2444
  	struct perf_event *child;
3df5edad8   Peter Zijlstra   perf_counter: rew...
2445

cdd6c482c   Ingo Molnar   perf: Do the big ...
2446
2447
2448
2449
  	WARN_ON_ONCE(event->ctx->parent_ctx);
  	mutex_lock(&event->child_mutex);
  	func(event);
  	list_for_each_entry(child, &event->child_list, child_list)
3df5edad8   Peter Zijlstra   perf_counter: rew...
2450
  		func(child);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2451
  	mutex_unlock(&event->child_mutex);
3df5edad8   Peter Zijlstra   perf_counter: rew...
2452
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2453
2454
  static void perf_event_for_each(struct perf_event *event,
  				  void (*func)(struct perf_event *))
3df5edad8   Peter Zijlstra   perf_counter: rew...
2455
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2456
2457
  	struct perf_event_context *ctx = event->ctx;
  	struct perf_event *sibling;
3df5edad8   Peter Zijlstra   perf_counter: rew...
2458

75f937f24   Peter Zijlstra   perf_counter: Fix...
2459
2460
  	WARN_ON_ONCE(ctx->parent_ctx);
  	mutex_lock(&ctx->mutex);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2461
  	event = event->group_leader;
75f937f24   Peter Zijlstra   perf_counter: Fix...
2462

cdd6c482c   Ingo Molnar   perf: Do the big ...
2463
2464
2465
2466
  	perf_event_for_each_child(event, func);
  	func(event);
  	list_for_each_entry(sibling, &event->sibling_list, group_entry)
  		perf_event_for_each_child(event, func);
75f937f24   Peter Zijlstra   perf_counter: Fix...
2467
  	mutex_unlock(&ctx->mutex);
6de6a7b95   Peter Zijlstra   perf_counter: add...
2468
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2469
  static int perf_event_period(struct perf_event *event, u64 __user *arg)
08247e31c   Peter Zijlstra   perf_counter: Add...
2470
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2471
  	struct perf_event_context *ctx = event->ctx;
08247e31c   Peter Zijlstra   perf_counter: Add...
2472
2473
  	int ret = 0;
  	u64 value;
6c7e550f1   Franck Bui-Huu   perf: Introduce i...
2474
  	if (!is_sampling_event(event))
08247e31c   Peter Zijlstra   perf_counter: Add...
2475
  		return -EINVAL;
ad0cf3478   John Blackwood   perf: Fix incorre...
2476
  	if (copy_from_user(&value, arg, sizeof(value)))
08247e31c   Peter Zijlstra   perf_counter: Add...
2477
2478
2479
2480
  		return -EFAULT;
  
  	if (!value)
  		return -EINVAL;
e625cce1b   Thomas Gleixner   perf_event: Conve...
2481
  	raw_spin_lock_irq(&ctx->lock);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2482
2483
  	if (event->attr.freq) {
  		if (value > sysctl_perf_event_sample_rate) {
08247e31c   Peter Zijlstra   perf_counter: Add...
2484
2485
2486
  			ret = -EINVAL;
  			goto unlock;
  		}
cdd6c482c   Ingo Molnar   perf: Do the big ...
2487
  		event->attr.sample_freq = value;
08247e31c   Peter Zijlstra   perf_counter: Add...
2488
  	} else {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2489
2490
  		event->attr.sample_period = value;
  		event->hw.sample_period = value;
08247e31c   Peter Zijlstra   perf_counter: Add...
2491
2492
  	}
  unlock:
e625cce1b   Thomas Gleixner   perf_event: Conve...
2493
  	raw_spin_unlock_irq(&ctx->lock);
08247e31c   Peter Zijlstra   perf_counter: Add...
2494
2495
2496
  
  	return ret;
  }
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
  static const struct file_operations perf_fops;
  
  static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  {
  	struct file *file;
  
  	file = fget_light(fd, fput_needed);
  	if (!file)
  		return ERR_PTR(-EBADF);
  
  	if (file->f_op != &perf_fops) {
  		fput_light(file, *fput_needed);
  		*fput_needed = 0;
  		return ERR_PTR(-EBADF);
  	}
  
  	return file->private_data;
  }
  
  static int perf_event_set_output(struct perf_event *event,
  				 struct perf_event *output_event);
6fb2915df   Li Zefan   tracing/profile: ...
2518
  static int perf_event_set_filter(struct perf_event *event, void __user *arg);
a4be7c277   Peter Zijlstra   perf_counter: All...
2519

d859e29fe   Paul Mackerras   perf_counter: Add...
2520
2521
  static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2522
2523
  	struct perf_event *event = file->private_data;
  	void (*func)(struct perf_event *);
3df5edad8   Peter Zijlstra   perf_counter: rew...
2524
  	u32 flags = arg;
d859e29fe   Paul Mackerras   perf_counter: Add...
2525
2526
  
  	switch (cmd) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2527
2528
  	case PERF_EVENT_IOC_ENABLE:
  		func = perf_event_enable;
d859e29fe   Paul Mackerras   perf_counter: Add...
2529
  		break;
cdd6c482c   Ingo Molnar   perf: Do the big ...
2530
2531
  	case PERF_EVENT_IOC_DISABLE:
  		func = perf_event_disable;
79f146415   Peter Zijlstra   perf_counter: cou...
2532
  		break;
cdd6c482c   Ingo Molnar   perf: Do the big ...
2533
2534
  	case PERF_EVENT_IOC_RESET:
  		func = perf_event_reset;
6de6a7b95   Peter Zijlstra   perf_counter: add...
2535
  		break;
3df5edad8   Peter Zijlstra   perf_counter: rew...
2536

cdd6c482c   Ingo Molnar   perf: Do the big ...
2537
2538
  	case PERF_EVENT_IOC_REFRESH:
  		return perf_event_refresh(event, arg);
08247e31c   Peter Zijlstra   perf_counter: Add...
2539

cdd6c482c   Ingo Molnar   perf: Do the big ...
2540
2541
  	case PERF_EVENT_IOC_PERIOD:
  		return perf_event_period(event, (u64 __user *)arg);
08247e31c   Peter Zijlstra   perf_counter: Add...
2542

cdd6c482c   Ingo Molnar   perf: Do the big ...
2543
  	case PERF_EVENT_IOC_SET_OUTPUT:
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
  	{
  		struct perf_event *output_event = NULL;
  		int fput_needed = 0;
  		int ret;
  
  		if (arg != -1) {
  			output_event = perf_fget_light(arg, &fput_needed);
  			if (IS_ERR(output_event))
  				return PTR_ERR(output_event);
  		}
  
  		ret = perf_event_set_output(event, output_event);
  		if (output_event)
  			fput_light(output_event->filp, fput_needed);
  
  		return ret;
  	}
a4be7c277   Peter Zijlstra   perf_counter: All...
2561

6fb2915df   Li Zefan   tracing/profile: ...
2562
2563
  	case PERF_EVENT_IOC_SET_FILTER:
  		return perf_event_set_filter(event, (void __user *)arg);
d859e29fe   Paul Mackerras   perf_counter: Add...
2564
  	default:
3df5edad8   Peter Zijlstra   perf_counter: rew...
2565
  		return -ENOTTY;
d859e29fe   Paul Mackerras   perf_counter: Add...
2566
  	}
3df5edad8   Peter Zijlstra   perf_counter: rew...
2567
2568
  
  	if (flags & PERF_IOC_FLAG_GROUP)
cdd6c482c   Ingo Molnar   perf: Do the big ...
2569
  		perf_event_for_each(event, func);
3df5edad8   Peter Zijlstra   perf_counter: rew...
2570
  	else
cdd6c482c   Ingo Molnar   perf: Do the big ...
2571
  		perf_event_for_each_child(event, func);
3df5edad8   Peter Zijlstra   perf_counter: rew...
2572
2573
  
  	return 0;
d859e29fe   Paul Mackerras   perf_counter: Add...
2574
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2575
  int perf_event_task_enable(void)
771d7cde1   Peter Zijlstra   perf_counter: Mak...
2576
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2577
  	struct perf_event *event;
771d7cde1   Peter Zijlstra   perf_counter: Mak...
2578

cdd6c482c   Ingo Molnar   perf: Do the big ...
2579
2580
2581
2582
  	mutex_lock(&current->perf_event_mutex);
  	list_for_each_entry(event, &current->perf_event_list, owner_entry)
  		perf_event_for_each_child(event, perf_event_enable);
  	mutex_unlock(&current->perf_event_mutex);
771d7cde1   Peter Zijlstra   perf_counter: Mak...
2583
2584
2585
  
  	return 0;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2586
  int perf_event_task_disable(void)
771d7cde1   Peter Zijlstra   perf_counter: Mak...
2587
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2588
  	struct perf_event *event;
771d7cde1   Peter Zijlstra   perf_counter: Mak...
2589

cdd6c482c   Ingo Molnar   perf: Do the big ...
2590
2591
2592
2593
  	mutex_lock(&current->perf_event_mutex);
  	list_for_each_entry(event, &current->perf_event_list, owner_entry)
  		perf_event_for_each_child(event, perf_event_disable);
  	mutex_unlock(&current->perf_event_mutex);
771d7cde1   Peter Zijlstra   perf_counter: Mak...
2594
2595
2596
  
  	return 0;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
2597
2598
  #ifndef PERF_EVENT_INDEX_OFFSET
  # define PERF_EVENT_INDEX_OFFSET 0
f738eb1b6   Ingo Molnar   perf_counter: Fix...
2599
  #endif
cdd6c482c   Ingo Molnar   perf: Do the big ...
2600
  static int perf_event_index(struct perf_event *event)
194002b27   Peter Zijlstra   perf_counter, x86...
2601
  {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
2602
2603
  	if (event->hw.state & PERF_HES_STOPPED)
  		return 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
2604
  	if (event->state != PERF_EVENT_STATE_ACTIVE)
194002b27   Peter Zijlstra   perf_counter, x86...
2605
  		return 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
2606
  	return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
194002b27   Peter Zijlstra   perf_counter, x86...
2607
  }
38ff667b3   Peter Zijlstra   perf_counter: fix...
2608
2609
2610
2611
2612
  /*
   * Callers need to ensure there can be no nesting of this function, otherwise
   * the seqlock logic goes bad. We can not serialize this because the arch
   * code calls this from NMI context.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
2613
  void perf_event_update_userpage(struct perf_event *event)
37d818283   Paul Mackerras   perf_counter: add...
2614
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2615
  	struct perf_event_mmap_page *userpg;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2616
  	struct perf_buffer *buffer;
38ff667b3   Peter Zijlstra   perf_counter: fix...
2617
2618
  
  	rcu_read_lock();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2619
2620
  	buffer = rcu_dereference(event->buffer);
  	if (!buffer)
38ff667b3   Peter Zijlstra   perf_counter: fix...
2621
  		goto unlock;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2622
  	userpg = buffer->user_page;
37d818283   Paul Mackerras   perf_counter: add...
2623

7b732a750   Peter Zijlstra   perf_counter: new...
2624
2625
2626
2627
2628
  	/*
  	 * Disable preemption so as to not let the corresponding user-space
  	 * spin too long if we get preempted.
  	 */
  	preempt_disable();
37d818283   Paul Mackerras   perf_counter: add...
2629
  	++userpg->lock;
92f22a386   Peter Zijlstra   perf_counter: upd...
2630
  	barrier();
cdd6c482c   Ingo Molnar   perf: Do the big ...
2631
  	userpg->index = perf_event_index(event);
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
2632
  	userpg->offset = perf_event_count(event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2633
  	if (event->state == PERF_EVENT_STATE_ACTIVE)
e78505958   Peter Zijlstra   perf: Convert per...
2634
  		userpg->offset -= local64_read(&event->hw.prev_count);
7b732a750   Peter Zijlstra   perf_counter: new...
2635

cdd6c482c   Ingo Molnar   perf: Do the big ...
2636
2637
  	userpg->time_enabled = event->total_time_enabled +
  			atomic64_read(&event->child_total_time_enabled);
7f8b4e4e0   Peter Zijlstra   perf_counter: Add...
2638

cdd6c482c   Ingo Molnar   perf: Do the big ...
2639
2640
  	userpg->time_running = event->total_time_running +
  			atomic64_read(&event->child_total_time_running);
7f8b4e4e0   Peter Zijlstra   perf_counter: Add...
2641

92f22a386   Peter Zijlstra   perf_counter: upd...
2642
  	barrier();
37d818283   Paul Mackerras   perf_counter: add...
2643
  	++userpg->lock;
7b732a750   Peter Zijlstra   perf_counter: new...
2644
  	preempt_enable();
38ff667b3   Peter Zijlstra   perf_counter: fix...
2645
  unlock:
7b732a750   Peter Zijlstra   perf_counter: new...
2646
  	rcu_read_unlock();
37d818283   Paul Mackerras   perf_counter: add...
2647
  }
d57e34fdd   Peter Zijlstra   perf: Simplify th...
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
  static unsigned long perf_data_size(struct perf_buffer *buffer);
  
  static void
  perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  {
  	long max_size = perf_data_size(buffer);
  
  	if (watermark)
  		buffer->watermark = min(max_size, watermark);
  
  	if (!buffer->watermark)
  		buffer->watermark = max_size / 2;
  
  	if (flags & PERF_BUFFER_WRITABLE)
  		buffer->writable = 1;
  
  	atomic_set(&buffer->refcount, 1);
  }
906010b21   Peter Zijlstra   perf_event: Provi...
2666
  #ifndef CONFIG_PERF_USE_VMALLOC
43a21ea81   Peter Zijlstra   perf_counter: Add...
2667

906010b21   Peter Zijlstra   perf_event: Provi...
2668
2669
2670
  /*
   * Back perf_mmap() with regular GFP_KERNEL-0 pages.
   */
43a21ea81   Peter Zijlstra   perf_counter: Add...
2671

906010b21   Peter Zijlstra   perf_event: Provi...
2672
  static struct page *
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2673
  perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
906010b21   Peter Zijlstra   perf_event: Provi...
2674
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2675
  	if (pgoff > buffer->nr_pages)
906010b21   Peter Zijlstra   perf_event: Provi...
2676
  		return NULL;
43a21ea81   Peter Zijlstra   perf_counter: Add...
2677

906010b21   Peter Zijlstra   perf_event: Provi...
2678
  	if (pgoff == 0)
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2679
  		return virt_to_page(buffer->user_page);
7b732a750   Peter Zijlstra   perf_counter: new...
2680

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2681
  	return virt_to_page(buffer->data_pages[pgoff - 1]);
7b732a750   Peter Zijlstra   perf_counter: new...
2682
  }
a19d35c11   Peter Zijlstra   perf: Optimize bu...
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
  static void *perf_mmap_alloc_page(int cpu)
  {
  	struct page *page;
  	int node;
  
  	node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  	page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  	if (!page)
  		return NULL;
  
  	return page_address(page);
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2695
  static struct perf_buffer *
d57e34fdd   Peter Zijlstra   perf: Simplify th...
2696
  perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
7b732a750   Peter Zijlstra   perf_counter: new...
2697
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2698
  	struct perf_buffer *buffer;
7b732a750   Peter Zijlstra   perf_counter: new...
2699
2700
  	unsigned long size;
  	int i;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2701
  	size = sizeof(struct perf_buffer);
7b732a750   Peter Zijlstra   perf_counter: new...
2702
  	size += nr_pages * sizeof(void *);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2703
2704
  	buffer = kzalloc(size, GFP_KERNEL);
  	if (!buffer)
7b732a750   Peter Zijlstra   perf_counter: new...
2705
  		goto fail;
d57e34fdd   Peter Zijlstra   perf: Simplify th...
2706
  	buffer->user_page = perf_mmap_alloc_page(cpu);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2707
  	if (!buffer->user_page)
7b732a750   Peter Zijlstra   perf_counter: new...
2708
2709
2710
  		goto fail_user_page;
  
  	for (i = 0; i < nr_pages; i++) {
d57e34fdd   Peter Zijlstra   perf: Simplify th...
2711
  		buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2712
  		if (!buffer->data_pages[i])
7b732a750   Peter Zijlstra   perf_counter: new...
2713
2714
  			goto fail_data_pages;
  	}
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2715
  	buffer->nr_pages = nr_pages;
7b732a750   Peter Zijlstra   perf_counter: new...
2716

d57e34fdd   Peter Zijlstra   perf: Simplify th...
2717
  	perf_buffer_init(buffer, watermark, flags);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2718
  	return buffer;
7b732a750   Peter Zijlstra   perf_counter: new...
2719
2720
2721
  
  fail_data_pages:
  	for (i--; i >= 0; i--)
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2722
  		free_page((unsigned long)buffer->data_pages[i]);
7b732a750   Peter Zijlstra   perf_counter: new...
2723

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2724
  	free_page((unsigned long)buffer->user_page);
7b732a750   Peter Zijlstra   perf_counter: new...
2725
2726
  
  fail_user_page:
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2727
  	kfree(buffer);
7b732a750   Peter Zijlstra   perf_counter: new...
2728
2729
  
  fail:
906010b21   Peter Zijlstra   perf_event: Provi...
2730
  	return NULL;
7b732a750   Peter Zijlstra   perf_counter: new...
2731
  }
43a21ea81   Peter Zijlstra   perf_counter: Add...
2732
2733
  static void perf_mmap_free_page(unsigned long addr)
  {
5bfd75609   Kevin Cernekee   Fix virt_to_phys(...
2734
  	struct page *page = virt_to_page((void *)addr);
43a21ea81   Peter Zijlstra   perf_counter: Add...
2735
2736
2737
2738
  
  	page->mapping = NULL;
  	__free_page(page);
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2739
  static void perf_buffer_free(struct perf_buffer *buffer)
7b732a750   Peter Zijlstra   perf_counter: new...
2740
  {
7b732a750   Peter Zijlstra   perf_counter: new...
2741
  	int i;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2742
2743
2744
2745
  	perf_mmap_free_page((unsigned long)buffer->user_page);
  	for (i = 0; i < buffer->nr_pages; i++)
  		perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  	kfree(buffer);
906010b21   Peter Zijlstra   perf_event: Provi...
2746
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2747
  static inline int page_order(struct perf_buffer *buffer)
3cafa9fbb   Peter Zijlstra   perf: Optimize th...
2748
2749
2750
  {
  	return 0;
  }
906010b21   Peter Zijlstra   perf_event: Provi...
2751
2752
2753
2754
2755
2756
2757
  #else
  
  /*
   * Back perf_mmap() with vmalloc memory.
   *
   * Required for architectures that have d-cache aliasing issues.
   */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2758
  static inline int page_order(struct perf_buffer *buffer)
3cafa9fbb   Peter Zijlstra   perf: Optimize th...
2759
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2760
  	return buffer->page_order;
3cafa9fbb   Peter Zijlstra   perf: Optimize th...
2761
  }
906010b21   Peter Zijlstra   perf_event: Provi...
2762
  static struct page *
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2763
  perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
906010b21   Peter Zijlstra   perf_event: Provi...
2764
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2765
  	if (pgoff > (1UL << page_order(buffer)))
906010b21   Peter Zijlstra   perf_event: Provi...
2766
  		return NULL;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2767
  	return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
906010b21   Peter Zijlstra   perf_event: Provi...
2768
2769
2770
2771
2772
2773
2774
2775
  }
  
  static void perf_mmap_unmark_page(void *addr)
  {
  	struct page *page = vmalloc_to_page(addr);
  
  	page->mapping = NULL;
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2776
  static void perf_buffer_free_work(struct work_struct *work)
906010b21   Peter Zijlstra   perf_event: Provi...
2777
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2778
  	struct perf_buffer *buffer;
906010b21   Peter Zijlstra   perf_event: Provi...
2779
2780
  	void *base;
  	int i, nr;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2781
2782
  	buffer = container_of(work, struct perf_buffer, work);
  	nr = 1 << page_order(buffer);
906010b21   Peter Zijlstra   perf_event: Provi...
2783

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2784
  	base = buffer->user_page;
906010b21   Peter Zijlstra   perf_event: Provi...
2785
2786
2787
2788
  	for (i = 0; i < nr + 1; i++)
  		perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  
  	vfree(base);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2789
  	kfree(buffer);
906010b21   Peter Zijlstra   perf_event: Provi...
2790
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2791
  static void perf_buffer_free(struct perf_buffer *buffer)
906010b21   Peter Zijlstra   perf_event: Provi...
2792
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2793
  	schedule_work(&buffer->work);
906010b21   Peter Zijlstra   perf_event: Provi...
2794
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2795
  static struct perf_buffer *
d57e34fdd   Peter Zijlstra   perf: Simplify th...
2796
  perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
906010b21   Peter Zijlstra   perf_event: Provi...
2797
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2798
  	struct perf_buffer *buffer;
906010b21   Peter Zijlstra   perf_event: Provi...
2799
2800
  	unsigned long size;
  	void *all_buf;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2801
  	size = sizeof(struct perf_buffer);
906010b21   Peter Zijlstra   perf_event: Provi...
2802
  	size += sizeof(void *);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2803
2804
  	buffer = kzalloc(size, GFP_KERNEL);
  	if (!buffer)
906010b21   Peter Zijlstra   perf_event: Provi...
2805
  		goto fail;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2806
  	INIT_WORK(&buffer->work, perf_buffer_free_work);
906010b21   Peter Zijlstra   perf_event: Provi...
2807
2808
2809
2810
  
  	all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  	if (!all_buf)
  		goto fail_all_buf;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2811
2812
2813
2814
  	buffer->user_page = all_buf;
  	buffer->data_pages[0] = all_buf + PAGE_SIZE;
  	buffer->page_order = ilog2(nr_pages);
  	buffer->nr_pages = 1;
906010b21   Peter Zijlstra   perf_event: Provi...
2815

d57e34fdd   Peter Zijlstra   perf: Simplify th...
2816
  	perf_buffer_init(buffer, watermark, flags);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2817
  	return buffer;
906010b21   Peter Zijlstra   perf_event: Provi...
2818
2819
  
  fail_all_buf:
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2820
  	kfree(buffer);
906010b21   Peter Zijlstra   perf_event: Provi...
2821
2822
2823
2824
2825
2826
  
  fail:
  	return NULL;
  }
  
  #endif
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2827
  static unsigned long perf_data_size(struct perf_buffer *buffer)
3cafa9fbb   Peter Zijlstra   perf: Optimize th...
2828
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2829
  	return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
3cafa9fbb   Peter Zijlstra   perf: Optimize th...
2830
  }
906010b21   Peter Zijlstra   perf_event: Provi...
2831
2832
2833
  static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  {
  	struct perf_event *event = vma->vm_file->private_data;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2834
  	struct perf_buffer *buffer;
906010b21   Peter Zijlstra   perf_event: Provi...
2835
2836
2837
2838
2839
2840
2841
2842
2843
  	int ret = VM_FAULT_SIGBUS;
  
  	if (vmf->flags & FAULT_FLAG_MKWRITE) {
  		if (vmf->pgoff == 0)
  			ret = 0;
  		return ret;
  	}
  
  	rcu_read_lock();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2844
2845
  	buffer = rcu_dereference(event->buffer);
  	if (!buffer)
906010b21   Peter Zijlstra   perf_event: Provi...
2846
2847
2848
2849
  		goto unlock;
  
  	if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  		goto unlock;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2850
  	vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
906010b21   Peter Zijlstra   perf_event: Provi...
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
  	if (!vmf->page)
  		goto unlock;
  
  	get_page(vmf->page);
  	vmf->page->mapping = vma->vm_file->f_mapping;
  	vmf->page->index   = vmf->pgoff;
  
  	ret = 0;
  unlock:
  	rcu_read_unlock();
  
  	return ret;
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2864
  static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
906010b21   Peter Zijlstra   perf_event: Provi...
2865
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2866
  	struct perf_buffer *buffer;
906010b21   Peter Zijlstra   perf_event: Provi...
2867

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2868
2869
  	buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  	perf_buffer_free(buffer);
7b732a750   Peter Zijlstra   perf_counter: new...
2870
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2871
  static struct perf_buffer *perf_buffer_get(struct perf_event *event)
7b732a750   Peter Zijlstra   perf_counter: new...
2872
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2873
  	struct perf_buffer *buffer;
7b732a750   Peter Zijlstra   perf_counter: new...
2874

ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2875
  	rcu_read_lock();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2876
2877
2878
2879
  	buffer = rcu_dereference(event->buffer);
  	if (buffer) {
  		if (!atomic_inc_not_zero(&buffer->refcount))
  			buffer = NULL;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2880
2881
  	}
  	rcu_read_unlock();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2882
  	return buffer;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2883
  }
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2884
  static void perf_buffer_put(struct perf_buffer *buffer)
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2885
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2886
  	if (!atomic_dec_and_test(&buffer->refcount))
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2887
  		return;
7b732a750   Peter Zijlstra   perf_counter: new...
2888

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2889
  	call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
7b732a750   Peter Zijlstra   perf_counter: new...
2890
2891
2892
2893
  }
  
  static void perf_mmap_open(struct vm_area_struct *vma)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2894
  	struct perf_event *event = vma->vm_file->private_data;
7b732a750   Peter Zijlstra   perf_counter: new...
2895

cdd6c482c   Ingo Molnar   perf: Do the big ...
2896
  	atomic_inc(&event->mmap_count);
7b732a750   Peter Zijlstra   perf_counter: new...
2897
2898
2899
2900
  }
  
  static void perf_mmap_close(struct vm_area_struct *vma)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2901
  	struct perf_event *event = vma->vm_file->private_data;
7b732a750   Peter Zijlstra   perf_counter: new...
2902

cdd6c482c   Ingo Molnar   perf: Do the big ...
2903
  	if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2904
  		unsigned long size = perf_data_size(event->buffer);
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2905
  		struct user_struct *user = event->mmap_user;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2906
  		struct perf_buffer *buffer = event->buffer;
789f90fcf   Peter Zijlstra   perf_counter: per...
2907

906010b21   Peter Zijlstra   perf_event: Provi...
2908
  		atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2909
  		vma->vm_mm->locked_vm -= event->mmap_locked;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2910
  		rcu_assign_pointer(event->buffer, NULL);
cdd6c482c   Ingo Molnar   perf: Do the big ...
2911
  		mutex_unlock(&event->mmap_mutex);
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2912

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2913
  		perf_buffer_put(buffer);
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2914
  		free_uid(user);
7b732a750   Peter Zijlstra   perf_counter: new...
2915
  	}
37d818283   Paul Mackerras   perf_counter: add...
2916
  }
f0f37e2f7   Alexey Dobriyan   const: mark struc...
2917
  static const struct vm_operations_struct perf_mmap_vmops = {
43a21ea81   Peter Zijlstra   perf_counter: Add...
2918
2919
2920
2921
  	.open		= perf_mmap_open,
  	.close		= perf_mmap_close,
  	.fault		= perf_mmap_fault,
  	.page_mkwrite	= perf_mmap_fault,
37d818283   Paul Mackerras   perf_counter: add...
2922
2923
2924
2925
  };
  
  static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
2926
  	struct perf_event *event = file->private_data;
22a4f650d   Ingo Molnar   perf_counter: Tid...
2927
  	unsigned long user_locked, user_lock_limit;
789f90fcf   Peter Zijlstra   perf_counter: per...
2928
  	struct user_struct *user = current_user();
22a4f650d   Ingo Molnar   perf_counter: Tid...
2929
  	unsigned long locked, lock_limit;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2930
  	struct perf_buffer *buffer;
7b732a750   Peter Zijlstra   perf_counter: new...
2931
2932
  	unsigned long vma_size;
  	unsigned long nr_pages;
789f90fcf   Peter Zijlstra   perf_counter: per...
2933
  	long user_extra, extra;
d57e34fdd   Peter Zijlstra   perf: Simplify th...
2934
  	int ret = 0, flags = 0;
37d818283   Paul Mackerras   perf_counter: add...
2935

c7920614c   Peter Zijlstra   perf: Disallow mm...
2936
2937
2938
2939
2940
2941
2942
  	/*
  	 * Don't allow mmap() of inherited per-task counters. This would
  	 * create a performance issue due to all children writing to the
  	 * same buffer.
  	 */
  	if (event->cpu == -1 && event->attr.inherit)
  		return -EINVAL;
43a21ea81   Peter Zijlstra   perf_counter: Add...
2943
  	if (!(vma->vm_flags & VM_SHARED))
37d818283   Paul Mackerras   perf_counter: add...
2944
  		return -EINVAL;
7b732a750   Peter Zijlstra   perf_counter: new...
2945
2946
2947
  
  	vma_size = vma->vm_end - vma->vm_start;
  	nr_pages = (vma_size / PAGE_SIZE) - 1;
7730d8655   Peter Zijlstra   perf_counter: all...
2948
  	/*
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2949
  	 * If we have buffer pages ensure they're a power-of-two number, so we
7730d8655   Peter Zijlstra   perf_counter: all...
2950
2951
2952
  	 * can do bitmasks instead of modulo.
  	 */
  	if (nr_pages != 0 && !is_power_of_2(nr_pages))
37d818283   Paul Mackerras   perf_counter: add...
2953
  		return -EINVAL;
7b732a750   Peter Zijlstra   perf_counter: new...
2954
  	if (vma_size != PAGE_SIZE * (1 + nr_pages))
37d818283   Paul Mackerras   perf_counter: add...
2955
  		return -EINVAL;
7b732a750   Peter Zijlstra   perf_counter: new...
2956
2957
  	if (vma->vm_pgoff != 0)
  		return -EINVAL;
37d818283   Paul Mackerras   perf_counter: add...
2958

cdd6c482c   Ingo Molnar   perf: Do the big ...
2959
2960
  	WARN_ON_ONCE(event->ctx->parent_ctx);
  	mutex_lock(&event->mmap_mutex);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2961
2962
2963
  	if (event->buffer) {
  		if (event->buffer->nr_pages == nr_pages)
  			atomic_inc(&event->buffer->refcount);
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2964
  		else
ebb3c4c4c   Peter Zijlstra   perf_counter: fix...
2965
2966
2967
  			ret = -EINVAL;
  		goto unlock;
  	}
789f90fcf   Peter Zijlstra   perf_counter: per...
2968
  	user_extra = nr_pages + 1;
cdd6c482c   Ingo Molnar   perf: Do the big ...
2969
  	user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
a3862d3f8   Ingo Molnar   perf_counter: Inc...
2970
2971
2972
2973
2974
  
  	/*
  	 * Increase the limit linearly with more CPUs:
  	 */
  	user_lock_limit *= num_online_cpus();
789f90fcf   Peter Zijlstra   perf_counter: per...
2975
  	user_locked = atomic_long_read(&user->locked_vm) + user_extra;
c5078f78b   Peter Zijlstra   perf_counter: pro...
2976

789f90fcf   Peter Zijlstra   perf_counter: per...
2977
2978
2979
  	extra = 0;
  	if (user_locked > user_lock_limit)
  		extra = user_locked - user_lock_limit;
7b732a750   Peter Zijlstra   perf_counter: new...
2980

78d7d407b   Jiri Slaby   kernel core: use ...
2981
  	lock_limit = rlimit(RLIMIT_MEMLOCK);
7b732a750   Peter Zijlstra   perf_counter: new...
2982
  	lock_limit >>= PAGE_SHIFT;
789f90fcf   Peter Zijlstra   perf_counter: per...
2983
  	locked = vma->vm_mm->locked_vm + extra;
7b732a750   Peter Zijlstra   perf_counter: new...
2984

459ec28ab   Ingo Molnar   perf_counter: All...
2985
2986
  	if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  		!capable(CAP_IPC_LOCK)) {
ebb3c4c4c   Peter Zijlstra   perf_counter: fix...
2987
2988
2989
  		ret = -EPERM;
  		goto unlock;
  	}
7b732a750   Peter Zijlstra   perf_counter: new...
2990

ca5135e6b   Peter Zijlstra   perf: Rename perf...
2991
  	WARN_ON(event->buffer);
906010b21   Peter Zijlstra   perf_event: Provi...
2992

d57e34fdd   Peter Zijlstra   perf: Simplify th...
2993
2994
2995
2996
2997
  	if (vma->vm_flags & VM_WRITE)
  		flags |= PERF_BUFFER_WRITABLE;
  
  	buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  				   event->cpu, flags);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
2998
  	if (!buffer) {
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
2999
  		ret = -ENOMEM;
ebb3c4c4c   Peter Zijlstra   perf_counter: fix...
3000
  		goto unlock;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
3001
  	}
d57e34fdd   Peter Zijlstra   perf: Simplify th...
3002
  	rcu_assign_pointer(event->buffer, buffer);
43a21ea81   Peter Zijlstra   perf_counter: Add...
3003

ac9721f3f   Peter Zijlstra   perf_events: Fix ...
3004
3005
3006
3007
  	atomic_long_add(user_extra, &user->locked_vm);
  	event->mmap_locked = extra;
  	event->mmap_user = get_current_user();
  	vma->vm_mm->locked_vm += event->mmap_locked;
ebb3c4c4c   Peter Zijlstra   perf_counter: fix...
3008
  unlock:
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
3009
3010
  	if (!ret)
  		atomic_inc(&event->mmap_count);
cdd6c482c   Ingo Molnar   perf: Do the big ...
3011
  	mutex_unlock(&event->mmap_mutex);
37d818283   Paul Mackerras   perf_counter: add...
3012

37d818283   Paul Mackerras   perf_counter: add...
3013
3014
  	vma->vm_flags |= VM_RESERVED;
  	vma->vm_ops = &perf_mmap_vmops;
7b732a750   Peter Zijlstra   perf_counter: new...
3015
3016
  
  	return ret;
37d818283   Paul Mackerras   perf_counter: add...
3017
  }
3c446b3d3   Peter Zijlstra   perf_counter: SIG...
3018
3019
  static int perf_fasync(int fd, struct file *filp, int on)
  {
3c446b3d3   Peter Zijlstra   perf_counter: SIG...
3020
  	struct inode *inode = filp->f_path.dentry->d_inode;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3021
  	struct perf_event *event = filp->private_data;
3c446b3d3   Peter Zijlstra   perf_counter: SIG...
3022
3023
3024
  	int retval;
  
  	mutex_lock(&inode->i_mutex);
cdd6c482c   Ingo Molnar   perf: Do the big ...
3025
  	retval = fasync_helper(fd, filp, on, &event->fasync);
3c446b3d3   Peter Zijlstra   perf_counter: SIG...
3026
3027
3028
3029
3030
3031
3032
  	mutex_unlock(&inode->i_mutex);
  
  	if (retval < 0)
  		return retval;
  
  	return 0;
  }
0793a61d4   Thomas Gleixner   performance count...
3033
  static const struct file_operations perf_fops = {
3326c1cee   Arnd Bergmann   perf_event: Make ...
3034
  	.llseek			= no_llseek,
0793a61d4   Thomas Gleixner   performance count...
3035
3036
3037
  	.release		= perf_release,
  	.read			= perf_read,
  	.poll			= perf_poll,
d859e29fe   Paul Mackerras   perf_counter: Add...
3038
3039
  	.unlocked_ioctl		= perf_ioctl,
  	.compat_ioctl		= perf_ioctl,
37d818283   Paul Mackerras   perf_counter: add...
3040
  	.mmap			= perf_mmap,
3c446b3d3   Peter Zijlstra   perf_counter: SIG...
3041
  	.fasync			= perf_fasync,
0793a61d4   Thomas Gleixner   performance count...
3042
  };
15dbf27cc   Peter Zijlstra   perf_counter: sof...
3043
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
3044
   * Perf event wakeup
925d519ab   Peter Zijlstra   perf_counter: uni...
3045
3046
3047
3048
   *
   * If there's data, ensure we set the poll() state and publish everything
   * to user-space before waking everybody up.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
3049
  void perf_event_wakeup(struct perf_event *event)
925d519ab   Peter Zijlstra   perf_counter: uni...
3050
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3051
  	wake_up_all(&event->waitq);
4c9e25428   Peter Zijlstra   perf_counter: cha...
3052

cdd6c482c   Ingo Molnar   perf: Do the big ...
3053
3054
3055
  	if (event->pending_kill) {
  		kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  		event->pending_kill = 0;
4c9e25428   Peter Zijlstra   perf_counter: cha...
3056
  	}
925d519ab   Peter Zijlstra   perf_counter: uni...
3057
  }
e360adbe2   Peter Zijlstra   irq_work: Add gen...
3058
  static void perf_pending_event(struct irq_work *entry)
79f146415   Peter Zijlstra   perf_counter: cou...
3059
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3060
3061
  	struct perf_event *event = container_of(entry,
  			struct perf_event, pending);
79f146415   Peter Zijlstra   perf_counter: cou...
3062

cdd6c482c   Ingo Molnar   perf: Do the big ...
3063
3064
3065
  	if (event->pending_disable) {
  		event->pending_disable = 0;
  		__perf_event_disable(event);
79f146415   Peter Zijlstra   perf_counter: cou...
3066
  	}
cdd6c482c   Ingo Molnar   perf: Do the big ...
3067
3068
3069
  	if (event->pending_wakeup) {
  		event->pending_wakeup = 0;
  		perf_event_wakeup(event);
79f146415   Peter Zijlstra   perf_counter: cou...
3070
3071
  	}
  }
394ee0762   Peter Zijlstra   perf_counter: pro...
3072
  /*
39447b386   Zhang, Yanmin   perf: Enhance per...
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
   * We assume there is only KVM supporting the callbacks.
   * Later on, we might change it to a list if there is
   * another virtualization implementation supporting the callbacks.
   */
  struct perf_guest_info_callbacks *perf_guest_cbs;
  
  int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  {
  	perf_guest_cbs = cbs;
  	return 0;
  }
  EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  
  int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  {
  	perf_guest_cbs = NULL;
  	return 0;
  }
  EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  
  /*
0322cd6ec   Peter Zijlstra   perf_counter: uni...
3094
3095
   * Output
   */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3096
  static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
2667de81f   Peter Zijlstra   perf_counter: All...
3097
  			      unsigned long offset, unsigned long head)
43a21ea81   Peter Zijlstra   perf_counter: Add...
3098
  {
43a21ea81   Peter Zijlstra   perf_counter: Add...
3099
  	unsigned long mask;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3100
  	if (!buffer->writable)
43a21ea81   Peter Zijlstra   perf_counter: Add...
3101
  		return true;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3102
  	mask = perf_data_size(buffer) - 1;
43a21ea81   Peter Zijlstra   perf_counter: Add...
3103
3104
3105
3106
3107
3108
3109
3110
3111
  
  	offset = (offset - tail) & mask;
  	head   = (head   - tail) & mask;
  
  	if ((int)(head - offset) < 0)
  		return false;
  
  	return true;
  }
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3112
  static void perf_output_wakeup(struct perf_output_handle *handle)
78d613eb1   Peter Zijlstra   perf_counter: sma...
3113
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3114
  	atomic_set(&handle->buffer->poll, POLL_IN);
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3115

671dec5da   Peter Zijlstra   perf_counter: gen...
3116
  	if (handle->nmi) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3117
  		handle->event->pending_wakeup = 1;
e360adbe2   Peter Zijlstra   irq_work: Add gen...
3118
  		irq_work_queue(&handle->event->pending);
671dec5da   Peter Zijlstra   perf_counter: gen...
3119
  	} else
cdd6c482c   Ingo Molnar   perf: Do the big ...
3120
  		perf_event_wakeup(handle->event);
78d613eb1   Peter Zijlstra   perf_counter: sma...
3121
  }
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3122
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
3123
   * We need to ensure a later event_id doesn't publish a head when a former
ef60777c9   Peter Zijlstra   perf: Optimize th...
3124
   * event isn't done writing. However since we need to deal with NMIs we
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3125
3126
   * cannot fully serialize things.
   *
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3127
   * We only publish the head (and generate a wakeup) when the outer-most
ef60777c9   Peter Zijlstra   perf: Optimize th...
3128
   * event completes.
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3129
   */
ef60777c9   Peter Zijlstra   perf: Optimize th...
3130
  static void perf_output_get_handle(struct perf_output_handle *handle)
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3131
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3132
  	struct perf_buffer *buffer = handle->buffer;
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3133

ef60777c9   Peter Zijlstra   perf: Optimize th...
3134
  	preempt_disable();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3135
3136
  	local_inc(&buffer->nest);
  	handle->wakeup = local_read(&buffer->wakeup);
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3137
  }
ef60777c9   Peter Zijlstra   perf: Optimize th...
3138
  static void perf_output_put_handle(struct perf_output_handle *handle)
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3139
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3140
  	struct perf_buffer *buffer = handle->buffer;
8e3747c13   Peter Zijlstra   perf_counter: Cha...
3141
  	unsigned long head;
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3142
3143
  
  again:
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3144
  	head = local_read(&buffer->head);
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3145
3146
  
  	/*
ef60777c9   Peter Zijlstra   perf: Optimize th...
3147
  	 * IRQ/NMI can happen here, which means we can miss a head update.
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3148
  	 */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3149
  	if (!local_dec_and_test(&buffer->nest))
acd35a463   Frederic Weisbecker   perf: Fix forgott...
3150
  		goto out;
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3151
3152
  
  	/*
ef60777c9   Peter Zijlstra   perf: Optimize th...
3153
  	 * Publish the known good head. Rely on the full barrier implied
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3154
  	 * by atomic_dec_and_test() order the buffer->head read and this
ef60777c9   Peter Zijlstra   perf: Optimize th...
3155
  	 * write.
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3156
  	 */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3157
  	buffer->user_page->data_head = head;
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3158

ef60777c9   Peter Zijlstra   perf: Optimize th...
3159
3160
  	/*
  	 * Now check if we missed an update, rely on the (compiler)
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3161
  	 * barrier in atomic_dec_and_test() to re-read buffer->head.
ef60777c9   Peter Zijlstra   perf: Optimize th...
3162
  	 */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3163
3164
  	if (unlikely(head != local_read(&buffer->head))) {
  		local_inc(&buffer->nest);
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3165
3166
  		goto again;
  	}
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3167
  	if (handle->wakeup != local_read(&buffer->wakeup))
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3168
  		perf_output_wakeup(handle);
ef60777c9   Peter Zijlstra   perf: Optimize th...
3169

9ed6060d2   Peter Zijlstra   perf: Unindent la...
3170
  out:
ef60777c9   Peter Zijlstra   perf: Optimize th...
3171
  	preempt_enable();
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3172
  }
a94ffaaf5   Peter Zijlstra   perf: Remove more...
3173
  __always_inline void perf_output_copy(struct perf_output_handle *handle,
5622f295b   Markus Metzger   x86, perf_counter...
3174
  		      const void *buf, unsigned int len)
43a21ea81   Peter Zijlstra   perf_counter: Add...
3175
  {
5d967a8be   Peter Zijlstra   perf: Optimize pe...
3176
  	do {
a94ffaaf5   Peter Zijlstra   perf: Remove more...
3177
  		unsigned long size = min_t(unsigned long, handle->size, len);
5d967a8be   Peter Zijlstra   perf: Optimize pe...
3178
3179
3180
3181
3182
  
  		memcpy(handle->addr, buf, size);
  
  		len -= size;
  		handle->addr += size;
74048f895   Frederic Weisbecker   perf_events: Fix ...
3183
  		buf += size;
5d967a8be   Peter Zijlstra   perf: Optimize pe...
3184
3185
  		handle->size -= size;
  		if (!handle->size) {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3186
  			struct perf_buffer *buffer = handle->buffer;
3cafa9fbb   Peter Zijlstra   perf: Optimize th...
3187

5d967a8be   Peter Zijlstra   perf: Optimize pe...
3188
  			handle->page++;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3189
3190
3191
  			handle->page &= buffer->nr_pages - 1;
  			handle->addr = buffer->data_pages[handle->page];
  			handle->size = PAGE_SIZE << page_order(buffer);
5d967a8be   Peter Zijlstra   perf: Optimize pe...
3192
3193
  		}
  	} while (len);
43a21ea81   Peter Zijlstra   perf_counter: Add...
3194
  }
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3195
3196
3197
  static void __perf_event_header__init_id(struct perf_event_header *header,
  					 struct perf_sample_data *data,
  					 struct perf_event *event)
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
  {
  	u64 sample_type = event->attr.sample_type;
  
  	data->type = sample_type;
  	header->size += event->id_header_size;
  
  	if (sample_type & PERF_SAMPLE_TID) {
  		/* namespace issues */
  		data->tid_entry.pid = perf_event_pid(event, current);
  		data->tid_entry.tid = perf_event_tid(event, current);
  	}
  
  	if (sample_type & PERF_SAMPLE_TIME)
  		data->time = perf_clock();
  
  	if (sample_type & PERF_SAMPLE_ID)
  		data->id = primary_event_id(event);
  
  	if (sample_type & PERF_SAMPLE_STREAM_ID)
  		data->stream_id = event->id;
  
  	if (sample_type & PERF_SAMPLE_CPU) {
  		data->cpu_entry.cpu	 = raw_smp_processor_id();
  		data->cpu_entry.reserved = 0;
  	}
  }
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
  static void perf_event_header__init_id(struct perf_event_header *header,
  				       struct perf_sample_data *data,
  				       struct perf_event *event)
  {
  	if (event->attr.sample_id_all)
  		__perf_event_header__init_id(header, data, event);
  }
  
  static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  					   struct perf_sample_data *data)
  {
  	u64 sample_type = data->type;
  
  	if (sample_type & PERF_SAMPLE_TID)
  		perf_output_put(handle, data->tid_entry);
  
  	if (sample_type & PERF_SAMPLE_TIME)
  		perf_output_put(handle, data->time);
  
  	if (sample_type & PERF_SAMPLE_ID)
  		perf_output_put(handle, data->id);
  
  	if (sample_type & PERF_SAMPLE_STREAM_ID)
  		perf_output_put(handle, data->stream_id);
  
  	if (sample_type & PERF_SAMPLE_CPU)
  		perf_output_put(handle, data->cpu_entry);
  }
  
  static void perf_event__output_id_sample(struct perf_event *event,
  					 struct perf_output_handle *handle,
  					 struct perf_sample_data *sample)
  {
  	if (event->attr.sample_id_all)
  		__perf_event__output_id_sample(handle, sample);
  }
5622f295b   Markus Metzger   x86, perf_counter...
3260
  int perf_output_begin(struct perf_output_handle *handle,
cdd6c482c   Ingo Molnar   perf: Do the big ...
3261
  		      struct perf_event *event, unsigned int size,
5622f295b   Markus Metzger   x86, perf_counter...
3262
  		      int nmi, int sample)
0322cd6ec   Peter Zijlstra   perf_counter: uni...
3263
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3264
  	struct perf_buffer *buffer;
2667de81f   Peter Zijlstra   perf_counter: All...
3265
  	unsigned long tail, offset, head;
43a21ea81   Peter Zijlstra   perf_counter: Add...
3266
  	int have_lost;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3267
  	struct perf_sample_data sample_data;
43a21ea81   Peter Zijlstra   perf_counter: Add...
3268
3269
3270
3271
3272
  	struct {
  		struct perf_event_header header;
  		u64			 id;
  		u64			 lost;
  	} lost_event;
0322cd6ec   Peter Zijlstra   perf_counter: uni...
3273

a4be7c277   Peter Zijlstra   perf_counter: All...
3274
  	rcu_read_lock();
2023b3592   Peter Zijlstra   perf_counter: inh...
3275
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
3276
  	 * For inherited events we send all the output towards the parent.
2023b3592   Peter Zijlstra   perf_counter: inh...
3277
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
3278
3279
  	if (event->parent)
  		event = event->parent;
2023b3592   Peter Zijlstra   perf_counter: inh...
3280

ca5135e6b   Peter Zijlstra   perf: Rename perf...
3281
3282
  	buffer = rcu_dereference(event->buffer);
  	if (!buffer)
7b732a750   Peter Zijlstra   perf_counter: new...
3283
  		goto out;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3284
  	handle->buffer	= buffer;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3285
  	handle->event	= event;
43a21ea81   Peter Zijlstra   perf_counter: Add...
3286
3287
  	handle->nmi	= nmi;
  	handle->sample	= sample;
78d613eb1   Peter Zijlstra   perf_counter: sma...
3288

ca5135e6b   Peter Zijlstra   perf: Rename perf...
3289
  	if (!buffer->nr_pages)
00d1d0b09   Stephane Eranian   perf: Fix errors ...
3290
  		goto out;
7b732a750   Peter Zijlstra   perf_counter: new...
3291

ca5135e6b   Peter Zijlstra   perf: Rename perf...
3292
  	have_lost = local_read(&buffer->lost);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3293
3294
3295
3296
3297
3298
  	if (have_lost) {
  		lost_event.header.size = sizeof(lost_event);
  		perf_event_header__init_id(&lost_event.header, &sample_data,
  					   event);
  		size += lost_event.header.size;
  	}
43a21ea81   Peter Zijlstra   perf_counter: Add...
3299

ef60777c9   Peter Zijlstra   perf: Optimize th...
3300
  	perf_output_get_handle(handle);
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3301

7b732a750   Peter Zijlstra   perf_counter: new...
3302
  	do {
2667de81f   Peter Zijlstra   perf_counter: All...
3303
3304
3305
3306
3307
  		/*
  		 * Userspace could choose to issue a mb() before updating the
  		 * tail pointer. So that all reads will be completed before the
  		 * write is issued.
  		 */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3308
  		tail = ACCESS_ONCE(buffer->user_page->data_tail);
2667de81f   Peter Zijlstra   perf_counter: All...
3309
  		smp_rmb();
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3310
  		offset = head = local_read(&buffer->head);
c7138f37f   Peter Zijlstra   perf_counter: fix...
3311
  		head += size;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3312
  		if (unlikely(!perf_output_space(buffer, tail, offset, head)))
43a21ea81   Peter Zijlstra   perf_counter: Add...
3313
  			goto fail;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3314
  	} while (local_cmpxchg(&buffer->head, offset, head) != offset);
7b732a750   Peter Zijlstra   perf_counter: new...
3315

ca5135e6b   Peter Zijlstra   perf: Rename perf...
3316
3317
  	if (head - local_read(&buffer->wakeup) > buffer->watermark)
  		local_add(buffer->watermark, &buffer->wakeup);
0322cd6ec   Peter Zijlstra   perf_counter: uni...
3318

ca5135e6b   Peter Zijlstra   perf: Rename perf...
3319
3320
3321
3322
  	handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  	handle->page &= buffer->nr_pages - 1;
  	handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  	handle->addr = buffer->data_pages[handle->page];
5d967a8be   Peter Zijlstra   perf: Optimize pe...
3323
  	handle->addr += handle->size;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3324
  	handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
5d967a8be   Peter Zijlstra   perf: Optimize pe...
3325

43a21ea81   Peter Zijlstra   perf_counter: Add...
3326
  	if (have_lost) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3327
  		lost_event.header.type = PERF_RECORD_LOST;
43a21ea81   Peter Zijlstra   perf_counter: Add...
3328
  		lost_event.header.misc = 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3329
  		lost_event.id          = event->id;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3330
  		lost_event.lost        = local_xchg(&buffer->lost, 0);
43a21ea81   Peter Zijlstra   perf_counter: Add...
3331
3332
  
  		perf_output_put(handle, lost_event);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3333
  		perf_event__output_id_sample(event, handle, &sample_data);
43a21ea81   Peter Zijlstra   perf_counter: Add...
3334
  	}
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
3335
  	return 0;
7b732a750   Peter Zijlstra   perf_counter: new...
3336

78d613eb1   Peter Zijlstra   perf_counter: sma...
3337
  fail:
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3338
  	local_inc(&buffer->lost);
ef60777c9   Peter Zijlstra   perf: Optimize th...
3339
  	perf_output_put_handle(handle);
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
3340
3341
  out:
  	rcu_read_unlock();
7b732a750   Peter Zijlstra   perf_counter: new...
3342

b9cacc7bf   Peter Zijlstra   perf_counter: mor...
3343
3344
  	return -ENOSPC;
  }
7b732a750   Peter Zijlstra   perf_counter: new...
3345

5622f295b   Markus Metzger   x86, perf_counter...
3346
  void perf_output_end(struct perf_output_handle *handle)
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
3347
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3348
  	struct perf_event *event = handle->event;
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3349
  	struct perf_buffer *buffer = handle->buffer;
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3350

cdd6c482c   Ingo Molnar   perf: Do the big ...
3351
  	int wakeup_events = event->attr.wakeup_events;
c457810ab   Peter Zijlstra   perf_counter: per...
3352

43a21ea81   Peter Zijlstra   perf_counter: Add...
3353
  	if (handle->sample && wakeup_events) {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3354
  		int events = local_inc_return(&buffer->events);
c457810ab   Peter Zijlstra   perf_counter: per...
3355
  		if (events >= wakeup_events) {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
3356
3357
  			local_sub(wakeup_events, &buffer->events);
  			local_inc(&buffer->wakeup);
c457810ab   Peter Zijlstra   perf_counter: per...
3358
  		}
c33a0bc4e   Peter Zijlstra   perf_counter: fix...
3359
  	}
ef60777c9   Peter Zijlstra   perf: Optimize th...
3360
  	perf_output_put_handle(handle);
7b732a750   Peter Zijlstra   perf_counter: new...
3361
  	rcu_read_unlock();
b9cacc7bf   Peter Zijlstra   perf_counter: mor...
3362
  }
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3363
  static void perf_output_read_one(struct perf_output_handle *handle,
eed01528a   Stephane Eranian   perf_events: Fix ...
3364
3365
  				 struct perf_event *event,
  				 u64 enabled, u64 running)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3366
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3367
  	u64 read_format = event->attr.read_format;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3368
3369
  	u64 values[4];
  	int n = 0;
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
3370
  	values[n++] = perf_event_count(event);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3371
  	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
eed01528a   Stephane Eranian   perf_events: Fix ...
3372
  		values[n++] = enabled +
cdd6c482c   Ingo Molnar   perf: Do the big ...
3373
  			atomic64_read(&event->child_total_time_enabled);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3374
3375
  	}
  	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
eed01528a   Stephane Eranian   perf_events: Fix ...
3376
  		values[n++] = running +
cdd6c482c   Ingo Molnar   perf: Do the big ...
3377
  			atomic64_read(&event->child_total_time_running);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3378
3379
  	}
  	if (read_format & PERF_FORMAT_ID)
cdd6c482c   Ingo Molnar   perf: Do the big ...
3380
  		values[n++] = primary_event_id(event);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3381
3382
3383
3384
3385
  
  	perf_output_copy(handle, values, n * sizeof(u64));
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
3386
   * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3387
3388
   */
  static void perf_output_read_group(struct perf_output_handle *handle,
eed01528a   Stephane Eranian   perf_events: Fix ...
3389
3390
  			    struct perf_event *event,
  			    u64 enabled, u64 running)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3391
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3392
3393
  	struct perf_event *leader = event->group_leader, *sub;
  	u64 read_format = event->attr.read_format;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3394
3395
3396
3397
3398
3399
  	u64 values[5];
  	int n = 0;
  
  	values[n++] = 1 + leader->nr_siblings;
  
  	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
eed01528a   Stephane Eranian   perf_events: Fix ...
3400
  		values[n++] = enabled;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3401
3402
  
  	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
eed01528a   Stephane Eranian   perf_events: Fix ...
3403
  		values[n++] = running;
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3404

cdd6c482c   Ingo Molnar   perf: Do the big ...
3405
  	if (leader != event)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3406
  		leader->pmu->read(leader);
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
3407
  	values[n++] = perf_event_count(leader);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3408
  	if (read_format & PERF_FORMAT_ID)
cdd6c482c   Ingo Molnar   perf: Do the big ...
3409
  		values[n++] = primary_event_id(leader);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3410
3411
  
  	perf_output_copy(handle, values, n * sizeof(u64));
65abc8653   Ingo Molnar   perf_counter: Ren...
3412
  	list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3413
  		n = 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3414
  		if (sub != event)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3415
  			sub->pmu->read(sub);
b5e58793c   Peter Zijlstra   perf: Add perf_ev...
3416
  		values[n++] = perf_event_count(sub);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3417
  		if (read_format & PERF_FORMAT_ID)
cdd6c482c   Ingo Molnar   perf: Do the big ...
3418
  			values[n++] = primary_event_id(sub);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3419
3420
3421
3422
  
  		perf_output_copy(handle, values, n * sizeof(u64));
  	}
  }
eed01528a   Stephane Eranian   perf_events: Fix ...
3423
3424
  #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  				 PERF_FORMAT_TOTAL_TIME_RUNNING)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3425
  static void perf_output_read(struct perf_output_handle *handle,
cdd6c482c   Ingo Molnar   perf: Do the big ...
3426
  			     struct perf_event *event)
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3427
  {
eed01528a   Stephane Eranian   perf_events: Fix ...
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
  	u64 enabled = 0, running = 0, now, ctx_time;
  	u64 read_format = event->attr.read_format;
  
  	/*
  	 * compute total_time_enabled, total_time_running
  	 * based on snapshot values taken when the event
  	 * was last scheduled in.
  	 *
  	 * we cannot simply called update_context_time()
  	 * because of locking issue as we are called in
  	 * NMI context
  	 */
  	if (read_format & PERF_FORMAT_TOTAL_TIMES) {
  		now = perf_clock();
  		ctx_time = event->shadow_ctx_time + now;
  		enabled = ctx_time - event->tstamp_enabled;
  		running = ctx_time - event->tstamp_running;
  	}
cdd6c482c   Ingo Molnar   perf: Do the big ...
3446
  	if (event->attr.read_format & PERF_FORMAT_GROUP)
eed01528a   Stephane Eranian   perf_events: Fix ...
3447
  		perf_output_read_group(handle, event, enabled, running);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3448
  	else
eed01528a   Stephane Eranian   perf_events: Fix ...
3449
  		perf_output_read_one(handle, event, enabled, running);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3450
  }
5622f295b   Markus Metzger   x86, perf_counter...
3451
3452
3453
  void perf_output_sample(struct perf_output_handle *handle,
  			struct perf_event_header *header,
  			struct perf_sample_data *data,
cdd6c482c   Ingo Molnar   perf: Do the big ...
3454
  			struct perf_event *event)
5622f295b   Markus Metzger   x86, perf_counter...
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
  {
  	u64 sample_type = data->type;
  
  	perf_output_put(handle, *header);
  
  	if (sample_type & PERF_SAMPLE_IP)
  		perf_output_put(handle, data->ip);
  
  	if (sample_type & PERF_SAMPLE_TID)
  		perf_output_put(handle, data->tid_entry);
  
  	if (sample_type & PERF_SAMPLE_TIME)
  		perf_output_put(handle, data->time);
  
  	if (sample_type & PERF_SAMPLE_ADDR)
  		perf_output_put(handle, data->addr);
  
  	if (sample_type & PERF_SAMPLE_ID)
  		perf_output_put(handle, data->id);
  
  	if (sample_type & PERF_SAMPLE_STREAM_ID)
  		perf_output_put(handle, data->stream_id);
  
  	if (sample_type & PERF_SAMPLE_CPU)
  		perf_output_put(handle, data->cpu_entry);
  
  	if (sample_type & PERF_SAMPLE_PERIOD)
  		perf_output_put(handle, data->period);
  
  	if (sample_type & PERF_SAMPLE_READ)
cdd6c482c   Ingo Molnar   perf: Do the big ...
3485
  		perf_output_read(handle, event);
5622f295b   Markus Metzger   x86, perf_counter...
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
  
  	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  		if (data->callchain) {
  			int size = 1;
  
  			if (data->callchain)
  				size += data->callchain->nr;
  
  			size *= sizeof(u64);
  
  			perf_output_copy(handle, data->callchain, size);
  		} else {
  			u64 nr = 0;
  			perf_output_put(handle, nr);
  		}
  	}
  
  	if (sample_type & PERF_SAMPLE_RAW) {
  		if (data->raw) {
  			perf_output_put(handle, data->raw->size);
  			perf_output_copy(handle, data->raw->data,
  					 data->raw->size);
  		} else {
  			struct {
  				u32	size;
  				u32	data;
  			} raw = {
  				.size = sizeof(u32),
  				.data = 0,
  			};
  			perf_output_put(handle, raw);
  		}
  	}
  }
  
  void perf_prepare_sample(struct perf_event_header *header,
  			 struct perf_sample_data *data,
cdd6c482c   Ingo Molnar   perf: Do the big ...
3523
  			 struct perf_event *event,
5622f295b   Markus Metzger   x86, perf_counter...
3524
  			 struct pt_regs *regs)
7b732a750   Peter Zijlstra   perf_counter: new...
3525
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3526
  	u64 sample_type = event->attr.sample_type;
7b732a750   Peter Zijlstra   perf_counter: new...
3527

cdd6c482c   Ingo Molnar   perf: Do the big ...
3528
  	header->type = PERF_RECORD_SAMPLE;
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
3529
  	header->size = sizeof(*header) + event->header_size;
5622f295b   Markus Metzger   x86, perf_counter...
3530
3531
3532
  
  	header->misc = 0;
  	header->misc |= perf_misc_flags(regs);
6fab01927   Peter Zijlstra   perf_counter: pro...
3533

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3534
  	__perf_event_header__init_id(header, data, event);
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
3535

c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
3536
  	if (sample_type & PERF_SAMPLE_IP)
5622f295b   Markus Metzger   x86, perf_counter...
3537
  		data->ip = perf_instruction_pointer(regs);
b23f3325e   Peter Zijlstra   perf_counter: Ren...
3538
  	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5622f295b   Markus Metzger   x86, perf_counter...
3539
  		int size = 1;
394ee0762   Peter Zijlstra   perf_counter: pro...
3540

5622f295b   Markus Metzger   x86, perf_counter...
3541
3542
3543
3544
3545
3546
  		data->callchain = perf_callchain(regs);
  
  		if (data->callchain)
  			size += data->callchain->nr;
  
  		header->size += size * sizeof(u64);
394ee0762   Peter Zijlstra   perf_counter: pro...
3547
  	}
3a43ce68a   Frederic Weisbecker   perf_counter: Fix...
3548
  	if (sample_type & PERF_SAMPLE_RAW) {
a044560c3   Peter Zijlstra   perf_counter: Cor...
3549
3550
3551
3552
3553
3554
3555
3556
  		int size = sizeof(u32);
  
  		if (data->raw)
  			size += data->raw->size;
  		else
  			size += sizeof(u32);
  
  		WARN_ON_ONCE(size & (sizeof(u64)-1));
5622f295b   Markus Metzger   x86, perf_counter...
3557
  		header->size += size;
7f453c24b   Peter Zijlstra   perf_counter: PER...
3558
  	}
5622f295b   Markus Metzger   x86, perf_counter...
3559
  }
7f453c24b   Peter Zijlstra   perf_counter: PER...
3560

cdd6c482c   Ingo Molnar   perf: Do the big ...
3561
  static void perf_event_output(struct perf_event *event, int nmi,
5622f295b   Markus Metzger   x86, perf_counter...
3562
3563
3564
3565
3566
  				struct perf_sample_data *data,
  				struct pt_regs *regs)
  {
  	struct perf_output_handle handle;
  	struct perf_event_header header;
689802b2d   Peter Zijlstra   perf_counter: Add...
3567

927c7a9e9   Frederic Weisbecker   perf: Fix race in...
3568
3569
  	/* protect the callchain buffers */
  	rcu_read_lock();
cdd6c482c   Ingo Molnar   perf: Do the big ...
3570
  	perf_prepare_sample(&header, data, event, regs);
5c1481943   Peter Zijlstra   perf_counter: out...
3571

cdd6c482c   Ingo Molnar   perf: Do the big ...
3572
  	if (perf_output_begin(&handle, event, header.size, nmi, 1))
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
3573
  		goto exit;
0322cd6ec   Peter Zijlstra   perf_counter: uni...
3574

cdd6c482c   Ingo Molnar   perf: Do the big ...
3575
  	perf_output_sample(&handle, &header, data, event);
f413cdb80   Frederic Weisbecker   perf_counter: Fix...
3576

8a057d849   Peter Zijlstra   perf_counter: mov...
3577
  	perf_output_end(&handle);
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
3578
3579
3580
  
  exit:
  	rcu_read_unlock();
0322cd6ec   Peter Zijlstra   perf_counter: uni...
3581
3582
3583
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
3584
   * read event_id
38b200d67   Peter Zijlstra   perf_counter: Add...
3585
3586
3587
3588
3589
3590
3591
   */
  
  struct perf_read_event {
  	struct perf_event_header	header;
  
  	u32				pid;
  	u32				tid;
38b200d67   Peter Zijlstra   perf_counter: Add...
3592
3593
3594
  };
  
  static void
cdd6c482c   Ingo Molnar   perf: Do the big ...
3595
  perf_event_read_event(struct perf_event *event,
38b200d67   Peter Zijlstra   perf_counter: Add...
3596
3597
3598
  			struct task_struct *task)
  {
  	struct perf_output_handle handle;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3599
  	struct perf_sample_data sample;
dfc65094d   Ingo Molnar   perf_counter: Ren...
3600
  	struct perf_read_event read_event = {
38b200d67   Peter Zijlstra   perf_counter: Add...
3601
  		.header = {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3602
  			.type = PERF_RECORD_READ,
38b200d67   Peter Zijlstra   perf_counter: Add...
3603
  			.misc = 0,
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
3604
  			.size = sizeof(read_event) + event->read_size,
38b200d67   Peter Zijlstra   perf_counter: Add...
3605
  		},
cdd6c482c   Ingo Molnar   perf: Do the big ...
3606
3607
  		.pid = perf_event_pid(event, task),
  		.tid = perf_event_tid(event, task),
38b200d67   Peter Zijlstra   perf_counter: Add...
3608
  	};
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3609
  	int ret;
38b200d67   Peter Zijlstra   perf_counter: Add...
3610

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3611
  	perf_event_header__init_id(&read_event.header, &sample, event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
3612
  	ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
38b200d67   Peter Zijlstra   perf_counter: Add...
3613
3614
  	if (ret)
  		return;
dfc65094d   Ingo Molnar   perf_counter: Ren...
3615
  	perf_output_put(&handle, read_event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
3616
  	perf_output_read(&handle, event);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3617
  	perf_event__output_id_sample(event, &handle, &sample);
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
3618

38b200d67   Peter Zijlstra   perf_counter: Add...
3619
3620
3621
3622
  	perf_output_end(&handle);
  }
  
  /*
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3623
3624
   * task tracking -- fork/exit
   *
3af9e8592   Eric B Munson   perf: Add non-exe...
3625
   * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
60313ebed   Peter Zijlstra   perf_counter: Add...
3626
   */
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3627
  struct perf_task_event {
3a80b4a35   Peter Zijlstra   perf_counter: Fix...
3628
  	struct task_struct		*task;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3629
  	struct perf_event_context	*task_ctx;
60313ebed   Peter Zijlstra   perf_counter: Add...
3630
3631
3632
3633
3634
3635
  
  	struct {
  		struct perf_event_header	header;
  
  		u32				pid;
  		u32				ppid;
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3636
3637
  		u32				tid;
  		u32				ptid;
393b2ad8c   Arjan van de Ven   perf: Add a times...
3638
  		u64				time;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3639
  	} event_id;
60313ebed   Peter Zijlstra   perf_counter: Add...
3640
  };
cdd6c482c   Ingo Molnar   perf: Do the big ...
3641
  static void perf_event_task_output(struct perf_event *event,
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3642
  				     struct perf_task_event *task_event)
60313ebed   Peter Zijlstra   perf_counter: Add...
3643
3644
  {
  	struct perf_output_handle handle;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3645
  	struct perf_sample_data	sample;
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3646
  	struct task_struct *task = task_event->task;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3647
  	int ret, size = task_event->event_id.header.size;
8bb39f9aa   Mike Galbraith   perf: Fix 'perf s...
3648

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3649
  	perf_event_header__init_id(&task_event->event_id.header, &sample, event);
60313ebed   Peter Zijlstra   perf_counter: Add...
3650

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3651
3652
  	ret = perf_output_begin(&handle, event,
  				task_event->event_id.header.size, 0, 0);
ef60777c9   Peter Zijlstra   perf: Optimize th...
3653
  	if (ret)
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3654
  		goto out;
60313ebed   Peter Zijlstra   perf_counter: Add...
3655

cdd6c482c   Ingo Molnar   perf: Do the big ...
3656
3657
  	task_event->event_id.pid = perf_event_pid(event, task);
  	task_event->event_id.ppid = perf_event_pid(event, current);
60313ebed   Peter Zijlstra   perf_counter: Add...
3658

cdd6c482c   Ingo Molnar   perf: Do the big ...
3659
3660
  	task_event->event_id.tid = perf_event_tid(event, task);
  	task_event->event_id.ptid = perf_event_tid(event, current);
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3661

cdd6c482c   Ingo Molnar   perf: Do the big ...
3662
  	perf_output_put(&handle, task_event->event_id);
393b2ad8c   Arjan van de Ven   perf: Add a times...
3663

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3664
  	perf_event__output_id_sample(event, &handle, &sample);
60313ebed   Peter Zijlstra   perf_counter: Add...
3665
  	perf_output_end(&handle);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3666
3667
  out:
  	task_event->event_id.header.size = size;
60313ebed   Peter Zijlstra   perf_counter: Add...
3668
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3669
  static int perf_event_task_match(struct perf_event *event)
60313ebed   Peter Zijlstra   perf_counter: Add...
3670
  {
6f93d0a7c   Peter Zijlstra   perf_events: Fix ...
3671
  	if (event->state < PERF_EVENT_STATE_INACTIVE)
22e190851   Peter Zijlstra   perf: Honour even...
3672
  		return 0;
5632ab12e   Stephane Eranian   perf_events: Gene...
3673
  	if (!event_filter_match(event))
5d27c23df   Peter Zijlstra   perf events: Dont...
3674
  		return 0;
3af9e8592   Eric B Munson   perf: Add non-exe...
3675
3676
  	if (event->attr.comm || event->attr.mmap ||
  	    event->attr.mmap_data || event->attr.task)
60313ebed   Peter Zijlstra   perf_counter: Add...
3677
3678
3679
3680
  		return 1;
  
  	return 0;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3681
  static void perf_event_task_ctx(struct perf_event_context *ctx,
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3682
  				  struct perf_task_event *task_event)
60313ebed   Peter Zijlstra   perf_counter: Add...
3683
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3684
  	struct perf_event *event;
60313ebed   Peter Zijlstra   perf_counter: Add...
3685

cdd6c482c   Ingo Molnar   perf: Do the big ...
3686
3687
3688
  	list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  		if (perf_event_task_match(event))
  			perf_event_task_output(event, task_event);
60313ebed   Peter Zijlstra   perf_counter: Add...
3689
  	}
60313ebed   Peter Zijlstra   perf_counter: Add...
3690
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3691
  static void perf_event_task_event(struct perf_task_event *task_event)
60313ebed   Peter Zijlstra   perf_counter: Add...
3692
3693
  {
  	struct perf_cpu_context *cpuctx;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3694
  	struct perf_event_context *ctx;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3695
  	struct pmu *pmu;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3696
  	int ctxn;
60313ebed   Peter Zijlstra   perf_counter: Add...
3697

d6ff86cfb   Peter Zijlstra   perf: Optimize pe...
3698
  	rcu_read_lock();
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3699
  	list_for_each_entry_rcu(pmu, &pmus, entry) {
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
3700
  		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
516769575   Peter Zijlstra   perf: Fix duplica...
3701
3702
  		if (cpuctx->active_pmu != pmu)
  			goto next;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3703
  		perf_event_task_ctx(&cpuctx->ctx, task_event);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3704
3705
3706
3707
3708
  
  		ctx = task_event->task_ctx;
  		if (!ctx) {
  			ctxn = pmu->task_ctx_nr;
  			if (ctxn < 0)
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
3709
  				goto next;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3710
3711
3712
3713
  			ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  		}
  		if (ctx)
  			perf_event_task_ctx(ctx, task_event);
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
3714
3715
  next:
  		put_cpu_ptr(pmu->pmu_cpu_context);
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3716
  	}
60313ebed   Peter Zijlstra   perf_counter: Add...
3717
3718
  	rcu_read_unlock();
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3719
3720
  static void perf_event_task(struct task_struct *task,
  			      struct perf_event_context *task_ctx,
3a80b4a35   Peter Zijlstra   perf_counter: Fix...
3721
  			      int new)
60313ebed   Peter Zijlstra   perf_counter: Add...
3722
  {
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3723
  	struct perf_task_event task_event;
60313ebed   Peter Zijlstra   perf_counter: Add...
3724

cdd6c482c   Ingo Molnar   perf: Do the big ...
3725
3726
3727
  	if (!atomic_read(&nr_comm_events) &&
  	    !atomic_read(&nr_mmap_events) &&
  	    !atomic_read(&nr_task_events))
60313ebed   Peter Zijlstra   perf_counter: Add...
3728
  		return;
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3729
  	task_event = (struct perf_task_event){
3a80b4a35   Peter Zijlstra   perf_counter: Fix...
3730
3731
  		.task	  = task,
  		.task_ctx = task_ctx,
cdd6c482c   Ingo Molnar   perf: Do the big ...
3732
  		.event_id    = {
60313ebed   Peter Zijlstra   perf_counter: Add...
3733
  			.header = {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3734
  				.type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
573402db0   Peter Zijlstra   perf_counter: Plu...
3735
  				.misc = 0,
cdd6c482c   Ingo Molnar   perf: Do the big ...
3736
  				.size = sizeof(task_event.event_id),
60313ebed   Peter Zijlstra   perf_counter: Add...
3737
  			},
573402db0   Peter Zijlstra   perf_counter: Plu...
3738
3739
  			/* .pid  */
  			/* .ppid */
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3740
3741
  			/* .tid  */
  			/* .ptid */
6f93d0a7c   Peter Zijlstra   perf_events: Fix ...
3742
  			.time = perf_clock(),
60313ebed   Peter Zijlstra   perf_counter: Add...
3743
3744
  		},
  	};
cdd6c482c   Ingo Molnar   perf: Do the big ...
3745
  	perf_event_task_event(&task_event);
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3746
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3747
  void perf_event_fork(struct task_struct *task)
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
3748
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3749
  	perf_event_task(task, NULL, 1);
60313ebed   Peter Zijlstra   perf_counter: Add...
3750
3751
3752
  }
  
  /*
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3753
3754
3755
3756
   * comm tracking
   */
  
  struct perf_comm_event {
22a4f650d   Ingo Molnar   perf_counter: Tid...
3757
3758
  	struct task_struct	*task;
  	char			*comm;
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3759
3760
3761
3762
3763
3764
3765
  	int			comm_size;
  
  	struct {
  		struct perf_event_header	header;
  
  		u32				pid;
  		u32				tid;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3766
  	} event_id;
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3767
  };
cdd6c482c   Ingo Molnar   perf: Do the big ...
3768
  static void perf_event_comm_output(struct perf_event *event,
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3769
3770
3771
  				     struct perf_comm_event *comm_event)
  {
  	struct perf_output_handle handle;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3772
  	struct perf_sample_data sample;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3773
  	int size = comm_event->event_id.header.size;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3774
3775
3776
3777
3778
  	int ret;
  
  	perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  	ret = perf_output_begin(&handle, event,
  				comm_event->event_id.header.size, 0, 0);
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3779
3780
  
  	if (ret)
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3781
  		goto out;
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3782

cdd6c482c   Ingo Molnar   perf: Do the big ...
3783
3784
  	comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  	comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
709e50cf8   Peter Zijlstra   perf_counter: Use...
3785

cdd6c482c   Ingo Molnar   perf: Do the big ...
3786
  	perf_output_put(&handle, comm_event->event_id);
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3787
3788
  	perf_output_copy(&handle, comm_event->comm,
  				   comm_event->comm_size);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3789
3790
  
  	perf_event__output_id_sample(event, &handle, &sample);
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3791
  	perf_output_end(&handle);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3792
3793
  out:
  	comm_event->event_id.header.size = size;
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3794
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3795
  static int perf_event_comm_match(struct perf_event *event)
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3796
  {
6f93d0a7c   Peter Zijlstra   perf_events: Fix ...
3797
  	if (event->state < PERF_EVENT_STATE_INACTIVE)
22e190851   Peter Zijlstra   perf: Honour even...
3798
  		return 0;
5632ab12e   Stephane Eranian   perf_events: Gene...
3799
  	if (!event_filter_match(event))
5d27c23df   Peter Zijlstra   perf events: Dont...
3800
  		return 0;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3801
  	if (event->attr.comm)
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3802
3803
3804
3805
  		return 1;
  
  	return 0;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3806
  static void perf_event_comm_ctx(struct perf_event_context *ctx,
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3807
3808
  				  struct perf_comm_event *comm_event)
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3809
  	struct perf_event *event;
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3810

cdd6c482c   Ingo Molnar   perf: Do the big ...
3811
3812
3813
  	list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  		if (perf_event_comm_match(event))
  			perf_event_comm_output(event, comm_event);
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3814
  	}
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3815
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3816
  static void perf_event_comm_event(struct perf_comm_event *comm_event)
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3817
3818
  {
  	struct perf_cpu_context *cpuctx;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3819
  	struct perf_event_context *ctx;
413ee3b48   Anton Blanchard   perf_counter: Mak...
3820
  	char comm[TASK_COMM_LEN];
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3821
  	unsigned int size;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3822
  	struct pmu *pmu;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3823
  	int ctxn;
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3824

413ee3b48   Anton Blanchard   perf_counter: Mak...
3825
  	memset(comm, 0, sizeof(comm));
96b02d78a   Márton Németh   perf_event: Remov...
3826
  	strlcpy(comm, comm_event->task->comm, sizeof(comm));
888fcee06   Ingo Molnar   perf_counter: fix...
3827
  	size = ALIGN(strlen(comm)+1, sizeof(u64));
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3828
3829
3830
  
  	comm_event->comm = comm;
  	comm_event->comm_size = size;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3831
  	comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
f6595f3a9   Peter Zijlstra   perf: Optimize pe...
3832
  	rcu_read_lock();
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3833
  	list_for_each_entry_rcu(pmu, &pmus, entry) {
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
3834
  		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
516769575   Peter Zijlstra   perf: Fix duplica...
3835
3836
  		if (cpuctx->active_pmu != pmu)
  			goto next;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3837
  		perf_event_comm_ctx(&cpuctx->ctx, comm_event);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3838
3839
3840
  
  		ctxn = pmu->task_ctx_nr;
  		if (ctxn < 0)
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
3841
  			goto next;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3842
3843
3844
3845
  
  		ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  		if (ctx)
  			perf_event_comm_ctx(ctx, comm_event);
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
3846
3847
  next:
  		put_cpu_ptr(pmu->pmu_cpu_context);
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3848
  	}
665c2142a   Peter Zijlstra   perf_counter: Cle...
3849
  	rcu_read_unlock();
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3850
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3851
  void perf_event_comm(struct task_struct *task)
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3852
  {
9ee318a78   Peter Zijlstra   perf_counter: opt...
3853
  	struct perf_comm_event comm_event;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3854
3855
  	struct perf_event_context *ctx;
  	int ctxn;
9ee318a78   Peter Zijlstra   perf_counter: opt...
3856

8dc85d547   Peter Zijlstra   perf: Multiple ta...
3857
3858
3859
3860
  	for_each_task_context_nr(ctxn) {
  		ctx = task->perf_event_ctxp[ctxn];
  		if (!ctx)
  			continue;
9ee318a78   Peter Zijlstra   perf_counter: opt...
3861

8dc85d547   Peter Zijlstra   perf: Multiple ta...
3862
3863
  		perf_event_enable_on_exec(ctx);
  	}
9ee318a78   Peter Zijlstra   perf_counter: opt...
3864

cdd6c482c   Ingo Molnar   perf: Do the big ...
3865
  	if (!atomic_read(&nr_comm_events))
9ee318a78   Peter Zijlstra   perf_counter: opt...
3866
  		return;
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
3867

9ee318a78   Peter Zijlstra   perf_counter: opt...
3868
  	comm_event = (struct perf_comm_event){
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3869
  		.task	= task,
573402db0   Peter Zijlstra   perf_counter: Plu...
3870
3871
  		/* .comm      */
  		/* .comm_size */
cdd6c482c   Ingo Molnar   perf: Do the big ...
3872
  		.event_id  = {
573402db0   Peter Zijlstra   perf_counter: Plu...
3873
  			.header = {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3874
  				.type = PERF_RECORD_COMM,
573402db0   Peter Zijlstra   perf_counter: Plu...
3875
3876
3877
3878
3879
  				.misc = 0,
  				/* .size */
  			},
  			/* .pid */
  			/* .tid */
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3880
3881
  		},
  	};
cdd6c482c   Ingo Molnar   perf: Do the big ...
3882
  	perf_event_comm_event(&comm_event);
8d1b2d936   Peter Zijlstra   perf_counter: tra...
3883
3884
3885
  }
  
  /*
0a4a93919   Peter Zijlstra   perf_counter: exe...
3886
3887
3888
3889
   * mmap tracking
   */
  
  struct perf_mmap_event {
089dd79db   Peter Zijlstra   perf_counter: Gen...
3890
3891
3892
3893
  	struct vm_area_struct	*vma;
  
  	const char		*file_name;
  	int			file_size;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3894
3895
3896
3897
3898
3899
3900
3901
3902
  
  	struct {
  		struct perf_event_header	header;
  
  		u32				pid;
  		u32				tid;
  		u64				start;
  		u64				len;
  		u64				pgoff;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3903
  	} event_id;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3904
  };
cdd6c482c   Ingo Molnar   perf: Do the big ...
3905
  static void perf_event_mmap_output(struct perf_event *event,
0a4a93919   Peter Zijlstra   perf_counter: exe...
3906
3907
3908
  				     struct perf_mmap_event *mmap_event)
  {
  	struct perf_output_handle handle;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3909
  	struct perf_sample_data sample;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3910
  	int size = mmap_event->event_id.header.size;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3911
  	int ret;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3912

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3913
3914
3915
  	perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  	ret = perf_output_begin(&handle, event,
  				mmap_event->event_id.header.size, 0, 0);
0a4a93919   Peter Zijlstra   perf_counter: exe...
3916
  	if (ret)
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3917
  		goto out;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3918

cdd6c482c   Ingo Molnar   perf: Do the big ...
3919
3920
  	mmap_event->event_id.pid = perf_event_pid(event, current);
  	mmap_event->event_id.tid = perf_event_tid(event, current);
709e50cf8   Peter Zijlstra   perf_counter: Use...
3921

cdd6c482c   Ingo Molnar   perf: Do the big ...
3922
  	perf_output_put(&handle, mmap_event->event_id);
0a4a93919   Peter Zijlstra   perf_counter: exe...
3923
3924
  	perf_output_copy(&handle, mmap_event->file_name,
  				   mmap_event->file_size);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3925
3926
  
  	perf_event__output_id_sample(event, &handle, &sample);
78d613eb1   Peter Zijlstra   perf_counter: sma...
3927
  	perf_output_end(&handle);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
3928
3929
  out:
  	mmap_event->event_id.header.size = size;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3930
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3931
  static int perf_event_mmap_match(struct perf_event *event,
3af9e8592   Eric B Munson   perf: Add non-exe...
3932
3933
  				   struct perf_mmap_event *mmap_event,
  				   int executable)
0a4a93919   Peter Zijlstra   perf_counter: exe...
3934
  {
6f93d0a7c   Peter Zijlstra   perf_events: Fix ...
3935
  	if (event->state < PERF_EVENT_STATE_INACTIVE)
22e190851   Peter Zijlstra   perf: Honour even...
3936
  		return 0;
5632ab12e   Stephane Eranian   perf_events: Gene...
3937
  	if (!event_filter_match(event))
5d27c23df   Peter Zijlstra   perf events: Dont...
3938
  		return 0;
3af9e8592   Eric B Munson   perf: Add non-exe...
3939
3940
  	if ((!executable && event->attr.mmap_data) ||
  	    (executable && event->attr.mmap))
0a4a93919   Peter Zijlstra   perf_counter: exe...
3941
3942
3943
3944
  		return 1;
  
  	return 0;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3945
  static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3af9e8592   Eric B Munson   perf: Add non-exe...
3946
3947
  				  struct perf_mmap_event *mmap_event,
  				  int executable)
0a4a93919   Peter Zijlstra   perf_counter: exe...
3948
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
3949
  	struct perf_event *event;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3950

cdd6c482c   Ingo Molnar   perf: Do the big ...
3951
  	list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3af9e8592   Eric B Munson   perf: Add non-exe...
3952
  		if (perf_event_mmap_match(event, mmap_event, executable))
cdd6c482c   Ingo Molnar   perf: Do the big ...
3953
  			perf_event_mmap_output(event, mmap_event);
0a4a93919   Peter Zijlstra   perf_counter: exe...
3954
  	}
0a4a93919   Peter Zijlstra   perf_counter: exe...
3955
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
3956
  static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
0a4a93919   Peter Zijlstra   perf_counter: exe...
3957
3958
  {
  	struct perf_cpu_context *cpuctx;
cdd6c482c   Ingo Molnar   perf: Do the big ...
3959
  	struct perf_event_context *ctx;
089dd79db   Peter Zijlstra   perf_counter: Gen...
3960
3961
  	struct vm_area_struct *vma = mmap_event->vma;
  	struct file *file = vma->vm_file;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3962
3963
3964
  	unsigned int size;
  	char tmp[16];
  	char *buf = NULL;
089dd79db   Peter Zijlstra   perf_counter: Gen...
3965
  	const char *name;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
3966
  	struct pmu *pmu;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
3967
  	int ctxn;
0a4a93919   Peter Zijlstra   perf_counter: exe...
3968

413ee3b48   Anton Blanchard   perf_counter: Mak...
3969
  	memset(tmp, 0, sizeof(tmp));
0a4a93919   Peter Zijlstra   perf_counter: exe...
3970
  	if (file) {
413ee3b48   Anton Blanchard   perf_counter: Mak...
3971
3972
3973
3974
3975
3976
  		/*
  		 * d_path works from the end of the buffer backwards, so we
  		 * need to add enough zero bytes after the string to handle
  		 * the 64bit alignment we do later.
  		 */
  		buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
0a4a93919   Peter Zijlstra   perf_counter: exe...
3977
3978
3979
3980
  		if (!buf) {
  			name = strncpy(tmp, "//enomem", sizeof(tmp));
  			goto got_name;
  		}
d3d21c412   Peter Zijlstra   perf_counter: log...
3981
  		name = d_path(&file->f_path, buf, PATH_MAX);
0a4a93919   Peter Zijlstra   perf_counter: exe...
3982
3983
3984
3985
3986
  		if (IS_ERR(name)) {
  			name = strncpy(tmp, "//toolong", sizeof(tmp));
  			goto got_name;
  		}
  	} else {
413ee3b48   Anton Blanchard   perf_counter: Mak...
3987
3988
3989
  		if (arch_vma_name(mmap_event->vma)) {
  			name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  				       sizeof(tmp));
089dd79db   Peter Zijlstra   perf_counter: Gen...
3990
  			goto got_name;
413ee3b48   Anton Blanchard   perf_counter: Mak...
3991
  		}
089dd79db   Peter Zijlstra   perf_counter: Gen...
3992
3993
3994
3995
  
  		if (!vma->vm_mm) {
  			name = strncpy(tmp, "[vdso]", sizeof(tmp));
  			goto got_name;
3af9e8592   Eric B Munson   perf: Add non-exe...
3996
3997
3998
3999
4000
4001
4002
4003
  		} else if (vma->vm_start <= vma->vm_mm->start_brk &&
  				vma->vm_end >= vma->vm_mm->brk) {
  			name = strncpy(tmp, "[heap]", sizeof(tmp));
  			goto got_name;
  		} else if (vma->vm_start <= vma->vm_mm->start_stack &&
  				vma->vm_end >= vma->vm_mm->start_stack) {
  			name = strncpy(tmp, "[stack]", sizeof(tmp));
  			goto got_name;
089dd79db   Peter Zijlstra   perf_counter: Gen...
4004
  		}
0a4a93919   Peter Zijlstra   perf_counter: exe...
4005
4006
4007
4008
4009
  		name = strncpy(tmp, "//anon", sizeof(tmp));
  		goto got_name;
  	}
  
  got_name:
888fcee06   Ingo Molnar   perf_counter: fix...
4010
  	size = ALIGN(strlen(name)+1, sizeof(u64));
0a4a93919   Peter Zijlstra   perf_counter: exe...
4011
4012
4013
  
  	mmap_event->file_name = name;
  	mmap_event->file_size = size;
cdd6c482c   Ingo Molnar   perf: Do the big ...
4014
  	mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
0a4a93919   Peter Zijlstra   perf_counter: exe...
4015

f6d9dd237   Peter Zijlstra   perf: Optimize pe...
4016
  	rcu_read_lock();
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
4017
  	list_for_each_entry_rcu(pmu, &pmus, entry) {
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
4018
  		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
516769575   Peter Zijlstra   perf: Fix duplica...
4019
4020
  		if (cpuctx->active_pmu != pmu)
  			goto next;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
4021
4022
  		perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  					vma->vm_flags & VM_EXEC);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4023
4024
4025
  
  		ctxn = pmu->task_ctx_nr;
  		if (ctxn < 0)
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
4026
  			goto next;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4027
4028
4029
4030
4031
4032
  
  		ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  		if (ctx) {
  			perf_event_mmap_ctx(ctx, mmap_event,
  					vma->vm_flags & VM_EXEC);
  		}
41945f6cc   Peter Zijlstra   perf: Avoid RCU v...
4033
4034
  next:
  		put_cpu_ptr(pmu->pmu_cpu_context);
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
4035
  	}
665c2142a   Peter Zijlstra   perf_counter: Cle...
4036
  	rcu_read_unlock();
0a4a93919   Peter Zijlstra   perf_counter: exe...
4037
4038
  	kfree(buf);
  }
3af9e8592   Eric B Munson   perf: Add non-exe...
4039
  void perf_event_mmap(struct vm_area_struct *vma)
0a4a93919   Peter Zijlstra   perf_counter: exe...
4040
  {
9ee318a78   Peter Zijlstra   perf_counter: opt...
4041
  	struct perf_mmap_event mmap_event;
cdd6c482c   Ingo Molnar   perf: Do the big ...
4042
  	if (!atomic_read(&nr_mmap_events))
9ee318a78   Peter Zijlstra   perf_counter: opt...
4043
4044
4045
  		return;
  
  	mmap_event = (struct perf_mmap_event){
089dd79db   Peter Zijlstra   perf_counter: Gen...
4046
  		.vma	= vma,
573402db0   Peter Zijlstra   perf_counter: Plu...
4047
4048
  		/* .file_name */
  		/* .file_size */
cdd6c482c   Ingo Molnar   perf: Do the big ...
4049
  		.event_id  = {
573402db0   Peter Zijlstra   perf_counter: Plu...
4050
  			.header = {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4051
  				.type = PERF_RECORD_MMAP,
39447b386   Zhang, Yanmin   perf: Enhance per...
4052
  				.misc = PERF_RECORD_MISC_USER,
573402db0   Peter Zijlstra   perf_counter: Plu...
4053
4054
4055
4056
  				/* .size */
  			},
  			/* .pid */
  			/* .tid */
089dd79db   Peter Zijlstra   perf_counter: Gen...
4057
4058
  			.start  = vma->vm_start,
  			.len    = vma->vm_end - vma->vm_start,
3a0304e90   Peter Zijlstra   perf_events: Repo...
4059
  			.pgoff  = (u64)vma->vm_pgoff << PAGE_SHIFT,
0a4a93919   Peter Zijlstra   perf_counter: exe...
4060
4061
  		},
  	};
cdd6c482c   Ingo Molnar   perf: Do the big ...
4062
  	perf_event_mmap_event(&mmap_event);
0a4a93919   Peter Zijlstra   perf_counter: exe...
4063
  }
0a4a93919   Peter Zijlstra   perf_counter: exe...
4064
  /*
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4065
4066
   * IRQ throttle logging
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
4067
  static void perf_log_throttle(struct perf_event *event, int enable)
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4068
4069
  {
  	struct perf_output_handle handle;
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
4070
  	struct perf_sample_data sample;
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4071
4072
4073
4074
4075
  	int ret;
  
  	struct {
  		struct perf_event_header	header;
  		u64				time;
cca3f454a   Peter Zijlstra   perf_counter: Add...
4076
  		u64				id;
7f453c24b   Peter Zijlstra   perf_counter: PER...
4077
  		u64				stream_id;
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4078
4079
  	} throttle_event = {
  		.header = {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4080
  			.type = PERF_RECORD_THROTTLE,
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4081
4082
4083
  			.misc = 0,
  			.size = sizeof(throttle_event),
  		},
def0a9b25   Peter Zijlstra   sched_clock: Make...
4084
  		.time		= perf_clock(),
cdd6c482c   Ingo Molnar   perf: Do the big ...
4085
4086
  		.id		= primary_event_id(event),
  		.stream_id	= event->id,
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4087
  	};
966ee4d6b   Anton Blanchard   perf_counter: Fix...
4088
  	if (enable)
cdd6c482c   Ingo Molnar   perf: Do the big ...
4089
  		throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
966ee4d6b   Anton Blanchard   perf_counter: Fix...
4090

c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
4091
4092
4093
4094
  	perf_event_header__init_id(&throttle_event.header, &sample, event);
  
  	ret = perf_output_begin(&handle, event,
  				throttle_event.header.size, 1, 0);
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4095
4096
4097
4098
  	if (ret)
  		return;
  
  	perf_output_put(&handle, throttle_event);
c980d1091   Arnaldo Carvalho de Melo   perf events: Make...
4099
  	perf_event__output_id_sample(event, &handle, &sample);
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4100
4101
4102
4103
  	perf_output_end(&handle);
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
4104
   * Generic event overflow handling, sampling.
f6c7d5fe5   Peter Zijlstra   perf_counter: the...
4105
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
4106
  static int __perf_event_overflow(struct perf_event *event, int nmi,
5622f295b   Markus Metzger   x86, perf_counter...
4107
4108
  				   int throttle, struct perf_sample_data *data,
  				   struct pt_regs *regs)
f6c7d5fe5   Peter Zijlstra   perf_counter: the...
4109
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4110
4111
  	int events = atomic_read(&event->event_limit);
  	struct hw_perf_event *hwc = &event->hw;
79f146415   Peter Zijlstra   perf_counter: cou...
4112
  	int ret = 0;
963988262   Peter Zijlstra   perf: Ignore non-...
4113
4114
4115
4116
4117
4118
  	/*
  	 * Non-sampling counters might still use the PMI to fold short
  	 * hardware counters, ignore those.
  	 */
  	if (unlikely(!is_sampling_event(event)))
  		return 0;
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4119
  	if (!throttle) {
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
4120
  		hwc->interrupts++;
128f048f0   Ingo Molnar   perf_counter: Fix...
4121
  	} else {
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
4122
4123
  		if (hwc->interrupts != MAX_INTERRUPTS) {
  			hwc->interrupts++;
df58ab24b   Peter Zijlstra   perf_counter: Ren...
4124
  			if (HZ * hwc->interrupts >
cdd6c482c   Ingo Molnar   perf: Do the big ...
4125
  					(u64)sysctl_perf_event_sample_rate) {
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
4126
  				hwc->interrupts = MAX_INTERRUPTS;
cdd6c482c   Ingo Molnar   perf: Do the big ...
4127
  				perf_log_throttle(event, 0);
128f048f0   Ingo Molnar   perf_counter: Fix...
4128
4129
4130
4131
  				ret = 1;
  			}
  		} else {
  			/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
4132
  			 * Keep re-disabling events even though on the previous
128f048f0   Ingo Molnar   perf_counter: Fix...
4133
  			 * pass we disabled it - just in case we raced with a
cdd6c482c   Ingo Molnar   perf: Do the big ...
4134
  			 * sched-in and the event got enabled again:
128f048f0   Ingo Molnar   perf_counter: Fix...
4135
  			 */
a78ac3258   Peter Zijlstra   perf_counter: Gen...
4136
4137
4138
  			ret = 1;
  		}
  	}
60db5e09c   Peter Zijlstra   perf_counter: fre...
4139

cdd6c482c   Ingo Molnar   perf: Do the big ...
4140
  	if (event->attr.freq) {
def0a9b25   Peter Zijlstra   sched_clock: Make...
4141
  		u64 now = perf_clock();
abd507139   Peter Zijlstra   perf: Reimplement...
4142
  		s64 delta = now - hwc->freq_time_stamp;
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
4143

abd507139   Peter Zijlstra   perf: Reimplement...
4144
  		hwc->freq_time_stamp = now;
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
4145

abd507139   Peter Zijlstra   perf: Reimplement...
4146
4147
  		if (delta > 0 && delta < 2*TICK_NSEC)
  			perf_adjust_period(event, delta, hwc->last_period);
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
4148
  	}
2023b3592   Peter Zijlstra   perf_counter: inh...
4149
4150
  	/*
  	 * XXX event_limit might not quite work as expected on inherited
cdd6c482c   Ingo Molnar   perf: Do the big ...
4151
  	 * events
2023b3592   Peter Zijlstra   perf_counter: inh...
4152
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
4153
4154
  	event->pending_kill = POLL_IN;
  	if (events && atomic_dec_and_test(&event->event_limit)) {
79f146415   Peter Zijlstra   perf_counter: cou...
4155
  		ret = 1;
cdd6c482c   Ingo Molnar   perf: Do the big ...
4156
  		event->pending_kill = POLL_HUP;
79f146415   Peter Zijlstra   perf_counter: cou...
4157
  		if (nmi) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4158
  			event->pending_disable = 1;
e360adbe2   Peter Zijlstra   irq_work: Add gen...
4159
  			irq_work_queue(&event->pending);
79f146415   Peter Zijlstra   perf_counter: cou...
4160
  		} else
cdd6c482c   Ingo Molnar   perf: Do the big ...
4161
  			perf_event_disable(event);
79f146415   Peter Zijlstra   perf_counter: cou...
4162
  	}
453f19eea   Peter Zijlstra   perf: Allow for c...
4163
4164
4165
4166
  	if (event->overflow_handler)
  		event->overflow_handler(event, nmi, data, regs);
  	else
  		perf_event_output(event, nmi, data, regs);
79f146415   Peter Zijlstra   perf_counter: cou...
4167
  	return ret;
f6c7d5fe5   Peter Zijlstra   perf_counter: the...
4168
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
4169
  int perf_event_overflow(struct perf_event *event, int nmi,
5622f295b   Markus Metzger   x86, perf_counter...
4170
4171
  			  struct perf_sample_data *data,
  			  struct pt_regs *regs)
850bc73ff   Peter Zijlstra   perf_counter: Do ...
4172
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4173
  	return __perf_event_overflow(event, nmi, 1, data, regs);
850bc73ff   Peter Zijlstra   perf_counter: Do ...
4174
  }
f6c7d5fe5   Peter Zijlstra   perf_counter: the...
4175
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
4176
   * Generic software event infrastructure
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4177
   */
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
  struct swevent_htable {
  	struct swevent_hlist		*swevent_hlist;
  	struct mutex			hlist_mutex;
  	int				hlist_refcount;
  
  	/* Recursion avoidance in each contexts */
  	int				recursion[PERF_NR_CONTEXTS];
  };
  
  static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4188
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
4189
4190
   * We directly increment event->count and keep a second value in
   * event->hw.period_left to count intervals. This period event
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4191
4192
4193
   * is kept in the range [-sample_period, 0] so that we can use the
   * sign as trigger.
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
4194
  static u64 perf_swevent_set_period(struct perf_event *event)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4195
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4196
  	struct hw_perf_event *hwc = &event->hw;
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4197
4198
4199
4200
4201
  	u64 period = hwc->last_period;
  	u64 nr, offset;
  	s64 old, val;
  
  	hwc->last_period = hwc->sample_period;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4202
4203
  
  again:
e78505958   Peter Zijlstra   perf: Convert per...
4204
  	old = val = local64_read(&hwc->period_left);
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4205
4206
  	if (val < 0)
  		return 0;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4207

7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4208
4209
4210
  	nr = div64_u64(period + val, period);
  	offset = nr * period;
  	val -= offset;
e78505958   Peter Zijlstra   perf: Convert per...
4211
  	if (local64_cmpxchg(&hwc->period_left, old, val) != old)
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4212
  		goto again;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4213

7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4214
  	return nr;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4215
  }
0cff784ae   Peter Zijlstra   perf: Optimize so...
4216
  static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
5622f295b   Markus Metzger   x86, perf_counter...
4217
4218
  				    int nmi, struct perf_sample_data *data,
  				    struct pt_regs *regs)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4219
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4220
  	struct hw_perf_event *hwc = &event->hw;
850bc73ff   Peter Zijlstra   perf_counter: Do ...
4221
  	int throttle = 0;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4222

cdd6c482c   Ingo Molnar   perf: Do the big ...
4223
  	data->period = event->hw.last_period;
0cff784ae   Peter Zijlstra   perf: Optimize so...
4224
4225
  	if (!overflow)
  		overflow = perf_swevent_set_period(event);
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4226

7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4227
4228
  	if (hwc->interrupts == MAX_INTERRUPTS)
  		return;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4229

7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4230
  	for (; overflow; overflow--) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4231
  		if (__perf_event_overflow(event, nmi, throttle,
5622f295b   Markus Metzger   x86, perf_counter...
4232
  					    data, regs)) {
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4233
4234
4235
4236
4237
4238
  			/*
  			 * We inhibit the overflow from happening when
  			 * hwc->interrupts == MAX_INTERRUPTS.
  			 */
  			break;
  		}
cf450a735   Peter Zijlstra   perf_counter: Fix...
4239
  		throttle = 1;
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4240
  	}
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4241
  }
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4242
  static void perf_swevent_event(struct perf_event *event, u64 nr,
5622f295b   Markus Metzger   x86, perf_counter...
4243
4244
  			       int nmi, struct perf_sample_data *data,
  			       struct pt_regs *regs)
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4245
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4246
  	struct hw_perf_event *hwc = &event->hw;
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4247

e78505958   Peter Zijlstra   perf: Convert per...
4248
  	local64_add(nr, &event->count);
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4249

0cff784ae   Peter Zijlstra   perf: Optimize so...
4250
4251
  	if (!regs)
  		return;
6c7e550f1   Franck Bui-Huu   perf: Introduce i...
4252
  	if (!is_sampling_event(event))
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4253
  		return;
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4254

0cff784ae   Peter Zijlstra   perf: Optimize so...
4255
4256
  	if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  		return perf_swevent_overflow(event, 1, nmi, data, regs);
e78505958   Peter Zijlstra   perf: Convert per...
4257
  	if (local64_add_negative(nr, &hwc->period_left))
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4258
  		return;
df1a132bf   Peter Zijlstra   perf_counter: Int...
4259

0cff784ae   Peter Zijlstra   perf: Optimize so...
4260
  	perf_swevent_overflow(event, 0, nmi, data, regs);
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4261
  }
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4262
4263
4264
  static int perf_exclude_event(struct perf_event *event,
  			      struct pt_regs *regs)
  {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4265
4266
  	if (event->hw.state & PERF_HES_STOPPED)
  		return 0;
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
  	if (regs) {
  		if (event->attr.exclude_user && user_mode(regs))
  			return 1;
  
  		if (event->attr.exclude_kernel && !user_mode(regs))
  			return 1;
  	}
  
  	return 0;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
4277
  static int perf_swevent_match(struct perf_event *event,
1c432d899   Peter Zijlstra   perf_counter: Ren...
4278
  				enum perf_type_id type,
6fb2915df   Li Zefan   tracing/profile: ...
4279
4280
4281
  				u32 event_id,
  				struct perf_sample_data *data,
  				struct pt_regs *regs)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4282
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
4283
  	if (event->attr.type != type)
a21ca2cac   Ingo Molnar   perf_counter: Sep...
4284
  		return 0;
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4285

cdd6c482c   Ingo Molnar   perf: Do the big ...
4286
  	if (event->attr.config != event_id)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4287
  		return 0;
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4288
4289
  	if (perf_exclude_event(event, regs))
  		return 0;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4290
4291
4292
  
  	return 1;
  }
76e1d9047   Frederic Weisbecker   perf: Store activ...
4293
4294
4295
4296
4297
4298
  static inline u64 swevent_hash(u64 type, u32 event_id)
  {
  	u64 val = event_id | (type << 32);
  
  	return hash_64(val, SWEVENT_HLIST_BITS);
  }
49f135ed0   Frederic Weisbecker   perf: Comply with...
4299
4300
  static inline struct hlist_head *
  __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
76e1d9047   Frederic Weisbecker   perf: Store activ...
4301
  {
49f135ed0   Frederic Weisbecker   perf: Comply with...
4302
4303
4304
4305
  	u64 hash = swevent_hash(type, event_id);
  
  	return &hlist->heads[hash];
  }
76e1d9047   Frederic Weisbecker   perf: Store activ...
4306

49f135ed0   Frederic Weisbecker   perf: Comply with...
4307
4308
  /* For the read side: events when they trigger */
  static inline struct hlist_head *
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4309
  find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
49f135ed0   Frederic Weisbecker   perf: Comply with...
4310
4311
  {
  	struct swevent_hlist *hlist;
76e1d9047   Frederic Weisbecker   perf: Store activ...
4312

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4313
  	hlist = rcu_dereference(swhash->swevent_hlist);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4314
4315
  	if (!hlist)
  		return NULL;
49f135ed0   Frederic Weisbecker   perf: Comply with...
4316
4317
4318
4319
4320
  	return __find_swevent_head(hlist, type, event_id);
  }
  
  /* For the event head insertion and removal in the hlist */
  static inline struct hlist_head *
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4321
  find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
49f135ed0   Frederic Weisbecker   perf: Comply with...
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
  {
  	struct swevent_hlist *hlist;
  	u32 event_id = event->attr.config;
  	u64 type = event->attr.type;
  
  	/*
  	 * Event scheduling is always serialized against hlist allocation
  	 * and release. Which makes the protected version suitable here.
  	 * The context lock guarantees that.
  	 */
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4332
  	hlist = rcu_dereference_protected(swhash->swevent_hlist,
49f135ed0   Frederic Weisbecker   perf: Comply with...
4333
4334
4335
4336
4337
  					  lockdep_is_held(&event->ctx->lock));
  	if (!hlist)
  		return NULL;
  
  	return __find_swevent_head(hlist, type, event_id);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4338
4339
4340
4341
4342
4343
  }
  
  static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  				    u64 nr, int nmi,
  				    struct perf_sample_data *data,
  				    struct pt_regs *regs)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4344
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4345
  	struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
cdd6c482c   Ingo Molnar   perf: Do the big ...
4346
  	struct perf_event *event;
76e1d9047   Frederic Weisbecker   perf: Store activ...
4347
4348
  	struct hlist_node *node;
  	struct hlist_head *head;
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4349

76e1d9047   Frederic Weisbecker   perf: Store activ...
4350
  	rcu_read_lock();
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4351
  	head = find_swevent_head_rcu(swhash, type, event_id);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4352
4353
4354
4355
  	if (!head)
  		goto end;
  
  	hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
6fb2915df   Li Zefan   tracing/profile: ...
4356
  		if (perf_swevent_match(event, type, event_id, data, regs))
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4357
  			perf_swevent_event(event, nr, nmi, data, regs);
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4358
  	}
76e1d9047   Frederic Weisbecker   perf: Store activ...
4359
4360
  end:
  	rcu_read_unlock();
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4361
  }
4ed7c92d6   Peter Zijlstra   perf_events: Undo...
4362
  int perf_swevent_get_recursion_context(void)
96f6d4444   Peter Zijlstra   perf_counter: avo...
4363
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4364
  	struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
96f6d4444   Peter Zijlstra   perf_counter: avo...
4365

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4366
  	return get_recursion_context(swhash->recursion);
96f6d4444   Peter Zijlstra   perf_counter: avo...
4367
  }
645e8cc0c   Ingo Molnar   perf_events: Fix ...
4368
  EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
96f6d4444   Peter Zijlstra   perf_counter: avo...
4369

fa9f90be7   Jesper Juhl   Kill off a bunch ...
4370
  inline void perf_swevent_put_recursion_context(int rctx)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4371
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4372
  	struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
4373

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4374
  	put_recursion_context(swhash->recursion, rctx);
ce71b9df8   Frederic Weisbecker   tracing: Use the ...
4375
  }
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4376

cdd6c482c   Ingo Molnar   perf: Do the big ...
4377
  void __perf_sw_event(u32 event_id, u64 nr, int nmi,
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4378
  			    struct pt_regs *regs, u64 addr)
b8e83514b   Peter Zijlstra   perf_counter: rev...
4379
  {
a4234bfcf   Ingo Molnar   perf_events: Opti...
4380
  	struct perf_sample_data data;
4ed7c92d6   Peter Zijlstra   perf_events: Undo...
4381
  	int rctx;
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4382
  	preempt_disable_notrace();
4ed7c92d6   Peter Zijlstra   perf_events: Undo...
4383
4384
4385
  	rctx = perf_swevent_get_recursion_context();
  	if (rctx < 0)
  		return;
a4234bfcf   Ingo Molnar   perf_events: Opti...
4386

dc1d628a6   Peter Zijlstra   perf: Provide gen...
4387
  	perf_sample_data_init(&data, addr);
92bf309a9   Peter Zijlstra   perf_counter: Pus...
4388

a4234bfcf   Ingo Molnar   perf_events: Opti...
4389
  	do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
4ed7c92d6   Peter Zijlstra   perf_events: Undo...
4390
4391
  
  	perf_swevent_put_recursion_context(rctx);
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4392
  	preempt_enable_notrace();
b8e83514b   Peter Zijlstra   perf_counter: rev...
4393
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
4394
  static void perf_swevent_read(struct perf_event *event)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4395
  {
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4396
  }
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4397
  static int perf_swevent_add(struct perf_event *event, int flags)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4398
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4399
  	struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
cdd6c482c   Ingo Molnar   perf: Do the big ...
4400
  	struct hw_perf_event *hwc = &event->hw;
76e1d9047   Frederic Weisbecker   perf: Store activ...
4401
  	struct hlist_head *head;
6c7e550f1   Franck Bui-Huu   perf: Introduce i...
4402
  	if (is_sampling_event(event)) {
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4403
  		hwc->last_period = hwc->sample_period;
cdd6c482c   Ingo Molnar   perf: Do the big ...
4404
  		perf_swevent_set_period(event);
7b4b6658e   Peter Zijlstra   perf_counter: Fix...
4405
  	}
76e1d9047   Frederic Weisbecker   perf: Store activ...
4406

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4407
  	hwc->state = !(flags & PERF_EF_START);
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4408
  	head = find_swevent_head(swhash, event);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4409
4410
4411
4412
  	if (WARN_ON_ONCE(!head))
  		return -EINVAL;
  
  	hlist_add_head_rcu(&event->hlist_entry, head);
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4413
4414
  	return 0;
  }
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4415
  static void perf_swevent_del(struct perf_event *event, int flags)
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4416
  {
76e1d9047   Frederic Weisbecker   perf: Store activ...
4417
  	hlist_del_rcu(&event->hlist_entry);
15dbf27cc   Peter Zijlstra   perf_counter: sof...
4418
  }
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4419
  static void perf_swevent_start(struct perf_event *event, int flags)
5c92d1241   Ingo Molnar   perf counters: im...
4420
  {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4421
  	event->hw.state = 0;
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4422
  }
aa9c4c0f9   Ingo Molnar   perfcounters: fix...
4423

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4424
  static void perf_swevent_stop(struct perf_event *event, int flags)
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4425
  {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4426
  	event->hw.state = PERF_HES_STOPPED;
bae43c994   Ingo Molnar   perf counters: im...
4427
  }
49f135ed0   Frederic Weisbecker   perf: Comply with...
4428
4429
  /* Deref the hlist from the update side */
  static inline struct swevent_hlist *
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4430
  swevent_hlist_deref(struct swevent_htable *swhash)
49f135ed0   Frederic Weisbecker   perf: Comply with...
4431
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4432
4433
  	return rcu_dereference_protected(swhash->swevent_hlist,
  					 lockdep_is_held(&swhash->hlist_mutex));
49f135ed0   Frederic Weisbecker   perf: Comply with...
4434
  }
76e1d9047   Frederic Weisbecker   perf: Store activ...
4435
4436
4437
4438
4439
4440
4441
  static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  {
  	struct swevent_hlist *hlist;
  
  	hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  	kfree(hlist);
  }
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4442
  static void swevent_hlist_release(struct swevent_htable *swhash)
76e1d9047   Frederic Weisbecker   perf: Store activ...
4443
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4444
  	struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4445

49f135ed0   Frederic Weisbecker   perf: Comply with...
4446
  	if (!hlist)
76e1d9047   Frederic Weisbecker   perf: Store activ...
4447
  		return;
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4448
  	rcu_assign_pointer(swhash->swevent_hlist, NULL);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4449
4450
4451
4452
4453
  	call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  }
  
  static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4454
  	struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4455

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4456
  	mutex_lock(&swhash->hlist_mutex);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4457

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4458
4459
  	if (!--swhash->hlist_refcount)
  		swevent_hlist_release(swhash);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4460

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4461
  	mutex_unlock(&swhash->hlist_mutex);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
  }
  
  static void swevent_hlist_put(struct perf_event *event)
  {
  	int cpu;
  
  	if (event->cpu != -1) {
  		swevent_hlist_put_cpu(event, event->cpu);
  		return;
  	}
  
  	for_each_possible_cpu(cpu)
  		swevent_hlist_put_cpu(event, cpu);
  }
  
  static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4479
  	struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4480
  	int err = 0;
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4481
  	mutex_lock(&swhash->hlist_mutex);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4482

b28ab83c5   Peter Zijlstra   perf: Remove the ...
4483
  	if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
76e1d9047   Frederic Weisbecker   perf: Store activ...
4484
4485
4486
4487
4488
4489
4490
  		struct swevent_hlist *hlist;
  
  		hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  		if (!hlist) {
  			err = -ENOMEM;
  			goto exit;
  		}
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4491
  		rcu_assign_pointer(swhash->swevent_hlist, hlist);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4492
  	}
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4493
  	swhash->hlist_refcount++;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
4494
  exit:
b28ab83c5   Peter Zijlstra   perf: Remove the ...
4495
  	mutex_unlock(&swhash->hlist_mutex);
76e1d9047   Frederic Weisbecker   perf: Store activ...
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
  
  	return err;
  }
  
  static int swevent_hlist_get(struct perf_event *event)
  {
  	int err;
  	int cpu, failed_cpu;
  
  	if (event->cpu != -1)
  		return swevent_hlist_get_cpu(event, event->cpu);
  
  	get_online_cpus();
  	for_each_possible_cpu(cpu) {
  		err = swevent_hlist_get_cpu(event, cpu);
  		if (err) {
  			failed_cpu = cpu;
  			goto fail;
  		}
  	}
  	put_online_cpus();
  
  	return 0;
9ed6060d2   Peter Zijlstra   perf: Unindent la...
4519
  fail:
76e1d9047   Frederic Weisbecker   perf: Store activ...
4520
4521
4522
4523
4524
4525
4526
4527
4528
  	for_each_possible_cpu(cpu) {
  		if (cpu == failed_cpu)
  			break;
  		swevent_hlist_put_cpu(event, cpu);
  	}
  
  	put_online_cpus();
  	return err;
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4529
  atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
95476b64a   Frederic Weisbecker   perf: Fix hlist r...
4530

b0a873ebb   Peter Zijlstra   perf: Register PM...
4531
4532
4533
  static void sw_perf_event_destroy(struct perf_event *event)
  {
  	u64 event_id = event->attr.config;
95476b64a   Frederic Weisbecker   perf: Fix hlist r...
4534

b0a873ebb   Peter Zijlstra   perf: Register PM...
4535
  	WARN_ON(event->parent);
7e54a5a0b   Peter Zijlstra   perf: Optimize sw...
4536
  	jump_label_dec(&perf_swevent_enabled[event_id]);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
  	swevent_hlist_put(event);
  }
  
  static int perf_swevent_init(struct perf_event *event)
  {
  	int event_id = event->attr.config;
  
  	if (event->attr.type != PERF_TYPE_SOFTWARE)
  		return -ENOENT;
  
  	switch (event_id) {
  	case PERF_COUNT_SW_CPU_CLOCK:
  	case PERF_COUNT_SW_TASK_CLOCK:
  		return -ENOENT;
  
  	default:
  		break;
  	}
ce677831a   Dan Carpenter   perf: Fix off by ...
4555
  	if (event_id >= PERF_COUNT_SW_MAX)
b0a873ebb   Peter Zijlstra   perf: Register PM...
4556
4557
4558
4559
4560
4561
4562
4563
  		return -ENOENT;
  
  	if (!event->parent) {
  		int err;
  
  		err = swevent_hlist_get(event);
  		if (err)
  			return err;
7e54a5a0b   Peter Zijlstra   perf: Optimize sw...
4564
  		jump_label_inc(&perf_swevent_enabled[event_id]);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4565
4566
4567
4568
4569
4570
4571
  		event->destroy = sw_perf_event_destroy;
  	}
  
  	return 0;
  }
  
  static struct pmu perf_swevent = {
89a1e1873   Peter Zijlstra   perf: Provide a s...
4572
  	.task_ctx_nr	= perf_sw_context,
95476b64a   Frederic Weisbecker   perf: Fix hlist r...
4573

b0a873ebb   Peter Zijlstra   perf: Register PM...
4574
  	.event_init	= perf_swevent_init,
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4575
4576
4577
4578
  	.add		= perf_swevent_add,
  	.del		= perf_swevent_del,
  	.start		= perf_swevent_start,
  	.stop		= perf_swevent_stop,
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4579
  	.read		= perf_swevent_read,
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4580
  };
b0a873ebb   Peter Zijlstra   perf: Register PM...
4581
  #ifdef CONFIG_EVENT_TRACING
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
  static int perf_tp_filter_match(struct perf_event *event,
  				struct perf_sample_data *data)
  {
  	void *record = data->raw->data;
  
  	if (likely(!event->filter) || filter_match_preds(event->filter, record))
  		return 1;
  	return 0;
  }
  
  static int perf_tp_event_match(struct perf_event *event,
  				struct perf_sample_data *data,
  				struct pt_regs *regs)
  {
580d607cd   Peter Zijlstra   perf: Optimize pe...
4596
4597
4598
4599
  	/*
  	 * All tracepoints are from kernel-space.
  	 */
  	if (event->attr.exclude_kernel)
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4600
4601
4602
4603
4604
4605
4606
4607
4608
  		return 0;
  
  	if (!perf_tp_filter_match(event, data))
  		return 0;
  
  	return 1;
  }
  
  void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
ecc55f84b   Peter Zijlstra   perf, trace: Inli...
4609
  		   struct pt_regs *regs, struct hlist_head *head, int rctx)
95476b64a   Frederic Weisbecker   perf: Fix hlist r...
4610
4611
  {
  	struct perf_sample_data data;
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4612
4613
  	struct perf_event *event;
  	struct hlist_node *node;
95476b64a   Frederic Weisbecker   perf: Fix hlist r...
4614
4615
4616
4617
4618
4619
4620
  	struct perf_raw_record raw = {
  		.size = entry_size,
  		.data = record,
  	};
  
  	perf_sample_data_init(&data, addr);
  	data.raw = &raw;
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4621
4622
  	hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  		if (perf_tp_event_match(event, &data, regs))
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4623
  			perf_swevent_event(event, count, 1, &data, regs);
4f41c013f   Peter Zijlstra   perf/ftrace: Opti...
4624
  	}
ecc55f84b   Peter Zijlstra   perf, trace: Inli...
4625
4626
  
  	perf_swevent_put_recursion_context(rctx);
95476b64a   Frederic Weisbecker   perf: Fix hlist r...
4627
4628
  }
  EXPORT_SYMBOL_GPL(perf_tp_event);
cdd6c482c   Ingo Molnar   perf: Do the big ...
4629
  static void tp_perf_event_destroy(struct perf_event *event)
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4630
  {
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4631
  	perf_trace_destroy(event);
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4632
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4633
  static int perf_tp_event_init(struct perf_event *event)
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4634
  {
76e1d9047   Frederic Weisbecker   perf: Store activ...
4635
  	int err;
b0a873ebb   Peter Zijlstra   perf: Register PM...
4636
4637
  	if (event->attr.type != PERF_TYPE_TRACEPOINT)
  		return -ENOENT;
1c024eca5   Peter Zijlstra   perf, trace: Opti...
4638
4639
  	err = perf_trace_init(event);
  	if (err)
b0a873ebb   Peter Zijlstra   perf: Register PM...
4640
  		return err;
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4641

cdd6c482c   Ingo Molnar   perf: Do the big ...
4642
  	event->destroy = tp_perf_event_destroy;
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4643

b0a873ebb   Peter Zijlstra   perf: Register PM...
4644
4645
4646
4647
  	return 0;
  }
  
  static struct pmu perf_tracepoint = {
89a1e1873   Peter Zijlstra   perf: Provide a s...
4648
  	.task_ctx_nr	= perf_sw_context,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4649
  	.event_init	= perf_tp_event_init,
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4650
4651
4652
4653
  	.add		= perf_trace_add,
  	.del		= perf_trace_del,
  	.start		= perf_swevent_start,
  	.stop		= perf_swevent_stop,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4654
  	.read		= perf_swevent_read,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4655
4656
4657
4658
  };
  
  static inline void perf_tp_register(void)
  {
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
4659
  	perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4660
  }
6fb2915df   Li Zefan   tracing/profile: ...
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
  
  static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  {
  	char *filter_str;
  	int ret;
  
  	if (event->attr.type != PERF_TYPE_TRACEPOINT)
  		return -EINVAL;
  
  	filter_str = strndup_user(arg, PAGE_SIZE);
  	if (IS_ERR(filter_str))
  		return PTR_ERR(filter_str);
  
  	ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  
  	kfree(filter_str);
  	return ret;
  }
  
  static void perf_event_free_filter(struct perf_event *event)
  {
  	ftrace_profile_free_filter(event);
  }
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4684
  #else
6fb2915df   Li Zefan   tracing/profile: ...
4685

b0a873ebb   Peter Zijlstra   perf: Register PM...
4686
  static inline void perf_tp_register(void)
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4687
  {
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4688
  }
6fb2915df   Li Zefan   tracing/profile: ...
4689
4690
4691
4692
4693
4694
4695
4696
4697
  
  static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  {
  	return -ENOENT;
  }
  
  static void perf_event_free_filter(struct perf_event *event)
  {
  }
07b139c8c   Li Zefan   perf events: Remo...
4698
  #endif /* CONFIG_EVENT_TRACING */
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4699

24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4700
  #ifdef CONFIG_HAVE_HW_BREAKPOINT
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4701
  void perf_bp_event(struct perf_event *bp, void *data)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4702
  {
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4703
4704
  	struct perf_sample_data sample;
  	struct pt_regs *regs = data;
dc1d628a6   Peter Zijlstra   perf: Provide gen...
4705
  	perf_sample_data_init(&sample, bp->attr.bp_addr);
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4706

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4707
4708
  	if (!bp->hw.state && !perf_exclude_event(bp, regs))
  		perf_swevent_event(bp, 1, 1, &sample, regs);
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4709
4710
  }
  #endif
b0a873ebb   Peter Zijlstra   perf: Register PM...
4711
4712
4713
  /*
   * hrtimer based swevent callback
   */
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4714

b0a873ebb   Peter Zijlstra   perf: Register PM...
4715
  static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4716
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4717
4718
4719
4720
4721
  	enum hrtimer_restart ret = HRTIMER_RESTART;
  	struct perf_sample_data data;
  	struct pt_regs *regs;
  	struct perf_event *event;
  	u64 period;
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4722

b0a873ebb   Peter Zijlstra   perf: Register PM...
4723
4724
  	event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  	event->pmu->read(event);
f344011cc   Peter Zijlstra   perf_counter: Opt...
4725

b0a873ebb   Peter Zijlstra   perf: Register PM...
4726
4727
4728
4729
4730
4731
4732
4733
4734
  	perf_sample_data_init(&data, 0);
  	data.period = event->hw.last_period;
  	regs = get_irq_regs();
  
  	if (regs && !perf_exclude_event(event, regs)) {
  		if (!(event->attr.exclude_idle && current->pid == 0))
  			if (perf_event_overflow(event, 0, &data, regs))
  				ret = HRTIMER_NORESTART;
  	}
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4735

b0a873ebb   Peter Zijlstra   perf: Register PM...
4736
4737
  	period = max_t(u64, 10000, event->hw.sample_period);
  	hrtimer_forward_now(hrtimer, ns_to_ktime(period));
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4738

b0a873ebb   Peter Zijlstra   perf: Register PM...
4739
  	return ret;
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4740
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4741
  static void perf_swevent_start_hrtimer(struct perf_event *event)
5c92d1241   Ingo Molnar   perf counters: im...
4742
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4743
  	struct hw_perf_event *hwc = &event->hw;
5d508e820   Franck Bui-Huu   perf: Don't bothe...
4744
4745
4746
4747
  	s64 period;
  
  	if (!is_sampling_event(event))
  		return;
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4748

b0a873ebb   Peter Zijlstra   perf: Register PM...
4749
4750
  	hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  	hwc->hrtimer.function = perf_swevent_hrtimer;
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4751

5d508e820   Franck Bui-Huu   perf: Don't bothe...
4752
4753
4754
4755
  	period = local64_read(&hwc->period_left);
  	if (period) {
  		if (period < 0)
  			period = 10000;
fa407f35e   Peter Zijlstra   perf: Shrink hw_p...
4756

5d508e820   Franck Bui-Huu   perf: Don't bothe...
4757
4758
4759
4760
4761
  		local64_set(&hwc->period_left, 0);
  	} else {
  		period = max_t(u64, 10000, hwc->sample_period);
  	}
  	__hrtimer_start_range_ns(&hwc->hrtimer,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4762
  				ns_to_ktime(period), 0,
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
4763
  				HRTIMER_MODE_REL_PINNED, 0);
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4764
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4765
4766
  
  static void perf_swevent_cancel_hrtimer(struct perf_event *event)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4767
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4768
  	struct hw_perf_event *hwc = &event->hw;
6c7e550f1   Franck Bui-Huu   perf: Introduce i...
4769
  	if (is_sampling_event(event)) {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4770
  		ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
fa407f35e   Peter Zijlstra   perf: Shrink hw_p...
4771
  		local64_set(&hwc->period_left, ktime_to_ns(remaining));
b0a873ebb   Peter Zijlstra   perf: Register PM...
4772
4773
4774
  
  		hrtimer_cancel(&hwc->hrtimer);
  	}
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4775
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4776
4777
4778
4779
4780
  /*
   * Software event: cpu wall time clock
   */
  
  static void cpu_clock_event_update(struct perf_event *event)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4781
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4782
4783
  	s64 prev;
  	u64 now;
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4784
  	now = local_clock();
b0a873ebb   Peter Zijlstra   perf: Register PM...
4785
4786
  	prev = local64_xchg(&event->hw.prev_count, now);
  	local64_add(now - prev, &event->count);
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4787
  }
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4788

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4789
  static void cpu_clock_event_start(struct perf_event *event, int flags)
b0a873ebb   Peter Zijlstra   perf: Register PM...
4790
  {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4791
  	local64_set(&event->hw.prev_count, local_clock());
b0a873ebb   Peter Zijlstra   perf: Register PM...
4792
  	perf_swevent_start_hrtimer(event);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4793
  }
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4794
  static void cpu_clock_event_stop(struct perf_event *event, int flags)
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4795
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4796
4797
4798
  	perf_swevent_cancel_hrtimer(event);
  	cpu_clock_event_update(event);
  }
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4799

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
  static int cpu_clock_event_add(struct perf_event *event, int flags)
  {
  	if (flags & PERF_EF_START)
  		cpu_clock_event_start(event, flags);
  
  	return 0;
  }
  
  static void cpu_clock_event_del(struct perf_event *event, int flags)
  {
  	cpu_clock_event_stop(event, flags);
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4812
4813
4814
4815
  static void cpu_clock_event_read(struct perf_event *event)
  {
  	cpu_clock_event_update(event);
  }
f344011cc   Peter Zijlstra   perf_counter: Opt...
4816

b0a873ebb   Peter Zijlstra   perf: Register PM...
4817
4818
4819
4820
4821
4822
4823
4824
4825
  static int cpu_clock_event_init(struct perf_event *event)
  {
  	if (event->attr.type != PERF_TYPE_SOFTWARE)
  		return -ENOENT;
  
  	if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  		return -ENOENT;
  
  	return 0;
f29ac756a   Peter Zijlstra   perf_counter: Opt...
4826
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4827
  static struct pmu perf_cpu_clock = {
89a1e1873   Peter Zijlstra   perf: Provide a s...
4828
  	.task_ctx_nr	= perf_sw_context,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4829
  	.event_init	= cpu_clock_event_init,
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4830
4831
4832
4833
  	.add		= cpu_clock_event_add,
  	.del		= cpu_clock_event_del,
  	.start		= cpu_clock_event_start,
  	.stop		= cpu_clock_event_stop,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4834
4835
4836
4837
4838
4839
4840
4841
  	.read		= cpu_clock_event_read,
  };
  
  /*
   * Software event: task time clock
   */
  
  static void task_clock_event_update(struct perf_event *event, u64 now)
5c92d1241   Ingo Molnar   perf counters: im...
4842
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4843
4844
  	u64 prev;
  	s64 delta;
5c92d1241   Ingo Molnar   perf counters: im...
4845

b0a873ebb   Peter Zijlstra   perf: Register PM...
4846
4847
4848
4849
  	prev = local64_xchg(&event->hw.prev_count, now);
  	delta = now - prev;
  	local64_add(delta, &event->count);
  }
5c92d1241   Ingo Molnar   perf counters: im...
4850

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4851
  static void task_clock_event_start(struct perf_event *event, int flags)
b0a873ebb   Peter Zijlstra   perf: Register PM...
4852
  {
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4853
  	local64_set(&event->hw.prev_count, event->ctx->time);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4854
  	perf_swevent_start_hrtimer(event);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4855
  }
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4856
  static void task_clock_event_stop(struct perf_event *event, int flags)
b0a873ebb   Peter Zijlstra   perf: Register PM...
4857
4858
4859
  {
  	perf_swevent_cancel_hrtimer(event);
  	task_clock_event_update(event, event->ctx->time);
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4860
4861
4862
4863
4864
4865
  }
  
  static int task_clock_event_add(struct perf_event *event, int flags)
  {
  	if (flags & PERF_EF_START)
  		task_clock_event_start(event, flags);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4866

a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4867
4868
4869
4870
4871
4872
  	return 0;
  }
  
  static void task_clock_event_del(struct perf_event *event, int flags)
  {
  	task_clock_event_stop(event, PERF_EF_UPDATE);
b0a873ebb   Peter Zijlstra   perf: Register PM...
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
  }
  
  static void task_clock_event_read(struct perf_event *event)
  {
  	u64 time;
  
  	if (!in_nmi()) {
  		update_context_time(event->ctx);
  		time = event->ctx->time;
  	} else {
  		u64 now = perf_clock();
  		u64 delta = now - event->ctx->timestamp;
  		time = event->ctx->time + delta;
  	}
  
  	task_clock_event_update(event, time);
  }
  
  static int task_clock_event_init(struct perf_event *event)
6fb2915df   Li Zefan   tracing/profile: ...
4892
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
4893
4894
4895
4896
4897
4898
4899
  	if (event->attr.type != PERF_TYPE_SOFTWARE)
  		return -ENOENT;
  
  	if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  		return -ENOENT;
  
  	return 0;
6fb2915df   Li Zefan   tracing/profile: ...
4900
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
4901
  static struct pmu perf_task_clock = {
89a1e1873   Peter Zijlstra   perf: Provide a s...
4902
  	.task_ctx_nr	= perf_sw_context,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4903
  	.event_init	= task_clock_event_init,
a4eaf7f14   Peter Zijlstra   perf: Rework the ...
4904
4905
4906
4907
  	.add		= task_clock_event_add,
  	.del		= task_clock_event_del,
  	.start		= task_clock_event_start,
  	.stop		= task_clock_event_stop,
b0a873ebb   Peter Zijlstra   perf: Register PM...
4908
4909
  	.read		= task_clock_event_read,
  };
6fb2915df   Li Zefan   tracing/profile: ...
4910

ad5133b70   Peter Zijlstra   perf: Default PMU...
4911
  static void perf_pmu_nop_void(struct pmu *pmu)
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4912
  {
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4913
  }
6fb2915df   Li Zefan   tracing/profile: ...
4914

ad5133b70   Peter Zijlstra   perf: Default PMU...
4915
  static int perf_pmu_nop_int(struct pmu *pmu)
6fb2915df   Li Zefan   tracing/profile: ...
4916
  {
ad5133b70   Peter Zijlstra   perf: Default PMU...
4917
  	return 0;
6fb2915df   Li Zefan   tracing/profile: ...
4918
  }
ad5133b70   Peter Zijlstra   perf: Default PMU...
4919
  static void perf_pmu_start_txn(struct pmu *pmu)
6fb2915df   Li Zefan   tracing/profile: ...
4920
  {
ad5133b70   Peter Zijlstra   perf: Default PMU...
4921
  	perf_pmu_disable(pmu);
6fb2915df   Li Zefan   tracing/profile: ...
4922
  }
ad5133b70   Peter Zijlstra   perf: Default PMU...
4923
4924
4925
4926
4927
  static int perf_pmu_commit_txn(struct pmu *pmu)
  {
  	perf_pmu_enable(pmu);
  	return 0;
  }
e077df4f4   Peter Zijlstra   perf_counter: hoo...
4928

ad5133b70   Peter Zijlstra   perf: Default PMU...
4929
  static void perf_pmu_cancel_txn(struct pmu *pmu)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4930
  {
ad5133b70   Peter Zijlstra   perf: Default PMU...
4931
  	perf_pmu_enable(pmu);
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4932
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4933
4934
4935
4936
4937
  /*
   * Ensures all contexts with the same task_ctx_nr have the same
   * pmu_cpu_context too.
   */
  static void *find_pmu_context(int ctxn)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4938
  {
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4939
  	struct pmu *pmu;
b326e9560   Frederic Weisbecker   hw-breakpoints: U...
4940

8dc85d547   Peter Zijlstra   perf: Multiple ta...
4941
4942
  	if (ctxn < 0)
  		return NULL;
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4943

8dc85d547   Peter Zijlstra   perf: Multiple ta...
4944
4945
4946
4947
  	list_for_each_entry(pmu, &pmus, entry) {
  		if (pmu->task_ctx_nr == ctxn)
  			return pmu->pmu_cpu_context;
  	}
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4948

8dc85d547   Peter Zijlstra   perf: Multiple ta...
4949
  	return NULL;
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4950
  }
516769575   Peter Zijlstra   perf: Fix duplica...
4951
  static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4952
  {
516769575   Peter Zijlstra   perf: Fix duplica...
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
  	int cpu;
  
  	for_each_possible_cpu(cpu) {
  		struct perf_cpu_context *cpuctx;
  
  		cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  
  		if (cpuctx->active_pmu == old_pmu)
  			cpuctx->active_pmu = pmu;
  	}
  }
  
  static void free_pmu_context(struct pmu *pmu)
  {
  	struct pmu *i;
f5ffe02e5   Frederic Weisbecker   perf: Add kernel ...
4968

8dc85d547   Peter Zijlstra   perf: Multiple ta...
4969
  	mutex_lock(&pmus_lock);
0475f9ea8   Paul Mackerras   perf_counters: al...
4970
  	/*
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4971
  	 * Like a real lame refcount.
0475f9ea8   Paul Mackerras   perf_counters: al...
4972
  	 */
516769575   Peter Zijlstra   perf: Fix duplica...
4973
4974
4975
  	list_for_each_entry(i, &pmus, entry) {
  		if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  			update_pmu_context(i, pmu);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4976
  			goto out;
516769575   Peter Zijlstra   perf: Fix duplica...
4977
  		}
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4978
  	}
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4979

516769575   Peter Zijlstra   perf: Fix duplica...
4980
  	free_percpu(pmu->pmu_cpu_context);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
4981
4982
  out:
  	mutex_unlock(&pmus_lock);
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
4983
  }
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
4984
  static struct idr pmu_idr;
d6d020e99   Peter Zijlstra   perf_counter: hrt...
4985

abe434005   Peter Zijlstra   perf: Sysfs enume...
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
  static ssize_t
  type_show(struct device *dev, struct device_attribute *attr, char *page)
  {
  	struct pmu *pmu = dev_get_drvdata(dev);
  
  	return snprintf(page, PAGE_SIZE-1, "%d
  ", pmu->type);
  }
  
  static struct device_attribute pmu_dev_attrs[] = {
         __ATTR_RO(type),
         __ATTR_NULL,
  };
  
  static int pmu_bus_running;
  static struct bus_type pmu_bus = {
  	.name		= "event_source",
  	.dev_attrs	= pmu_dev_attrs,
  };
  
  static void pmu_dev_release(struct device *dev)
  {
  	kfree(dev);
  }
  
  static int pmu_dev_alloc(struct pmu *pmu)
  {
  	int ret = -ENOMEM;
  
  	pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  	if (!pmu->dev)
  		goto out;
  
  	device_initialize(pmu->dev);
  	ret = dev_set_name(pmu->dev, "%s", pmu->name);
  	if (ret)
  		goto free_dev;
  
  	dev_set_drvdata(pmu->dev, pmu);
  	pmu->dev->bus = &pmu_bus;
  	pmu->dev->release = pmu_dev_release;
  	ret = device_add(pmu->dev);
  	if (ret)
  		goto free_dev;
  
  out:
  	return ret;
  
  free_dev:
  	put_device(pmu->dev);
  	goto out;
  }
547e9fd7d   Peter Zijlstra   perf: Annotate cp...
5038
  static struct lock_class_key cpuctx_mutex;
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
5039
  int perf_pmu_register(struct pmu *pmu, char *name, int type)
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
5040
  {
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5041
  	int cpu, ret;
24f1e32c6   Frederic Weisbecker   hw-breakpoints: R...
5042

b0a873ebb   Peter Zijlstra   perf: Register PM...
5043
  	mutex_lock(&pmus_lock);
33696fc0d   Peter Zijlstra   perf: Per PMU dis...
5044
5045
5046
5047
  	ret = -ENOMEM;
  	pmu->pmu_disable_count = alloc_percpu(int);
  	if (!pmu->pmu_disable_count)
  		goto unlock;
f29ac756a   Peter Zijlstra   perf_counter: Opt...
5048

2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
  	pmu->type = -1;
  	if (!name)
  		goto skip_type;
  	pmu->name = name;
  
  	if (type < 0) {
  		int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
  		if (!err)
  			goto free_pdc;
  
  		err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
  		if (err) {
  			ret = err;
  			goto free_pdc;
  		}
  	}
  	pmu->type = type;
abe434005   Peter Zijlstra   perf: Sysfs enume...
5066
5067
5068
5069
5070
  	if (pmu_bus_running) {
  		ret = pmu_dev_alloc(pmu);
  		if (ret)
  			goto free_idr;
  	}
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
5071
  skip_type:
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5072
5073
5074
  	pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  	if (pmu->pmu_cpu_context)
  		goto got_cpu_context;
f29ac756a   Peter Zijlstra   perf_counter: Opt...
5075

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5076
5077
  	pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  	if (!pmu->pmu_cpu_context)
abe434005   Peter Zijlstra   perf: Sysfs enume...
5078
  		goto free_dev;
f344011cc   Peter Zijlstra   perf_counter: Opt...
5079

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5080
5081
5082
5083
  	for_each_possible_cpu(cpu) {
  		struct perf_cpu_context *cpuctx;
  
  		cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
eb1844798   Peter Zijlstra   perf: Clean up pe...
5084
  		__perf_event_init_context(&cpuctx->ctx);
547e9fd7d   Peter Zijlstra   perf: Annotate cp...
5085
  		lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
b04243ef7   Peter Zijlstra   perf: Complete so...
5086
  		cpuctx->ctx.type = cpu_context;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5087
  		cpuctx->ctx.pmu = pmu;
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
5088
5089
  		cpuctx->jiffies_interval = 1;
  		INIT_LIST_HEAD(&cpuctx->rotation_list);
516769575   Peter Zijlstra   perf: Fix duplica...
5090
  		cpuctx->active_pmu = pmu;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5091
  	}
76e1d9047   Frederic Weisbecker   perf: Store activ...
5092

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5093
  got_cpu_context:
ad5133b70   Peter Zijlstra   perf: Default PMU...
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
  	if (!pmu->start_txn) {
  		if (pmu->pmu_enable) {
  			/*
  			 * If we have pmu_enable/pmu_disable calls, install
  			 * transaction stubs that use that to try and batch
  			 * hardware accesses.
  			 */
  			pmu->start_txn  = perf_pmu_start_txn;
  			pmu->commit_txn = perf_pmu_commit_txn;
  			pmu->cancel_txn = perf_pmu_cancel_txn;
  		} else {
  			pmu->start_txn  = perf_pmu_nop_void;
  			pmu->commit_txn = perf_pmu_nop_int;
  			pmu->cancel_txn = perf_pmu_nop_void;
f344011cc   Peter Zijlstra   perf_counter: Opt...
5108
  		}
5c92d1241   Ingo Molnar   perf counters: im...
5109
  	}
15dbf27cc   Peter Zijlstra   perf_counter: sof...
5110

ad5133b70   Peter Zijlstra   perf: Default PMU...
5111
5112
5113
5114
  	if (!pmu->pmu_enable) {
  		pmu->pmu_enable  = perf_pmu_nop_void;
  		pmu->pmu_disable = perf_pmu_nop_void;
  	}
b0a873ebb   Peter Zijlstra   perf: Register PM...
5115
  	list_add_rcu(&pmu->entry, &pmus);
33696fc0d   Peter Zijlstra   perf: Per PMU dis...
5116
5117
  	ret = 0;
  unlock:
b0a873ebb   Peter Zijlstra   perf: Register PM...
5118
  	mutex_unlock(&pmus_lock);
33696fc0d   Peter Zijlstra   perf: Per PMU dis...
5119
  	return ret;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5120

abe434005   Peter Zijlstra   perf: Sysfs enume...
5121
5122
5123
  free_dev:
  	device_del(pmu->dev);
  	put_device(pmu->dev);
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
5124
5125
5126
  free_idr:
  	if (pmu->type >= PERF_TYPE_MAX)
  		idr_remove(&pmu_idr, pmu->type);
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
5127
5128
5129
  free_pdc:
  	free_percpu(pmu->pmu_disable_count);
  	goto unlock;
f29ac756a   Peter Zijlstra   perf_counter: Opt...
5130
  }
b0a873ebb   Peter Zijlstra   perf: Register PM...
5131
  void perf_pmu_unregister(struct pmu *pmu)
5c92d1241   Ingo Molnar   perf counters: im...
5132
  {
b0a873ebb   Peter Zijlstra   perf: Register PM...
5133
5134
5135
  	mutex_lock(&pmus_lock);
  	list_del_rcu(&pmu->entry);
  	mutex_unlock(&pmus_lock);
5c92d1241   Ingo Molnar   perf counters: im...
5136

0475f9ea8   Paul Mackerras   perf_counters: al...
5137
  	/*
cde8e8849   Peter Zijlstra   perf: Sanitize th...
5138
5139
  	 * We dereference the pmu list under both SRCU and regular RCU, so
  	 * synchronize against both of those.
0475f9ea8   Paul Mackerras   perf_counters: al...
5140
  	 */
b0a873ebb   Peter Zijlstra   perf: Register PM...
5141
  	synchronize_srcu(&pmus_srcu);
cde8e8849   Peter Zijlstra   perf: Sanitize th...
5142
  	synchronize_rcu();
d6d020e99   Peter Zijlstra   perf_counter: hrt...
5143

33696fc0d   Peter Zijlstra   perf: Per PMU dis...
5144
  	free_percpu(pmu->pmu_disable_count);
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
5145
5146
  	if (pmu->type >= PERF_TYPE_MAX)
  		idr_remove(&pmu_idr, pmu->type);
abe434005   Peter Zijlstra   perf: Sysfs enume...
5147
5148
  	device_del(pmu->dev);
  	put_device(pmu->dev);
516769575   Peter Zijlstra   perf: Fix duplica...
5149
  	free_pmu_context(pmu);
b0a873ebb   Peter Zijlstra   perf: Register PM...
5150
  }
d6d020e99   Peter Zijlstra   perf_counter: hrt...
5151

b0a873ebb   Peter Zijlstra   perf: Register PM...
5152
5153
5154
5155
5156
5157
  struct pmu *perf_init_event(struct perf_event *event)
  {
  	struct pmu *pmu = NULL;
  	int idx;
  
  	idx = srcu_read_lock(&pmus_srcu);
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
5158
5159
5160
5161
5162
5163
  
  	rcu_read_lock();
  	pmu = idr_find(&pmu_idr, event->attr.type);
  	rcu_read_unlock();
  	if (pmu)
  		goto unlock;
b0a873ebb   Peter Zijlstra   perf: Register PM...
5164
5165
5166
  	list_for_each_entry_rcu(pmu, &pmus, entry) {
  		int ret = pmu->event_init(event);
  		if (!ret)
e5f4d3394   Peter Zijlstra   perf: Fix perf_in...
5167
  			goto unlock;
76e1d9047   Frederic Weisbecker   perf: Store activ...
5168

b0a873ebb   Peter Zijlstra   perf: Register PM...
5169
5170
  		if (ret != -ENOENT) {
  			pmu = ERR_PTR(ret);
e5f4d3394   Peter Zijlstra   perf: Fix perf_in...
5171
  			goto unlock;
f344011cc   Peter Zijlstra   perf_counter: Opt...
5172
  		}
5c92d1241   Ingo Molnar   perf counters: im...
5173
  	}
e5f4d3394   Peter Zijlstra   perf: Fix perf_in...
5174
5175
  	pmu = ERR_PTR(-ENOENT);
  unlock:
b0a873ebb   Peter Zijlstra   perf: Register PM...
5176
  	srcu_read_unlock(&pmus_srcu, idx);
15dbf27cc   Peter Zijlstra   perf_counter: sof...
5177

4aeb0b423   Robert Richter   perfcounters: ren...
5178
  	return pmu;
5c92d1241   Ingo Molnar   perf counters: im...
5179
  }
0793a61d4   Thomas Gleixner   performance count...
5180
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5181
   * Allocate and initialize a event structure
0793a61d4   Thomas Gleixner   performance count...
5182
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5183
  static struct perf_event *
c3f00c702   Peter Zijlstra   perf: Separate fi...
5184
  perf_event_alloc(struct perf_event_attr *attr, int cpu,
d580ff869   Peter Zijlstra   perf, hw_breakpoi...
5185
5186
5187
5188
  		 struct task_struct *task,
  		 struct perf_event *group_leader,
  		 struct perf_event *parent_event,
  		 perf_overflow_handler_t overflow_handler)
0793a61d4   Thomas Gleixner   performance count...
5189
  {
51b0fe395   Peter Zijlstra   perf: Deconstify ...
5190
  	struct pmu *pmu;
cdd6c482c   Ingo Molnar   perf: Do the big ...
5191
5192
  	struct perf_event *event;
  	struct hw_perf_event *hwc;
d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5193
  	long err;
0793a61d4   Thomas Gleixner   performance count...
5194

66832eb4b   Oleg Nesterov   perf: Validate cp...
5195
5196
5197
5198
  	if ((unsigned)cpu >= nr_cpu_ids) {
  		if (!task || cpu != -1)
  			return ERR_PTR(-EINVAL);
  	}
c3f00c702   Peter Zijlstra   perf: Separate fi...
5199
  	event = kzalloc(sizeof(*event), GFP_KERNEL);
cdd6c482c   Ingo Molnar   perf: Do the big ...
5200
  	if (!event)
d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5201
  		return ERR_PTR(-ENOMEM);
0793a61d4   Thomas Gleixner   performance count...
5202

04289bb98   Ingo Molnar   perf counters: ad...
5203
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5204
  	 * Single events are their own group leaders, with an
04289bb98   Ingo Molnar   perf counters: ad...
5205
5206
5207
  	 * empty sibling list:
  	 */
  	if (!group_leader)
cdd6c482c   Ingo Molnar   perf: Do the big ...
5208
  		group_leader = event;
04289bb98   Ingo Molnar   perf counters: ad...
5209

cdd6c482c   Ingo Molnar   perf: Do the big ...
5210
5211
  	mutex_init(&event->child_mutex);
  	INIT_LIST_HEAD(&event->child_list);
fccc714b3   Peter Zijlstra   perf_counter: San...
5212

cdd6c482c   Ingo Molnar   perf: Do the big ...
5213
5214
5215
5216
  	INIT_LIST_HEAD(&event->group_entry);
  	INIT_LIST_HEAD(&event->event_entry);
  	INIT_LIST_HEAD(&event->sibling_list);
  	init_waitqueue_head(&event->waitq);
e360adbe2   Peter Zijlstra   irq_work: Add gen...
5217
  	init_irq_work(&event->pending, perf_pending_event);
0793a61d4   Thomas Gleixner   performance count...
5218

cdd6c482c   Ingo Molnar   perf: Do the big ...
5219
  	mutex_init(&event->mmap_mutex);
7b732a750   Peter Zijlstra   perf_counter: new...
5220

cdd6c482c   Ingo Molnar   perf: Do the big ...
5221
5222
5223
5224
  	event->cpu		= cpu;
  	event->attr		= *attr;
  	event->group_leader	= group_leader;
  	event->pmu		= NULL;
cdd6c482c   Ingo Molnar   perf: Do the big ...
5225
  	event->oncpu		= -1;
a96bbc164   Peter Zijlstra   perf_counter: Fix...
5226

cdd6c482c   Ingo Molnar   perf: Do the big ...
5227
  	event->parent		= parent_event;
b84fbc9fb   Peter Zijlstra   perf_counter: Pus...
5228

cdd6c482c   Ingo Molnar   perf: Do the big ...
5229
5230
  	event->ns		= get_pid_ns(current->nsproxy->pid_ns);
  	event->id		= atomic64_inc_return(&perf_event_id);
a96bbc164   Peter Zijlstra   perf_counter: Fix...
5231

cdd6c482c   Ingo Molnar   perf: Do the big ...
5232
  	event->state		= PERF_EVENT_STATE_INACTIVE;
329d876d6   Ingo Molnar   perf_counter: Ini...
5233

d580ff869   Peter Zijlstra   perf, hw_breakpoi...
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
  	if (task) {
  		event->attach_state = PERF_ATTACH_TASK;
  #ifdef CONFIG_HAVE_HW_BREAKPOINT
  		/*
  		 * hw_breakpoint is a bit difficult here..
  		 */
  		if (attr->type == PERF_TYPE_BREAKPOINT)
  			event->hw.bp_target = task;
  #endif
  	}
b326e9560   Frederic Weisbecker   hw-breakpoints: U...
5244
5245
  	if (!overflow_handler && parent_event)
  		overflow_handler = parent_event->overflow_handler;
66832eb4b   Oleg Nesterov   perf: Validate cp...
5246

b326e9560   Frederic Weisbecker   hw-breakpoints: U...
5247
  	event->overflow_handler	= overflow_handler;
97eaf5300   Frederic Weisbecker   perf/core: Add a ...
5248

0d48696f8   Peter Zijlstra   perf_counter: Ren...
5249
  	if (attr->disabled)
cdd6c482c   Ingo Molnar   perf: Do the big ...
5250
  		event->state = PERF_EVENT_STATE_OFF;
a86ed5085   Ingo Molnar   perfcounters: use...
5251

4aeb0b423   Robert Richter   perfcounters: ren...
5252
  	pmu = NULL;
b8e83514b   Peter Zijlstra   perf_counter: rev...
5253

cdd6c482c   Ingo Molnar   perf: Do the big ...
5254
  	hwc = &event->hw;
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
5255
  	hwc->sample_period = attr->sample_period;
0d48696f8   Peter Zijlstra   perf_counter: Ren...
5256
  	if (attr->freq && attr->sample_freq)
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
5257
  		hwc->sample_period = 1;
eced1dfcf   Peter Zijlstra   perf_counter: Fix...
5258
  	hwc->last_period = hwc->sample_period;
bd2b5b128   Peter Zijlstra   perf_counter: Mor...
5259

e78505958   Peter Zijlstra   perf: Convert per...
5260
  	local64_set(&hwc->period_left, hwc->sample_period);
60db5e09c   Peter Zijlstra   perf_counter: fre...
5261

2023b3592   Peter Zijlstra   perf_counter: inh...
5262
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5263
  	 * we currently do not support PERF_FORMAT_GROUP on inherited events
2023b3592   Peter Zijlstra   perf_counter: inh...
5264
  	 */
3dab77fb1   Peter Zijlstra   perf: Rework/fix ...
5265
  	if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
2023b3592   Peter Zijlstra   perf_counter: inh...
5266
  		goto done;
b0a873ebb   Peter Zijlstra   perf: Register PM...
5267
  	pmu = perf_init_event(event);
974802eaa   Peter Zijlstra   perf_counter: Add...
5268

d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5269
5270
  done:
  	err = 0;
4aeb0b423   Robert Richter   perfcounters: ren...
5271
  	if (!pmu)
d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5272
  		err = -EINVAL;
4aeb0b423   Robert Richter   perfcounters: ren...
5273
5274
  	else if (IS_ERR(pmu))
  		err = PTR_ERR(pmu);
5c92d1241   Ingo Molnar   perf counters: im...
5275

d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5276
  	if (err) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
5277
5278
5279
  		if (event->ns)
  			put_pid_ns(event->ns);
  		kfree(event);
d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5280
  		return ERR_PTR(err);
621a01eac   Ingo Molnar   perf counters: hw...
5281
  	}
d5d2bc0dd   Paul Mackerras   perf_counter: mak...
5282

cdd6c482c   Ingo Molnar   perf: Do the big ...
5283
  	event->pmu = pmu;
0793a61d4   Thomas Gleixner   performance count...
5284

cdd6c482c   Ingo Molnar   perf: Do the big ...
5285
  	if (!event->parent) {
82cd6def9   Peter Zijlstra   perf: Use jump_la...
5286
5287
  		if (event->attach_state & PERF_ATTACH_TASK)
  			jump_label_inc(&perf_task_events);
3af9e8592   Eric B Munson   perf: Add non-exe...
5288
  		if (event->attr.mmap || event->attr.mmap_data)
cdd6c482c   Ingo Molnar   perf: Do the big ...
5289
5290
5291
5292
5293
  			atomic_inc(&nr_mmap_events);
  		if (event->attr.comm)
  			atomic_inc(&nr_comm_events);
  		if (event->attr.task)
  			atomic_inc(&nr_task_events);
927c7a9e9   Frederic Weisbecker   perf: Fix race in...
5294
5295
5296
5297
5298
5299
5300
  		if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  			err = get_callchain_buffers();
  			if (err) {
  				free_event(event);
  				return ERR_PTR(err);
  			}
  		}
f344011cc   Peter Zijlstra   perf_counter: Opt...
5301
  	}
9ee318a78   Peter Zijlstra   perf_counter: opt...
5302

cdd6c482c   Ingo Molnar   perf: Do the big ...
5303
  	return event;
0793a61d4   Thomas Gleixner   performance count...
5304
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
5305
5306
  static int perf_copy_attr(struct perf_event_attr __user *uattr,
  			  struct perf_event_attr *attr)
974802eaa   Peter Zijlstra   perf_counter: Add...
5307
  {
974802eaa   Peter Zijlstra   perf_counter: Add...
5308
  	u32 size;
cdf8073d6   Ian Schram   perf_counter: Fix...
5309
  	int ret;
974802eaa   Peter Zijlstra   perf_counter: Add...
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
  
  	if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  		return -EFAULT;
  
  	/*
  	 * zero the full structure, so that a short copy will be nice.
  	 */
  	memset(attr, 0, sizeof(*attr));
  
  	ret = get_user(size, &uattr->size);
  	if (ret)
  		return ret;
  
  	if (size > PAGE_SIZE)	/* silly large */
  		goto err_size;
  
  	if (!size)		/* abi compat */
  		size = PERF_ATTR_SIZE_VER0;
  
  	if (size < PERF_ATTR_SIZE_VER0)
  		goto err_size;
  
  	/*
  	 * If we're handed a bigger struct than we know of,
cdf8073d6   Ian Schram   perf_counter: Fix...
5334
5335
5336
  	 * ensure all the unknown bits are 0 - i.e. new
  	 * user-space does not rely on any kernel feature
  	 * extensions we dont know about yet.
974802eaa   Peter Zijlstra   perf_counter: Add...
5337
5338
  	 */
  	if (size > sizeof(*attr)) {
cdf8073d6   Ian Schram   perf_counter: Fix...
5339
5340
5341
  		unsigned char __user *addr;
  		unsigned char __user *end;
  		unsigned char val;
974802eaa   Peter Zijlstra   perf_counter: Add...
5342

cdf8073d6   Ian Schram   perf_counter: Fix...
5343
5344
  		addr = (void __user *)uattr + sizeof(*attr);
  		end  = (void __user *)uattr + size;
974802eaa   Peter Zijlstra   perf_counter: Add...
5345

cdf8073d6   Ian Schram   perf_counter: Fix...
5346
  		for (; addr < end; addr++) {
974802eaa   Peter Zijlstra   perf_counter: Add...
5347
5348
5349
5350
5351
5352
  			ret = get_user(val, addr);
  			if (ret)
  				return ret;
  			if (val)
  				goto err_size;
  		}
b3e62e350   Xiao Guangrong   perf_counter: Fix...
5353
  		size = sizeof(*attr);
974802eaa   Peter Zijlstra   perf_counter: Add...
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
  	}
  
  	ret = copy_from_user(attr, uattr, size);
  	if (ret)
  		return -EFAULT;
  
  	/*
  	 * If the type exists, the corresponding creation will verify
  	 * the attr->config.
  	 */
  	if (attr->type >= PERF_TYPE_MAX)
  		return -EINVAL;
cd757645f   Mahesh Salgaonkar   perf: Make bp_len...
5366
  	if (attr->__reserved_1)
974802eaa   Peter Zijlstra   perf_counter: Add...
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
  		return -EINVAL;
  
  	if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  		return -EINVAL;
  
  	if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  		return -EINVAL;
  
  out:
  	return ret;
  
  err_size:
  	put_user(sizeof(*attr), &uattr->size);
  	ret = -E2BIG;
  	goto out;
  }
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5383
5384
  static int
  perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
a4be7c277   Peter Zijlstra   perf_counter: All...
5385
  {
ca5135e6b   Peter Zijlstra   perf: Rename perf...
5386
  	struct perf_buffer *buffer = NULL, *old_buffer = NULL;
a4be7c277   Peter Zijlstra   perf_counter: All...
5387
  	int ret = -EINVAL;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5388
  	if (!output_event)
a4be7c277   Peter Zijlstra   perf_counter: All...
5389
  		goto set;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5390
5391
  	/* don't allow circular references */
  	if (event == output_event)
a4be7c277   Peter Zijlstra   perf_counter: All...
5392
  		goto out;
0f139300c   Peter Zijlstra   perf: Ensure that...
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
  	/*
  	 * Don't allow cross-cpu buffers
  	 */
  	if (output_event->cpu != event->cpu)
  		goto out;
  
  	/*
  	 * If its not a per-cpu buffer, it must be the same task.
  	 */
  	if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  		goto out;
a4be7c277   Peter Zijlstra   perf_counter: All...
5404
  set:
cdd6c482c   Ingo Molnar   perf: Do the big ...
5405
  	mutex_lock(&event->mmap_mutex);
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5406
5407
5408
  	/* Can't redirect output if we've got an active mmap() */
  	if (atomic_read(&event->mmap_count))
  		goto unlock;
a4be7c277   Peter Zijlstra   perf_counter: All...
5409

ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5410
5411
  	if (output_event) {
  		/* get the buffer we want to redirect to */
ca5135e6b   Peter Zijlstra   perf: Rename perf...
5412
5413
  		buffer = perf_buffer_get(output_event);
  		if (!buffer)
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5414
  			goto unlock;
a4be7c277   Peter Zijlstra   perf_counter: All...
5415
  	}
ca5135e6b   Peter Zijlstra   perf: Rename perf...
5416
5417
  	old_buffer = event->buffer;
  	rcu_assign_pointer(event->buffer, buffer);
a4be7c277   Peter Zijlstra   perf_counter: All...
5418
  	ret = 0;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5419
5420
  unlock:
  	mutex_unlock(&event->mmap_mutex);
ca5135e6b   Peter Zijlstra   perf: Rename perf...
5421
5422
  	if (old_buffer)
  		perf_buffer_put(old_buffer);
a4be7c277   Peter Zijlstra   perf_counter: All...
5423
  out:
a4be7c277   Peter Zijlstra   perf_counter: All...
5424
5425
  	return ret;
  }
0793a61d4   Thomas Gleixner   performance count...
5426
  /**
cdd6c482c   Ingo Molnar   perf: Do the big ...
5427
   * sys_perf_event_open - open a performance event, associate it to a task/cpu
9f66a3810   Ingo Molnar   perf counters: re...
5428
   *
cdd6c482c   Ingo Molnar   perf: Do the big ...
5429
   * @attr_uptr:	event_id type attributes for monitoring/sampling
0793a61d4   Thomas Gleixner   performance count...
5430
   * @pid:		target pid
9f66a3810   Ingo Molnar   perf counters: re...
5431
   * @cpu:		target cpu
cdd6c482c   Ingo Molnar   perf: Do the big ...
5432
   * @group_fd:		group leader event fd
0793a61d4   Thomas Gleixner   performance count...
5433
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5434
5435
  SYSCALL_DEFINE5(perf_event_open,
  		struct perf_event_attr __user *, attr_uptr,
2743a5b0f   Paul Mackerras   perfcounters: pro...
5436
  		pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
0793a61d4   Thomas Gleixner   performance count...
5437
  {
b04243ef7   Peter Zijlstra   perf: Complete so...
5438
5439
  	struct perf_event *group_leader = NULL, *output_event = NULL;
  	struct perf_event *event, *sibling;
cdd6c482c   Ingo Molnar   perf: Do the big ...
5440
5441
5442
  	struct perf_event_attr attr;
  	struct perf_event_context *ctx;
  	struct file *event_file = NULL;
04289bb98   Ingo Molnar   perf counters: ad...
5443
  	struct file *group_file = NULL;
38a81da22   Matt Helsley   perf events: Clea...
5444
  	struct task_struct *task = NULL;
89a1e1873   Peter Zijlstra   perf: Provide a s...
5445
  	struct pmu *pmu;
ea635c64e   Al Viro   Fix racy use of a...
5446
  	int event_fd;
b04243ef7   Peter Zijlstra   perf: Complete so...
5447
  	int move_group = 0;
04289bb98   Ingo Molnar   perf counters: ad...
5448
  	int fput_needed = 0;
dc86cabe4   Ingo Molnar   perf_counter: Fix...
5449
  	int err;
0793a61d4   Thomas Gleixner   performance count...
5450

2743a5b0f   Paul Mackerras   perfcounters: pro...
5451
  	/* for future expandability... */
a4be7c277   Peter Zijlstra   perf_counter: All...
5452
  	if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
2743a5b0f   Paul Mackerras   perfcounters: pro...
5453
  		return -EINVAL;
dc86cabe4   Ingo Molnar   perf_counter: Fix...
5454
5455
5456
  	err = perf_copy_attr(attr_uptr, &attr);
  	if (err)
  		return err;
eab656ae0   Thomas Gleixner   perf counters: cl...
5457

0764771da   Peter Zijlstra   perf_counter: Mor...
5458
5459
5460
5461
  	if (!attr.exclude_kernel) {
  		if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  			return -EACCES;
  	}
df58ab24b   Peter Zijlstra   perf_counter: Ren...
5462
  	if (attr.freq) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
5463
  		if (attr.sample_freq > sysctl_perf_event_sample_rate)
df58ab24b   Peter Zijlstra   perf_counter: Ren...
5464
5465
  			return -EINVAL;
  	}
ea635c64e   Al Viro   Fix racy use of a...
5466
5467
5468
  	event_fd = get_unused_fd_flags(O_RDWR);
  	if (event_fd < 0)
  		return event_fd;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5469
5470
5471
5472
  	if (group_fd != -1) {
  		group_leader = perf_fget_light(group_fd, &fput_needed);
  		if (IS_ERR(group_leader)) {
  			err = PTR_ERR(group_leader);
d14b12d7a   Stephane Eranian   perf_events: Fix ...
5473
  			goto err_fd;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5474
5475
5476
5477
5478
5479
5480
  		}
  		group_file = group_leader->filp;
  		if (flags & PERF_FLAG_FD_OUTPUT)
  			output_event = group_leader;
  		if (flags & PERF_FLAG_FD_NO_GROUP)
  			group_leader = NULL;
  	}
c6be5a5cb   Peter Zijlstra   perf: Find task b...
5481
5482
5483
5484
5485
5486
5487
  	if (pid != -1) {
  		task = find_lively_task_by_vpid(pid);
  		if (IS_ERR(task)) {
  			err = PTR_ERR(task);
  			goto err_group_fd;
  		}
  	}
d580ff869   Peter Zijlstra   perf, hw_breakpoi...
5488
  	event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
d14b12d7a   Stephane Eranian   perf_events: Fix ...
5489
5490
  	if (IS_ERR(event)) {
  		err = PTR_ERR(event);
c6be5a5cb   Peter Zijlstra   perf: Find task b...
5491
  		goto err_task;
d14b12d7a   Stephane Eranian   perf_events: Fix ...
5492
  	}
ccff286d8   Ingo Molnar   perf counters: gr...
5493
  	/*
89a1e1873   Peter Zijlstra   perf: Provide a s...
5494
5495
5496
5497
  	 * Special case software events and allow them to be part of
  	 * any hardware group.
  	 */
  	pmu = event->pmu;
b04243ef7   Peter Zijlstra   perf: Complete so...
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
  
  	if (group_leader &&
  	    (is_software_event(event) != is_software_event(group_leader))) {
  		if (is_software_event(event)) {
  			/*
  			 * If event and group_leader are not both a software
  			 * event, and event is, then group leader is not.
  			 *
  			 * Allow the addition of software events to !software
  			 * groups, this is safe because software events never
  			 * fail to schedule.
  			 */
  			pmu = group_leader->pmu;
  		} else if (is_software_event(group_leader) &&
  			   (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  			/*
  			 * In case the group is a pure software group, and we
  			 * try to add a hardware event, move the whole group to
  			 * the hardware context.
  			 */
  			move_group = 1;
  		}
  	}
89a1e1873   Peter Zijlstra   perf: Provide a s...
5521
5522
5523
5524
  
  	/*
  	 * Get the target context (task or percpu):
  	 */
38a81da22   Matt Helsley   perf events: Clea...
5525
  	ctx = find_get_context(pmu, task, cpu);
89a1e1873   Peter Zijlstra   perf: Provide a s...
5526
5527
  	if (IS_ERR(ctx)) {
  		err = PTR_ERR(ctx);
c6be5a5cb   Peter Zijlstra   perf: Find task b...
5528
  		goto err_alloc;
89a1e1873   Peter Zijlstra   perf: Provide a s...
5529
  	}
ccff286d8   Ingo Molnar   perf counters: gr...
5530
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5531
  	 * Look up the group leader (we will attach this event to it):
04289bb98   Ingo Molnar   perf counters: ad...
5532
  	 */
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5533
  	if (group_leader) {
dc86cabe4   Ingo Molnar   perf_counter: Fix...
5534
  		err = -EINVAL;
04289bb98   Ingo Molnar   perf counters: ad...
5535

04289bb98   Ingo Molnar   perf counters: ad...
5536
  		/*
ccff286d8   Ingo Molnar   perf counters: gr...
5537
5538
5539
5540
  		 * Do not allow a recursive hierarchy (this new sibling
  		 * becoming part of another group-sibling):
  		 */
  		if (group_leader->group_leader != group_leader)
c3f00c702   Peter Zijlstra   perf: Separate fi...
5541
  			goto err_context;
ccff286d8   Ingo Molnar   perf counters: gr...
5542
5543
5544
  		/*
  		 * Do not allow to attach to a group in a different
  		 * task or CPU context:
04289bb98   Ingo Molnar   perf counters: ad...
5545
  		 */
b04243ef7   Peter Zijlstra   perf: Complete so...
5546
5547
5548
5549
5550
5551
5552
  		if (move_group) {
  			if (group_leader->ctx->type != ctx->type)
  				goto err_context;
  		} else {
  			if (group_leader->ctx != ctx)
  				goto err_context;
  		}
3b6f9e5cb   Paul Mackerras   perf_counter: Add...
5553
5554
5555
  		/*
  		 * Only a group leader can be exclusive or pinned
  		 */
0d48696f8   Peter Zijlstra   perf_counter: Ren...
5556
  		if (attr.exclusive || attr.pinned)
c3f00c702   Peter Zijlstra   perf: Separate fi...
5557
  			goto err_context;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5558
5559
5560
5561
5562
  	}
  
  	if (output_event) {
  		err = perf_event_set_output(event, output_event);
  		if (err)
c3f00c702   Peter Zijlstra   perf: Separate fi...
5563
  			goto err_context;
ac9721f3f   Peter Zijlstra   perf_events: Fix ...
5564
  	}
0793a61d4   Thomas Gleixner   performance count...
5565

ea635c64e   Al Viro   Fix racy use of a...
5566
5567
5568
  	event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  	if (IS_ERR(event_file)) {
  		err = PTR_ERR(event_file);
c3f00c702   Peter Zijlstra   perf: Separate fi...
5569
  		goto err_context;
ea635c64e   Al Viro   Fix racy use of a...
5570
  	}
9b51f66dc   Ingo Molnar   perfcounters: imp...
5571

b04243ef7   Peter Zijlstra   perf: Complete so...
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
  	if (move_group) {
  		struct perf_event_context *gctx = group_leader->ctx;
  
  		mutex_lock(&gctx->mutex);
  		perf_event_remove_from_context(group_leader);
  		list_for_each_entry(sibling, &group_leader->sibling_list,
  				    group_entry) {
  			perf_event_remove_from_context(sibling);
  			put_ctx(gctx);
  		}
  		mutex_unlock(&gctx->mutex);
  		put_ctx(gctx);
ea635c64e   Al Viro   Fix racy use of a...
5584
  	}
9b51f66dc   Ingo Molnar   perfcounters: imp...
5585

cdd6c482c   Ingo Molnar   perf: Do the big ...
5586
  	event->filp = event_file;
ad3a37de8   Paul Mackerras   perf_counter: Don...
5587
  	WARN_ON_ONCE(ctx->parent_ctx);
d859e29fe   Paul Mackerras   perf_counter: Add...
5588
  	mutex_lock(&ctx->mutex);
b04243ef7   Peter Zijlstra   perf: Complete so...
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
  
  	if (move_group) {
  		perf_install_in_context(ctx, group_leader, cpu);
  		get_ctx(ctx);
  		list_for_each_entry(sibling, &group_leader->sibling_list,
  				    group_entry) {
  			perf_install_in_context(ctx, sibling, cpu);
  			get_ctx(ctx);
  		}
  	}
cdd6c482c   Ingo Molnar   perf: Do the big ...
5599
  	perf_install_in_context(ctx, event, cpu);
ad3a37de8   Paul Mackerras   perf_counter: Don...
5600
  	++ctx->generation;
d859e29fe   Paul Mackerras   perf_counter: Add...
5601
  	mutex_unlock(&ctx->mutex);
9b51f66dc   Ingo Molnar   perfcounters: imp...
5602

cdd6c482c   Ingo Molnar   perf: Do the big ...
5603
  	event->owner = current;
8882135bc   Peter Zijlstra   perf: Fix owner-l...
5604

cdd6c482c   Ingo Molnar   perf: Do the big ...
5605
5606
5607
  	mutex_lock(&current->perf_event_mutex);
  	list_add_tail(&event->owner_entry, &current->perf_event_list);
  	mutex_unlock(&current->perf_event_mutex);
082ff5a27   Peter Zijlstra   perf_counter: Cha...
5608

8a49542c0   Peter Zijlstra   perf_events: Fix ...
5609
  	/*
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
5610
5611
5612
  	 * Precalculate sample_data sizes
  	 */
  	perf_event__header_size(event);
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
5613
  	perf_event__id_header_size(event);
c320c7b7d   Arnaldo Carvalho de Melo   perf events: Prec...
5614
5615
  
  	/*
8a49542c0   Peter Zijlstra   perf_events: Fix ...
5616
5617
5618
5619
5620
  	 * Drop the reference on the group_event after placing the
  	 * new event on the sibling_list. This ensures destruction
  	 * of the group leader will find the pointer to itself in
  	 * perf_group_detach().
  	 */
ea635c64e   Al Viro   Fix racy use of a...
5621
5622
5623
  	fput_light(group_file, fput_needed);
  	fd_install(event_fd, event_file);
  	return event_fd;
0793a61d4   Thomas Gleixner   performance count...
5624

c3f00c702   Peter Zijlstra   perf: Separate fi...
5625
  err_context:
ea635c64e   Al Viro   Fix racy use of a...
5626
  	put_ctx(ctx);
c6be5a5cb   Peter Zijlstra   perf: Find task b...
5627
  err_alloc:
ea635c64e   Al Viro   Fix racy use of a...
5628
  	free_event(event);
e7d0bc047   Peter Zijlstra   perf: Fix task re...
5629
5630
5631
  err_task:
  	if (task)
  		put_task_struct(task);
89a1e1873   Peter Zijlstra   perf: Provide a s...
5632
  err_group_fd:
dc86cabe4   Ingo Molnar   perf_counter: Fix...
5633
  	fput_light(group_file, fput_needed);
ea635c64e   Al Viro   Fix racy use of a...
5634
5635
  err_fd:
  	put_unused_fd(event_fd);
dc86cabe4   Ingo Molnar   perf_counter: Fix...
5636
  	return err;
0793a61d4   Thomas Gleixner   performance count...
5637
  }
fb0459d75   Arjan van de Ven   perf/core: Provid...
5638
5639
5640
5641
5642
  /**
   * perf_event_create_kernel_counter
   *
   * @attr: attributes of the counter to create
   * @cpu: cpu in which the counter is bound
38a81da22   Matt Helsley   perf events: Clea...
5643
   * @task: task to profile (NULL for percpu)
fb0459d75   Arjan van de Ven   perf/core: Provid...
5644
5645
5646
   */
  struct perf_event *
  perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
38a81da22   Matt Helsley   perf events: Clea...
5647
  				 struct task_struct *task,
b326e9560   Frederic Weisbecker   hw-breakpoints: U...
5648
  				 perf_overflow_handler_t overflow_handler)
fb0459d75   Arjan van de Ven   perf/core: Provid...
5649
  {
fb0459d75   Arjan van de Ven   perf/core: Provid...
5650
  	struct perf_event_context *ctx;
c3f00c702   Peter Zijlstra   perf: Separate fi...
5651
  	struct perf_event *event;
fb0459d75   Arjan van de Ven   perf/core: Provid...
5652
  	int err;
d859e29fe   Paul Mackerras   perf_counter: Add...
5653

fb0459d75   Arjan van de Ven   perf/core: Provid...
5654
5655
5656
  	/*
  	 * Get the target context (task or percpu):
  	 */
d859e29fe   Paul Mackerras   perf_counter: Add...
5657

d580ff869   Peter Zijlstra   perf, hw_breakpoi...
5658
  	event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
c3f00c702   Peter Zijlstra   perf: Separate fi...
5659
5660
5661
5662
  	if (IS_ERR(event)) {
  		err = PTR_ERR(event);
  		goto err;
  	}
d859e29fe   Paul Mackerras   perf_counter: Add...
5663

38a81da22   Matt Helsley   perf events: Clea...
5664
  	ctx = find_get_context(event->pmu, task, cpu);
c6567f642   Frederic Weisbecker   hw-breakpoints: I...
5665
5666
  	if (IS_ERR(ctx)) {
  		err = PTR_ERR(ctx);
c3f00c702   Peter Zijlstra   perf: Separate fi...
5667
  		goto err_free;
d859e29fe   Paul Mackerras   perf_counter: Add...
5668
  	}
fb0459d75   Arjan van de Ven   perf/core: Provid...
5669
5670
5671
5672
5673
5674
5675
  
  	event->filp = NULL;
  	WARN_ON_ONCE(ctx->parent_ctx);
  	mutex_lock(&ctx->mutex);
  	perf_install_in_context(ctx, event, cpu);
  	++ctx->generation;
  	mutex_unlock(&ctx->mutex);
fb0459d75   Arjan van de Ven   perf/core: Provid...
5676
  	return event;
c3f00c702   Peter Zijlstra   perf: Separate fi...
5677
5678
5679
  err_free:
  	free_event(event);
  err:
c6567f642   Frederic Weisbecker   hw-breakpoints: I...
5680
  	return ERR_PTR(err);
9b51f66dc   Ingo Molnar   perfcounters: imp...
5681
  }
fb0459d75   Arjan van de Ven   perf/core: Provid...
5682
  EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9b51f66dc   Ingo Molnar   perfcounters: imp...
5683

cdd6c482c   Ingo Molnar   perf: Do the big ...
5684
  static void sync_child_event(struct perf_event *child_event,
38b200d67   Peter Zijlstra   perf_counter: Add...
5685
  			       struct task_struct *child)
d859e29fe   Paul Mackerras   perf_counter: Add...
5686
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
5687
  	struct perf_event *parent_event = child_event->parent;
8bc209595   Peter Zijlstra   perf_counter: Fix...
5688
  	u64 child_val;
d859e29fe   Paul Mackerras   perf_counter: Add...
5689

cdd6c482c   Ingo Molnar   perf: Do the big ...
5690
5691
  	if (child_event->attr.inherit_stat)
  		perf_event_read_event(child_event, child);
38b200d67   Peter Zijlstra   perf_counter: Add...
5692

b5e58793c   Peter Zijlstra   perf: Add perf_ev...
5693
  	child_val = perf_event_count(child_event);
d859e29fe   Paul Mackerras   perf_counter: Add...
5694
5695
5696
5697
  
  	/*
  	 * Add back the child's count to the parent's count:
  	 */
a6e6dea68   Peter Zijlstra   perf: Add perf_ev...
5698
  	atomic64_add(child_val, &parent_event->child_count);
cdd6c482c   Ingo Molnar   perf: Do the big ...
5699
5700
5701
5702
  	atomic64_add(child_event->total_time_enabled,
  		     &parent_event->child_total_time_enabled);
  	atomic64_add(child_event->total_time_running,
  		     &parent_event->child_total_time_running);
d859e29fe   Paul Mackerras   perf_counter: Add...
5703
5704
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5705
  	 * Remove this event from the parent's list
d859e29fe   Paul Mackerras   perf_counter: Add...
5706
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5707
5708
5709
5710
  	WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  	mutex_lock(&parent_event->child_mutex);
  	list_del_init(&child_event->child_list);
  	mutex_unlock(&parent_event->child_mutex);
d859e29fe   Paul Mackerras   perf_counter: Add...
5711
5712
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5713
  	 * Release the parent event, if this was the last
d859e29fe   Paul Mackerras   perf_counter: Add...
5714
5715
  	 * reference to it.
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5716
  	fput(parent_event->filp);
d859e29fe   Paul Mackerras   perf_counter: Add...
5717
  }
9b51f66dc   Ingo Molnar   perfcounters: imp...
5718
  static void
cdd6c482c   Ingo Molnar   perf: Do the big ...
5719
5720
  __perf_event_exit_task(struct perf_event *child_event,
  			 struct perf_event_context *child_ctx,
38b200d67   Peter Zijlstra   perf_counter: Add...
5721
  			 struct task_struct *child)
9b51f66dc   Ingo Molnar   perfcounters: imp...
5722
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
5723
  	struct perf_event *parent_event;
9b51f66dc   Ingo Molnar   perfcounters: imp...
5724

cdd6c482c   Ingo Molnar   perf: Do the big ...
5725
  	perf_event_remove_from_context(child_event);
0cc0c027d   Ingo Molnar   perfcounters: rel...
5726

cdd6c482c   Ingo Molnar   perf: Do the big ...
5727
  	parent_event = child_event->parent;
9b51f66dc   Ingo Molnar   perfcounters: imp...
5728
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5729
  	 * It can happen that parent exits first, and has events
9b51f66dc   Ingo Molnar   perfcounters: imp...
5730
  	 * that are still around due to the child reference. These
cdd6c482c   Ingo Molnar   perf: Do the big ...
5731
  	 * events need to be zapped - but otherwise linger.
9b51f66dc   Ingo Molnar   perfcounters: imp...
5732
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5733
5734
5735
  	if (parent_event) {
  		sync_child_event(child_event, child);
  		free_event(child_event);
4bcf349a0   Paul Mackerras   perfcounters: fix...
5736
  	}
9b51f66dc   Ingo Molnar   perfcounters: imp...
5737
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5738
  static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
9b51f66dc   Ingo Molnar   perfcounters: imp...
5739
  {
cdd6c482c   Ingo Molnar   perf: Do the big ...
5740
5741
  	struct perf_event *child_event, *tmp;
  	struct perf_event_context *child_ctx;
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
5742
  	unsigned long flags;
9b51f66dc   Ingo Molnar   perfcounters: imp...
5743

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5744
  	if (likely(!child->perf_event_ctxp[ctxn])) {
cdd6c482c   Ingo Molnar   perf: Do the big ...
5745
  		perf_event_task(child, NULL, 0);
9b51f66dc   Ingo Molnar   perfcounters: imp...
5746
  		return;
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
5747
  	}
9b51f66dc   Ingo Molnar   perfcounters: imp...
5748

a63eaf34a   Paul Mackerras   perf_counter: Dyn...
5749
  	local_irq_save(flags);
ad3a37de8   Paul Mackerras   perf_counter: Don...
5750
5751
5752
5753
5754
5755
  	/*
  	 * We can't reschedule here because interrupts are disabled,
  	 * and either child is current or it is a task that can't be
  	 * scheduled, so we are now safe from rescheduling changing
  	 * our context.
  	 */
806839b22   Oleg Nesterov   perf: perf_event_...
5756
  	child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
82cd6def9   Peter Zijlstra   perf: Use jump_la...
5757
  	task_ctx_sched_out(child_ctx, EVENT_ALL);
c93f76690   Paul Mackerras   perf_counter: Fix...
5758
5759
5760
  
  	/*
  	 * Take the context lock here so that if find_get_context is
cdd6c482c   Ingo Molnar   perf: Do the big ...
5761
  	 * reading child->perf_event_ctxp, we wait until it has
c93f76690   Paul Mackerras   perf_counter: Fix...
5762
5763
  	 * incremented the context's refcount before we do put_ctx below.
  	 */
e625cce1b   Thomas Gleixner   perf_event: Conve...
5764
  	raw_spin_lock(&child_ctx->lock);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5765
  	child->perf_event_ctxp[ctxn] = NULL;
71a851b4d   Peter Zijlstra   perf_counter: Sto...
5766
5767
5768
  	/*
  	 * If this context is a clone; unclone it so it can't get
  	 * swapped to another process while we're removing all
cdd6c482c   Ingo Molnar   perf: Do the big ...
5769
  	 * the events from it.
71a851b4d   Peter Zijlstra   perf_counter: Sto...
5770
5771
  	 */
  	unclone_ctx(child_ctx);
5e942bb33   Peter Zijlstra   perf_events: Upda...
5772
  	update_context_time(child_ctx);
e625cce1b   Thomas Gleixner   perf_event: Conve...
5773
  	raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
5774
5775
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5776
5777
5778
  	 * Report the task dead after unscheduling the events so that we
  	 * won't get any samples after PERF_RECORD_EXIT. We can however still
  	 * get a few PERF_RECORD_READ events.
9f498cc5b   Peter Zijlstra   perf_counter: Ful...
5779
  	 */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5780
  	perf_event_task(child, child_ctx, 0);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
5781

66fff2248   Peter Zijlstra   perf_counter: Ann...
5782
5783
5784
  	/*
  	 * We can recurse on the same lock type through:
  	 *
cdd6c482c   Ingo Molnar   perf: Do the big ...
5785
5786
5787
  	 *   __perf_event_exit_task()
  	 *     sync_child_event()
  	 *       fput(parent_event->filp)
66fff2248   Peter Zijlstra   perf_counter: Ann...
5788
5789
5790
5791
5792
  	 *         perf_release()
  	 *           mutex_lock(&ctx->mutex)
  	 *
  	 * But since its the parent context it won't be the same instance.
  	 */
a0507c84b   Peter Zijlstra   perf: Annotate pe...
5793
  	mutex_lock(&child_ctx->mutex);
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
5794

8bc209595   Peter Zijlstra   perf_counter: Fix...
5795
  again:
889ff0150   Frederic Weisbecker   perf/core: Split ...
5796
5797
5798
5799
5800
  	list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  				 group_entry)
  		__perf_event_exit_task(child_event, child_ctx, child);
  
  	list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
65abc8653   Ingo Molnar   perf_counter: Ren...
5801
  				 group_entry)
cdd6c482c   Ingo Molnar   perf: Do the big ...
5802
  		__perf_event_exit_task(child_event, child_ctx, child);
8bc209595   Peter Zijlstra   perf_counter: Fix...
5803
5804
  
  	/*
cdd6c482c   Ingo Molnar   perf: Do the big ...
5805
  	 * If the last event was a group event, it will have appended all
8bc209595   Peter Zijlstra   perf_counter: Fix...
5806
5807
5808
  	 * its siblings to the list, but we obtained 'tmp' before that which
  	 * will still point to the list head terminating the iteration.
  	 */
889ff0150   Frederic Weisbecker   perf/core: Split ...
5809
5810
  	if (!list_empty(&child_ctx->pinned_groups) ||
  	    !list_empty(&child_ctx->flexible_groups))
8bc209595   Peter Zijlstra   perf_counter: Fix...
5811
  		goto again;
a63eaf34a   Paul Mackerras   perf_counter: Dyn...
5812
5813
5814
5815
  
  	mutex_unlock(&child_ctx->mutex);
  
  	put_ctx(child_ctx);
9b51f66dc   Ingo Molnar   perfcounters: imp...
5816
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5817
5818
5819
5820
5821
  /*
   * When a child task exits, feed back event values to parent events.
   */
  void perf_event_exit_task(struct task_struct *child)
  {
8882135bc   Peter Zijlstra   perf: Fix owner-l...
5822
  	struct perf_event *event, *tmp;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5823
  	int ctxn;
8882135bc   Peter Zijlstra   perf: Fix owner-l...
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
  	mutex_lock(&child->perf_event_mutex);
  	list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  				 owner_entry) {
  		list_del_init(&event->owner_entry);
  
  		/*
  		 * Ensure the list deletion is visible before we clear
  		 * the owner, closes a race against perf_release() where
  		 * we need to serialize on the owner->perf_event_mutex.
  		 */
  		smp_wmb();
  		event->owner = NULL;
  	}
  	mutex_unlock(&child->perf_event_mutex);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5838
5839
5840
  	for_each_task_context_nr(ctxn)
  		perf_event_exit_task_context(child, ctxn);
  }
889ff0150   Frederic Weisbecker   perf/core: Split ...
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
  static void perf_free_event(struct perf_event *event,
  			    struct perf_event_context *ctx)
  {
  	struct perf_event *parent = event->parent;
  
  	if (WARN_ON_ONCE(!parent))
  		return;
  
  	mutex_lock(&parent->child_mutex);
  	list_del_init(&event->child_list);
  	mutex_unlock(&parent->child_mutex);
  
  	fput(parent->filp);
8a49542c0   Peter Zijlstra   perf_events: Fix ...
5854
  	perf_group_detach(event);
889ff0150   Frederic Weisbecker   perf/core: Split ...
5855
5856
5857
  	list_del_event(event, ctx);
  	free_event(event);
  }
9b51f66dc   Ingo Molnar   perfcounters: imp...
5858
  /*
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5859
   * free an unexposed, unused context as created by inheritance by
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5860
   * perf_event_init_task below, used by fork() in case of fail.
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5861
   */
cdd6c482c   Ingo Molnar   perf: Do the big ...
5862
  void perf_event_free_task(struct task_struct *task)
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5863
  {
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5864
  	struct perf_event_context *ctx;
cdd6c482c   Ingo Molnar   perf: Do the big ...
5865
  	struct perf_event *event, *tmp;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5866
  	int ctxn;
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5867

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5868
5869
5870
5871
  	for_each_task_context_nr(ctxn) {
  		ctx = task->perf_event_ctxp[ctxn];
  		if (!ctx)
  			continue;
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5872

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5873
  		mutex_lock(&ctx->mutex);
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5874
  again:
8dc85d547   Peter Zijlstra   perf: Multiple ta...
5875
5876
5877
  		list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  				group_entry)
  			perf_free_event(event, ctx);
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5878

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5879
5880
5881
  		list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  				group_entry)
  			perf_free_event(event, ctx);
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5882

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5883
5884
5885
  		if (!list_empty(&ctx->pinned_groups) ||
  				!list_empty(&ctx->flexible_groups))
  			goto again;
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5886

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5887
  		mutex_unlock(&ctx->mutex);
bbbee9082   Peter Zijlstra   perf_counter: Amm...
5888

8dc85d547   Peter Zijlstra   perf: Multiple ta...
5889
5890
  		put_ctx(ctx);
  	}
889ff0150   Frederic Weisbecker   perf/core: Split ...
5891
  }
4e231c796   Peter Zijlstra   perf: Fix up dela...
5892
5893
5894
5895
5896
5897
5898
  void perf_event_delayed_put(struct task_struct *task)
  {
  	int ctxn;
  
  	for_each_task_context_nr(ctxn)
  		WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  }
97dee4f32   Peter Zijlstra   perf: Move some c...
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
  /*
   * inherit a event from parent task to child task:
   */
  static struct perf_event *
  inherit_event(struct perf_event *parent_event,
  	      struct task_struct *parent,
  	      struct perf_event_context *parent_ctx,
  	      struct task_struct *child,
  	      struct perf_event *group_leader,
  	      struct perf_event_context *child_ctx)
  {
  	struct perf_event *child_event;
cee010ec5   Peter Zijlstra   perf: Ensure we c...
5911
  	unsigned long flags;
97dee4f32   Peter Zijlstra   perf: Move some c...
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
  
  	/*
  	 * Instead of creating recursive hierarchies of events,
  	 * we link inherited events back to the original parent,
  	 * which has a filp for sure, which we use as the reference
  	 * count:
  	 */
  	if (parent_event->parent)
  		parent_event = parent_event->parent;
  
  	child_event = perf_event_alloc(&parent_event->attr,
  					   parent_event->cpu,
d580ff869   Peter Zijlstra   perf, hw_breakpoi...
5924
  					   child,
97dee4f32   Peter Zijlstra   perf: Move some c...
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
  					   group_leader, parent_event,
  					   NULL);
  	if (IS_ERR(child_event))
  		return child_event;
  	get_ctx(child_ctx);
  
  	/*
  	 * Make the child state follow the state of the parent event,
  	 * not its attr.disabled bit.  We hold the parent's mutex,
  	 * so we won't race with perf_event_{en, dis}able_family.
  	 */
  	if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  		child_event->state = PERF_EVENT_STATE_INACTIVE;
  	else
  		child_event->state = PERF_EVENT_STATE_OFF;
  
  	if (parent_event->attr.freq) {
  		u64 sample_period = parent_event->hw.sample_period;
  		struct hw_perf_event *hwc = &child_event->hw;
  
  		hwc->sample_period = sample_period;
  		hwc->last_period   = sample_period;
  
  		local64_set(&hwc->period_left, sample_period);
  	}
  
  	child_event->ctx = child_ctx;
  	child_event->overflow_handler = parent_event->overflow_handler;
  
  	/*
614b6780e   Thomas Gleixner   perf events: Fix ...
5955
5956
5957
  	 * Precalculate sample_data sizes
  	 */
  	perf_event__header_size(child_event);
6844c09d8   Arnaldo Carvalho de Melo   perf events: Sepa...
5958
  	perf_event__id_header_size(child_event);
614b6780e   Thomas Gleixner   perf events: Fix ...
5959
5960
  
  	/*
97dee4f32   Peter Zijlstra   perf: Move some c...
5961
5962
  	 * Link it up in the child's context:
  	 */
cee010ec5   Peter Zijlstra   perf: Ensure we c...
5963
  	raw_spin_lock_irqsave(&child_ctx->lock, flags);
97dee4f32   Peter Zijlstra   perf: Move some c...
5964
  	add_event_to_ctx(child_event, child_ctx);
cee010ec5   Peter Zijlstra   perf: Ensure we c...
5965
  	raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
97dee4f32   Peter Zijlstra   perf: Move some c...
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
  
  	/*
  	 * Get a reference to the parent filp - we will fput it
  	 * when the child event exits. This is safe to do because
  	 * we are in the parent and we know that the filp still
  	 * exists and has a nonzero count:
  	 */
  	atomic_long_inc(&parent_event->filp->f_count);
  
  	/*
  	 * Link this into the parent event's child list
  	 */
  	WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  	mutex_lock(&parent_event->child_mutex);
  	list_add_tail(&child_event->child_list, &parent_event->child_list);
  	mutex_unlock(&parent_event->child_mutex);
  
  	return child_event;
  }
  
  static int inherit_group(struct perf_event *parent_event,
  	      struct task_struct *parent,
  	      struct perf_event_context *parent_ctx,
  	      struct task_struct *child,
  	      struct perf_event_context *child_ctx)
  {
  	struct perf_event *leader;
  	struct perf_event *sub;
  	struct perf_event *child_ctr;
  
  	leader = inherit_event(parent_event, parent, parent_ctx,
  				 child, NULL, child_ctx);
  	if (IS_ERR(leader))
  		return PTR_ERR(leader);
  	list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  		child_ctr = inherit_event(sub, parent, parent_ctx,
  					    child, leader, child_ctx);
  		if (IS_ERR(child_ctr))
  			return PTR_ERR(child_ctr);
  	}
  	return 0;
889ff0150   Frederic Weisbecker   perf/core: Split ...
6007
6008
6009
6010
6011
  }
  
  static int
  inherit_task_group(struct perf_event *event, struct task_struct *parent,
  		   struct perf_event_context *parent_ctx,
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6012
  		   struct task_struct *child, int ctxn,
889ff0150   Frederic Weisbecker   perf/core: Split ...
6013
6014
6015
  		   int *inherited_all)
  {
  	int ret;
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6016
  	struct perf_event_context *child_ctx;
889ff0150   Frederic Weisbecker   perf/core: Split ...
6017
6018
6019
6020
  
  	if (!event->attr.inherit) {
  		*inherited_all = 0;
  		return 0;
bbbee9082   Peter Zijlstra   perf_counter: Amm...
6021
  	}
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6022
         	child_ctx = child->perf_event_ctxp[ctxn];
889ff0150   Frederic Weisbecker   perf/core: Split ...
6023
6024
6025
6026
6027
6028
6029
  	if (!child_ctx) {
  		/*
  		 * This is executed from the parent task context, so
  		 * inherit events that have been marked for cloning.
  		 * First allocate and initialize a context for the
  		 * child.
  		 */
bbbee9082   Peter Zijlstra   perf_counter: Amm...
6030

eb1844798   Peter Zijlstra   perf: Clean up pe...
6031
  		child_ctx = alloc_perf_context(event->pmu, child);
889ff0150   Frederic Weisbecker   perf/core: Split ...
6032
6033
  		if (!child_ctx)
  			return -ENOMEM;
bbbee9082   Peter Zijlstra   perf_counter: Amm...
6034

8dc85d547   Peter Zijlstra   perf: Multiple ta...
6035
  		child->perf_event_ctxp[ctxn] = child_ctx;
889ff0150   Frederic Weisbecker   perf/core: Split ...
6036
6037
6038
6039
6040
6041
6042
6043
6044
  	}
  
  	ret = inherit_group(event, parent, parent_ctx,
  			    child, child_ctx);
  
  	if (ret)
  		*inherited_all = 0;
  
  	return ret;
bbbee9082   Peter Zijlstra   perf_counter: Amm...
6045
6046
6047
  }
  
  /*
cdd6c482c   Ingo Molnar   perf: Do the big ...
6048
   * Initialize the perf_event context in task_struct
9b51f66dc   Ingo Molnar   perfcounters: imp...
6049
   */
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6050
  int perf_event_init_context(struct task_struct *child, int ctxn)
9b51f66dc   Ingo Molnar   perfcounters: imp...
6051
  {
889ff0150   Frederic Weisbecker   perf/core: Split ...
6052
  	struct perf_event_context *child_ctx, *parent_ctx;
cdd6c482c   Ingo Molnar   perf: Do the big ...
6053
6054
  	struct perf_event_context *cloned_ctx;
  	struct perf_event *event;
9b51f66dc   Ingo Molnar   perfcounters: imp...
6055
  	struct task_struct *parent = current;
564c2b210   Paul Mackerras   perf_counter: Opt...
6056
  	int inherited_all = 1;
dddd3379a   Thomas Gleixner   perf: Fix inherit...
6057
  	unsigned long flags;
6ab423e0e   Peter Zijlstra   perf_counter: Pro...
6058
  	int ret = 0;
9b51f66dc   Ingo Molnar   perfcounters: imp...
6059

8dc85d547   Peter Zijlstra   perf: Multiple ta...
6060
  	if (likely(!parent->perf_event_ctxp[ctxn]))
6ab423e0e   Peter Zijlstra   perf_counter: Pro...
6061
  		return 0;
9b51f66dc   Ingo Molnar   perfcounters: imp...
6062
  	/*
25346b93c   Paul Mackerras   perf_counter: Pro...
6063
6064
  	 * If the parent's context is a clone, pin it so it won't get
  	 * swapped under us.
ad3a37de8   Paul Mackerras   perf_counter: Don...
6065
  	 */
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6066
  	parent_ctx = perf_pin_task_context(parent, ctxn);
25346b93c   Paul Mackerras   perf_counter: Pro...
6067

ad3a37de8   Paul Mackerras   perf_counter: Don...
6068
6069
6070
6071
6072
6073
  	/*
  	 * No need to check if parent_ctx != NULL here; since we saw
  	 * it non-NULL earlier, the only reason for it to become NULL
  	 * is if we exit, and since we're currently in the middle of
  	 * a fork we can't be exiting at the same time.
  	 */
ad3a37de8   Paul Mackerras   perf_counter: Don...
6074
6075
  
  	/*
9b51f66dc   Ingo Molnar   perfcounters: imp...
6076
6077
6078
  	 * Lock the parent list. No need to lock the child - not PID
  	 * hashed yet and not running, so nobody can access it.
  	 */
d859e29fe   Paul Mackerras   perf_counter: Add...
6079
  	mutex_lock(&parent_ctx->mutex);
9b51f66dc   Ingo Molnar   perfcounters: imp...
6080
6081
6082
6083
6084
  
  	/*
  	 * We dont have to disable NMIs - we are only looking at
  	 * the list, not manipulating it:
  	 */
889ff0150   Frederic Weisbecker   perf/core: Split ...
6085
  	list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6086
6087
  		ret = inherit_task_group(event, parent, parent_ctx,
  					 child, ctxn, &inherited_all);
889ff0150   Frederic Weisbecker   perf/core: Split ...
6088
6089
6090
  		if (ret)
  			break;
  	}
b93f7978a   Xiao Guangrong   perf_event: Alloc...
6091

dddd3379a   Thomas Gleixner   perf: Fix inherit...
6092
6093
6094
6095
6096
6097
6098
6099
  	/*
  	 * We can't hold ctx->lock when iterating the ->flexible_group list due
  	 * to allocations, but we need to prevent rotation because
  	 * rotate_ctx() will change the list from interrupt context.
  	 */
  	raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  	parent_ctx->rotate_disable = 1;
  	raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
889ff0150   Frederic Weisbecker   perf/core: Split ...
6100
  	list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6101
6102
  		ret = inherit_task_group(event, parent, parent_ctx,
  					 child, ctxn, &inherited_all);
889ff0150   Frederic Weisbecker   perf/core: Split ...
6103
  		if (ret)
9b51f66dc   Ingo Molnar   perfcounters: imp...
6104
  			break;
564c2b210   Paul Mackerras   perf_counter: Opt...
6105
  	}
dddd3379a   Thomas Gleixner   perf: Fix inherit...
6106
6107
  	raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  	parent_ctx->rotate_disable = 0;
dddd3379a   Thomas Gleixner   perf: Fix inherit...
6108

8dc85d547   Peter Zijlstra   perf: Multiple ta...
6109
  	child_ctx = child->perf_event_ctxp[ctxn];
889ff0150   Frederic Weisbecker   perf/core: Split ...
6110

05cbaa285   Peter Zijlstra   perf: Fix NULL de...
6111
  	if (child_ctx && inherited_all) {
564c2b210   Paul Mackerras   perf_counter: Opt...
6112
6113
6114
  		/*
  		 * Mark the child context as a clone of the parent
  		 * context, or of whatever the parent is a clone of.
c5ed51455   Peter Zijlstra   perf: Fix context...
6115
6116
6117
  		 *
  		 * Note that if the parent is a clone, the holding of
  		 * parent_ctx->lock avoids it from being uncloned.
564c2b210   Paul Mackerras   perf_counter: Opt...
6118
  		 */
c5ed51455   Peter Zijlstra   perf: Fix context...
6119
  		cloned_ctx = parent_ctx->parent_ctx;
ad3a37de8   Paul Mackerras   perf_counter: Don...
6120
6121
  		if (cloned_ctx) {
  			child_ctx->parent_ctx = cloned_ctx;
25346b93c   Paul Mackerras   perf_counter: Pro...
6122
  			child_ctx->parent_gen = parent_ctx->parent_gen;
564c2b210   Paul Mackerras   perf_counter: Opt...
6123
6124
6125
6126
6127
  		} else {
  			child_ctx->parent_ctx = parent_ctx;
  			child_ctx->parent_gen = parent_ctx->generation;
  		}
  		get_ctx(child_ctx->parent_ctx);
9b51f66dc   Ingo Molnar   perfcounters: imp...
6128
  	}
c5ed51455   Peter Zijlstra   perf: Fix context...
6129
  	raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
d859e29fe   Paul Mackerras   perf_counter: Add...
6130
  	mutex_unlock(&parent_ctx->mutex);
6ab423e0e   Peter Zijlstra   perf_counter: Pro...
6131

25346b93c   Paul Mackerras   perf_counter: Pro...
6132
  	perf_unpin_context(parent_ctx);
ad3a37de8   Paul Mackerras   perf_counter: Don...
6133

6ab423e0e   Peter Zijlstra   perf_counter: Pro...
6134
  	return ret;
9b51f66dc   Ingo Molnar   perfcounters: imp...
6135
  }
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6136
6137
6138
6139
6140
6141
  /*
   * Initialize the perf_event context in task_struct
   */
  int perf_event_init_task(struct task_struct *child)
  {
  	int ctxn, ret;
8550d7cb6   Oleg Nesterov   perf: Fix perf_ev...
6142
6143
6144
  	memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  	mutex_init(&child->perf_event_mutex);
  	INIT_LIST_HEAD(&child->perf_event_list);
8dc85d547   Peter Zijlstra   perf: Multiple ta...
6145
6146
6147
6148
6149
6150
6151
6152
  	for_each_task_context_nr(ctxn) {
  		ret = perf_event_init_context(child, ctxn);
  		if (ret)
  			return ret;
  	}
  
  	return 0;
  }
220b140b5   Paul Mackerras   perf_event: Fix o...
6153
6154
  static void __init perf_event_init_all_cpus(void)
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
6155
  	struct swevent_htable *swhash;
220b140b5   Paul Mackerras   perf_event: Fix o...
6156
  	int cpu;
220b140b5   Paul Mackerras   perf_event: Fix o...
6157
6158
  
  	for_each_possible_cpu(cpu) {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
6159
6160
  		swhash = &per_cpu(swevent_htable, cpu);
  		mutex_init(&swhash->hlist_mutex);
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
6161
  		INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
220b140b5   Paul Mackerras   perf_event: Fix o...
6162
6163
  	}
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
6164
  static void __cpuinit perf_event_init_cpu(int cpu)
0793a61d4   Thomas Gleixner   performance count...
6165
  {
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6166
  	struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
0793a61d4   Thomas Gleixner   performance count...
6167

b28ab83c5   Peter Zijlstra   perf: Remove the ...
6168
6169
  	mutex_lock(&swhash->hlist_mutex);
  	if (swhash->hlist_refcount > 0) {
76e1d9047   Frederic Weisbecker   perf: Store activ...
6170
  		struct swevent_hlist *hlist;
b28ab83c5   Peter Zijlstra   perf: Remove the ...
6171
6172
6173
  		hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  		WARN_ON(!hlist);
  		rcu_assign_pointer(swhash->swevent_hlist, hlist);
76e1d9047   Frederic Weisbecker   perf: Store activ...
6174
  	}
b28ab83c5   Peter Zijlstra   perf: Remove the ...
6175
  	mutex_unlock(&swhash->hlist_mutex);
0793a61d4   Thomas Gleixner   performance count...
6176
  }
c277443cf   Peter Zijlstra   perf: Stop all co...
6177
  #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
6178
  static void perf_pmu_rotate_stop(struct pmu *pmu)
0793a61d4   Thomas Gleixner   performance count...
6179
  {
e9d2b0641   Peter Zijlstra   perf: Undo the pe...
6180
6181
6182
6183
6184
6185
  	struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  
  	WARN_ON(!irqs_disabled());
  
  	list_del_init(&cpuctx->rotation_list);
  }
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6186
  static void __perf_event_exit_context(void *__info)
0793a61d4   Thomas Gleixner   performance count...
6187
  {
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6188
  	struct perf_event_context *ctx = __info;
cdd6c482c   Ingo Molnar   perf: Do the big ...
6189
  	struct perf_event *event, *tmp;
0793a61d4   Thomas Gleixner   performance count...
6190

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6191
  	perf_pmu_rotate_stop(ctx->pmu);
b5ab4cd56   Peter Zijlstra   perf: Per cpu-con...
6192

889ff0150   Frederic Weisbecker   perf/core: Split ...
6193
6194
6195
  	list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  		__perf_event_remove_from_context(event);
  	list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
cdd6c482c   Ingo Molnar   perf: Do the big ...
6196
  		__perf_event_remove_from_context(event);
0793a61d4   Thomas Gleixner   performance count...
6197
  }
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6198
6199
6200
6201
6202
6203
6204
6205
6206
  
  static void perf_event_exit_cpu_context(int cpu)
  {
  	struct perf_event_context *ctx;
  	struct pmu *pmu;
  	int idx;
  
  	idx = srcu_read_lock(&pmus_srcu);
  	list_for_each_entry_rcu(pmu, &pmus, entry) {
917bdd1c9   Peter Zijlstra   perf: Fix perf_ev...
6207
  		ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6208
6209
6210
6211
6212
6213
  
  		mutex_lock(&ctx->mutex);
  		smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  		mutex_unlock(&ctx->mutex);
  	}
  	srcu_read_unlock(&pmus_srcu, idx);
108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6214
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
6215
  static void perf_event_exit_cpu(int cpu)
0793a61d4   Thomas Gleixner   performance count...
6216
  {
b28ab83c5   Peter Zijlstra   perf: Remove the ...
6217
  	struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
d859e29fe   Paul Mackerras   perf_counter: Add...
6218

b28ab83c5   Peter Zijlstra   perf: Remove the ...
6219
6220
6221
  	mutex_lock(&swhash->hlist_mutex);
  	swevent_hlist_release(swhash);
  	mutex_unlock(&swhash->hlist_mutex);
76e1d9047   Frederic Weisbecker   perf: Store activ...
6222

108b02cfc   Peter Zijlstra   perf: Per-pmu-per...
6223
  	perf_event_exit_cpu_context(cpu);
0793a61d4   Thomas Gleixner   performance count...
6224
6225
  }
  #else
cdd6c482c   Ingo Molnar   perf: Do the big ...
6226
  static inline void perf_event_exit_cpu(int cpu) { }
0793a61d4   Thomas Gleixner   performance count...
6227
  #endif
c277443cf   Peter Zijlstra   perf: Stop all co...
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
  static int
  perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  {
  	int cpu;
  
  	for_each_online_cpu(cpu)
  		perf_event_exit_cpu(cpu);
  
  	return NOTIFY_OK;
  }
  
  /*
   * Run the perf reboot notifier at the very last possible moment so that
   * the generic watchdog code runs as long as possible.
   */
  static struct notifier_block perf_reboot_notifier = {
  	.notifier_call = perf_reboot,
  	.priority = INT_MIN,
  };
0793a61d4   Thomas Gleixner   performance count...
6247
6248
6249
6250
  static int __cpuinit
  perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  {
  	unsigned int cpu = (long)hcpu;
5e11637e2   Peter Zijlstra   perf: Fix CPU hot...
6251
  	switch (action & ~CPU_TASKS_FROZEN) {
0793a61d4   Thomas Gleixner   performance count...
6252
6253
  
  	case CPU_UP_PREPARE:
5e11637e2   Peter Zijlstra   perf: Fix CPU hot...
6254
  	case CPU_DOWN_FAILED:
cdd6c482c   Ingo Molnar   perf: Do the big ...
6255
  		perf_event_init_cpu(cpu);
0793a61d4   Thomas Gleixner   performance count...
6256
  		break;
5e11637e2   Peter Zijlstra   perf: Fix CPU hot...
6257
  	case CPU_UP_CANCELED:
0793a61d4   Thomas Gleixner   performance count...
6258
  	case CPU_DOWN_PREPARE:
cdd6c482c   Ingo Molnar   perf: Do the big ...
6259
  		perf_event_exit_cpu(cpu);
0793a61d4   Thomas Gleixner   performance count...
6260
6261
6262
6263
6264
6265
6266
6267
  		break;
  
  	default:
  		break;
  	}
  
  	return NOTIFY_OK;
  }
cdd6c482c   Ingo Molnar   perf: Do the big ...
6268
  void __init perf_event_init(void)
0793a61d4   Thomas Gleixner   performance count...
6269
  {
3c502e7a0   Jason Wessel   perf,hw_breakpoin...
6270
  	int ret;
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
6271
  	idr_init(&pmu_idr);
220b140b5   Paul Mackerras   perf_event: Fix o...
6272
  	perf_event_init_all_cpus();
b0a873ebb   Peter Zijlstra   perf: Register PM...
6273
  	init_srcu_struct(&pmus_srcu);
2e80a82a4   Peter Zijlstra   perf: Dynamic pmu...
6274
6275
6276
  	perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  	perf_pmu_register(&perf_cpu_clock, NULL, -1);
  	perf_pmu_register(&perf_task_clock, NULL, -1);
b0a873ebb   Peter Zijlstra   perf: Register PM...
6277
6278
  	perf_tp_register();
  	perf_cpu_notifier(perf_cpu_notify);
c277443cf   Peter Zijlstra   perf: Stop all co...
6279
  	register_reboot_notifier(&perf_reboot_notifier);
3c502e7a0   Jason Wessel   perf,hw_breakpoin...
6280
6281
6282
  
  	ret = init_hw_breakpoint();
  	WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
0793a61d4   Thomas Gleixner   performance count...
6283
  }
abe434005   Peter Zijlstra   perf: Sysfs enume...
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
  
  static int __init perf_event_sysfs_init(void)
  {
  	struct pmu *pmu;
  	int ret;
  
  	mutex_lock(&pmus_lock);
  
  	ret = bus_register(&pmu_bus);
  	if (ret)
  		goto unlock;
  
  	list_for_each_entry(pmu, &pmus, entry) {
  		if (!pmu->name || pmu->type < 0)
  			continue;
  
  		ret = pmu_dev_alloc(pmu);
  		WARN(ret, "Failed to register pmu: %s, reason %d
  ", pmu->name, ret);
  	}
  	pmu_bus_running = 1;
  	ret = 0;
  
  unlock:
  	mutex_unlock(&pmus_lock);
  
  	return ret;
  }
  device_initcall(perf_event_sysfs_init);