buffer_sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. * 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.
  13. *
  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.
  17. *
  18. * See fs/dcookies.c for a description of the dentry/offset
  19. * objects.
  20. */
  21. #include <linux/mm.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/notifier.h>
  24. #include <linux/dcookies.h>
  25. #include <linux/profile.h>
  26. #include <linux/module.h>
  27. #include <linux/fs.h>
  28. #include "oprofile_stats.h"
  29. #include "event_buffer.h"
  30. #include "cpu_buffer.h"
  31. #include "buffer_sync.h"
  32. static LIST_HEAD(dying_tasks);
  33. static LIST_HEAD(dead_tasks);
  34. static cpumask_t marked_cpus = CPU_MASK_NONE;
  35. static DEFINE_SPINLOCK(task_mortuary);
  36. static void process_task_mortuary(void);
  37. /* Take ownership of the task struct and place it on the
  38. * list for processing. Only after two full buffer syncs
  39. * does the task eventually get freed, because by then
  40. * we are sure we will not reference it again.
  41. */
  42. static int task_free_notify(struct notifier_block * self, unsigned long val, void * data)
  43. {
  44. struct task_struct * task = data;
  45. spin_lock(&task_mortuary);
  46. list_add(&task->tasks, &dying_tasks);
  47. spin_unlock(&task_mortuary);
  48. return NOTIFY_OK;
  49. }
  50. /* The task is on its way out. A sync of the buffer means we can catch
  51. * any remaining samples for this task.
  52. */
  53. static int task_exit_notify(struct notifier_block * self, unsigned long val, void * data)
  54. {
  55. /* To avoid latency problems, we only process the current CPU,
  56. * hoping that most samples for the task are on this CPU
  57. */
  58. sync_buffer(raw_smp_processor_id());
  59. return 0;
  60. }
  61. /* The task is about to try a do_munmap(). We peek at what it's going to
  62. * do, and if it's an executable region, process the samples first, so
  63. * we don't lose any. This does not have to be exact, it's a QoI issue
  64. * only.
  65. */
  66. static int munmap_notify(struct notifier_block * self, unsigned long val, void * data)
  67. {
  68. unsigned long addr = (unsigned long)data;
  69. struct mm_struct * mm = current->mm;
  70. struct vm_area_struct * mpnt;
  71. down_read(&mm->mmap_sem);
  72. mpnt = find_vma(mm, addr);
  73. if (mpnt && mpnt->vm_file && (mpnt->vm_flags & VM_EXEC)) {
  74. up_read(&mm->mmap_sem);
  75. /* To avoid latency problems, we only process the current CPU,
  76. * hoping that most samples for the task are on this CPU
  77. */
  78. sync_buffer(raw_smp_processor_id());
  79. return 0;
  80. }
  81. up_read(&mm->mmap_sem);
  82. return 0;
  83. }
  84. /* We need to be told about new modules so we don't attribute to a previously
  85. * loaded module, or drop the samples on the floor.
  86. */
  87. static int module_load_notify(struct notifier_block * self, unsigned long val, void * data)
  88. {
  89. #ifdef CONFIG_MODULES
  90. if (val != MODULE_STATE_COMING)
  91. return 0;
  92. /* FIXME: should we process all CPU buffers ? */
  93. down(&buffer_sem);
  94. add_event_entry(ESCAPE_CODE);
  95. add_event_entry(MODULE_LOADED_CODE);
  96. up(&buffer_sem);
  97. #endif
  98. return 0;
  99. }
  100. static struct notifier_block task_free_nb = {
  101. .notifier_call = task_free_notify,
  102. };
  103. static struct notifier_block task_exit_nb = {
  104. .notifier_call = task_exit_notify,
  105. };
  106. static struct notifier_block munmap_nb = {
  107. .notifier_call = munmap_notify,
  108. };
  109. static struct notifier_block module_load_nb = {
  110. .notifier_call = module_load_notify,
  111. };
  112. static void end_sync(void)
  113. {
  114. end_cpu_work();
  115. /* make sure we don't leak task structs */
  116. process_task_mortuary();
  117. process_task_mortuary();
  118. }
  119. int sync_start(void)
  120. {
  121. int err;
  122. start_cpu_work();
  123. err = task_handoff_register(&task_free_nb);
  124. if (err)
  125. goto out1;
  126. err = profile_event_register(PROFILE_TASK_EXIT, &task_exit_nb);
  127. if (err)
  128. goto out2;
  129. err = profile_event_register(PROFILE_MUNMAP, &munmap_nb);
  130. if (err)
  131. goto out3;
  132. err = register_module_notifier(&module_load_nb);
  133. if (err)
  134. goto out4;
  135. out:
  136. return err;
  137. out4:
  138. profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
  139. out3:
  140. profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
  141. out2:
  142. task_handoff_unregister(&task_free_nb);
  143. out1:
  144. end_sync();
  145. goto out;
  146. }
  147. void sync_stop(void)
  148. {
  149. unregister_module_notifier(&module_load_nb);
  150. profile_event_unregister(PROFILE_MUNMAP, &munmap_nb);
  151. profile_event_unregister(PROFILE_TASK_EXIT, &task_exit_nb);
  152. task_handoff_unregister(&task_free_nb);
  153. end_sync();
  154. }
  155. /* Optimisation. We can manage without taking the dcookie sem
  156. * because we cannot reach this code without at least one
  157. * dcookie user still being registered (namely, the reader
  158. * of the event buffer). */
  159. static inline unsigned long fast_get_dcookie(struct dentry * dentry,
  160. struct vfsmount * vfsmnt)
  161. {
  162. unsigned long cookie;
  163. if (dentry->d_cookie)
  164. return (unsigned long)dentry;
  165. get_dcookie(dentry, vfsmnt, &cookie);
  166. return cookie;
  167. }
  168. /* Look up the dcookie for the task's first VM_EXECUTABLE mapping,
  169. * which corresponds loosely to "application name". This is
  170. * not strictly necessary but allows oprofile to associate
  171. * shared-library samples with particular applications
  172. */
  173. static unsigned long get_exec_dcookie(struct mm_struct * mm)
  174. {
  175. unsigned long cookie = NO_COOKIE;
  176. struct vm_area_struct * vma;
  177. if (!mm)
  178. goto out;
  179. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  180. if (!vma->vm_file)
  181. continue;
  182. if (!(vma->vm_flags & VM_EXECUTABLE))
  183. continue;
  184. cookie = fast_get_dcookie(vma->vm_file->f_dentry,
  185. vma->vm_file->f_vfsmnt);
  186. break;
  187. }
  188. out:
  189. return cookie;
  190. }
  191. /* Convert the EIP value of a sample into a persistent dentry/offset
  192. * pair that can then be added to the global event buffer. We make
  193. * sure to do this lookup before a mm->mmap modification happens so
  194. * we don't lose track.
  195. */
  196. static unsigned long lookup_dcookie(struct mm_struct * mm, unsigned long addr, off_t * offset)
  197. {
  198. unsigned long cookie = NO_COOKIE;
  199. struct vm_area_struct * vma;
  200. for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
  201. if (addr < vma->vm_start || addr >= vma->vm_end)
  202. continue;
  203. if (vma->vm_file) {
  204. cookie = fast_get_dcookie(vma->vm_file->f_dentry,
  205. vma->vm_file->f_vfsmnt);
  206. *offset = (vma->vm_pgoff << PAGE_SHIFT) + addr -
  207. vma->vm_start;
  208. } else {
  209. /* must be an anonymous map */
  210. *offset = addr;
  211. }
  212. break;
  213. }
  214. if (!vma)
  215. cookie = INVALID_COOKIE;
  216. return cookie;
  217. }
  218. static unsigned long last_cookie = INVALID_COOKIE;
  219. static void add_cpu_switch(int i)
  220. {
  221. add_event_entry(ESCAPE_CODE);
  222. add_event_entry(CPU_SWITCH_CODE);
  223. add_event_entry(i);
  224. last_cookie = INVALID_COOKIE;
  225. }
  226. static void add_kernel_ctx_switch(unsigned int in_kernel)
  227. {
  228. add_event_entry(ESCAPE_CODE);
  229. if (in_kernel)
  230. add_event_entry(KERNEL_ENTER_SWITCH_CODE);
  231. else
  232. add_event_entry(KERNEL_EXIT_SWITCH_CODE);
  233. }
  234. static void
  235. add_user_ctx_switch(struct task_struct const * task, unsigned long cookie)
  236. {
  237. add_event_entry(ESCAPE_CODE);
  238. add_event_entry(CTX_SWITCH_CODE);
  239. add_event_entry(task->pid);
  240. add_event_entry(cookie);
  241. /* Another code for daemon back-compat */
  242. add_event_entry(ESCAPE_CODE);
  243. add_event_entry(CTX_TGID_CODE);
  244. add_event_entry(task->tgid);
  245. }
  246. static void add_cookie_switch(unsigned long cookie)
  247. {
  248. add_event_entry(ESCAPE_CODE);
  249. add_event_entry(COOKIE_SWITCH_CODE);
  250. add_event_entry(cookie);
  251. }
  252. static void add_trace_begin(void)
  253. {
  254. add_event_entry(ESCAPE_CODE);
  255. add_event_entry(TRACE_BEGIN_CODE);
  256. }
  257. static void add_sample_entry(unsigned long offset, unsigned long event)
  258. {
  259. add_event_entry(offset);
  260. add_event_entry(event);
  261. }
  262. static int add_us_sample(struct mm_struct * mm, struct op_sample * s)
  263. {
  264. unsigned long cookie;
  265. off_t offset;
  266. cookie = lookup_dcookie(mm, s->eip, &offset);
  267. if (cookie == INVALID_COOKIE) {
  268. atomic_inc(&oprofile_stats.sample_lost_no_mapping);
  269. return 0;
  270. }
  271. if (cookie != last_cookie) {
  272. add_cookie_switch(cookie);
  273. last_cookie = cookie;
  274. }
  275. add_sample_entry(offset, s->event);
  276. return 1;
  277. }
  278. /* Add a sample to the global event buffer. If possible the
  279. * sample is converted into a persistent dentry/offset pair
  280. * for later lookup from userspace.
  281. */
  282. static int
  283. add_sample(struct mm_struct * mm, struct op_sample * s, int in_kernel)
  284. {
  285. if (in_kernel) {
  286. add_sample_entry(s->eip, s->event);
  287. return 1;
  288. } else if (mm) {
  289. return add_us_sample(mm, s);
  290. } else {
  291. atomic_inc(&oprofile_stats.sample_lost_no_mm);
  292. }
  293. return 0;
  294. }
  295. static void release_mm(struct mm_struct * mm)
  296. {
  297. if (!mm)
  298. return;
  299. up_read(&mm->mmap_sem);
  300. mmput(mm);
  301. }
  302. static struct mm_struct * take_tasks_mm(struct task_struct * task)
  303. {
  304. struct mm_struct * mm = get_task_mm(task);
  305. if (mm)
  306. down_read(&mm->mmap_sem);
  307. return mm;
  308. }
  309. static inline int is_code(unsigned long val)
  310. {
  311. return val == ESCAPE_CODE;
  312. }
  313. /* "acquire" as many cpu buffer slots as we can */
  314. static unsigned long get_slots(struct oprofile_cpu_buffer * b)
  315. {
  316. unsigned long head = b->head_pos;
  317. unsigned long tail = b->tail_pos;
  318. /*
  319. * Subtle. This resets the persistent last_task
  320. * and in_kernel values used for switching notes.
  321. * BUT, there is a small window between reading
  322. * head_pos, and this call, that means samples
  323. * can appear at the new head position, but not
  324. * be prefixed with the notes for switching
  325. * kernel mode or a task switch. This small hole
  326. * can lead to mis-attribution or samples where
  327. * we don't know if it's in the kernel or not,
  328. * at the start of an event buffer.
  329. */
  330. cpu_buffer_reset(b);
  331. if (head >= tail)
  332. return head - tail;
  333. return head + (b->buffer_size - tail);
  334. }
  335. static void increment_tail(struct oprofile_cpu_buffer * b)
  336. {
  337. unsigned long new_tail = b->tail_pos + 1;
  338. rmb();
  339. if (new_tail < b->buffer_size)
  340. b->tail_pos = new_tail;
  341. else
  342. b->tail_pos = 0;
  343. }
  344. /* Move tasks along towards death. Any tasks on dead_tasks
  345. * will definitely have no remaining references in any
  346. * CPU buffers at this point, because we use two lists,
  347. * and to have reached the list, it must have gone through
  348. * one full sync already.
  349. */
  350. static void process_task_mortuary(void)
  351. {
  352. struct list_head * pos;
  353. struct list_head * pos2;
  354. struct task_struct * task;
  355. spin_lock(&task_mortuary);
  356. list_for_each_safe(pos, pos2, &dead_tasks) {
  357. task = list_entry(pos, struct task_struct, tasks);
  358. list_del(&task->tasks);
  359. free_task(task);
  360. }
  361. list_for_each_safe(pos, pos2, &dying_tasks) {
  362. task = list_entry(pos, struct task_struct, tasks);
  363. list_del(&task->tasks);
  364. list_add_tail(&task->tasks, &dead_tasks);
  365. }
  366. spin_unlock(&task_mortuary);
  367. }
  368. static void mark_done(int cpu)
  369. {
  370. int i;
  371. cpu_set(cpu, marked_cpus);
  372. for_each_online_cpu(i) {
  373. if (!cpu_isset(i, marked_cpus))
  374. return;
  375. }
  376. /* All CPUs have been processed at least once,
  377. * we can process the mortuary once
  378. */
  379. process_task_mortuary();
  380. cpus_clear(marked_cpus);
  381. }
  382. /* FIXME: this is not sufficient if we implement syscall barrier backtrace
  383. * traversal, the code switch to sb_sample_start at first kernel enter/exit
  384. * switch so we need a fifth state and some special handling in sync_buffer()
  385. */
  386. typedef enum {
  387. sb_bt_ignore = -2,
  388. sb_buffer_start,
  389. sb_bt_start,
  390. sb_sample_start,
  391. } sync_buffer_state;
  392. /* Sync one of the CPU's buffers into the global event buffer.
  393. * Here we need to go through each batch of samples punctuated
  394. * by context switch notes, taking the task's mmap_sem and doing
  395. * lookup in task->mm->mmap to convert EIP into dcookie/offset
  396. * value.
  397. */
  398. void sync_buffer(int cpu)
  399. {
  400. struct oprofile_cpu_buffer * cpu_buf = &cpu_buffer[cpu];
  401. struct mm_struct *mm = NULL;
  402. struct task_struct * new;
  403. unsigned long cookie = 0;
  404. int in_kernel = 1;
  405. unsigned int i;
  406. sync_buffer_state state = sb_buffer_start;
  407. unsigned long available;
  408. down(&buffer_sem);
  409. add_cpu_switch(cpu);
  410. /* Remember, only we can modify tail_pos */
  411. available = get_slots(cpu_buf);
  412. for (i = 0; i < available; ++i) {
  413. struct op_sample * s = &cpu_buf->buffer[cpu_buf->tail_pos];
  414. if (is_code(s->eip)) {
  415. if (s->event <= CPU_IS_KERNEL) {
  416. /* kernel/userspace switch */
  417. in_kernel = s->event;
  418. if (state == sb_buffer_start)
  419. state = sb_sample_start;
  420. add_kernel_ctx_switch(s->event);
  421. } else if (s->event == CPU_TRACE_BEGIN) {
  422. state = sb_bt_start;
  423. add_trace_begin();
  424. } else {
  425. struct mm_struct * oldmm = mm;
  426. /* userspace context switch */
  427. new = (struct task_struct *)s->event;
  428. release_mm(oldmm);
  429. mm = take_tasks_mm(new);
  430. if (mm != oldmm)
  431. cookie = get_exec_dcookie(mm);
  432. add_user_ctx_switch(new, cookie);
  433. }
  434. } else {
  435. if (state >= sb_bt_start &&
  436. !add_sample(mm, s, in_kernel)) {
  437. if (state == sb_bt_start) {
  438. state = sb_bt_ignore;
  439. atomic_inc(&oprofile_stats.bt_lost_no_mapping);
  440. }
  441. }
  442. }
  443. increment_tail(cpu_buf);
  444. }
  445. release_mm(mm);
  446. mark_done(cpu);
  447. up(&buffer_sem);
  448. }