Blame view

drivers/oprofile/buffer_sync.c 13.5 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
23
24
25
26
27
28
29
30
   *
   * 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.
   */
  
  #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>
5a0e3ad6a   Tejun Heo   include cleanup: ...
33
  #include <linux/gfp.h>
1474855d0   Bob Nelson   [CELL] oprofile: ...
34

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
  static LIST_HEAD(dying_tasks);
  static LIST_HEAD(dead_tasks);
f7df8ed16   Rusty Russell   cpumask: convert ...
42
  static cpumask_var_t marked_cpus;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
43
44
  static DEFINE_SPINLOCK(task_mortuary);
  static void process_task_mortuary(void);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
  /* 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 ...
49
50
   * 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
51
   */
73185e0a5   Robert Richter   drivers/oprofile:...
52
53
  static int
  task_free_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  {
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
55
  	unsigned long flags;
73185e0a5   Robert Richter   drivers/oprofile:...
56
  	struct task_struct *task = data;
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
57
  	spin_lock_irqsave(&task_mortuary, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
58
  	list_add(&task->tasks, &dying_tasks);
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
59
  	spin_unlock_irqrestore(&task_mortuary, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
61
62
63
64
65
66
  	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:...
67
68
  static int
  task_exit_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
  {
  	/* 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...
73
  	sync_buffer(raw_smp_processor_id());
73185e0a5   Robert Richter   drivers/oprofile:...
74
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
77
78
79
80
81
82
  }
  
  
  /* 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:...
83
84
  static int
  munmap_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  {
  	unsigned long addr = (unsigned long)data;
73185e0a5   Robert Richter   drivers/oprofile:...
87
88
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *mpnt;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
89
90
91
92
93
94
95
96
97
  
  	down_read(&mm->mmap_sem);
  
  	mpnt = find_vma(mm, addr);
  	if (mpnt && mpnt->vm_file && (mpnt->vm_flags & VM_EXEC)) {
  		up_read(&mm->mmap_sem);
  		/* 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...
98
  		sync_buffer(raw_smp_processor_id());
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
100
101
102
103
104
  		return 0;
  	}
  
  	up_read(&mm->mmap_sem);
  	return 0;
  }
73185e0a5   Robert Richter   drivers/oprofile:...
105

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
107
108
  /* 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:...
109
110
  static int
  module_load_notify(struct notifier_block *self, unsigned long val, void *data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
112
113
114
115
116
  {
  #ifdef CONFIG_MODULES
  	if (val != MODULE_STATE_COMING)
  		return 0;
  
  	/* FIXME: should we process all CPU buffers ? */
59cc185ad   Markus Armbruster   [PATCH] oprofile:...
117
  	mutex_lock(&buffer_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
119
  	add_event_entry(ESCAPE_CODE);
  	add_event_entry(MODULE_LOADED_CODE);
59cc185ad   Markus Armbruster   [PATCH] oprofile:...
120
  	mutex_unlock(&buffer_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
  #endif
  	return 0;
  }
73185e0a5   Robert Richter   drivers/oprofile:...
124

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  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...
140
141
142
143
144
145
  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
146
147
148
  int sync_start(void)
  {
  	int err;
79f559977   Li Zefan   cpumask: use zall...
149
  	if (!zalloc_cpumask_var(&marked_cpus, GFP_KERNEL))
4c50d9ea9   Robert Richter   cpumask: modifiy ...
150
  		return -ENOMEM;
4c50d9ea9   Robert Richter   cpumask: modifiy ...
151

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152
153
154
155
156
157
158
159
160
161
162
163
  	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...
164
  	start_cpu_work();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
167
168
169
170
171
172
  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...
173
  	free_all_tasks();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
  out1:
4c50d9ea9   Robert Richter   cpumask: modifiy ...
175
  	free_cpumask_var(marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
178
179
180
181
  	goto out;
  }
  
  
  void sync_stop(void)
  {
750d857c6   Robert Richter   oprofile: fix cra...
182
  	end_cpu_work();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
  	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...
187
  	barrier();			/* do all of the above first */
3d7851b3c   Tejun Heo   oprofile: Remove ...
188
  	flush_cpu_work();
750d857c6   Robert Richter   oprofile: fix cra...
189

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
195
196
197
  /* 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). */
448678a0f   Jan Blunck   d_path: Make get_...
198
  static inline unsigned long fast_get_dcookie(struct path *path)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
  {
  	unsigned long cookie;
448678a0f   Jan Blunck   d_path: Make get_...
201

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

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
  /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
   * 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:...
213
  static unsigned long get_exec_dcookie(struct mm_struct *mm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
  {
0c0a400d1   John Levon   [PATCH] oprofile:...
215
  	unsigned long cookie = NO_COOKIE;
73185e0a5   Robert Richter   drivers/oprofile:...
216
  	struct vm_area_struct *vma;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
  	if (!mm)
  		goto out;
73185e0a5   Robert Richter   drivers/oprofile:...
219

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
221
222
223
224
  	for (vma = mm->mmap; vma; vma = vma->vm_next) {
  		if (!vma->vm_file)
  			continue;
  		if (!(vma->vm_flags & VM_EXECUTABLE))
  			continue;
448678a0f   Jan Blunck   d_path: Make get_...
225
  		cookie = fast_get_dcookie(&vma->vm_file->f_path);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
228
229
230
231
232
233
234
235
236
237
238
  		break;
  	}
  
  out:
  	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.
   */
73185e0a5   Robert Richter   drivers/oprofile:...
239
240
  static unsigned long
  lookup_dcookie(struct mm_struct *mm, unsigned long addr, off_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
241
  {
0c0a400d1   John Levon   [PATCH] oprofile:...
242
  	unsigned long cookie = NO_COOKIE;
73185e0a5   Robert Richter   drivers/oprofile:...
243
  	struct vm_area_struct *vma;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
  
  	for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
73185e0a5   Robert Richter   drivers/oprofile:...
246

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
  		if (addr < vma->vm_start || addr >= vma->vm_end)
  			continue;
0c0a400d1   John Levon   [PATCH] oprofile:...
249
  		if (vma->vm_file) {
448678a0f   Jan Blunck   d_path: Make get_...
250
  			cookie = fast_get_dcookie(&vma->vm_file->f_path);
0c0a400d1   John Levon   [PATCH] oprofile:...
251
252
253
254
255
256
  			*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
257
258
  		break;
  	}
0c0a400d1   John Levon   [PATCH] oprofile:...
259
260
  	if (!vma)
  		cookie = INVALID_COOKIE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
261
262
  	return cookie;
  }
0c0a400d1   John Levon   [PATCH] oprofile:...
263
  static unsigned long last_cookie = INVALID_COOKIE;
73185e0a5   Robert Richter   drivers/oprofile:...
264

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
266
267
268
269
  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:...
270
  	last_cookie = INVALID_COOKIE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
  }
  
  static void add_kernel_ctx_switch(unsigned int in_kernel)
  {
  	add_event_entry(ESCAPE_CODE);
  	if (in_kernel)
73185e0a5   Robert Richter   drivers/oprofile:...
277
  		add_event_entry(KERNEL_ENTER_SWITCH_CODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
  	else
73185e0a5   Robert Richter   drivers/oprofile:...
279
  		add_event_entry(KERNEL_EXIT_SWITCH_CODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
280
  }
73185e0a5   Robert Richter   drivers/oprofile:...
281

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
282
  static void
73185e0a5   Robert Richter   drivers/oprofile:...
283
  add_user_ctx_switch(struct task_struct const *task, unsigned long cookie)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
284
285
  {
  	add_event_entry(ESCAPE_CODE);
73185e0a5   Robert Richter   drivers/oprofile:...
286
  	add_event_entry(CTX_SWITCH_CODE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
293
  	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:...
294

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
298
299
300
  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:...
301

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

1acda878e   Robert Richter   oprofile: use new...
313
314
315
316
317
  	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...
318
  		return;
345c25730   Barry Kasindorf   x86/oprofile: add...
319
320
  
  	if (mm) {
d358e75fc   Robert Richter   oprofile: rename ...
321
  		cookie = lookup_dcookie(mm, pc, &offset);
345c25730   Barry Kasindorf   x86/oprofile: add...
322

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

6368a1f4d   Robert Richter   oprofile: making ...
343
  static inline void add_sample_entry(unsigned long offset, unsigned long event)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
345
346
347
  {
  	add_event_entry(offset);
  	add_event_entry(event);
  }
9741b309b   Robert Richter   oprofile: simplif...
348
349
350
351
352
353
354
  /*
   * 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
355
356
357
  {
  	unsigned long cookie;
  	off_t offset;
73185e0a5   Robert Richter   drivers/oprofile:...
358

9741b309b   Robert Richter   oprofile: simplif...
359
360
361
362
363
364
365
366
367
368
369
  	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:...
370
  	cookie = lookup_dcookie(mm, s->eip, &offset);
0c0a400d1   John Levon   [PATCH] oprofile:...
371
  	if (cookie == INVALID_COOKIE) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
373
374
375
376
377
378
379
380
381
382
383
384
  		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:...
385

73185e0a5   Robert Richter   drivers/oprofile:...
386
  static void release_mm(struct mm_struct *mm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
388
389
390
391
392
  {
  	if (!mm)
  		return;
  	up_read(&mm->mmap_sem);
  	mmput(mm);
  }
73185e0a5   Robert Richter   drivers/oprofile:...
393
  static struct mm_struct *take_tasks_mm(struct task_struct *task)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
  {
73185e0a5   Robert Richter   drivers/oprofile:...
395
  	struct mm_struct *mm = get_task_mm(task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
396
397
398
399
400
401
402
403
404
405
  	if (mm)
  		down_read(&mm->mmap_sem);
  	return mm;
  }
  
  
  static inline int is_code(unsigned long val)
  {
  	return val == ESCAPE_CODE;
  }
73185e0a5   Robert Richter   drivers/oprofile:...
406

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
408
409
410
411
412
413
414
415
  /* 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 ...
416
417
  	unsigned long flags;
  	LIST_HEAD(local_dead_tasks);
73185e0a5   Robert Richter   drivers/oprofile:...
418
419
  	struct task_struct *task;
  	struct task_struct *ttask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420

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

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

4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
426
427
428
  	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
429
  		list_del(&task->tasks);
4369ef3c3   Paul E. McKenney   [PATCH] Make RCU ...
430
  		free_task(task);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
431
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
434
435
436
437
  }
  
  
  static void mark_done(int cpu)
  {
  	int i;
f7df8ed16   Rusty Russell   cpumask: convert ...
438
  	cpumask_set_cpu(cpu, marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
439
440
  
  	for_each_online_cpu(i) {
f7df8ed16   Rusty Russell   cpumask: convert ...
441
  		if (!cpumask_test_cpu(i, marked_cpus))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
442
443
444
445
446
447
448
  			return;
  	}
  
  	/* All CPUs have been processed at least once,
  	 * we can process the mortuary once
  	 */
  	process_task_mortuary();
f7df8ed16   Rusty Russell   cpumask: convert ...
449
  	cpumask_clear(marked_cpus);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  }
  
  
  /* 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
   * by context switch notes, taking the task's mmap_sem and doing
   * 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
472
  	struct mm_struct *mm = NULL;
fd7826d56   Robert Richter   oprofile: impleme...
473
  	struct mm_struct *oldmm;
bd7dc46f7   Robert Richter   oprofile: add op_...
474
  	unsigned long val;
73185e0a5   Robert Richter   drivers/oprofile:...
475
  	struct task_struct *new;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
  	unsigned long cookie = 0;
  	int in_kernel = 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
478
  	sync_buffer_state state = sb_buffer_start;
9b1f26116   Barry Kasindorf   OProfile: Fix buf...
479
  	unsigned int i;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
480
  	unsigned long available;
ae735e996   Robert Richter   oprofile: rework ...
481
  	unsigned long flags;
2d87b14cf   Robert Richter   oprofile: modify ...
482
483
  	struct op_entry entry;
  	struct op_sample *sample;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484

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

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

2d87b14cf   Robert Richter   oprofile: modify ...
496
  		if (is_code(sample->eip)) {
ae735e996   Robert Richter   oprofile: rework ...
497
498
499
500
501
502
  			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
503
  				/* kernel/userspace switch */
ae735e996   Robert Richter   oprofile: rework ...
504
  				in_kernel = flags & IS_KERNEL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
  				if (state == sb_buffer_start)
  					state = sb_sample_start;
ae735e996   Robert Richter   oprofile: rework ...
507
508
  				add_kernel_ctx_switch(flags & IS_KERNEL);
  			}
bd7dc46f7   Robert Richter   oprofile: add op_...
509
510
  			if (flags & USER_CTX_SWITCH
  			    && op_cpu_buffer_get_data(&entry, &val)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
  				/* userspace context switch */
bd7dc46f7   Robert Richter   oprofile: add op_...
512
  				new = (struct task_struct *)val;
fd7826d56   Robert Richter   oprofile: impleme...
513
  				oldmm = mm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
515
516
517
518
  				release_mm(oldmm);
  				mm = take_tasks_mm(new);
  				if (mm != oldmm)
  					cookie = get_exec_dcookie(mm);
  				add_user_ctx_switch(new, cookie);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
  			}
1acda878e   Robert Richter   oprofile: use new...
520
521
  			if (op_cpu_buffer_get_size(&entry))
  				add_data(&entry, mm);
317f33bce   Robert Richter   oprofile: simplif...
522
523
524
525
526
527
  			continue;
  		}
  
  		if (state < sb_bt_start)
  			/* ignore sample */
  			continue;
2d87b14cf   Robert Richter   oprofile: modify ...
528
  		if (add_sample(mm, sample, in_kernel))
317f33bce   Robert Richter   oprofile: simplif...
529
530
531
532
533
534
  			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
535
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
536
537
538
539
  	}
  	release_mm(mm);
  
  	mark_done(cpu);
59cc185ad   Markus Armbruster   [PATCH] oprofile:...
540
  	mutex_unlock(&buffer_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
  }
a5598ca0d   Carl Love   powerpc/oprofile:...
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
  
  /* 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);
  }