Fix rmmod/read/write races in /proc entries
[pv_ops_mirror.git] / drivers / oprofile / buffer_sync.c
blobedd6de9957260abb4f631b22090903d916fc8b34
1 /**
2 * @file buffer_sync.c
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
7 * @author John Levon <levon@movementarian.org>
9 * This is the core of the buffer management. Each
10 * CPU buffer is processed and entered into the
11 * global event buffer. Such processing is necessary
12 * in several circumstances, mentioned below.
14 * The processing does the job of converting the
15 * transitory EIP value into a persistent dentry/offset
16 * value that the profiler can record at its leisure.
18 * See fs/dcookies.c for a description of the dentry/offset
19 * objects.
22 #include <linux/mm.h>
23 #include <linux/workqueue.h>
24 #include <linux/notifier.h>
25 #include <linux/dcookies.h>
26 #include <linux/profile.h>
27 #include <linux/module.h>
28 #include <linux/fs.h>
29 #include <linux/sched.h>
31 #include "oprofile_stats.h"
32 #include "event_buffer.h"
33 #include "cpu_buffer.h"
34 #include "buffer_sync.h"
36 static LIST_HEAD(dying_tasks);
37 static LIST_HEAD(dead_tasks);
38 static cpumask_t marked_cpus = CPU_MASK_NONE;
39 static DEFINE_SPINLOCK(task_mortuary);
40 static void process_task_mortuary(void);
43 /* Take ownership of the task struct and place it on the
44 * list for processing. Only after two full buffer syncs
45 * does the task eventually get freed, because by then
46 * we are sure we will not reference it again.
47 * Can be invoked from softirq via RCU callback due to
48 * call_rcu() of the task struct, hence the _irqsave.
50 static int task_free_notify(struct notifier_block * self, unsigned long val, void * data)
52 unsigned long flags;
53 struct task_struct * task = data;
54 spin_lock_irqsave(&task_mortuary, flags);
55 list_add(&task->tasks, &dying_tasks);
56 spin_unlock_irqrestore(&task_mortuary, flags);
57 return NOTIFY_OK;
61 /* The task is on its way out. A sync of the buffer means we can catch
62 * any remaining samples for this task.
64 static int task_exit_notify(struct notifier_block * self, unsigned long val, void * data)
66 /* To avoid latency problems, we only process the current CPU,
67 * hoping that most samples for the task are on this CPU
69 sync_buffer(raw_smp_processor_id());
70 return 0;
74 /* The task is about to try a do_munmap(). We peek at what it's going to
75 * do, and if it's an executable region, process the samples first, so
76 * we don't lose any. This does not have to be exact, it's a QoI issue
77 * only.
79 static int munmap_notify(struct notifier_block * self, unsigned long val, void * data)
81 unsigned long addr = (unsigned long)data;
82 struct mm_struct * mm = current->mm;
83 struct vm_area_struct * mpnt;
85 down_read(&mm->mmap_sem);
87 mpnt = find_vma(mm, addr);
88 if (mpnt && mpnt->vm_file && (mpnt->vm_flags & VM_EXEC)) {
89 up_read(&mm->mmap_sem);
90 /* To avoid latency problems, we only process the current CPU,
91 * hoping that most samples for the task are on this CPU
93 sync_buffer(raw_smp_processor_id());
94 return 0;
97 up_read(&mm->mmap_sem);
98 return 0;
102 /* We need to be told about new modules so we don't attribute to a previously
103 * loaded module, or drop the samples on the floor.
105 static int module_load_notify(struct notifier_block * self, unsigned long val, void * data)
107 #ifdef CONFIG_MODULES
108 if (val != MODULE_STATE_COMING)
109 return 0;
111 /* FIXME: should we process all CPU buffers ? */
112 mutex_lock(&buffer_mutex);
113 add_event_entry(ESCAPE_CODE);
114 add_event_entry(MODULE_LOADED_CODE);
115 mutex_unlock(&buffer_mutex);
116 #endif
117 return 0;
121 static struct notifier_block task_free_nb = {
122 .notifier_call = task_free_notify,
125 static struct notifier_block task_exit_nb = {
126 .notifier_call = task_exit_notify,
129 static struct notifier_block munmap_nb = {
130 .notifier_call = munmap_notify,
133 static struct notifier_block module_load_nb = {
134 .notifier_call = module_load_notify,
138 static void end_sync(void)
140 end_cpu_work();
141 /* make sure we don't leak task structs */
142 process_task_mortuary();
143 process_task_mortuary();
147 int sync_start(void)
149 int err;
151 start_cpu_work();
153 err = task_handoff_register(&task_free_nb);
154 if (err)
155 goto out1;
156 err = profile_event_register(PROFILE_TASK_EXIT, &task_exit_nb);
157 if (err)
158 goto out2;
159 err = profile_event_register(PROFILE_MUNMAP, &munmap_nb);
160 if (err)
161 goto out3;
162 err = register_module_notifier(&module_load_nb);
163 if (err)
164 goto out4;
166 out:
167 return err;
168 out4:
169 profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
170 out3:
171 profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
172 out2:
173 task_handoff_unregister(&task_free_nb);
174 out1:
175 end_sync();
176 goto out;
180 void sync_stop(void)
182 unregister_module_notifier(&module_load_nb);
183 profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
184 profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
185 task_handoff_unregister(&task_free_nb);
186 end_sync();
190 /* Optimisation. We can manage without taking the dcookie sem
191 * because we cannot reach this code without at least one
192 * dcookie user still being registered (namely, the reader
193 * of the event buffer). */
194 static inline unsigned long fast_get_dcookie(struct dentry * dentry,
195 struct vfsmount * vfsmnt)
197 unsigned long cookie;
199 if (dentry->d_cookie)
200 return (unsigned long)dentry;
201 get_dcookie(dentry, vfsmnt, &cookie);
202 return cookie;
206 /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
207 * which corresponds loosely to "application name". This is
208 * not strictly necessary but allows oprofile to associate
209 * shared-library samples with particular applications
211 static unsigned long get_exec_dcookie(struct mm_struct * mm)
213 unsigned long cookie = NO_COOKIE;
214 struct vm_area_struct * vma;
216 if (!mm)
217 goto out;
219 for (vma = mm->mmap; vma; vma = vma->vm_next) {
220 if (!vma->vm_file)
221 continue;
222 if (!(vma->vm_flags & VM_EXECUTABLE))
223 continue;
224 cookie = fast_get_dcookie(vma->vm_file->f_path.dentry,
225 vma->vm_file->f_path.mnt);
226 break;
229 out:
230 return cookie;
234 /* Convert the EIP value of a sample into a persistent dentry/offset
235 * pair that can then be added to the global event buffer. We make
236 * sure to do this lookup before a mm->mmap modification happens so
237 * we don't lose track.
239 static unsigned long lookup_dcookie(struct mm_struct * mm, unsigned long addr, off_t * offset)
241 unsigned long cookie = NO_COOKIE;
242 struct vm_area_struct * vma;
244 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
246 if (addr < vma->vm_start || addr >= vma->vm_end)
247 continue;
249 if (vma->vm_file) {
250 cookie = fast_get_dcookie(vma->vm_file->f_path.dentry,
251 vma->vm_file->f_path.mnt);
252 *offset = (vma->vm_pgoff << PAGE_SHIFT) + addr -
253 vma->vm_start;
254 } else {
255 /* must be an anonymous map */
256 *offset = addr;
259 break;
262 if (!vma)
263 cookie = INVALID_COOKIE;
265 return cookie;
269 static unsigned long last_cookie = INVALID_COOKIE;
271 static void add_cpu_switch(int i)
273 add_event_entry(ESCAPE_CODE);
274 add_event_entry(CPU_SWITCH_CODE);
275 add_event_entry(i);
276 last_cookie = INVALID_COOKIE;
279 static void add_kernel_ctx_switch(unsigned int in_kernel)
281 add_event_entry(ESCAPE_CODE);
282 if (in_kernel)
283 add_event_entry(KERNEL_ENTER_SWITCH_CODE);
284 else
285 add_event_entry(KERNEL_EXIT_SWITCH_CODE);
288 static void
289 add_user_ctx_switch(struct task_struct const * task, unsigned long cookie)
291 add_event_entry(ESCAPE_CODE);
292 add_event_entry(CTX_SWITCH_CODE);
293 add_event_entry(task->pid);
294 add_event_entry(cookie);
295 /* Another code for daemon back-compat */
296 add_event_entry(ESCAPE_CODE);
297 add_event_entry(CTX_TGID_CODE);
298 add_event_entry(task->tgid);
302 static void add_cookie_switch(unsigned long cookie)
304 add_event_entry(ESCAPE_CODE);
305 add_event_entry(COOKIE_SWITCH_CODE);
306 add_event_entry(cookie);
310 static void add_trace_begin(void)
312 add_event_entry(ESCAPE_CODE);
313 add_event_entry(TRACE_BEGIN_CODE);
317 static void add_sample_entry(unsigned long offset, unsigned long event)
319 add_event_entry(offset);
320 add_event_entry(event);
324 static int add_us_sample(struct mm_struct * mm, struct op_sample * s)
326 unsigned long cookie;
327 off_t offset;
329 cookie = lookup_dcookie(mm, s->eip, &offset);
331 if (cookie == INVALID_COOKIE) {
332 atomic_inc(&oprofile_stats.sample_lost_no_mapping);
333 return 0;
336 if (cookie != last_cookie) {
337 add_cookie_switch(cookie);
338 last_cookie = cookie;
341 add_sample_entry(offset, s->event);
343 return 1;
347 /* Add a sample to the global event buffer. If possible the
348 * sample is converted into a persistent dentry/offset pair
349 * for later lookup from userspace.
351 static int
352 add_sample(struct mm_struct * mm, struct op_sample * s, int in_kernel)
354 if (in_kernel) {
355 add_sample_entry(s->eip, s->event);
356 return 1;
357 } else if (mm) {
358 return add_us_sample(mm, s);
359 } else {
360 atomic_inc(&oprofile_stats.sample_lost_no_mm);
362 return 0;
366 static void release_mm(struct mm_struct * mm)
368 if (!mm)
369 return;
370 up_read(&mm->mmap_sem);
371 mmput(mm);
375 static struct mm_struct * take_tasks_mm(struct task_struct * task)
377 struct mm_struct * mm = get_task_mm(task);
378 if (mm)
379 down_read(&mm->mmap_sem);
380 return mm;
384 static inline int is_code(unsigned long val)
386 return val == ESCAPE_CODE;
390 /* "acquire" as many cpu buffer slots as we can */
391 static unsigned long get_slots(struct oprofile_cpu_buffer * b)
393 unsigned long head = b->head_pos;
394 unsigned long tail = b->tail_pos;
397 * Subtle. This resets the persistent last_task
398 * and in_kernel values used for switching notes.
399 * BUT, there is a small window between reading
400 * head_pos, and this call, that means samples
401 * can appear at the new head position, but not
402 * be prefixed with the notes for switching
403 * kernel mode or a task switch. This small hole
404 * can lead to mis-attribution or samples where
405 * we don't know if it's in the kernel or not,
406 * at the start of an event buffer.
408 cpu_buffer_reset(b);
410 if (head >= tail)
411 return head - tail;
413 return head + (b->buffer_size - tail);
417 static void increment_tail(struct oprofile_cpu_buffer * b)
419 unsigned long new_tail = b->tail_pos + 1;
421 rmb();
423 if (new_tail < b->buffer_size)
424 b->tail_pos = new_tail;
425 else
426 b->tail_pos = 0;
430 /* Move tasks along towards death. Any tasks on dead_tasks
431 * will definitely have no remaining references in any
432 * CPU buffers at this point, because we use two lists,
433 * and to have reached the list, it must have gone through
434 * one full sync already.
436 static void process_task_mortuary(void)
438 unsigned long flags;
439 LIST_HEAD(local_dead_tasks);
440 struct task_struct * task;
441 struct task_struct * ttask;
443 spin_lock_irqsave(&task_mortuary, flags);
445 list_splice_init(&dead_tasks, &local_dead_tasks);
446 list_splice_init(&dying_tasks, &dead_tasks);
448 spin_unlock_irqrestore(&task_mortuary, flags);
450 list_for_each_entry_safe(task, ttask, &local_dead_tasks, tasks) {
451 list_del(&task->tasks);
452 free_task(task);
457 static void mark_done(int cpu)
459 int i;
461 cpu_set(cpu, marked_cpus);
463 for_each_online_cpu(i) {
464 if (!cpu_isset(i, marked_cpus))
465 return;
468 /* All CPUs have been processed at least once,
469 * we can process the mortuary once
471 process_task_mortuary();
473 cpus_clear(marked_cpus);
477 /* FIXME: this is not sufficient if we implement syscall barrier backtrace
478 * traversal, the code switch to sb_sample_start at first kernel enter/exit
479 * switch so we need a fifth state and some special handling in sync_buffer()
481 typedef enum {
482 sb_bt_ignore = -2,
483 sb_buffer_start,
484 sb_bt_start,
485 sb_sample_start,
486 } sync_buffer_state;
488 /* Sync one of the CPU's buffers into the global event buffer.
489 * Here we need to go through each batch of samples punctuated
490 * by context switch notes, taking the task's mmap_sem and doing
491 * lookup in task->mm->mmap to convert EIP into dcookie/offset
492 * value.
494 void sync_buffer(int cpu)
496 struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[cpu];
497 struct mm_struct *mm = NULL;
498 struct task_struct * new;
499 unsigned long cookie = 0;
500 int in_kernel = 1;
501 unsigned int i;
502 sync_buffer_state state = sb_buffer_start;
503 unsigned long available;
505 mutex_lock(&buffer_mutex);
507 add_cpu_switch(cpu);
509 /* Remember, only we can modify tail_pos */
511 available = get_slots(cpu_buf);
513 for (i = 0; i < available; ++i) {
514 struct op_sample * s = &cpu_buf->buffer[cpu_buf->tail_pos];
516 if (is_code(s->eip)) {
517 if (s->event <= CPU_IS_KERNEL) {
518 /* kernel/userspace switch */
519 in_kernel = s->event;
520 if (state == sb_buffer_start)
521 state = sb_sample_start;
522 add_kernel_ctx_switch(s->event);
523 } else if (s->event == CPU_TRACE_BEGIN) {
524 state = sb_bt_start;
525 add_trace_begin();
526 } else {
527 struct mm_struct * oldmm = mm;
529 /* userspace context switch */
530 new = (struct task_struct *)s->event;
532 release_mm(oldmm);
533 mm = take_tasks_mm(new);
534 if (mm != oldmm)
535 cookie = get_exec_dcookie(mm);
536 add_user_ctx_switch(new, cookie);
538 } else {
539 if (state >= sb_bt_start &&
540 !add_sample(mm, s, in_kernel)) {
541 if (state == sb_bt_start) {
542 state = sb_bt_ignore;
543 atomic_inc(&oprofile_stats.bt_lost_no_mapping);
548 increment_tail(cpu_buf);
550 release_mm(mm);
552 mark_done(cpu);
554 mutex_unlock(&buffer_mutex);