buffer_sync.c 13 KB

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