Blame view

drivers/oprofile/buffer_sync.c 13.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /**
   * @file buffer_sync.c
   *
ae735e996   Robert Richter   oprofile: rework ...
4
   * @remark Copyright 2002-2009 OProfile authors
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
   * @remark Read the file COPYING
   *
   * @author John Levon <levon@movementarian.org>
345c25730   Barry Kasindorf   x86/oprofile: add...
8
   * @author Barry Kasindorf
ae735e996   Robert Richter   oprofile: rework ...
9
   * @author Robert Richter <robert.richter@amd.com>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
16
17
18
19
20
21
22
   *
   * This is the core of the buffer management. Each
   * CPU buffer is processed and entered into the
   * global event buffer. Such processing is necessary
   * in several circumstances, mentioned below.
   *
   * The processing does the job of converting the
   * transitory EIP value into a persistent dentry/offset
   * value that the profiler can record at its leisure.
   *
   * See fs/dcookies.c for a description of the dentry/offset
   * objects.
   */
11163348a   Davidlohr Bueso   oprofile: reduce ...
23
  #include <linux/file.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
27
28
29
30
  #include <linux/mm.h>
  #include <linux/workqueue.h>
  #include <linux/notifier.h>
  #include <linux/dcookies.h>
  #include <linux/profile.h>
  #include <linux/module.h>
  #include <linux/fs.h>
1474855d0   Bob Nelson   [CELL] oprofile: ...
31
  #include <linux/oprofile.h>
e8edc6e03   Alexey Dobriyan   Detach sched.h fr...
32
  #include <linux/sched.h>
6e84f3152   Ingo Molnar   sched/headers: Pr...
33
  #include <linux/sched/mm.h>
0881e7bd3   Ingo Molnar   sched/headers: Pr...
34
  #include <linux/sched/task.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
35
  #include <linux/gfp.h>
1474855d0   Bob Nelson   [CELL] oprofile: ...
36

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
  #include "oprofile_stats.h"
  #include "event_buffer.h"
  #include "cpu_buffer.h"
  #include "buffer_sync.h"
73185e0a5   Robert Richter   drivers/oprofile:...
41

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
  static LIST_HEAD(dying_tasks);
  static LIST_HEAD(dead_tasks);
f7df8ed16   Rusty Russell   cpumask: convert ...
44
  static cpumask_var_t marked_cpus;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
  static DEFINE_SPINLOCK(task_mortuary);
  static void process_task_mortuary(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
48
49
50
  /* Take ownership of the task struct and place it on the
   * list for processing. Only after two full buffer syncs
   * does the task eventually get freed, because by then
   * we are sure we will not reference it again.
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
51
52
   * Can be invoked from softirq via RCU callback due to
   * call_rcu() of the task struct, hence the _irqsave.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
   */
73185e0a5   Robert Richter   drivers/oprofile:...
54
55
  static int
  task_free_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
  {
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
57
  	unsigned long flags;
73185e0a5   Robert Richter   drivers/oprofile:...
58
  	struct task_struct *task = data;
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
59
  	spin_lock_irqsave(&task_mortuary, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  	list_add(&task->tasks, &dying_tasks);
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
61
  	spin_unlock_irqrestore(&task_mortuary, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
  	return NOTIFY_OK;
  }
  
  
  /* The task is on its way out. A sync of the buffer means we can catch
   * any remaining samples for this task.
   */
73185e0a5   Robert Richter   drivers/oprofile:...
69
70
  static int
  task_exit_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
  {
  	/* To avoid latency problems, we only process the current CPU,
  	 * hoping that most samples for the task are on this CPU
  	 */
39c715b71   Ingo Molnar   [PATCH] smp_proce...
75
  	sync_buffer(raw_smp_processor_id());
73185e0a5   Robert Richter   drivers/oprofile:...
76
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
79
80
81
82
83
84
  }
  
  
  /* The task is about to try a do_munmap(). We peek at what it's going to
   * do, and if it's an executable region, process the samples first, so
   * we don't lose any. This does not have to be exact, it's a QoI issue
   * only.
   */
73185e0a5   Robert Richter   drivers/oprofile:...
85
86
  static int
  munmap_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
88
  {
  	unsigned long addr = (unsigned long)data;
73185e0a5   Robert Richter   drivers/oprofile:...
89
90
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *mpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91

d8ed45c5d   Michel Lespinasse   mmap locking API:...
92
  	mmap_read_lock(mm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
95
  
  	mpnt = find_vma(mm, addr);
  	if (mpnt && mpnt->vm_file && (mpnt->vm_flags & VM_EXEC)) {
d8ed45c5d   Michel Lespinasse   mmap locking API:...
96
  		mmap_read_unlock(mm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
  		/* To avoid latency problems, we only process the current CPU,
  		 * hoping that most samples for the task are on this CPU
  		 */
39c715b71   Ingo Molnar   [PATCH] smp_proce...
100
  		sync_buffer(raw_smp_processor_id());
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  		return 0;
  	}
d8ed45c5d   Michel Lespinasse   mmap locking API:...
103
  	mmap_read_unlock(mm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
  	return 0;
  }
73185e0a5   Robert Richter   drivers/oprofile:...
106

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
  /* We need to be told about new modules so we don't attribute to a previously
   * loaded module, or drop the samples on the floor.
   */
73185e0a5   Robert Richter   drivers/oprofile:...
110
111
  static int
  module_load_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
113
114
  {
  #ifdef CONFIG_MODULES
  	if (val != MODULE_STATE_COMING)
0340a6b7f   Peter Zijlstra   module: Fix up mo...
115
  		return NOTIFY_DONE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
117
  
  	/* FIXME: should we process all CPU buffers ? */
59cc185ad   Markus Armbruster   [PATCH] oprofile:...
118
  	mutex_lock(&buffer_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(MODULE_LOADED_CODE);
59cc185ad   Markus Armbruster   [PATCH] oprofile:...
121
  	mutex_unlock(&buffer_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  #endif
0340a6b7f   Peter Zijlstra   module: Fix up mo...
123
  	return NOTIFY_OK;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  }
73185e0a5   Robert Richter   drivers/oprofile:...
125

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  static struct notifier_block task_free_nb = {
  	.notifier_call	= task_free_notify,
  };
  
  static struct notifier_block task_exit_nb = {
  	.notifier_call	= task_exit_notify,
  };
  
  static struct notifier_block munmap_nb = {
  	.notifier_call	= munmap_notify,
  };
  
  static struct notifier_block module_load_nb = {
  	.notifier_call = module_load_notify,
  };
6ac6519b9   Robert Richter   oprofile: Free po...
141
142
143
144
145
146
  static void free_all_tasks(void)
  {
  	/* make sure we don't leak task structs */
  	process_task_mortuary();
  	process_task_mortuary();
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
  int sync_start(void)
  {
  	int err;
79f559977   Li Zefan   cpumask: use zall...
150
  	if (!zalloc_cpumask_var(&marked_cpus, GFP_KERNEL))
4c50d9ea9   Robert Richter   cpumask: modifiy ...
151
  		return -ENOMEM;
4c50d9ea9   Robert Richter   cpumask: modifiy ...
152

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
154
155
156
157
158
159
160
161
162
163
164
  	err = task_handoff_register(&task_free_nb);
  	if (err)
  		goto out1;
  	err = profile_event_register(PROFILE_TASK_EXIT, &task_exit_nb);
  	if (err)
  		goto out2;
  	err = profile_event_register(PROFILE_MUNMAP, &munmap_nb);
  	if (err)
  		goto out3;
  	err = register_module_notifier(&module_load_nb);
  	if (err)
  		goto out4;
750d857c6   Robert Richter   oprofile: fix cra...
165
  	start_cpu_work();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
166
167
168
169
170
171
172
173
  out:
  	return err;
  out4:
  	profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
  out3:
  	profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
  out2:
  	task_handoff_unregister(&task_free_nb);
6ac6519b9   Robert Richter   oprofile: Free po...
174
  	free_all_tasks();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
  out1:
4c50d9ea9   Robert Richter   cpumask: modifiy ...
176
  	free_cpumask_var(marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
181
182
  	goto out;
  }
  
  
  void sync_stop(void)
  {
750d857c6   Robert Richter   oprofile: fix cra...
183
  	end_cpu_work();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
  	unregister_module_notifier(&module_load_nb);
  	profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
  	profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
  	task_handoff_unregister(&task_free_nb);
130c5ce71   Robert Richter   oprofile: Fix loc...
188
  	barrier();			/* do all of the above first */
3d7851b3c   Tejun Heo   oprofile: Remove ...
189
  	flush_cpu_work();
750d857c6   Robert Richter   oprofile: fix cra...
190

6ac6519b9   Robert Richter   oprofile: Free po...
191
  	free_all_tasks();
4c50d9ea9   Robert Richter   cpumask: modifiy ...
192
  	free_cpumask_var(marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
  }
448678a0f   Jan Blunck   d_path: Make get_...
194

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
  /* Optimisation. We can manage without taking the dcookie sem
   * because we cannot reach this code without at least one
   * dcookie user still being registered (namely, the reader
   * of the event buffer). */
71215a75c   Al Viro   constify get_dcoo...
199
  static inline unsigned long fast_get_dcookie(const struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
  {
  	unsigned long cookie;
448678a0f   Jan Blunck   d_path: Make get_...
202

c2452f327   Nick Piggin   shrink struct dentry
203
  	if (path->dentry->d_flags & DCACHE_COOKIE)
448678a0f   Jan Blunck   d_path: Make get_...
204
205
  		return (unsigned long)path->dentry;
  	get_dcookie(path, &cookie);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
  	return cookie;
  }
448678a0f   Jan Blunck   d_path: Make get_...
208

2dd8ad81e   Konstantin Khlebnikov   mm: use mm->exe_f...
209
  /* Look up the dcookie for the task's mm->exe_file,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210
211
212
213
   * which corresponds loosely to "application name". This is
   * not strictly necessary but allows oprofile to associate
   * shared-library samples with particular applications
   */
73185e0a5   Robert Richter   drivers/oprofile:...
214
  static unsigned long get_exec_dcookie(struct mm_struct *mm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
  {
0c0a400d1   John Levon   [PATCH] oprofile:...
216
  	unsigned long cookie = NO_COOKIE;
11163348a   Davidlohr Bueso   oprofile: reduce ...
217
  	struct file *exe_file;
73185e0a5   Robert Richter   drivers/oprofile:...
218

11163348a   Davidlohr Bueso   oprofile: reduce ...
219
220
221
222
223
224
  	if (!mm)
  		goto done;
  
  	exe_file = get_mm_exe_file(mm);
  	if (!exe_file)
  		goto done;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
225

11163348a   Davidlohr Bueso   oprofile: reduce ...
226
227
228
  	cookie = fast_get_dcookie(&exe_file->f_path);
  	fput(exe_file);
  done:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
229
230
231
232
233
234
235
236
  	return cookie;
  }
  
  
  /* Convert the EIP value of a sample into a persistent dentry/offset
   * pair that can then be added to the global event buffer. We make
   * sure to do this lookup before a mm->mmap modification happens so
   * we don't lose track.
11163348a   Davidlohr Bueso   oprofile: reduce ...
237
238
   *
   * The caller must ensure the mm is not nil (ie: not a kernel thread).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
   */
73185e0a5   Robert Richter   drivers/oprofile:...
240
241
  static unsigned long
  lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
  {
0c0a400d1   John Levon   [PATCH] oprofile:...
243
  	unsigned long cookie = NO_COOKIE;
73185e0a5   Robert Richter   drivers/oprofile:...
244
  	struct vm_area_struct *vma;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
245

d8ed45c5d   Michel Lespinasse   mmap locking API:...
246
  	mmap_read_lock(mm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
  	for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
73185e0a5   Robert Richter   drivers/oprofile:...
248

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
249
250
  		if (addr < vma->vm_start || addr >= vma->vm_end)
  			continue;
0c0a400d1   John Levon   [PATCH] oprofile:...
251
  		if (vma->vm_file) {
448678a0f   Jan Blunck   d_path: Make get_...
252
  			cookie = fast_get_dcookie(&vma->vm_file->f_path);
0c0a400d1   John Levon   [PATCH] oprofile:...
253
254
255
256
257
258
  			*offset = (vma->vm_pgoff << PAGE_SHIFT) + addr -
  				vma->vm_start;
  		} else {
  			/* must be an anonymous map */
  			*offset = addr;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
  		break;
  	}
0c0a400d1   John Levon   [PATCH] oprofile:...
261
262
  	if (!vma)
  		cookie = INVALID_COOKIE;
d8ed45c5d   Michel Lespinasse   mmap locking API:...
263
  	mmap_read_unlock(mm);
0c0a400d1   John Levon   [PATCH] oprofile:...
264

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
  	return cookie;
  }
0c0a400d1   John Levon   [PATCH] oprofile:...
267
  static unsigned long last_cookie = INVALID_COOKIE;
73185e0a5   Robert Richter   drivers/oprofile:...
268

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
273
  static void add_cpu_switch(int i)
  {
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(CPU_SWITCH_CODE);
  	add_event_entry(i);
0c0a400d1   John Levon   [PATCH] oprofile:...
274
  	last_cookie = INVALID_COOKIE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
275
276
277
278
279
280
  }
  
  static void add_kernel_ctx_switch(unsigned int in_kernel)
  {
  	add_event_entry(ESCAPE_CODE);
  	if (in_kernel)
73185e0a5   Robert Richter   drivers/oprofile:...
281
  		add_event_entry(KERNEL_ENTER_SWITCH_CODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282
  	else
73185e0a5   Robert Richter   drivers/oprofile:...
283
  		add_event_entry(KERNEL_EXIT_SWITCH_CODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
  }
73185e0a5   Robert Richter   drivers/oprofile:...
285

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
286
  static void
73185e0a5   Robert Richter   drivers/oprofile:...
287
  add_user_ctx_switch(struct task_struct const *task, unsigned long cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
  {
  	add_event_entry(ESCAPE_CODE);
73185e0a5   Robert Richter   drivers/oprofile:...
290
  	add_event_entry(CTX_SWITCH_CODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
292
293
294
295
296
297
  	add_event_entry(task->pid);
  	add_event_entry(cookie);
  	/* Another code for daemon back-compat */
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(CTX_TGID_CODE);
  	add_event_entry(task->tgid);
  }
73185e0a5   Robert Richter   drivers/oprofile:...
298

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
301
302
303
304
  static void add_cookie_switch(unsigned long cookie)
  {
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(COOKIE_SWITCH_CODE);
  	add_event_entry(cookie);
  }
73185e0a5   Robert Richter   drivers/oprofile:...
305

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
306
307
308
309
310
  static void add_trace_begin(void)
  {
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(TRACE_BEGIN_CODE);
  }
1acda878e   Robert Richter   oprofile: use new...
311
  static void add_data(struct op_entry *entry, struct mm_struct *mm)
345c25730   Barry Kasindorf   x86/oprofile: add...
312
  {
1acda878e   Robert Richter   oprofile: use new...
313
314
  	unsigned long code, pc, val;
  	unsigned long cookie;
345c25730   Barry Kasindorf   x86/oprofile: add...
315
  	off_t offset;
345c25730   Barry Kasindorf   x86/oprofile: add...
316

1acda878e   Robert Richter   oprofile: use new...
317
318
319
320
321
  	if (!op_cpu_buffer_get_data(entry, &code))
  		return;
  	if (!op_cpu_buffer_get_data(entry, &pc))
  		return;
  	if (!op_cpu_buffer_get_size(entry))
dbe6e2835   Robert Richter   oprofile: simplif...
322
  		return;
345c25730   Barry Kasindorf   x86/oprofile: add...
323
324
  
  	if (mm) {
d358e75fc   Robert Richter   oprofile: rename ...
325
  		cookie = lookup_dcookie(mm, pc, &offset);
345c25730   Barry Kasindorf   x86/oprofile: add...
326

d358e75fc   Robert Richter   oprofile: rename ...
327
328
329
  		if (cookie == NO_COOKIE)
  			offset = pc;
  		if (cookie == INVALID_COOKIE) {
345c25730   Barry Kasindorf   x86/oprofile: add...
330
  			atomic_inc(&oprofile_stats.sample_lost_no_mapping);
d358e75fc   Robert Richter   oprofile: rename ...
331
  			offset = pc;
345c25730   Barry Kasindorf   x86/oprofile: add...
332
  		}
d358e75fc   Robert Richter   oprofile: rename ...
333
334
335
  		if (cookie != last_cookie) {
  			add_cookie_switch(cookie);
  			last_cookie = cookie;
345c25730   Barry Kasindorf   x86/oprofile: add...
336
337
  		}
  	} else
d358e75fc   Robert Richter   oprofile: rename ...
338
  		offset = pc;
345c25730   Barry Kasindorf   x86/oprofile: add...
339
340
341
342
  
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(code);
  	add_event_entry(offset);	/* Offset from Dcookie */
1acda878e   Robert Richter   oprofile: use new...
343
344
  	while (op_cpu_buffer_get_data(entry, &val))
  		add_event_entry(val);
345c25730   Barry Kasindorf   x86/oprofile: add...
345
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346

6368a1f4d   Robert Richter   oprofile: making ...
347
  static inline void add_sample_entry(unsigned long offset, unsigned long event)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
348
349
350
351
  {
  	add_event_entry(offset);
  	add_event_entry(event);
  }
9741b309b   Robert Richter   oprofile: simplif...
352
353
354
355
356
357
358
  /*
   * Add a sample to the global event buffer. If possible the
   * sample is converted into a persistent dentry/offset pair
   * for later lookup from userspace. Return 0 on failure.
   */
  static int
  add_sample(struct mm_struct *mm, struct op_sample *s, int in_kernel)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
  {
  	unsigned long cookie;
  	off_t offset;
73185e0a5   Robert Richter   drivers/oprofile:...
362

9741b309b   Robert Richter   oprofile: simplif...
363
364
365
366
367
368
369
370
371
372
373
  	if (in_kernel) {
  		add_sample_entry(s->eip, s->event);
  		return 1;
  	}
  
  	/* add userspace sample */
  
  	if (!mm) {
  		atomic_inc(&oprofile_stats.sample_lost_no_mm);
  		return 0;
  	}
73185e0a5   Robert Richter   drivers/oprofile:...
374
  	cookie = lookup_dcookie(mm, s->eip, &offset);
0c0a400d1   John Levon   [PATCH] oprofile:...
375
  	if (cookie == INVALID_COOKIE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
379
380
381
382
383
384
385
386
387
388
  		atomic_inc(&oprofile_stats.sample_lost_no_mapping);
  		return 0;
  	}
  
  	if (cookie != last_cookie) {
  		add_cookie_switch(cookie);
  		last_cookie = cookie;
  	}
  
  	add_sample_entry(offset, s->event);
  
  	return 1;
  }
73185e0a5   Robert Richter   drivers/oprofile:...
389

73185e0a5   Robert Richter   drivers/oprofile:...
390
  static void release_mm(struct mm_struct *mm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
  {
  	if (!mm)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
  	mmput(mm);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
398
399
  static inline int is_code(unsigned long val)
  {
  	return val == ESCAPE_CODE;
  }
73185e0a5   Robert Richter   drivers/oprofile:...
400

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
401

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
405
406
407
408
409
  /* Move tasks along towards death. Any tasks on dead_tasks
   * will definitely have no remaining references in any
   * CPU buffers at this point, because we use two lists,
   * and to have reached the list, it must have gone through
   * one full sync already.
   */
  static void process_task_mortuary(void)
  {
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
410
411
  	unsigned long flags;
  	LIST_HEAD(local_dead_tasks);
73185e0a5   Robert Richter   drivers/oprofile:...
412
413
  	struct task_struct *task;
  	struct task_struct *ttask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
414

4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
415
  	spin_lock_irqsave(&task_mortuary, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416

4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
417
418
  	list_splice_init(&dead_tasks, &local_dead_tasks);
  	list_splice_init(&dying_tasks, &dead_tasks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
419

4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
420
421
422
  	spin_unlock_irqrestore(&task_mortuary, flags);
  
  	list_for_each_entry_safe(task, ttask, &local_dead_tasks, tasks) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
423
  		list_del(&task->tasks);
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
424
  		free_task(task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426
427
428
429
430
431
  }
  
  
  static void mark_done(int cpu)
  {
  	int i;
f7df8ed16   Rusty Russell   cpumask: convert ...
432
  	cpumask_set_cpu(cpu, marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
433
434
  
  	for_each_online_cpu(i) {
f7df8ed16   Rusty Russell   cpumask: convert ...
435
  		if (!cpumask_test_cpu(i, marked_cpus))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
437
438
439
440
441
442
  			return;
  	}
  
  	/* All CPUs have been processed at least once,
  	 * we can process the mortuary once
  	 */
  	process_task_mortuary();
f7df8ed16   Rusty Russell   cpumask: convert ...
443
  	cpumask_clear(marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  }
  
  
  /* FIXME: this is not sufficient if we implement syscall barrier backtrace
   * traversal, the code switch to sb_sample_start at first kernel enter/exit
   * switch so we need a fifth state and some special handling in sync_buffer()
   */
  typedef enum {
  	sb_bt_ignore = -2,
  	sb_buffer_start,
  	sb_bt_start,
  	sb_sample_start,
  } sync_buffer_state;
  
  /* Sync one of the CPU's buffers into the global event buffer.
   * Here we need to go through each batch of samples punctuated
c1e8d7c6a   Michel Lespinasse   mmap locking API:...
460
   * by context switch notes, taking the task's mmap_lock and doing
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
461
462
463
464
465
   * lookup in task->mm->mmap to convert EIP into dcookie/offset
   * value.
   */
  void sync_buffer(int cpu)
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
466
  	struct mm_struct *mm = NULL;
fd7826d56   Robert Richter   oprofile: impleme...
467
  	struct mm_struct *oldmm;
bd7dc46f7   Robert Richter   oprofile: add op_...
468
  	unsigned long val;
73185e0a5   Robert Richter   drivers/oprofile:...
469
  	struct task_struct *new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
470
471
  	unsigned long cookie = 0;
  	int in_kernel = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
472
  	sync_buffer_state state = sb_buffer_start;
9b1f26116   Barry Kasindorf   OProfile: Fix buf...
473
  	unsigned int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
474
  	unsigned long available;
ae735e996   Robert Richter   oprofile: rework ...
475
  	unsigned long flags;
2d87b14cf   Robert Richter   oprofile: modify ...
476
477
  	struct op_entry entry;
  	struct op_sample *sample;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478

59cc185ad   Markus Armbruster   [PATCH] oprofile:...
479
  	mutex_lock(&buffer_mutex);
73185e0a5   Robert Richter   drivers/oprofile:...
480

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
  	add_cpu_switch(cpu);
6d2c53f3c   Robert Richter   oprofile: rename ...
482
483
  	op_cpu_buffer_reset(cpu);
  	available = op_cpu_buffer_entries(cpu);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
  
  	for (i = 0; i < available; ++i) {
2d87b14cf   Robert Richter   oprofile: modify ...
486
487
  		sample = op_cpu_buffer_read_entry(&entry, cpu);
  		if (!sample)
6dad828b7   Robert Richter   oprofile: port to...
488
  			break;
73185e0a5   Robert Richter   drivers/oprofile:...
489

2d87b14cf   Robert Richter   oprofile: modify ...
490
  		if (is_code(sample->eip)) {
ae735e996   Robert Richter   oprofile: rework ...
491
492
493
494
495
496
  			flags = sample->event;
  			if (flags & TRACE_BEGIN) {
  				state = sb_bt_start;
  				add_trace_begin();
  			}
  			if (flags & KERNEL_CTX_SWITCH) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497
  				/* kernel/userspace switch */
ae735e996   Robert Richter   oprofile: rework ...
498
  				in_kernel = flags & IS_KERNEL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
499
500
  				if (state == sb_buffer_start)
  					state = sb_sample_start;
ae735e996   Robert Richter   oprofile: rework ...
501
502
  				add_kernel_ctx_switch(flags & IS_KERNEL);
  			}
bd7dc46f7   Robert Richter   oprofile: add op_...
503
504
  			if (flags & USER_CTX_SWITCH
  			    && op_cpu_buffer_get_data(&entry, &val)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
  				/* userspace context switch */
bd7dc46f7   Robert Richter   oprofile: add op_...
506
  				new = (struct task_struct *)val;
fd7826d56   Robert Richter   oprofile: impleme...
507
  				oldmm = mm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
508
  				release_mm(oldmm);
11163348a   Davidlohr Bueso   oprofile: reduce ...
509
  				mm = get_task_mm(new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
510
511
512
  				if (mm != oldmm)
  					cookie = get_exec_dcookie(mm);
  				add_user_ctx_switch(new, cookie);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
513
  			}
1acda878e   Robert Richter   oprofile: use new...
514
515
  			if (op_cpu_buffer_get_size(&entry))
  				add_data(&entry, mm);
317f33bce   Robert Richter   oprofile: simplif...
516
517
518
519
520
521
  			continue;
  		}
  
  		if (state < sb_bt_start)
  			/* ignore sample */
  			continue;
2d87b14cf   Robert Richter   oprofile: modify ...
522
  		if (add_sample(mm, sample, in_kernel))
317f33bce   Robert Richter   oprofile: simplif...
523
524
525
526
527
528
  			continue;
  
  		/* ignore backtraces if failed to add a sample */
  		if (state == sb_bt_start) {
  			state = sb_bt_ignore;
  			atomic_inc(&oprofile_stats.bt_lost_no_mapping);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
530
531
532
533
  	}
  	release_mm(mm);
  
  	mark_done(cpu);
59cc185ad   Markus Armbruster   [PATCH] oprofile:...
534
  	mutex_unlock(&buffer_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
535
  }
a5598ca0d   Carl Love   powerpc/oprofile:...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  
  /* The function can be used to add a buffer worth of data directly to
   * the kernel buffer. The buffer is assumed to be a circular buffer.
   * Take the entries from index start and end at index end, wrapping
   * at max_entries.
   */
  void oprofile_put_buff(unsigned long *buf, unsigned int start,
  		       unsigned int stop, unsigned int max)
  {
  	int i;
  
  	i = start;
  
  	mutex_lock(&buffer_mutex);
  	while (i != stop) {
  		add_event_entry(buf[i++]);
  
  		if (i >= max)
  			i = 0;
  	}
  
  	mutex_unlock(&buffer_mutex);
  }