ia64/linux-2.6.18-xen.hg

view drivers/oprofile/buffer_sync.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

Signed-off-by: Jan Beulich <jbeulich@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jun 04 10:32:57 2009 +0100 (2009-06-04)
parents 132f24200f4c
children
line source
1 /**
2 * @file buffer_sync.c
3 *
4 * @remark Copyright 2002 OProfile authors
5 * @remark Read the file COPYING
6 *
7 * @author John Levon <levon@movementarian.org>
8 *
9 * Modified by Aravind Menon for Xen
10 * These modifications are:
11 * Copyright (C) 2005 Hewlett-Packard Co.
12 *
13 * This is the core of the buffer management. Each
14 * CPU buffer is processed and entered into the
15 * global event buffer. Such processing is necessary
16 * in several circumstances, mentioned below.
17 *
18 * The processing does the job of converting the
19 * transitory EIP value into a persistent dentry/offset
20 * value that the profiler can record at its leisure.
21 *
22 * See fs/dcookies.c for a description of the dentry/offset
23 * objects.
24 */
26 #include <linux/mm.h>
27 #include <linux/workqueue.h>
28 #include <linux/notifier.h>
29 #include <linux/dcookies.h>
30 #include <linux/profile.h>
31 #include <linux/module.h>
32 #include <linux/fs.h>
34 #include "oprofile_stats.h"
35 #include "event_buffer.h"
36 #include "cpu_buffer.h"
37 #include "buffer_sync.h"
39 static LIST_HEAD(dying_tasks);
40 static LIST_HEAD(dead_tasks);
41 static cpumask_t marked_cpus = CPU_MASK_NONE;
42 static DEFINE_SPINLOCK(task_mortuary);
43 static void process_task_mortuary(void);
45 static int cpu_current_domain[NR_CPUS];
47 /* Take ownership of the task struct and place it on the
48 * list for processing. Only after two full buffer syncs
49 * does the task eventually get freed, because by then
50 * we are sure we will not reference it again.
51 * Can be invoked from softirq via RCU callback due to
52 * call_rcu() of the task struct, hence the _irqsave.
53 */
54 static int task_free_notify(struct notifier_block * self, unsigned long val, void * data)
55 {
56 unsigned long flags;
57 struct task_struct * task = data;
58 spin_lock_irqsave(&task_mortuary, flags);
59 list_add(&task->tasks, &dying_tasks);
60 spin_unlock_irqrestore(&task_mortuary, flags);
61 return NOTIFY_OK;
62 }
65 /* The task is on its way out. A sync of the buffer means we can catch
66 * any remaining samples for this task.
67 */
68 static int task_exit_notify(struct notifier_block * self, unsigned long val, void * data)
69 {
70 /* To avoid latency problems, we only process the current CPU,
71 * hoping that most samples for the task are on this CPU
72 */
73 sync_buffer(raw_smp_processor_id());
74 return 0;
75 }
78 /* The task is about to try a do_munmap(). We peek at what it's going to
79 * do, and if it's an executable region, process the samples first, so
80 * we don't lose any. This does not have to be exact, it's a QoI issue
81 * only.
82 */
83 static int munmap_notify(struct notifier_block * self, unsigned long val, void * data)
84 {
85 unsigned long addr = (unsigned long)data;
86 struct mm_struct * mm = current->mm;
87 struct vm_area_struct * mpnt;
89 down_read(&mm->mmap_sem);
91 mpnt = find_vma(mm, addr);
92 if (mpnt && mpnt->vm_file && (mpnt->vm_flags & VM_EXEC)) {
93 up_read(&mm->mmap_sem);
94 /* To avoid latency problems, we only process the current CPU,
95 * hoping that most samples for the task are on this CPU
96 */
97 sync_buffer(raw_smp_processor_id());
98 return 0;
99 }
101 up_read(&mm->mmap_sem);
102 return 0;
103 }
106 /* We need to be told about new modules so we don't attribute to a previously
107 * loaded module, or drop the samples on the floor.
108 */
109 static int module_load_notify(struct notifier_block * self, unsigned long val, void * data)
110 {
111 #ifdef CONFIG_MODULES
112 if (val != MODULE_STATE_COMING)
113 return 0;
115 /* FIXME: should we process all CPU buffers ? */
116 mutex_lock(&buffer_mutex);
117 add_event_entry(ESCAPE_CODE);
118 add_event_entry(MODULE_LOADED_CODE);
119 mutex_unlock(&buffer_mutex);
120 #endif
121 return 0;
122 }
125 static struct notifier_block task_free_nb = {
126 .notifier_call = task_free_notify,
127 };
129 static struct notifier_block task_exit_nb = {
130 .notifier_call = task_exit_notify,
131 };
133 static struct notifier_block munmap_nb = {
134 .notifier_call = munmap_notify,
135 };
137 static struct notifier_block module_load_nb = {
138 .notifier_call = module_load_notify,
139 };
142 static void end_sync(void)
143 {
144 end_cpu_work();
145 /* make sure we don't leak task structs */
146 process_task_mortuary();
147 process_task_mortuary();
148 }
151 int sync_start(void)
152 {
153 int err;
154 int i;
156 for (i = 0; i < NR_CPUS; i++) {
157 cpu_current_domain[i] = COORDINATOR_DOMAIN;
158 }
160 start_cpu_work();
162 err = task_handoff_register(&task_free_nb);
163 if (err)
164 goto out1;
165 err = profile_event_register(PROFILE_TASK_EXIT, &task_exit_nb);
166 if (err)
167 goto out2;
168 err = profile_event_register(PROFILE_MUNMAP, &munmap_nb);
169 if (err)
170 goto out3;
171 err = register_module_notifier(&module_load_nb);
172 if (err)
173 goto out4;
175 out:
176 return err;
177 out4:
178 profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
179 out3:
180 profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
181 out2:
182 task_handoff_unregister(&task_free_nb);
183 out1:
184 end_sync();
185 goto out;
186 }
189 void sync_stop(void)
190 {
191 unregister_module_notifier(&module_load_nb);
192 profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
193 profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
194 task_handoff_unregister(&task_free_nb);
195 end_sync();
196 }
199 /* Optimisation. We can manage without taking the dcookie sem
200 * because we cannot reach this code without at least one
201 * dcookie user still being registered (namely, the reader
202 * of the event buffer). */
203 static inline unsigned long fast_get_dcookie(struct dentry * dentry,
204 struct vfsmount * vfsmnt)
205 {
206 unsigned long cookie;
208 if (dentry->d_cookie)
209 return (unsigned long)dentry;
210 get_dcookie(dentry, vfsmnt, &cookie);
211 return cookie;
212 }
215 /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
216 * which corresponds loosely to "application name". This is
217 * not strictly necessary but allows oprofile to associate
218 * shared-library samples with particular applications
219 */
220 static unsigned long get_exec_dcookie(struct mm_struct * mm)
221 {
222 unsigned long cookie = NO_COOKIE;
223 struct vm_area_struct * vma;
225 if (!mm)
226 goto out;
228 for (vma = mm->mmap; vma; vma = vma->vm_next) {
229 if (!vma->vm_file)
230 continue;
231 if (!(vma->vm_flags & VM_EXECUTABLE))
232 continue;
233 cookie = fast_get_dcookie(vma->vm_file->f_dentry,
234 vma->vm_file->f_vfsmnt);
235 break;
236 }
238 out:
239 return cookie;
240 }
243 /* Convert the EIP value of a sample into a persistent dentry/offset
244 * pair that can then be added to the global event buffer. We make
245 * sure to do this lookup before a mm->mmap modification happens so
246 * we don't lose track.
247 */
248 static unsigned long lookup_dcookie(struct mm_struct * mm, unsigned long addr, off_t * offset)
249 {
250 unsigned long cookie = NO_COOKIE;
251 struct vm_area_struct * vma;
253 for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
255 if (addr < vma->vm_start || addr >= vma->vm_end)
256 continue;
258 if (vma->vm_file) {
259 cookie = fast_get_dcookie(vma->vm_file->f_dentry,
260 vma->vm_file->f_vfsmnt);
261 *offset = (vma->vm_pgoff << PAGE_SHIFT) + addr -
262 vma->vm_start;
263 } else {
264 /* must be an anonymous map */
265 *offset = addr;
266 }
268 break;
269 }
271 if (!vma)
272 cookie = INVALID_COOKIE;
274 return cookie;
275 }
278 static unsigned long last_cookie = INVALID_COOKIE;
280 static void add_cpu_switch(int i)
281 {
282 add_event_entry(ESCAPE_CODE);
283 add_event_entry(CPU_SWITCH_CODE);
284 add_event_entry(i);
285 last_cookie = INVALID_COOKIE;
286 }
288 static void add_cpu_mode_switch(unsigned int cpu_mode)
289 {
290 add_event_entry(ESCAPE_CODE);
291 switch (cpu_mode) {
292 case CPU_MODE_USER:
293 add_event_entry(USER_ENTER_SWITCH_CODE);
294 break;
295 case CPU_MODE_KERNEL:
296 add_event_entry(KERNEL_ENTER_SWITCH_CODE);
297 break;
298 case CPU_MODE_XEN:
299 add_event_entry(XEN_ENTER_SWITCH_CODE);
300 break;
301 default:
302 break;
303 }
304 }
306 static void add_domain_switch(unsigned long domain_id)
307 {
308 add_event_entry(ESCAPE_CODE);
309 add_event_entry(DOMAIN_SWITCH_CODE);
310 add_event_entry(domain_id);
311 }
313 static void
314 add_user_ctx_switch(struct task_struct const * task, unsigned long cookie)
315 {
316 add_event_entry(ESCAPE_CODE);
317 add_event_entry(CTX_SWITCH_CODE);
318 add_event_entry(task->pid);
319 add_event_entry(cookie);
320 /* Another code for daemon back-compat */
321 add_event_entry(ESCAPE_CODE);
322 add_event_entry(CTX_TGID_CODE);
323 add_event_entry(task->tgid);
324 }
327 static void add_cookie_switch(unsigned long cookie)
328 {
329 add_event_entry(ESCAPE_CODE);
330 add_event_entry(COOKIE_SWITCH_CODE);
331 add_event_entry(cookie);
332 }
335 static void add_trace_begin(void)
336 {
337 add_event_entry(ESCAPE_CODE);
338 add_event_entry(TRACE_BEGIN_CODE);
339 }
342 static void add_sample_entry(unsigned long offset, unsigned long event)
343 {
344 add_event_entry(offset);
345 add_event_entry(event);
346 }
349 static int add_us_sample(struct mm_struct * mm, struct op_sample * s)
350 {
351 unsigned long cookie;
352 off_t offset;
354 cookie = lookup_dcookie(mm, s->eip, &offset);
356 if (cookie == INVALID_COOKIE) {
357 atomic_inc(&oprofile_stats.sample_lost_no_mapping);
358 return 0;
359 }
361 if (cookie != last_cookie) {
362 add_cookie_switch(cookie);
363 last_cookie = cookie;
364 }
366 add_sample_entry(offset, s->event);
368 return 1;
369 }
372 /* Add a sample to the global event buffer. If possible the
373 * sample is converted into a persistent dentry/offset pair
374 * for later lookup from userspace.
375 */
376 static int
377 add_sample(struct mm_struct * mm, struct op_sample * s, int cpu_mode)
378 {
379 if (cpu_mode >= CPU_MODE_KERNEL) {
380 add_sample_entry(s->eip, s->event);
381 return 1;
382 } else if (mm) {
383 return add_us_sample(mm, s);
384 } else {
385 atomic_inc(&oprofile_stats.sample_lost_no_mm);
386 }
387 return 0;
388 }
391 static void release_mm(struct mm_struct * mm)
392 {
393 if (!mm)
394 return;
395 up_read(&mm->mmap_sem);
396 mmput(mm);
397 }
400 static struct mm_struct * take_tasks_mm(struct task_struct * task)
401 {
402 struct mm_struct * mm = get_task_mm(task);
403 if (mm)
404 down_read(&mm->mmap_sem);
405 return mm;
406 }
409 static inline int is_code(unsigned long val)
410 {
411 return val == ESCAPE_CODE;
412 }
415 /* "acquire" as many cpu buffer slots as we can */
416 static unsigned long get_slots(struct oprofile_cpu_buffer * b)
417 {
418 unsigned long head = b->head_pos;
419 unsigned long tail = b->tail_pos;
421 /*
422 * Subtle. This resets the persistent last_task
423 * and in_kernel values used for switching notes.
424 * BUT, there is a small window between reading
425 * head_pos, and this call, that means samples
426 * can appear at the new head position, but not
427 * be prefixed with the notes for switching
428 * kernel mode or a task switch. This small hole
429 * can lead to mis-attribution or samples where
430 * we don't know if it's in the kernel or not,
431 * at the start of an event buffer.
432 */
433 cpu_buffer_reset(b);
435 if (head >= tail)
436 return head - tail;
438 return head + (b->buffer_size - tail);
439 }
442 static void increment_tail(struct oprofile_cpu_buffer * b)
443 {
444 unsigned long new_tail = b->tail_pos + 1;
446 rmb();
448 if (new_tail < b->buffer_size)
449 b->tail_pos = new_tail;
450 else
451 b->tail_pos = 0;
452 }
455 /* Move tasks along towards death. Any tasks on dead_tasks
456 * will definitely have no remaining references in any
457 * CPU buffers at this point, because we use two lists,
458 * and to have reached the list, it must have gone through
459 * one full sync already.
460 */
461 static void process_task_mortuary(void)
462 {
463 unsigned long flags;
464 LIST_HEAD(local_dead_tasks);
465 struct task_struct * task;
466 struct task_struct * ttask;
468 spin_lock_irqsave(&task_mortuary, flags);
470 list_splice_init(&dead_tasks, &local_dead_tasks);
471 list_splice_init(&dying_tasks, &dead_tasks);
473 spin_unlock_irqrestore(&task_mortuary, flags);
475 list_for_each_entry_safe(task, ttask, &local_dead_tasks, tasks) {
476 list_del(&task->tasks);
477 free_task(task);
478 }
479 }
482 static void mark_done(int cpu)
483 {
484 int i;
486 cpu_set(cpu, marked_cpus);
488 for_each_online_cpu(i) {
489 if (!cpu_isset(i, marked_cpus))
490 return;
491 }
493 /* All CPUs have been processed at least once,
494 * we can process the mortuary once
495 */
496 process_task_mortuary();
498 cpus_clear(marked_cpus);
499 }
502 /* FIXME: this is not sufficient if we implement syscall barrier backtrace
503 * traversal, the code switch to sb_sample_start at first kernel enter/exit
504 * switch so we need a fifth state and some special handling in sync_buffer()
505 */
506 typedef enum {
507 sb_bt_ignore = -2,
508 sb_buffer_start,
509 sb_bt_start,
510 sb_sample_start,
511 } sync_buffer_state;
513 /* Sync one of the CPU's buffers into the global event buffer.
514 * Here we need to go through each batch of samples punctuated
515 * by context switch notes, taking the task's mmap_sem and doing
516 * lookup in task->mm->mmap to convert EIP into dcookie/offset
517 * value.
518 */
519 void sync_buffer(int cpu)
520 {
521 struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[cpu];
522 struct mm_struct *mm = NULL;
523 struct task_struct * new;
524 unsigned long cookie = 0;
525 int cpu_mode = 1;
526 unsigned int i;
527 sync_buffer_state state = sb_buffer_start;
528 unsigned long available;
529 int domain_switch = 0;
531 mutex_lock(&buffer_mutex);
533 add_cpu_switch(cpu);
535 /* We need to assign the first samples in this CPU buffer to the
536 same domain that we were processing at the last sync_buffer */
537 if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN) {
538 add_domain_switch(cpu_current_domain[cpu]);
539 }
540 /* Remember, only we can modify tail_pos */
542 available = get_slots(cpu_buf);
544 for (i = 0; i < available; ++i) {
545 struct op_sample * s = &cpu_buf->buffer[cpu_buf->tail_pos];
547 if (is_code(s->eip) && !domain_switch) {
548 if (s->event <= CPU_MODE_XEN) {
549 /* xen/kernel/userspace switch */
550 cpu_mode = s->event;
551 if (state == sb_buffer_start)
552 state = sb_sample_start;
553 add_cpu_mode_switch(s->event);
554 } else if (s->event == CPU_TRACE_BEGIN) {
555 state = sb_bt_start;
556 add_trace_begin();
557 } else if (s->event == CPU_DOMAIN_SWITCH) {
558 domain_switch = 1;
559 } else {
560 struct mm_struct * oldmm = mm;
562 /* userspace context switch */
563 new = (struct task_struct *)s->event;
565 release_mm(oldmm);
566 mm = take_tasks_mm(new);
567 if (mm != oldmm)
568 cookie = get_exec_dcookie(mm);
569 add_user_ctx_switch(new, cookie);
570 }
571 } else {
572 if (domain_switch) {
573 cpu_current_domain[cpu] = s->eip;
574 add_domain_switch(s->eip);
575 domain_switch = 0;
576 } else {
577 if (cpu_current_domain[cpu] !=
578 COORDINATOR_DOMAIN) {
579 add_sample_entry(s->eip, s->event);
580 }
581 else if (state >= sb_bt_start &&
582 !add_sample(mm, s, cpu_mode)) {
583 if (state == sb_bt_start) {
584 state = sb_bt_ignore;
585 atomic_inc(&oprofile_stats.bt_lost_no_mapping);
586 }
587 }
588 }
589 }
591 increment_tail(cpu_buf);
592 }
593 release_mm(mm);
595 /* We reset domain to COORDINATOR at each CPU switch */
596 if (cpu_current_domain[cpu] != COORDINATOR_DOMAIN) {
597 add_domain_switch(COORDINATOR_DOMAIN);
598 }
600 mark_done(cpu);
602 mutex_unlock(&buffer_mutex);
603 }