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