buffer_sync.c 13 KB

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